Buckets:

download
raw
42 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Schedulers&quot;,&quot;local&quot;:&quot;schedulers&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Loading schedulers&quot;,&quot;local&quot;:&quot;loading-schedulers&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Timestep schedules&quot;,&quot;local&quot;:&quot;timestep-schedules&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Rescaling schedules&quot;,&quot;local&quot;:&quot;rescaling-schedules&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Timestep spacing&quot;,&quot;local&quot;:&quot;timestep-spacing&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Sigmas&quot;,&quot;local&quot;:&quot;sigmas&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Karras sigmas&quot;,&quot;local&quot;:&quot;karras-sigmas&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Choosing a scheduler&quot;,&quot;local&quot;:&quot;choosing-a-scheduler&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Resources&quot;,&quot;local&quot;:&quot;resources&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/diffusers/pr_12403/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/entry/start.33959e67.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/chunks/scheduler.8c3d61f6.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/chunks/singletons.46d5608c.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/chunks/index.0997d446.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/chunks/paths.0dc9c45f.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/entry/app.87796ad1.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/chunks/index.da70eac4.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/nodes/0.9198881c.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/nodes/319.9bf2cd7f.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/chunks/CodeBlock.a9c4becf.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/chunks/DocNotebookDropdown.48852948.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/chunks/globals.7f7f1b26.js">
<link rel="modulepreload" href="/docs/diffusers/pr_12403/en/_app/immutable/chunks/getInferenceSnippets.ea1775db.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Schedulers&quot;,&quot;local&quot;:&quot;schedulers&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Loading schedulers&quot;,&quot;local&quot;:&quot;loading-schedulers&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Timestep schedules&quot;,&quot;local&quot;:&quot;timestep-schedules&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Rescaling schedules&quot;,&quot;local&quot;:&quot;rescaling-schedules&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Timestep spacing&quot;,&quot;local&quot;:&quot;timestep-spacing&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Sigmas&quot;,&quot;local&quot;:&quot;sigmas&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Karras sigmas&quot;,&quot;local&quot;:&quot;karras-sigmas&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Choosing a scheduler&quot;,&quot;local&quot;:&quot;choosing-a-scheduler&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Resources&quot;,&quot;local&quot;:&quot;resources&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <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> <h1 class="relative group"><a id="schedulers" 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="#schedulers"><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>Schedulers</span></h1> <p data-svelte-h="svelte-102wtk8">A scheduler is an algorithm that provides instructions to the denoising process such as how much noise to remove at a certain step. It takes the model prediction from step <em>t</em> and applies an update for how to compute the next sample at step <em>t-1</em>. Different schedulers produce different results; some are faster while others are more accurate.</p> <p data-svelte-h="svelte-7gdk9f">Diffusers supports many schedulers and allows you to modify their timestep schedules, timestep spacing, and more, to generate high-quality images in fewer steps.</p> <p data-svelte-h="svelte-n44qmz">This guide will show you how to load and customize schedulers.</p> <h2 class="relative group"><a id="loading-schedulers" 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="#loading-schedulers"><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>Loading schedulers</span></h2> <p data-svelte-h="svelte-thfjbi">Schedulers don’t have any parameters and are defined in a configuration file. Access the <code>.scheduler</code> attribute of a pipeline to view the configuration.</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 class=""><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>, torch_dtype=torch.float16, device_map=<span class="hljs-string">&quot;cuda&quot;</span>
)
pipeline.scheduler<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-87ljw9">Load a different scheduler with <a href="/docs/diffusers/pr_12403/en/api/schedulers/overview#diffusers.SchedulerMixin.from_pretrained">from_pretrained()</a> and specify the <code>subfolder</code> argument to load the configuration file into the correct subfolder of the pipeline repository. Pass the new scheduler to the existing pipeline.</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 class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DPMSolverMultistepScheduler
dpm = DPMSolverMultistepScheduler.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>, subfolder=<span class="hljs-string">&quot;scheduler&quot;</span>
)
pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
scheduler=dpm,
torch_dtype=torch.float16,
device_map=<span class="hljs-string">&quot;cuda&quot;</span>
)
pipeline.scheduler<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="timestep-schedules" 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="#timestep-schedules"><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>Timestep schedules</span></h2> <p data-svelte-h="svelte-41eu0f">Timestep or noise schedule decides how noise is distributed over the denoising process. The schedule can be linear or more concentrated toward the beginning or end. It is a precomputed sequence of noise levels generated from the scheduler’s default configuration, but it can be customized to use other schedules.</p> <blockquote class="tip" data-svelte-h="svelte-fdqszo"><p>The <code>timesteps</code> argument is only supported for a select list of schedulers and pipelines. Feel free to open a feature request if you want to extend these parameters to a scheduler and pipeline that does not currently support it!</p></blockquote> <p data-svelte-h="svelte-1ohacqg">The example below uses the <a href="https://research.nvidia.com/labs/toronto-ai/AlignYourSteps/" rel="nofollow">Align Your Steps (AYS)</a> schedule which can generate a high-quality image in 10 steps, significantly speeding up generation and reducing computation time.</p> <p data-svelte-h="svelte-rm6bho">Import the schedule and pass it to the <code>timesteps</code> argument in the pipeline.</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 class=""><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline, DPMSolverMultistepScheduler
<span class="hljs-keyword">from</span> diffusers.schedulers <span class="hljs-keyword">import</span> AysSchedules
sampling_schedule = AysSchedules[<span class="hljs-string">&quot;StableDiffusionXLTimesteps&quot;</span>]
<span class="hljs-built_in">print</span>(sampling_schedule)
<span class="hljs-string">&quot;[999, 845, 730, 587, 443, 310, 193, 116, 53, 13]&quot;</span>
pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;SG161222/RealVisXL_V4.0&quot;</span>,
torch_dtype=torch.float16,
device_map=<span class="hljs-string">&quot;cuda&quot;</span>
)
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(
pipeline.scheduler.config, algorithm_type=<span class="hljs-string">&quot;sde-dpmsolver++&quot;</span>
)
prompt = <span class="hljs-string">&quot;A cinematic shot of a cute little rabbit wearing a jacket and doing a thumbs up&quot;</span>
image = pipeline(
prompt=prompt,
negative_prompt=<span class="hljs-string">&quot;&quot;</span>,
timesteps=sampling_schedule,
).images[<span class="hljs-number">0</span>]<!-- HTML_TAG_END --></pre></div> <div class="flex gap-4" data-svelte-h="svelte-1wwk2vr"><div><img class="rounded-xl" src="https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/ays.png"> <figcaption class="mt-2 text-center text-sm text-gray-500">AYS timestep schedule 10 steps</figcaption></div> <div><img class="rounded-xl" src="https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/10.png"> <figcaption class="mt-2 text-center text-sm text-gray-500">Linearly-spaced timestep schedule 10 steps</figcaption></div> <div><img class="rounded-xl" src="https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/25.png"> <figcaption class="mt-2 text-center text-sm text-gray-500">Linearly-spaced timestep schedule 25 steps</figcaption></div></div> <h3 class="relative group"><a id="rescaling-schedules" 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="#rescaling-schedules"><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>Rescaling schedules</span></h3> <p data-svelte-h="svelte-qyan8p">Denoising should begin with pure noise and the signal-to-noise (SNR) ration should be zero. However, some models don’t actually start from pure noise which makes it difficult to generate images at brightness extremes.</p> <blockquote class="tip" data-svelte-h="svelte-1ytgrjg"><p>Train your own model with <code>v_prediction</code> by adding the <code>--prediction_type=&quot;v_prediction&quot;</code> flag to your training script. You can also <a href="https://huggingface.co/search/full-text?q=v_prediction&type=model" rel="nofollow">search</a> for existing models trained with <code>v_prediction</code>.</p></blockquote> <p data-svelte-h="svelte-u69ydw">To fix this, a model must be trained with <code>v_prediction</code>. If a model is trained with <code>v_prediction</code>, then enable the following arguments in the scheduler.</p> <ul data-svelte-h="svelte-4v39hu"><li>Set <code>rescale_betas_zero_snr=True</code> to rescale the noise schedule to the very last timestep with exactly zero SNR</li> <li>Set <code>timestep_spacing=&quot;trailing&quot;</code> to force sampling from the last timestep with pure noise</li></ul> <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 class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline, DDIMScheduler
pipeline = DiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;ptx0/pseudo-journey-v2&quot;</span>, device_map=<span class="hljs-string">&quot;cuda&quot;</span>)
pipeline.scheduler = DDIMScheduler.from_config(
pipeline.scheduler.config, rescale_betas_zero_snr=<span class="hljs-literal">True</span>, timestep_spacing=<span class="hljs-string">&quot;trailing&quot;</span>
)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-bj3sil">Set <code>guidance_rescale</code> in the pipeline to avoid overexposed images. A lower value increases brightness, but some details may appear washed out.</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 class=""><!-- HTML_TAG_START -->prompt = <span class="hljs-string">&quot;&quot;&quot;
cinematic photo of a snowy mountain at night with the northern lights aurora borealis
overhead, 35mm photograph, film, professional, 4k, highly detailed
&quot;&quot;&quot;</span>
image = pipeline(prompt, guidance_rescale=<span class="hljs-number">0.7</span>).images[<span class="hljs-number">0</span>]<!-- HTML_TAG_END --></pre></div> <div class="flex gap-4" data-svelte-h="svelte-2pxhst"><div><img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/no-zero-snr.png"> <figcaption class="mt-2 text-center text-sm text-gray-500">default Stable Diffusion v2-1 image</figcaption></div> <div><img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/zero-snr.png"> <figcaption class="mt-2 text-center text-sm text-gray-500">image with zero SNR and trailing timestep spacing enabled</figcaption></div></div> <h2 class="relative group"><a id="timestep-spacing" 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="#timestep-spacing"><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>Timestep spacing</span></h2> <p data-svelte-h="svelte-p812a3">Timestep spacing refers to the specific steps <em>t</em> to sample from from the schedule. Diffusers provides three spacing types as shown below.</p> <table data-svelte-h="svelte-1e67q19"><thead><tr><th>spacing strategy</th> <th>spacing calculation</th> <th>example timesteps</th></tr></thead> <tbody><tr><td><code>leading</code></td> <td>evenly spaced steps</td> <td><code>[900, 800, 700, ..., 100, 0]</code></td></tr> <tr><td><code>linspace</code></td> <td>include first and last steps and evenly divide remaining intermediate steps</td> <td><code>[1000, 888.89, 777.78, ..., 111.11, 0]</code></td></tr> <tr><td><code>trailing</code></td> <td>include last step and evenly divide remaining intermediate steps beginning from the end</td> <td><code>[999, 899, 799, 699, 599, 499, 399, 299, 199, 99]</code></td></tr></tbody></table> <p data-svelte-h="svelte-d1j5xm">Pass the spacing strategy to the <code>timestep_spacing</code> argument in the scheduler.</p> <blockquote class="tip" data-svelte-h="svelte-15bsxi3"><p>The <code>trailing</code> strategy typically produces higher quality images with more details with fewer steps, but the difference in quality is not as obvious for more standard step values.</p></blockquote> <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 class=""><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline, DPMSolverMultistepScheduler
pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;SG161222/RealVisXL_V4.0&quot;</span>,
torch_dtype=torch.float16,
device_map=<span class="hljs-string">&quot;cuda&quot;</span>
)
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(
pipeline.scheduler.config, timestep_spacing=<span class="hljs-string">&quot;trailing&quot;</span>
)
prompt = <span class="hljs-string">&quot;A cinematic shot of a cute little black cat sitting on a pumpkin at night&quot;</span>
image = pipeline(
prompt=prompt,
negative_prompt=<span class="hljs-string">&quot;&quot;</span>,
num_inference_steps=<span class="hljs-number">5</span>,
).images[<span class="hljs-number">0</span>]
image<!-- HTML_TAG_END --></pre></div> <div class="flex gap-4" data-svelte-h="svelte-106qvjp"><div><img class="rounded-xl" src="https://huggingface.co/datasets/stevhliu/testing-images/resolve/main/trailing_spacing.png"> <figcaption class="mt-2 text-center text-sm text-gray-500">trailing spacing after 5 steps</figcaption></div> <div><img class="rounded-xl" src="https://huggingface.co/datasets/stevhliu/testing-images/resolve/main/leading_spacing.png"> <figcaption class="mt-2 text-center text-sm text-gray-500">leading spacing after 5 steps</figcaption></div></div> <h2 class="relative group"><a id="sigmas" 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="#sigmas"><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>Sigmas</span></h2> <p data-svelte-h="svelte-10a435k">Sigmas is a measure of how noisy a sample is at a certain step as defined by the schedule. When using custom <code>sigmas</code>, the <code>timesteps</code> are calculated from these values instead of the default scheduler configuration.</p> <blockquote class="tip" data-svelte-h="svelte-118exvu"><p>The <code>sigmas</code> argument is only supported for a select list of schedulers and pipelines. Feel free to open a feature request if you want to extend these parameters to a scheduler and pipeline that does not currently support it!</p></blockquote> <p data-svelte-h="svelte-x20l5u">Pass the custom sigmas to the <code>sigmas</code> argument in the pipeline. The example below uses the <a href="https://github.com/huggingface/diffusers/blob/6529ee67ec02fcf58d2fd9242164ea002b351d75/src/diffusers/schedulers/scheduling_utils.py#L55" rel="nofollow">sigmas</a> from the 10-step AYS schedule.</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 class=""><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline, DPMSolverMultistepScheduler
pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;SG161222/RealVisXL_V4.0&quot;</span>,
torch_dtype=torch.float16,
device_map=<span class="hljs-string">&quot;cuda&quot;</span>
)
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(
pipeline.scheduler.config, algorithm_type=<span class="hljs-string">&quot;sde-dpmsolver++&quot;</span>
)
sigmas = [<span class="hljs-number">14.615</span>, <span class="hljs-number">6.315</span>, <span class="hljs-number">3.771</span>, <span class="hljs-number">2.181</span>, <span class="hljs-number">1.342</span>, <span class="hljs-number">0.862</span>, <span class="hljs-number">0.555</span>, <span class="hljs-number">0.380</span>, <span class="hljs-number">0.234</span>, <span class="hljs-number">0.113</span>, <span class="hljs-number">0.0</span>]
prompt = <span class="hljs-string">&quot;A cinematic shot of a cute little rabbit wearing a jacket and doing a thumbs up&quot;</span>
image = pipeline(
prompt=prompt,
negative_prompt=<span class="hljs-string">&quot;&quot;</span>,
sigmas=sigmas,
).images[<span class="hljs-number">0</span>]<!-- HTML_TAG_END --></pre></div> <h3 class="relative group"><a id="karras-sigmas" 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="#karras-sigmas"><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>Karras sigmas</span></h3> <p data-svelte-h="svelte-3ub125"><a href="https://huggingface.co/papers/2206.00364" rel="nofollow">Karras sigmas</a> resamples the noise schedule for more efficient sampling by clustering sigmas more densely in the middle of the sequence where structure reconstruction is critical, while using fewer sigmas at the beginning and end where noise changes have less impact. This can increase the level of details in a generated image.</p> <p data-svelte-h="svelte-10otcr1">Set <code>use_karras_sigmas=True</code> in the scheduler to enable it.</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 class=""><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline, DPMSolverMultistepScheduler
pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;SG161222/RealVisXL_V4.0&quot;</span>,
torch_dtype=torch.float16,
device_map=<span class="hljs-string">&quot;cuda&quot;</span>
)
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(
pipeline.scheduler.config,
algorithm_type=<span class="hljs-string">&quot;sde-dpmsolver++&quot;</span>,
use_karras_sigmas=<span class="hljs-literal">True</span>,
)
prompt = <span class="hljs-string">&quot;A cinematic shot of a cute little rabbit wearing a jacket and doing a thumbs up&quot;</span>
image = pipeline(
prompt=prompt,
negative_prompt=<span class="hljs-string">&quot;&quot;</span>,
sigmas=sigmas,
).images[<span class="hljs-number">0</span>]<!-- HTML_TAG_END --></pre></div> <div class="flex gap-4" data-svelte-h="svelte-gruv4r"><div><img class="rounded-xl" src="https://huggingface.co/datasets/stevhliu/testing-images/resolve/main/karras_sigmas_true.png"> <figcaption class="mt-2 text-center text-sm text-gray-500">Karras sigmas enabled</figcaption></div> <div><img class="rounded-xl" src="https://huggingface.co/datasets/stevhliu/testing-images/resolve/main/karras_sigmas_false.png"> <figcaption class="mt-2 text-center text-sm text-gray-500">Karras sigmas disabled</figcaption></div></div> <p data-svelte-h="svelte-1livq9e">Refer to the scheduler API <a href="../api/schedulers/overview">overview</a> for a list of schedulers that support Karras sigmas. It should only be used for models trained with Karras sigmas.</p> <h2 class="relative group"><a id="choosing-a-scheduler" 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="#choosing-a-scheduler"><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>Choosing a scheduler</span></h2> <p data-svelte-h="svelte-1gtdusx">It’s important to try different schedulers to find the best one for your use case. Here are a few recommendations to help you get started.</p> <ul data-svelte-h="svelte-vpsmta"><li>DPM++ 2M SDE Karras is generally a good all-purpose option.</li> <li><a href="/docs/diffusers/pr_12403/en/api/schedulers/tcd#diffusers.TCDScheduler">TCDScheduler</a> works well for distilled models.</li> <li><a href="/docs/diffusers/pr_12403/en/api/schedulers/flow_match_euler_discrete#diffusers.FlowMatchEulerDiscreteScheduler">FlowMatchEulerDiscreteScheduler</a> and <a href="/docs/diffusers/pr_12403/en/api/schedulers/flow_match_heun_discrete#diffusers.FlowMatchHeunDiscreteScheduler">FlowMatchHeunDiscreteScheduler</a> for FlowMatch models.</li> <li><a href="/docs/diffusers/pr_12403/en/api/schedulers/euler#diffusers.EulerDiscreteScheduler">EulerDiscreteScheduler</a> or <a href="/docs/diffusers/pr_12403/en/api/schedulers/euler_ancestral#diffusers.EulerAncestralDiscreteScheduler">EulerAncestralDiscreteScheduler</a> for generating anime style images.</li> <li>DPM++ 2M paired with <a href="/docs/diffusers/pr_12403/en/api/schedulers/lcm#diffusers.LCMScheduler">LCMScheduler</a> on SDXL for generating realistic images.</li></ul> <h2 class="relative group"><a id="resources" 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="#resources"><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>Resources</span></h2> <ul data-svelte-h="svelte-t1vvcd"><li>Read the <a href="https://huggingface.co/papers/2305.08891" rel="nofollow">Common Diffusion Noise Schedules and Sample Steps are Flawed</a> paper for more details about rescaling the noise schedule to enforce zero SNR.</li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/diffusers/blob/main/docs/source/en/using-diffusers/schedulers.md" target="_blank"><span data-svelte-h="svelte-1kd6by1">&lt;</span> <span data-svelte-h="svelte-x0xyl0">&gt;</span> <span data-svelte-h="svelte-1dajgef"><span class="underline ml-1.5">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_g87enx = {
assets: "/docs/diffusers/pr_12403/en",
base: "/docs/diffusers/pr_12403/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/diffusers/pr_12403/en/_app/immutable/entry/start.33959e67.js"),
import("/docs/diffusers/pr_12403/en/_app/immutable/entry/app.87796ad1.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 319],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
42 kB
·
Xet hash:
4877f721ad7b0d67e6244155e295f9d25c016113bb7fe1e13efa967ad925aeb4

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