import{S as Sp,i as Ap,s as Jp,e as s,k as d,w as m,t as l,M as Pp,c as r,d as o,m as h,a as n,x as v,h as a,b as f,G as t,g as p,y,q as w,o as b,B as M,v as jp}from"../../chunks/vendor-hf-doc-builder.js";import{T as Np}from"../../chunks/Tip-hf-doc-builder.js";import{I}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as Je}from"../../chunks/CodeBlock-hf-doc-builder.js";function Gp(zo){let c,ne,C,J,O,T,Pe,V,D,ie,G;return{c(){c=s("p"),ne=l("You can also run the "),C=s("code"),J=l("diffusers"),O=l(" Python codebase on Apple Silicon Macs using the "),T=s("code"),Pe=l("mps"),V=l(" accelerator built into PyTorch. This approach is explained in depth in "),D=s("a"),ie=l("the mps guide"),G=l(", but it is not compatible with native apps."),this.h()},l(S){c=r(S,"P",{});var g=n(c);ne=a(g,"You can also run the "),C=r(g,"CODE",{});var Et=n(C);J=a(Et,"diffusers"),Et.forEach(o),O=a(g," Python codebase on Apple Silicon Macs using the "),T=r(g,"CODE",{});var je=n(T);Pe=a(je,"mps"),je.forEach(o),V=a(g," accelerator built into PyTorch. This approach is explained in depth in "),D=r(g,"A",{href:!0});var q=n(D);ie=a(q,"the mps guide"),q.forEach(o),G=a(g,", but it is not compatible with native apps."),g.forEach(o),this.h()},h(){f(D,"href","mps")},m(S,g){p(S,c,g),t(c,ne),t(c,C),t(C,J),t(c,O),t(c,T),t(T,Pe),t(c,V),t(c,D),t(D,ie),t(c,G)},d(S){S&&o(c)}}}function Lp(zo){let c,ne,C,J,O,T,Pe,V,D,ie,G,S,g,Et,je,q,ma,Ko,pe,el,Q,fe,Zt,Ne,va,Xt,ya,tl,Ct,wa,ol,B,ba,Ge,Ma,_a,Wt,Ea,Ca,ll,Ut,Ua,al,Z,Le,ga,Ie,ka,$a,Ta,gt,De,Sa,Aa,Ja,Be,Pa,Ze,ja,Na,sl,de,Ga,Xe,La,Ia,rl,H,he,Yt,We,Da,Ft,Ba,nl,kt,Za,il,ce,Ye,Rt,Xa,Wa,Fe,ue,xt,Ya,Fa,Re,Ra,xa,Oa,U,Va,Ot,qa,Qa,Vt,Ha,za,qt,Ka,es,xe,ts,os,Oe,ls,as,ss,Ve,Qt,rs,ns,qe,$t,Ht,is,ps,fs,P,zt,ds,hs,Kt,cs,us,me,eo,ms,vs,ys,to,ws,bs,pl,ve,Ms,Qe,_s,Es,fl,He,dl,Tt,Cs,hl,z,ye,oo,ze,Us,lo,gs,cl,St,ks,ul,Ke,ml,K,we,ao,et,$s,so,Ts,vl,k,Ss,ro,As,Js,no,Ps,js,io,Ns,Gs,po,Ls,Is,yl,X,Ds,fo,Bs,Zs,ho,Xs,Ws,wl,tt,bl,ee,be,co,ot,Ys,uo,Fs,Ml,At,Rs,_l,lt,El,_,mo,xs,Os,vo,Vs,qs,yo,Qs,Hs,wo,zs,Ks,bo,er,tr,Mo,or,lr,Cl,j,ar,_o,sr,rr,Eo,nr,ir,Co,pr,fr,Ul,Me,dr,at,Uo,hr,cr,gl,st,kl,te,_e,go,rt,ur,ko,mr,$l,Ee,vr,$o,yr,wr,Tl,oe,Ce,To,nt,br,So,Mr,Sl,W,_r,Ao,Er,Cr,Jo,Ur,gr,Al,it,Jl,le,Ue,Po,pt,kr,jo,$r,Pl,Jt,Tr,jl,ft,Nl,ge,Sr,dt,Ar,Jr,Gl,ht,Ll,u,Pr,No,jr,Nr,Go,Gr,Lr,Lo,Ir,Dr,Io,Br,Zr,Do,Xr,Wr,Bo,Yr,Fr,Zo,Rr,xr,Il,ke,Or,ct,Vr,qr,Dl,ae,$e,Xo,ut,Qr,Wo,Hr,Bl,Pt,zr,Zl,Y,Yo,Kr,en,L,tn,Fo,on,ln,Ro,an,sn,xo,rn,nn,pn,Oo,fn,Xl,F,dn,mt,hn,cn,vt,un,mn,Wl,jt,vn,Yl,se,Te,Vo,yt,yn,qo,wn,Fl,$,bn,wt,Mn,_n,Qo,En,Cn,bt,Un,gn,Mt,kn,$n,Rl;return T=new I({}),pe=new Np({props:{$$slots:{default:[Gp]},$$scope:{ctx:zo}}}),Ne=new I({}),We=new I({}),He=new Je({props:{code:"Y29yZW1sLXN0YWJsZS1kaWZmdXNpb24tdjEtNCUwQSVFMiU5NCU5QyVFMiU5NCU4MCVFMiU5NCU4MCUyMFJFQURNRS5tZCUwQSVFMiU5NCU5QyVFMiU5NCU4MCVFMiU5NCU4MCUyMG9yaWdpbmFsJTBBJUUyJTk0JTgyJTIwJTIwJTIwJUUyJTk0JTlDJUUyJTk0JTgwJUUyJTk0JTgwJTIwY29tcGlsZWQlMEElRTIlOTQlODIlMjAlMjAlMjAlRTIlOTQlOTQlRTIlOTQlODAlRTIlOTQlODAlMjBwYWNrYWdlcyUwQSVFMiU5NCU5NCVFMiU5NCU4MCVFMiU5NCU4MCUyMHNwbGl0X2VpbnN1bSUwQSUyMCUyMCUyMCUyMCVFMiU5NCU5QyVFMiU5NCU4MCVFMiU5NCU4MCUyMGNvbXBpbGVkJTBBJTIwJTIwJTIwJTIwJUUyJTk0JTk0JUUyJTk0JTgwJUUyJTk0JTgwJTIwcGFja2FnZXM=",highlighted:`coreml-stable-<span class="hljs-keyword">diffusion-v1-4
</span>\u251C\u2500\u2500 README.md
\u251C\u2500\u2500 <span class="hljs-keyword">original
</span>\u2502   \u251C\u2500\u2500 compiled
\u2502   \u2514\u2500\u2500 packages
\u2514\u2500\u2500 split_einsum
    \u251C\u2500\u2500 compiled
    \u2514\u2500\u2500 packages`}}),ze=new I({}),Ke=new Je({props:{code:"cGlwJTIwaW5zdGFsbCUyMGh1Z2dpbmdmYWNlX2h1YiUwQXBpcCUyMGluc3RhbGwlMjBnaXQlMkJodHRwcyUzQSUyRiUyRmdpdGh1Yi5jb20lMkZhcHBsZSUyRm1sLXN0YWJsZS1kaWZmdXNpb24=",highlighted:`pip install huggingface_hub
pip install git+https://github.com/apple/ml-stable-diffusion`}}),et=new I({}),tt=new Je({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMHNuYXBzaG90X2Rvd25sb2FkJTBBZnJvbSUyMHBhdGhsaWIlMjBpbXBvcnQlMjBQYXRoJTBBJTBBcmVwb19pZCUyMCUzRCUyMCUyMmFwcGxlJTJGY29yZW1sLXN0YWJsZS1kaWZmdXNpb24tdjEtNCUyMiUwQXZhcmlhbnQlMjAlM0QlMjAlMjJvcmlnaW5hbCUyRnBhY2thZ2VzJTIyJTBBJTBBbW9kZWxfcGF0aCUyMCUzRCUyMFBhdGgoJTIyLiUyRm1vZGVscyUyMiklMjAlMkYlMjAocmVwb19pZC5zcGxpdCglMjIlMkYlMjIpJTVCLTElNUQlMjAlMkIlMjAlMjJfJTIyJTIwJTJCJTIwdmFyaWFudC5yZXBsYWNlKCUyMiUyRiUyMiUyQyUyMCUyMl8lMjIpKSUwQXNuYXBzaG90X2Rvd25sb2FkKHJlcG9faWQlMkMlMjBhbGxvd19wYXR0ZXJucyUzRGYlMjIlN0J2YXJpYW50JTdEJTJGKiUyMiUyQyUyMGxvY2FsX2RpciUzRG1vZGVsX3BhdGglMkMlMjBsb2NhbF9kaXJfdXNlX3N5bWxpbmtzJTNERmFsc2UpJTBBcHJpbnQoZiUyMk1vZGVsJTIwZG93bmxvYWRlZCUyMGF0JTIwJTdCbW9kZWxfcGF0aCU3RCUyMik=",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> snapshot_download
<span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path

repo_id = <span class="hljs-string">&quot;apple/coreml-stable-diffusion-v1-4&quot;</span>
variant = <span class="hljs-string">&quot;original/packages&quot;</span>

model_path = Path(<span class="hljs-string">&quot;./models&quot;</span>) / (repo_id.split(<span class="hljs-string">&quot;/&quot;</span>)[-<span class="hljs-number">1</span>] + <span class="hljs-string">&quot;_&quot;</span> + variant.replace(<span class="hljs-string">&quot;/&quot;</span>, <span class="hljs-string">&quot;_&quot;</span>))
snapshot_download(repo_id, allow_patterns=<span class="hljs-string">f&quot;<span class="hljs-subst">{variant}</span>/*&quot;</span>, local_dir=model_path, local_dir_use_symlinks=<span class="hljs-literal">False</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Model downloaded at <span class="hljs-subst">{model_path}</span>&quot;</span>)`}}),ot=new I({}),lt=new Je({props:{code:"cHl0aG9uJTIwLW0lMjBweXRob25fY29yZW1sX3N0YWJsZV9kaWZmdXNpb24ucGlwZWxpbmUlMjAtLXByb21wdCUyMCUyMmElMjBwaG90byUyMG9mJTIwYW4lMjBhc3Ryb25hdXQlMjByaWRpbmclMjBhJTIwaG9yc2UlMjBvbiUyMG1hcnMlMjIlMjAtaSUyMG1vZGVscyUyRmNvcmVtbC1zdGFibGUtZGlmZnVzaW9uLXYxLTRfb3JpZ2luYWxfcGFja2FnZXMlMjAtbyUyMCUzQyUyRnBhdGglMkZ0byUyRm91dHB1dCUyRmltYWdlJTNFJTIwLS1jb21wdXRlLXVuaXQlMjBDUFVfQU5EX0dQVSUyMC0tc2VlZCUyMDkz",highlighted:"python -m python_coreml_stable_diffusion.pipeline --prompt &quot;a photo of an astronaut riding a horse on mars&quot; -i models/coreml-stable-diffusion-v1-4_original_packages -o &lt;/path/to/output/image&gt; --compute-unit CPU_AND_GPU --seed 93"}}),st=new Je({props:{code:"cHl0aG9uJTIwLW0lMjBweXRob25fY29yZW1sX3N0YWJsZV9kaWZmdXNpb24ucGlwZWxpbmUlMjAtLXByb21wdCUyMCUyMmElMjBwaG90byUyMG9mJTIwYW4lMjBhc3Ryb25hdXQlMjByaWRpbmclMjBhJTIwaG9yc2UlMjBvbiUyMG1hcnMlMjIlMjAtLWNvbXB1dGUtdW5pdCUyMEFMTCUyMC1vJTIwb3V0cHV0JTIwLS1zZWVkJTIwOTMlMjAtaSUyMG1vZGVscyUyRmNvcmVtbC1zdGFibGUtZGlmZnVzaW9uLXYxLTVfb3JpZ2luYWxfcGFja2FnZXMlMjAtLW1vZGVsLXZlcnNpb24lMjBydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNQ==",highlighted:"python -m python_coreml_stable_diffusion.pipeline --prompt &quot;a photo of an astronaut riding a horse on mars&quot; --compute-unit ALL -o output --seed 93 -i models/coreml-stable-diffusion-v1-5_original_packages --model-version runwayml/stable-diffusion-v1-5"}}),rt=new I({}),nt=new I({}),it=new Je({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMHNuYXBzaG90X2Rvd25sb2FkJTBBZnJvbSUyMHBhdGhsaWIlMjBpbXBvcnQlMjBQYXRoJTBBJTBBcmVwb19pZCUyMCUzRCUyMCUyMmFwcGxlJTJGY29yZW1sLXN0YWJsZS1kaWZmdXNpb24tdjEtNCUyMiUwQXZhcmlhbnQlMjAlM0QlMjAlMjJvcmlnaW5hbCUyRmNvbXBpbGVkJTIyJTBBJTBBbW9kZWxfcGF0aCUyMCUzRCUyMFBhdGgoJTIyLiUyRm1vZGVscyUyMiklMjAlMkYlMjAocmVwb19pZC5zcGxpdCglMjIlMkYlMjIpJTVCLTElNUQlMjAlMkIlMjAlMjJfJTIyJTIwJTJCJTIwdmFyaWFudC5yZXBsYWNlKCUyMiUyRiUyMiUyQyUyMCUyMl8lMjIpKSUwQXNuYXBzaG90X2Rvd25sb2FkKHJlcG9faWQlMkMlMjBhbGxvd19wYXR0ZXJucyUzRGYlMjIlN0J2YXJpYW50JTdEJTJGKiUyMiUyQyUyMGxvY2FsX2RpciUzRG1vZGVsX3BhdGglMkMlMjBsb2NhbF9kaXJfdXNlX3N5bWxpbmtzJTNERmFsc2UpJTBBcHJpbnQoZiUyMk1vZGVsJTIwZG93bmxvYWRlZCUyMGF0JTIwJTdCbW9kZWxfcGF0aCU3RCUyMik=",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> snapshot_download
<span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path

repo_id = <span class="hljs-string">&quot;apple/coreml-stable-diffusion-v1-4&quot;</span>
variant = <span class="hljs-string">&quot;original/compiled&quot;</span>

model_path = Path(<span class="hljs-string">&quot;./models&quot;</span>) / (repo_id.split(<span class="hljs-string">&quot;/&quot;</span>)[-<span class="hljs-number">1</span>] + <span class="hljs-string">&quot;_&quot;</span> + variant.replace(<span class="hljs-string">&quot;/&quot;</span>, <span class="hljs-string">&quot;_&quot;</span>))
snapshot_download(repo_id, allow_patterns=<span class="hljs-string">f&quot;<span class="hljs-subst">{variant}</span>/*&quot;</span>, local_dir=model_path, local_dir_use_symlinks=<span class="hljs-literal">False</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Model downloaded at <span class="hljs-subst">{model_path}</span>&quot;</span>)`}}),pt=new I({}),ft=new Je({props:{code:"Z2l0JTIwY2xvbmUlMjBodHRwcyUzQSUyRiUyRmdpdGh1Yi5jb20lMkZhcHBsZSUyRm1sLXN0YWJsZS1kaWZmdXNpb24lMEFjZCUyMG1sLXN0YWJsZS1kaWZmdXNpb24=",highlighted:`git <span class="hljs-built_in">clone</span> https://github.com/apple/ml-stable-diffusion
<span class="hljs-built_in">cd</span> ml-stable-diffusion`}}),ht=new Je({props:{code:"c3dpZnQlMjBydW4lMjBTdGFibGVEaWZmdXNpb25TYW1wbGUlMjAtLXJlc291cmNlLXBhdGglMjBtb2RlbHMlMkZjb3JlbWwtc3RhYmxlLWRpZmZ1c2lvbi12MS00X29yaWdpbmFsX2NvbXBpbGVkJTIwLS1jb21wdXRlLXVuaXRzJTIwYWxsJTIwJTIyYSUyMHBob3RvJTIwb2YlMjBhbiUyMGFzdHJvbmF1dCUyMHJpZGluZyUyMGElMjBob3JzZSUyMG9uJTIwbWFycyUyMg==",highlighted:'swift run StableDiffusionSample --resource-path models/coreml-stable-diffusion-v1-4_original_compiled --compute-units all <span class="hljs-string">&quot;a photo of an astronaut riding a horse on mars&quot;</span>'}}),ut=new I({}),yt=new I({}),{c(){c=s("meta"),ne=d(),C=s("h1"),J=s("a"),O=s("span"),m(T.$$.fragment),Pe=d(),V=s("span"),D=l("How to run Stable Diffusion with Core ML"),ie=d(),G=s("p"),S=s("a"),g=l("Core ML"),Et=l(" is the model format and machine learning library supported by Apple frameworks. If you are interested in running Stable Diffusion models inside your macOS or iOS/iPadOS apps, this guide will show you how to convert existing PyTorch checkpoints into the Core ML format and use them for inference with Python or Swift."),je=d(),q=s("p"),ma=l("Core ML models can leverage all the compute engines available in Apple devices: the CPU, the GPU, and the Apple Neural Engine (or ANE, a tensor-optimized accelerator available in Apple Silicon Macs and modern iPhones/iPads). Depending on the model and the device it\u2019s running on, Core ML can mix and match compute engines too, so some portions of the model may run on the CPU while others run on GPU, for example."),Ko=d(),m(pe.$$.fragment),el=d(),Q=s("h2"),fe=s("a"),Zt=s("span"),m(Ne.$$.fragment),va=d(),Xt=s("span"),ya=l("Stable Diffusion Core ML Checkpoints"),tl=d(),Ct=s("p"),wa=l("Stable Diffusion weights (or checkpoints) are stored in the PyTorch format, so you need to convert them to the Core ML format before we can use them inside native apps."),ol=d(),B=s("p"),ba=l("Thankfully, Apple engineers developed "),Ge=s("a"),Ma=l("a conversion tool"),_a=l(" based on "),Wt=s("code"),Ea=l("diffusers"),Ca=l(" to convert the PyTorch checkpoints to Core ML."),ll=d(),Ut=s("p"),Ua=l("Before you convert a model, though, take a moment to explore the Hugging Face Hub \u2013 chances are the model you\u2019re interested in is already available in Core ML format:"),al=d(),Z=s("ul"),Le=s("li"),ga=l("the "),Ie=s("a"),ka=l("Apple"),$a=l(" organization includes Stable Diffusion versions 1.4, 1.5, 2.0 base, and 2.1 base"),Ta=d(),gt=s("li"),De=s("a"),Sa=l("coreml"),Aa=l(" organization includes custom DreamBoothed and finetuned models"),Ja=d(),Be=s("li"),Pa=l("use this "),Ze=s("a"),ja=l("filter"),Na=l(" to return all available Core ML checkpoints"),sl=d(),de=s("p"),Ga=l("If you can\u2019t find the model you\u2019re interested in, we recommend you follow the instructions for "),Xe=s("a"),La=l("Converting Models to Core ML"),Ia=l(" by Apple."),rl=d(),H=s("h2"),he=s("a"),Yt=s("span"),m(We.$$.fragment),Da=d(),Ft=s("span"),Ba=l("Selecting the Core ML Variant to Use"),nl=d(),kt=s("p"),Za=l("Stable Diffusion models can be converted to different Core ML variants intended for different purposes:"),il=d(),ce=s("ul"),Ye=s("li"),Rt=s("p"),Xa=l("The type of attention blocks used. The attention operation is used to \u201Cpay attention\u201D to the relationship between different areas in the image representations and to understand how the image and text representations are related. Attention is compute- and memory-intensive, so different implementations exist that consider the hardware characteristics of different devices. For Core ML Stable Diffusion models, there are two attention variants:"),Wa=d(),Fe=s("ul"),ue=s("li"),xt=s("code"),Ya=l("split_einsum"),Fa=l(" ("),Re=s("a"),Ra=l("introduced by Apple"),xa=l(") is optimized for ANE devices, which is available in modern iPhones, iPads and M-series computers."),Oa=d(),U=s("li"),Va=l("The \u201Coriginal\u201D attention (the base implementation used in "),Ot=s("code"),qa=l("diffusers"),Qa=l(") is only compatible with CPU/GPU and not ANE. It can be "),Vt=s("em"),Ha=l("faster"),za=l(" to run your model on CPU + GPU using "),qt=s("code"),Ka=l("original"),es=l(" attention than ANE. See "),xe=s("a"),ts=l("this performance benchmark"),os=l(" as well as some "),Oe=s("a"),ls=l("additional measures provided by the community"),as=l(" for additional details."),ss=d(),Ve=s("li"),Qt=s("p"),rs=l("The supported inference framework."),ns=d(),qe=s("ul"),$t=s("li"),Ht=s("code"),is=l("packages"),ps=l(" are suitable for Python inference. This can be used to test converted Core ML models before attempting to integrate them inside native apps, or if you want to explore Core ML performance but don\u2019t need to support native apps. For example, an application with a web UI could perfectly use a Python Core ML backend."),fs=d(),P=s("li"),zt=s("code"),ds=l("compiled"),hs=l(" models are required for Swift code. The "),Kt=s("code"),cs=l("compiled"),us=l(" models in the Hub split the large UNet model weights into several files for compatibility with iOS and iPadOS devices. This corresponds to the "),me=s("a"),eo=s("code"),ms=l("--chunk-unet"),vs=l(" conversion option"),ys=l(". If you want to support native apps, then you need to select the "),to=s("code"),ws=l("compiled"),bs=l(" variant."),pl=d(),ve=s("p"),Ms=l("The official Core ML Stable Diffusion "),Qe=s("a"),_s=l("models"),Es=l(" include these variants, but the community ones may vary:"),fl=d(),m(He.$$.fragment),dl=d(),Tt=s("p"),Cs=l("You can download and use the variant you need as shown below."),hl=d(),z=s("h2"),ye=s("a"),oo=s("span"),m(ze.$$.fragment),Us=d(),lo=s("span"),gs=l("Core ML Inference in Python"),cl=d(),St=s("p"),ks=l("Install the following libraries to run Core ML inference in Python:"),ul=d(),m(Ke.$$.fragment),ml=d(),K=s("h3"),we=s("a"),ao=s("span"),m(et.$$.fragment),$s=d(),so=s("span"),Ts=l("Download the Model Checkpoints"),vl=d(),k=s("p"),Ss=l("To run inference in Python, use one of the versions stored in the "),ro=s("code"),As=l("packages"),Js=l(" folders because the "),no=s("code"),Ps=l("compiled"),js=l(" ones are only compatible with Swift. You may choose whether you want to use "),io=s("code"),Ns=l("original"),Gs=l(" or "),po=s("code"),Ls=l("split_einsum"),Is=l(" attention."),yl=d(),X=s("p"),Ds=l("This is how you\u2019d download the "),fo=s("code"),Bs=l("original"),Zs=l(" attention variant from the Hub to a directory called "),ho=s("code"),Xs=l("models"),Ws=l(":"),wl=d(),m(tt.$$.fragment),bl=d(),ee=s("h3"),be=s("a"),co=s("span"),m(ot.$$.fragment),Ys=d(),uo=s("span"),Fs=l("Inference"),Ml=d(),At=s("p"),Rs=l("Once you have downloaded a snapshot of the model, you can test it using Apple\u2019s Python script."),_l=d(),m(lt.$$.fragment),El=d(),_=s("p"),mo=s("code"),xs=l("<output-mlpackages-directory>"),Os=l(" should point to the checkpoint you downloaded in the step above, and "),vo=s("code"),Vs=l("--compute-unit"),qs=l(" indicates the hardware you want to allow for inference. It must be one of the following options: "),yo=s("code"),Qs=l("ALL"),Hs=l(", "),wo=s("code"),zs=l("CPU_AND_GPU"),Ks=l(", "),bo=s("code"),er=l("CPU_ONLY"),tr=l(", "),Mo=s("code"),or=l("CPU_AND_NE"),lr=l(". You may also provide an optional output path, and a seed for reproducibility."),Cl=d(),j=s("p"),ar=l("The inference script assumes you\u2019re using the original version of the Stable Diffusion model, "),_o=s("code"),sr=l("CompVis/stable-diffusion-v1-4"),rr=l(". If you use another model, you "),Eo=s("em"),nr=l("have"),ir=l(" to specify its Hub id in the inference command line, using the "),Co=s("code"),pr=l("--model-version"),fr=l(" option. This works for models already supported and custom models you trained or fine-tuned yourself."),Ul=d(),Me=s("p"),dr=l("For example, if you want to use "),at=s("a"),Uo=s("code"),hr=l("runwayml/stable-diffusion-v1-5"),cr=l(":"),gl=d(),m(st.$$.fragment),kl=d(),te=s("h2"),_e=s("a"),go=s("span"),m(rt.$$.fragment),ur=d(),ko=s("span"),mr=l("Core ML inference in Swift"),$l=d(),Ee=s("p"),vr=l("Running inference in Swift is slightly faster than in Python because the models are already compiled in the "),$o=s("code"),yr=l("mlmodelc"),wr=l(" format. This is noticeable on app startup when the model is loaded but shouldn\u2019t be noticeable if you run several generations afterward."),Tl=d(),oe=s("h3"),Ce=s("a"),To=s("span"),m(nt.$$.fragment),br=d(),So=s("span"),Mr=l("Download"),Sl=d(),W=s("p"),_r=l("To run inference in Swift on your Mac, you need one of the "),Ao=s("code"),Er=l("compiled"),Cr=l(" checkpoint versions. We recommend you download them locally using Python code similar to the previous example, but with one of the "),Jo=s("code"),Ur=l("compiled"),gr=l(" variants:"),Al=d(),m(it.$$.fragment),Jl=d(),le=s("h3"),Ue=s("a"),Po=s("span"),m(pt.$$.fragment),kr=d(),jo=s("span"),$r=l("Inference"),Pl=d(),Jt=s("p"),Tr=l("To run inference, please clone Apple\u2019s repo:"),jl=d(),m(ft.$$.fragment),Nl=d(),ge=s("p"),Sr=l("And then use Apple\u2019s command line tool, "),dt=s("a"),Ar=l("Swift Package Manager"),Jr=l(":"),Gl=d(),m(ht.$$.fragment),Ll=d(),u=s("p"),Pr=l("You have to specify in "),No=s("code"),jr=l("--resource-path"),Nr=l(" one of the checkpoints downloaded in the previous step, so please make sure it contains compiled Core ML bundles with the extension "),Go=s("code"),Gr=l(".mlmodelc"),Lr=l(". The "),Lo=s("code"),Ir=l("--compute-units"),Dr=l(" has to be one of these values: "),Io=s("code"),Br=l("all"),Zr=l(", "),Do=s("code"),Xr=l("cpuOnly"),Wr=l(", "),Bo=s("code"),Yr=l("cpuAndGPU"),Fr=l(", "),Zo=s("code"),Rr=l("cpuAndNeuralEngine"),xr=l("."),Il=d(),ke=s("p"),Or=l("For more details, please refer to the "),ct=s("a"),Vr=l("instructions in Apple\u2019s repo"),qr=l("."),Dl=d(),ae=s("h2"),$e=s("a"),Xo=s("span"),m(ut.$$.fragment),Qr=d(),Wo=s("span"),Hr=l("Supported Diffusers Features"),Bl=d(),Pt=s("p"),zr=l("The Core ML models and inference code don\u2019t support many of the features, options, and flexibility of \u{1F9E8} Diffusers. These are some of the limitations to keep in mind:"),Zl=d(),Y=s("ul"),Yo=s("li"),Kr=l("Core ML models are only suitable for inference. They can\u2019t be used for training or fine-tuning."),en=d(),L=s("li"),tn=l("Only two schedulers have been ported to Swift, the default one used by Stable Diffusion and "),Fo=s("code"),on=l("DPMSolverMultistepScheduler"),ln=l(", which we ported to Swift from our "),Ro=s("code"),an=l("diffusers"),sn=l(" implementation. We recommend you use "),xo=s("code"),rn=l("DPMSolverMultistepScheduler"),nn=l(", since it produces the same quality in about half the steps."),pn=d(),Oo=s("li"),fn=l("Negative prompts, classifier-free guidance scale, and image-to-image tasks are available in the inference code. Advanced features such as depth guidance, ControlNet, and latent upscalers are not available yet."),Xl=d(),F=s("p"),dn=l("Apple\u2019s "),mt=s("a"),hn=l("conversion and inference repo"),cn=l(" and our own "),vt=s("a"),un=l("swift-coreml-diffusers"),mn=l(" repos are intended as technology demonstrators to enable other developers to build upon."),Wl=d(),jt=s("p"),vn=l("If you feel strongly about any missing features, please feel free to open a feature request or, better yet, a contribution PR :)"),Yl=d(),se=s("h2"),Te=s("a"),Vo=s("span"),m(yt.$$.fragment),yn=d(),qo=s("span"),wn=l("Native Diffusers Swift app"),Fl=d(),$=s("p"),bn=l("One easy way to run Stable Diffusion on your own Apple hardware is to use "),wt=s("a"),Mn=l("our open-source Swift repo"),_n=l(", based on "),Qo=s("code"),En=l("diffusers"),Cn=l(" and Apple\u2019s conversion and inference repo. You can study the code, compile it with "),bt=s("a"),Un=l("Xcode"),gn=l(" and adapt it for your own needs. For your convenience, there\u2019s also a "),Mt=s("a"),kn=l("standalone Mac app in the App Store"),$n=l(", so you can play with it without having to deal with the code or IDE. If you are a developer and have determined that Core ML is the best solution to build your Stable Diffusion app, then you can use the rest of this guide to get started with your project. We can\u2019t wait to see what you\u2019ll build :)"),this.h()},l(e){const i=Pp('[data-svelte="svelte-1phssyn"]',document.head);c=r(i,"META",{name:!0,content:!0}),i.forEach(o),ne=h(e),C=r(e,"H1",{class:!0});var _t=n(C);J=r(_t,"A",{id:!0,class:!0,href:!0});var Pn=n(J);O=r(Pn,"SPAN",{});var jn=n(O);v(T.$$.fragment,jn),jn.forEach(o),Pn.forEach(o),Pe=h(_t),V=r(_t,"SPAN",{});var Nn=n(V);D=a(Nn,"How to run Stable Diffusion with Core ML"),Nn.forEach(o),_t.forEach(o),ie=h(e),G=r(e,"P",{});var Tn=n(G);S=r(Tn,"A",{href:!0,rel:!0});var Gn=n(S);g=a(Gn,"Core ML"),Gn.forEach(o),Et=a(Tn," is the model format and machine learning library supported by Apple frameworks. If you are interested in running Stable Diffusion models inside your macOS or iOS/iPadOS apps, this guide will show you how to convert existing PyTorch checkpoints into the Core ML format and use them for inference with Python or Swift."),Tn.forEach(o),je=h(e),q=r(e,"P",{});var Ln=n(q);ma=a(Ln,"Core ML models can leverage all the compute engines available in Apple devices: the CPU, the GPU, and the Apple Neural Engine (or ANE, a tensor-optimized accelerator available in Apple Silicon Macs and modern iPhones/iPads). Depending on the model and the device it\u2019s running on, Core ML can mix and match compute engines too, so some portions of the model may run on the CPU while others run on GPU, for example."),Ln.forEach(o),Ko=h(e),v(pe.$$.fragment,e),el=h(e),Q=r(e,"H2",{class:!0});var xl=n(Q);fe=r(xl,"A",{id:!0,class:!0,href:!0});var In=n(fe);Zt=r(In,"SPAN",{});var Dn=n(Zt);v(Ne.$$.fragment,Dn),Dn.forEach(o),In.forEach(o),va=h(xl),Xt=r(xl,"SPAN",{});var Bn=n(Xt);ya=a(Bn,"Stable Diffusion Core ML Checkpoints"),Bn.forEach(o),xl.forEach(o),tl=h(e),Ct=r(e,"P",{});var Zn=n(Ct);wa=a(Zn,"Stable Diffusion weights (or checkpoints) are stored in the PyTorch format, so you need to convert them to the Core ML format before we can use them inside native apps."),Zn.forEach(o),ol=h(e),B=r(e,"P",{});var Nt=n(B);ba=a(Nt,"Thankfully, Apple engineers developed "),Ge=r(Nt,"A",{href:!0,rel:!0});var Xn=n(Ge);Ma=a(Xn,"a conversion tool"),Xn.forEach(o),_a=a(Nt," based on "),Wt=r(Nt,"CODE",{});var Wn=n(Wt);Ea=a(Wn,"diffusers"),Wn.forEach(o),Ca=a(Nt," to convert the PyTorch checkpoints to Core ML."),Nt.forEach(o),ll=h(e),Ut=r(e,"P",{});var Yn=n(Ut);Ua=a(Yn,"Before you convert a model, though, take a moment to explore the Hugging Face Hub \u2013 chances are the model you\u2019re interested in is already available in Core ML format:"),Yn.forEach(o),al=h(e),Z=r(e,"UL",{});var Gt=n(Z);Le=r(Gt,"LI",{});var Ol=n(Le);ga=a(Ol,"the "),Ie=r(Ol,"A",{href:!0,rel:!0});var Fn=n(Ie);ka=a(Fn,"Apple"),Fn.forEach(o),$a=a(Ol," organization includes Stable Diffusion versions 1.4, 1.5, 2.0 base, and 2.1 base"),Ol.forEach(o),Ta=h(Gt),gt=r(Gt,"LI",{});var Sn=n(gt);De=r(Sn,"A",{href:!0,rel:!0});var Rn=n(De);Sa=a(Rn,"coreml"),Rn.forEach(o),Aa=a(Sn," organization includes custom DreamBoothed and finetuned models"),Sn.forEach(o),Ja=h(Gt),Be=r(Gt,"LI",{});var Vl=n(Be);Pa=a(Vl,"use this "),Ze=r(Vl,"A",{href:!0,rel:!0});var xn=n(Ze);ja=a(xn,"filter"),xn.forEach(o),Na=a(Vl," to return all available Core ML checkpoints"),Vl.forEach(o),Gt.forEach(o),sl=h(e),de=r(e,"P",{});var ql=n(de);Ga=a(ql,"If you can\u2019t find the model you\u2019re interested in, we recommend you follow the instructions for "),Xe=r(ql,"A",{href:!0,rel:!0});var On=n(Xe);La=a(On,"Converting Models to Core ML"),On.forEach(o),Ia=a(ql," by Apple."),ql.forEach(o),rl=h(e),H=r(e,"H2",{class:!0});var Ql=n(H);he=r(Ql,"A",{id:!0,class:!0,href:!0});var Vn=n(he);Yt=r(Vn,"SPAN",{});var qn=n(Yt);v(We.$$.fragment,qn),qn.forEach(o),Vn.forEach(o),Da=h(Ql),Ft=r(Ql,"SPAN",{});var Qn=n(Ft);Ba=a(Qn,"Selecting the Core ML Variant to Use"),Qn.forEach(o),Ql.forEach(o),nl=h(e),kt=r(e,"P",{});var Hn=n(kt);Za=a(Hn,"Stable Diffusion models can be converted to different Core ML variants intended for different purposes:"),Hn.forEach(o),il=h(e),ce=r(e,"UL",{});var Hl=n(ce);Ye=r(Hl,"LI",{});var zl=n(Ye);Rt=r(zl,"P",{});var zn=n(Rt);Xa=a(zn,"The type of attention blocks used. The attention operation is used to \u201Cpay attention\u201D to the relationship between different areas in the image representations and to understand how the image and text representations are related. Attention is compute- and memory-intensive, so different implementations exist that consider the hardware characteristics of different devices. For Core ML Stable Diffusion models, there are two attention variants:"),zn.forEach(o),Wa=h(zl),Fe=r(zl,"UL",{});var Kl=n(Fe);ue=r(Kl,"LI",{});var Ho=n(ue);xt=r(Ho,"CODE",{});var Kn=n(xt);Ya=a(Kn,"split_einsum"),Kn.forEach(o),Fa=a(Ho," ("),Re=r(Ho,"A",{href:!0,rel:!0});var ei=n(Re);Ra=a(ei,"introduced by Apple"),ei.forEach(o),xa=a(Ho,") is optimized for ANE devices, which is available in modern iPhones, iPads and M-series computers."),Ho.forEach(o),Oa=h(Kl),U=r(Kl,"LI",{});var N=n(U);Va=a(N,"The \u201Coriginal\u201D attention (the base implementation used in "),Ot=r(N,"CODE",{});var ti=n(Ot);qa=a(ti,"diffusers"),ti.forEach(o),Qa=a(N,") is only compatible with CPU/GPU and not ANE. It can be "),Vt=r(N,"EM",{});var oi=n(Vt);Ha=a(oi,"faster"),oi.forEach(o),za=a(N," to run your model on CPU + GPU using "),qt=r(N,"CODE",{});var li=n(qt);Ka=a(li,"original"),li.forEach(o),es=a(N," attention than ANE. See "),xe=r(N,"A",{href:!0,rel:!0});var ai=n(xe);ts=a(ai,"this performance benchmark"),ai.forEach(o),os=a(N," as well as some "),Oe=r(N,"A",{href:!0,rel:!0});var si=n(Oe);ls=a(si,"additional measures provided by the community"),si.forEach(o),as=a(N," for additional details."),N.forEach(o),Kl.forEach(o),zl.forEach(o),ss=h(Hl),Ve=r(Hl,"LI",{});var ea=n(Ve);Qt=r(ea,"P",{});var ri=n(Qt);rs=a(ri,"The supported inference framework."),ri.forEach(o),ns=h(ea),qe=r(ea,"UL",{});var ta=n(qe);$t=r(ta,"LI",{});var An=n($t);Ht=r(An,"CODE",{});var ni=n(Ht);is=a(ni,"packages"),ni.forEach(o),ps=a(An," are suitable for Python inference. This can be used to test converted Core ML models before attempting to integrate them inside native apps, or if you want to explore Core ML performance but don\u2019t need to support native apps. For example, an application with a web UI could perfectly use a Python Core ML backend."),An.forEach(o),fs=h(ta),P=r(ta,"LI",{});var re=n(P);zt=r(re,"CODE",{});var ii=n(zt);ds=a(ii,"compiled"),ii.forEach(o),hs=a(re," models are required for Swift code. The "),Kt=r(re,"CODE",{});var pi=n(Kt);cs=a(pi,"compiled"),pi.forEach(o),us=a(re," models in the Hub split the large UNet model weights into several files for compatibility with iOS and iPadOS devices. This corresponds to the "),me=r(re,"A",{href:!0,rel:!0});var Jn=n(me);eo=r(Jn,"CODE",{});var fi=n(eo);ms=a(fi,"--chunk-unet"),fi.forEach(o),vs=a(Jn," conversion option"),Jn.forEach(o),ys=a(re,". If you want to support native apps, then you need to select the "),to=r(re,"CODE",{});var di=n(to);ws=a(di,"compiled"),di.forEach(o),bs=a(re," variant."),re.forEach(o),ta.forEach(o),ea.forEach(o),Hl.forEach(o),pl=h(e),ve=r(e,"P",{});var oa=n(ve);Ms=a(oa,"The official Core ML Stable Diffusion "),Qe=r(oa,"A",{href:!0,rel:!0});var hi=n(Qe);_s=a(hi,"models"),hi.forEach(o),Es=a(oa," include these variants, but the community ones may vary:"),oa.forEach(o),fl=h(e),v(He.$$.fragment,e),dl=h(e),Tt=r(e,"P",{});var ci=n(Tt);Cs=a(ci,"You can download and use the variant you need as shown below."),ci.forEach(o),hl=h(e),z=r(e,"H2",{class:!0});var la=n(z);ye=r(la,"A",{id:!0,class:!0,href:!0});var ui=n(ye);oo=r(ui,"SPAN",{});var mi=n(oo);v(ze.$$.fragment,mi),mi.forEach(o),ui.forEach(o),Us=h(la),lo=r(la,"SPAN",{});var vi=n(lo);gs=a(vi,"Core ML Inference in Python"),vi.forEach(o),la.forEach(o),cl=h(e),St=r(e,"P",{});var yi=n(St);ks=a(yi,"Install the following libraries to run Core ML inference in Python:"),yi.forEach(o),ul=h(e),v(Ke.$$.fragment,e),ml=h(e),K=r(e,"H3",{class:!0});var aa=n(K);we=r(aa,"A",{id:!0,class:!0,href:!0});var wi=n(we);ao=r(wi,"SPAN",{});var bi=n(ao);v(et.$$.fragment,bi),bi.forEach(o),wi.forEach(o),$s=h(aa),so=r(aa,"SPAN",{});var Mi=n(so);Ts=a(Mi,"Download the Model Checkpoints"),Mi.forEach(o),aa.forEach(o),vl=h(e),k=r(e,"P",{});var R=n(k);Ss=a(R,"To run inference in Python, use one of the versions stored in the "),ro=r(R,"CODE",{});var _i=n(ro);As=a(_i,"packages"),_i.forEach(o),Js=a(R," folders because the "),no=r(R,"CODE",{});var Ei=n(no);Ps=a(Ei,"compiled"),Ei.forEach(o),js=a(R," ones are only compatible with Swift. You may choose whether you want to use "),io=r(R,"CODE",{});var Ci=n(io);Ns=a(Ci,"original"),Ci.forEach(o),Gs=a(R," or "),po=r(R,"CODE",{});var Ui=n(po);Ls=a(Ui,"split_einsum"),Ui.forEach(o),Is=a(R," attention."),R.forEach(o),yl=h(e),X=r(e,"P",{});var Lt=n(X);Ds=a(Lt,"This is how you\u2019d download the "),fo=r(Lt,"CODE",{});var gi=n(fo);Bs=a(gi,"original"),gi.forEach(o),Zs=a(Lt," attention variant from the Hub to a directory called "),ho=r(Lt,"CODE",{});var ki=n(ho);Xs=a(ki,"models"),ki.forEach(o),Ws=a(Lt,":"),Lt.forEach(o),wl=h(e),v(tt.$$.fragment,e),bl=h(e),ee=r(e,"H3",{class:!0});var sa=n(ee);be=r(sa,"A",{id:!0,class:!0,href:!0});var $i=n(be);co=r($i,"SPAN",{});var Ti=n(co);v(ot.$$.fragment,Ti),Ti.forEach(o),$i.forEach(o),Ys=h(sa),uo=r(sa,"SPAN",{});var Si=n(uo);Fs=a(Si,"Inference"),Si.forEach(o),sa.forEach(o),Ml=h(e),At=r(e,"P",{});var Ai=n(At);Rs=a(Ai,"Once you have downloaded a snapshot of the model, you can test it using Apple\u2019s Python script."),Ai.forEach(o),_l=h(e),v(lt.$$.fragment,e),El=h(e),_=r(e,"P",{});var A=n(_);mo=r(A,"CODE",{});var Ji=n(mo);xs=a(Ji,"<output-mlpackages-directory>"),Ji.forEach(o),Os=a(A," should point to the checkpoint you downloaded in the step above, and "),vo=r(A,"CODE",{});var Pi=n(vo);Vs=a(Pi,"--compute-unit"),Pi.forEach(o),qs=a(A," indicates the hardware you want to allow for inference. It must be one of the following options: "),yo=r(A,"CODE",{});var ji=n(yo);Qs=a(ji,"ALL"),ji.forEach(o),Hs=a(A,", "),wo=r(A,"CODE",{});var Ni=n(wo);zs=a(Ni,"CPU_AND_GPU"),Ni.forEach(o),Ks=a(A,", "),bo=r(A,"CODE",{});var Gi=n(bo);er=a(Gi,"CPU_ONLY"),Gi.forEach(o),tr=a(A,", "),Mo=r(A,"CODE",{});var Li=n(Mo);or=a(Li,"CPU_AND_NE"),Li.forEach(o),lr=a(A,". You may also provide an optional output path, and a seed for reproducibility."),A.forEach(o),Cl=h(e),j=r(e,"P",{});var Se=n(j);ar=a(Se,"The inference script assumes you\u2019re using the original version of the Stable Diffusion model, "),_o=r(Se,"CODE",{});var Ii=n(_o);sr=a(Ii,"CompVis/stable-diffusion-v1-4"),Ii.forEach(o),rr=a(Se,". If you use another model, you "),Eo=r(Se,"EM",{});var Di=n(Eo);nr=a(Di,"have"),Di.forEach(o),ir=a(Se," to specify its Hub id in the inference command line, using the "),Co=r(Se,"CODE",{});var Bi=n(Co);pr=a(Bi,"--model-version"),Bi.forEach(o),fr=a(Se," option. This works for models already supported and custom models you trained or fine-tuned yourself."),Se.forEach(o),Ul=h(e),Me=r(e,"P",{});var ra=n(Me);dr=a(ra,"For example, if you want to use "),at=r(ra,"A",{href:!0,rel:!0});var Zi=n(at);Uo=r(Zi,"CODE",{});var Xi=n(Uo);hr=a(Xi,"runwayml/stable-diffusion-v1-5"),Xi.forEach(o),Zi.forEach(o),cr=a(ra,":"),ra.forEach(o),gl=h(e),v(st.$$.fragment,e),kl=h(e),te=r(e,"H2",{class:!0});var na=n(te);_e=r(na,"A",{id:!0,class:!0,href:!0});var Wi=n(_e);go=r(Wi,"SPAN",{});var Yi=n(go);v(rt.$$.fragment,Yi),Yi.forEach(o),Wi.forEach(o),ur=h(na),ko=r(na,"SPAN",{});var Fi=n(ko);mr=a(Fi,"Core ML inference in Swift"),Fi.forEach(o),na.forEach(o),$l=h(e),Ee=r(e,"P",{});var ia=n(Ee);vr=a(ia,"Running inference in Swift is slightly faster than in Python because the models are already compiled in the "),$o=r(ia,"CODE",{});var Ri=n($o);yr=a(Ri,"mlmodelc"),Ri.forEach(o),wr=a(ia," format. This is noticeable on app startup when the model is loaded but shouldn\u2019t be noticeable if you run several generations afterward."),ia.forEach(o),Tl=h(e),oe=r(e,"H3",{class:!0});var pa=n(oe);Ce=r(pa,"A",{id:!0,class:!0,href:!0});var xi=n(Ce);To=r(xi,"SPAN",{});var Oi=n(To);v(nt.$$.fragment,Oi),Oi.forEach(o),xi.forEach(o),br=h(pa),So=r(pa,"SPAN",{});var Vi=n(So);Mr=a(Vi,"Download"),Vi.forEach(o),pa.forEach(o),Sl=h(e),W=r(e,"P",{});var It=n(W);_r=a(It,"To run inference in Swift on your Mac, you need one of the "),Ao=r(It,"CODE",{});var qi=n(Ao);Er=a(qi,"compiled"),qi.forEach(o),Cr=a(It," checkpoint versions. We recommend you download them locally using Python code similar to the previous example, but with one of the "),Jo=r(It,"CODE",{});var Qi=n(Jo);Ur=a(Qi,"compiled"),Qi.forEach(o),gr=a(It," variants:"),It.forEach(o),Al=h(e),v(it.$$.fragment,e),Jl=h(e),le=r(e,"H3",{class:!0});var fa=n(le);Ue=r(fa,"A",{id:!0,class:!0,href:!0});var Hi=n(Ue);Po=r(Hi,"SPAN",{});var zi=n(Po);v(pt.$$.fragment,zi),zi.forEach(o),Hi.forEach(o),kr=h(fa),jo=r(fa,"SPAN",{});var Ki=n(jo);$r=a(Ki,"Inference"),Ki.forEach(o),fa.forEach(o),Pl=h(e),Jt=r(e,"P",{});var ep=n(Jt);Tr=a(ep,"To run inference, please clone Apple\u2019s repo:"),ep.forEach(o),jl=h(e),v(ft.$$.fragment,e),Nl=h(e),ge=r(e,"P",{});var da=n(ge);Sr=a(da,"And then use Apple\u2019s command line tool, "),dt=r(da,"A",{href:!0,rel:!0});var tp=n(dt);Ar=a(tp,"Swift Package Manager"),tp.forEach(o),Jr=a(da,":"),da.forEach(o),Gl=h(e),v(ht.$$.fragment,e),Ll=h(e),u=r(e,"P",{});var E=n(u);Pr=a(E,"You have to specify in "),No=r(E,"CODE",{});var op=n(No);jr=a(op,"--resource-path"),op.forEach(o),Nr=a(E," one of the checkpoints downloaded in the previous step, so please make sure it contains compiled Core ML bundles with the extension "),Go=r(E,"CODE",{});var lp=n(Go);Gr=a(lp,".mlmodelc"),lp.forEach(o),Lr=a(E,". The "),Lo=r(E,"CODE",{});var ap=n(Lo);Ir=a(ap,"--compute-units"),ap.forEach(o),Dr=a(E," has to be one of these values: "),Io=r(E,"CODE",{});var sp=n(Io);Br=a(sp,"all"),sp.forEach(o),Zr=a(E,", "),Do=r(E,"CODE",{});var rp=n(Do);Xr=a(rp,"cpuOnly"),rp.forEach(o),Wr=a(E,", "),Bo=r(E,"CODE",{});var np=n(Bo);Yr=a(np,"cpuAndGPU"),np.forEach(o),Fr=a(E,", "),Zo=r(E,"CODE",{});var ip=n(Zo);Rr=a(ip,"cpuAndNeuralEngine"),ip.forEach(o),xr=a(E,"."),E.forEach(o),Il=h(e),ke=r(e,"P",{});var ha=n(ke);Or=a(ha,"For more details, please refer to the "),ct=r(ha,"A",{href:!0,rel:!0});var pp=n(ct);Vr=a(pp,"instructions in Apple\u2019s repo"),pp.forEach(o),qr=a(ha,"."),ha.forEach(o),Dl=h(e),ae=r(e,"H2",{class:!0});var ca=n(ae);$e=r(ca,"A",{id:!0,class:!0,href:!0});var fp=n($e);Xo=r(fp,"SPAN",{});var dp=n(Xo);v(ut.$$.fragment,dp),dp.forEach(o),fp.forEach(o),Qr=h(ca),Wo=r(ca,"SPAN",{});var hp=n(Wo);Hr=a(hp,"Supported Diffusers Features"),hp.forEach(o),ca.forEach(o),Bl=h(e),Pt=r(e,"P",{});var cp=n(Pt);zr=a(cp,"The Core ML models and inference code don\u2019t support many of the features, options, and flexibility of \u{1F9E8} Diffusers. These are some of the limitations to keep in mind:"),cp.forEach(o),Zl=h(e),Y=r(e,"UL",{});var Dt=n(Y);Yo=r(Dt,"LI",{});var up=n(Yo);Kr=a(up,"Core ML models are only suitable for inference. They can\u2019t be used for training or fine-tuning."),up.forEach(o),en=h(Dt),L=r(Dt,"LI",{});var Ae=n(L);tn=a(Ae,"Only two schedulers have been ported to Swift, the default one used by Stable Diffusion and "),Fo=r(Ae,"CODE",{});var mp=n(Fo);on=a(mp,"DPMSolverMultistepScheduler"),mp.forEach(o),ln=a(Ae,", which we ported to Swift from our "),Ro=r(Ae,"CODE",{});var vp=n(Ro);an=a(vp,"diffusers"),vp.forEach(o),sn=a(Ae," implementation. We recommend you use "),xo=r(Ae,"CODE",{});var yp=n(xo);rn=a(yp,"DPMSolverMultistepScheduler"),yp.forEach(o),nn=a(Ae,", since it produces the same quality in about half the steps."),Ae.forEach(o),pn=h(Dt),Oo=r(Dt,"LI",{});var wp=n(Oo);fn=a(wp,"Negative prompts, classifier-free guidance scale, and image-to-image tasks are available in the inference code. Advanced features such as depth guidance, ControlNet, and latent upscalers are not available yet."),wp.forEach(o),Dt.forEach(o),Xl=h(e),F=r(e,"P",{});var Bt=n(F);dn=a(Bt,"Apple\u2019s "),mt=r(Bt,"A",{href:!0,rel:!0});var bp=n(mt);hn=a(bp,"conversion and inference repo"),bp.forEach(o),cn=a(Bt," and our own "),vt=r(Bt,"A",{href:!0,rel:!0});var Mp=n(vt);un=a(Mp,"swift-coreml-diffusers"),Mp.forEach(o),mn=a(Bt," repos are intended as technology demonstrators to enable other developers to build upon."),Bt.forEach(o),Wl=h(e),jt=r(e,"P",{});var _p=n(jt);vn=a(_p,"If you feel strongly about any missing features, please feel free to open a feature request or, better yet, a contribution PR :)"),_p.forEach(o),Yl=h(e),se=r(e,"H2",{class:!0});var ua=n(se);Te=r(ua,"A",{id:!0,class:!0,href:!0});var Ep=n(Te);Vo=r(Ep,"SPAN",{});var Cp=n(Vo);v(yt.$$.fragment,Cp),Cp.forEach(o),Ep.forEach(o),yn=h(ua),qo=r(ua,"SPAN",{});var Up=n(qo);wn=a(Up,"Native Diffusers Swift app"),Up.forEach(o),ua.forEach(o),Fl=h(e),$=r(e,"P",{});var x=n($);bn=a(x,"One easy way to run Stable Diffusion on your own Apple hardware is to use "),wt=r(x,"A",{href:!0,rel:!0});var gp=n(wt);Mn=a(gp,"our open-source Swift repo"),gp.forEach(o),_n=a(x,", based on "),Qo=r(x,"CODE",{});var kp=n(Qo);En=a(kp,"diffusers"),kp.forEach(o),Cn=a(x," and Apple\u2019s conversion and inference repo. You can study the code, compile it with "),bt=r(x,"A",{href:!0,rel:!0});var $p=n(bt);Un=a($p,"Xcode"),$p.forEach(o),gn=a(x," and adapt it for your own needs. For your convenience, there\u2019s also a "),Mt=r(x,"A",{href:!0,rel:!0});var Tp=n(Mt);kn=a(Tp,"standalone Mac app in the App Store"),Tp.forEach(o),$n=a(x,", so you can play with it without having to deal with the code or IDE. If you are a developer and have determined that Core ML is the best solution to build your Stable Diffusion app, then you can use the rest of this guide to get started with your project. We can\u2019t wait to see what you\u2019ll build :)"),x.forEach(o),this.h()},h(){f(c,"name","hf:doc:metadata"),f(c,"content",JSON.stringify(Ip)),f(J,"id","how-to-run-stable-diffusion-with-core-ml"),f(J,"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"),f(J,"href","#how-to-run-stable-diffusion-with-core-ml"),f(C,"class","relative group"),f(S,"href","https://developer.apple.com/documentation/coreml"),f(S,"rel","nofollow"),f(fe,"id","stable-diffusion-core-ml-checkpoints"),f(fe,"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"),f(fe,"href","#stable-diffusion-core-ml-checkpoints"),f(Q,"class","relative group"),f(Ge,"href","https://github.com/apple/ml-stable-diffusion#-converting-models-to-core-ml"),f(Ge,"rel","nofollow"),f(Ie,"href","https://huggingface.co/apple"),f(Ie,"rel","nofollow"),f(De,"href","https://huggingface.co/coreml"),f(De,"rel","nofollow"),f(Ze,"href","https://huggingface.co/models?pipeline_tag=text-to-image&library=coreml&p=2&sort=likes"),f(Ze,"rel","nofollow"),f(Xe,"href","https://github.com/apple/ml-stable-diffusion#-converting-models-to-core-ml"),f(Xe,"rel","nofollow"),f(he,"id","selecting-the-core-ml-variant-to-use"),f(he,"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"),f(he,"href","#selecting-the-core-ml-variant-to-use"),f(H,"class","relative group"),f(Re,"href","https://machinelearning.apple.com/research/neural-engine-transformers"),f(Re,"rel","nofollow"),f(xe,"href","https://huggingface.co/blog/fast-mac-diffusers#performance-benchmarks"),f(xe,"rel","nofollow"),f(Oe,"href","https://github.com/huggingface/swift-coreml-diffusers/issues/31"),f(Oe,"rel","nofollow"),f(me,"href","https://github.com/apple/ml-stable-diffusion#-converting-models-to-core-ml"),f(me,"rel","nofollow"),f(Qe,"href","https://huggingface.co/apple/coreml-stable-diffusion-v1-4/tree/main"),f(Qe,"rel","nofollow"),f(ye,"id","core-ml-inference-in-python"),f(ye,"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"),f(ye,"href","#core-ml-inference-in-python"),f(z,"class","relative group"),f(we,"id","download-the-model-checkpoints"),f(we,"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"),f(we,"href","#download-the-model-checkpoints"),f(K,"class","relative group"),f(be,"id","python-inference"),f(be,"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"),f(be,"href","#python-inference"),f(ee,"class","relative group"),f(at,"href","https://huggingface.co/runwayml/stable-diffusion-v1-5"),f(at,"rel","nofollow"),f(_e,"id","core-ml-inference-in-swift"),f(_e,"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"),f(_e,"href","#core-ml-inference-in-swift"),f(te,"class","relative group"),f(Ce,"id","download"),f(Ce,"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"),f(Ce,"href","#download"),f(oe,"class","relative group"),f(Ue,"id","swift-inference"),f(Ue,"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"),f(Ue,"href","#swift-inference"),f(le,"class","relative group"),f(dt,"href","https://www.swift.org/package-manager/#"),f(dt,"rel","nofollow"),f(ct,"href","https://github.com/apple/ml-stable-diffusion"),f(ct,"rel","nofollow"),f($e,"id","supported-diffusers-features"),f($e,"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"),f($e,"href","#supported-diffusers-features"),f(ae,"class","relative group"),f(mt,"href","https://github.com/apple/ml-stable-diffusion"),f(mt,"rel","nofollow"),f(vt,"href","https://github.com/huggingface/swift-coreml-diffusers"),f(vt,"rel","nofollow"),f(Te,"id","native-diffusers-swift-app"),f(Te,"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"),f(Te,"href","#native-diffusers-swift-app"),f(se,"class","relative group"),f(wt,"href","https://github.com/huggingface/swift-coreml-diffusers"),f(wt,"rel","nofollow"),f(bt,"href","https://developer.apple.com/xcode/"),f(bt,"rel","nofollow"),f(Mt,"href","https://apps.apple.com/app/diffusers/id1666309574"),f(Mt,"rel","nofollow")},m(e,i){t(document.head,c),p(e,ne,i),p(e,C,i),t(C,J),t(J,O),y(T,O,null),t(C,Pe),t(C,V),t(V,D),p(e,ie,i),p(e,G,i),t(G,S),t(S,g),t(G,Et),p(e,je,i),p(e,q,i),t(q,ma),p(e,Ko,i),y(pe,e,i),p(e,el,i),p(e,Q,i),t(Q,fe),t(fe,Zt),y(Ne,Zt,null),t(Q,va),t(Q,Xt),t(Xt,ya),p(e,tl,i),p(e,Ct,i),t(Ct,wa),p(e,ol,i),p(e,B,i),t(B,ba),t(B,Ge),t(Ge,Ma),t(B,_a),t(B,Wt),t(Wt,Ea),t(B,Ca),p(e,ll,i),p(e,Ut,i),t(Ut,Ua),p(e,al,i),p(e,Z,i),t(Z,Le),t(Le,ga),t(Le,Ie),t(Ie,ka),t(Le,$a),t(Z,Ta),t(Z,gt),t(gt,De),t(De,Sa),t(gt,Aa),t(Z,Ja),t(Z,Be),t(Be,Pa),t(Be,Ze),t(Ze,ja),t(Be,Na),p(e,sl,i),p(e,de,i),t(de,Ga),t(de,Xe),t(Xe,La),t(de,Ia),p(e,rl,i),p(e,H,i),t(H,he),t(he,Yt),y(We,Yt,null),t(H,Da),t(H,Ft),t(Ft,Ba),p(e,nl,i),p(e,kt,i),t(kt,Za),p(e,il,i),p(e,ce,i),t(ce,Ye),t(Ye,Rt),t(Rt,Xa),t(Ye,Wa),t(Ye,Fe),t(Fe,ue),t(ue,xt),t(xt,Ya),t(ue,Fa),t(ue,Re),t(Re,Ra),t(ue,xa),t(Fe,Oa),t(Fe,U),t(U,Va),t(U,Ot),t(Ot,qa),t(U,Qa),t(U,Vt),t(Vt,Ha),t(U,za),t(U,qt),t(qt,Ka),t(U,es),t(U,xe),t(xe,ts),t(U,os),t(U,Oe),t(Oe,ls),t(U,as),t(ce,ss),t(ce,Ve),t(Ve,Qt),t(Qt,rs),t(Ve,ns),t(Ve,qe),t(qe,$t),t($t,Ht),t(Ht,is),t($t,ps),t(qe,fs),t(qe,P),t(P,zt),t(zt,ds),t(P,hs),t(P,Kt),t(Kt,cs),t(P,us),t(P,me),t(me,eo),t(eo,ms),t(me,vs),t(P,ys),t(P,to),t(to,ws),t(P,bs),p(e,pl,i),p(e,ve,i),t(ve,Ms),t(ve,Qe),t(Qe,_s),t(ve,Es),p(e,fl,i),y(He,e,i),p(e,dl,i),p(e,Tt,i),t(Tt,Cs),p(e,hl,i),p(e,z,i),t(z,ye),t(ye,oo),y(ze,oo,null),t(z,Us),t(z,lo),t(lo,gs),p(e,cl,i),p(e,St,i),t(St,ks),p(e,ul,i),y(Ke,e,i),p(e,ml,i),p(e,K,i),t(K,we),t(we,ao),y(et,ao,null),t(K,$s),t(K,so),t(so,Ts),p(e,vl,i),p(e,k,i),t(k,Ss),t(k,ro),t(ro,As),t(k,Js),t(k,no),t(no,Ps),t(k,js),t(k,io),t(io,Ns),t(k,Gs),t(k,po),t(po,Ls),t(k,Is),p(e,yl,i),p(e,X,i),t(X,Ds),t(X,fo),t(fo,Bs),t(X,Zs),t(X,ho),t(ho,Xs),t(X,Ws),p(e,wl,i),y(tt,e,i),p(e,bl,i),p(e,ee,i),t(ee,be),t(be,co),y(ot,co,null),t(ee,Ys),t(ee,uo),t(uo,Fs),p(e,Ml,i),p(e,At,i),t(At,Rs),p(e,_l,i),y(lt,e,i),p(e,El,i),p(e,_,i),t(_,mo),t(mo,xs),t(_,Os),t(_,vo),t(vo,Vs),t(_,qs),t(_,yo),t(yo,Qs),t(_,Hs),t(_,wo),t(wo,zs),t(_,Ks),t(_,bo),t(bo,er),t(_,tr),t(_,Mo),t(Mo,or),t(_,lr),p(e,Cl,i),p(e,j,i),t(j,ar),t(j,_o),t(_o,sr),t(j,rr),t(j,Eo),t(Eo,nr),t(j,ir),t(j,Co),t(Co,pr),t(j,fr),p(e,Ul,i),p(e,Me,i),t(Me,dr),t(Me,at),t(at,Uo),t(Uo,hr),t(Me,cr),p(e,gl,i),y(st,e,i),p(e,kl,i),p(e,te,i),t(te,_e),t(_e,go),y(rt,go,null),t(te,ur),t(te,ko),t(ko,mr),p(e,$l,i),p(e,Ee,i),t(Ee,vr),t(Ee,$o),t($o,yr),t(Ee,wr),p(e,Tl,i),p(e,oe,i),t(oe,Ce),t(Ce,To),y(nt,To,null),t(oe,br),t(oe,So),t(So,Mr),p(e,Sl,i),p(e,W,i),t(W,_r),t(W,Ao),t(Ao,Er),t(W,Cr),t(W,Jo),t(Jo,Ur),t(W,gr),p(e,Al,i),y(it,e,i),p(e,Jl,i),p(e,le,i),t(le,Ue),t(Ue,Po),y(pt,Po,null),t(le,kr),t(le,jo),t(jo,$r),p(e,Pl,i),p(e,Jt,i),t(Jt,Tr),p(e,jl,i),y(ft,e,i),p(e,Nl,i),p(e,ge,i),t(ge,Sr),t(ge,dt),t(dt,Ar),t(ge,Jr),p(e,Gl,i),y(ht,e,i),p(e,Ll,i),p(e,u,i),t(u,Pr),t(u,No),t(No,jr),t(u,Nr),t(u,Go),t(Go,Gr),t(u,Lr),t(u,Lo),t(Lo,Ir),t(u,Dr),t(u,Io),t(Io,Br),t(u,Zr),t(u,Do),t(Do,Xr),t(u,Wr),t(u,Bo),t(Bo,Yr),t(u,Fr),t(u,Zo),t(Zo,Rr),t(u,xr),p(e,Il,i),p(e,ke,i),t(ke,Or),t(ke,ct),t(ct,Vr),t(ke,qr),p(e,Dl,i),p(e,ae,i),t(ae,$e),t($e,Xo),y(ut,Xo,null),t(ae,Qr),t(ae,Wo),t(Wo,Hr),p(e,Bl,i),p(e,Pt,i),t(Pt,zr),p(e,Zl,i),p(e,Y,i),t(Y,Yo),t(Yo,Kr),t(Y,en),t(Y,L),t(L,tn),t(L,Fo),t(Fo,on),t(L,ln),t(L,Ro),t(Ro,an),t(L,sn),t(L,xo),t(xo,rn),t(L,nn),t(Y,pn),t(Y,Oo),t(Oo,fn),p(e,Xl,i),p(e,F,i),t(F,dn),t(F,mt),t(mt,hn),t(F,cn),t(F,vt),t(vt,un),t(F,mn),p(e,Wl,i),p(e,jt,i),t(jt,vn),p(e,Yl,i),p(e,se,i),t(se,Te),t(Te,Vo),y(yt,Vo,null),t(se,yn),t(se,qo),t(qo,wn),p(e,Fl,i),p(e,$,i),t($,bn),t($,wt),t(wt,Mn),t($,_n),t($,Qo),t(Qo,En),t($,Cn),t($,bt),t(bt,Un),t($,gn),t($,Mt),t(Mt,kn),t($,$n),Rl=!0},p(e,[i]){const _t={};i&2&&(_t.$$scope={dirty:i,ctx:e}),pe.$set(_t)},i(e){Rl||(w(T.$$.fragment,e),w(pe.$$.fragment,e),w(Ne.$$.fragment,e),w(We.$$.fragment,e),w(He.$$.fragment,e),w(ze.$$.fragment,e),w(Ke.$$.fragment,e),w(et.$$.fragment,e),w(tt.$$.fragment,e),w(ot.$$.fragment,e),w(lt.$$.fragment,e),w(st.$$.fragment,e),w(rt.$$.fragment,e),w(nt.$$.fragment,e),w(it.$$.fragment,e),w(pt.$$.fragment,e),w(ft.$$.fragment,e),w(ht.$$.fragment,e),w(ut.$$.fragment,e),w(yt.$$.fragment,e),Rl=!0)},o(e){b(T.$$.fragment,e),b(pe.$$.fragment,e),b(Ne.$$.fragment,e),b(We.$$.fragment,e),b(He.$$.fragment,e),b(ze.$$.fragment,e),b(Ke.$$.fragment,e),b(et.$$.fragment,e),b(tt.$$.fragment,e),b(ot.$$.fragment,e),b(lt.$$.fragment,e),b(st.$$.fragment,e),b(rt.$$.fragment,e),b(nt.$$.fragment,e),b(it.$$.fragment,e),b(pt.$$.fragment,e),b(ft.$$.fragment,e),b(ht.$$.fragment,e),b(ut.$$.fragment,e),b(yt.$$.fragment,e),Rl=!1},d(e){o(c),e&&o(ne),e&&o(C),M(T),e&&o(ie),e&&o(G),e&&o(je),e&&o(q),e&&o(Ko),M(pe,e),e&&o(el),e&&o(Q),M(Ne),e&&o(tl),e&&o(Ct),e&&o(ol),e&&o(B),e&&o(ll),e&&o(Ut),e&&o(al),e&&o(Z),e&&o(sl),e&&o(de),e&&o(rl),e&&o(H),M(We),e&&o(nl),e&&o(kt),e&&o(il),e&&o(ce),e&&o(pl),e&&o(ve),e&&o(fl),M(He,e),e&&o(dl),e&&o(Tt),e&&o(hl),e&&o(z),M(ze),e&&o(cl),e&&o(St),e&&o(ul),M(Ke,e),e&&o(ml),e&&o(K),M(et),e&&o(vl),e&&o(k),e&&o(yl),e&&o(X),e&&o(wl),M(tt,e),e&&o(bl),e&&o(ee),M(ot),e&&o(Ml),e&&o(At),e&&o(_l),M(lt,e),e&&o(El),e&&o(_),e&&o(Cl),e&&o(j),e&&o(Ul),e&&o(Me),e&&o(gl),M(st,e),e&&o(kl),e&&o(te),M(rt),e&&o($l),e&&o(Ee),e&&o(Tl),e&&o(oe),M(nt),e&&o(Sl),e&&o(W),e&&o(Al),M(it,e),e&&o(Jl),e&&o(le),M(pt),e&&o(Pl),e&&o(Jt),e&&o(jl),M(ft,e),e&&o(Nl),e&&o(ge),e&&o(Gl),M(ht,e),e&&o(Ll),e&&o(u),e&&o(Il),e&&o(ke),e&&o(Dl),e&&o(ae),M(ut),e&&o(Bl),e&&o(Pt),e&&o(Zl),e&&o(Y),e&&o(Xl),e&&o(F),e&&o(Wl),e&&o(jt),e&&o(Yl),e&&o(se),M(yt),e&&o(Fl),e&&o($)}}}const Ip={local:"how-to-run-stable-diffusion-with-core-ml",sections:[{local:"stable-diffusion-core-ml-checkpoints",title:"Stable Diffusion Core ML Checkpoints"},{local:"selecting-the-core-ml-variant-to-use",title:"Selecting the Core ML Variant to Use"},{local:"core-ml-inference-in-python",sections:[{local:"download-the-model-checkpoints",title:"Download the Model Checkpoints"},{local:"python-inference",title:"Inference"}],title:"Core ML Inference in Python"},{local:"core-ml-inference-in-swift",sections:[{local:"download",title:"Download"},{local:"swift-inference",title:"Inference"}],title:"Core ML inference in Swift"},{local:"supported-diffusers-features",title:"Supported Diffusers Features"},{local:"native-diffusers-swift-app",title:"Native Diffusers Swift app"}],title:"How to run Stable Diffusion with Core ML"};function Dp(zo){return jp(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Yp extends Sp{constructor(c){super();Ap(this,c,Dp,Lp,Jp,{})}}export{Yp as default,Ip as metadata};
