import{S as $t,i as bt,s as yt,e as n,k as f,w as x,t as i,M as jt,c as o,d as t,m,a as p,x as C,h as l,b as g,N as wt,G as s,g as r,y as O,L as kt,q as G,o as N,B as A,v as Et}from"../../chunks/vendor-hf-doc-builder.js";import{I as qt}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as U}from"../../chunks/CodeBlock-hf-doc-builder.js";function Pt(Ze){let h,se,d,_,z,y,qe,F,Pe,ae,v,Le,j,De,xe,re,c,Ce,K,Oe,Ge,Q,Ne,Ae,ne,w,Ie,I,X,Se,Re,oe,k,ie,S,We,le,E,pe,u,Me,Y,Te,Be,Z,Ve,He,fe,q,me,R,Je,ge,P,ce,W,M,et,ue,$,Ue,ee,ze,Fe,he,L,de,b,Ke,te,Qe,Xe,_e,T,Ye,ve,D,we,B,V,tt,$e;return y=new qt({}),k=new U({props:{code:'prompt = "Labrador in the style of Vermeer"',highlighted:'prompt = <span class="hljs-string">&quot;Labrador in the style of Vermeer&quot;</span>'}}),E=new U({props:{code:`from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
pipe = pipe.to("cuda")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

<span class="hljs-meta">&gt;&gt;&gt; </span>pipe = DiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>, torch_dtype=torch.float16)
<span class="hljs-meta">&gt;&gt;&gt; </span>pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)`}}),q=new U({props:{code:`import torch

generator = [torch.Generator(device="cuda").manual_seed(i) for i in range(4)]`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> torch

<span class="hljs-meta">&gt;&gt;&gt; </span>generator = [torch.Generator(device=<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(i) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">4</span>)]`}}),P=new U({props:{code:`images = pipe(prompt, generator=generator, num_images_per_prompt=4).images
images`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>images = pipe(prompt, generator=generator, num_images_per_prompt=<span class="hljs-number">4</span>).images
<span class="hljs-meta">&gt;&gt;&gt; </span>images`}}),L=new U({props:{code:`prompt = [prompt + t for t in [", highly realistic", ", artsy", ", trending", ", colorful"]]
generator = [torch.Generator(device="cuda").manual_seed(0) for i in range(4)]`,highlighted:`prompt = [prompt + t <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> [<span class="hljs-string">&quot;, highly realistic&quot;</span>, <span class="hljs-string">&quot;, artsy&quot;</span>, <span class="hljs-string">&quot;, trending&quot;</span>, <span class="hljs-string">&quot;, colorful&quot;</span>]]
generator = [torch.Generator(device=<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(<span class="hljs-number">0</span>) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">4</span>)]`}}),D=new U({props:{code:`images = pipe(prompt, generator=generator).images
images`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>images = pipe(prompt, generator=generator).images
<span class="hljs-meta">&gt;&gt;&gt; </span>images`}}),{c(){h=n("meta"),se=f(),d=n("h1"),_=n("a"),z=n("span"),x(y.$$.fragment),qe=f(),F=n("span"),Pe=i("Re-using seeds for fast prompt engineering"),ae=f(),v=n("p"),Le=i(`A common use case when generating images is to generate a batch of images, select one image and improve it with a better, more detailed prompt in a second run.
To do this, one needs to make each generated image of the batch deterministic.
Images are generated by denoising gaussian random noise which can be instantiated by passing a `),j=n("a"),De=i("torch generator"),xe=i("."),re=f(),c=n("p"),Ce=i("Now, for batched generation, we need to make sure that every single generated image in the batch is tied exactly to one seed. In \u{1F9E8} Diffusers, this can be achieved by not passing one "),K=n("code"),Oe=i("generator"),Ge=i(`, but a list
of `),Q=n("code"),Ne=i("generators"),Ae=i(" to the pipeline."),ne=f(),w=n("p"),Ie=i("Let\u2019s go through an example using "),I=n("a"),X=n("code"),Se=i("runwayml/stable-diffusion-v1-5"),Re=i(`.
We want to generate several versions of the prompt:`),oe=f(),x(k.$$.fragment),ie=f(),S=n("p"),We=i("Let\u2019s load the pipeline"),le=f(),x(E.$$.fragment),pe=f(),u=n("p"),Me=i("Now, let\u2019s define 4 different generators, since we would like to reproduce a certain image. We\u2019ll use seeds "),Y=n("code"),Te=i("0"),Be=i(" to "),Z=n("code"),Ve=i("3"),He=i(" to create our generators."),fe=f(),x(q.$$.fragment),me=f(),R=n("p"),Je=i("Let\u2019s generate 4 images:"),ge=f(),x(P.$$.fragment),ce=f(),W=n("p"),M=n("img"),ue=f(),$=n("p"),Ue=i(`Ok, the last images has some double eyes, but the first image looks good!
Let\u2019s try to make the prompt a bit better `),ee=n("strong"),ze=i("while keeping the first seed"),Fe=i(`
so that the images are similar to the first image.`),he=f(),x(L.$$.fragment),de=f(),b=n("p"),Ke=i("We create 4 generators with seed "),te=n("code"),Qe=i("0"),Xe=i(", which is the first seed we used before."),_e=f(),T=n("p"),Ye=i("Let\u2019s run the pipeline again."),ve=f(),x(D.$$.fragment),we=f(),B=n("p"),V=n("img"),this.h()},l(e){const a=jt('[data-svelte="svelte-1phssyn"]',document.head);h=o(a,"META",{name:!0,content:!0}),a.forEach(t),se=m(e),d=o(e,"H1",{class:!0});var be=p(d);_=o(be,"A",{id:!0,class:!0,href:!0});var st=p(_);z=o(st,"SPAN",{});var at=p(z);C(y.$$.fragment,at),at.forEach(t),st.forEach(t),qe=m(be),F=o(be,"SPAN",{});var rt=p(F);Pe=l(rt,"Re-using seeds for fast prompt engineering"),rt.forEach(t),be.forEach(t),ae=m(e),v=o(e,"P",{});var ye=p(v);Le=l(ye,`A common use case when generating images is to generate a batch of images, select one image and improve it with a better, more detailed prompt in a second run.
To do this, one needs to make each generated image of the batch deterministic.
Images are generated by denoising gaussian random noise which can be instantiated by passing a `),j=o(ye,"A",{href:!0,rel:!0});var nt=p(j);De=l(nt,"torch generator"),nt.forEach(t),xe=l(ye,"."),ye.forEach(t),re=m(e),c=o(e,"P",{});var H=p(c);Ce=l(H,"Now, for batched generation, we need to make sure that every single generated image in the batch is tied exactly to one seed. In \u{1F9E8} Diffusers, this can be achieved by not passing one "),K=o(H,"CODE",{});var ot=p(K);Oe=l(ot,"generator"),ot.forEach(t),Ge=l(H,`, but a list
of `),Q=o(H,"CODE",{});var it=p(Q);Ne=l(it,"generators"),it.forEach(t),Ae=l(H," to the pipeline."),H.forEach(t),ne=m(e),w=o(e,"P",{});var je=p(w);Ie=l(je,"Let\u2019s go through an example using "),I=o(je,"A",{href:!0});var lt=p(I);X=o(lt,"CODE",{});var pt=p(X);Se=l(pt,"runwayml/stable-diffusion-v1-5"),pt.forEach(t),lt.forEach(t),Re=l(je,`.
We want to generate several versions of the prompt:`),je.forEach(t),oe=m(e),C(k.$$.fragment,e),ie=m(e),S=o(e,"P",{});var ft=p(S);We=l(ft,"Let\u2019s load the pipeline"),ft.forEach(t),le=m(e),C(E.$$.fragment,e),pe=m(e),u=o(e,"P",{});var J=p(u);Me=l(J,"Now, let\u2019s define 4 different generators, since we would like to reproduce a certain image. We\u2019ll use seeds "),Y=o(J,"CODE",{});var mt=p(Y);Te=l(mt,"0"),mt.forEach(t),Be=l(J," to "),Z=o(J,"CODE",{});var gt=p(Z);Ve=l(gt,"3"),gt.forEach(t),He=l(J," to create our generators."),J.forEach(t),fe=m(e),C(q.$$.fragment,e),me=m(e),R=o(e,"P",{});var ct=p(R);Je=l(ct,"Let\u2019s generate 4 images:"),ct.forEach(t),ge=m(e),C(P.$$.fragment,e),ce=m(e),W=o(e,"P",{});var ut=p(W);M=o(ut,"IMG",{src:!0,alt:!0}),ut.forEach(t),ue=m(e),$=o(e,"P",{});var ke=p($);Ue=l(ke,`Ok, the last images has some double eyes, but the first image looks good!
Let\u2019s try to make the prompt a bit better `),ee=o(ke,"STRONG",{});var ht=p(ee);ze=l(ht,"while keeping the first seed"),ht.forEach(t),Fe=l(ke,`
so that the images are similar to the first image.`),ke.forEach(t),he=m(e),C(L.$$.fragment,e),de=m(e),b=o(e,"P",{});var Ee=p(b);Ke=l(Ee,"We create 4 generators with seed "),te=o(Ee,"CODE",{});var dt=p(te);Qe=l(dt,"0"),dt.forEach(t),Xe=l(Ee,", which is the first seed we used before."),Ee.forEach(t),_e=m(e),T=o(e,"P",{});var _t=p(T);Ye=l(_t,"Let\u2019s run the pipeline again."),_t.forEach(t),ve=m(e),C(D.$$.fragment,e),we=m(e),B=o(e,"P",{});var vt=p(B);V=o(vt,"IMG",{src:!0,alt:!0}),vt.forEach(t),this.h()},h(){g(h,"name","hf:doc:metadata"),g(h,"content",JSON.stringify(Lt)),g(_,"id","reusing-seeds-for-fast-prompt-engineering"),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"),g(_,"href","#reusing-seeds-for-fast-prompt-engineering"),g(d,"class","relative group"),g(j,"href","https://pytorch.org/docs/stable/generated/torch.Generator.html#generator"),g(j,"rel","nofollow"),g(I,"href","runwayml/stable-diffusion-v1-5"),wt(M.src,et="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds.jpg")||g(M,"src",et),g(M,"alt","img"),wt(V.src,tt="https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds_2.jpg")||g(V,"src",tt),g(V,"alt","img")},m(e,a){s(document.head,h),r(e,se,a),r(e,d,a),s(d,_),s(_,z),O(y,z,null),s(d,qe),s(d,F),s(F,Pe),r(e,ae,a),r(e,v,a),s(v,Le),s(v,j),s(j,De),s(v,xe),r(e,re,a),r(e,c,a),s(c,Ce),s(c,K),s(K,Oe),s(c,Ge),s(c,Q),s(Q,Ne),s(c,Ae),r(e,ne,a),r(e,w,a),s(w,Ie),s(w,I),s(I,X),s(X,Se),s(w,Re),r(e,oe,a),O(k,e,a),r(e,ie,a),r(e,S,a),s(S,We),r(e,le,a),O(E,e,a),r(e,pe,a),r(e,u,a),s(u,Me),s(u,Y),s(Y,Te),s(u,Be),s(u,Z),s(Z,Ve),s(u,He),r(e,fe,a),O(q,e,a),r(e,me,a),r(e,R,a),s(R,Je),r(e,ge,a),O(P,e,a),r(e,ce,a),r(e,W,a),s(W,M),r(e,ue,a),r(e,$,a),s($,Ue),s($,ee),s(ee,ze),s($,Fe),r(e,he,a),O(L,e,a),r(e,de,a),r(e,b,a),s(b,Ke),s(b,te),s(te,Qe),s(b,Xe),r(e,_e,a),r(e,T,a),s(T,Ye),r(e,ve,a),O(D,e,a),r(e,we,a),r(e,B,a),s(B,V),$e=!0},p:kt,i(e){$e||(G(y.$$.fragment,e),G(k.$$.fragment,e),G(E.$$.fragment,e),G(q.$$.fragment,e),G(P.$$.fragment,e),G(L.$$.fragment,e),G(D.$$.fragment,e),$e=!0)},o(e){N(y.$$.fragment,e),N(k.$$.fragment,e),N(E.$$.fragment,e),N(q.$$.fragment,e),N(P.$$.fragment,e),N(L.$$.fragment,e),N(D.$$.fragment,e),$e=!1},d(e){t(h),e&&t(se),e&&t(d),A(y),e&&t(ae),e&&t(v),e&&t(re),e&&t(c),e&&t(ne),e&&t(w),e&&t(oe),A(k,e),e&&t(ie),e&&t(S),e&&t(le),A(E,e),e&&t(pe),e&&t(u),e&&t(fe),A(q,e),e&&t(me),e&&t(R),e&&t(ge),A(P,e),e&&t(ce),e&&t(W),e&&t(ue),e&&t($),e&&t(he),A(L,e),e&&t(de),e&&t(b),e&&t(_e),e&&t(T),e&&t(ve),A(D,e),e&&t(we),e&&t(B)}}}const Lt={local:"reusing-seeds-for-fast-prompt-engineering",title:"Re-using seeds for fast prompt engineering"};function Dt(Ze){return Et(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Gt extends $t{constructor(h){super();bt(this,h,Dt,Pt,yt,{})}}export{Gt as default,Lt as metadata};
