import{S as Dn,i as Cn,s as Pn,e as o,k as c,w as q,t as a,M as xn,c as s,d as n,m as d,a as i,x as j,h as r,b as p,G as t,g as f,y as S,L as kn,q as T,o as A,B as M,v as In}from"../../../chunks/vendor-hf-doc-builder.js";import{D as Ve}from"../../../chunks/Docstring-hf-doc-builder.js";import{C as En}from"../../../chunks/CodeBlock-hf-doc-builder.js";import{I as Ht}from"../../../chunks/IconCopyLink-hf-doc-builder.js";function $n(Rt){let _,we,y,D,se,L,He,ie,Re,De,v,C,ae,O,Ke,re,Je,Ce,P,Qe,G,Xe,Ye,Pe,X,Ze,xe,Y,le,et,ke,N,ce,tt,nt,Ie,x,U,ot,Z,st,it,at,z,rt,ee,lt,ct,Ee,F,de,dt,pt,$e,W,ft,te,ut,qe,B,je,b,k,pe,V,mt,fe,ht,Se,u,H,gt,ue,_t,yt,R,vt,ne,bt,wt,Dt,I,K,Ct,me,Pt,xt,E,J,kt,m,It,he,Et,$t,ge,qt,jt,_e,St,Tt,ye,At,Mt,Lt,$,Q,Ot,w,Gt,ve,Nt,Ut,be,zt,Ft,Te;return L=new Ht({}),O=new Ht({}),B=new En({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>)`}}),V=new Ht({}),H=new Ve({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"}}),K=new Ve({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:"prompt_embeds",val:": typing.Optional[torch.FloatTensor] = 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:": int = 1"}],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__.prompt_embeds",description:`<strong>prompt_embeds</strong> (<code>torch.FloatTensor</code>, <em>optional</em>) &#x2014;
Pre-generated text embeddings. Can be used to easily tweak text inputs, <em>e.g.</em> prompt weighting. If not
provided, text embeddings will be generated from <code>prompt</code> input argument.`,name:"prompt_embeds"},{anchor:"diffusers.CycleDiffusionPipeline.__call__.negative_prompt_embeds",description:`<strong>negative_prompt_embeds</strong> (<code>torch.FloatTensor</code>, <em>optional</em>) &#x2014;
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, <em>e.g.</em> prompt
weighting. If not provided, negative_prompt_embeds will be generated from <code>negative_prompt</code> input
argument.`,name:"negative_prompt_embeds"},{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#L567",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>
`}}),J=new Ve({props:{name:"enable_model_cpu_offload",anchor:"diffusers.CycleDiffusionPipeline.enable_model_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#L247"}}),Q=new Ve({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(){_=o("meta"),we=c(),y=o("h1"),D=o("a"),se=o("span"),q(L.$$.fragment),He=c(),ie=o("span"),Re=a("Cycle Diffusion"),De=c(),v=o("h2"),C=o("a"),ae=o("span"),q(O.$$.fragment),Ke=c(),re=o("span"),Je=a("Overview"),Ce=c(),P=o("p"),Qe=a("Cycle Diffusion is a Text-Guided Image-to-Image Generation model proposed in "),G=o("a"),Xe=a("Unifying Diffusion Models\u2019 Latent Space, with Applications to CycleDiffusion and Guidance"),Ye=a(" by Chen Henry Wu, Fernando De la Torre."),Pe=c(),X=o("p"),Ze=a("The abstract of the paper is the following:"),xe=c(),Y=o("p"),le=o("em"),et=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."),ke=c(),N=o("p"),ce=o("em"),tt=a("Tips"),nt=a(":"),Ie=c(),x=o("ul"),U=o("li"),ot=a("The Cycle Diffusion pipeline is fully compatible with any "),Z=o("a"),st=a("Stable Diffusion"),it=a(" checkpoints"),at=c(),z=o("li"),rt=a("Currently Cycle Diffusion only works with the "),ee=o("a"),lt=a("DDIMScheduler"),ct=a("."),Ee=c(),F=o("p"),de=o("em"),dt=a("Example"),pt=a(":"),$e=c(),W=o("p"),ft=a("In the following we should how to best use the "),te=o("a"),ut=a("CycleDiffusionPipeline"),qe=c(),q(B.$$.fragment),je=c(),b=o("h2"),k=o("a"),pe=o("span"),q(V.$$.fragment),mt=c(),fe=o("span"),ht=a("CycleDiffusionPipeline"),Se=c(),u=o("div"),q(H.$$.fragment),gt=c(),ue=o("p"),_t=a("Pipeline for text-guided image to image generation using Stable Diffusion."),yt=c(),R=o("p"),vt=a("This model inherits from "),ne=o("a"),bt=a("DiffusionPipeline"),wt=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.)`),Dt=c(),I=o("div"),q(K.$$.fragment),Ct=c(),me=o("p"),Pt=a("Function invoked when calling the pipeline for generation."),xt=c(),E=o("div"),q(J.$$.fragment),kt=c(),m=o("p"),It=a(`Offloads all models to CPU using accelerate, reducing memory usage with a low impact on performance. Compared
to `),he=o("code"),Et=a("enable_sequential_cpu_offload"),$t=a(", this method moves one whole model at a time to the GPU when its "),ge=o("code"),qt=a("forward"),jt=a(`
method is called, and the model remains in GPU until the next model runs. Memory savings are lower than with
`),_e=o("code"),St=a("enable_sequential_cpu_offload"),Tt=a(", but performance is much better due to the iterative execution of the "),ye=o("code"),At=a("unet"),Mt=a("."),Lt=c(),$=o("div"),q(Q.$$.fragment),Ot=c(),w=o("p"),Gt=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
`),ve=o("code"),Nt=a("torch.device('meta') and loaded to GPU only when their specific submodule has its "),Ut=a("forward"),be=o("code"),zt=a("method called. Note that offloading happens on a submodule basis. Memory savings are higher than with"),Ft=a("enable_model_cpu_offload`, but performance is lower."),this.h()},l(e){const l=xn('[data-svelte="svelte-1phssyn"]',document.head);_=s(l,"META",{name:!0,content:!0}),l.forEach(n),we=d(e),y=s(e,"H1",{class:!0});var Ae=i(y);D=s(Ae,"A",{id:!0,class:!0,href:!0});var Kt=i(D);se=s(Kt,"SPAN",{});var Jt=i(se);j(L.$$.fragment,Jt),Jt.forEach(n),Kt.forEach(n),He=d(Ae),ie=s(Ae,"SPAN",{});var Qt=i(ie);Re=r(Qt,"Cycle Diffusion"),Qt.forEach(n),Ae.forEach(n),De=d(e),v=s(e,"H2",{class:!0});var Me=i(v);C=s(Me,"A",{id:!0,class:!0,href:!0});var Xt=i(C);ae=s(Xt,"SPAN",{});var Yt=i(ae);j(O.$$.fragment,Yt),Yt.forEach(n),Xt.forEach(n),Ke=d(Me),re=s(Me,"SPAN",{});var Zt=i(re);Je=r(Zt,"Overview"),Zt.forEach(n),Me.forEach(n),Ce=d(e),P=s(e,"P",{});var Le=i(P);Qe=r(Le,"Cycle Diffusion is a Text-Guided Image-to-Image Generation model proposed in "),G=s(Le,"A",{href:!0,rel:!0});var en=i(G);Xe=r(en,"Unifying Diffusion Models\u2019 Latent Space, with Applications to CycleDiffusion and Guidance"),en.forEach(n),Ye=r(Le," by Chen Henry Wu, Fernando De la Torre."),Le.forEach(n),Pe=d(e),X=s(e,"P",{});var tn=i(X);Ze=r(tn,"The abstract of the paper is the following:"),tn.forEach(n),xe=d(e),Y=s(e,"P",{});var nn=i(Y);le=s(nn,"EM",{});var on=i(le);et=r(on,"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."),on.forEach(n),nn.forEach(n),ke=d(e),N=s(e,"P",{});var Wt=i(N);ce=s(Wt,"EM",{});var sn=i(ce);tt=r(sn,"Tips"),sn.forEach(n),nt=r(Wt,":"),Wt.forEach(n),Ie=d(e),x=s(e,"UL",{});var Oe=i(x);U=s(Oe,"LI",{});var Ge=i(U);ot=r(Ge,"The Cycle Diffusion pipeline is fully compatible with any "),Z=s(Ge,"A",{href:!0});var an=i(Z);st=r(an,"Stable Diffusion"),an.forEach(n),it=r(Ge," checkpoints"),Ge.forEach(n),at=d(Oe),z=s(Oe,"LI",{});var Ne=i(z);rt=r(Ne,"Currently Cycle Diffusion only works with the "),ee=s(Ne,"A",{href:!0});var rn=i(ee);lt=r(rn,"DDIMScheduler"),rn.forEach(n),ct=r(Ne,"."),Ne.forEach(n),Oe.forEach(n),Ee=d(e),F=s(e,"P",{});var Bt=i(F);de=s(Bt,"EM",{});var ln=i(de);dt=r(ln,"Example"),ln.forEach(n),pt=r(Bt,":"),Bt.forEach(n),$e=d(e),W=s(e,"P",{});var Vt=i(W);ft=r(Vt,"In the following we should how to best use the "),te=s(Vt,"A",{href:!0});var cn=i(te);ut=r(cn,"CycleDiffusionPipeline"),cn.forEach(n),Vt.forEach(n),qe=d(e),j(B.$$.fragment,e),je=d(e),b=s(e,"H2",{class:!0});var Ue=i(b);k=s(Ue,"A",{id:!0,class:!0,href:!0});var dn=i(k);pe=s(dn,"SPAN",{});var pn=i(pe);j(V.$$.fragment,pn),pn.forEach(n),dn.forEach(n),mt=d(Ue),fe=s(Ue,"SPAN",{});var fn=i(fe);ht=r(fn,"CycleDiffusionPipeline"),fn.forEach(n),Ue.forEach(n),Se=d(e),u=s(e,"DIV",{class:!0});var h=i(u);j(H.$$.fragment,h),gt=d(h),ue=s(h,"P",{});var un=i(ue);_t=r(un,"Pipeline for text-guided image to image generation using Stable Diffusion."),un.forEach(n),yt=d(h),R=s(h,"P",{});var ze=i(R);vt=r(ze,"This model inherits from "),ne=s(ze,"A",{href:!0});var mn=i(ne);bt=r(mn,"DiffusionPipeline"),mn.forEach(n),wt=r(ze,`. 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.)`),ze.forEach(n),Dt=d(h),I=s(h,"DIV",{class:!0});var Fe=i(I);j(K.$$.fragment,Fe),Ct=d(Fe),me=s(Fe,"P",{});var hn=i(me);Pt=r(hn,"Function invoked when calling the pipeline for generation."),hn.forEach(n),Fe.forEach(n),xt=d(h),E=s(h,"DIV",{class:!0});var We=i(E);j(J.$$.fragment,We),kt=d(We),m=s(We,"P",{});var g=i(m);It=r(g,`Offloads all models to CPU using accelerate, reducing memory usage with a low impact on performance. Compared
to `),he=s(g,"CODE",{});var gn=i(he);Et=r(gn,"enable_sequential_cpu_offload"),gn.forEach(n),$t=r(g,", this method moves one whole model at a time to the GPU when its "),ge=s(g,"CODE",{});var _n=i(ge);qt=r(_n,"forward"),_n.forEach(n),jt=r(g,`
method is called, and the model remains in GPU until the next model runs. Memory savings are lower than with
`),_e=s(g,"CODE",{});var yn=i(_e);St=r(yn,"enable_sequential_cpu_offload"),yn.forEach(n),Tt=r(g,", but performance is much better due to the iterative execution of the "),ye=s(g,"CODE",{});var vn=i(ye);At=r(vn,"unet"),vn.forEach(n),Mt=r(g,"."),g.forEach(n),We.forEach(n),Lt=d(h),$=s(h,"DIV",{class:!0});var Be=i($);j(Q.$$.fragment,Be),Ot=d(Be),w=s(Be,"P",{});var oe=i(w);Gt=r(oe,`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
`),ve=s(oe,"CODE",{});var bn=i(ve);Nt=r(bn,"torch.device('meta') and loaded to GPU only when their specific submodule has its "),bn.forEach(n),Ut=r(oe,"forward"),be=s(oe,"CODE",{});var wn=i(be);zt=r(wn,"method called. Note that offloading happens on a submodule basis. Memory savings are higher than with"),wn.forEach(n),Ft=r(oe,"enable_model_cpu_offload`, but performance is lower."),oe.forEach(n),Be.forEach(n),h.forEach(n),this.h()},h(){p(_,"name","hf:doc:metadata"),p(_,"content",JSON.stringify(qn)),p(D,"id","cycle-diffusion"),p(D,"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"),p(D,"href","#cycle-diffusion"),p(y,"class","relative group"),p(C,"id","overview"),p(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"),p(C,"href","#overview"),p(v,"class","relative group"),p(G,"href","https://arxiv.org/abs/2210.05559"),p(G,"rel","nofollow"),p(Z,"href","./stable_diffusion"),p(ee,"href","/docs/diffusers/main/en/api/schedulers/ddim#diffusers.DDIMScheduler"),p(te,"href","/docs/diffusers/main/en/api/pipelines/cycle_diffusion#diffusers.CycleDiffusionPipeline"),p(k,"id","diffusers.CycleDiffusionPipeline"),p(k,"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"),p(k,"href","#diffusers.CycleDiffusionPipeline"),p(b,"class","relative group"),p(ne,"href","/docs/diffusers/main/en/api/diffusion_pipeline#diffusers.DiffusionPipeline"),p(I,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),p(E,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),p($,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),p(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){t(document.head,_),f(e,we,l),f(e,y,l),t(y,D),t(D,se),S(L,se,null),t(y,He),t(y,ie),t(ie,Re),f(e,De,l),f(e,v,l),t(v,C),t(C,ae),S(O,ae,null),t(v,Ke),t(v,re),t(re,Je),f(e,Ce,l),f(e,P,l),t(P,Qe),t(P,G),t(G,Xe),t(P,Ye),f(e,Pe,l),f(e,X,l),t(X,Ze),f(e,xe,l),f(e,Y,l),t(Y,le),t(le,et),f(e,ke,l),f(e,N,l),t(N,ce),t(ce,tt),t(N,nt),f(e,Ie,l),f(e,x,l),t(x,U),t(U,ot),t(U,Z),t(Z,st),t(U,it),t(x,at),t(x,z),t(z,rt),t(z,ee),t(ee,lt),t(z,ct),f(e,Ee,l),f(e,F,l),t(F,de),t(de,dt),t(F,pt),f(e,$e,l),f(e,W,l),t(W,ft),t(W,te),t(te,ut),f(e,qe,l),S(B,e,l),f(e,je,l),f(e,b,l),t(b,k),t(k,pe),S(V,pe,null),t(b,mt),t(b,fe),t(fe,ht),f(e,Se,l),f(e,u,l),S(H,u,null),t(u,gt),t(u,ue),t(ue,_t),t(u,yt),t(u,R),t(R,vt),t(R,ne),t(ne,bt),t(R,wt),t(u,Dt),t(u,I),S(K,I,null),t(I,Ct),t(I,me),t(me,Pt),t(u,xt),t(u,E),S(J,E,null),t(E,kt),t(E,m),t(m,It),t(m,he),t(he,Et),t(m,$t),t(m,ge),t(ge,qt),t(m,jt),t(m,_e),t(_e,St),t(m,Tt),t(m,ye),t(ye,At),t(m,Mt),t(u,Lt),t(u,$),S(Q,$,null),t($,Ot),t($,w),t(w,Gt),t(w,ve),t(ve,Nt),t(w,Ut),t(w,be),t(be,zt),t(w,Ft),Te=!0},p:kn,i(e){Te||(T(L.$$.fragment,e),T(O.$$.fragment,e),T(B.$$.fragment,e),T(V.$$.fragment,e),T(H.$$.fragment,e),T(K.$$.fragment,e),T(J.$$.fragment,e),T(Q.$$.fragment,e),Te=!0)},o(e){A(L.$$.fragment,e),A(O.$$.fragment,e),A(B.$$.fragment,e),A(V.$$.fragment,e),A(H.$$.fragment,e),A(K.$$.fragment,e),A(J.$$.fragment,e),A(Q.$$.fragment,e),Te=!1},d(e){n(_),e&&n(we),e&&n(y),M(L),e&&n(De),e&&n(v),M(O),e&&n(Ce),e&&n(P),e&&n(Pe),e&&n(X),e&&n(xe),e&&n(Y),e&&n(ke),e&&n(N),e&&n(Ie),e&&n(x),e&&n(Ee),e&&n(F),e&&n($e),e&&n(W),e&&n(qe),M(B,e),e&&n(je),e&&n(b),M(V),e&&n(Se),e&&n(u),M(H),M(K),M(J),M(Q)}}}const qn={local:"cycle-diffusion",sections:[{local:"overview",title:"Overview"},{local:"diffusers.CycleDiffusionPipeline",title:"CycleDiffusionPipeline"}],title:"Cycle Diffusion"};function jn(Rt){return In(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ln extends Dn{constructor(_){super();Cn(this,_,jn,$n,Pn,{})}}export{Ln as default,qn as metadata};
