import{S as $n,i as yn,s as kn,e as l,k as f,w as u,t as i,M as jn,c as r,d as t,m as h,a as n,x as c,h as o,b as m,N as bn,G as s,g as p,y as d,L as En,q as w,o as g,B as v,v as xn}from"../../chunks/vendor-hf-doc-builder.js";import{I as me}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as _}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as Pn}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";function Tn(Ll){let S,Es,z,J,bt,ue,li,$t,ri,xs,ce,Ps,T,ni,de,pi,fi,yt,hi,mi,Ts,X,ui,we,ci,di,As,y,wi,kt,gi,vi,jt,_i,bi,Et,$i,yi,Cs,Ze,ki,Ds,F,M,xt,ge,ji,Pt,Ei,qs,et,xi,Os,ve,Ss,_e,zs,be,Fs,$e,Us,tt,Pi,Is,ye,Ws,U,G,Tt,ke,Ti,At,Ai,Ns,A,Ci,Ct,Di,qi,Dt,Oi,Si,Ls,je,Hs,R,zi,qt,Fi,Ui,Js,Ee,Xs,I,Y,Ot,xe,Ii,St,Wi,Ms,st,Ni,Gs,V,Li,zt,Hi,Ji,Rs,Pe,Ys,Te,Vs,W,B,Ft,Ae,Xi,Ut,Mi,Bs,C,Gi,It,Ri,Yi,Wt,Vi,Bi,Ks,Ce,Qs,De,Zs,qe,ea,b,Ki,Nt,Qi,Zi,Lt,eo,to,Ht,so,ao,Jt,io,oo,Xt,lo,ro,ta,at,no,sa,it,po,aa,Oe,ia,ot,fo,oa,Se,la,lt,ho,ra,K,mo,Mt,uo,co,na,Q,wo,Gt,Rt,go,vo,pa,ze,fa,Fe,ha,D,_o,Yt,bo,$o,Vt,yo,ko,ma,Ue,ua,N,Z,Bt,Ie,jo,Kt,Eo,ca,rt,xo,da,We,wa,Ne,ga,nt,pt,Hl,va,L,ee,Qt,Le,Po,Zt,To,_a,te,Ao,es,Co,Do,ba,ft,qo,$a,He,ya,Je,ka,ht,mt,Jl,ja,H,se,ts,Xe,Oo,ss,So,Ea,ae,zo,as,Fo,Uo,xa,k,Io,is,Wo,No,Me,Lo,Ho,ie,os,Jo,Xo,Mo,Pa,Ge,ls,Go,Ro,Ta,oe,P,Yo,rs,Vo,Bo,ns,Ko,Qo,ps,Zo,el,tl,fs,sl,Aa,j,al,hs,il,ol,ms,ll,rl,us,nl,pl,Ca,Re,Da,q,fl,cs,hl,ml,ds,ul,cl,qa,le,Ye,dl,ws,wl,gl,vl,$,_l,gs,bl,$l,vs,yl,kl,_s,jl,El,bs,xl,Pl,$s,Tl,Al,Oa,O,Cl,ys,Dl,ql,ks,Ol,Sl,Sa,ut,zl,za,Ve,Fa,Be,Ua,Ke,Ia,Qe,Wa,re,Fl,js,Ul,Il,Na;return ue=new me({}),ce=new Pn({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/stable_diffusion_jax_how_to.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/stable_diffusion_jax_how_to.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/stable_diffusion_jax_how_to.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/stable_diffusion_jax_how_to.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/stable_diffusion_jax_how_to.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/stable_diffusion_jax_how_to.ipynb"}]}}),ge=new me({}),ve=new _({props:{code:`!pip install jax==0.3.25 jaxlib==0.3.25 flax transformers ftfy
!pip install diffusers`,highlighted:`!pip install jax==0.3.25 jaxlib==0.3.25 flax transformers ftfy
!pip install diffusers`}}),_e=new _({props:{code:`import jax.tools.colab_tpu

jax.tools.colab_tpu.setup_tpu()
import jax`,highlighted:`<span class="hljs-keyword">import</span> jax.tools.colab_tpu

jax.tools.colab_tpu.setup_tpu()
<span class="hljs-keyword">import</span> jax`}}),be=new _({props:{code:`num_devices = jax.device_count()
device_type = jax.devices()[0].device_kind

print(f"Found {num_devices} JAX devices of type {device_type}.")
assert (
    "TPU" in device_type
), "Available device is not a TPU, please select TPU from Edit > Notebook settings > Hardware accelerator"`,highlighted:`num_devices = jax.device_count()
device_type = jax.devices()[<span class="hljs-number">0</span>].device_kind

<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Found <span class="hljs-subst">{num_devices}</span> JAX devices of type <span class="hljs-subst">{device_type}</span>.&quot;</span>)
<span class="hljs-keyword">assert</span> (
    <span class="hljs-string">&quot;TPU&quot;</span> <span class="hljs-keyword">in</span> device_type
), <span class="hljs-string">&quot;Available device is not a TPU, please select TPU from Edit &gt; Notebook settings &gt; Hardware accelerator&quot;</span>`}}),$e=new _({props:{code:"Found 8 JAX devices of type Cloud TPU.",highlighted:'Found <span class="hljs-number">8</span> JAX devices of <span class="hljs-built_in">type</span> Cloud TPU.'}}),ye=new _({props:{code:`import numpy as np
import jax
import jax.numpy as jnp

from pathlib import Path
from jax import pmap
from flax.jax_utils import replicate
from flax.training.common_utils import shard
from PIL import Image

from huggingface_hub import notebook_login
from diffusers import FlaxStableDiffusionPipeline`,highlighted:`<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> jax
<span class="hljs-keyword">import</span> jax.numpy <span class="hljs-keyword">as</span> jnp

<span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
<span class="hljs-keyword">from</span> jax <span class="hljs-keyword">import</span> pmap
<span class="hljs-keyword">from</span> flax.jax_utils <span class="hljs-keyword">import</span> replicate
<span class="hljs-keyword">from</span> flax.training.common_utils <span class="hljs-keyword">import</span> shard
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image

<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> FlaxStableDiffusionPipeline`}}),ke=new me({}),je=new _({props:{code:"dtype = jnp.bfloat16",highlighted:"dtype = jnp.bfloat16"}}),Ee=new _({props:{code:`pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4",
    revision="bf16",
    dtype=dtype,
)`,highlighted:`pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>,
    revision=<span class="hljs-string">&quot;bf16&quot;</span>,
    dtype=dtype,
)`}}),xe=new me({}),Pe=new _({props:{code:`prompt = "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of field, close up, split lighting, cinematic"
prompt = [prompt] * jax.device_count()
prompt_ids = pipeline.prepare_inputs(prompt)
prompt_ids.shape`,highlighted:`prompt = <span class="hljs-string">&quot;A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of field, close up, split lighting, cinematic&quot;</span>
prompt = [prompt] * jax.device_count()
prompt_ids = pipeline.prepare_inputs(prompt)
prompt_ids.shape`}}),Te=new _({props:{code:"(8, 77)",highlighted:'(<span class="hljs-number">8</span>, <span class="hljs-number">77</span>)'}}),Ae=new me({}),Ce=new _({props:{code:"p_params = replicate(params)",highlighted:"p_params = replicate(params)"}}),De=new _({props:{code:`prompt_ids = shard(prompt_ids)
prompt_ids.shape`,highlighted:`prompt_ids = shard(prompt_ids)
prompt_ids.shape`}}),qe=new _({props:{code:"(8, 1, 77)",highlighted:'(<span class="hljs-number">8</span>, <span class="hljs-number">1</span>, <span class="hljs-number">77</span>)'}}),Oe=new _({props:{code:`def create_key(seed=0):
    return jax.random.PRNGKey(seed)`,highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">create_key</span>(<span class="hljs-params">seed=<span class="hljs-number">0</span></span>):
    <span class="hljs-keyword">return</span> jax.random.PRNGKey(seed)`}}),Se=new _({props:{code:`rng = create_key(0)
rng = jax.random.split(rng, jax.device_count())`,highlighted:`rng = create_key(<span class="hljs-number">0</span>)
rng = jax.random.split(rng, jax.device_count())`}}),ze=new _({props:{code:`%%time
images = pipeline(prompt_ids, p_params, rng, jit=True)[0]`,highlighted:`%%time
images = pipeline(prompt_ids, p_params, rng, <span class="hljs-attribute">jit</span>=<span class="hljs-literal">True</span>)[0]`}}),Fe=new _({props:{code:`CPU times: user 56.2 s, sys: 42.5 s, total: 1min 38s
Wall time: 1min 29s`,highlighted:`CPU times: user <span class="hljs-number">56.2</span> s, sys: <span class="hljs-number">42.5</span> s, total: 1<span class="hljs-built_in">min</span> 38s
Wall time: 1<span class="hljs-built_in">min</span> 29s`}}),Ue=new _({props:{code:`images = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:])
images = pipeline.numpy_to_pil(images)`,highlighted:`images = images.reshape((images.shape[<span class="hljs-number">0</span>] * images.shape[<span class="hljs-number">1</span>],) + images.shape[-<span class="hljs-number">3</span>:])
images = pipeline.numpy_to_pil(images)`}}),Ie=new me({}),We=new _({props:{code:`def image_grid(imgs, rows, cols):
    w, h = imgs[0].size
    grid = Image.new("RGB", size=(cols * w, rows * h))
    for i, img in enumerate(imgs):
        grid.paste(img, box=(i % cols * w, i // cols * h))
    return grid`,highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">image_grid</span>(<span class="hljs-params">imgs, rows, cols</span>):
    w, h = imgs[<span class="hljs-number">0</span>].size
    grid = Image.new(<span class="hljs-string">&quot;RGB&quot;</span>, size=(cols * w, rows * h))
    <span class="hljs-keyword">for</span> i, img <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(imgs):
        grid.paste(img, box=(i % cols * w, i // cols * h))
    <span class="hljs-keyword">return</span> grid`}}),Ne=new _({props:{code:"image_grid(images, 2, 4)",highlighted:'image_grid(images, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>)'}}),Le=new me({}),He=new _({props:{code:`prompts = [
    "Labrador in the style of Hokusai",
    "Painting of a squirrel skating in New York",
    "HAL-9000 in the style of Van Gogh",
    "Times Square under water, with fish and a dolphin swimming around",
    "Ancient Roman fresco showing a man working on his laptop",
    "Close-up photograph of young black woman against urban background, high quality, bokeh",
    "Armchair in the shape of an avocado",
    "Clown astronaut in space, with Earth in the background",
]`,highlighted:`prompts = [
    <span class="hljs-string">&quot;Labrador in the style of Hokusai&quot;</span>,
    <span class="hljs-string">&quot;Painting of a squirrel skating in New York&quot;</span>,
    <span class="hljs-string">&quot;HAL-9000 in the style of Van Gogh&quot;</span>,
    <span class="hljs-string">&quot;Times Square under water, with fish and a dolphin swimming around&quot;</span>,
    <span class="hljs-string">&quot;Ancient Roman fresco showing a man working on his laptop&quot;</span>,
    <span class="hljs-string">&quot;Close-up photograph of young black woman against urban background, high quality, bokeh&quot;</span>,
    <span class="hljs-string">&quot;Armchair in the shape of an avocado&quot;</span>,
    <span class="hljs-string">&quot;Clown astronaut in space, with Earth in the background&quot;</span>,
]`}}),Je=new _({props:{code:`prompt_ids = pipeline.prepare_inputs(prompts)
prompt_ids = shard(prompt_ids)

images = pipeline(prompt_ids, p_params, rng, jit=True).images
images = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:])
images = pipeline.numpy_to_pil(images)

image_grid(images, 2, 4)`,highlighted:`prompt_ids = pipeline.prepare_inputs(prompts)
prompt_ids = shard(prompt_ids)

images = pipeline(prompt_ids, p_params, rng, jit=<span class="hljs-literal">True</span>).images
images = images.reshape((images.shape[<span class="hljs-number">0</span>] * images.shape[<span class="hljs-number">1</span>],) + images.shape[-<span class="hljs-number">3</span>:])
images = pipeline.numpy_to_pil(images)

image_grid(images, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>)`}}),Xe=new me({}),Re=new _({props:{code:"p_generate = pmap(pipeline._generate)",highlighted:"p_generate = pmap(pipeline._generate)"}}),Ve=new _({props:{code:`%%time
images = p_generate(prompt_ids, p_params, rng)
images = images.block_until_ready()
images.shape`,highlighted:`%%time
images = p<span class="hljs-constructor">_generate(<span class="hljs-params">prompt_ids</span>, <span class="hljs-params">p_params</span>, <span class="hljs-params">rng</span>)</span>
images = images.block<span class="hljs-constructor">_until_ready()</span>
images.shape`}}),Be=new _({props:{code:`CPU times: user 1min 15s, sys: 18.2 s, total: 1min 34s
Wall time: 1min 15s`,highlighted:`CPU times: user 1<span class="hljs-built_in">min</span> 15s, sys: <span class="hljs-number">18.2</span> s, total: 1<span class="hljs-built_in">min</span> 34s
Wall time: 1<span class="hljs-built_in">min</span> 15s`}}),Ke=new _({props:{code:"images.shape",highlighted:"images.shape"}}),Qe=new _({props:{code:"(8, 1, 512, 512, 3)",highlighted:'(<span class="hljs-number">8</span>, <span class="hljs-number">1</span>, <span class="hljs-number">512</span>, <span class="hljs-number">512</span>, <span class="hljs-number">3</span>)'}}),{c(){S=l("meta"),Es=f(),z=l("h1"),J=l("a"),bt=l("span"),u(ue.$$.fragment),li=f(),$t=l("span"),ri=i("\u{1F9E8} Stable Diffusion in JAX / Flax !"),xs=f(),u(ce.$$.fragment),Ps=f(),T=l("p"),ni=i("\u{1F917} Hugging Face "),de=l("a"),pi=i("Diffusers"),fi=i(" supports Flax since version "),yt=l("code"),hi=i("0.5.1"),mi=i("! This allows for super fast inference on Google TPUs, such as those available in Colab, Kaggle or Google Cloud Platform."),Ts=f(),X=l("p"),ui=i("This notebook shows how to run inference using JAX / Flax. If you want more details about how Stable Diffusion works or want to run it in GPU, please refer to "),we=l("a"),ci=i("this notebook"),di=i("."),As=f(),y=l("p"),wi=i("First, make sure you are using a TPU backend. If you are running this notebook in Colab, select "),kt=l("code"),gi=i("Runtime"),vi=i(" in the menu above, then select the option \u201CChange runtime type\u201D and then select "),jt=l("code"),_i=i("TPU"),bi=i(" under the "),Et=l("code"),$i=i("Hardware accelerator"),yi=i(" setting."),Cs=f(),Ze=l("p"),ki=i("Note that JAX is not exclusive to TPUs, but it shines on that hardware because each TPU server has 8 TPU accelerators working in parallel."),Ds=f(),F=l("h2"),M=l("a"),xt=l("span"),u(ge.$$.fragment),ji=f(),Pt=l("span"),Ei=i("Setup"),qs=f(),et=l("p"),xi=i("First make sure diffusers is installed."),Os=f(),u(ve.$$.fragment),Ss=f(),u(_e.$$.fragment),zs=f(),u(be.$$.fragment),Fs=f(),u($e.$$.fragment),Us=f(),tt=l("p"),Pi=i("Then we import all the dependencies."),Is=f(),u(ye.$$.fragment),Ws=f(),U=l("h2"),G=l("a"),Tt=l("span"),u(ke.$$.fragment),Ti=f(),At=l("span"),Ai=i("Model Loading"),Ns=f(),A=l("p"),Ci=i("TPU devices support "),Ct=l("code"),Di=i("bfloat16"),qi=i(", an efficient half-float type. We\u2019ll use it for our tests, but you can also use "),Dt=l("code"),Oi=i("float32"),Si=i(" to use full precision instead."),Ls=f(),u(je.$$.fragment),Hs=f(),R=l("p"),zi=i("Flax is a functional framework, so models are stateless and parameters are stored outside them. Loading the pre-trained Flax pipeline will return both the pipeline itself and the model weights (or parameters). We are using a "),qt=l("code"),Fi=i("bf16"),Ui=i(" version of the weights, which leads to type warnings that you can safely ignore."),Js=f(),u(Ee.$$.fragment),Xs=f(),I=l("h2"),Y=l("a"),Ot=l("span"),u(xe.$$.fragment),Ii=f(),St=l("span"),Wi=i("Inference"),Ms=f(),st=l("p"),Ni=i("Since TPUs usually have 8 devices working in parallel, we\u2019ll replicate our prompt as many times as devices we have. Then we\u2019ll perform inference on the 8 devices at once, each responsible for generating one image. Thus, we\u2019ll get 8 images in the same amount of time it takes for one chip to generate a single one."),Gs=f(),V=l("p"),Li=i("After replicating the prompt, we obtain the tokenized text ids by invoking the "),zt=l("code"),Hi=i("prepare_inputs"),Ji=i(" function of the pipeline. The length of the tokenized text is set to 77 tokens, as required by the configuration of the underlying CLIP Text model."),Rs=f(),u(Pe.$$.fragment),Ys=f(),u(Te.$$.fragment),Vs=f(),W=l("h3"),B=l("a"),Ft=l("span"),u(Ae.$$.fragment),Xi=f(),Ut=l("span"),Mi=i("Replication and parallelization"),Bs=f(),C=l("p"),Gi=i("Model parameters and inputs have to be replicated across the 8 parallel devices we have. The parameters dictionary is replicated using "),It=l("code"),Ri=i("flax.jax_utils.replicate"),Yi=i(", which traverses the dictionary and changes the shape of the weights so they are repeated 8 times. Arrays are replicated using "),Wt=l("code"),Vi=i("shard"),Bi=i("."),Ks=f(),u(Ce.$$.fragment),Qs=f(),u(De.$$.fragment),Zs=f(),u(qe.$$.fragment),ea=f(),b=l("p"),Ki=i("That shape means that each one of the "),Nt=l("code"),Qi=i("8"),Zi=i(" devices will receive as an input a "),Lt=l("code"),eo=i("jnp"),to=i(" array with shape "),Ht=l("code"),so=i("(1, 77)"),ao=i(". "),Jt=l("code"),io=i("1"),oo=i(" is therefore the batch size per device. In TPUs with sufficient memory, it could be larger than "),Xt=l("code"),lo=i("1"),ro=i(" if we wanted to generate multiple images (per chip) at once."),ta=f(),at=l("p"),no=i("We are almost ready to generate images! We just need to create a random number generator to pass to the generation function. This is the standard procedure in Flax, which is very serious and opinionated about random numbers \u2013 all functions that deal with random numbers are expected to receive a generator. This ensures reproducibility, even when we are training across multiple distributed devices."),sa=f(),it=l("p"),po=i("The helper function below uses a seed to initialize a random number generator. As long as we use the same seed, we\u2019ll get the exact same results. Feel free to use different seeds when exploring results later in the notebook."),aa=f(),u(Oe.$$.fragment),ia=f(),ot=l("p"),fo=i("We obtain a rng and then \u201Csplit\u201D it 8 times so each device receives a different generator. Therefore, each device will create a different image, and the full process is reproducible."),oa=f(),u(Se.$$.fragment),la=f(),lt=l("p"),ho=i("JAX code can be compiled to an efficient representation that runs very fast. However, we need to ensure that all inputs have the same shape in subsequent calls; otherwise, JAX will have to recompile the code, and we wouldn\u2019t be able to take advantage of the optimized speed."),ra=f(),K=l("p"),mo=i("The Flax pipeline can compile the code for us if we pass "),Mt=l("code"),uo=i("jit = True"),co=i(" as an argument. It will also ensure that the model runs in parallel in the 8 available devices."),na=f(),Q=l("p"),wo=i("The first time we run the following cell it will take a long time to compile, but subequent calls (even with different inputs) will be much faster. For example, it took more than a minute to compile in a TPU v2-8 when I tested, but then it takes about "),Gt=l("strong"),Rt=l("code"),go=i("7s"),vo=i(" for future inference runs."),pa=f(),u(ze.$$.fragment),fa=f(),u(Fe.$$.fragment),ha=f(),D=l("p"),_o=i("The returned array has shape "),Yt=l("code"),bo=i("(8, 1, 512, 512, 3)"),$o=i(". We reshape it to get rid of the second dimension and obtain 8 images of "),Vt=l("code"),yo=i("512 \xD7 512 \xD7 3"),ko=i(" and then convert them to PIL."),ma=f(),u(Ue.$$.fragment),ua=f(),N=l("h3"),Z=l("a"),Bt=l("span"),u(Ie.$$.fragment),jo=f(),Kt=l("span"),Eo=i("Visualization"),ca=f(),rt=l("p"),xo=i("Let\u2019s create a helper function to display images in a grid."),da=f(),u(We.$$.fragment),wa=f(),u(Ne.$$.fragment),ga=f(),nt=l("p"),pt=l("img"),va=f(),L=l("h2"),ee=l("a"),Qt=l("span"),u(Le.$$.fragment),Po=f(),Zt=l("span"),To=i("Using different prompts"),_a=f(),te=l("p"),Ao=i("We don\u2019t have to replicate the "),es=l("em"),Co=i("same"),Do=i(" prompt in all the devices. We can do whatever we want: generate 2 prompts 4 times each, or even generate 8 different prompts at once. Let\u2019s do that!"),ba=f(),ft=l("p"),qo=i("First, we\u2019ll refactor the input preparation code into a handy function:"),$a=f(),u(He.$$.fragment),ya=f(),u(Je.$$.fragment),ka=f(),ht=l("p"),mt=l("img"),ja=f(),H=l("h2"),se=l("a"),ts=l("span"),u(Xe.$$.fragment),Oo=f(),ss=l("span"),So=i("How does parallelization work?"),Ea=f(),ae=l("p"),zo=i("We said before that the "),as=l("code"),Fo=i("diffusers"),Uo=i(" Flax pipeline automatically compiles the model and runs it in parallel on all available devices. We\u2019ll now briefly look inside that process to show how it works."),xa=f(),k=l("p"),Io=i("JAX parallelization can be done in multiple ways. The easiest one revolves around using the "),is=l("code"),Wo=i("jax.pmap"),No=i(" function to achieve single-program, multiple-data (SPMD) parallelization. It means we\u2019ll run several copies of the same code, each on different data inputs. More sophisticated approaches are possible, we invite you to go over the "),Me=l("a"),Lo=i("JAX documentation"),Ho=i(" and the "),ie=l("a"),os=l("code"),Jo=i("pjit"),Xo=i(" pages"),Mo=i(" to explore this topic if you are interested!"),Pa=f(),Ge=l("p"),ls=l("code"),Go=i("jax.pmap"),Ro=i(" does two things for us:"),Ta=f(),oe=l("ul"),P=l("li"),Yo=i("Compiles (or "),rs=l("code"),Vo=i("jit"),Bo=i("s) the code, as if we had invoked "),ns=l("code"),Ko=i("jax.jit()"),Qo=i(". This does not happen when we call "),ps=l("code"),Zo=i("pmap"),el=i(", but the first time the pmapped function is invoked."),tl=f(),fs=l("li"),sl=i("Ensures the compiled code runs in parallel in all the available devices."),Aa=f(),j=l("p"),al=i("To show how it works we "),hs=l("code"),il=i("pmap"),ol=i(" the "),ms=l("code"),ll=i("_generate"),rl=i(" method of the pipeline, which is the private method that runs generates images. Please, note that this method may be renamed or removed in future releases of "),us=l("code"),nl=i("diffusers"),pl=i("."),Ca=f(),u(Re.$$.fragment),Da=f(),q=l("p"),fl=i("After we use "),cs=l("code"),hl=i("pmap"),ml=i(", the prepared function "),ds=l("code"),ul=i("p_generate"),cl=i(" will conceptually do the following:"),qa=f(),le=l("ul"),Ye=l("li"),dl=i("Invoke a copy of the underlying function "),ws=l("code"),wl=i("pipeline._generate"),gl=i(" in each device."),vl=f(),$=l("li"),_l=i("Send each device a different portion of the input arguments. That\u2019s what sharding is used for. In our case, "),gs=l("code"),bl=i("prompt_ids"),$l=i(" has shape "),vs=l("code"),yl=i("(8, 1, 77, 768)"),kl=i(". This array will be split in "),_s=l("code"),jl=i("8"),El=i(" and each copy of "),bs=l("code"),xl=i("_generate"),Pl=i(" will receive an input with shape "),$s=l("code"),Tl=i("(1, 77, 768)"),Al=i("."),Oa=f(),O=l("p"),Cl=i("We can code "),ys=l("code"),Dl=i("_generate"),ql=i(" completely ignoring the fact that it will be invoked in parallel. We just care about our batch size ("),ks=l("code"),Ol=i("1"),Sl=i(" in this example) and the dimensions that make sense for our code, and don\u2019t have to change anything to make it work in parallel."),Sa=f(),ut=l("p"),zl=i("The same way as when we used the pipeline call, the first time we run the following cell it will take a while, but then it will be much faster."),za=f(),u(Ve.$$.fragment),Fa=f(),u(Be.$$.fragment),Ua=f(),u(Ke.$$.fragment),Ia=f(),u(Qe.$$.fragment),Wa=f(),re=l("p"),Fl=i("We use "),js=l("code"),Ul=i("block_until_ready()"),Il=i(" to correctly measure inference time, because JAX uses asynchronous dispatch and returns control to the Python loop as soon as it can. You don\u2019t need to use that in your code; blocking will occur automatically when you want to use the result of a computation that has not yet been materialized."),this.h()},l(e){const a=jn('[data-svelte="svelte-1phssyn"]',document.head);S=r(a,"META",{name:!0,content:!0}),a.forEach(t),Es=h(e),z=r(e,"H1",{class:!0});var La=n(z);J=r(La,"A",{id:!0,class:!0,href:!0});var Xl=n(J);bt=r(Xl,"SPAN",{});var Ml=n(bt);c(ue.$$.fragment,Ml),Ml.forEach(t),Xl.forEach(t),li=h(La),$t=r(La,"SPAN",{});var Gl=n($t);ri=o(Gl,"\u{1F9E8} Stable Diffusion in JAX / Flax !"),Gl.forEach(t),La.forEach(t),xs=h(e),c(ce.$$.fragment,e),Ps=h(e),T=r(e,"P",{});var ct=n(T);ni=o(ct,"\u{1F917} Hugging Face "),de=r(ct,"A",{href:!0,rel:!0});var Rl=n(de);pi=o(Rl,"Diffusers"),Rl.forEach(t),fi=o(ct," supports Flax since version "),yt=r(ct,"CODE",{});var Yl=n(yt);hi=o(Yl,"0.5.1"),Yl.forEach(t),mi=o(ct,"! This allows for super fast inference on Google TPUs, such as those available in Colab, Kaggle or Google Cloud Platform."),ct.forEach(t),Ts=h(e),X=r(e,"P",{});var Ha=n(X);ui=o(Ha,"This notebook shows how to run inference using JAX / Flax. If you want more details about how Stable Diffusion works or want to run it in GPU, please refer to "),we=r(Ha,"A",{href:!0,rel:!0});var Vl=n(we);ci=o(Vl,"this notebook"),Vl.forEach(t),di=o(Ha,"."),Ha.forEach(t),As=h(e),y=r(e,"P",{});var ne=n(y);wi=o(ne,"First, make sure you are using a TPU backend. If you are running this notebook in Colab, select "),kt=r(ne,"CODE",{});var Bl=n(kt);gi=o(Bl,"Runtime"),Bl.forEach(t),vi=o(ne," in the menu above, then select the option \u201CChange runtime type\u201D and then select "),jt=r(ne,"CODE",{});var Kl=n(jt);_i=o(Kl,"TPU"),Kl.forEach(t),bi=o(ne," under the "),Et=r(ne,"CODE",{});var Ql=n(Et);$i=o(Ql,"Hardware accelerator"),Ql.forEach(t),yi=o(ne," setting."),ne.forEach(t),Cs=h(e),Ze=r(e,"P",{});var Zl=n(Ze);ki=o(Zl,"Note that JAX is not exclusive to TPUs, but it shines on that hardware because each TPU server has 8 TPU accelerators working in parallel."),Zl.forEach(t),Ds=h(e),F=r(e,"H2",{class:!0});var Ja=n(F);M=r(Ja,"A",{id:!0,class:!0,href:!0});var er=n(M);xt=r(er,"SPAN",{});var tr=n(xt);c(ge.$$.fragment,tr),tr.forEach(t),er.forEach(t),ji=h(Ja),Pt=r(Ja,"SPAN",{});var sr=n(Pt);Ei=o(sr,"Setup"),sr.forEach(t),Ja.forEach(t),qs=h(e),et=r(e,"P",{});var ar=n(et);xi=o(ar,"First make sure diffusers is installed."),ar.forEach(t),Os=h(e),c(ve.$$.fragment,e),Ss=h(e),c(_e.$$.fragment,e),zs=h(e),c(be.$$.fragment,e),Fs=h(e),c($e.$$.fragment,e),Us=h(e),tt=r(e,"P",{});var ir=n(tt);Pi=o(ir,"Then we import all the dependencies."),ir.forEach(t),Is=h(e),c(ye.$$.fragment,e),Ws=h(e),U=r(e,"H2",{class:!0});var Xa=n(U);G=r(Xa,"A",{id:!0,class:!0,href:!0});var or=n(G);Tt=r(or,"SPAN",{});var lr=n(Tt);c(ke.$$.fragment,lr),lr.forEach(t),or.forEach(t),Ti=h(Xa),At=r(Xa,"SPAN",{});var rr=n(At);Ai=o(rr,"Model Loading"),rr.forEach(t),Xa.forEach(t),Ns=h(e),A=r(e,"P",{});var dt=n(A);Ci=o(dt,"TPU devices support "),Ct=r(dt,"CODE",{});var nr=n(Ct);Di=o(nr,"bfloat16"),nr.forEach(t),qi=o(dt,", an efficient half-float type. We\u2019ll use it for our tests, but you can also use "),Dt=r(dt,"CODE",{});var pr=n(Dt);Oi=o(pr,"float32"),pr.forEach(t),Si=o(dt," to use full precision instead."),dt.forEach(t),Ls=h(e),c(je.$$.fragment,e),Hs=h(e),R=r(e,"P",{});var Ma=n(R);zi=o(Ma,"Flax is a functional framework, so models are stateless and parameters are stored outside them. Loading the pre-trained Flax pipeline will return both the pipeline itself and the model weights (or parameters). We are using a "),qt=r(Ma,"CODE",{});var fr=n(qt);Fi=o(fr,"bf16"),fr.forEach(t),Ui=o(Ma," version of the weights, which leads to type warnings that you can safely ignore."),Ma.forEach(t),Js=h(e),c(Ee.$$.fragment,e),Xs=h(e),I=r(e,"H2",{class:!0});var Ga=n(I);Y=r(Ga,"A",{id:!0,class:!0,href:!0});var hr=n(Y);Ot=r(hr,"SPAN",{});var mr=n(Ot);c(xe.$$.fragment,mr),mr.forEach(t),hr.forEach(t),Ii=h(Ga),St=r(Ga,"SPAN",{});var ur=n(St);Wi=o(ur,"Inference"),ur.forEach(t),Ga.forEach(t),Ms=h(e),st=r(e,"P",{});var cr=n(st);Ni=o(cr,"Since TPUs usually have 8 devices working in parallel, we\u2019ll replicate our prompt as many times as devices we have. Then we\u2019ll perform inference on the 8 devices at once, each responsible for generating one image. Thus, we\u2019ll get 8 images in the same amount of time it takes for one chip to generate a single one."),cr.forEach(t),Gs=h(e),V=r(e,"P",{});var Ra=n(V);Li=o(Ra,"After replicating the prompt, we obtain the tokenized text ids by invoking the "),zt=r(Ra,"CODE",{});var dr=n(zt);Hi=o(dr,"prepare_inputs"),dr.forEach(t),Ji=o(Ra," function of the pipeline. The length of the tokenized text is set to 77 tokens, as required by the configuration of the underlying CLIP Text model."),Ra.forEach(t),Rs=h(e),c(Pe.$$.fragment,e),Ys=h(e),c(Te.$$.fragment,e),Vs=h(e),W=r(e,"H3",{class:!0});var Ya=n(W);B=r(Ya,"A",{id:!0,class:!0,href:!0});var wr=n(B);Ft=r(wr,"SPAN",{});var gr=n(Ft);c(Ae.$$.fragment,gr),gr.forEach(t),wr.forEach(t),Xi=h(Ya),Ut=r(Ya,"SPAN",{});var vr=n(Ut);Mi=o(vr,"Replication and parallelization"),vr.forEach(t),Ya.forEach(t),Bs=h(e),C=r(e,"P",{});var wt=n(C);Gi=o(wt,"Model parameters and inputs have to be replicated across the 8 parallel devices we have. The parameters dictionary is replicated using "),It=r(wt,"CODE",{});var _r=n(It);Ri=o(_r,"flax.jax_utils.replicate"),_r.forEach(t),Yi=o(wt,", which traverses the dictionary and changes the shape of the weights so they are repeated 8 times. Arrays are replicated using "),Wt=r(wt,"CODE",{});var br=n(Wt);Vi=o(br,"shard"),br.forEach(t),Bi=o(wt,"."),wt.forEach(t),Ks=h(e),c(Ce.$$.fragment,e),Qs=h(e),c(De.$$.fragment,e),Zs=h(e),c(qe.$$.fragment,e),ea=h(e),b=r(e,"P",{});var E=n(b);Ki=o(E,"That shape means that each one of the "),Nt=r(E,"CODE",{});var $r=n(Nt);Qi=o($r,"8"),$r.forEach(t),Zi=o(E," devices will receive as an input a "),Lt=r(E,"CODE",{});var yr=n(Lt);eo=o(yr,"jnp"),yr.forEach(t),to=o(E," array with shape "),Ht=r(E,"CODE",{});var kr=n(Ht);so=o(kr,"(1, 77)"),kr.forEach(t),ao=o(E,". "),Jt=r(E,"CODE",{});var jr=n(Jt);io=o(jr,"1"),jr.forEach(t),oo=o(E," is therefore the batch size per device. In TPUs with sufficient memory, it could be larger than "),Xt=r(E,"CODE",{});var Er=n(Xt);lo=o(Er,"1"),Er.forEach(t),ro=o(E," if we wanted to generate multiple images (per chip) at once."),E.forEach(t),ta=h(e),at=r(e,"P",{});var xr=n(at);no=o(xr,"We are almost ready to generate images! We just need to create a random number generator to pass to the generation function. This is the standard procedure in Flax, which is very serious and opinionated about random numbers \u2013 all functions that deal with random numbers are expected to receive a generator. This ensures reproducibility, even when we are training across multiple distributed devices."),xr.forEach(t),sa=h(e),it=r(e,"P",{});var Pr=n(it);po=o(Pr,"The helper function below uses a seed to initialize a random number generator. As long as we use the same seed, we\u2019ll get the exact same results. Feel free to use different seeds when exploring results later in the notebook."),Pr.forEach(t),aa=h(e),c(Oe.$$.fragment,e),ia=h(e),ot=r(e,"P",{});var Tr=n(ot);fo=o(Tr,"We obtain a rng and then \u201Csplit\u201D it 8 times so each device receives a different generator. Therefore, each device will create a different image, and the full process is reproducible."),Tr.forEach(t),oa=h(e),c(Se.$$.fragment,e),la=h(e),lt=r(e,"P",{});var Ar=n(lt);ho=o(Ar,"JAX code can be compiled to an efficient representation that runs very fast. However, we need to ensure that all inputs have the same shape in subsequent calls; otherwise, JAX will have to recompile the code, and we wouldn\u2019t be able to take advantage of the optimized speed."),Ar.forEach(t),ra=h(e),K=r(e,"P",{});var Va=n(K);mo=o(Va,"The Flax pipeline can compile the code for us if we pass "),Mt=r(Va,"CODE",{});var Cr=n(Mt);uo=o(Cr,"jit = True"),Cr.forEach(t),co=o(Va," as an argument. It will also ensure that the model runs in parallel in the 8 available devices."),Va.forEach(t),na=h(e),Q=r(e,"P",{});var Ba=n(Q);wo=o(Ba,"The first time we run the following cell it will take a long time to compile, but subequent calls (even with different inputs) will be much faster. For example, it took more than a minute to compile in a TPU v2-8 when I tested, but then it takes about "),Gt=r(Ba,"STRONG",{});var Dr=n(Gt);Rt=r(Dr,"CODE",{});var qr=n(Rt);go=o(qr,"7s"),qr.forEach(t),Dr.forEach(t),vo=o(Ba," for future inference runs."),Ba.forEach(t),pa=h(e),c(ze.$$.fragment,e),fa=h(e),c(Fe.$$.fragment,e),ha=h(e),D=r(e,"P",{});var gt=n(D);_o=o(gt,"The returned array has shape "),Yt=r(gt,"CODE",{});var Or=n(Yt);bo=o(Or,"(8, 1, 512, 512, 3)"),Or.forEach(t),$o=o(gt,". We reshape it to get rid of the second dimension and obtain 8 images of "),Vt=r(gt,"CODE",{});var Sr=n(Vt);yo=o(Sr,"512 \xD7 512 \xD7 3"),Sr.forEach(t),ko=o(gt," and then convert them to PIL."),gt.forEach(t),ma=h(e),c(Ue.$$.fragment,e),ua=h(e),N=r(e,"H3",{class:!0});var Ka=n(N);Z=r(Ka,"A",{id:!0,class:!0,href:!0});var zr=n(Z);Bt=r(zr,"SPAN",{});var Fr=n(Bt);c(Ie.$$.fragment,Fr),Fr.forEach(t),zr.forEach(t),jo=h(Ka),Kt=r(Ka,"SPAN",{});var Ur=n(Kt);Eo=o(Ur,"Visualization"),Ur.forEach(t),Ka.forEach(t),ca=h(e),rt=r(e,"P",{});var Ir=n(rt);xo=o(Ir,"Let\u2019s create a helper function to display images in a grid."),Ir.forEach(t),da=h(e),c(We.$$.fragment,e),wa=h(e),c(Ne.$$.fragment,e),ga=h(e),nt=r(e,"P",{});var Wr=n(nt);pt=r(Wr,"IMG",{src:!0,alt:!0}),Wr.forEach(t),va=h(e),L=r(e,"H2",{class:!0});var Qa=n(L);ee=r(Qa,"A",{id:!0,class:!0,href:!0});var Nr=n(ee);Qt=r(Nr,"SPAN",{});var Lr=n(Qt);c(Le.$$.fragment,Lr),Lr.forEach(t),Nr.forEach(t),Po=h(Qa),Zt=r(Qa,"SPAN",{});var Hr=n(Zt);To=o(Hr,"Using different prompts"),Hr.forEach(t),Qa.forEach(t),_a=h(e),te=r(e,"P",{});var Za=n(te);Ao=o(Za,"We don\u2019t have to replicate the "),es=r(Za,"EM",{});var Jr=n(es);Co=o(Jr,"same"),Jr.forEach(t),Do=o(Za," prompt in all the devices. We can do whatever we want: generate 2 prompts 4 times each, or even generate 8 different prompts at once. Let\u2019s do that!"),Za.forEach(t),ba=h(e),ft=r(e,"P",{});var Xr=n(ft);qo=o(Xr,"First, we\u2019ll refactor the input preparation code into a handy function:"),Xr.forEach(t),$a=h(e),c(He.$$.fragment,e),ya=h(e),c(Je.$$.fragment,e),ka=h(e),ht=r(e,"P",{});var Mr=n(ht);mt=r(Mr,"IMG",{src:!0,alt:!0}),Mr.forEach(t),ja=h(e),H=r(e,"H2",{class:!0});var ei=n(H);se=r(ei,"A",{id:!0,class:!0,href:!0});var Gr=n(se);ts=r(Gr,"SPAN",{});var Rr=n(ts);c(Xe.$$.fragment,Rr),Rr.forEach(t),Gr.forEach(t),Oo=h(ei),ss=r(ei,"SPAN",{});var Yr=n(ss);So=o(Yr,"How does parallelization work?"),Yr.forEach(t),ei.forEach(t),Ea=h(e),ae=r(e,"P",{});var ti=n(ae);zo=o(ti,"We said before that the "),as=r(ti,"CODE",{});var Vr=n(as);Fo=o(Vr,"diffusers"),Vr.forEach(t),Uo=o(ti," Flax pipeline automatically compiles the model and runs it in parallel on all available devices. We\u2019ll now briefly look inside that process to show how it works."),ti.forEach(t),xa=h(e),k=r(e,"P",{});var pe=n(k);Io=o(pe,"JAX parallelization can be done in multiple ways. The easiest one revolves around using the "),is=r(pe,"CODE",{});var Br=n(is);Wo=o(Br,"jax.pmap"),Br.forEach(t),No=o(pe," function to achieve single-program, multiple-data (SPMD) parallelization. It means we\u2019ll run several copies of the same code, each on different data inputs. More sophisticated approaches are possible, we invite you to go over the "),Me=r(pe,"A",{href:!0,rel:!0});var Kr=n(Me);Lo=o(Kr,"JAX documentation"),Kr.forEach(t),Ho=o(pe," and the "),ie=r(pe,"A",{href:!0,rel:!0});var Wl=n(ie);os=r(Wl,"CODE",{});var Qr=n(os);Jo=o(Qr,"pjit"),Qr.forEach(t),Xo=o(Wl," pages"),Wl.forEach(t),Mo=o(pe," to explore this topic if you are interested!"),pe.forEach(t),Pa=h(e),Ge=r(e,"P",{});var Nl=n(Ge);ls=r(Nl,"CODE",{});var Zr=n(ls);Go=o(Zr,"jax.pmap"),Zr.forEach(t),Ro=o(Nl," does two things for us:"),Nl.forEach(t),Ta=h(e),oe=r(e,"UL",{});var si=n(oe);P=r(si,"LI",{});var fe=n(P);Yo=o(fe,"Compiles (or "),rs=r(fe,"CODE",{});var en=n(rs);Vo=o(en,"jit"),en.forEach(t),Bo=o(fe,"s) the code, as if we had invoked "),ns=r(fe,"CODE",{});var tn=n(ns);Ko=o(tn,"jax.jit()"),tn.forEach(t),Qo=o(fe,". This does not happen when we call "),ps=r(fe,"CODE",{});var sn=n(ps);Zo=o(sn,"pmap"),sn.forEach(t),el=o(fe,", but the first time the pmapped function is invoked."),fe.forEach(t),tl=h(si),fs=r(si,"LI",{});var an=n(fs);sl=o(an,"Ensures the compiled code runs in parallel in all the available devices."),an.forEach(t),si.forEach(t),Aa=h(e),j=r(e,"P",{});var he=n(j);al=o(he,"To show how it works we "),hs=r(he,"CODE",{});var on=n(hs);il=o(on,"pmap"),on.forEach(t),ol=o(he," the "),ms=r(he,"CODE",{});var ln=n(ms);ll=o(ln,"_generate"),ln.forEach(t),rl=o(he," method of the pipeline, which is the private method that runs generates images. Please, note that this method may be renamed or removed in future releases of "),us=r(he,"CODE",{});var rn=n(us);nl=o(rn,"diffusers"),rn.forEach(t),pl=o(he,"."),he.forEach(t),Ca=h(e),c(Re.$$.fragment,e),Da=h(e),q=r(e,"P",{});var vt=n(q);fl=o(vt,"After we use "),cs=r(vt,"CODE",{});var nn=n(cs);hl=o(nn,"pmap"),nn.forEach(t),ml=o(vt,", the prepared function "),ds=r(vt,"CODE",{});var pn=n(ds);ul=o(pn,"p_generate"),pn.forEach(t),cl=o(vt," will conceptually do the following:"),vt.forEach(t),qa=h(e),le=r(e,"UL",{});var ai=n(le);Ye=r(ai,"LI",{});var ii=n(Ye);dl=o(ii,"Invoke a copy of the underlying function "),ws=r(ii,"CODE",{});var fn=n(ws);wl=o(fn,"pipeline._generate"),fn.forEach(t),gl=o(ii," in each device."),ii.forEach(t),vl=h(ai),$=r(ai,"LI",{});var x=n($);_l=o(x,"Send each device a different portion of the input arguments. That\u2019s what sharding is used for. In our case, "),gs=r(x,"CODE",{});var hn=n(gs);bl=o(hn,"prompt_ids"),hn.forEach(t),$l=o(x," has shape "),vs=r(x,"CODE",{});var mn=n(vs);yl=o(mn,"(8, 1, 77, 768)"),mn.forEach(t),kl=o(x,". This array will be split in "),_s=r(x,"CODE",{});var un=n(_s);jl=o(un,"8"),un.forEach(t),El=o(x," and each copy of "),bs=r(x,"CODE",{});var cn=n(bs);xl=o(cn,"_generate"),cn.forEach(t),Pl=o(x," will receive an input with shape "),$s=r(x,"CODE",{});var dn=n($s);Tl=o(dn,"(1, 77, 768)"),dn.forEach(t),Al=o(x,"."),x.forEach(t),ai.forEach(t),Oa=h(e),O=r(e,"P",{});var _t=n(O);Cl=o(_t,"We can code "),ys=r(_t,"CODE",{});var wn=n(ys);Dl=o(wn,"_generate"),wn.forEach(t),ql=o(_t," completely ignoring the fact that it will be invoked in parallel. We just care about our batch size ("),ks=r(_t,"CODE",{});var gn=n(ks);Ol=o(gn,"1"),gn.forEach(t),Sl=o(_t," in this example) and the dimensions that make sense for our code, and don\u2019t have to change anything to make it work in parallel."),_t.forEach(t),Sa=h(e),ut=r(e,"P",{});var vn=n(ut);zl=o(vn,"The same way as when we used the pipeline call, the first time we run the following cell it will take a while, but then it will be much faster."),vn.forEach(t),za=h(e),c(Ve.$$.fragment,e),Fa=h(e),c(Be.$$.fragment,e),Ua=h(e),c(Ke.$$.fragment,e),Ia=h(e),c(Qe.$$.fragment,e),Wa=h(e),re=r(e,"P",{});var oi=n(re);Fl=o(oi,"We use "),js=r(oi,"CODE",{});var _n=n(js);Ul=o(_n,"block_until_ready()"),_n.forEach(t),Il=o(oi," to correctly measure inference time, because JAX uses asynchronous dispatch and returns control to the Python loop as soon as it can. You don\u2019t need to use that in your code; blocking will occur automatically when you want to use the result of a computation that has not yet been materialized."),oi.forEach(t),this.h()},h(){m(S,"name","hf:doc:metadata"),m(S,"content",JSON.stringify(An)),m(J,"id","stable-diffusion-in-jax-flax"),m(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"),m(J,"href","#stable-diffusion-in-jax-flax"),m(z,"class","relative group"),m(de,"href","https://github.com/huggingface/diffusers"),m(de,"rel","nofollow"),m(we,"href","https://huggingface.co/docs/diffusers/stable_diffusion"),m(we,"rel","nofollow"),m(M,"id","setup"),m(M,"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"),m(M,"href","#setup"),m(F,"class","relative group"),m(G,"id","model-loading"),m(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"),m(G,"href","#model-loading"),m(U,"class","relative group"),m(Y,"id","inference"),m(Y,"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"),m(Y,"href","#inference"),m(I,"class","relative group"),m(B,"id","replication-and-parallelization"),m(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"),m(B,"href","#replication-and-parallelization"),m(W,"class","relative group"),m(Z,"id","visualization"),m(Z,"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"),m(Z,"href","#visualization"),m(N,"class","relative group"),bn(pt.src,Hl="https://huggingface.co/datasets/YiYiXu/test-doc-assets/resolve/main/stable_diffusion_jax_how_to_cell_38_output_0.jpeg")||m(pt,"src",Hl),m(pt,"alt","img"),m(ee,"id","using-different-prompts"),m(ee,"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"),m(ee,"href","#using-different-prompts"),m(L,"class","relative group"),bn(mt.src,Jl="https://huggingface.co/datasets/YiYiXu/test-doc-assets/resolve/main/stable_diffusion_jax_how_to_cell_43_output_0.jpeg")||m(mt,"src",Jl),m(mt,"alt","img"),m(se,"id","how-does-parallelization-work"),m(se,"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"),m(se,"href","#how-does-parallelization-work"),m(H,"class","relative group"),m(Me,"href","https://jax.readthedocs.io/en/latest/index.html"),m(Me,"rel","nofollow"),m(ie,"href","https://jax.readthedocs.io/en/latest/jax-101/08-pjit.html?highlight=pjit"),m(ie,"rel","nofollow")},m(e,a){s(document.head,S),p(e,Es,a),p(e,z,a),s(z,J),s(J,bt),d(ue,bt,null),s(z,li),s(z,$t),s($t,ri),p(e,xs,a),d(ce,e,a),p(e,Ps,a),p(e,T,a),s(T,ni),s(T,de),s(de,pi),s(T,fi),s(T,yt),s(yt,hi),s(T,mi),p(e,Ts,a),p(e,X,a),s(X,ui),s(X,we),s(we,ci),s(X,di),p(e,As,a),p(e,y,a),s(y,wi),s(y,kt),s(kt,gi),s(y,vi),s(y,jt),s(jt,_i),s(y,bi),s(y,Et),s(Et,$i),s(y,yi),p(e,Cs,a),p(e,Ze,a),s(Ze,ki),p(e,Ds,a),p(e,F,a),s(F,M),s(M,xt),d(ge,xt,null),s(F,ji),s(F,Pt),s(Pt,Ei),p(e,qs,a),p(e,et,a),s(et,xi),p(e,Os,a),d(ve,e,a),p(e,Ss,a),d(_e,e,a),p(e,zs,a),d(be,e,a),p(e,Fs,a),d($e,e,a),p(e,Us,a),p(e,tt,a),s(tt,Pi),p(e,Is,a),d(ye,e,a),p(e,Ws,a),p(e,U,a),s(U,G),s(G,Tt),d(ke,Tt,null),s(U,Ti),s(U,At),s(At,Ai),p(e,Ns,a),p(e,A,a),s(A,Ci),s(A,Ct),s(Ct,Di),s(A,qi),s(A,Dt),s(Dt,Oi),s(A,Si),p(e,Ls,a),d(je,e,a),p(e,Hs,a),p(e,R,a),s(R,zi),s(R,qt),s(qt,Fi),s(R,Ui),p(e,Js,a),d(Ee,e,a),p(e,Xs,a),p(e,I,a),s(I,Y),s(Y,Ot),d(xe,Ot,null),s(I,Ii),s(I,St),s(St,Wi),p(e,Ms,a),p(e,st,a),s(st,Ni),p(e,Gs,a),p(e,V,a),s(V,Li),s(V,zt),s(zt,Hi),s(V,Ji),p(e,Rs,a),d(Pe,e,a),p(e,Ys,a),d(Te,e,a),p(e,Vs,a),p(e,W,a),s(W,B),s(B,Ft),d(Ae,Ft,null),s(W,Xi),s(W,Ut),s(Ut,Mi),p(e,Bs,a),p(e,C,a),s(C,Gi),s(C,It),s(It,Ri),s(C,Yi),s(C,Wt),s(Wt,Vi),s(C,Bi),p(e,Ks,a),d(Ce,e,a),p(e,Qs,a),d(De,e,a),p(e,Zs,a),d(qe,e,a),p(e,ea,a),p(e,b,a),s(b,Ki),s(b,Nt),s(Nt,Qi),s(b,Zi),s(b,Lt),s(Lt,eo),s(b,to),s(b,Ht),s(Ht,so),s(b,ao),s(b,Jt),s(Jt,io),s(b,oo),s(b,Xt),s(Xt,lo),s(b,ro),p(e,ta,a),p(e,at,a),s(at,no),p(e,sa,a),p(e,it,a),s(it,po),p(e,aa,a),d(Oe,e,a),p(e,ia,a),p(e,ot,a),s(ot,fo),p(e,oa,a),d(Se,e,a),p(e,la,a),p(e,lt,a),s(lt,ho),p(e,ra,a),p(e,K,a),s(K,mo),s(K,Mt),s(Mt,uo),s(K,co),p(e,na,a),p(e,Q,a),s(Q,wo),s(Q,Gt),s(Gt,Rt),s(Rt,go),s(Q,vo),p(e,pa,a),d(ze,e,a),p(e,fa,a),d(Fe,e,a),p(e,ha,a),p(e,D,a),s(D,_o),s(D,Yt),s(Yt,bo),s(D,$o),s(D,Vt),s(Vt,yo),s(D,ko),p(e,ma,a),d(Ue,e,a),p(e,ua,a),p(e,N,a),s(N,Z),s(Z,Bt),d(Ie,Bt,null),s(N,jo),s(N,Kt),s(Kt,Eo),p(e,ca,a),p(e,rt,a),s(rt,xo),p(e,da,a),d(We,e,a),p(e,wa,a),d(Ne,e,a),p(e,ga,a),p(e,nt,a),s(nt,pt),p(e,va,a),p(e,L,a),s(L,ee),s(ee,Qt),d(Le,Qt,null),s(L,Po),s(L,Zt),s(Zt,To),p(e,_a,a),p(e,te,a),s(te,Ao),s(te,es),s(es,Co),s(te,Do),p(e,ba,a),p(e,ft,a),s(ft,qo),p(e,$a,a),d(He,e,a),p(e,ya,a),d(Je,e,a),p(e,ka,a),p(e,ht,a),s(ht,mt),p(e,ja,a),p(e,H,a),s(H,se),s(se,ts),d(Xe,ts,null),s(H,Oo),s(H,ss),s(ss,So),p(e,Ea,a),p(e,ae,a),s(ae,zo),s(ae,as),s(as,Fo),s(ae,Uo),p(e,xa,a),p(e,k,a),s(k,Io),s(k,is),s(is,Wo),s(k,No),s(k,Me),s(Me,Lo),s(k,Ho),s(k,ie),s(ie,os),s(os,Jo),s(ie,Xo),s(k,Mo),p(e,Pa,a),p(e,Ge,a),s(Ge,ls),s(ls,Go),s(Ge,Ro),p(e,Ta,a),p(e,oe,a),s(oe,P),s(P,Yo),s(P,rs),s(rs,Vo),s(P,Bo),s(P,ns),s(ns,Ko),s(P,Qo),s(P,ps),s(ps,Zo),s(P,el),s(oe,tl),s(oe,fs),s(fs,sl),p(e,Aa,a),p(e,j,a),s(j,al),s(j,hs),s(hs,il),s(j,ol),s(j,ms),s(ms,ll),s(j,rl),s(j,us),s(us,nl),s(j,pl),p(e,Ca,a),d(Re,e,a),p(e,Da,a),p(e,q,a),s(q,fl),s(q,cs),s(cs,hl),s(q,ml),s(q,ds),s(ds,ul),s(q,cl),p(e,qa,a),p(e,le,a),s(le,Ye),s(Ye,dl),s(Ye,ws),s(ws,wl),s(Ye,gl),s(le,vl),s(le,$),s($,_l),s($,gs),s(gs,bl),s($,$l),s($,vs),s(vs,yl),s($,kl),s($,_s),s(_s,jl),s($,El),s($,bs),s(bs,xl),s($,Pl),s($,$s),s($s,Tl),s($,Al),p(e,Oa,a),p(e,O,a),s(O,Cl),s(O,ys),s(ys,Dl),s(O,ql),s(O,ks),s(ks,Ol),s(O,Sl),p(e,Sa,a),p(e,ut,a),s(ut,zl),p(e,za,a),d(Ve,e,a),p(e,Fa,a),d(Be,e,a),p(e,Ua,a),d(Ke,e,a),p(e,Ia,a),d(Qe,e,a),p(e,Wa,a),p(e,re,a),s(re,Fl),s(re,js),s(js,Ul),s(re,Il),Na=!0},p:En,i(e){Na||(w(ue.$$.fragment,e),w(ce.$$.fragment,e),w(ge.$$.fragment,e),w(ve.$$.fragment,e),w(_e.$$.fragment,e),w(be.$$.fragment,e),w($e.$$.fragment,e),w(ye.$$.fragment,e),w(ke.$$.fragment,e),w(je.$$.fragment,e),w(Ee.$$.fragment,e),w(xe.$$.fragment,e),w(Pe.$$.fragment,e),w(Te.$$.fragment,e),w(Ae.$$.fragment,e),w(Ce.$$.fragment,e),w(De.$$.fragment,e),w(qe.$$.fragment,e),w(Oe.$$.fragment,e),w(Se.$$.fragment,e),w(ze.$$.fragment,e),w(Fe.$$.fragment,e),w(Ue.$$.fragment,e),w(Ie.$$.fragment,e),w(We.$$.fragment,e),w(Ne.$$.fragment,e),w(Le.$$.fragment,e),w(He.$$.fragment,e),w(Je.$$.fragment,e),w(Xe.$$.fragment,e),w(Re.$$.fragment,e),w(Ve.$$.fragment,e),w(Be.$$.fragment,e),w(Ke.$$.fragment,e),w(Qe.$$.fragment,e),Na=!0)},o(e){g(ue.$$.fragment,e),g(ce.$$.fragment,e),g(ge.$$.fragment,e),g(ve.$$.fragment,e),g(_e.$$.fragment,e),g(be.$$.fragment,e),g($e.$$.fragment,e),g(ye.$$.fragment,e),g(ke.$$.fragment,e),g(je.$$.fragment,e),g(Ee.$$.fragment,e),g(xe.$$.fragment,e),g(Pe.$$.fragment,e),g(Te.$$.fragment,e),g(Ae.$$.fragment,e),g(Ce.$$.fragment,e),g(De.$$.fragment,e),g(qe.$$.fragment,e),g(Oe.$$.fragment,e),g(Se.$$.fragment,e),g(ze.$$.fragment,e),g(Fe.$$.fragment,e),g(Ue.$$.fragment,e),g(Ie.$$.fragment,e),g(We.$$.fragment,e),g(Ne.$$.fragment,e),g(Le.$$.fragment,e),g(He.$$.fragment,e),g(Je.$$.fragment,e),g(Xe.$$.fragment,e),g(Re.$$.fragment,e),g(Ve.$$.fragment,e),g(Be.$$.fragment,e),g(Ke.$$.fragment,e),g(Qe.$$.fragment,e),Na=!1},d(e){t(S),e&&t(Es),e&&t(z),v(ue),e&&t(xs),v(ce,e),e&&t(Ps),e&&t(T),e&&t(Ts),e&&t(X),e&&t(As),e&&t(y),e&&t(Cs),e&&t(Ze),e&&t(Ds),e&&t(F),v(ge),e&&t(qs),e&&t(et),e&&t(Os),v(ve,e),e&&t(Ss),v(_e,e),e&&t(zs),v(be,e),e&&t(Fs),v($e,e),e&&t(Us),e&&t(tt),e&&t(Is),v(ye,e),e&&t(Ws),e&&t(U),v(ke),e&&t(Ns),e&&t(A),e&&t(Ls),v(je,e),e&&t(Hs),e&&t(R),e&&t(Js),v(Ee,e),e&&t(Xs),e&&t(I),v(xe),e&&t(Ms),e&&t(st),e&&t(Gs),e&&t(V),e&&t(Rs),v(Pe,e),e&&t(Ys),v(Te,e),e&&t(Vs),e&&t(W),v(Ae),e&&t(Bs),e&&t(C),e&&t(Ks),v(Ce,e),e&&t(Qs),v(De,e),e&&t(Zs),v(qe,e),e&&t(ea),e&&t(b),e&&t(ta),e&&t(at),e&&t(sa),e&&t(it),e&&t(aa),v(Oe,e),e&&t(ia),e&&t(ot),e&&t(oa),v(Se,e),e&&t(la),e&&t(lt),e&&t(ra),e&&t(K),e&&t(na),e&&t(Q),e&&t(pa),v(ze,e),e&&t(fa),v(Fe,e),e&&t(ha),e&&t(D),e&&t(ma),v(Ue,e),e&&t(ua),e&&t(N),v(Ie),e&&t(ca),e&&t(rt),e&&t(da),v(We,e),e&&t(wa),v(Ne,e),e&&t(ga),e&&t(nt),e&&t(va),e&&t(L),v(Le),e&&t(_a),e&&t(te),e&&t(ba),e&&t(ft),e&&t($a),v(He,e),e&&t(ya),v(Je,e),e&&t(ka),e&&t(ht),e&&t(ja),e&&t(H),v(Xe),e&&t(Ea),e&&t(ae),e&&t(xa),e&&t(k),e&&t(Pa),e&&t(Ge),e&&t(Ta),e&&t(oe),e&&t(Aa),e&&t(j),e&&t(Ca),v(Re,e),e&&t(Da),e&&t(q),e&&t(qa),e&&t(le),e&&t(Oa),e&&t(O),e&&t(Sa),e&&t(ut),e&&t(za),v(Ve,e),e&&t(Fa),v(Be,e),e&&t(Ua),v(Ke,e),e&&t(Ia),v(Qe,e),e&&t(Wa),e&&t(re)}}}const An={local:"stable-diffusion-in-jax-flax",sections:[{local:"setup",title:"Setup"},{local:"model-loading",title:"Model Loading"},{local:"inference",sections:[{local:"replication-and-parallelization",title:"Replication and parallelization"},{local:"visualization",title:"Visualization"}],title:"Inference"},{local:"using-different-prompts",title:"Using different prompts"},{local:"how-does-parallelization-work",title:"How does parallelization work?"}],title:"\u{1F9E8} Stable Diffusion in JAX / Flax !"};function Cn(Ll){return xn(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class zn extends $n{constructor(S){super();yn(this,S,Cn,Tn,kn,{})}}export{zn as default,An as metadata};
