Buckets:

rtrm's picture
download
raw
32.9 kB
<meta charset="utf-8" /><meta http-equiv="content-security-policy" content=""><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;bentoml-integration-guide&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;prerequisites&quot;,&quot;title&quot;:&quot;Prerequisites&quot;},{&quot;local&quot;:&quot;import-a-diffusion-model&quot;,&quot;title&quot;:&quot;Import a diffusion model&quot;},{&quot;local&quot;:&quot;turn-a-diffusion-model-into-a-restful-service-with-bentoml&quot;,&quot;title&quot;:&quot;Turn a diffusion model into a RESTful service with BentoML&quot;},{&quot;local&quot;:&quot;package-a-bentoml-service-for-cloud-deployment&quot;,&quot;title&quot;:&quot;Package a BentoML Service for cloud deployment&quot;}],&quot;title&quot;:&quot;BentoML Integration Guide&quot;}" data-svelte="svelte-1phssyn">
<link rel="modulepreload" href="/docs/diffusers/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css">
<link rel="modulepreload" href="/docs/diffusers/main/en/_app/start-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/main/en/_app/chunks/vendor-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/main/en/_app/chunks/paths-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/main/en/_app/pages/__layout.svelte-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/main/en/_app/pages/optimization/bentoml.mdx-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/main/en/_app/chunks/IconCopyLink-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/main/en/_app/chunks/CodeBlock-hf-doc-builder.js">
<link rel="modulepreload" href="/docs/diffusers/main/en/_app/chunks/DocNotebookDropdown-hf-doc-builder.js">
<h1 class="relative group"><a id="bentoml-integration-guide" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#bentoml-integration-guide"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a>
<span>BentoML Integration Guide
</span></h1>
<div class="flex space-x-1 absolute z-10 right-0 top-0">
<div class="relative colab-dropdown ">
<button class=" " type="button">
<img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg">
</button>
</div>
<div class="relative colab-dropdown ">
<button class=" " type="button">
<img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg">
</button>
</div></div>
<p><a href="https://github.com/bentoml/BentoML/" rel="nofollow">BentoML</a> is an open-source framework designed for building,
shipping, and scaling AI applications. It allows users to easily package and serve diffusion models
for production, ensuring reliable and efficient deployments. It features out-of-the-box operational
management tools like monitoring and tracing, and facilitates the deployment to various cloud platforms
with ease. BentoML’s distributed architecture and the separation of API server logic from
model inference logic enable efficient scaling of deployments, even with budget constraints.
As a result, integrating it with Diffusers provides a valuable tool for real-world deployments.</p>
<p>This tutorial demonstrates how to integrate BentoML with Diffusers.</p>
<h2 class="relative group"><a id="prerequisites" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#prerequisites"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a>
<span>Prerequisites
</span></h2>
<ul><li>Install <a href="https://huggingface.co/docs/diffusers/installation" rel="nofollow">Diffusers</a>.</li>
<li>Install BentoML by running <code>pip install bentoml</code>. For more information, see the <a href="https://docs.bentoml.com" rel="nofollow">BentoML documentation</a>.</li></ul>
<h2 class="relative group"><a id="import-a-diffusion-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#import-a-diffusion-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a>
<span>Import a diffusion model
</span></h2>
<p>First, you need to prepare the model. BentoML has its own <a href="https://docs.bentoml.com/en/latest/concepts/model.html" rel="nofollow">Model Store</a>
for model management. Create a <code>download_model.py</code> file as below to import a diffusion model into BentoML’s Model
Store:</p>
<div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg>
<div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> bentoml
bentoml.diffusers.import_model(
<span class="hljs-string">&quot;sd2.1&quot;</span>, <span class="hljs-comment"># Model tag in the BentoML Model Store</span>
<span class="hljs-string">&quot;stabilityai/stable-diffusion-2-1&quot;</span>, <span class="hljs-comment"># Hugging Face model identifier</span>
)<!-- HTML_TAG_END --></pre></div>
<p>This code snippet downloads the Stable Diffusion 2.1 model (using it’s repo id
<code>stabilityai/stable-diffusion-2-1</code>) from the Hugging Face Hub (or use the cached download
files if the model is already downloaded) and imports it into the BentoML Model
Store with the name <code>sd2.1</code>.</p>
<p>For models already fine-tuned and stored on disk, you can provide the path instead of
the repo id.</p>
<div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg>
<div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> bentoml
bentoml.diffusers.import_model(
<span class="hljs-string">&quot;sd2.1-local&quot;</span>,
<span class="hljs-string">&quot;./local_stable_diffusion_2.1/&quot;</span>,
)<!-- HTML_TAG_END --></pre></div>
<p>You can view the model in the Model Store:</p>
<div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg>
<div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-attribute">bentoml</span> models list
<span class="hljs-attribute">Tag</span> Module Size Creation Time
<span class="hljs-attribute">sd2</span>.<span class="hljs-number">1</span>:ysrlmubascajwnry bentoml.diffusers <span class="hljs-number">33</span>.<span class="hljs-number">85</span> GiB <span class="hljs-number">2023</span>-<span class="hljs-number">07</span>-<span class="hljs-number">12</span> <span class="hljs-number">16</span>:<span class="hljs-number">47</span>:<span class="hljs-number">44</span> <!-- HTML_TAG_END --></pre></div>
<h2 class="relative group"><a id="turn-a-diffusion-model-into-a-restful-service-with-bentoml" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#turn-a-diffusion-model-into-a-restful-service-with-bentoml"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a>
<span>Turn a diffusion model into a RESTful service with BentoML
</span></h2>
<p>Once the diffusion model is in BentoML’s Model Store, you can implement a text-to-image
service with it. The Stable Diffusion model accepts various arguments
in addition to the required prompt to guide the image generation process.
To validate these input arguments, use BentoML’s <a href="https://github.com/pydantic/pydantic" rel="nofollow">pydantic</a> integration.
Create a <code>sdargs.py</code> file with an example pydantic model:</p>
<div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg>
<div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> typing <span class="hljs-keyword">as</span> t
<span class="hljs-keyword">from</span> pydantic <span class="hljs-keyword">import</span> BaseModel
<span class="hljs-keyword">class</span> <span class="hljs-title class_">SDArgs</span>(<span class="hljs-title class_ inherited__">BaseModel</span>):
prompt: <span class="hljs-built_in">str</span>
negative_prompt: t.<span class="hljs-type">Optional</span>[<span class="hljs-built_in">str</span>] = <span class="hljs-literal">None</span>
height: t.<span class="hljs-type">Optional</span>[<span class="hljs-built_in">int</span>] = <span class="hljs-number">512</span>
width: t.<span class="hljs-type">Optional</span>[<span class="hljs-built_in">int</span>] = <span class="hljs-number">512</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Config</span>:
extra = <span class="hljs-string">&quot;allow&quot;</span><!-- HTML_TAG_END --></pre></div>
<p>This pydantic model requires a string field <code>prompt</code> and three optional fields: <code>height</code>, <code>width</code>, and <code>negative_prompt</code>,
each with corresponding types. The <code>extra = &quot;allow&quot;</code> line supports adding additional fields not defined in the <code>SDArgs</code> class.
In a real-world scenario, you may define all the desired fields and not allow extra ones.</p>
<p>Next, create a BentoML Service file that defines a Stable Diffusion service:</p>
<div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg>
<div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> bentoml
<span class="hljs-keyword">from</span> bentoml.io <span class="hljs-keyword">import</span> Image, JSON
<span class="hljs-keyword">from</span> sdargs <span class="hljs-keyword">import</span> SDArgs
bento_model = bentoml.diffusers.get(<span class="hljs-string">&quot;sd2.1:latest&quot;</span>)
sd21_runner = bento_model.to_runner(name=<span class="hljs-string">&quot;sd21-runner&quot;</span>)
svc = bentoml.Service(<span class="hljs-string">&quot;stable-diffusion-21&quot;</span>, runners=[sd21_runner])
<span class="hljs-meta">@svc.api(<span class="hljs-params"><span class="hljs-built_in">input</span>=JSON(<span class="hljs-params">pydantic_model=SDArgs</span>), output=Image(<span class="hljs-params"></span>)</span>)</span>
<span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">txt2img</span>(<span class="hljs-params">input_data</span>):
kwargs = input_data.<span class="hljs-built_in">dict</span>()
res = <span class="hljs-keyword">await</span> sd21_runner.async_run(**kwargs)
images = res[<span class="hljs-number">0</span>]
<span class="hljs-keyword">return</span> images[<span class="hljs-number">0</span>]<!-- HTML_TAG_END --></pre></div>
<p>Save the file as <code>service.py</code>, and spin up a BentoML Service endpoint using:</p>
<div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg>
<div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-keyword">bentoml </span>serve service:svc<!-- HTML_TAG_END --></pre></div>
<p>An HTTP server with <code>/txt2img</code> endpoint that accepts a JSON dictionary should be up at
port 3000. Go to &lt;<a href="http://127.0.0.1:3000%3E" rel="nofollow">http://127.0.0.1:3000&gt;</a> in your web browser to access the Swagger UI.</p>
<p>You can also test the text-to-image generation using <code>curl</code> and write the returned image to
<code>output.jpg</code>.</p>
<div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg>
<div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div>
Copied</div></button></div>
<pre><!-- HTML_TAG_START -->curl <span class="hljs-operator">-</span><span class="hljs-type">X</span> <span class="hljs-type">POST</span> http:<span class="hljs-comment">//127.0.0.1:3000/txt2img \</span>
<span class="hljs-operator">-</span><span class="hljs-type">H</span> &#x27;<span class="hljs-type">Content</span><span class="hljs-operator">-</span><span class="hljs-type">Type</span>: application<span class="hljs-operator">/</span>json&#x27; \
<span class="hljs-operator">-</span>d <span class="hljs-string">&quot;{<span class="hljs-subst">\&quot;</span>prompt<span class="hljs-subst">\&quot;</span>:<span class="hljs-subst">\&quot;</span>a black cat<span class="hljs-subst">\&quot;</span>, <span class="hljs-subst">\&quot;</span>height<span class="hljs-subst">\&quot;</span>:768, <span class="hljs-subst">\&quot;</span>width<span class="hljs-subst">\&quot;</span>:768}&quot;</span> \
<span class="hljs-operator">--</span>output output.jpg<!-- HTML_TAG_END --></pre></div>
<h2 class="relative group"><a id="package-a-bentoml-service-for-cloud-deployment" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#package-a-bentoml-service-for-cloud-deployment"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a>
<span>Package a BentoML Service for cloud deployment
</span></h2>
<p>To deploy a BentoML Service, you need to pack it into a BentoML
<a href="https://docs.bentoml.com/en/latest/concepts/bento.html" rel="nofollow">Bento</a>, a file archive with all the source code,
models, data files, and dependencies. This can be done by providing a <code>bentofile.yaml</code> file as follows:</p>
<div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg>
<div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-attr">service:</span> <span class="hljs-string">&quot;service.py:svc&quot;</span>
<span class="hljs-attr">include:</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">&quot;service.py&quot;</span>
<span class="hljs-attr">python:</span>
<span class="hljs-attr">packages:</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">torch</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">transformers</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">accelerate</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">diffusers</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">triton</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">xformers</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">pydantic</span>
<span class="hljs-attr">docker:</span>
<span class="hljs-attr">distro:</span> <span class="hljs-string">debian</span>
<span class="hljs-attr">cuda_version:</span> <span class="hljs-string">&quot;11.6&quot;</span><!-- HTML_TAG_END --></pre></div>
<p>The <code>bentofile.yaml</code> file contains <a href="https://docs.bentoml.com/en/latest/concepts/bento.html#bento-build-options" rel="nofollow">Bento build
options</a>,
such as package dependencies and Docker options.</p>
<p>Then you build a Bento using:</p>
<div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg>
<div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-attribute">bentoml build</span><!-- HTML_TAG_END --></pre></div>
<p>The output looks like:</p>
<div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg>
<div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div>
Copied</div></button></div>
<pre><!-- HTML_TAG_START -->Successfully <span class="hljs-keyword">built </span><span class="hljs-keyword">Bento(tag=&quot;stable-diffusion-21:crkuh7a7rw5bcasc&quot;).
</span>
Possible next steps:
* Containerize your <span class="hljs-keyword">Bento </span>with `<span class="hljs-keyword">bentoml </span>containerize`:
$ <span class="hljs-keyword">bentoml </span>containerize stable-<span class="hljs-keyword">diffusion-21:crkuh7a7rw5bcasc
</span>
* Push to <span class="hljs-keyword">BentoCloud </span>with `<span class="hljs-keyword">bentoml </span>push`:
$ <span class="hljs-keyword">bentoml </span>push stable-<span class="hljs-keyword">diffusion-21:crkuh7a7rw5bcasc</span><!-- HTML_TAG_END --></pre></div>
<p>You can create a Docker image based on the Bento by running the following command and deploy it to a cloud provider.</p>
<div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg>
<div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div>
Copied</div></button></div>
<pre><!-- HTML_TAG_START --><span class="hljs-attribute">bentoml</span> containerize stable-diffusion-<span class="hljs-number">21</span>:crkuh7a7rw5bcasc<!-- HTML_TAG_END --></pre></div>
<p>If you want an end-to-end solution for deploying and managing models, you can push the Bento to <a href="https://github.com/bentoml/Yatai" rel="nofollow">Yatai</a> or
<a href="https://bentoml.com/cloud" rel="nofollow">BentoCloud</a> for a distributed deployment.</p>
<p>For more information about BentoML’s integration with Diffusers, see the <a href="https://docs.bentoml.com/en/latest/frameworks/diffusers.html" rel="nofollow">BentoML Diffusers
Guide</a>.</p>
<script type="module" data-hydrate="u9oox0">
import { start } from "/docs/diffusers/main/en/_app/start-hf-doc-builder.js";
start({
target: document.querySelector('[data-hydrate="u9oox0"]').parentNode,
paths: {"base":"/docs/diffusers/main/en","assets":"/docs/diffusers/main/en"},
session: {},
route: false,
spa: false,
trailing_slash: "never",
hydrate: {
status: 200,
error: null,
nodes: [
import("/docs/diffusers/main/en/_app/pages/__layout.svelte-hf-doc-builder.js"),
import("/docs/diffusers/main/en/_app/pages/optimization/bentoml.mdx-hf-doc-builder.js")
],
params: {}
}
});
</script>

Xet Storage Details

Size:
32.9 kB
·
Xet hash:
fb4fa4d0a7c718ef33d3fa4672cf7d3c6534df824532152154a1e7396ccf436d

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.