import{S as hr,i as ur,s as mr,e as s,k as p,w as v,t as i,M as gr,c as r,d as o,m as f,a as n,x as b,h as a,b as d,G as t,g as c,y,L as _r,q as D,o as w,B as M,v as vr}from"../../../chunks/vendor-hf-doc-builder.js";import{D as Gt}from"../../../chunks/Docstring-hf-doc-builder.js";import{C as Jt}from"../../../chunks/CodeBlock-hf-doc-builder.js";import{I as Pt}from"../../../chunks/IconCopyLink-hf-doc-builder.js";function br(hs){let $,ot,T,U,Ie,B,Ct,$e,Vt,st,N,Z,Te,L,Wt,Ne,Ft,rt,q,X,At,Rt,nt,pe,Bt,it,fe,ke,Lt,at,x,qt,Q,Xt,Qt,z,zt,Yt,lt,k,G,je,Y,Ht,Ue,Ot,dt,J,Kt,Ze,H,eo,to,ct,he,oo,pt,ue,so,ft,_,Ge,ro,no,Je,io,ao,Pe,lo,co,Ce,po,ht,E,fo,Ve,ho,uo,me,mo,go,ut,ge,We,_o,mt,O,gt,P,S,vo,K,Fe,bo,yo,ee,Ae,Do,wo,Re,Mo,So,xo,Be,Eo,_t,te,vt,_e,Le,Io,bt,oe,yt,I,$o,se,To,No,qe,ko,jo,Dt,ve,Uo,wt,re,Mt,j,C,Xe,ne,Zo,Qe,Go,St,h,ie,Jo,ze,Po,Co,u,be,Vo,Wo,Ye,Fo,Ao,He,Ro,Bo,Oe,Lo,qo,ye,Xo,Qo,De,zo,Yo,we,Ho,Oo,Ko,Me,es,ae,ts,os,V,le,ss,Ke,rs,ns,W,de,is,et,as,ls,F,ce,ds,tt,cs,xt;return B=new Pt({}),L=new Pt({}),Y=new Pt({}),O=new Jt({props:{code:"cGlwZS5zY2hlZHVsZXIlMjAlM0QlMjBERElNU2NoZWR1bGVyLmZyb21fY29uZmlnKHBpcGUuc2NoZWR1bGVyLmNvbmZpZyUyQyUyMHJlc2NhbGVfYmV0YXNfemVyb19zbnIlM0RUcnVlKQ==",highlighted:'pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config, rescale_betas_zero_snr=<span class="hljs-literal">True</span>)'}}),te=new Jt({props:{code:"cGlwZS5zY2hlZHVsZXIlMjAlM0QlMjBERElNU2NoZWR1bGVyLmZyb21fY29uZmlnKHBpcGUuc2NoZWR1bGVyLmNvbmZpZyUyQyUyMHRpbWVzdGVwX3NjYWxpbmclM0QlMjJ0cmFpbGluZyUyMik=",highlighted:'pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config, timestep_scaling=<span class="hljs-string">&quot;trailing&quot;</span>)'}}),oe=new Jt({props:{code:"cGlwZSguLi4lMkMlMjBndWlkYW5jZV9yZXNjYWxlJTNEMC43KQ==",highlighted:'pipe(..., guidance_rescale=<span class="hljs-number">0.7</span>)'}}),re=new Jt({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTJDJTIwRERJTVNjaGVkdWxlciUwQSUwQXBpcGUlMjAlM0QlMjBEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIycHR4MCUyRnBzZXVkby1qb3VybmV5LXYyJTIyJTJDJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2KSUwQXBpcGUuc2NoZWR1bGVyJTIwJTNEJTIwRERJTVNjaGVkdWxlci5mcm9tX2NvbmZpZyglMEElMjAlMjAlMjAlMjBwaXBlLnNjaGVkdWxlci5jb25maWclMkMlMjByZXNjYWxlX2JldGFzX3plcm9fc25yJTNEVHJ1ZSUyQyUyMHRpbWVzdGVwX3NjYWxpbmclM0QlMjJ0cmFpbGluZyUyMiUwQSklMEFwaXBlLnRvKCUyMmN1ZGElMjIpJTBBJTBBcHJvbXB0JTIwJTNEJTIwJTIyQSUyMGxpb24lMjBpbiUyMGdhbGF4aWVzJTJDJTIwc3BpcmFscyUyQyUyMG5lYnVsYWUlMkMlMjBzdGFycyUyQyUyMHNtb2tlJTJDJTIwaXJpZGVzY2VudCUyQyUyMGludHJpY2F0ZSUyMGRldGFpbCUyQyUyMG9jdGFuZSUyMHJlbmRlciUyQyUyMDhrJTIyJTBBaW1hZ2UlMjAlM0QlMjBwaXBlbGluZShwcm9tcHQlMkMlMjBndWlkYW5jZV9yZXNjYWxlJTNEMC43KS5pbWFnZXMlNUIwJTVE",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline, DDIMScheduler

pipe = DiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;ptx0/pseudo-journey-v2&quot;</span>, torch_dtype=torch.float16)
pipe.scheduler = DDIMScheduler.from_config(
    pipe.scheduler.config, rescale_betas_zero_snr=<span class="hljs-literal">True</span>, timestep_scaling=<span class="hljs-string">&quot;trailing&quot;</span>
)
pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)

prompt = <span class="hljs-string">&quot;A lion in galaxies, spirals, nebulae, stars, smoke, iridescent, intricate detail, octane render, 8k&quot;</span>
image = pipeline(prompt, guidance_rescale=<span class="hljs-number">0.7</span>).images[<span class="hljs-number">0</span>]`}}),ne=new Pt({}),ie=new Gt({props:{name:"class diffusers.DDIMScheduler",anchor:"diffusers.DDIMScheduler",parameters:[{name:"num_train_timesteps",val:": int = 1000"},{name:"beta_start",val:": float = 0.0001"},{name:"beta_end",val:": float = 0.02"},{name:"beta_schedule",val:": str = 'linear'"},{name:"trained_betas",val:": typing.Union[numpy.ndarray, typing.List[float], NoneType] = None"},{name:"clip_sample",val:": bool = True"},{name:"set_alpha_to_one",val:": bool = True"},{name:"steps_offset",val:": int = 0"},{name:"prediction_type",val:": str = 'epsilon'"},{name:"thresholding",val:": bool = False"},{name:"dynamic_thresholding_ratio",val:": float = 0.995"},{name:"clip_sample_range",val:": float = 1.0"},{name:"sample_max_value",val:": float = 1.0"},{name:"timestep_spacing",val:": str = 'leading'"},{name:"rescale_betas_zero_snr",val:": bool = False"}],parametersDescription:[{anchor:"diffusers.DDIMScheduler.num_train_timesteps",description:"<strong>num_train_timesteps</strong> (<code>int</code>) &#x2014; number of diffusion steps used to train the model.",name:"num_train_timesteps"},{anchor:"diffusers.DDIMScheduler.beta_start",description:"<strong>beta_start</strong> (<code>float</code>) &#x2014; the starting <code>beta</code> value of inference.",name:"beta_start"},{anchor:"diffusers.DDIMScheduler.beta_end",description:"<strong>beta_end</strong> (<code>float</code>) &#x2014; the final <code>beta</code> value.",name:"beta_end"},{anchor:"diffusers.DDIMScheduler.beta_schedule",description:`<strong>beta_schedule</strong> (<code>str</code>) &#x2014;
the beta schedule, a mapping from a beta range to a sequence of betas for stepping the model. Choose from
<code>linear</code>, <code>scaled_linear</code>, or <code>squaredcos_cap_v2</code>.`,name:"beta_schedule"},{anchor:"diffusers.DDIMScheduler.trained_betas",description:`<strong>trained_betas</strong> (<code>np.ndarray</code>, optional) &#x2014;
option to pass an array of betas directly to the constructor to bypass <code>beta_start</code>, <code>beta_end</code> etc.`,name:"trained_betas"},{anchor:"diffusers.DDIMScheduler.clip_sample",description:`<strong>clip_sample</strong> (<code>bool</code>, default <code>True</code>) &#x2014;
option to clip predicted sample for numerical stability.`,name:"clip_sample"},{anchor:"diffusers.DDIMScheduler.clip_sample_range",description:`<strong>clip_sample_range</strong> (<code>float</code>, default <code>1.0</code>) &#x2014;
the maximum magnitude for sample clipping. Valid only when <code>clip_sample=True</code>.`,name:"clip_sample_range"},{anchor:"diffusers.DDIMScheduler.set_alpha_to_one",description:`<strong>set_alpha_to_one</strong> (<code>bool</code>, default <code>True</code>) &#x2014;
each diffusion step uses the value of alphas product at that step and at the previous one. For the final
step there is no previous alpha. When this option is <code>True</code> the previous alpha product is fixed to <code>1</code>,
otherwise it uses the value of alpha at step 0.`,name:"set_alpha_to_one"},{anchor:"diffusers.DDIMScheduler.steps_offset",description:`<strong>steps_offset</strong> (<code>int</code>, default <code>0</code>) &#x2014;
an offset added to the inference steps. You can use a combination of <code>offset=1</code> and
<code>set_alpha_to_one=False</code>, to make the last step use step 0 for the previous alpha product, as done in
stable diffusion.`,name:"steps_offset"},{anchor:"diffusers.DDIMScheduler.prediction_type",description:`<strong>prediction_type</strong> (<code>str</code>, default <code>epsilon</code>, optional) &#x2014;
prediction type of the scheduler function, one of <code>epsilon</code> (predicting the noise of the diffusion
process), <code>sample</code> (directly predicting the noisy sample<code>) or </code>v_prediction\` (see section 2.4
<a href="https://imagen.research.google/video/paper.pdf" rel="nofollow">https://imagen.research.google/video/paper.pdf</a>)`,name:"prediction_type"},{anchor:"diffusers.DDIMScheduler.thresholding",description:`<strong>thresholding</strong> (<code>bool</code>, default <code>False</code>) &#x2014;
whether to use the &#x201C;dynamic thresholding&#x201D; method (introduced by Imagen, <a href="https://arxiv.org/abs/2205.11487" rel="nofollow">https://arxiv.org/abs/2205.11487</a>).
Note that the thresholding method is unsuitable for latent-space diffusion models (such as
stable-diffusion).`,name:"thresholding"},{anchor:"diffusers.DDIMScheduler.dynamic_thresholding_ratio",description:`<strong>dynamic_thresholding_ratio</strong> (<code>float</code>, default <code>0.995</code>) &#x2014;
the ratio for the dynamic thresholding method. Default is <code>0.995</code>, the same as Imagen
(<a href="https://arxiv.org/abs/2205.11487" rel="nofollow">https://arxiv.org/abs/2205.11487</a>). Valid only when <code>thresholding=True</code>.`,name:"dynamic_thresholding_ratio"},{anchor:"diffusers.DDIMScheduler.sample_max_value",description:`<strong>sample_max_value</strong> (<code>float</code>, default <code>1.0</code>) &#x2014;
the threshold value for dynamic thresholding. Valid only when <code>thresholding=True</code>.`,name:"sample_max_value"},{anchor:"diffusers.DDIMScheduler.timestep_spacing",description:`<strong>timestep_spacing</strong> (<code>str</code>, default <code>&quot;leading&quot;</code>) &#x2014;
The way the timesteps should be scaled. Refer to Table 2. of <a href="https://arxiv.org/abs/2305.08891" rel="nofollow">Common Diffusion Noise Schedules and Sample
Steps are Flawed</a> for more information.`,name:"timestep_spacing"},{anchor:"diffusers.DDIMScheduler.rescale_betas_zero_snr",description:`<strong>rescale_betas_zero_snr</strong> (<code>bool</code>, default <code>False</code>) &#x2014;
whether to rescale the betas to have zero terminal SNR (proposed by <a href="https://arxiv.org/pdf/2305.08891.pdf" rel="nofollow">https://arxiv.org/pdf/2305.08891.pdf</a>).
This can enable the model to generate very bright and dark samples instead of limiting it to samples with
medium brightness. Loosely related to
<a href="https://github.com/huggingface/diffusers/blob/74fd735eb073eb1d774b1ab4154a0876eb82f055/examples/dreambooth/train_dreambooth.py#L506" rel="nofollow"><code>--offset_noise</code></a>.`,name:"rescale_betas_zero_snr"}],source:"https://github.com/huggingface/diffusers/blob/v0.17.0/src/diffusers/schedulers/scheduling_ddim.py#L115"}}),le=new Gt({props:{name:"scale_model_input",anchor:"diffusers.DDIMScheduler.scale_model_input",parameters:[{name:"sample",val:": FloatTensor"},{name:"timestep",val:": typing.Optional[int] = None"}],parametersDescription:[{anchor:"diffusers.DDIMScheduler.scale_model_input.sample",description:"<strong>sample</strong> (<code>torch.FloatTensor</code>) &#x2014; input sample",name:"sample"},{anchor:"diffusers.DDIMScheduler.scale_model_input.timestep",description:"<strong>timestep</strong> (<code>int</code>, optional) &#x2014; current timestep",name:"timestep"}],source:"https://github.com/huggingface/diffusers/blob/v0.17.0/src/diffusers/schedulers/scheduling_ddim.py#L228",returnDescription:`
<p>scaled input sample</p>
`,returnType:`
<p><code>torch.FloatTensor</code></p>
`}}),de=new Gt({props:{name:"set_timesteps",anchor:"diffusers.DDIMScheduler.set_timesteps",parameters:[{name:"num_inference_steps",val:": int"},{name:"device",val:": typing.Union[str, torch.device] = None"}],parametersDescription:[{anchor:"diffusers.DDIMScheduler.set_timesteps.num_inference_steps",description:`<strong>num_inference_steps</strong> (<code>int</code>) &#x2014;
the number of diffusion steps used when generating samples with a pre-trained model.`,name:"num_inference_steps"}],source:"https://github.com/huggingface/diffusers/blob/v0.17.0/src/diffusers/schedulers/scheduling_ddim.py#L287"}}),ce=new Gt({props:{name:"step",anchor:"diffusers.DDIMScheduler.step",parameters:[{name:"model_output",val:": FloatTensor"},{name:"timestep",val:": int"},{name:"sample",val:": FloatTensor"},{name:"eta",val:": float = 0.0"},{name:"use_clipped_model_output",val:": bool = False"},{name:"generator",val:" = None"},{name:"variance_noise",val:": typing.Optional[torch.FloatTensor] = None"},{name:"return_dict",val:": bool = True"}],parametersDescription:[{anchor:"diffusers.DDIMScheduler.step.model_output",description:"<strong>model_output</strong> (<code>torch.FloatTensor</code>) &#x2014; direct output from learned diffusion model.",name:"model_output"},{anchor:"diffusers.DDIMScheduler.step.timestep",description:"<strong>timestep</strong> (<code>int</code>) &#x2014; current discrete timestep in the diffusion chain.",name:"timestep"},{anchor:"diffusers.DDIMScheduler.step.sample",description:`<strong>sample</strong> (<code>torch.FloatTensor</code>) &#x2014;
current instance of sample being created by diffusion process.`,name:"sample"},{anchor:"diffusers.DDIMScheduler.step.eta",description:"<strong>eta</strong> (<code>float</code>) &#x2014; weight of noise for added noise in diffusion step.",name:"eta"},{anchor:"diffusers.DDIMScheduler.step.use_clipped_model_output",description:`<strong>use_clipped_model_output</strong> (<code>bool</code>) &#x2014; if <code>True</code>, compute &#x201C;corrected&#x201D; <code>model_output</code> from the clipped
predicted original sample. Necessary because predicted original sample is clipped to [-1, 1] when
<code>self.config.clip_sample</code> is <code>True</code>. If no clipping has happened, &#x201C;corrected&#x201D; <code>model_output</code> would
coincide with the one provided as input and <code>use_clipped_model_output</code> will have not effect.
generator &#x2014; random number generator.`,name:"use_clipped_model_output"},{anchor:"diffusers.DDIMScheduler.step.variance_noise",description:`<strong>variance_noise</strong> (<code>torch.FloatTensor</code>) &#x2014; instead of generating noise for the variance using <code>generator</code>, we
can directly provide the noise for the variance itself. This is useful for methods such as
CycleDiffusion. (<a href="https://arxiv.org/abs/2210.05559" rel="nofollow">https://arxiv.org/abs/2210.05559</a>)`,name:"variance_noise"},{anchor:"diffusers.DDIMScheduler.step.return_dict",description:"<strong>return_dict</strong> (<code>bool</code>) &#x2014; option for returning tuple rather than DDIMSchedulerOutput class",name:"return_dict"}],source:"https://github.com/huggingface/diffusers/blob/v0.17.0/src/diffusers/schedulers/scheduling_ddim.py#L325",returnDescription:`
<p><code>~schedulers.scheduling_utils.DDIMSchedulerOutput</code> if <code>return_dict</code> is True, otherwise a <code>tuple</code>. When
returning a tuple, the first element is the sample tensor.</p>
`,returnType:`
<p><code>~schedulers.scheduling_utils.DDIMSchedulerOutput</code> or <code>tuple</code></p>
`}}),{c(){$=s("meta"),ot=p(),T=s("h1"),U=s("a"),Ie=s("span"),v(B.$$.fragment),Ct=p(),$e=s("span"),Vt=i("Denoising Diffusion Implicit Models (DDIM)"),st=p(),N=s("h2"),Z=s("a"),Te=s("span"),v(L.$$.fragment),Wt=p(),Ne=s("span"),Ft=i("Overview"),rt=p(),q=s("p"),X=s("a"),At=i("Denoising Diffusion Implicit Models"),Rt=i(" (DDIM) by Jiaming Song, Chenlin Meng and Stefano Ermon."),nt=p(),pe=s("p"),Bt=i("The abstract of the paper is the following:"),it=p(),fe=s("p"),ke=s("em"),Lt=i(`Denoising diffusion probabilistic models (DDPMs) have achieved high quality image generation without adversarial training,
yet they require simulating a Markov chain for many steps to produce a sample.
To accelerate sampling, we present denoising diffusion implicit models (DDIMs), a more efficient class of iterative implicit probabilistic models
with the same training procedure as DDPMs. In DDPMs, the generative process is defined as the reverse of a Markovian diffusion process.
We construct a class of non-Markovian diffusion processes that lead to the same training objective, but whose reverse process can be much faster to sample from.
We empirically demonstrate that DDIMs can produce high quality samples 10\xD7 to 50\xD7 faster in terms of wall-clock time compared to DDPMs, allow us to trade off
computation for sample quality, and can perform semantically meaningful image interpolation directly in the latent space.`),at=p(),x=s("p"),qt=i("The original codebase of this paper can be found here: "),Q=s("a"),Xt=i("ermongroup/ddim"),Qt=i(`.
For questions, feel free to contact the author on `),z=s("a"),zt=i("tsong.me"),Yt=i("."),lt=p(),k=s("h3"),G=s("a"),je=s("span"),v(Y.$$.fragment),Ht=p(),Ue=s("span"),Ot=i('Experimental: "Common Diffusion Noise Schedules and Sample Steps are Flawed":'),dt=p(),J=s("p"),Kt=i("The paper "),Ze=s("strong"),H=s("a"),eo=i("Common Diffusion Noise Schedules and Sample Steps are Flawed"),to=i(`
claims that a mismatch between the training and inference settings leads to suboptimal inference generation results for Stable Diffusion.`),ct=p(),he=s("p"),oo=i("The abstract reads as follows:"),pt=p(),ue=s("p"),so=i(`*We discover that common diffusion noise schedules do not enforce the last timestep to have zero signal-to-noise ratio (SNR),
and some implementations of diffusion samplers do not start from the last timestep.
Such designs are flawed and do not reflect the fact that the model is given pure Gaussian noise at inference, creating a discrepancy between training and inference.
We show that the flawed design causes real problems in existing implementations.
In Stable Diffusion, it severely limits the model to only generate images with medium brightness and
prevents it from generating very bright and dark samples. We propose a few simple fixes:`),ft=p(),_=s("ul"),Ge=s("li"),ro=i("(1) rescale the noise schedule to enforce zero terminal SNR;"),no=p(),Je=s("li"),io=i("(2) train the model with v prediction;"),ao=p(),Pe=s("li"),lo=i("(3) change the sampler to always start from the last timestep;"),co=p(),Ce=s("li"),po=i(`(4) rescale classifier-free guidance to prevent over-exposure.
These simple changes ensure the diffusion process is congruent between training and inference and
allow the model to generate samples more faithful to the original data distribution.*`),ht=p(),E=s("p"),fo=i("You can apply all of these changes in "),Ve=s("code"),ho=i("diffusers"),uo=i(" when using "),me=s("a"),mo=i("DDIMScheduler"),go=i(":"),ut=p(),ge=s("ul"),We=s("li"),_o=i("(1) rescale the noise schedule to enforce zero terminal SNR;"),mt=p(),v(O.$$.fragment),gt=p(),P=s("ul"),S=s("li"),vo=i(`(2) train the model with v prediction;
Continue fine-tuning a checkpoint with `),K=s("a"),Fe=s("code"),bo=i("train_text_to_image.py"),yo=i(" or "),ee=s("a"),Ae=s("code"),Do=i("train_text_to_image_lora.py"),wo=i(`
and `),Re=s("code"),Mo=i('--prediction_type="v_prediction"'),So=i("."),xo=p(),Be=s("li"),Eo=i("(3) change the sampler to always start from the last timestep;"),_t=p(),v(te.$$.fragment),vt=p(),_e=s("ul"),Le=s("li"),Io=i("(4) rescale classifier-free guidance to prevent over-exposure."),bt=p(),v(oe.$$.fragment),yt=p(),I=s("p"),$o=i("An example is to use "),se=s("a"),To=i("this checkpoint"),No=i(`
which has been fine-tuned using the `),qe=s("code"),ko=i('"v_prediction"'),jo=i("."),Dt=p(),ve=s("p"),Uo=i("The checkpoint can then be run in inference as follows:"),wt=p(),v(re.$$.fragment),Mt=p(),j=s("h2"),C=s("a"),Xe=s("span"),v(ne.$$.fragment),Zo=p(),Qe=s("span"),Go=i("DDIMScheduler"),St=p(),h=s("div"),v(ie.$$.fragment),Jo=p(),ze=s("p"),Po=i(`Denoising diffusion implicit models is a scheduler that extends the denoising procedure introduced in denoising
diffusion probabilistic models (DDPMs) with non-Markovian guidance.`),Co=p(),u=s("p"),be=s("a"),Vo=i("~ConfigMixin"),Wo=i(" takes care of storing all config attributes that are passed in the scheduler\u2019s "),Ye=s("code"),Fo=i("__init__"),Ao=i(`
function, such as `),He=s("code"),Ro=i("num_train_timesteps"),Bo=i(". They can be accessed via "),Oe=s("code"),Lo=i("scheduler.config.num_train_timesteps"),qo=i(`.
`),ye=s("a"),Xo=i("SchedulerMixin"),Qo=i(" provides general loading and saving functionality via the "),De=s("a"),zo=i("SchedulerMixin.save_pretrained()"),Yo=i(` and
`),we=s("a"),Ho=i("from_pretrained()"),Oo=i(" functions."),Ko=p(),Me=s("p"),es=i("For more details, see the original paper: "),ae=s("a"),ts=i("https://arxiv.org/abs/2010.02502"),os=p(),V=s("div"),v(le.$$.fragment),ss=p(),Ke=s("p"),rs=i(`Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
current timestep.`),ns=p(),W=s("div"),v(de.$$.fragment),is=p(),et=s("p"),as=i("Sets the discrete timesteps used for the diffusion chain. Supporting function to be run before inference."),ls=p(),F=s("div"),v(ce.$$.fragment),ds=p(),tt=s("p"),cs=i(`Predict the sample at the previous timestep by reversing the SDE. Core function to propagate the diffusion
process from the learned model outputs (most often the predicted noise).`),this.h()},l(e){const l=gr('[data-svelte="svelte-1phssyn"]',document.head);$=r(l,"META",{name:!0,content:!0}),l.forEach(o),ot=f(e),T=r(e,"H1",{class:!0});var Et=n(T);U=r(Et,"A",{id:!0,class:!0,href:!0});var us=n(U);Ie=r(us,"SPAN",{});var ms=n(Ie);b(B.$$.fragment,ms),ms.forEach(o),us.forEach(o),Ct=f(Et),$e=r(Et,"SPAN",{});var gs=n($e);Vt=a(gs,"Denoising Diffusion Implicit Models (DDIM)"),gs.forEach(o),Et.forEach(o),st=f(e),N=r(e,"H2",{class:!0});var It=n(N);Z=r(It,"A",{id:!0,class:!0,href:!0});var _s=n(Z);Te=r(_s,"SPAN",{});var vs=n(Te);b(L.$$.fragment,vs),vs.forEach(o),_s.forEach(o),Wt=f(It),Ne=r(It,"SPAN",{});var bs=n(Ne);Ft=a(bs,"Overview"),bs.forEach(o),It.forEach(o),rt=f(e),q=r(e,"P",{});var ps=n(q);X=r(ps,"A",{href:!0,rel:!0});var ys=n(X);At=a(ys,"Denoising Diffusion Implicit Models"),ys.forEach(o),Rt=a(ps," (DDIM) by Jiaming Song, Chenlin Meng and Stefano Ermon."),ps.forEach(o),nt=f(e),pe=r(e,"P",{});var Ds=n(pe);Bt=a(Ds,"The abstract of the paper is the following:"),Ds.forEach(o),it=f(e),fe=r(e,"P",{});var ws=n(fe);ke=r(ws,"EM",{});var Ms=n(ke);Lt=a(Ms,`Denoising diffusion probabilistic models (DDPMs) have achieved high quality image generation without adversarial training,
yet they require simulating a Markov chain for many steps to produce a sample.
To accelerate sampling, we present denoising diffusion implicit models (DDIMs), a more efficient class of iterative implicit probabilistic models
with the same training procedure as DDPMs. In DDPMs, the generative process is defined as the reverse of a Markovian diffusion process.
We construct a class of non-Markovian diffusion processes that lead to the same training objective, but whose reverse process can be much faster to sample from.
We empirically demonstrate that DDIMs can produce high quality samples 10\xD7 to 50\xD7 faster in terms of wall-clock time compared to DDPMs, allow us to trade off
computation for sample quality, and can perform semantically meaningful image interpolation directly in the latent space.`),Ms.forEach(o),ws.forEach(o),at=f(e),x=r(e,"P",{});var Se=n(x);qt=a(Se,"The original codebase of this paper can be found here: "),Q=r(Se,"A",{href:!0,rel:!0});var Ss=n(Q);Xt=a(Ss,"ermongroup/ddim"),Ss.forEach(o),Qt=a(Se,`.
For questions, feel free to contact the author on `),z=r(Se,"A",{href:!0,rel:!0});var xs=n(z);zt=a(xs,"tsong.me"),xs.forEach(o),Yt=a(Se,"."),Se.forEach(o),lt=f(e),k=r(e,"H3",{class:!0});var $t=n(k);G=r($t,"A",{id:!0,class:!0,href:!0});var Es=n(G);je=r(Es,"SPAN",{});var Is=n(je);b(Y.$$.fragment,Is),Is.forEach(o),Es.forEach(o),Ht=f($t),Ue=r($t,"SPAN",{});var $s=n(Ue);Ot=a($s,'Experimental: "Common Diffusion Noise Schedules and Sample Steps are Flawed":'),$s.forEach(o),$t.forEach(o),dt=f(e),J=r(e,"P",{});var Tt=n(J);Kt=a(Tt,"The paper "),Ze=r(Tt,"STRONG",{});var Ts=n(Ze);H=r(Ts,"A",{href:!0,rel:!0});var Ns=n(H);eo=a(Ns,"Common Diffusion Noise Schedules and Sample Steps are Flawed"),Ns.forEach(o),Ts.forEach(o),to=a(Tt,`
claims that a mismatch between the training and inference settings leads to suboptimal inference generation results for Stable Diffusion.`),Tt.forEach(o),ct=f(e),he=r(e,"P",{});var ks=n(he);oo=a(ks,"The abstract reads as follows:"),ks.forEach(o),pt=f(e),ue=r(e,"P",{});var js=n(ue);so=a(js,`*We discover that common diffusion noise schedules do not enforce the last timestep to have zero signal-to-noise ratio (SNR),
and some implementations of diffusion samplers do not start from the last timestep.
Such designs are flawed and do not reflect the fact that the model is given pure Gaussian noise at inference, creating a discrepancy between training and inference.
We show that the flawed design causes real problems in existing implementations.
In Stable Diffusion, it severely limits the model to only generate images with medium brightness and
prevents it from generating very bright and dark samples. We propose a few simple fixes:`),js.forEach(o),ft=f(e),_=r(e,"UL",{});var A=n(_);Ge=r(A,"LI",{});var Us=n(Ge);ro=a(Us,"(1) rescale the noise schedule to enforce zero terminal SNR;"),Us.forEach(o),no=f(A),Je=r(A,"LI",{});var Zs=n(Je);io=a(Zs,"(2) train the model with v prediction;"),Zs.forEach(o),ao=f(A),Pe=r(A,"LI",{});var Gs=n(Pe);lo=a(Gs,"(3) change the sampler to always start from the last timestep;"),Gs.forEach(o),co=f(A),Ce=r(A,"LI",{});var Js=n(Ce);po=a(Js,`(4) rescale classifier-free guidance to prevent over-exposure.
These simple changes ensure the diffusion process is congruent between training and inference and
allow the model to generate samples more faithful to the original data distribution.*`),Js.forEach(o),A.forEach(o),ht=f(e),E=r(e,"P",{});var xe=n(E);fo=a(xe,"You can apply all of these changes in "),Ve=r(xe,"CODE",{});var Ps=n(Ve);ho=a(Ps,"diffusers"),Ps.forEach(o),uo=a(xe," when using "),me=r(xe,"A",{href:!0});var Cs=n(me);mo=a(Cs,"DDIMScheduler"),Cs.forEach(o),go=a(xe,":"),xe.forEach(o),ut=f(e),ge=r(e,"UL",{});var Vs=n(ge);We=r(Vs,"LI",{});var Ws=n(We);_o=a(Ws,"(1) rescale the noise schedule to enforce zero terminal SNR;"),Ws.forEach(o),Vs.forEach(o),mt=f(e),b(O.$$.fragment,e),gt=f(e),P=r(e,"UL",{});var Nt=n(P);S=r(Nt,"LI",{});var R=n(S);vo=a(R,`(2) train the model with v prediction;
Continue fine-tuning a checkpoint with `),K=r(R,"A",{href:!0,rel:!0});var Fs=n(K);Fe=r(Fs,"CODE",{});var As=n(Fe);bo=a(As,"train_text_to_image.py"),As.forEach(o),Fs.forEach(o),yo=a(R," or "),ee=r(R,"A",{href:!0,rel:!0});var Rs=n(ee);Ae=r(Rs,"CODE",{});var Bs=n(Ae);Do=a(Bs,"train_text_to_image_lora.py"),Bs.forEach(o),Rs.forEach(o),wo=a(R,`
and `),Re=r(R,"CODE",{});var Ls=n(Re);Mo=a(Ls,'--prediction_type="v_prediction"'),Ls.forEach(o),So=a(R,"."),R.forEach(o),xo=f(Nt),Be=r(Nt,"LI",{});var qs=n(Be);Eo=a(qs,"(3) change the sampler to always start from the last timestep;"),qs.forEach(o),Nt.forEach(o),_t=f(e),b(te.$$.fragment,e),vt=f(e),_e=r(e,"UL",{});var Xs=n(_e);Le=r(Xs,"LI",{});var Qs=n(Le);Io=a(Qs,"(4) rescale classifier-free guidance to prevent over-exposure."),Qs.forEach(o),Xs.forEach(o),bt=f(e),b(oe.$$.fragment,e),yt=f(e),I=r(e,"P",{});var Ee=n(I);$o=a(Ee,"An example is to use "),se=r(Ee,"A",{href:!0,rel:!0});var zs=n(se);To=a(zs,"this checkpoint"),zs.forEach(o),No=a(Ee,`
which has been fine-tuned using the `),qe=r(Ee,"CODE",{});var Ys=n(qe);ko=a(Ys,'"v_prediction"'),Ys.forEach(o),jo=a(Ee,"."),Ee.forEach(o),Dt=f(e),ve=r(e,"P",{});var Hs=n(ve);Uo=a(Hs,"The checkpoint can then be run in inference as follows:"),Hs.forEach(o),wt=f(e),b(re.$$.fragment,e),Mt=f(e),j=r(e,"H2",{class:!0});var kt=n(j);C=r(kt,"A",{id:!0,class:!0,href:!0});var Os=n(C);Xe=r(Os,"SPAN",{});var Ks=n(Xe);b(ne.$$.fragment,Ks),Ks.forEach(o),Os.forEach(o),Zo=f(kt),Qe=r(kt,"SPAN",{});var er=n(Qe);Go=a(er,"DDIMScheduler"),er.forEach(o),kt.forEach(o),St=f(e),h=r(e,"DIV",{class:!0});var g=n(h);b(ie.$$.fragment,g),Jo=f(g),ze=r(g,"P",{});var tr=n(ze);Po=a(tr,`Denoising diffusion implicit models is a scheduler that extends the denoising procedure introduced in denoising
diffusion probabilistic models (DDPMs) with non-Markovian guidance.`),tr.forEach(o),Co=f(g),u=r(g,"P",{});var m=n(u);be=r(m,"A",{href:!0});var or=n(be);Vo=a(or,"~ConfigMixin"),or.forEach(o),Wo=a(m," takes care of storing all config attributes that are passed in the scheduler\u2019s "),Ye=r(m,"CODE",{});var sr=n(Ye);Fo=a(sr,"__init__"),sr.forEach(o),Ao=a(m,`
function, such as `),He=r(m,"CODE",{});var rr=n(He);Ro=a(rr,"num_train_timesteps"),rr.forEach(o),Bo=a(m,". They can be accessed via "),Oe=r(m,"CODE",{});var nr=n(Oe);Lo=a(nr,"scheduler.config.num_train_timesteps"),nr.forEach(o),qo=a(m,`.
`),ye=r(m,"A",{href:!0});var ir=n(ye);Xo=a(ir,"SchedulerMixin"),ir.forEach(o),Qo=a(m," provides general loading and saving functionality via the "),De=r(m,"A",{href:!0});var ar=n(De);zo=a(ar,"SchedulerMixin.save_pretrained()"),ar.forEach(o),Yo=a(m,` and
`),we=r(m,"A",{href:!0});var lr=n(we);Ho=a(lr,"from_pretrained()"),lr.forEach(o),Oo=a(m," functions."),m.forEach(o),Ko=f(g),Me=r(g,"P",{});var fs=n(Me);es=a(fs,"For more details, see the original paper: "),ae=r(fs,"A",{href:!0,rel:!0});var dr=n(ae);ts=a(dr,"https://arxiv.org/abs/2010.02502"),dr.forEach(o),fs.forEach(o),os=f(g),V=r(g,"DIV",{class:!0});var jt=n(V);b(le.$$.fragment,jt),ss=f(jt),Ke=r(jt,"P",{});var cr=n(Ke);rs=a(cr,`Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
current timestep.`),cr.forEach(o),jt.forEach(o),ns=f(g),W=r(g,"DIV",{class:!0});var Ut=n(W);b(de.$$.fragment,Ut),is=f(Ut),et=r(Ut,"P",{});var pr=n(et);as=a(pr,"Sets the discrete timesteps used for the diffusion chain. Supporting function to be run before inference."),pr.forEach(o),Ut.forEach(o),ls=f(g),F=r(g,"DIV",{class:!0});var Zt=n(F);b(ce.$$.fragment,Zt),ds=f(Zt),tt=r(Zt,"P",{});var fr=n(tt);cs=a(fr,`Predict the sample at the previous timestep by reversing the SDE. Core function to propagate the diffusion
process from the learned model outputs (most often the predicted noise).`),fr.forEach(o),Zt.forEach(o),g.forEach(o),this.h()},h(){d($,"name","hf:doc:metadata"),d($,"content",JSON.stringify(yr)),d(U,"id","denoising-diffusion-implicit-models-ddim"),d(U,"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"),d(U,"href","#denoising-diffusion-implicit-models-ddim"),d(T,"class","relative group"),d(Z,"id","overview"),d(Z,"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"),d(Z,"href","#overview"),d(N,"class","relative group"),d(X,"href","https://arxiv.org/abs/2010.02502"),d(X,"rel","nofollow"),d(Q,"href","https://github.com/ermongroup/ddim"),d(Q,"rel","nofollow"),d(z,"href","https://tsong.me/"),d(z,"rel","nofollow"),d(G,"id","experimental-common-diffusion-noise-schedules-and-sample-steps-are-flawed"),d(G,"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"),d(G,"href","#experimental-common-diffusion-noise-schedules-and-sample-steps-are-flawed"),d(k,"class","relative group"),d(H,"href","https://arxiv.org/abs/2305.08891"),d(H,"rel","nofollow"),d(me,"href","/docs/diffusers/v0.17.0/en/api/schedulers/ddim#diffusers.DDIMScheduler"),d(K,"href","https://github.com/huggingface/diffusers/blob/main/examples/text_to_image/train_text_to_image.py"),d(K,"rel","nofollow"),d(ee,"href","https://github.com/huggingface/diffusers/blob/main/examples/text_to_image/train_text_to_image_lora.py"),d(ee,"rel","nofollow"),d(se,"href","https://huggingface.co/ptx0/pseudo-journey-v2"),d(se,"rel","nofollow"),d(C,"id","diffusers.DDIMScheduler"),d(C,"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"),d(C,"href","#diffusers.DDIMScheduler"),d(j,"class","relative group"),d(be,"href","/docs/diffusers/v0.17.0/en/api/configuration#diffusers.ConfigMixin"),d(ye,"href","/docs/diffusers/v0.17.0/en/api/schedulers/overview#diffusers.SchedulerMixin"),d(De,"href","/docs/diffusers/v0.17.0/en/api/schedulers/overview#diffusers.SchedulerMixin.save_pretrained"),d(we,"href","/docs/diffusers/v0.17.0/en/api/schedulers/overview#diffusers.SchedulerMixin.from_pretrained"),d(ae,"href","https://arxiv.org/abs/2010.02502"),d(ae,"rel","nofollow"),d(V,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),d(W,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),d(F,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),d(h,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8")},m(e,l){t(document.head,$),c(e,ot,l),c(e,T,l),t(T,U),t(U,Ie),y(B,Ie,null),t(T,Ct),t(T,$e),t($e,Vt),c(e,st,l),c(e,N,l),t(N,Z),t(Z,Te),y(L,Te,null),t(N,Wt),t(N,Ne),t(Ne,Ft),c(e,rt,l),c(e,q,l),t(q,X),t(X,At),t(q,Rt),c(e,nt,l),c(e,pe,l),t(pe,Bt),c(e,it,l),c(e,fe,l),t(fe,ke),t(ke,Lt),c(e,at,l),c(e,x,l),t(x,qt),t(x,Q),t(Q,Xt),t(x,Qt),t(x,z),t(z,zt),t(x,Yt),c(e,lt,l),c(e,k,l),t(k,G),t(G,je),y(Y,je,null),t(k,Ht),t(k,Ue),t(Ue,Ot),c(e,dt,l),c(e,J,l),t(J,Kt),t(J,Ze),t(Ze,H),t(H,eo),t(J,to),c(e,ct,l),c(e,he,l),t(he,oo),c(e,pt,l),c(e,ue,l),t(ue,so),c(e,ft,l),c(e,_,l),t(_,Ge),t(Ge,ro),t(_,no),t(_,Je),t(Je,io),t(_,ao),t(_,Pe),t(Pe,lo),t(_,co),t(_,Ce),t(Ce,po),c(e,ht,l),c(e,E,l),t(E,fo),t(E,Ve),t(Ve,ho),t(E,uo),t(E,me),t(me,mo),t(E,go),c(e,ut,l),c(e,ge,l),t(ge,We),t(We,_o),c(e,mt,l),y(O,e,l),c(e,gt,l),c(e,P,l),t(P,S),t(S,vo),t(S,K),t(K,Fe),t(Fe,bo),t(S,yo),t(S,ee),t(ee,Ae),t(Ae,Do),t(S,wo),t(S,Re),t(Re,Mo),t(S,So),t(P,xo),t(P,Be),t(Be,Eo),c(e,_t,l),y(te,e,l),c(e,vt,l),c(e,_e,l),t(_e,Le),t(Le,Io),c(e,bt,l),y(oe,e,l),c(e,yt,l),c(e,I,l),t(I,$o),t(I,se),t(se,To),t(I,No),t(I,qe),t(qe,ko),t(I,jo),c(e,Dt,l),c(e,ve,l),t(ve,Uo),c(e,wt,l),y(re,e,l),c(e,Mt,l),c(e,j,l),t(j,C),t(C,Xe),y(ne,Xe,null),t(j,Zo),t(j,Qe),t(Qe,Go),c(e,St,l),c(e,h,l),y(ie,h,null),t(h,Jo),t(h,ze),t(ze,Po),t(h,Co),t(h,u),t(u,be),t(be,Vo),t(u,Wo),t(u,Ye),t(Ye,Fo),t(u,Ao),t(u,He),t(He,Ro),t(u,Bo),t(u,Oe),t(Oe,Lo),t(u,qo),t(u,ye),t(ye,Xo),t(u,Qo),t(u,De),t(De,zo),t(u,Yo),t(u,we),t(we,Ho),t(u,Oo),t(h,Ko),t(h,Me),t(Me,es),t(Me,ae),t(ae,ts),t(h,os),t(h,V),y(le,V,null),t(V,ss),t(V,Ke),t(Ke,rs),t(h,ns),t(h,W),y(de,W,null),t(W,is),t(W,et),t(et,as),t(h,ls),t(h,F),y(ce,F,null),t(F,ds),t(F,tt),t(tt,cs),xt=!0},p:_r,i(e){xt||(D(B.$$.fragment,e),D(L.$$.fragment,e),D(Y.$$.fragment,e),D(O.$$.fragment,e),D(te.$$.fragment,e),D(oe.$$.fragment,e),D(re.$$.fragment,e),D(ne.$$.fragment,e),D(ie.$$.fragment,e),D(le.$$.fragment,e),D(de.$$.fragment,e),D(ce.$$.fragment,e),xt=!0)},o(e){w(B.$$.fragment,e),w(L.$$.fragment,e),w(Y.$$.fragment,e),w(O.$$.fragment,e),w(te.$$.fragment,e),w(oe.$$.fragment,e),w(re.$$.fragment,e),w(ne.$$.fragment,e),w(ie.$$.fragment,e),w(le.$$.fragment,e),w(de.$$.fragment,e),w(ce.$$.fragment,e),xt=!1},d(e){o($),e&&o(ot),e&&o(T),M(B),e&&o(st),e&&o(N),M(L),e&&o(rt),e&&o(q),e&&o(nt),e&&o(pe),e&&o(it),e&&o(fe),e&&o(at),e&&o(x),e&&o(lt),e&&o(k),M(Y),e&&o(dt),e&&o(J),e&&o(ct),e&&o(he),e&&o(pt),e&&o(ue),e&&o(ft),e&&o(_),e&&o(ht),e&&o(E),e&&o(ut),e&&o(ge),e&&o(mt),M(O,e),e&&o(gt),e&&o(P),e&&o(_t),M(te,e),e&&o(vt),e&&o(_e),e&&o(bt),M(oe,e),e&&o(yt),e&&o(I),e&&o(Dt),e&&o(ve),e&&o(wt),M(re,e),e&&o(Mt),e&&o(j),M(ne),e&&o(St),e&&o(h),M(ie),M(le),M(de),M(ce)}}}const yr={local:"denoising-diffusion-implicit-models-ddim",sections:[{local:"overview",sections:[{local:"experimental-common-diffusion-noise-schedules-and-sample-steps-are-flawed",title:'Experimental: "Common Diffusion Noise Schedules and Sample Steps are Flawed": '}],title:"Overview"},{local:"diffusers.DDIMScheduler",title:"DDIMScheduler"}],title:"Denoising Diffusion Implicit Models (DDIM)"};function Dr(hs){return vr(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Er extends hr{constructor($){super();ur(this,$,Dr,br,mr,{})}}export{Er as default,yr as metadata};
