import{s as ae,o as ne,n as ie}from"../chunks/scheduler.182ea377.js";import{S as ce,i as oe,g as c,s as n,r as X,A as pe,h as o,f as s,c as i,j as te,u as V,x as r,k as se,y as re,a as l,v as z,d as E,t as F,w as N}from"../chunks/index.abf12888.js";import{T as de}from"../chunks/Tip.230e2334.js";import{C as le}from"../chunks/CodeBlock.57fe6e13.js";import{H as me}from"../chunks/Heading.16916d63.js";function ue(I){let a,m='🤗 Diffusers currently only supports <code>callback_on_step_end</code>, but feel free to open a <a href="https://github.com/huggingface/diffusers/issues/new/choose" rel="nofollow">feature request</a> if you have a cool use-case and require a callback function with a different execution point!';return{c(){a=c("p"),a.innerHTML=m},l(p){a=o(p,"P",{"data-svelte-h":!0}),r(a)!=="svelte-1wsblrf"&&(a.innerHTML=m)},m(p,U){l(p,a,U)},p:ie,d(p){p&&s(a)}}}function fe(I){let a,m,p,U,u,C,f,Q="The denoising loop of a pipeline can be modified with custom defined functions using the <code>callback_on_step_end</code> parameter. This can be really useful for <em>dynamically</em> adjusting certain pipeline attributes, or modifying tensor variables. The flexibility of callbacks opens up some interesting use-cases such as changing the prompt embeddings at each timestep, assigning different weights to the prompt embeddings, and editing the guidance scale.",k,h,L="This guide will show you how to use the <code>callback_on_step_end</code> parameter to disable classifier-free guidance (CFG) after 40% of the inference steps to save compute with minimal cost to performance.",Z,b,S="The callback function should have the following arguments:",v,M,q="<li><code>pipe</code> (or the pipeline instance) provides access to useful properties such as <code>num_timestep</code> and <code>guidance_scale</code>. You can modify these properties by updating the underlying attributes. For this example, you’ll disable CFG by setting <code>pipe._guidance_scale=0.0</code>.</li> <li><code>step_index</code> and <code>timestep</code> tell you where you are in the denoising loop. Use <code>step_index</code> to turn off CFG after reaching 40% of <code>num_timestep</code>.</li> <li><code>callback_kwargs</code> is a dict that contains tensor variables you can modify during the denoising loop. It only includes variables specified in the <code>callback_on_step_end_tensor_inputs</code> argument, which is passed to the pipeline’s <code>__call__</code> method. Different pipelines may use different sets of variables, so please check a pipeline’s <code>_callback_tensor_inputs</code> attribute for the list of variables you can modify. Some common variables include <code>latents</code> and <code>prompt_embeds</code>. For this function, change the batch size of <code>prompt_embeds</code> after setting <code>guidance_scale=0.0</code> in order for it to work properly.</li>",x,w,P="Your callback function should look something like this:",G,J,Y,y,D="Now, you can pass the callback function to the <code>callback_on_step_end</code> parameter and the <code>prompt_embeds</code> to <code>callback_on_step_end_tensor_inputs</code>.",R,_,B,T,K="The callback function is executed at the end of each denoising step, and modifies the pipeline attributes and tensor variables for the next denoising step.",$,j,O="With callbacks, you can implement features such as dynamic CFG without having to modify the underlying code at all!",W,d,A,g,H;return u=new me({props:{title:"Pipeline callbacks",local:"pipeline-callbacks",headingTag:"h1"}}),J=new le({props:{code:"ZGVmJTIwY2FsbGJhY2tfZHluYW1pY19jZmcocGlwZSUyQyUyMHN0ZXBfaW5kZXglMkMlMjB0aW1lc3RlcCUyQyUyMGNhbGxiYWNrX2t3YXJncyklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjMlMjBhZGp1c3QlMjB0aGUlMjBiYXRjaF9zaXplJTIwb2YlMjBwcm9tcHRfZW1iZWRzJTIwYWNjb3JkaW5nJTIwdG8lMjBndWlkYW5jZV9zY2FsZSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGlmJTIwc3RlcF9pbmRleCUyMCUzRCUzRCUyMGludChwaXBlLm51bV90aW1lc3RlcCUyMColMjAwLjQpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcHJvbXB0X2VtYmVkcyUyMCUzRCUyMGNhbGxiYWNrX2t3YXJncyU1QiUyMnByb21wdF9lbWJlZHMlMjIlNUQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcm9tcHRfZW1iZWRzJTIwJTNEJTIwcHJvbXB0X2VtYmVkcy5jaHVuaygyKSU1Qi0xJTVEJTBBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIzJTIwdXBkYXRlJTIwZ3VpZGFuY2Vfc2NhbGUlMjBhbmQlMjBwcm9tcHRfZW1iZWRzJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcGlwZS5fZ3VpZGFuY2Vfc2NhbGUlMjAlM0QlMjAwLjAlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBjYWxsYmFja19rd2FyZ3MlNUIlMjJwcm9tcHRfZW1iZWRzJTIyJTVEJTIwJTNEJTIwcHJvbXB0X2VtYmVkcyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHJldHVybiUyMGNhbGxiYWNrX2t3YXJncw==",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">callback_dynamic_cfg</span>(<span class="hljs-params">pipe, step_index, timestep, callback_kwargs</span>):
        <span class="hljs-comment"># adjust the batch_size of prompt_embeds according to guidance_scale</span>
        <span class="hljs-keyword">if</span> step_index == <span class="hljs-built_in">int</span>(pipe.num_timestep * <span class="hljs-number">0.4</span>):
                prompt_embeds = callback_kwargs[<span class="hljs-string">&quot;prompt_embeds&quot;</span>]
                prompt_embeds = prompt_embeds.chunk(<span class="hljs-number">2</span>)[-<span class="hljs-number">1</span>]

        <span class="hljs-comment"># update guidance_scale and prompt_embeds</span>
        pipe._guidance_scale = <span class="hljs-number">0.0</span>
        callback_kwargs[<span class="hljs-string">&quot;prompt_embeds&quot;</span>] = prompt_embeds
        <span class="hljs-keyword">return</span> callback_kwargs`,wrap:!1}}),_=new le({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlJTIwJTNEJTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUyMnJ1bndheW1sJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTJDJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2KSUwQXBpcGUlMjAlM0QlMjBwaXBlLnRvKCUyMmN1ZGElMjIpJTBBJTBBcHJvbXB0JTIwJTNEJTIwJTIyYSUyMHBob3RvJTIwb2YlMjBhbiUyMGFzdHJvbmF1dCUyMHJpZGluZyUyMGElMjBob3JzZSUyMG9uJTIwbWFycyUyMiUwQSUwQWdlbmVyYXRvciUyMCUzRCUyMHRvcmNoLkdlbmVyYXRvcihkZXZpY2UlM0QlMjJjdWRhJTIyKS5tYW51YWxfc2VlZCgxKSUwQW91dCUyMCUzRCUyMHBpcGUocHJvbXB0JTJDJTIwZ2VuZXJhdG9yJTNEZ2VuZXJhdG9yJTJDJTIwY2FsbGJhY2tfb25fc3RlcF9lbmQlM0RjYWxsYmFja19jdXN0b21fY2ZnJTJDJTIwY2FsbGJhY2tfb25fc3RlcF9lbmRfdGVuc29yX2lucHV0cyUzRCU1Qidwcm9tcHRfZW1iZWRzJyU1RCklMEElMEFvdXQuaW1hZ2VzJTVCMCU1RC5zYXZlKCUyMm91dF9jdXN0b21fY2ZnLnBuZyUyMik=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>, torch_dtype=torch.float16)
pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)

prompt = <span class="hljs-string">&quot;a photo of an astronaut riding a horse on mars&quot;</span>

generator = torch.Generator(device=<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(<span class="hljs-number">1</span>)
out = pipe(prompt, generator=generator, callback_on_step_end=callback_custom_cfg, callback_on_step_end_tensor_inputs=[<span class="hljs-string">&#x27;prompt_embeds&#x27;</span>])

out.images[<span class="hljs-number">0</span>].save(<span class="hljs-string">&quot;out_custom_cfg.png&quot;</span>)`,wrap:!1}}),d=new de({props:{$$slots:{default:[ue]},$$scope:{ctx:I}}}),{c(){a=c("meta"),m=n(),p=c("p"),U=n(),X(u.$$.fragment),C=n(),f=c("p"),f.innerHTML=Q,k=n(),h=c("p"),h.innerHTML=L,Z=n(),b=c("p"),b.textContent=S,v=n(),M=c("ul"),M.innerHTML=q,x=n(),w=c("p"),w.textContent=P,G=n(),X(J.$$.fragment),Y=n(),y=c("p"),y.innerHTML=D,R=n(),X(_.$$.fragment),B=n(),T=c("p"),T.textContent=K,$=n(),j=c("p"),j.textContent=O,W=n(),X(d.$$.fragment),A=n(),g=c("p"),this.h()},l(e){const t=pe("svelte-u9bgzb",document.head);a=o(t,"META",{name:!0,content:!0}),t.forEach(s),m=i(e),p=o(e,"P",{}),te(p).forEach(s),U=i(e),V(u.$$.fragment,e),C=i(e),f=o(e,"P",{"data-svelte-h":!0}),r(f)!=="svelte-z3ufgq"&&(f.innerHTML=Q),k=i(e),h=o(e,"P",{"data-svelte-h":!0}),r(h)!=="svelte-1e9w3c8"&&(h.innerHTML=L),Z=i(e),b=o(e,"P",{"data-svelte-h":!0}),r(b)!=="svelte-gxr3fz"&&(b.textContent=S),v=i(e),M=o(e,"UL",{"data-svelte-h":!0}),r(M)!=="svelte-gz6h7k"&&(M.innerHTML=q),x=i(e),w=o(e,"P",{"data-svelte-h":!0}),r(w)!=="svelte-1s2b9st"&&(w.textContent=P),G=i(e),V(J.$$.fragment,e),Y=i(e),y=o(e,"P",{"data-svelte-h":!0}),r(y)!=="svelte-y3yxaw"&&(y.innerHTML=D),R=i(e),V(_.$$.fragment,e),B=i(e),T=o(e,"P",{"data-svelte-h":!0}),r(T)!=="svelte-1531nu7"&&(T.textContent=K),$=i(e),j=o(e,"P",{"data-svelte-h":!0}),r(j)!=="svelte-14y2as5"&&(j.textContent=O),W=i(e),V(d.$$.fragment,e),A=i(e),g=o(e,"P",{}),te(g).forEach(s),this.h()},h(){se(a,"name","hf:doc:metadata"),se(a,"content",he)},m(e,t){re(document.head,a),l(e,m,t),l(e,p,t),l(e,U,t),z(u,e,t),l(e,C,t),l(e,f,t),l(e,k,t),l(e,h,t),l(e,Z,t),l(e,b,t),l(e,v,t),l(e,M,t),l(e,x,t),l(e,w,t),l(e,G,t),z(J,e,t),l(e,Y,t),l(e,y,t),l(e,R,t),z(_,e,t),l(e,B,t),l(e,T,t),l(e,$,t),l(e,j,t),l(e,W,t),z(d,e,t),l(e,A,t),l(e,g,t),H=!0},p(e,[t]){const ee={};t&2&&(ee.$$scope={dirty:t,ctx:e}),d.$set(ee)},i(e){H||(E(u.$$.fragment,e),E(J.$$.fragment,e),E(_.$$.fragment,e),E(d.$$.fragment,e),H=!0)},o(e){F(u.$$.fragment,e),F(J.$$.fragment,e),F(_.$$.fragment,e),F(d.$$.fragment,e),H=!1},d(e){e&&(s(m),s(p),s(U),s(C),s(f),s(k),s(h),s(Z),s(b),s(v),s(M),s(x),s(w),s(G),s(Y),s(y),s(R),s(B),s(T),s($),s(j),s(W),s(A),s(g)),s(a),N(u,e),N(J,e),N(_,e),N(d,e)}}}const he='{"title":"Pipeline callbacks","local":"pipeline-callbacks","sections":[],"depth":1}';function be(I){return ne(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Te extends ce{constructor(a){super(),oe(this,a,be,fe,ae,{})}}export{Te as component};
