import{S as to,i as no,s as oo,e as o,k as f,w as $,t as a,M as io,c as i,d as n,m as d,a as s,x as P,h as r,b as p,G as e,g as b,y as D,q as x,o as S,B as E,v as ao,L as Xn}from"../../../../chunks/vendor-hf-doc-builder.js";import{D as Y}from"../../../../chunks/Docstring-hf-doc-builder.js";import{C as eo}from"../../../../chunks/CodeBlock-hf-doc-builder.js";import{I as Zn}from"../../../../chunks/IconCopyLink-hf-doc-builder.js";import{E as Qn}from"../../../../chunks/ExampleCodeBlock-hf-doc-builder.js";function so(me){let u,C,_,m,I;return m=new eo({props:{code:`import requests
import torch
from PIL import Image
from io import BytesIO

from diffusers import StableDiffusionImg2ImgPipeline

device = "cuda"
model_id_or_path = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
pipe = pipe.to(device)

url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"

response = requests.get(url)
init_image = Image.open(BytesIO(response.content)).convert("RGB")
init_image = init_image.resize((768, 512))

prompt = "A fantasy landscape, trending on artstation"

images = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5).images
images[0].save("fantasy_landscape.png")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> requests
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> torch
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> io <span class="hljs-keyword">import</span> BytesIO

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionImg2ImgPipeline

<span class="hljs-meta">&gt;&gt;&gt; </span>device = <span class="hljs-string">&quot;cuda&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>model_id_or_path = <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
<span class="hljs-meta">&gt;&gt;&gt; </span>pipe = pipe.to(device)

<span class="hljs-meta">&gt;&gt;&gt; </span>url = <span class="hljs-string">&quot;https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg&quot;</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>response = requests.get(url)
<span class="hljs-meta">&gt;&gt;&gt; </span>init_image = Image.<span class="hljs-built_in">open</span>(BytesIO(response.content)).convert(<span class="hljs-string">&quot;RGB&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>init_image = init_image.resize((<span class="hljs-number">768</span>, <span class="hljs-number">512</span>))

<span class="hljs-meta">&gt;&gt;&gt; </span>prompt = <span class="hljs-string">&quot;A fantasy landscape, trending on artstation&quot;</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>images = pipe(prompt=prompt, image=init_image, strength=<span class="hljs-number">0.75</span>, guidance_scale=<span class="hljs-number">7.5</span>).images
<span class="hljs-meta">&gt;&gt;&gt; </span>images[<span class="hljs-number">0</span>].save(<span class="hljs-string">&quot;fantasy_landscape.png&quot;</span>)`}}),{c(){u=o("p"),C=a("Examples:"),_=f(),$(m.$$.fragment)},l(l){u=i(l,"P",{});var v=s(u);C=r(v,"Examples:"),v.forEach(n),_=d(l),P(m.$$.fragment,l)},m(l,v){b(l,u,v),e(u,C),b(l,_,v),D(m,l,v),I=!0},p:Xn,i(l){I||(x(m.$$.fragment,l),I=!0)},o(l){S(m.$$.fragment,l),I=!1},d(l){l&&n(u),l&&n(_),E(m,l)}}}function ro(me){let u,C,_,m,I;return m=new eo({props:{code:`import torch
from diffusers import DiffusionPipeline
from xformers.ops import MemoryEfficientAttentionFlashAttentionOp

pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)
pipe = pipe.to("cuda")
pipe.enable_xformers_memory_efficient_attention(attention_op=MemoryEfficientAttentionFlashAttentionOp)
# Workaround for not accepting attention shape using VAE for Flash Attention
pipe.vae.enable_xformers_memory_efficient_attention(attention_op=None)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> torch
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> xformers.ops <span class="hljs-keyword">import</span> MemoryEfficientAttentionFlashAttentionOp

<span class="hljs-meta">&gt;&gt;&gt; </span>pipe = DiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;stabilityai/stable-diffusion-2-1&quot;</span>, torch_dtype=torch.float16)
<span class="hljs-meta">&gt;&gt;&gt; </span>pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>pipe.enable_xformers_memory_efficient_attention(attention_op=MemoryEfficientAttentionFlashAttentionOp)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Workaround for not accepting attention shape using VAE for Flash Attention</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>pipe.vae.enable_xformers_memory_efficient_attention(attention_op=<span class="hljs-literal">None</span>)`}}),{c(){u=o("p"),C=a("Examples:"),_=f(),$(m.$$.fragment)},l(l){u=i(l,"P",{});var v=s(u);C=r(v,"Examples:"),v.forEach(n),_=d(l),P(m.$$.fragment,l)},m(l,v){b(l,u,v),e(u,C),b(l,_,v),D(m,l,v),I=!0},p:Xn,i(l){I||(x(m.$$.fragment,l),I=!0)},o(l){S(m.$$.fragment,l),I=!1},d(l){l&&n(u),l&&n(_),E(m,l)}}}function lo(me){let u,C,_,m,I,l,v,Ie,nt,Ge,O,N,we,H,ot,$e,it,Fe,y,at,K,st,rt,R,lt,pt,Z,ft,dt,Q,ct,mt,ue,ut,gt,Ve,X,ht,ee,_t,ze,U,ge,bt,vt,he,yt,We,G,It,te,wt,$t,Be,c,ne,Pt,Pe,Dt,xt,oe,St,_e,Et,kt,Ct,T,ie,jt,De,Tt,At,F,qt,A,ae,Lt,xe,Ot,Mt,Se,Nt,Ut,V,se,Gt,re,Ft,Ee,Vt,zt,Wt,w,le,Bt,ke,Jt,Yt,Ce,Ht,Kt,je,Rt,Zt,z,Qt,W,pe,Xt,Te,en,tn,B,fe,nn,k,on,Ae,an,sn,qe,rn,ln,Le,pn,fn,Oe,dn,cn,mn,J,de,un,M,gn,Me,hn,_n,Ne,bn,vn,Je;return l=new Zn({}),H=new Zn({}),ne=new Y({props:{name:"class diffusers.StableDiffusionImg2ImgPipeline",anchor:"diffusers.StableDiffusionImg2ImgPipeline",parameters:[{name:"vae",val:": AutoencoderKL"},{name:"text_encoder",val:": CLIPTextModel"},{name:"tokenizer",val:": CLIPTokenizer"},{name:"unet",val:": UNet2DConditionModel"},{name:"scheduler",val:": KarrasDiffusionSchedulers"},{name:"safety_checker",val:": StableDiffusionSafetyChecker"},{name:"feature_extractor",val:": CLIPFeatureExtractor"},{name:"requires_safety_checker",val:": bool = True"}],parametersDescription:[{anchor:"diffusers.StableDiffusionImg2ImgPipeline.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.StableDiffusionImg2ImgPipeline.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.StableDiffusionImg2ImgPipeline.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.StableDiffusionImg2ImgPipeline.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.StableDiffusionImg2ImgPipeline.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.StableDiffusionImg2ImgPipeline.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/runwayml/stable-diffusion-v1-5" rel="nofollow">model card</a> for details.`,name:"safety_checker"},{anchor:"diffusers.StableDiffusionImg2ImgPipeline.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_stable_diffusion_img2img.py#L93"}}),ie=new Y({props:{name:"__call__",anchor:"diffusers.StableDiffusionImg2ImgPipeline.__call__",parameters:[{name:"prompt",val:": typing.Union[str, typing.List[str]] = None"},{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:"negative_prompt",val:": typing.Union[str, typing.List[str], NoneType] = None"},{name:"num_images_per_prompt",val:": typing.Optional[int] = 1"},{name:"eta",val:": typing.Optional[float] = 0.0"},{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:"negative_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.StableDiffusionImg2ImgPipeline.__call__.prompt",description:`<strong>prompt</strong> (<code>str</code> or <code>List[str]</code>, <em>optional</em>) &#x2014;
The prompt or prompts to guide the image generation. If not defined, one has to pass <code>prompt_embeds</code>.
instead.`,name:"prompt"},{anchor:"diffusers.StableDiffusionImg2ImgPipeline.__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.StableDiffusionImg2ImgPipeline.__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.StableDiffusionImg2ImgPipeline.__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.StableDiffusionImg2ImgPipeline.__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.StableDiffusionImg2ImgPipeline.__call__.negative_prompt",description:`<strong>negative_prompt</strong> (<code>str</code> or <code>List[str]</code>, <em>optional</em>) &#x2014;
The prompt or prompts not to guide the image generation. If not defined, one has to pass
<code>negative_prompt_embeds</code>. instead. Ignored when not using guidance (i.e., ignored if <code>guidance_scale</code>
is less than <code>1</code>).`,name:"negative_prompt"},{anchor:"diffusers.StableDiffusionImg2ImgPipeline.__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.StableDiffusionImg2ImgPipeline.__call__.eta",description:`<strong>eta</strong> (<code>float</code>, <em>optional</em>, defaults to 0.0) &#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.StableDiffusionImg2ImgPipeline.__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.StableDiffusionImg2ImgPipeline.__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.StableDiffusionImg2ImgPipeline.__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.StableDiffusionImg2ImgPipeline.__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.StableDiffusionImg2ImgPipeline.__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.StableDiffusionImg2ImgPipeline.__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.StableDiffusionImg2ImgPipeline.__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_stable_diffusion_img2img.py#L556",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>
`}}),F=new Qn({props:{anchor:"diffusers.StableDiffusionImg2ImgPipeline.__call__.example",$$slots:{default:[so]},$$scope:{ctx:me}}}),ae=new Y({props:{name:"enable_attention_slicing",anchor:"diffusers.StableDiffusionImg2ImgPipeline.enable_attention_slicing",parameters:[{name:"slice_size",val:": typing.Union[str, int, NoneType] = 'auto'"}],parametersDescription:[{anchor:"diffusers.StableDiffusionImg2ImgPipeline.enable_attention_slicing.slice_size",description:`<strong>slice_size</strong> (<code>str</code> or <code>int</code>, <em>optional</em>, defaults to <code>&quot;auto&quot;</code>) &#x2014;
When <code>&quot;auto&quot;</code>, halves the input to the attention heads, so attention will be computed in two steps. If
<code>&quot;max&quot;</code>, maxium amount of memory will be saved by running only one slice at a time. If a number is
provided, uses as many slices as <code>attention_head_dim // slice_size</code>. In this case, <code>attention_head_dim</code>
must be a multiple of <code>slice_size</code>.`,name:"slice_size"}],source:"https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/pipeline_utils.py#L1033"}}),se=new Y({props:{name:"disable_attention_slicing",anchor:"diffusers.StableDiffusionImg2ImgPipeline.disable_attention_slicing",parameters:[],source:"https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/pipeline_utils.py#L1049"}}),le=new Y({props:{name:"enable_xformers_memory_efficient_attention",anchor:"diffusers.StableDiffusionImg2ImgPipeline.enable_xformers_memory_efficient_attention",parameters:[{name:"attention_op",val:": typing.Optional[typing.Callable] = None"}],parametersDescription:[{anchor:"diffusers.StableDiffusionImg2ImgPipeline.enable_xformers_memory_efficient_attention.attention_op",description:`<strong>attention_op</strong> (<code>Callable</code>, <em>optional</em>) &#x2014;
Override the default <code>None</code> operator for use as <code>op</code> argument to the
<a href="https://facebookresearch.github.io/xformers/components/ops.html#xformers.ops.memory_efficient_attention" rel="nofollow"><code>memory_efficient_attention()</code></a>
function of xFormers.`,name:"attention_op"}],source:"https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/pipeline_utils.py#L976"}}),z=new Qn({props:{anchor:"diffusers.StableDiffusionImg2ImgPipeline.enable_xformers_memory_efficient_attention.example",$$slots:{default:[ro]},$$scope:{ctx:me}}}),pe=new Y({props:{name:"disable_xformers_memory_efficient_attention",anchor:"diffusers.StableDiffusionImg2ImgPipeline.disable_xformers_memory_efficient_attention",parameters:[],source:"https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/pipeline_utils.py#L1008"}}),fe=new Y({props:{name:"enable_model_cpu_offload",anchor:"diffusers.StableDiffusionImg2ImgPipeline.enable_model_cpu_offload",parameters:[{name:"gpu_id",val:" = 0"}],source:"https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py#L235"}}),de=new Y({props:{name:"enable_sequential_cpu_offload",anchor:"diffusers.StableDiffusionImg2ImgPipeline.enable_sequential_cpu_offload",parameters:[{name:"gpu_id",val:" = 0"}],source:"https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py#L213"}}),{c(){u=o("meta"),C=f(),_=o("h1"),m=o("a"),I=o("span"),$(l.$$.fragment),v=f(),Ie=o("span"),nt=a("Image-to-Image Generation"),Ge=f(),O=o("h2"),N=o("a"),we=o("span"),$(H.$$.fragment),ot=f(),$e=o("span"),it=a("StableDiffusionImg2ImgPipeline"),Fe=f(),y=o("p"),at=a("The Stable Diffusion model was created by the researchers and engineers from "),K=o("a"),st=a("CompVis"),rt=a(", "),R=o("a"),lt=a("Stability AI"),pt=a(", "),Z=o("a"),ft=a("runway"),dt=a(", and "),Q=o("a"),ct=a("LAION"),mt=a(". The "),ue=o("a"),ut=a("StableDiffusionImg2ImgPipeline"),gt=a(" lets you pass a text prompt and an initial image to condition the generation of new images using Stable Diffusion."),Ve=f(),X=o("p"),ht=a("The original codebase can be found here: "),ee=o("a"),_t=a("CampVis/stable-diffusion"),ze=f(),U=o("p"),ge=o("a"),bt=a("StableDiffusionImg2ImgPipeline"),vt=a(" is compatible with all Stable Diffusion checkpoints for "),he=o("a"),yt=a("Text-to-Image"),We=f(),G=o("p"),It=a("The pipeline uses the diffusion-denoising mechanism proposed by SDEdit ("),te=o("a"),wt=a("SDEdit: Guided Image Synthesis and Editing with Stochastic Differential Equations"),$t=a(`
proposed by Chenlin Meng, Yutong He, Yang Song, Jiaming Song, Jiajun Wu, Jun-Yan Zhu, Stefano Ermon).`),Be=f(),c=o("div"),$(ne.$$.fragment),Pt=f(),Pe=o("p"),Dt=a("Pipeline for text-guided image to image generation using Stable Diffusion."),xt=f(),oe=o("p"),St=a("This model inherits from "),_e=o("a"),Et=a("DiffusionPipeline"),kt=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.)`),Ct=f(),T=o("div"),$(ie.$$.fragment),jt=f(),De=o("p"),Tt=a("Function invoked when calling the pipeline for generation."),At=f(),$(F.$$.fragment),qt=f(),A=o("div"),$(ae.$$.fragment),Lt=f(),xe=o("p"),Ot=a("Enable sliced attention computation."),Mt=f(),Se=o("p"),Nt=a(`When this option is enabled, the attention module will split the input tensor in slices, to compute attention
in several steps. This is useful to save some memory in exchange for a small speed decrease.`),Ut=f(),V=o("div"),$(se.$$.fragment),Gt=f(),re=o("p"),Ft=a("Disable sliced attention computation. If "),Ee=o("code"),Vt=a("enable_attention_slicing"),zt=a(` was previously invoked, this method will go
back to computing attention in one step.`),Wt=f(),w=o("div"),$(le.$$.fragment),Bt=f(),ke=o("p"),Jt=a("Enable memory efficient attention as implemented in xformers."),Yt=f(),Ce=o("p"),Ht=a(`When this option is enabled, you should observe lower GPU memory usage and a potential speed up at inference
time. Speed up at training time is not guaranteed.`),Kt=f(),je=o("p"),Rt=a(`Warning: When Memory Efficient Attention and Sliced attention are both enabled, the Memory Efficient Attention
is used.`),Zt=f(),$(z.$$.fragment),Qt=f(),W=o("div"),$(pe.$$.fragment),Xt=f(),Te=o("p"),en=a("Disable memory efficient attention as implemented in xformers."),tn=f(),B=o("div"),$(fe.$$.fragment),nn=f(),k=o("p"),on=a(`Offloads all models to CPU using accelerate, reducing memory usage with a low impact on performance. Compared
to `),Ae=o("code"),an=a("enable_sequential_cpu_offload"),sn=a(", this method moves one whole model at a time to the GPU when its "),qe=o("code"),rn=a("forward"),ln=a(`
method is called, and the model remains in GPU until the next model runs. Memory savings are lower than with
`),Le=o("code"),pn=a("enable_sequential_cpu_offload"),fn=a(", but performance is much better due to the iterative execution of the "),Oe=o("code"),dn=a("unet"),cn=a("."),mn=f(),J=o("div"),$(de.$$.fragment),un=f(),M=o("p"),gn=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
`),Me=o("code"),hn=a("torch.device('meta') and loaded to GPU only when their specific submodule has its "),_n=a("forward"),Ne=o("code"),bn=a("method called. Note that offloading happens on a submodule basis. Memory savings are higher than with"),vn=a("enable_model_cpu_offload`, but performance is lower."),this.h()},l(t){const g=io('[data-svelte="svelte-1phssyn"]',document.head);u=i(g,"META",{name:!0,content:!0}),g.forEach(n),C=d(t),_=i(t,"H1",{class:!0});var ce=s(_);m=i(ce,"A",{id:!0,class:!0,href:!0});var Ue=s(m);I=i(Ue,"SPAN",{});var In=s(I);P(l.$$.fragment,In),In.forEach(n),Ue.forEach(n),v=d(ce),Ie=i(ce,"SPAN",{});var wn=s(Ie);nt=r(wn,"Image-to-Image Generation"),wn.forEach(n),ce.forEach(n),Ge=d(t),O=i(t,"H2",{class:!0});var Ye=s(O);N=i(Ye,"A",{id:!0,class:!0,href:!0});var $n=s(N);we=i($n,"SPAN",{});var Pn=s(we);P(H.$$.fragment,Pn),Pn.forEach(n),$n.forEach(n),ot=d(Ye),$e=i(Ye,"SPAN",{});var Dn=s($e);it=r(Dn,"StableDiffusionImg2ImgPipeline"),Dn.forEach(n),Ye.forEach(n),Fe=d(t),y=i(t,"P",{});var j=s(y);at=r(j,"The Stable Diffusion model was created by the researchers and engineers from "),K=i(j,"A",{href:!0,rel:!0});var xn=s(K);st=r(xn,"CompVis"),xn.forEach(n),rt=r(j,", "),R=i(j,"A",{href:!0,rel:!0});var Sn=s(R);lt=r(Sn,"Stability AI"),Sn.forEach(n),pt=r(j,", "),Z=i(j,"A",{href:!0,rel:!0});var En=s(Z);ft=r(En,"runway"),En.forEach(n),dt=r(j,", and "),Q=i(j,"A",{href:!0,rel:!0});var kn=s(Q);ct=r(kn,"LAION"),kn.forEach(n),mt=r(j,". The "),ue=i(j,"A",{href:!0});var Cn=s(ue);ut=r(Cn,"StableDiffusionImg2ImgPipeline"),Cn.forEach(n),gt=r(j," lets you pass a text prompt and an initial image to condition the generation of new images using Stable Diffusion."),j.forEach(n),Ve=d(t),X=i(t,"P",{});var yn=s(X);ht=r(yn,"The original codebase can be found here: "),ee=i(yn,"A",{href:!0,rel:!0});var jn=s(ee);_t=r(jn,"CampVis/stable-diffusion"),jn.forEach(n),yn.forEach(n),ze=d(t),U=i(t,"P",{});var He=s(U);ge=i(He,"A",{href:!0});var Tn=s(ge);bt=r(Tn,"StableDiffusionImg2ImgPipeline"),Tn.forEach(n),vt=r(He," is compatible with all Stable Diffusion checkpoints for "),he=i(He,"A",{href:!0});var An=s(he);yt=r(An,"Text-to-Image"),An.forEach(n),He.forEach(n),We=d(t),G=i(t,"P",{});var Ke=s(G);It=r(Ke,"The pipeline uses the diffusion-denoising mechanism proposed by SDEdit ("),te=i(Ke,"A",{href:!0,rel:!0});var qn=s(te);wt=r(qn,"SDEdit: Guided Image Synthesis and Editing with Stochastic Differential Equations"),qn.forEach(n),$t=r(Ke,`
proposed by Chenlin Meng, Yutong He, Yang Song, Jiaming Song, Jiajun Wu, Jun-Yan Zhu, Stefano Ermon).`),Ke.forEach(n),Be=d(t),c=i(t,"DIV",{class:!0});var h=s(c);P(ne.$$.fragment,h),Pt=d(h),Pe=i(h,"P",{});var Ln=s(Pe);Dt=r(Ln,"Pipeline for text-guided image to image generation using Stable Diffusion."),Ln.forEach(n),xt=d(h),oe=i(h,"P",{});var Re=s(oe);St=r(Re,"This model inherits from "),_e=i(Re,"A",{href:!0});var On=s(_e);Et=r(On,"DiffusionPipeline"),On.forEach(n),kt=r(Re,`. 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.)`),Re.forEach(n),Ct=d(h),T=i(h,"DIV",{class:!0});var be=s(T);P(ie.$$.fragment,be),jt=d(be),De=i(be,"P",{});var Mn=s(De);Tt=r(Mn,"Function invoked when calling the pipeline for generation."),Mn.forEach(n),At=d(be),P(F.$$.fragment,be),be.forEach(n),qt=d(h),A=i(h,"DIV",{class:!0});var ve=s(A);P(ae.$$.fragment,ve),Lt=d(ve),xe=i(ve,"P",{});var Nn=s(xe);Ot=r(Nn,"Enable sliced attention computation."),Nn.forEach(n),Mt=d(ve),Se=i(ve,"P",{});var Un=s(Se);Nt=r(Un,`When this option is enabled, the attention module will split the input tensor in slices, to compute attention
in several steps. This is useful to save some memory in exchange for a small speed decrease.`),Un.forEach(n),ve.forEach(n),Ut=d(h),V=i(h,"DIV",{class:!0});var Ze=s(V);P(se.$$.fragment,Ze),Gt=d(Ze),re=i(Ze,"P",{});var Qe=s(re);Ft=r(Qe,"Disable sliced attention computation. If "),Ee=i(Qe,"CODE",{});var Gn=s(Ee);Vt=r(Gn,"enable_attention_slicing"),Gn.forEach(n),zt=r(Qe,` was previously invoked, this method will go
back to computing attention in one step.`),Qe.forEach(n),Ze.forEach(n),Wt=d(h),w=i(h,"DIV",{class:!0});var q=s(w);P(le.$$.fragment,q),Bt=d(q),ke=i(q,"P",{});var Fn=s(ke);Jt=r(Fn,"Enable memory efficient attention as implemented in xformers."),Fn.forEach(n),Yt=d(q),Ce=i(q,"P",{});var Vn=s(Ce);Ht=r(Vn,`When this option is enabled, you should observe lower GPU memory usage and a potential speed up at inference
time. Speed up at training time is not guaranteed.`),Vn.forEach(n),Kt=d(q),je=i(q,"P",{});var zn=s(je);Rt=r(zn,`Warning: When Memory Efficient Attention and Sliced attention are both enabled, the Memory Efficient Attention
is used.`),zn.forEach(n),Zt=d(q),P(z.$$.fragment,q),q.forEach(n),Qt=d(h),W=i(h,"DIV",{class:!0});var Xe=s(W);P(pe.$$.fragment,Xe),Xt=d(Xe),Te=i(Xe,"P",{});var Wn=s(Te);en=r(Wn,"Disable memory efficient attention as implemented in xformers."),Wn.forEach(n),Xe.forEach(n),tn=d(h),B=i(h,"DIV",{class:!0});var et=s(B);P(fe.$$.fragment,et),nn=d(et),k=i(et,"P",{});var L=s(k);on=r(L,`Offloads all models to CPU using accelerate, reducing memory usage with a low impact on performance. Compared
to `),Ae=i(L,"CODE",{});var Bn=s(Ae);an=r(Bn,"enable_sequential_cpu_offload"),Bn.forEach(n),sn=r(L,", this method moves one whole model at a time to the GPU when its "),qe=i(L,"CODE",{});var Jn=s(qe);rn=r(Jn,"forward"),Jn.forEach(n),ln=r(L,`
method is called, and the model remains in GPU until the next model runs. Memory savings are lower than with
`),Le=i(L,"CODE",{});var Yn=s(Le);pn=r(Yn,"enable_sequential_cpu_offload"),Yn.forEach(n),fn=r(L,", but performance is much better due to the iterative execution of the "),Oe=i(L,"CODE",{});var Hn=s(Oe);dn=r(Hn,"unet"),Hn.forEach(n),cn=r(L,"."),L.forEach(n),et.forEach(n),mn=d(h),J=i(h,"DIV",{class:!0});var tt=s(J);P(de.$$.fragment,tt),un=d(tt),M=i(tt,"P",{});var ye=s(M);gn=r(ye,`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
`),Me=i(ye,"CODE",{});var Kn=s(Me);hn=r(Kn,"torch.device('meta') and loaded to GPU only when their specific submodule has its "),Kn.forEach(n),_n=r(ye,"forward"),Ne=i(ye,"CODE",{});var Rn=s(Ne);bn=r(Rn,"method called. Note that offloading happens on a submodule basis. Memory savings are higher than with"),Rn.forEach(n),vn=r(ye,"enable_model_cpu_offload`, but performance is lower."),ye.forEach(n),tt.forEach(n),h.forEach(n),this.h()},h(){p(u,"name","hf:doc:metadata"),p(u,"content",JSON.stringify(po)),p(m,"id","imagetoimage-generation"),p(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"),p(m,"href","#imagetoimage-generation"),p(_,"class","relative group"),p(N,"id","diffusers.StableDiffusionImg2ImgPipeline"),p(N,"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(N,"href","#diffusers.StableDiffusionImg2ImgPipeline"),p(O,"class","relative group"),p(K,"href","https://github.com/CompVis"),p(K,"rel","nofollow"),p(R,"href","https://stability.ai/"),p(R,"rel","nofollow"),p(Z,"href","https://github.com/runwayml"),p(Z,"rel","nofollow"),p(Q,"href","https://laion.ai/"),p(Q,"rel","nofollow"),p(ue,"href","/docs/diffusers/main/en/api/pipelines/stable_diffusion/img2img#diffusers.StableDiffusionImg2ImgPipeline"),p(ee,"href","https://github.com/CompVis/stable-diffusion/blob/main/scripts/img2img.py"),p(ee,"rel","nofollow"),p(ge,"href","/docs/diffusers/main/en/api/pipelines/stable_diffusion/img2img#diffusers.StableDiffusionImg2ImgPipeline"),p(he,"href","./text2img"),p(te,"href","https://arxiv.org/abs/2108.01073"),p(te,"rel","nofollow"),p(_e,"href","/docs/diffusers/main/en/api/diffusion_pipeline#diffusers.DiffusionPipeline"),p(T,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),p(A,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),p(V,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),p(w,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),p(W,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),p(B,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),p(J,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),p(c,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8")},m(t,g){e(document.head,u),b(t,C,g),b(t,_,g),e(_,m),e(m,I),D(l,I,null),e(_,v),e(_,Ie),e(Ie,nt),b(t,Ge,g),b(t,O,g),e(O,N),e(N,we),D(H,we,null),e(O,ot),e(O,$e),e($e,it),b(t,Fe,g),b(t,y,g),e(y,at),e(y,K),e(K,st),e(y,rt),e(y,R),e(R,lt),e(y,pt),e(y,Z),e(Z,ft),e(y,dt),e(y,Q),e(Q,ct),e(y,mt),e(y,ue),e(ue,ut),e(y,gt),b(t,Ve,g),b(t,X,g),e(X,ht),e(X,ee),e(ee,_t),b(t,ze,g),b(t,U,g),e(U,ge),e(ge,bt),e(U,vt),e(U,he),e(he,yt),b(t,We,g),b(t,G,g),e(G,It),e(G,te),e(te,wt),e(G,$t),b(t,Be,g),b(t,c,g),D(ne,c,null),e(c,Pt),e(c,Pe),e(Pe,Dt),e(c,xt),e(c,oe),e(oe,St),e(oe,_e),e(_e,Et),e(oe,kt),e(c,Ct),e(c,T),D(ie,T,null),e(T,jt),e(T,De),e(De,Tt),e(T,At),D(F,T,null),e(c,qt),e(c,A),D(ae,A,null),e(A,Lt),e(A,xe),e(xe,Ot),e(A,Mt),e(A,Se),e(Se,Nt),e(c,Ut),e(c,V),D(se,V,null),e(V,Gt),e(V,re),e(re,Ft),e(re,Ee),e(Ee,Vt),e(re,zt),e(c,Wt),e(c,w),D(le,w,null),e(w,Bt),e(w,ke),e(ke,Jt),e(w,Yt),e(w,Ce),e(Ce,Ht),e(w,Kt),e(w,je),e(je,Rt),e(w,Zt),D(z,w,null),e(c,Qt),e(c,W),D(pe,W,null),e(W,Xt),e(W,Te),e(Te,en),e(c,tn),e(c,B),D(fe,B,null),e(B,nn),e(B,k),e(k,on),e(k,Ae),e(Ae,an),e(k,sn),e(k,qe),e(qe,rn),e(k,ln),e(k,Le),e(Le,pn),e(k,fn),e(k,Oe),e(Oe,dn),e(k,cn),e(c,mn),e(c,J),D(de,J,null),e(J,un),e(J,M),e(M,gn),e(M,Me),e(Me,hn),e(M,_n),e(M,Ne),e(Ne,bn),e(M,vn),Je=!0},p(t,[g]){const ce={};g&2&&(ce.$$scope={dirty:g,ctx:t}),F.$set(ce);const Ue={};g&2&&(Ue.$$scope={dirty:g,ctx:t}),z.$set(Ue)},i(t){Je||(x(l.$$.fragment,t),x(H.$$.fragment,t),x(ne.$$.fragment,t),x(ie.$$.fragment,t),x(F.$$.fragment,t),x(ae.$$.fragment,t),x(se.$$.fragment,t),x(le.$$.fragment,t),x(z.$$.fragment,t),x(pe.$$.fragment,t),x(fe.$$.fragment,t),x(de.$$.fragment,t),Je=!0)},o(t){S(l.$$.fragment,t),S(H.$$.fragment,t),S(ne.$$.fragment,t),S(ie.$$.fragment,t),S(F.$$.fragment,t),S(ae.$$.fragment,t),S(se.$$.fragment,t),S(le.$$.fragment,t),S(z.$$.fragment,t),S(pe.$$.fragment,t),S(fe.$$.fragment,t),S(de.$$.fragment,t),Je=!1},d(t){n(u),t&&n(C),t&&n(_),E(l),t&&n(Ge),t&&n(O),E(H),t&&n(Fe),t&&n(y),t&&n(Ve),t&&n(X),t&&n(ze),t&&n(U),t&&n(We),t&&n(G),t&&n(Be),t&&n(c),E(ne),E(ie),E(F),E(ae),E(se),E(le),E(z),E(pe),E(fe),E(de)}}}const po={local:"imagetoimage-generation",sections:[{local:"diffusers.StableDiffusionImg2ImgPipeline",title:"StableDiffusionImg2ImgPipeline"}],title:"Image-to-Image Generation"};function fo(me){return ao(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class _o extends to{constructor(u){super();no(this,u,fo,lo,oo,{})}}export{_o as default,po as metadata};
