import{S as Kn,i as Jn,s as Qn,e as t,k as d,w as z,t as a,M as Xn,c as i,d as s,m as f,a as o,x as W,h as r,b as c,G as n,g as p,y as F,L as Yn,q as B,o as V,B as H,v as Zn}from"../../../chunks/vendor-hf-doc-builder.js";import{D as Cn}from"../../../chunks/Docstring-hf-doc-builder.js";import{C as es}from"../../../chunks/CodeBlock-hf-doc-builder.js";import{I as Pn}from"../../../chunks/IconCopyLink-hf-doc-builder.js";function ns(xn){let h,de,g,v,Z,k,Me,ee,Le,fe,_,b,ne,I,Ge,se,Oe,ue,w,Ne,$,Ue,ze,me,R,We,he,K,te,Fe,ge,E,ie,Be,Ve,_e,D,q,He,J,Re,Ke,Je,j,Qe,Q,Xe,Ye,ye,S,oe,Ze,en,ve,T,nn,X,sn,be,A,we,y,C,ae,M,tn,re,on,De,u,L,an,le,rn,ln,G,cn,Y,pn,dn,fn,P,O,un,ce,mn,hn,x,N,gn,U,_n,pe,yn,vn,Ce;return k=new Pn({}),I=new Pn({}),A=new es({props:{code:`import requests
import torch
from PIL import Image
from io import BytesIO

from diffusers import CycleDiffusionPipeline, DDIMScheduler

# load the pipeline
# make sure you're logged in with \`huggingface-cli login\`
model_id_or_path = "CompVis/stable-diffusion-v1-4"
scheduler = DDIMScheduler.from_pretrained(model_id_or_path, subfolder="scheduler")
pipe = CycleDiffusionPipeline.from_pretrained(model_id_or_path, scheduler=scheduler).to("cuda")

# let's download an initial image
url = "https://raw.githubusercontent.com/ChenWu98/cycle-diffusion/main/data/dalle2/An%20astronaut%20riding%20a%20horse.png"
response = requests.get(url)
init_image = Image.open(BytesIO(response.content)).convert("RGB")
init_image = init_image.resize((512, 512))
init_image.save("horse.png")

# let's specify a prompt
source_prompt = "An astronaut riding a horse"
prompt = "An astronaut riding an elephant"

# call the pipeline
image = pipe(
    prompt=prompt,
    source_prompt=source_prompt,
    image=init_image,
    num_inference_steps=100,
    eta=0.1,
    strength=0.8,
    guidance_scale=2,
    source_guidance_scale=1,
).images[0]

image.save("horse_to_elephant.png")

# let's try another example
# See more samples at the original repo: https://github.com/ChenWu98/cycle-diffusion
url = "https://raw.githubusercontent.com/ChenWu98/cycle-diffusion/main/data/dalle2/A%20black%20colored%20car.png"
response = requests.get(url)
init_image = Image.open(BytesIO(response.content)).convert("RGB")
init_image = init_image.resize((512, 512))
init_image.save("black.png")

source_prompt = "A black colored car"
prompt = "A blue colored car"

# call the pipeline
torch.manual_seed(0)
image = pipe(
    prompt=prompt,
    source_prompt=source_prompt,
    image=init_image,
    num_inference_steps=100,
    eta=0.1,
    strength=0.85,
    guidance_scale=3,
    source_guidance_scale=1,
).images[0]

image.save("black_to_blue.png")`,highlighted:`<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
<span class="hljs-keyword">from</span> io <span class="hljs-keyword">import</span> BytesIO

<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> CycleDiffusionPipeline, DDIMScheduler

<span class="hljs-comment"># load the pipeline</span>
<span class="hljs-comment"># make sure you&#x27;re logged in with \`huggingface-cli login\`</span>
model_id_or_path = <span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>
scheduler = DDIMScheduler.from_pretrained(model_id_or_path, subfolder=<span class="hljs-string">&quot;scheduler&quot;</span>)
pipe = CycleDiffusionPipeline.from_pretrained(model_id_or_path, scheduler=scheduler).to(<span class="hljs-string">&quot;cuda&quot;</span>)

<span class="hljs-comment"># let&#x27;s download an initial image</span>
url = <span class="hljs-string">&quot;https://raw.githubusercontent.com/ChenWu98/cycle-diffusion/main/data/dalle2/An%20astronaut%20riding%20a%20horse.png&quot;</span>
response = requests.get(url)
init_image = Image.<span class="hljs-built_in">open</span>(BytesIO(response.content)).convert(<span class="hljs-string">&quot;RGB&quot;</span>)
init_image = init_image.resize((<span class="hljs-number">512</span>, <span class="hljs-number">512</span>))
init_image.save(<span class="hljs-string">&quot;horse.png&quot;</span>)

<span class="hljs-comment"># let&#x27;s specify a prompt</span>
source_prompt = <span class="hljs-string">&quot;An astronaut riding a horse&quot;</span>
prompt = <span class="hljs-string">&quot;An astronaut riding an elephant&quot;</span>

<span class="hljs-comment"># call the pipeline</span>
image = pipe(
    prompt=prompt,
    source_prompt=source_prompt,
    image=init_image,
    num_inference_steps=<span class="hljs-number">100</span>,
    eta=<span class="hljs-number">0.1</span>,
    strength=<span class="hljs-number">0.8</span>,
    guidance_scale=<span class="hljs-number">2</span>,
    source_guidance_scale=<span class="hljs-number">1</span>,
).images[<span class="hljs-number">0</span>]

image.save(<span class="hljs-string">&quot;horse_to_elephant.png&quot;</span>)

<span class="hljs-comment"># let&#x27;s try another example</span>
<span class="hljs-comment"># See more samples at the original repo: https://github.com/ChenWu98/cycle-diffusion</span>
url = <span class="hljs-string">&quot;https://raw.githubusercontent.com/ChenWu98/cycle-diffusion/main/data/dalle2/A%20black%20colored%20car.png&quot;</span>
response = requests.get(url)
init_image = Image.<span class="hljs-built_in">open</span>(BytesIO(response.content)).convert(<span class="hljs-string">&quot;RGB&quot;</span>)
init_image = init_image.resize((<span class="hljs-number">512</span>, <span class="hljs-number">512</span>))
init_image.save(<span class="hljs-string">&quot;black.png&quot;</span>)

source_prompt = <span class="hljs-string">&quot;A black colored car&quot;</span>
prompt = <span class="hljs-string">&quot;A blue colored car&quot;</span>

<span class="hljs-comment"># call the pipeline</span>
torch.manual_seed(<span class="hljs-number">0</span>)
image = pipe(
    prompt=prompt,
    source_prompt=source_prompt,
    image=init_image,
    num_inference_steps=<span class="hljs-number">100</span>,
    eta=<span class="hljs-number">0.1</span>,
    strength=<span class="hljs-number">0.85</span>,
    guidance_scale=<span class="hljs-number">3</span>,
    source_guidance_scale=<span class="hljs-number">1</span>,
).images[<span class="hljs-number">0</span>]

image.save(<span class="hljs-string">&quot;black_to_blue.png&quot;</span>)`}}),M=new Pn({}),L=new Cn({props:{name:"class diffusers.CycleDiffusionPipeline",anchor:"diffusers.CycleDiffusionPipeline",parameters:[{name:"vae",val:": AutoencoderKL"},{name:"text_encoder",val:": CLIPTextModel"},{name:"tokenizer",val:": CLIPTokenizer"},{name:"unet",val:": UNet2DConditionModel"},{name:"scheduler",val:": DDIMScheduler"},{name:"safety_checker",val:": StableDiffusionSafetyChecker"},{name:"feature_extractor",val:": CLIPFeatureExtractor"},{name:"requires_safety_checker",val:": bool = True"}],parametersDescription:[{anchor:"diffusers.CycleDiffusionPipeline.vae",description:`<strong>vae</strong> (<a href="/docs/diffusers/main/en/api/models#diffusers.AutoencoderKL">AutoencoderKL</a>) &#x2014;
Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.`,name:"vae"},{anchor:"diffusers.CycleDiffusionPipeline.text_encoder",description:`<strong>text_encoder</strong> (<code>CLIPTextModel</code>) &#x2014;
Frozen text-encoder. Stable Diffusion uses the text portion of
<a href="https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel" rel="nofollow">CLIP</a>, specifically
the <a href="https://huggingface.co/openai/clip-vit-large-patch14" rel="nofollow">clip-vit-large-patch14</a> variant.`,name:"text_encoder"},{anchor:"diffusers.CycleDiffusionPipeline.tokenizer",description:`<strong>tokenizer</strong> (<code>CLIPTokenizer</code>) &#x2014;
Tokenizer of class
<a href="https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer" rel="nofollow">CLIPTokenizer</a>.`,name:"tokenizer"},{anchor:"diffusers.CycleDiffusionPipeline.unet",description:'<strong>unet</strong> (<a href="/docs/diffusers/main/en/api/models#diffusers.UNet2DConditionModel">UNet2DConditionModel</a>) &#x2014; Conditional U-Net architecture to denoise the encoded image latents.',name:"unet"},{anchor:"diffusers.CycleDiffusionPipeline.scheduler",description:`<strong>scheduler</strong> (<a href="/docs/diffusers/main/en/api/schedulers/overview#diffusers.SchedulerMixin">SchedulerMixin</a>) &#x2014;
A scheduler to be used in combination with <code>unet</code> to denoise the encoded image latents. Can be one of
<a href="/docs/diffusers/main/en/api/schedulers/ddim#diffusers.DDIMScheduler">DDIMScheduler</a>, <a href="/docs/diffusers/main/en/api/schedulers/lms_discrete#diffusers.LMSDiscreteScheduler">LMSDiscreteScheduler</a>, or <a href="/docs/diffusers/main/en/api/schedulers/pndm#diffusers.PNDMScheduler">PNDMScheduler</a>.`,name:"scheduler"},{anchor:"diffusers.CycleDiffusionPipeline.safety_checker",description:`<strong>safety_checker</strong> (<code>StableDiffusionSafetyChecker</code>) &#x2014;
Classification module that estimates whether generated images could be considered offensive or harmful.
Please, refer to the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" rel="nofollow">model card</a> for details.`,name:"safety_checker"},{anchor:"diffusers.CycleDiffusionPipeline.feature_extractor",description:`<strong>feature_extractor</strong> (<code>CLIPFeatureExtractor</code>) &#x2014;
Model that extracts features from generated images to be used as inputs for the <code>safety_checker</code>.`,name:"feature_extractor"}],source:"https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_cycle_diffusion.py#L121"}}),O=new Cn({props:{name:"__call__",anchor:"diffusers.CycleDiffusionPipeline.__call__",parameters:[{name:"prompt",val:": typing.Union[str, typing.List[str]]"},{name:"source_prompt",val:": typing.Union[str, typing.List[str]]"},{name:"image",val:": typing.Union[torch.FloatTensor, PIL.Image.Image] = None"},{name:"strength",val:": float = 0.8"},{name:"num_inference_steps",val:": typing.Optional[int] = 50"},{name:"guidance_scale",val:": typing.Optional[float] = 7.5"},{name:"source_guidance_scale",val:": typing.Optional[float] = 1"},{name:"num_images_per_prompt",val:": typing.Optional[int] = 1"},{name:"eta",val:": typing.Optional[float] = 0.1"},{name:"generator",val:": typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None"},{name:"output_type",val:": typing.Optional[str] = 'pil'"},{name:"return_dict",val:": bool = True"},{name:"callback",val:": typing.Union[typing.Callable[[int, int, torch.FloatTensor], NoneType], NoneType] = None"},{name:"callback_steps",val:": typing.Optional[int] = 1"},{name:"**kwargs",val:""}],parametersDescription:[{anchor:"diffusers.CycleDiffusionPipeline.__call__.prompt",description:`<strong>prompt</strong> (<code>str</code> or <code>List[str]</code>) &#x2014;
The prompt or prompts to guide the image generation.`,name:"prompt"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.image",description:`<strong>image</strong> (<code>torch.FloatTensor</code> or <code>PIL.Image.Image</code>) &#x2014;
<code>Image</code>, or tensor representing an image batch, that will be used as the starting point for the
process.`,name:"image"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.strength",description:`<strong>strength</strong> (<code>float</code>, <em>optional</em>, defaults to 0.8) &#x2014;
Conceptually, indicates how much to transform the reference <code>image</code>. Must be between 0 and 1. <code>image</code>
will be used as a starting point, adding more noise to it the larger the <code>strength</code>. The number of
denoising steps depends on the amount of noise initially added. When <code>strength</code> is 1, added noise will
be maximum and the denoising process will run for the full number of iterations specified in
<code>num_inference_steps</code>. A value of 1, therefore, essentially ignores <code>image</code>.`,name:"strength"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.num_inference_steps",description:`<strong>num_inference_steps</strong> (<code>int</code>, <em>optional</em>, defaults to 50) &#x2014;
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. This parameter will be modulated by <code>strength</code>.`,name:"num_inference_steps"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.guidance_scale",description:`<strong>guidance_scale</strong> (<code>float</code>, <em>optional</em>, defaults to 7.5) &#x2014;
Guidance scale as defined in <a href="https://arxiv.org/abs/2207.12598" rel="nofollow">Classifier-Free Diffusion Guidance</a>.
<code>guidance_scale</code> is defined as <code>w</code> of equation 2. of <a href="https://arxiv.org/pdf/2205.11487.pdf" rel="nofollow">Imagen
Paper</a>. Guidance scale is enabled by setting <code>guidance_scale &gt; 1</code>. Higher guidance scale encourages to generate images that are closely linked to the text <code>prompt</code>,
usually at the expense of lower image quality.`,name:"guidance_scale"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.source_guidance_scale",description:`<strong>source_guidance_scale</strong> (<code>float</code>, <em>optional</em>, defaults to 1) &#x2014;
Guidance scale for the source prompt. This is useful to control the amount of influence the source
prompt for encoding.`,name:"source_guidance_scale"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.num_images_per_prompt",description:`<strong>num_images_per_prompt</strong> (<code>int</code>, <em>optional</em>, defaults to 1) &#x2014;
The number of images to generate per prompt.`,name:"num_images_per_prompt"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.eta",description:`<strong>eta</strong> (<code>float</code>, <em>optional</em>, defaults to 0.1) &#x2014;
Corresponds to parameter eta (&#x3B7;) in the DDIM paper: <a href="https://arxiv.org/abs/2010.02502" rel="nofollow">https://arxiv.org/abs/2010.02502</a>. Only applies to
<a href="/docs/diffusers/main/en/api/schedulers/ddim#diffusers.DDIMScheduler">schedulers.DDIMScheduler</a>, will be ignored for others.`,name:"eta"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.generator",description:`<strong>generator</strong> (<code>torch.Generator</code>, <em>optional</em>) &#x2014;
One or a list of <a href="https://pytorch.org/docs/stable/generated/torch.Generator.html" rel="nofollow">torch generator(s)</a>
to make generation deterministic.`,name:"generator"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.output_type",description:`<strong>output_type</strong> (<code>str</code>, <em>optional</em>, defaults to <code>&quot;pil&quot;</code>) &#x2014;
The output format of the generate image. Choose between
<a href="https://pillow.readthedocs.io/en/stable/" rel="nofollow">PIL</a>: <code>PIL.Image.Image</code> or <code>np.array</code>.`,name:"output_type"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.return_dict",description:`<strong>return_dict</strong> (<code>bool</code>, <em>optional</em>, defaults to <code>True</code>) &#x2014;
Whether or not to return a <a href="/docs/diffusers/main/en/api/pipelines/stable_diffusion/overview#diffusers.pipelines.stable_diffusion.StableDiffusionPipelineOutput">StableDiffusionPipelineOutput</a> instead of a
plain tuple.`,name:"return_dict"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.callback",description:`<strong>callback</strong> (<code>Callable</code>, <em>optional</em>) &#x2014;
A function that will be called every <code>callback_steps</code> steps during inference. The function will be
called with the following arguments: <code>callback(step: int, timestep: int, latents: torch.FloatTensor)</code>.`,name:"callback"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.callback_steps",description:`<strong>callback_steps</strong> (<code>int</code>, <em>optional</em>, defaults to 1) &#x2014;
The frequency at which the <code>callback</code> function will be called. If not specified, the callback will be
called at every step.`,name:"callback_steps"}],source:"https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_cycle_diffusion.py#L482",returnDescription:`
<p><a
  href="/docs/diffusers/main/en/api/pipelines/stable_diffusion/overview#diffusers.pipelines.stable_diffusion.StableDiffusionPipelineOutput"
>StableDiffusionPipelineOutput</a> if <code>return_dict</code> is True, otherwise a <code>tuple. When returning a tuple, the first element is a list with the generated images, and the second element is a list of </code>bool<code>s denoting whether the corresponding generated image likely represents "not-safe-for-work" (nsfw) content, according to the </code>safety_checker\`.</p>
`,returnType:`
<p><a
  href="/docs/diffusers/main/en/api/pipelines/stable_diffusion/overview#diffusers.pipelines.stable_diffusion.StableDiffusionPipelineOutput"
>StableDiffusionPipelineOutput</a> or <code>tuple</code></p>
`}}),N=new Cn({props:{name:"enable_sequential_cpu_offload",anchor:"diffusers.CycleDiffusionPipeline.enable_sequential_cpu_offload",parameters:[{name:"gpu_id",val:" = 0"}],source:"https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_cycle_diffusion.py#L225"}}),{c(){h=t("meta"),de=d(),g=t("h1"),v=t("a"),Z=t("span"),z(k.$$.fragment),Me=d(),ee=t("span"),Le=a("Cycle Diffusion"),fe=d(),_=t("h2"),b=t("a"),ne=t("span"),z(I.$$.fragment),Ge=d(),se=t("span"),Oe=a("Overview"),ue=d(),w=t("p"),Ne=a("Cycle Diffusion is a Text-Guided Image-to-Image Generation model proposed in "),$=t("a"),Ue=a("Unifying Diffusion Models\u2019 Latent Space, with Applications to CycleDiffusion and Guidance"),ze=a(" by Chen Henry Wu, Fernando De la Torre."),me=d(),R=t("p"),We=a("The abstract of the paper is the following:"),he=d(),K=t("p"),te=t("em"),Fe=a("Diffusion models have achieved unprecedented performance in generative modeling. The commonly-adopted formulation of the latent code of diffusion models is a sequence of gradually denoised samples, as opposed to the simpler (e.g., Gaussian) latent space of GANs, VAEs, and normalizing flows. This paper provides an alternative, Gaussian formulation of the latent space of various diffusion models, as well as an invertible DPM-Encoder that maps images into the latent space. While our formulation is purely based on the definition of diffusion models, we demonstrate several intriguing consequences. (1) Empirically, we observe that a common latent space emerges from two diffusion models trained independently on related domains. In light of this finding, we propose CycleDiffusion, which uses DPM-Encoder for unpaired image-to-image translation. Furthermore, applying CycleDiffusion to text-to-image diffusion models, we show that large-scale text-to-image diffusion models can be used as zero-shot image-to-image editors. (2) One can guide pre-trained diffusion models and GANs by controlling the latent codes in a unified, plug-and-play formulation based on energy-based models. Using the CLIP model and a face recognition model as guidance, we demonstrate that diffusion models have better coverage of low-density sub-populations and individuals than GANs."),ge=d(),E=t("p"),ie=t("em"),Be=a("Tips"),Ve=a(":"),_e=d(),D=t("ul"),q=t("li"),He=a("The Cycle Diffusion pipeline is fully compatible with any "),J=t("a"),Re=a("Stable Diffusion"),Ke=a(" checkpoints"),Je=d(),j=t("li"),Qe=a("Currently Cycle Diffusion only works with the "),Q=t("a"),Xe=a("DDIMScheduler"),Ye=a("."),ye=d(),S=t("p"),oe=t("em"),Ze=a("Example"),en=a(":"),ve=d(),T=t("p"),nn=a("In the following we should how to best use the "),X=t("a"),sn=a("CycleDiffusionPipeline"),be=d(),z(A.$$.fragment),we=d(),y=t("h2"),C=t("a"),ae=t("span"),z(M.$$.fragment),tn=d(),re=t("span"),on=a("CycleDiffusionPipeline"),De=d(),u=t("div"),z(L.$$.fragment),an=d(),le=t("p"),rn=a("Pipeline for text-guided image to image generation using Stable Diffusion."),ln=d(),G=t("p"),cn=a("This model inherits from "),Y=t("a"),pn=a("DiffusionPipeline"),dn=a(`. Check the superclass documentation for the generic methods the
library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)`),fn=d(),P=t("div"),z(O.$$.fragment),un=d(),ce=t("p"),mn=a("Function invoked when calling the pipeline for generation."),hn=d(),x=t("div"),z(N.$$.fragment),gn=d(),U=t("p"),_n=a(`Offloads all models to CPU using accelerate, significantly reducing memory usage. When called, unet,
text_encoder, vae and safety checker have their state dicts saved to CPU and then are moved to a
`),pe=t("code"),yn=a("torch.device('meta') and loaded to GPU only when their specific submodule has its "),vn=a("forward` method called."),this.h()},l(e){const l=Xn('[data-svelte="svelte-1phssyn"]',document.head);h=i(l,"META",{name:!0,content:!0}),l.forEach(s),de=f(e),g=i(e,"H1",{class:!0});var Pe=o(g);v=i(Pe,"A",{id:!0,class:!0,href:!0});var kn=o(v);Z=i(kn,"SPAN",{});var In=o(Z);W(k.$$.fragment,In),In.forEach(s),kn.forEach(s),Me=f(Pe),ee=i(Pe,"SPAN",{});var $n=o(ee);Le=r($n,"Cycle Diffusion"),$n.forEach(s),Pe.forEach(s),fe=f(e),_=i(e,"H2",{class:!0});var xe=o(_);b=i(xe,"A",{id:!0,class:!0,href:!0});var En=o(b);ne=i(En,"SPAN",{});var qn=o(ne);W(I.$$.fragment,qn),qn.forEach(s),En.forEach(s),Ge=f(xe),se=i(xe,"SPAN",{});var jn=o(se);Oe=r(jn,"Overview"),jn.forEach(s),xe.forEach(s),ue=f(e),w=i(e,"P",{});var ke=o(w);Ne=r(ke,"Cycle Diffusion is a Text-Guided Image-to-Image Generation model proposed in "),$=i(ke,"A",{href:!0,rel:!0});var Sn=o($);Ue=r(Sn,"Unifying Diffusion Models\u2019 Latent Space, with Applications to CycleDiffusion and Guidance"),Sn.forEach(s),ze=r(ke," by Chen Henry Wu, Fernando De la Torre."),ke.forEach(s),me=f(e),R=i(e,"P",{});var Tn=o(R);We=r(Tn,"The abstract of the paper is the following:"),Tn.forEach(s),he=f(e),K=i(e,"P",{});var An=o(K);te=i(An,"EM",{});var Mn=o(te);Fe=r(Mn,"Diffusion models have achieved unprecedented performance in generative modeling. The commonly-adopted formulation of the latent code of diffusion models is a sequence of gradually denoised samples, as opposed to the simpler (e.g., Gaussian) latent space of GANs, VAEs, and normalizing flows. This paper provides an alternative, Gaussian formulation of the latent space of various diffusion models, as well as an invertible DPM-Encoder that maps images into the latent space. While our formulation is purely based on the definition of diffusion models, we demonstrate several intriguing consequences. (1) Empirically, we observe that a common latent space emerges from two diffusion models trained independently on related domains. In light of this finding, we propose CycleDiffusion, which uses DPM-Encoder for unpaired image-to-image translation. Furthermore, applying CycleDiffusion to text-to-image diffusion models, we show that large-scale text-to-image diffusion models can be used as zero-shot image-to-image editors. (2) One can guide pre-trained diffusion models and GANs by controlling the latent codes in a unified, plug-and-play formulation based on energy-based models. Using the CLIP model and a face recognition model as guidance, we demonstrate that diffusion models have better coverage of low-density sub-populations and individuals than GANs."),Mn.forEach(s),An.forEach(s),ge=f(e),E=i(e,"P",{});var bn=o(E);ie=i(bn,"EM",{});var Ln=o(ie);Be=r(Ln,"Tips"),Ln.forEach(s),Ve=r(bn,":"),bn.forEach(s),_e=f(e),D=i(e,"UL",{});var Ie=o(D);q=i(Ie,"LI",{});var $e=o(q);He=r($e,"The Cycle Diffusion pipeline is fully compatible with any "),J=i($e,"A",{href:!0});var Gn=o(J);Re=r(Gn,"Stable Diffusion"),Gn.forEach(s),Ke=r($e," checkpoints"),$e.forEach(s),Je=f(Ie),j=i(Ie,"LI",{});var Ee=o(j);Qe=r(Ee,"Currently Cycle Diffusion only works with the "),Q=i(Ee,"A",{href:!0});var On=o(Q);Xe=r(On,"DDIMScheduler"),On.forEach(s),Ye=r(Ee,"."),Ee.forEach(s),Ie.forEach(s),ye=f(e),S=i(e,"P",{});var wn=o(S);oe=i(wn,"EM",{});var Nn=o(oe);Ze=r(Nn,"Example"),Nn.forEach(s),en=r(wn,":"),wn.forEach(s),ve=f(e),T=i(e,"P",{});var Dn=o(T);nn=r(Dn,"In the following we should how to best use the "),X=i(Dn,"A",{href:!0});var Un=o(X);sn=r(Un,"CycleDiffusionPipeline"),Un.forEach(s),Dn.forEach(s),be=f(e),W(A.$$.fragment,e),we=f(e),y=i(e,"H2",{class:!0});var qe=o(y);C=i(qe,"A",{id:!0,class:!0,href:!0});var zn=o(C);ae=i(zn,"SPAN",{});var Wn=o(ae);W(M.$$.fragment,Wn),Wn.forEach(s),zn.forEach(s),tn=f(qe),re=i(qe,"SPAN",{});var Fn=o(re);on=r(Fn,"CycleDiffusionPipeline"),Fn.forEach(s),qe.forEach(s),De=f(e),u=i(e,"DIV",{class:!0});var m=o(u);W(L.$$.fragment,m),an=f(m),le=i(m,"P",{});var Bn=o(le);rn=r(Bn,"Pipeline for text-guided image to image generation using Stable Diffusion."),Bn.forEach(s),ln=f(m),G=i(m,"P",{});var je=o(G);cn=r(je,"This model inherits from "),Y=i(je,"A",{href:!0});var Vn=o(Y);pn=r(Vn,"DiffusionPipeline"),Vn.forEach(s),dn=r(je,`. Check the superclass documentation for the generic methods the
library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)`),je.forEach(s),fn=f(m),P=i(m,"DIV",{class:!0});var Se=o(P);W(O.$$.fragment,Se),un=f(Se),ce=i(Se,"P",{});var Hn=o(ce);mn=r(Hn,"Function invoked when calling the pipeline for generation."),Hn.forEach(s),Se.forEach(s),hn=f(m),x=i(m,"DIV",{class:!0});var Te=o(x);W(N.$$.fragment,Te),gn=f(Te),U=i(Te,"P",{});var Ae=o(U);_n=r(Ae,`Offloads all models to CPU using accelerate, significantly reducing memory usage. When called, unet,
text_encoder, vae and safety checker have their state dicts saved to CPU and then are moved to a
`),pe=i(Ae,"CODE",{});var Rn=o(pe);yn=r(Rn,"torch.device('meta') and loaded to GPU only when their specific submodule has its "),Rn.forEach(s),vn=r(Ae,"forward` method called."),Ae.forEach(s),Te.forEach(s),m.forEach(s),this.h()},h(){c(h,"name","hf:doc:metadata"),c(h,"content",JSON.stringify(ss)),c(v,"id","cycle-diffusion"),c(v,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),c(v,"href","#cycle-diffusion"),c(g,"class","relative group"),c(b,"id","overview"),c(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"),c(b,"href","#overview"),c(_,"class","relative group"),c($,"href","https://arxiv.org/abs/2210.05559"),c($,"rel","nofollow"),c(J,"href","./stable_diffusion"),c(Q,"href","/docs/diffusers/main/en/api/schedulers/ddim#diffusers.DDIMScheduler"),c(X,"href","/docs/diffusers/main/en/api/pipelines/cycle_diffusion#diffusers.CycleDiffusionPipeline"),c(C,"id","diffusers.CycleDiffusionPipeline"),c(C,"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"),c(C,"href","#diffusers.CycleDiffusionPipeline"),c(y,"class","relative group"),c(Y,"href","/docs/diffusers/main/en/api/diffusion_pipeline#diffusers.DiffusionPipeline"),c(P,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),c(x,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),c(u,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8")},m(e,l){n(document.head,h),p(e,de,l),p(e,g,l),n(g,v),n(v,Z),F(k,Z,null),n(g,Me),n(g,ee),n(ee,Le),p(e,fe,l),p(e,_,l),n(_,b),n(b,ne),F(I,ne,null),n(_,Ge),n(_,se),n(se,Oe),p(e,ue,l),p(e,w,l),n(w,Ne),n(w,$),n($,Ue),n(w,ze),p(e,me,l),p(e,R,l),n(R,We),p(e,he,l),p(e,K,l),n(K,te),n(te,Fe),p(e,ge,l),p(e,E,l),n(E,ie),n(ie,Be),n(E,Ve),p(e,_e,l),p(e,D,l),n(D,q),n(q,He),n(q,J),n(J,Re),n(q,Ke),n(D,Je),n(D,j),n(j,Qe),n(j,Q),n(Q,Xe),n(j,Ye),p(e,ye,l),p(e,S,l),n(S,oe),n(oe,Ze),n(S,en),p(e,ve,l),p(e,T,l),n(T,nn),n(T,X),n(X,sn),p(e,be,l),F(A,e,l),p(e,we,l),p(e,y,l),n(y,C),n(C,ae),F(M,ae,null),n(y,tn),n(y,re),n(re,on),p(e,De,l),p(e,u,l),F(L,u,null),n(u,an),n(u,le),n(le,rn),n(u,ln),n(u,G),n(G,cn),n(G,Y),n(Y,pn),n(G,dn),n(u,fn),n(u,P),F(O,P,null),n(P,un),n(P,ce),n(ce,mn),n(u,hn),n(u,x),F(N,x,null),n(x,gn),n(x,U),n(U,_n),n(U,pe),n(pe,yn),n(U,vn),Ce=!0},p:Yn,i(e){Ce||(B(k.$$.fragment,e),B(I.$$.fragment,e),B(A.$$.fragment,e),B(M.$$.fragment,e),B(L.$$.fragment,e),B(O.$$.fragment,e),B(N.$$.fragment,e),Ce=!0)},o(e){V(k.$$.fragment,e),V(I.$$.fragment,e),V(A.$$.fragment,e),V(M.$$.fragment,e),V(L.$$.fragment,e),V(O.$$.fragment,e),V(N.$$.fragment,e),Ce=!1},d(e){s(h),e&&s(de),e&&s(g),H(k),e&&s(fe),e&&s(_),H(I),e&&s(ue),e&&s(w),e&&s(me),e&&s(R),e&&s(he),e&&s(K),e&&s(ge),e&&s(E),e&&s(_e),e&&s(D),e&&s(ye),e&&s(S),e&&s(ve),e&&s(T),e&&s(be),H(A,e),e&&s(we),e&&s(y),H(M),e&&s(De),e&&s(u),H(L),H(O),H(N)}}}const ss={local:"cycle-diffusion",sections:[{local:"overview",title:"Overview"},{local:"diffusers.CycleDiffusionPipeline",title:"CycleDiffusionPipeline"}],title:"Cycle Diffusion"};function ts(xn){return Zn(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ls extends Kn{constructor(h){super();Jn(this,h,ts,ns,Qn,{})}}export{ls as default,ss as metadata};
