Buckets:

rtrm's picture
download
raw
15.5 kB
import{s as $e,o as Re,n as Ee}from"../chunks/scheduler.182ea377.js";import{S as Ve,i as Ne,g as n,s as i,p as R,A as He,h as a,f as l,c as o,j as E,q as V,m as c,k as d,v as m,a as s,r as N,d as H,t as S,u as z}from"../chunks/index.008d68e4.js";import{T as Se}from"../chunks/Tip.4f096367.js";import{I as Xe}from"../chunks/IconCopyLink.96bbb92b.js";import{C as Me}from"../chunks/CodeBlock.5ed6eb7b.js";function ze(L){let p,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(){p=n("p"),p.innerHTML=M},l(r){p=a(r,"P",{"data-svelte-h":!0}),c(p)!=="svelte-1wsblrf"&&(p.innerHTML=M)},m(r,u){s(r,p,u)},p:Ee,d(r){r&&l(p)}}}function Pe(L){let p,M,r,u,P,w,he,X,we="Pipeline callbacks",Q,y,ye="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.",q,J,Je="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.",D,_,_e="The callback function should have the following arguments:",K,j,je="<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>",O,T,Te="Your callback function should look something like this:",ee,g,te,U,ge="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>.",le,k,se,v,Ue="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.",ne,C,ke="With callbacks, you can implement features such as dynamic CFG without having to modify the underlying code at all!",ae,h,ie,f,b,F,I,be,$,ve="Using Callbacks to interrupt the Diffusion Process",oe,Z,Ce="The following Pipelines support interrupting the diffusion process via callback",pe,B,Ie='<li><a href="../api/pipelines/stable_diffusion/overview.md">StableDiffusionPipeline</a></li> <li><a href="..api/pipelines/stable_diffusion/img2img.md">StableDiffusionImg2ImgPipeline</a></li> <li><a href="..api/pipelines/stable_diffusion/inpaint.md">StableDiffusionInpaintPipeline</a></li> <li><a href="../api/pipelines/stable_diffusion/stable_diffusion_xl.md">StableDiffusionXLPipeline</a></li> <li><a href="../api/pipelines/stable_diffusion/stable_diffusion_xl.md">StableDiffusionXLImg2ImgPipeline</a></li> <li><a href="../api/pipelines/stable_diffusion/stable_diffusion_xl.md">StableDiffusionXLInpaintPipeline</a></li>',ce,x,Ze="Interrupting the diffusion process is particularly useful when building UIs that work with Diffusers because it allows users to stop the generation process if they’re unhappy with the intermediate results. You can incorporate this into your pipeline with a callback.",re,G,Be="This callback function should take the following arguments: <code>pipe</code>, <code>i</code>, <code>t</code>, and <code>callback_kwargs</code> (this must be returned). Set the pipeline’s <code>_interrupt</code> attribute to <code>True</code> to stop the diffusion process after a certain number of steps. You are also free to implement your own custom stopping logic inside the callback.",ue,A,xe="In this example, the diffusion process is stopped after 10 ste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MCUyMCUyMCUyMCUyMCUyMCUyMHJldHVybiUyMGNhbGxiYWNrX2t3YXJncw==",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`}}),k=new Me({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>)`}}),h=new Se({props:{$$slots:{default:[ze]},$$scope:{ctx:L}}}),I=new Xe({}),Y=new Me({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZSUyMCUzRCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiklMEFwaXBlLmVuYWJsZV9tb2RlbF9jcHVfb2ZmbG9hZCgpJTBBbnVtX2luZmVyZW5jZV9zdGVwcyUyMCUzRCUyMDUwJTBBJTBBZGVmJTIwaW50ZXJydXB0X2NhbGxiYWNrKHBpcGUlMkMlMjBpJTJDJTIwdCUyQyUyMGNhbGxiYWNrX2t3YXJncyklM0ElMEElMjAlMjAlMjAlMjBzdG9wX2lkeCUyMCUzRCUyMDEwJTBBJTIwJTIwJTIwJTIwaWYlMjBpJTIwJTNEJTNEJTIwc3RvcF9pZHglM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwaXBlLl9pbnRlcnJ1cHQlMjAlM0QlMjBUcnVlJTBBJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwY2FsbGJhY2tfa3dhcmdzJTBBJTBBcGlwZSglMEElMjAlMjAlMjAlMjAlMjJBJTIwcGhvdG8lMjBvZiUyMGElMjBjYXQlMjIlMkMlMEElMjAlMjAlMjAlMjBudW1faW5mZXJlbmNlX3N0ZXBzJTNEbnVtX2luZmVyZW5jZV9zdGVwcyUyQyUwQSUyMCUyMCUyMCUyMGNhbGxiYWNrX29uX3N0ZXBfZW5kJTNEaW50ZXJydXB0X2NhbGxiYWNrJTJDJTBBKQ==",highlighted:`<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>)
pipe.enable_model_cpu_offload()
num_inference_steps = <span class="hljs-number">50</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">interrupt_callback</span>(<span class="hljs-params">pipe, i, t, callback_kwargs</span>):
stop_idx = <span class="hljs-number">10</span>
<span class="hljs-keyword">if</span> i == stop_idx:
pipe._interrupt = <span class="hljs-literal">True</span>
<span class="hljs-keyword">return</span> callback_kwargs
pipe(
<span class="hljs-string">&quot;A photo of a cat&quot;</span>,
num_inference_steps=num_inference_steps,
callback_on_step_end=interrupt_callback,
)`}}),{c(){p=n("meta"),M=i(),r=n("h1"),u=n("a"),P=n("span"),R(w.$$.fragment),he=i(),X=n("span"),X.textContent=we,Q=i(),y=n("p"),y.innerHTML=ye,q=i(),J=n("p"),J.innerHTML=Je,D=i(),_=n("p"),_.textContent=_e,K=i(),j=n("ul"),j.innerHTML=je,O=i(),T=n("p"),T.textContent=Te,ee=i(),R(g.$$.fragment),te=i(),U=n("p"),U.innerHTML=ge,le=i(),R(k.$$.fragment),se=i(),v=n("p"),v.textContent=Ue,ne=i(),C=n("p"),C.textContent=ke,ae=i(),R(h.$$.fragment),ie=i(),f=n("h2"),b=n("a"),F=n("span"),R(I.$$.fragment),be=i(),$=n("span"),$.textContent=ve,oe=i(),Z=n("p"),Z.textContent=Ce,pe=i(),B=n("ul"),B.innerHTML=Ie,ce=i(),x=n("p"),x.textContent=Ze,re=i(),G=n("p"),G.innerHTML=Be,ue=i(),A=n("p"),A.innerHTML=xe,de=i(),R(Y.$$.fragment),this.h()},l(e){const t=He("svelte-1phssyn",document.head);p=a(t,"META",{name:!0,content:!0}),t.forEach(l),M=o(e),r=a(e,"H1",{class:!0});var W=E(r);u=a(W,"A",{id:!0,class:!0,href:!0});var Ge=E(u);P=a(Ge,"SPAN",{});var Ae=E(P);V(w.$$.fragment,Ae),Ae.forEach(l),Ge.forEach(l),he=o(W),X=a(W,"SPAN",{"data-svelte-h":!0}),c(X)!=="svelte-1wzwks3"&&(X.textContent=we),W.forEach(l),Q=o(e),y=a(e,"P",{"data-svelte-h":!0}),c(y)!=="svelte-z3ufgq"&&(y.innerHTML=ye),q=o(e),J=a(e,"P",{"data-svelte-h":!0}),c(J)!=="svelte-1e9w3c8"&&(J.innerHTML=Je),D=o(e),_=a(e,"P",{"data-svelte-h":!0}),c(_)!=="svelte-gxr3fz"&&(_.textContent=_e),K=o(e),j=a(e,"UL",{"data-svelte-h":!0}),c(j)!=="svelte-gz6h7k"&&(j.innerHTML=je),O=o(e),T=a(e,"P",{"data-svelte-h":!0}),c(T)!=="svelte-1s2b9st"&&(T.textContent=Te),ee=o(e),V(g.$$.fragment,e),te=o(e),U=a(e,"P",{"data-svelte-h":!0}),c(U)!=="svelte-y3yxaw"&&(U.innerHTML=ge),le=o(e),V(k.$$.fragment,e),se=o(e),v=a(e,"P",{"data-svelte-h":!0}),c(v)!=="svelte-1531nu7"&&(v.textContent=Ue),ne=o(e),C=a(e,"P",{"data-svelte-h":!0}),c(C)!=="svelte-14y2as5"&&(C.textContent=ke),ae=o(e),V(h.$$.fragment,e),ie=o(e),f=a(e,"H2",{class:!0});var me=E(f);b=a(me,"A",{id:!0,class:!0,href:!0});var Ye=E(b);F=a(Ye,"SPAN",{});var We=E(F);V(I.$$.fragment,We),We.forEach(l),Ye.forEach(l),be=o(me),$=a(me,"SPAN",{"data-svelte-h":!0}),c($)!=="svelte-6xnjdk"&&($.textContent=ve),me.forEach(l),oe=o(e),Z=a(e,"P",{"data-svelte-h":!0}),c(Z)!=="svelte-1v6aptz"&&(Z.textContent=Ce),pe=o(e),B=a(e,"UL",{"data-svelte-h":!0}),c(B)!=="svelte-uxt8z2"&&(B.innerHTML=Ie),ce=o(e),x=a(e,"P",{"data-svelte-h":!0}),c(x)!=="svelte-hvg75e"&&(x.textContent=Ze),re=o(e),G=a(e,"P",{"data-svelte-h":!0}),c(G)!=="svelte-1q7cqry"&&(G.innerHTML=Be),ue=o(e),A=a(e,"P",{"data-svelte-h":!0}),c(A)!=="svelte-s21qgj"&&(A.innerHTML=xe),de=o(e),V(Y.$$.fragment,e),this.h()},h(){d(p,"name","hf:doc:metadata"),d(p,"content",JSON.stringify(Fe)),d(u,"id","pipeline-callbacks"),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","#pipeline-callbacks"),d(r,"class","relative group"),d(b,"id","using-callbacks-to-interrupt-the-diffusion-process"),d(b,"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(b,"href","#using-callbacks-to-interrupt-the-diffusion-process"),d(f,"class","relative group")},m(e,t){m(document.head,p),s(e,M,t),s(e,r,t),m(r,u),m(u,P),N(w,P,null),m(r,he),m(r,X),s(e,Q,t),s(e,y,t),s(e,q,t),s(e,J,t),s(e,D,t),s(e,_,t),s(e,K,t),s(e,j,t),s(e,O,t),s(e,T,t),s(e,ee,t),N(g,e,t),s(e,te,t),s(e,U,t),s(e,le,t),N(k,e,t),s(e,se,t),s(e,v,t),s(e,ne,t),s(e,C,t),s(e,ae,t),N(h,e,t),s(e,ie,t),s(e,f,t),m(f,b),m(b,F),N(I,F,null),m(f,be),m(f,$),s(e,oe,t),s(e,Z,t),s(e,pe,t),s(e,B,t),s(e,ce,t),s(e,x,t),s(e,re,t),s(e,G,t),s(e,ue,t),s(e,A,t),s(e,de,t),N(Y,e,t),fe=!0},p(e,[t]){const W={};t&2&&(W.$$scope={dirty:t,ctx:e}),h.$set(W)},i(e){fe||(H(w.$$.fragment,e),H(g.$$.fragment,e),H(k.$$.fragment,e),H(h.$$.fragment,e),H(I.$$.fragment,e),H(Y.$$.fragment,e),fe=!0)},o(e){S(w.$$.fragment,e),S(g.$$.fragment,e),S(k.$$.fragment,e),S(h.$$.fragment,e),S(I.$$.fragment,e),S(Y.$$.fragment,e),fe=!1},d(e){e&&(l(M),l(r),l(Q),l(y),l(q),l(J),l(D),l(_),l(K),l(j),l(O),l(T),l(ee),l(te),l(U),l(le),l(se),l(v),l(ne),l(C),l(ae),l(ie),l(f),l(oe),l(Z),l(pe),l(B),l(ce),l(x),l(re),l(G),l(ue),l(A),l(de)),l(p),z(w),z(g,e),z(k,e),z(h,e),z(I),z(Y,e)}}}const Fe={local:"pipeline-callbacks",sections:[{local:"using-callbacks-to-interrupt-the-diffusion-process",title:"Using Callbacks to interrupt the Diffusion Process"}],title:"Pipeline callbacks"};function Le(L){return Re(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class et extends Ve{constructor(p){super(),Ne(this,p,Le,Pe,$e,{})}}export{et as component};

Xet Storage Details

Size:
15.5 kB
·
Xet hash:
84a41806bed1e7b5820d8726a0c418f6e19009de26732b19b0d379c7bc742d33

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