import{S as Ro,i as Ko,s as Qo,e as a,k as f,w as A,t as i,M as Xo,c as l,d as t,m,a as n,x as j,h as r,b as h,N as Vo,G as o,g as p,y as I,L as Yo,q,o as T,B as O,v as Zo}from"../../chunks/vendor-hf-doc-builder.js";import{I as Jo}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as pe}from"../../chunks/CodeBlock-hf-doc-builder.js";function es(mo){let g,ke,_,v,ne,N,nt,fe,ft,xe,X,mt,Pe,c,ht,Y,ut,dt,W,ct,gt,Ce,b,w,me,G,_t,he,bt,De,u,vt,ue,wt,yt,M,$t,Et,L,kt,xt,de,Pt,Ct,U,ce,Dt,St,Z,zt,At,Se,y,jt,H,It,qt,ze,$,Tt,ge,Ot,Nt,Ae,B,je,ee,Wt,Ie,te,oe,ho,qe,se,Gt,Te,E,Mt,_e,Lt,Ut,Oe,F,Ne,k,Ht,be,Bt,Ft,We,V,Ge,x,Vt,ve,Jt,Rt,Me,J,Le,P,Kt,we,Qt,Xt,Ue,R,He,C,Yt,ye,Zt,eo,Be,K,Fe,ie,to,Ve,re,ae,uo,Je,D,oo,$e,so,io,Re,S,ro,Ee,ao,lo,Ke,z,po,Q,no,fo,Qe;return N=new Jo({}),G=new Jo({}),B=new pe({props:{code:`from diffusers import StableDiffusionPipeline, UniPCMultistepScheduler

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)

prompt = "a red cat playing with a ball"

generator = torch.Generator(device="cpu").manual_seed(33)

image = pipe(prompt, generator=generator, num_inference_steps=20).images[0]
image`,highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline, UniPCMultistepScheduler

pipe = StableDiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)

prompt = <span class="hljs-string">&quot;a red cat playing with a ball&quot;</span>

generator = torch.Generator(device=<span class="hljs-string">&quot;cpu&quot;</span>).manual_seed(<span class="hljs-number">33</span>)

image = pipe(prompt, generator=generator, num_inference_steps=<span class="hljs-number">20</span>).images[<span class="hljs-number">0</span>]
image`}}),F=new pe({props:{code:"pip install compel",highlighted:'pip <span class="hljs-keyword">install</span> compel'}}),V=new pe({props:{code:`from compel import Compel

compel_proc = Compel(tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder)`,highlighted:`<span class="hljs-keyword">from</span> compel <span class="hljs-keyword">import</span> Compel

compel_proc = Compel(tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder)`}}),J=new pe({props:{code:'prompt = "a red cat playing with a ball++"',highlighted:'prompt = <span class="hljs-string">&quot;a red cat playing with a ball++&quot;</span>'}}),R=new pe({props:{code:"prompt_embeds = compel_proc(prompt)",highlighted:"prompt_embeds = compel_proc(prompt)"}}),K=new pe({props:{code:`generator = torch.Generator(device="cpu").manual_seed(33)

images = pipe(prompt_embeds=prompt_embeds, generator=generator, num_inference_steps=20).images[0]
image`,highlighted:`generator = torch.Generator(device=<span class="hljs-string">&quot;cpu&quot;</span>).manual_seed(<span class="hljs-number">33</span>)

images = pipe(prompt_embeds=prompt_embeds, generator=generator, num_inference_steps=<span class="hljs-number">20</span>).images[<span class="hljs-number">0</span>]
image`}}),{c(){g=a("meta"),ke=f(),_=a("h1"),v=a("a"),ne=a("span"),A(N.$$.fragment),nt=f(),fe=a("span"),ft=i("Weighting prompts"),xe=f(),X=a("p"),mt=i(`Text-guided diffusion models generate images based on a given text prompt. The text prompt
can include multiple concepts that the model should generate and it\u2019s often desirable to weight
certain parts of the prompt more or less.`),Pe=f(),c=a("p"),ht=i("Diffusion models work by conditioning the cross attention layers of the diffusion model with contextualized text embeddings (see the "),Y=a("a"),ut=i("Stable Diffusion Guide for more information"),dt=i(`).
Thus a simple way to emphasize (or de-emphasize) certain parts of the prompt is by increasing or reducing the scale of the text embedding vector that corresponds to the relevant part of the prompt.
This is called \u201Cprompt-weighting\u201D and has been a highly demanded feature by the community (see issue `),W=a("a"),ct=i("here"),gt=i(")."),Ce=f(),b=a("h2"),w=a("a"),me=a("span"),A(G.$$.fragment),_t=f(),he=a("span"),bt=i("How to do prompt-weighting in Diffusers"),De=f(),u=a("p"),vt=i("We believe the role of "),ue=a("code"),wt=i("diffusers"),yt=i(" is to be a toolbox that provides essential features that enable other projects, such as "),M=a("a"),$t=i("InvokeAI"),Et=i(" or "),L=a("a"),kt=i("diffuzers"),xt=i(", to build powerful UIs. In order to support arbitrary methods to manipulate prompts, "),de=a("code"),Pt=i("diffusers"),Ct=i(" exposes a "),U=a("a"),ce=a("code"),Dt=i("prompt_embeds"),St=i(" function argument to many pipelines such as "),Z=a("a"),zt=i("StableDiffusionPipeline"),At=i(", allowing to directly pass the \u201Cprompt-weighted\u201D/scaled text embeddings to the pipeline."),Se=f(),y=a("p"),jt=i("The "),H=a("a"),It=i("compel library"),qt=i(" provides an easy way to emphasize or de-emphasize portions of the prompt for you. We strongly recommend it instead of preparing the embeddings yourself."),ze=f(),$=a("p"),Tt=i("Let\u2019s look at a simple example. Imagine you want to generate an image of "),ge=a("code"),Ot=i('"a red cat playing with a ball"'),Nt=i(` as
follows:`),Ae=f(),A(B.$$.fragment),je=f(),ee=a("p"),Wt=i("This gives you:"),Ie=f(),te=a("p"),oe=a("img"),qe=f(),se=a("p"),Gt=i("As you can see, there is no \u201Cball\u201D in the image. Let\u2019s emphasize this part!"),Te=f(),E=a("p"),Mt=i("For this we should install the "),_e=a("code"),Lt=i("compel"),Ut=i(" library:"),Oe=f(),A(F.$$.fragment),Ne=f(),k=a("p"),Ht=i("and then create a "),be=a("code"),Bt=i("Compel"),Ft=i(" object:"),We=f(),A(V.$$.fragment),Ge=f(),x=a("p"),Vt=i("Now we emphasize the part \u201Cball\u201D with the "),ve=a("code"),Jt=i('"++"'),Rt=i(" syntax:"),Me=f(),A(J.$$.fragment),Le=f(),P=a("p"),Kt=i("and instead of passing this to the pipeline directly, we have to process it using "),we=a("code"),Qt=i("compel_proc"),Xt=i(":"),Ue=f(),A(R.$$.fragment),He=f(),C=a("p"),Yt=i("Now we can pass "),ye=a("code"),Zt=i("prompt_embeds"),eo=i(" directly to the pipeline:"),Be=f(),A(K.$$.fragment),Fe=f(),ie=a("p"),to=i("We now get the following image which has a \u201Cball\u201D!"),Ve=f(),re=a("p"),ae=a("img"),Je=f(),D=a("p"),oo=i("Similarly, we de-emphasize parts of the sentence by using the "),$e=a("code"),so=i("--"),io=i(` suffix for words, feel free to give it
a try!`),Re=f(),S=a("p"),ro=i("If your favorite pipeline does not have a "),Ee=a("code"),ao=i("prompt_embeds"),lo=i(` input, please make sure to open an issue, the
diffusers team tries to be as responsive as possible.`),Ke=f(),z=a("p"),po=i("Also, please check out the documentation of the "),Q=a("a"),no=i("compel"),fo=i(` library for
more information.`),this.h()},l(e){const s=Xo('[data-svelte="svelte-1phssyn"]',document.head);g=l(s,"META",{name:!0,content:!0}),s.forEach(t),ke=m(e),_=l(e,"H1",{class:!0});var Xe=n(_);v=l(Xe,"A",{id:!0,class:!0,href:!0});var co=n(v);ne=l(co,"SPAN",{});var go=n(ne);j(N.$$.fragment,go),go.forEach(t),co.forEach(t),nt=m(Xe),fe=l(Xe,"SPAN",{});var _o=n(fe);ft=r(_o,"Weighting prompts"),_o.forEach(t),Xe.forEach(t),xe=m(e),X=l(e,"P",{});var bo=n(X);mt=r(bo,`Text-guided diffusion models generate images based on a given text prompt. The text prompt
can include multiple concepts that the model should generate and it\u2019s often desirable to weight
certain parts of the prompt more or less.`),bo.forEach(t),Pe=m(e),c=l(e,"P",{});var le=n(c);ht=r(le,"Diffusion models work by conditioning the cross attention layers of the diffusion model with contextualized text embeddings (see the "),Y=l(le,"A",{href:!0});var vo=n(Y);ut=r(vo,"Stable Diffusion Guide for more information"),vo.forEach(t),dt=r(le,`).
Thus a simple way to emphasize (or de-emphasize) certain parts of the prompt is by increasing or reducing the scale of the text embedding vector that corresponds to the relevant part of the prompt.
This is called \u201Cprompt-weighting\u201D and has been a highly demanded feature by the community (see issue `),W=l(le,"A",{href:!0,rel:!0});var wo=n(W);ct=r(wo,"here"),wo.forEach(t),gt=r(le,")."),le.forEach(t),Ce=m(e),b=l(e,"H2",{class:!0});var Ye=n(b);w=l(Ye,"A",{id:!0,class:!0,href:!0});var yo=n(w);me=l(yo,"SPAN",{});var $o=n(me);j(G.$$.fragment,$o),$o.forEach(t),yo.forEach(t),_t=m(Ye),he=l(Ye,"SPAN",{});var Eo=n(he);bt=r(Eo,"How to do prompt-weighting in Diffusers"),Eo.forEach(t),Ye.forEach(t),De=m(e),u=l(e,"P",{});var d=n(u);vt=r(d,"We believe the role of "),ue=l(d,"CODE",{});var ko=n(ue);wt=r(ko,"diffusers"),ko.forEach(t),yt=r(d," is to be a toolbox that provides essential features that enable other projects, such as "),M=l(d,"A",{href:!0,rel:!0});var xo=n(M);$t=r(xo,"InvokeAI"),xo.forEach(t),Et=r(d," or "),L=l(d,"A",{href:!0,rel:!0});var Po=n(L);kt=r(Po,"diffuzers"),Po.forEach(t),xt=r(d,", to build powerful UIs. In order to support arbitrary methods to manipulate prompts, "),de=l(d,"CODE",{});var Co=n(de);Pt=r(Co,"diffusers"),Co.forEach(t),Ct=r(d," exposes a "),U=l(d,"A",{href:!0,rel:!0});var Do=n(U);ce=l(Do,"CODE",{});var So=n(ce);Dt=r(So,"prompt_embeds"),So.forEach(t),Do.forEach(t),St=r(d," function argument to many pipelines such as "),Z=l(d,"A",{href:!0});var zo=n(Z);zt=r(zo,"StableDiffusionPipeline"),zo.forEach(t),At=r(d,", allowing to directly pass the \u201Cprompt-weighted\u201D/scaled text embeddings to the pipeline."),d.forEach(t),Se=m(e),y=l(e,"P",{});var Ze=n(y);jt=r(Ze,"The "),H=l(Ze,"A",{href:!0,rel:!0});var Ao=n(H);It=r(Ao,"compel library"),Ao.forEach(t),qt=r(Ze," provides an easy way to emphasize or de-emphasize portions of the prompt for you. We strongly recommend it instead of preparing the embeddings yourself."),Ze.forEach(t),ze=m(e),$=l(e,"P",{});var et=n($);Tt=r(et,"Let\u2019s look at a simple example. Imagine you want to generate an image of "),ge=l(et,"CODE",{});var jo=n(ge);Ot=r(jo,'"a red cat playing with a ball"'),jo.forEach(t),Nt=r(et,` as
follows:`),et.forEach(t),Ae=m(e),j(B.$$.fragment,e),je=m(e),ee=l(e,"P",{});var Io=n(ee);Wt=r(Io,"This gives you:"),Io.forEach(t),Ie=m(e),te=l(e,"P",{});var qo=n(te);oe=l(qo,"IMG",{src:!0,alt:!0}),qo.forEach(t),qe=m(e),se=l(e,"P",{});var To=n(se);Gt=r(To,"As you can see, there is no \u201Cball\u201D in the image. Let\u2019s emphasize this part!"),To.forEach(t),Te=m(e),E=l(e,"P",{});var tt=n(E);Mt=r(tt,"For this we should install the "),_e=l(tt,"CODE",{});var Oo=n(_e);Lt=r(Oo,"compel"),Oo.forEach(t),Ut=r(tt," library:"),tt.forEach(t),Oe=m(e),j(F.$$.fragment,e),Ne=m(e),k=l(e,"P",{});var ot=n(k);Ht=r(ot,"and then create a "),be=l(ot,"CODE",{});var No=n(be);Bt=r(No,"Compel"),No.forEach(t),Ft=r(ot," object:"),ot.forEach(t),We=m(e),j(V.$$.fragment,e),Ge=m(e),x=l(e,"P",{});var st=n(x);Vt=r(st,"Now we emphasize the part \u201Cball\u201D with the "),ve=l(st,"CODE",{});var Wo=n(ve);Jt=r(Wo,'"++"'),Wo.forEach(t),Rt=r(st," syntax:"),st.forEach(t),Me=m(e),j(J.$$.fragment,e),Le=m(e),P=l(e,"P",{});var it=n(P);Kt=r(it,"and instead of passing this to the pipeline directly, we have to process it using "),we=l(it,"CODE",{});var Go=n(we);Qt=r(Go,"compel_proc"),Go.forEach(t),Xt=r(it,":"),it.forEach(t),Ue=m(e),j(R.$$.fragment,e),He=m(e),C=l(e,"P",{});var rt=n(C);Yt=r(rt,"Now we can pass "),ye=l(rt,"CODE",{});var Mo=n(ye);Zt=r(Mo,"prompt_embeds"),Mo.forEach(t),eo=r(rt," directly to the pipeline:"),rt.forEach(t),Be=m(e),j(K.$$.fragment,e),Fe=m(e),ie=l(e,"P",{});var Lo=n(ie);to=r(Lo,"We now get the following image which has a \u201Cball\u201D!"),Lo.forEach(t),Ve=m(e),re=l(e,"P",{});var Uo=n(re);ae=l(Uo,"IMG",{src:!0,alt:!0}),Uo.forEach(t),Je=m(e),D=l(e,"P",{});var at=n(D);oo=r(at,"Similarly, we de-emphasize parts of the sentence by using the "),$e=l(at,"CODE",{});var Ho=n($e);so=r(Ho,"--"),Ho.forEach(t),io=r(at,` suffix for words, feel free to give it
a try!`),at.forEach(t),Re=m(e),S=l(e,"P",{});var lt=n(S);ro=r(lt,"If your favorite pipeline does not have a "),Ee=l(lt,"CODE",{});var Bo=n(Ee);ao=r(Bo,"prompt_embeds"),Bo.forEach(t),lo=r(lt,` input, please make sure to open an issue, the
diffusers team tries to be as responsive as possible.`),lt.forEach(t),Ke=m(e),z=l(e,"P",{});var pt=n(z);po=r(pt,"Also, please check out the documentation of the "),Q=l(pt,"A",{href:!0,rel:!0});var Fo=n(Q);no=r(Fo,"compel"),Fo.forEach(t),fo=r(pt,` library for
more information.`),pt.forEach(t),this.h()},h(){h(g,"name","hf:doc:metadata"),h(g,"content",JSON.stringify(ts)),h(v,"id","weighting-prompts"),h(v,"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"),h(v,"href","#weighting-prompts"),h(_,"class","relative group"),h(Y,"href","../stable-diffusion"),h(W,"href","https://github.com/huggingface/diffusers/issues/2431"),h(W,"rel","nofollow"),h(w,"id","how-to-do-promptweighting-in-diffusers"),h(w,"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"),h(w,"href","#how-to-do-promptweighting-in-diffusers"),h(b,"class","relative group"),h(M,"href","https://github.com/invoke-ai/InvokeAI"),h(M,"rel","nofollow"),h(L,"href","https://github.com/abhishekkrthakur/diffuzers"),h(L,"rel","nofollow"),h(U,"href","https://huggingface.co/docs/diffusers/v0.14.0/en/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline.__call__.prompt_embeds"),h(U,"rel","nofollow"),h(Z,"href","/docs/diffusers/v0.16.0/en/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline"),h(H,"href","https://github.com/damian0815/compel"),h(H,"rel","nofollow"),Vo(oe.src,ho="https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/compel/forest_0.png")||h(oe,"src",ho),h(oe,"alt","img"),Vo(ae.src,uo="https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/compel/forest_1.png")||h(ae,"src",uo),h(ae,"alt","img"),h(Q,"href","https://github.com/damian0815/compel"),h(Q,"rel","nofollow")},m(e,s){o(document.head,g),p(e,ke,s),p(e,_,s),o(_,v),o(v,ne),I(N,ne,null),o(_,nt),o(_,fe),o(fe,ft),p(e,xe,s),p(e,X,s),o(X,mt),p(e,Pe,s),p(e,c,s),o(c,ht),o(c,Y),o(Y,ut),o(c,dt),o(c,W),o(W,ct),o(c,gt),p(e,Ce,s),p(e,b,s),o(b,w),o(w,me),I(G,me,null),o(b,_t),o(b,he),o(he,bt),p(e,De,s),p(e,u,s),o(u,vt),o(u,ue),o(ue,wt),o(u,yt),o(u,M),o(M,$t),o(u,Et),o(u,L),o(L,kt),o(u,xt),o(u,de),o(de,Pt),o(u,Ct),o(u,U),o(U,ce),o(ce,Dt),o(u,St),o(u,Z),o(Z,zt),o(u,At),p(e,Se,s),p(e,y,s),o(y,jt),o(y,H),o(H,It),o(y,qt),p(e,ze,s),p(e,$,s),o($,Tt),o($,ge),o(ge,Ot),o($,Nt),p(e,Ae,s),I(B,e,s),p(e,je,s),p(e,ee,s),o(ee,Wt),p(e,Ie,s),p(e,te,s),o(te,oe),p(e,qe,s),p(e,se,s),o(se,Gt),p(e,Te,s),p(e,E,s),o(E,Mt),o(E,_e),o(_e,Lt),o(E,Ut),p(e,Oe,s),I(F,e,s),p(e,Ne,s),p(e,k,s),o(k,Ht),o(k,be),o(be,Bt),o(k,Ft),p(e,We,s),I(V,e,s),p(e,Ge,s),p(e,x,s),o(x,Vt),o(x,ve),o(ve,Jt),o(x,Rt),p(e,Me,s),I(J,e,s),p(e,Le,s),p(e,P,s),o(P,Kt),o(P,we),o(we,Qt),o(P,Xt),p(e,Ue,s),I(R,e,s),p(e,He,s),p(e,C,s),o(C,Yt),o(C,ye),o(ye,Zt),o(C,eo),p(e,Be,s),I(K,e,s),p(e,Fe,s),p(e,ie,s),o(ie,to),p(e,Ve,s),p(e,re,s),o(re,ae),p(e,Je,s),p(e,D,s),o(D,oo),o(D,$e),o($e,so),o(D,io),p(e,Re,s),p(e,S,s),o(S,ro),o(S,Ee),o(Ee,ao),o(S,lo),p(e,Ke,s),p(e,z,s),o(z,po),o(z,Q),o(Q,no),o(z,fo),Qe=!0},p:Yo,i(e){Qe||(q(N.$$.fragment,e),q(G.$$.fragment,e),q(B.$$.fragment,e),q(F.$$.fragment,e),q(V.$$.fragment,e),q(J.$$.fragment,e),q(R.$$.fragment,e),q(K.$$.fragment,e),Qe=!0)},o(e){T(N.$$.fragment,e),T(G.$$.fragment,e),T(B.$$.fragment,e),T(F.$$.fragment,e),T(V.$$.fragment,e),T(J.$$.fragment,e),T(R.$$.fragment,e),T(K.$$.fragment,e),Qe=!1},d(e){t(g),e&&t(ke),e&&t(_),O(N),e&&t(xe),e&&t(X),e&&t(Pe),e&&t(c),e&&t(Ce),e&&t(b),O(G),e&&t(De),e&&t(u),e&&t(Se),e&&t(y),e&&t(ze),e&&t($),e&&t(Ae),O(B,e),e&&t(je),e&&t(ee),e&&t(Ie),e&&t(te),e&&t(qe),e&&t(se),e&&t(Te),e&&t(E),e&&t(Oe),O(F,e),e&&t(Ne),e&&t(k),e&&t(We),O(V,e),e&&t(Ge),e&&t(x),e&&t(Me),O(J,e),e&&t(Le),e&&t(P),e&&t(Ue),O(R,e),e&&t(He),e&&t(C),e&&t(Be),O(K,e),e&&t(Fe),e&&t(ie),e&&t(Ve),e&&t(re),e&&t(Je),e&&t(D),e&&t(Re),e&&t(S),e&&t(Ke),e&&t(z)}}}const ts={local:"weighting-prompts",sections:[{local:"how-to-do-promptweighting-in-diffusers",title:"How to do prompt-weighting in Diffusers"}],title:"Weighting prompts"};function os(mo){return Zo(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class as extends Ro{constructor(g){super();Ko(this,g,os,es,Qo,{})}}export{as as default,ts as metadata};
