# Kandinsky

## Kandinsky

### Overview

Kandinsky inherits best practices from [DALL-E 2](https://huggingface.co/papers/2204.06125) and [Latent Diffusion](https://huggingface.co/docs/diffusers/api/pipelines/latent_diffusion), while introducing some new ideas.

It uses [CLIP](https://huggingface.co/docs/transformers/model_doc/clip) for encoding images and text, and a diffusion image prior (mapping) between latent spaces of CLIP modalities. This approach enhances the visual performance of the model and unveils new horizons in blending images and text-guided image manipulation.

The Kandinsky model is created by [Arseniy Shakhmatov](https://github.com/cene555), [Anton Razzhigaev](https://github.com/razzant), [Aleksandr Nikolich](https://github.com/AlexWortega), [Igor Pavlov](https://github.com/boomb0om), [Andrey Kuznetsov](https://github.com/kuznetsoffandrey) and [Denis Dimitrov](https://github.com/denndimitrov). The original codebase can be found [here](https://github.com/ai-forever/Kandinsky-2)

### Usage example

In the following, we will walk you through some examples of how to use the Kandinsky pipelines to create some visually aesthetic artwork.

#### Text-to-Image Generation

For text-to-image generation, we need to use both [KandinskyPriorPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyPriorPipeline) and [KandinskyPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyPipeline). The first step is to encode text prompts with CLIP and then diffuse the CLIP text embeddings to CLIP image embeddings, as first proposed in [DALL-E 2](https://cdn.openai.com/papers/dall-e-2.pdf). Let’s throw a fun prompt at Kandinsky to see what it comes up with.

Copied

```
prompt = "A alien cheeseburger creature eating itself, claymation, cinematic, moody lighting"
```

First, let’s instantiate the prior pipeline and the text-to-image pipeline. Both pipelines are diffusion models.

Copied

```
from diffusers import DiffusionPipeline
import torch

pipe_prior = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16)
pipe_prior.to("cuda")

t2i_pipe = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
t2i_pipe.to("cuda")
```

By default, the text-to-image pipeline use [DDIMScheduler](https://huggingface.co/docs/diffusers/v0.21.0/en/api/schedulers/ddim#diffusers.DDIMScheduler), you can change the scheduler to [DDPMScheduler](https://huggingface.co/docs/diffusers/v0.21.0/en/api/schedulers/ddpm#diffusers.DDPMScheduler)

Copied

```
scheduler = DDPMScheduler.from_pretrained("kandinsky-community/kandinsky-2-1", subfolder="ddpm_scheduler")
t2i_pipe = DiffusionPipeline.from_pretrained(
    "kandinsky-community/kandinsky-2-1", scheduler=scheduler, torch_dtype=torch.float16
)
t2i_pipe.to("cuda")
```

Now we pass the prompt through the prior to generate image embeddings. The prior returns both the image embeddings corresponding to the prompt and negative/unconditional image embeddings corresponding to an empty string.

Copied

```
image_embeds, negative_image_embeds = pipe_prior(prompt, guidance_scale=1.0).to_tuple()
```

The text-to-image pipeline expects both `image_embeds`, `negative_image_embeds` and the original `prompt` as the text-to-image pipeline uses another text encoder to better guide the second diffusion process of `t2i_pipe`.

By default, the prior returns unconditioned negative image embeddings corresponding to the negative prompt of `""`. For better results, you can also pass a `negative_prompt` to the prior. This will increase the effective batch size of the prior by a factor of 2.

Copied

```
prompt = "A alien cheeseburger creature eating itself, claymation, cinematic, moody lighting"
negative_prompt = "low quality, bad quality"

image_embeds, negative_image_embeds = pipe_prior(prompt, negative_prompt, guidance_scale=1.0).to_tuple()
```

Next, we can pass the embeddings as well as the prompt to the text-to-image pipeline. Remember that in case you are using a customized negative prompt, that you should pass this one also to the text-to-image pipelines with `negative_prompt=negative_prompt`:

Copied

```
image = t2i_pipe(
    prompt, image_embeds=image_embeds, negative_image_embeds=negative_image_embeds, height=768, width=768
).images[0]
image.save("cheeseburger_monster.png")
```

One cheeseburger monster coming up! Enjoy!

![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/cheeseburger.png)

We also provide an end-to-end Kandinsky pipeline [KandinskyCombinedPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyCombinedPipeline), which combines both the prior pipeline and text-to-image pipeline, and lets you perform inference in a single step. You can create the combined pipeline with the [from\_pretrained()](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/auto_pipeline#diffusers.AutoPipelineForText2Image.from_pretrained) method

Copied

```
from diffusers import AutoPipelineForText2Image
import torch

pipe = AutoPipelineForText2Image.from_pretrained(
    "kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16
)
pipe.enable_model_cpu_offload()
```

Under the hood, it will automatically load both [KandinskyPriorPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyPriorPipeline) and [KandinskyPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyPipeline). To generate images, you no longer need to call both pipelines and pass the outputs from one to another. You only need to call the combined pipeline once. You can set different `guidance_scale` and `num_inference_steps` for the prior pipeline with the `prior_guidance_scale` and `prior_num_inference_steps` arguments.

Copied

```
prompt = "A alien cheeseburger creature eating itself, claymation, cinematic, moody lighting"
negative_prompt = "low quality, bad quality"

image = pipe(prompt=prompt, negative_prompt=negative_prompt, prior_guidance_scale =1.0, guidance_scacle = 4.0, height=768, width=768).images[0]
```

The Kandinsky model works extremely well with creative prompts. Here is some of the amazing art that can be created using the exact same process but with different prompts.

Copied

```
prompt = "bird eye view shot of a full body woman with cyan light orange magenta makeup, digital art, long braided hair her face separated by makeup in the style of yin Yang surrealism, symmetrical face, real image, contrasting tone, pastel gradient background"
```

![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/hair.png)

Copied

```
prompt = "A car exploding into colorful dust"
```

![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/dusts.png)

Copied

```
prompt = "editorial photography of an organic, almost liquid smoke style armchair"
```

![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/smokechair.png)

Copied

```
prompt = "birds eye view of a quilted paper style alien planet landscape, vibrant colours, Cinematic lighting"
```

![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/alienplanet.png)

#### Text Guided Image-to-Image Generation

The same Kandinsky model weights can be used for text-guided image-to-image translation. In this case, just make sure to load the weights using the [KandinskyImg2ImgPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyImg2ImgPipeline) pipeline.

**Note**: You can also directly move the weights of the text-to-image pipelines to the image-to-image pipelines without loading them twice by making use of the [components](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/overview#diffusers.DiffusionPipeline.components) function as explained [here](https://huggingface.co/docs/diffusers/api/pipelines/kandinsky#converting-between-different-pipelines).

Let’s download an image.

Copied

```
from PIL import Image
import requests
from io import BytesIO

# download image
url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
response = requests.get(url)
original_image = Image.open(BytesIO(response.content)).convert("RGB")
original_image = original_image.resize((768, 512))
```

![img](https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg)

Copied

```
import torch
from diffusers import KandinskyImg2ImgPipeline, KandinskyPriorPipeline

# create prior
pipe_prior = KandinskyPriorPipeline.from_pretrained(
    "kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16
)
pipe_prior.to("cuda")

# create img2img pipeline
pipe = KandinskyImg2ImgPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
pipe.to("cuda")

prompt = "A fantasy landscape, Cinematic lighting"
negative_prompt = "low quality, bad quality"

image_embeds, negative_image_embeds = pipe_prior(prompt, negative_prompt).to_tuple()

out = pipe(
    prompt,
    image=original_image,
    image_embeds=image_embeds,
    negative_image_embeds=negative_image_embeds,
    height=768,
    width=768,
    strength=0.3,
)

out.images[0].save("fantasy_land.png")
```

![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/img2img_fantasyland.png)

You can also use the [KandinskyImg2ImgCombinedPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyImg2ImgCombinedPipeline) for end-to-end image-to-image generation with Kandinsky 2.1

Copied

```
from diffusers import AutoPipelineForImage2Image
import torch
import requests
from io import BytesIO
from PIL import Image
import os

pipe = AutoPipelineForImage2Image.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
pipe.enable_model_cpu_offload()

prompt = "A fantasy landscape, Cinematic lighting"
negative_prompt = "low quality, bad quality"

url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
 
response = requests.get(url)
original_image = Image.open(BytesIO(response.content)).convert("RGB")
original_image.thumbnail((768, 768))

image = pipe(prompt=prompt, image=original_image, strength=0.3).images[0]
```

#### Text Guided Inpainting Generation

You can use [KandinskyInpaintPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyInpaintPipeline) to edit images. In this example, we will add a hat to the portrait of a cat.

Copied

```
from diffusers import KandinskyInpaintPipeline, KandinskyPriorPipeline
from diffusers.utils import load_image
import torch
import numpy as np

pipe_prior = KandinskyPriorPipeline.from_pretrained(
    "kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16
)
pipe_prior.to("cuda")

prompt = "a hat"
prior_output = pipe_prior(prompt)

pipe = KandinskyInpaintPipeline.from_pretrained("kandinsky-community/kandinsky-2-1-inpaint", torch_dtype=torch.float16)
pipe.to("cuda")

init_image = load_image(
    "https://boincai.com/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"
)

mask = np.zeros((768, 768), dtype=np.float32)
# Let's mask out an area above the cat's head
mask[:250, 250:-250] = 1

out = pipe(
    prompt,
    image=init_image,
    mask_image=mask,
    **prior_output,
    height=768,
    width=768,
    num_inference_steps=150,
)

image = out.images[0]
image.save("cat_with_hat.png")
```

![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/inpaint_cat_hat.png)

To use the [KandinskyInpaintCombinedPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyInpaintCombinedPipeline) to perform end-to-end image inpainting generation, you can run below code instead

Copied

```
from diffusers import AutoPipelineForInpainting

pipe = AutoPipelineForInpainting.from_pretrained("kandinsky-community/kandinsky-2-1-inpaint", torch_dtype=torch.float16)
pipe.enable_model_cpu_offload()
image = pipe(prompt=prompt, image=original_image, mask_image=mask).images[0]
```

🚨🚨🚨 **Breaking change for Kandinsky Mask Inpainting** 🚨🚨🚨

We introduced a breaking change for Kandinsky inpainting pipeline in the following pull request: [https://github.com/boincai/diffusers/pull/4207](https://github.com/huggingface/diffusers/pull/4207). Previously we accepted a mask format where black pixels represent the masked-out area. This is inconsistent with all other pipelines in diffusers. We have changed the mask format in Knaindsky and now using white pixels instead. Please upgrade your inpainting code to follow the above. If you are using Kandinsky Inpaint in production. You now need to change the mask to:

Copied

```
# For PIL input
import PIL.ImageOps
mask = PIL.ImageOps.invert(mask)

# For PyTorch and Numpy input
mask = 1 - mask
```

#### Interpolate

The [KandinskyPriorPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyPriorPipeline) also comes with a cool utility function that will allow you to interpolate the latent space of different images and texts super easily. Here is an example of how you can create an Impressionist-style portrait for your pet based on “The Starry Night”.

Note that you can interpolate between texts and images - in the below example, we passed a text prompt “a cat” and two images to the `interplate` function, along with a `weights` variable containing the corresponding weights for each condition we interplate.

Copied

```
from diffusers import KandinskyPriorPipeline, KandinskyPipeline
from diffusers.utils import load_image
import PIL

import torch

pipe_prior = KandinskyPriorPipeline.from_pretrained(
    "kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16
)
pipe_prior.to("cuda")

img1 = load_image(
    "https://boincai.com/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"
)

img2 = load_image(
    "https://boincai.com/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/starry_night.jpeg"
)

# add all the conditions we want to interpolate, can be either text or image
images_texts = ["a cat", img1, img2]

# specify the weights for each condition in images_texts
weights = [0.3, 0.3, 0.4]

# We can leave the prompt empty
prompt = ""
prior_out = pipe_prior.interpolate(images_texts, weights)

pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
pipe.to("cuda")

image = pipe(prompt, **prior_out, height=768, width=768).images[0]

image.save("starry_cat.png")
```

![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/starry_cat.png)

### Optimization

Running Kandinsky in inference requires running both a first prior pipeline: [KandinskyPriorPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyPriorPipeline) and a second image decoding pipeline which is one of [KandinskyPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyPipeline), [KandinskyImg2ImgPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyImg2ImgPipeline), or [KandinskyInpaintPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyInpaintPipeline).

The bulk of the computation time will always be the second image decoding pipeline, so when looking into optimizing the model, one should look into the second image decoding pipeline.

When running with PyTorch < 2.0, we strongly recommend making use of [`xformers`](https://github.com/facebookresearch/xformers) to speed-up the optimization. This can be done by simply running:

Copied

```
from diffusers import DiffusionPipeline
import torch

t2i_pipe = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
t2i_pipe.enable_xformers_memory_efficient_attention()
```

When running on PyTorch >= 2.0, PyTorch’s SDPA attention will automatically be used. For more information on PyTorch’s SDPA, feel free to have a look at [this blog post](https://pytorch.org/blog/accelerated-diffusers-pt-20/).

To have explicit control , you can also manually set the pipeline to use PyTorch’s 2.0 efficient attention:

Copied

```
from diffusers.models.attention_processor import AttnAddedKVProcessor2_0

t2i_pipe.unet.set_attn_processor(AttnAddedKVProcessor2_0())
```

The slowest and most memory intense attention processor is the default `AttnAddedKVProcessor` processor. We do **not** recommend using it except for testing purposes or cases where very high determistic behaviour is desired. You can set it with:

Copied

```
from diffusers.models.attention_processor import AttnAddedKVProcessor

t2i_pipe.unet.set_attn_processor(AttnAddedKVProcessor())
```

With PyTorch >= 2.0, you can also use Kandinsky with `torch.compile` which depending on your hardware can signficantly speed-up your inference time once the model is compiled. To use Kandinsksy with `torch.compile`, you can do:

Copied

```
t2i_pipe.unet.to(memory_format=torch.channels_last)
t2i_pipe.unet = torch.compile(t2i_pipe.unet, mode="reduce-overhead", fullgraph=True)
```

After compilation you should see a very fast inference time. For more information, feel free to have a look at [Our PyTorch 2.0 benchmark](https://huggingface.co/docs/diffusers/main/en/optimization/torch2.0).

To generate images directly from a single pipeline, you can use [KandinskyCombinedPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyCombinedPipeline), [KandinskyImg2ImgCombinedPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyImg2ImgCombinedPipeline), [KandinskyInpaintCombinedPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyInpaintCombinedPipeline). These combined pipelines wrap the [KandinskyPriorPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyPriorPipeline) and [KandinskyPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyPipeline), [KandinskyImg2ImgPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyImg2ImgPipeline), [KandinskyInpaintPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/kandinsky#diffusers.KandinskyInpaintPipeline) respectively into a single pipeline for a simpler user experience

### Available Pipelines:

| Pipeline                                                                                                                                                   | Tasks                                                             |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| [pipeline\_kandinsky.py](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky/pipeline_kandinsky.py)                       | *Text-to-Image Generation*                                        |
| [pipeline\_kandinsky\_combined.py](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky_combined.py) | *End-to-end Text-to-Image, image-to-image, Inpainting Generation* |
| [pipeline\_kandinsky\_inpaint.py](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_inpaint.py)      | *Image-Guided Image Generation*                                   |
| [pipeline\_kandinsky\_img2img.py](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_img2img.py)      | *Image-Guided Image Generation*                                   |

#### KandinskyPriorPipeline

#### class diffusers.KandinskyPriorPipeline

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_prior.py#L128)

( prior: PriorTransformerimage\_encoder: CLIPVisionModelWithProjectiontext\_encoder: CLIPTextModelWithProjectiontokenizer: CLIPTokenizerscheduler: UnCLIPSchedulerimage\_processor: CLIPImageProcessor )

Parameters

* **prior** ([PriorTransformer](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/prior_transformer#diffusers.PriorTransformer)) — The canonincal unCLIP prior to approximate the image embedding from the text embedding.
* **image\_encoder** (`CLIPVisionModelWithProjection`) — Frozen image-encoder.
* **text\_encoder** (`CLIPTextModelWithProjection`) — Frozen text-encoder.
* **tokenizer** (`CLIPTokenizer`) — Tokenizer of class [CLIPTokenizer](https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer).
* **scheduler** (`UnCLIPScheduler`) — A scheduler to be used in combination with `prior` to generate image embedding.

Pipeline for generating image prior for Kandinsky

This model inherits from [DiffusionPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/overview#diffusers.DiffusionPipeline). 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.)

**\_\_call\_\_**

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_prior.py#L397)

( prompt: typing.Union\[str, typing.List\[str]]negative\_prompt: typing.Union\[str, typing.List\[str], NoneType] = Nonenum\_images\_per\_prompt: int = 1num\_inference\_steps: int = 25generator: typing.Union\[torch.\_C.Generator, typing.List\[torch.\_C.Generator], NoneType] = Nonelatents: typing.Optional\[torch.FloatTensor] = Noneguidance\_scale: float = 4.0output\_type: typing.Optional\[str] = 'pt'return\_dict: bool = True ) → `KandinskyPriorPipelineOutput` or `tuple`

Parameters

* **prompt** (`str` or `List[str]`) — The prompt or prompts to guide the image generation.
* **negative\_prompt** (`str` or `List[str]`, *optional*) — The prompt or prompts not to guide the image generation. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
* **num\_images\_per\_prompt** (`int`, *optional*, defaults to 1) — The number of images to generate per prompt.
* **num\_inference\_steps** (`int`, *optional*, defaults to 25) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
* **generator** (`torch.Generator` or `List[torch.Generator]`, *optional*) — One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic.
* **latents** (`torch.FloatTensor`, *optional*) — Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will ge generated by sampling using the supplied random `generator`.
* **guidance\_scale** (`float`, *optional*, defaults to 4.0) — Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality.
* **output\_type** (`str`, *optional*, defaults to `"pt"`) — The output format of the generate image. Choose between: `"np"` (`np.array`) or `"pt"` (`torch.Tensor`).
* **return\_dict** (`bool`, *optional*, defaults to `True`) — Whether or not to return a [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) instead of a plain tuple.

Returns

`KandinskyPriorPipelineOutput` or `tuple`

Function invoked when calling the pipeline for generation.

Examples:

Copied

```
>>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline
>>> import torch

>>> pipe_prior = KandinskyPriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-1-prior")
>>> pipe_prior.to("cuda")

>>> prompt = "red cat, 4k photo"
>>> out = pipe_prior(prompt)
>>> image_emb = out.image_embeds
>>> negative_image_emb = out.negative_image_embeds

>>> pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1")
>>> pipe.to("cuda")

>>> image = pipe(
...     prompt,
...     image_embeds=image_emb,
...     negative_image_embeds=negative_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=100,
... ).images

>>> image[0].save("cat.png")
```

**interpolate**

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_prior.py#L172)

( images\_and\_prompts: typing.List\[typing.Union\[str, PIL.Image.Image, torch.FloatTensor]]weights: typing.List\[float]num\_images\_per\_prompt: int = 1num\_inference\_steps: int = 25generator: typing.Union\[torch.\_C.Generator, typing.List\[torch.\_C.Generator], NoneType] = Nonelatents: typing.Optional\[torch.FloatTensor] = Nonenegative\_prior\_prompt: typing.Optional\[str] = Nonenegative\_prompt: str = ''guidance\_scale: float = 4.0device = None ) → `KandinskyPriorPipelineOutput` or `tuple`

Parameters

* **images\_and\_prompts** (`List[Union[str, PIL.Image.Image, torch.FloatTensor]]`) — list of prompts and images to guide the image generation. weights — (`List[float]`): list of weights for each condition in `images_and_prompts`
* **num\_images\_per\_prompt** (`int`, *optional*, defaults to 1) — The number of images to generate per prompt.
* **num\_inference\_steps** (`int`, *optional*, defaults to 25) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
* **generator** (`torch.Generator` or `List[torch.Generator]`, *optional*) — One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic.
* **latents** (`torch.FloatTensor`, *optional*) — Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will ge generated by sampling using the supplied random `generator`.
* **negative\_prior\_prompt** (`str`, *optional*) — The prompt not to guide the prior diffusion process. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
* **negative\_prompt** (`str` or `List[str]`, *optional*) — The prompt not to guide the image generation. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
* **guidance\_scale** (`float`, *optional*, defaults to 4.0) — Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality.

Returns

`KandinskyPriorPipelineOutput` or `tuple`

Function invoked when using the prior pipeline for interpolation.

Examples:

Copied

```
>>> from diffusers import KandinskyPriorPipeline, KandinskyPipeline
>>> from diffusers.utils import load_image
>>> import PIL

>>> import torch
>>> from torchvision import transforms

>>> pipe_prior = KandinskyPriorPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16
... )
>>> pipe_prior.to("cuda")

>>> img1 = load_image(
...     "https://boincai.com/datasets/hf-internal-testing/diffusers-images/resolve/main"
...     "/kandinsky/cat.png"
... )

>>> img2 = load_image(
...     "https://boincai.com/datasets/hf-internal-testing/diffusers-images/resolve/main"
...     "/kandinsky/starry_night.jpeg"
... )

>>> images_texts = ["a cat", img1, img2]
>>> weights = [0.3, 0.3, 0.4]
>>> image_emb, zero_image_emb = pipe_prior.interpolate(images_texts, weights)

>>> pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
>>> pipe.to("cuda")

>>> image = pipe(
...     "",
...     image_embeds=image_emb,
...     negative_image_embeds=zero_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=150,
... ).images[0]

>>> image.save("starry_cat.png")
```

#### KandinskyPipeline

#### class diffusers.KandinskyPipeline

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky.py#L76)

( text\_encoder: MultilingualCLIPtokenizer: XLMRobertaTokenizerunet: UNet2DConditionModelscheduler: typing.Union\[diffusers.schedulers.scheduling\_ddim.DDIMScheduler, diffusers.schedulers.scheduling\_ddpm.DDPMScheduler]movq: VQModel )

Parameters

* **text\_encoder** (`MultilingualCLIP`) — Frozen text-encoder.
* **tokenizer** (`XLMRobertaTokenizer`) — Tokenizer of class
* **scheduler** (Union\[`DDIMScheduler`,`DDPMScheduler`]) — A scheduler to be used in combination with `unet` to generate image latents.
* **unet** ([UNet2DConditionModel](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/unet2d-cond#diffusers.UNet2DConditionModel)) — Conditional U-Net architecture to denoise the image embedding.
* **movq** ([VQModel](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/vq#diffusers.VQModel)) — MoVQ Decoder to generate the image from the latents.

Pipeline for text-to-image generation using Kandinsky

This model inherits from [DiffusionPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/overview#diffusers.DiffusionPipeline). 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.)

**\_\_call\_\_**

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky.py#L231)

( prompt: typing.Union\[str, typing.List\[str]]image\_embeds: typing.Union\[torch.FloatTensor, typing.List\[torch.FloatTensor]]negative\_image\_embeds: typing.Union\[torch.FloatTensor, typing.List\[torch.FloatTensor]]negative\_prompt: typing.Union\[str, typing.List\[str], NoneType] = Noneheight: int = 512width: int = 512num\_inference\_steps: int = 100guidance\_scale: float = 4.0num\_images\_per\_prompt: int = 1generator: typing.Union\[torch.\_C.Generator, typing.List\[torch.\_C.Generator], NoneType] = Nonelatents: typing.Optional\[torch.FloatTensor] = Noneoutput\_type: typing.Optional\[str] = 'pil'callback: typing.Union\[typing.Callable\[\[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback\_steps: int = 1return\_dict: bool = True ) → [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) or `tuple`

Parameters

* **prompt** (`str` or `List[str]`) — The prompt or prompts to guide the image generation.
* **image\_embeds** (`torch.FloatTensor` or `List[torch.FloatTensor]`) — The clip image embeddings for text prompt, that will be used to condition the image generation.
* **negative\_image\_embeds** (`torch.FloatTensor` or `List[torch.FloatTensor]`) — The clip image embeddings for negative text prompt, will be used to condition the image generation.
* **negative\_prompt** (`str` or `List[str]`, *optional*) — The prompt or prompts not to guide the image generation. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
* **height** (`int`, *optional*, defaults to 512) — The height in pixels of the generated image.
* **width** (`int`, *optional*, defaults to 512) — The width in pixels of the generated image.
* **num\_inference\_steps** (`int`, *optional*, defaults to 100) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
* **guidance\_scale** (`float`, *optional*, defaults to 4.0) — Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality.
* **num\_images\_per\_prompt** (`int`, *optional*, defaults to 1) — The number of images to generate per prompt.
* **generator** (`torch.Generator` or `List[torch.Generator]`, *optional*) — One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic.
* **latents** (`torch.FloatTensor`, *optional*) — Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will ge generated by sampling using the supplied random `generator`.
* **output\_type** (`str`, *optional*, defaults to `"pil"`) — The output format of the generate image. Choose between: `"pil"` (`PIL.Image.Image`), `"np"` (`np.array`) or `"pt"` (`torch.Tensor`).
* **callback** (`Callable`, *optional*) — A function that calls every `callback_steps` steps during inference. The function is called with the following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
* **callback\_steps** (`int`, *optional*, defaults to 1) — The frequency at which the `callback` function is called. If not specified, the callback is called at every step.
* **return\_dict** (`bool`, *optional*, defaults to `True`) — Whether or not to return a [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) instead of a plain tuple.

Returns

[ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) or `tuple`

Function invoked when calling the pipeline for generation.

Examples:

Copied

```
>>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline
>>> import torch

>>> pipe_prior = KandinskyPriorPipeline.from_pretrained("kandinsky-community/Kandinsky-2-1-prior")
>>> pipe_prior.to("cuda")

>>> prompt = "red cat, 4k photo"
>>> out = pipe_prior(prompt)
>>> image_emb = out.image_embeds
>>> negative_image_emb = out.negative_image_embeds

>>> pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1")
>>> pipe.to("cuda")

>>> image = pipe(
...     prompt,
...     image_embeds=image_emb,
...     negative_image_embeds=negative_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=100,
... ).images

>>> image[0].save("cat.png")
```

#### KandinskyImg2ImgPipeline

#### class diffusers.KandinskyImg2ImgPipeline

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_img2img.py#L98)

( text\_encoder: MultilingualCLIPmovq: VQModeltokenizer: XLMRobertaTokenizerunet: UNet2DConditionModelscheduler: DDIMScheduler )

Parameters

* **text\_encoder** (`MultilingualCLIP`) — Frozen text-encoder.
* **tokenizer** (`XLMRobertaTokenizer`) — Tokenizer of class
* **scheduler** ([DDIMScheduler](https://huggingface.co/docs/diffusers/v0.21.0/en/api/schedulers/ddim#diffusers.DDIMScheduler)) — A scheduler to be used in combination with `unet` to generate image latents.
* **unet** ([UNet2DConditionModel](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/unet2d-cond#diffusers.UNet2DConditionModel)) — Conditional U-Net architecture to denoise the image embedding.
* **movq** ([VQModel](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/vq#diffusers.VQModel)) — MoVQ image encoder and decoder

Pipeline for image-to-image generation using Kandinsky

This model inherits from [DiffusionPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/overview#diffusers.DiffusionPipeline). 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.)

**\_\_call\_\_**

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_img2img.py#L293)

( prompt: typing.Union\[str, typing.List\[str]]image: typing.Union\[torch.FloatTensor, PIL.Image.Image, typing.List\[torch.FloatTensor], typing.List\[PIL.Image.Image]]image\_embeds: FloatTensornegative\_image\_embeds: FloatTensornegative\_prompt: typing.Union\[str, typing.List\[str], NoneType] = Noneheight: int = 512width: int = 512num\_inference\_steps: int = 100strength: float = 0.3guidance\_scale: float = 7.0num\_images\_per\_prompt: int = 1generator: typing.Union\[torch.\_C.Generator, typing.List\[torch.\_C.Generator], NoneType] = Noneoutput\_type: typing.Optional\[str] = 'pil'callback: typing.Union\[typing.Callable\[\[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback\_steps: int = 1return\_dict: bool = True ) → [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) or `tuple`

Parameters

* **prompt** (`str` or `List[str]`) — The prompt or prompts to guide the image generation.
* **image** (`torch.FloatTensor`, `PIL.Image.Image`) — `Image`, or tensor representing an image batch, that will be used as the starting point for the process.
* **image\_embeds** (`torch.FloatTensor` or `List[torch.FloatTensor]`) — The clip image embeddings for text prompt, that will be used to condition the image generation.
* **negative\_image\_embeds** (`torch.FloatTensor` or `List[torch.FloatTensor]`) — The clip image embeddings for negative text prompt, will be used to condition the image generation.
* **negative\_prompt** (`str` or `List[str]`, *optional*) — The prompt or prompts not to guide the image generation. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
* **height** (`int`, *optional*, defaults to 512) — The height in pixels of the generated image.
* **width** (`int`, *optional*, defaults to 512) — The width in pixels of the generated image.
* **num\_inference\_steps** (`int`, *optional*, defaults to 100) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
* **strength** (`float`, *optional*, defaults to 0.3) — Conceptually, indicates how much to transform the reference `image`. Must be between 0 and 1. `image` will be used as a starting point, adding more noise to it the larger the `strength`. The number of denoising steps depends on the amount of noise initially added. When `strength` is 1, added noise will be maximum and the denoising process will run for the full number of iterations specified in `num_inference_steps`. A value of 1, therefore, essentially ignores `image`.
* **guidance\_scale** (`float`, *optional*, defaults to 4.0) — Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality.
* **num\_images\_per\_prompt** (`int`, *optional*, defaults to 1) — The number of images to generate per prompt.
* **generator** (`torch.Generator` or `List[torch.Generator]`, *optional*) — One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic.
* **output\_type** (`str`, *optional*, defaults to `"pil"`) — The output format of the generate image. Choose between: `"pil"` (`PIL.Image.Image`), `"np"` (`np.array`) or `"pt"` (`torch.Tensor`).
* **callback** (`Callable`, *optional*) — A function that calls every `callback_steps` steps during inference. The function is called with the following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
* **callback\_steps** (`int`, *optional*, defaults to 1) — The frequency at which the `callback` function is called. If not specified, the callback is called at every step.
* **return\_dict** (`bool`, *optional*, defaults to `True`) — Whether or not to return a [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) instead of a plain tuple.

Returns

[ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) or `tuple`

Function invoked when calling the pipeline for generation.

Examples:

Copied

```
>>> from diffusers import KandinskyImg2ImgPipeline, KandinskyPriorPipeline
>>> from diffusers.utils import load_image
>>> import torch

>>> pipe_prior = KandinskyPriorPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16
... )
>>> pipe_prior.to("cuda")

>>> prompt = "A red cartoon frog, 4k"
>>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)

>>> pipe = KandinskyImg2ImgPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16
... )
>>> pipe.to("cuda")

>>> init_image = load_image(
...     "https://boincai.com/datasets/hf-internal-testing/diffusers-images/resolve/main"
...     "/kandinsky/frog.png"
... )

>>> image = pipe(
...     prompt,
...     image=init_image,
...     image_embeds=image_emb,
...     negative_image_embeds=zero_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=100,
...     strength=0.2,
... ).images

>>> image[0].save("red_frog.png")
```

#### KandinskyInpaintPipeline

#### class diffusers.KandinskyInpaintPipeline

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_inpaint.py#L240)

( text\_encoder: MultilingualCLIPmovq: VQModeltokenizer: XLMRobertaTokenizerunet: UNet2DConditionModelscheduler: DDIMScheduler )

Parameters

* **text\_encoder** (`MultilingualCLIP`) — Frozen text-encoder.
* **tokenizer** (`XLMRobertaTokenizer`) — Tokenizer of class
* **scheduler** ([DDIMScheduler](https://huggingface.co/docs/diffusers/v0.21.0/en/api/schedulers/ddim#diffusers.DDIMScheduler)) — A scheduler to be used in combination with `unet` to generate image latents.
* **unet** ([UNet2DConditionModel](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/unet2d-cond#diffusers.UNet2DConditionModel)) — Conditional U-Net architecture to denoise the image embedding.
* **movq** ([VQModel](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/vq#diffusers.VQModel)) — MoVQ image encoder and decoder

Pipeline for text-guided image inpainting using Kandinsky2.1

This model inherits from [DiffusionPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/overview#diffusers.DiffusionPipeline). 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.)

**\_\_call\_\_**

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_inpaint.py#L396)

( prompt: typing.Union\[str, typing.List\[str]]image: typing.Union\[torch.FloatTensor, PIL.Image.Image]mask\_image: typing.Union\[torch.FloatTensor, PIL.Image.Image, numpy.ndarray]image\_embeds: FloatTensornegative\_image\_embeds: FloatTensornegative\_prompt: typing.Union\[str, typing.List\[str], NoneType] = Noneheight: int = 512width: int = 512num\_inference\_steps: int = 100guidance\_scale: float = 4.0num\_images\_per\_prompt: int = 1generator: typing.Union\[torch.\_C.Generator, typing.List\[torch.\_C.Generator], NoneType] = Nonelatents: typing.Optional\[torch.FloatTensor] = Noneoutput\_type: typing.Optional\[str] = 'pil'callback: typing.Union\[typing.Callable\[\[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback\_steps: int = 1return\_dict: bool = True ) → [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) or `tuple`

Parameters

* **prompt** (`str` or `List[str]`) — The prompt or prompts to guide the image generation.
* **image** (`torch.FloatTensor`, `PIL.Image.Image` or `np.ndarray`) — `Image`, or tensor representing an image batch, that will be used as the starting point for the process.
* **mask\_image** (`PIL.Image.Image`,`torch.FloatTensor` or `np.ndarray`) — `Image`, or a tensor representing an image batch, to mask `image`. White pixels in the mask will be repainted, while black pixels will be preserved. You can pass a pytorch tensor as mask only if the image you passed is a pytorch tensor, and it should contain one color channel (L) instead of 3, so the expected shape would be either `(B, 1, H, W,)`, `(B, H, W)`, `(1, H, W)` or `(H, W)` If image is an PIL image or numpy array, mask should also be a either PIL image or numpy array. If it is a PIL image, it will be converted to a single channel (luminance) before use. If it is a nummpy array, the expected shape is `(H, W)`.
* **image\_embeds** (`torch.FloatTensor` or `List[torch.FloatTensor]`) — The clip image embeddings for text prompt, that will be used to condition the image generation.
* **negative\_image\_embeds** (`torch.FloatTensor` or `List[torch.FloatTensor]`) — The clip image embeddings for negative text prompt, will be used to condition the image generation.
* **negative\_prompt** (`str` or `List[str]`, *optional*) — The prompt or prompts not to guide the image generation. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
* **height** (`int`, *optional*, defaults to 512) — The height in pixels of the generated image.
* **width** (`int`, *optional*, defaults to 512) — The width in pixels of the generated image.
* **num\_inference\_steps** (`int`, *optional*, defaults to 100) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
* **guidance\_scale** (`float`, *optional*, defaults to 4.0) — Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality.
* **num\_images\_per\_prompt** (`int`, *optional*, defaults to 1) — The number of images to generate per prompt.
* **generator** (`torch.Generator` or `List[torch.Generator]`, *optional*) — One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic.
* **latents** (`torch.FloatTensor`, *optional*) — Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will ge generated by sampling using the supplied random `generator`.
* **output\_type** (`str`, *optional*, defaults to `"pil"`) — The output format of the generate image. Choose between: `"pil"` (`PIL.Image.Image`), `"np"` (`np.array`) or `"pt"` (`torch.Tensor`).
* **callback** (`Callable`, *optional*) — A function that calls every `callback_steps` steps during inference. The function is called with the following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
* **callback\_steps** (`int`, *optional*, defaults to 1) — The frequency at which the `callback` function is called. If not specified, the callback is called at every step.
* **return\_dict** (`bool`, *optional*, defaults to `True`) — Whether or not to return a [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) instead of a plain tuple.

Returns

[ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) or `tuple`

Function invoked when calling the pipeline for generation.

Examples:

Copied

```
>>> from diffusers import KandinskyInpaintPipeline, KandinskyPriorPipeline
>>> from diffusers.utils import load_image
>>> import torch
>>> import numpy as np

>>> pipe_prior = KandinskyPriorPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16
... )
>>> pipe_prior.to("cuda")

>>> prompt = "a hat"
>>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)

>>> pipe = KandinskyInpaintPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-1-inpaint", torch_dtype=torch.float16
... )
>>> pipe.to("cuda")

>>> init_image = load_image(
...     "https://boincai.com/datasets/hf-internal-testing/diffusers-images/resolve/main"
...     "/kandinsky/cat.png"
... )

>>> mask = np.zeros((768, 768), dtype=np.float32)
>>> mask[:250, 250:-250] = 1

>>> out = pipe(
...     prompt,
...     image=init_image,
...     mask_image=mask,
...     image_embeds=image_emb,
...     negative_image_embeds=zero_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=50,
... )

>>> image = out.images[0]
>>> image.save("cat_with_hat.png")
```

#### KandinskyCombinedPipeline

#### class diffusers.KandinskyCombinedPipeline

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py#L113)

( text\_encoder: MultilingualCLIPtokenizer: XLMRobertaTokenizerunet: UNet2DConditionModelscheduler: typing.Union\[diffusers.schedulers.scheduling\_ddim.DDIMScheduler, diffusers.schedulers.scheduling\_ddpm.DDPMScheduler]movq: VQModelprior\_prior: PriorTransformerprior\_image\_encoder: CLIPVisionModelWithProjectionprior\_text\_encoder: CLIPTextModelWithProjectionprior\_tokenizer: CLIPTokenizerprior\_scheduler: UnCLIPSchedulerprior\_image\_processor: CLIPImageProcessor )

Parameters

* **text\_encoder** (`MultilingualCLIP`) — Frozen text-encoder.
* **tokenizer** (`XLMRobertaTokenizer`) — Tokenizer of class
* **scheduler** (Union\[`DDIMScheduler`,`DDPMScheduler`]) — A scheduler to be used in combination with `unet` to generate image latents.
* **unet** ([UNet2DConditionModel](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/unet2d-cond#diffusers.UNet2DConditionModel)) — Conditional U-Net architecture to denoise the image embedding.
* **movq** ([VQModel](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/vq#diffusers.VQModel)) — MoVQ Decoder to generate the image from the latents.
* **prior\_prior** ([PriorTransformer](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/prior_transformer#diffusers.PriorTransformer)) — The canonincal unCLIP prior to approximate the image embedding from the text embedding.
* **prior\_image\_encoder** (`CLIPVisionModelWithProjection`) — Frozen image-encoder.
* **prior\_text\_encoder** (`CLIPTextModelWithProjection`) — Frozen text-encoder.
* **prior\_tokenizer** (`CLIPTokenizer`) — Tokenizer of class [CLIPTokenizer](https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer).
* **prior\_scheduler** (`UnCLIPScheduler`) — A scheduler to be used in combination with `prior` to generate image embedding.

Combined Pipeline for text-to-image generation using Kandinsky

This model inherits from [DiffusionPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/overview#diffusers.DiffusionPipeline). 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.)

**\_\_call\_\_**

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py#L214)

( prompt: typing.Union\[str, typing.List\[str]]negative\_prompt: typing.Union\[str, typing.List\[str], NoneType] = Nonenum\_inference\_steps: int = 100guidance\_scale: float = 4.0num\_images\_per\_prompt: int = 1height: int = 512width: int = 512prior\_guidance\_scale: float = 4.0prior\_num\_inference\_steps: int = 25generator: typing.Union\[torch.\_C.Generator, typing.List\[torch.\_C.Generator], NoneType] = Nonelatents: typing.Optional\[torch.FloatTensor] = Noneoutput\_type: typing.Optional\[str] = 'pil'callback: typing.Union\[typing.Callable\[\[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback\_steps: int = 1return\_dict: bool = True ) → [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) or `tuple`

Parameters

* **prompt** (`str` or `List[str]`) — The prompt or prompts to guide the image generation.
* **negative\_prompt** (`str` or `List[str]`, *optional*) — The prompt or prompts not to guide the image generation. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
* **num\_images\_per\_prompt** (`int`, *optional*, defaults to 1) — The number of images to generate per prompt.
* **num\_inference\_steps** (`int`, *optional*, defaults to 100) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
* **height** (`int`, *optional*, defaults to 512) — The height in pixels of the generated image.
* **width** (`int`, *optional*, defaults to 512) — The width in pixels of the generated image.
* **prior\_guidance\_scale** (`float`, *optional*, defaults to 4.0) — Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality.
* **prior\_num\_inference\_steps** (`int`, *optional*, defaults to 100) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
* **guidance\_scale** (`float`, *optional*, defaults to 4.0) — Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality.
* **generator** (`torch.Generator` or `List[torch.Generator]`, *optional*) — One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic.
* **latents** (`torch.FloatTensor`, *optional*) — Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will ge generated by sampling using the supplied random `generator`.
* **output\_type** (`str`, *optional*, defaults to `"pil"`) — The output format of the generate image. Choose between: `"pil"` (`PIL.Image.Image`), `"np"` (`np.array`) or `"pt"` (`torch.Tensor`).
* **callback** (`Callable`, *optional*) — A function that calls every `callback_steps` steps during inference. The function is called with the following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
* **callback\_steps** (`int`, *optional*, defaults to 1) — The frequency at which the `callback` function is called. If not specified, the callback is called at every step.
* **return\_dict** (`bool`, *optional*, defaults to `True`) — Whether or not to return a [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) instead of a plain tuple.

Returns

[ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) or `tuple`

Function invoked when calling the pipeline for generation.

Examples:

Copied

```
from diffusers import AutoPipelineForText2Image
import torch

pipe = AutoPipelineForText2Image.from_pretrained(
    "kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16
)
pipe.enable_model_cpu_offload()

prompt = "A lion in galaxies, spirals, nebulae, stars, smoke, iridescent, intricate detail, octane render, 8k"

image = pipe(prompt=prompt, num_inference_steps=25).images[0]
```

**enable\_sequential\_cpu\_offload**

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py#L195)

( gpu\_id = 0 )

Offloads all models (`unet`, `text_encoder`, `vae`, and `safety checker` state dicts) to CPU using 🤗 Accelerate, significantly reducing memory usage. Models are moved to a `torch.device('meta')` and loaded on a GPU only when their specific submodule’s `forward` method is called. Offloading happens on a submodule basis. Memory savings are higher than using `enable_model_cpu_offload`, but performance is lower.

#### KandinskyImg2ImgCombinedPipeline

#### class diffusers.KandinskyImg2ImgCombinedPipeline

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py#L327)

( text\_encoder: MultilingualCLIPtokenizer: XLMRobertaTokenizerunet: UNet2DConditionModelscheduler: typing.Union\[diffusers.schedulers.scheduling\_ddim.DDIMScheduler, diffusers.schedulers.scheduling\_ddpm.DDPMScheduler]movq: VQModelprior\_prior: PriorTransformerprior\_image\_encoder: CLIPVisionModelWithProjectionprior\_text\_encoder: CLIPTextModelWithProjectionprior\_tokenizer: CLIPTokenizerprior\_scheduler: UnCLIPSchedulerprior\_image\_processor: CLIPImageProcessor )

Parameters

* **text\_encoder** (`MultilingualCLIP`) — Frozen text-encoder.
* **tokenizer** (`XLMRobertaTokenizer`) — Tokenizer of class
* **scheduler** (Union\[`DDIMScheduler`,`DDPMScheduler`]) — A scheduler to be used in combination with `unet` to generate image latents.
* **unet** ([UNet2DConditionModel](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/unet2d-cond#diffusers.UNet2DConditionModel)) — Conditional U-Net architecture to denoise the image embedding.
* **movq** ([VQModel](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/vq#diffusers.VQModel)) — MoVQ Decoder to generate the image from the latents.
* **prior\_prior** ([PriorTransformer](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/prior_transformer#diffusers.PriorTransformer)) — The canonincal unCLIP prior to approximate the image embedding from the text embedding.
* **prior\_image\_encoder** (`CLIPVisionModelWithProjection`) — Frozen image-encoder.
* **prior\_text\_encoder** (`CLIPTextModelWithProjection`) — Frozen text-encoder.
* **prior\_tokenizer** (`CLIPTokenizer`) — Tokenizer of class [CLIPTokenizer](https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer).
* **prior\_scheduler** (`UnCLIPScheduler`) — A scheduler to be used in combination with `prior` to generate image embedding.

Combined Pipeline for image-to-image generation using Kandinsky

This model inherits from [DiffusionPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/overview#diffusers.DiffusionPipeline). 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.)

**\_\_call\_\_**

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py#L429)

( prompt: typing.Union\[str, typing.List\[str]]image: typing.Union\[torch.FloatTensor, PIL.Image.Image, typing.List\[torch.FloatTensor], typing.List\[PIL.Image.Image]]negative\_prompt: typing.Union\[str, typing.List\[str], NoneType] = Nonenum\_inference\_steps: int = 100guidance\_scale: float = 4.0num\_images\_per\_prompt: int = 1strength: float = 0.3height: int = 512width: int = 512prior\_guidance\_scale: float = 4.0prior\_num\_inference\_steps: int = 25generator: typing.Union\[torch.\_C.Generator, typing.List\[torch.\_C.Generator], NoneType] = Nonelatents: typing.Optional\[torch.FloatTensor] = Noneoutput\_type: typing.Optional\[str] = 'pil'callback: typing.Union\[typing.Callable\[\[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback\_steps: int = 1return\_dict: bool = True ) → [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) or `tuple`

Parameters

* **prompt** (`str` or `List[str]`) — The prompt or prompts to guide the image generation.
* **image** (`torch.FloatTensor`, `PIL.Image.Image`, `np.ndarray`, `List[torch.FloatTensor]`, `List[PIL.Image.Image]`, or `List[np.ndarray]`) — `Image`, or tensor representing an image batch, that will be used as the starting point for the process. Can also accept image latents as `image`, if passing latents directly, it will not be encoded again.
* **negative\_prompt** (`str` or `List[str]`, *optional*) — The prompt or prompts not to guide the image generation. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
* **num\_images\_per\_prompt** (`int`, *optional*, defaults to 1) — The number of images to generate per prompt.
* **num\_inference\_steps** (`int`, *optional*, defaults to 100) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
* **height** (`int`, *optional*, defaults to 512) — The height in pixels of the generated image.
* **width** (`int`, *optional*, defaults to 512) — The width in pixels of the generated image.
* **strength** (`float`, *optional*, defaults to 0.3) — Conceptually, indicates how much to transform the reference `image`. Must be between 0 and 1. `image` will be used as a starting point, adding more noise to it the larger the `strength`. The number of denoising steps depends on the amount of noise initially added. When `strength` is 1, added noise will be maximum and the denoising process will run for the full number of iterations specified in `num_inference_steps`. A value of 1, therefore, essentially ignores `image`.
* **prior\_guidance\_scale** (`float`, *optional*, defaults to 4.0) — Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality.
* **prior\_num\_inference\_steps** (`int`, *optional*, defaults to 100) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
* **guidance\_scale** (`float`, *optional*, defaults to 4.0) — Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality.
* **generator** (`torch.Generator` or `List[torch.Generator]`, *optional*) — One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic.
* **latents** (`torch.FloatTensor`, *optional*) — Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will ge generated by sampling using the supplied random `generator`.
* **output\_type** (`str`, *optional*, defaults to `"pil"`) — The output format of the generate image. Choose between: `"pil"` (`PIL.Image.Image`), `"np"` (`np.array`) or `"pt"` (`torch.Tensor`).
* **callback** (`Callable`, *optional*) — A function that calls every `callback_steps` steps during inference. The function is called with the following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
* **callback\_steps** (`int`, *optional*, defaults to 1) — The frequency at which the `callback` function is called. If not specified, the callback is called at every step.
* **return\_dict** (`bool`, *optional*, defaults to `True`) — Whether or not to return a [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) instead of a plain tuple.

Returns

[ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) or `tuple`

Function invoked when calling the pipeline for generation.

Examples:

Copied

```
from diffusers import AutoPipelineForImage2Image
import torch
import requests
from io import BytesIO
from PIL import Image
import os

pipe = AutoPipelineForImage2Image.from_pretrained(
    "kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16
)
pipe.enable_model_cpu_offload()

prompt = "A fantasy landscape, Cinematic lighting"
negative_prompt = "low quality, bad quality"

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

response = requests.get(url)
image = Image.open(BytesIO(response.content)).convert("RGB")
image.thumbnail((768, 768))

image = pipe(prompt=prompt, image=original_image, num_inference_steps=25).images[0]
```

**enable\_sequential\_cpu\_offload**

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py#L409)

( gpu\_id = 0 )

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 `torch.device('meta') and loaded to GPU only when their specific submodule has its` forward`method called. Note that offloading happens on a submodule basis. Memory savings are higher than with`enable\_model\_cpu\_offload\`, but performance is lower.

#### KandinskyInpaintCombinedPipeline

#### class diffusers.KandinskyInpaintCombinedPipeline

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py#L564)

( text\_encoder: MultilingualCLIPtokenizer: XLMRobertaTokenizerunet: UNet2DConditionModelscheduler: typing.Union\[diffusers.schedulers.scheduling\_ddim.DDIMScheduler, diffusers.schedulers.scheduling\_ddpm.DDPMScheduler]movq: VQModelprior\_prior: PriorTransformerprior\_image\_encoder: CLIPVisionModelWithProjectionprior\_text\_encoder: CLIPTextModelWithProjectionprior\_tokenizer: CLIPTokenizerprior\_scheduler: UnCLIPSchedulerprior\_image\_processor: CLIPImageProcessor )

Parameters

* **text\_encoder** (`MultilingualCLIP`) — Frozen text-encoder.
* **tokenizer** (`XLMRobertaTokenizer`) — Tokenizer of class
* **scheduler** (Union\[`DDIMScheduler`,`DDPMScheduler`]) — A scheduler to be used in combination with `unet` to generate image latents.
* **unet** ([UNet2DConditionModel](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/unet2d-cond#diffusers.UNet2DConditionModel)) — Conditional U-Net architecture to denoise the image embedding.
* **movq** ([VQModel](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/vq#diffusers.VQModel)) — MoVQ Decoder to generate the image from the latents.
* **prior\_prior** ([PriorTransformer](https://huggingface.co/docs/diffusers/v0.21.0/en/api/models/prior_transformer#diffusers.PriorTransformer)) — The canonincal unCLIP prior to approximate the image embedding from the text embedding.
* **prior\_image\_encoder** (`CLIPVisionModelWithProjection`) — Frozen image-encoder.
* **prior\_text\_encoder** (`CLIPTextModelWithProjection`) — Frozen text-encoder.
* **prior\_tokenizer** (`CLIPTokenizer`) — Tokenizer of class [CLIPTokenizer](https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer).
* **prior\_scheduler** (`UnCLIPScheduler`) — A scheduler to be used in combination with `prior` to generate image embedding.

Combined Pipeline for generation using Kandinsky

This model inherits from [DiffusionPipeline](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/overview#diffusers.DiffusionPipeline). 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.)

**\_\_call\_\_**

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py#L666)

( prompt: typing.Union\[str, typing.List\[str]]image: typing.Union\[torch.FloatTensor, PIL.Image.Image, typing.List\[torch.FloatTensor], typing.List\[PIL.Image.Image]]mask\_image: typing.Union\[torch.FloatTensor, PIL.Image.Image, typing.List\[torch.FloatTensor], typing.List\[PIL.Image.Image]]negative\_prompt: typing.Union\[str, typing.List\[str], NoneType] = Nonenum\_inference\_steps: int = 100guidance\_scale: float = 4.0num\_images\_per\_prompt: int = 1height: int = 512width: int = 512prior\_guidance\_scale: float = 4.0prior\_num\_inference\_steps: int = 25generator: typing.Union\[torch.\_C.Generator, typing.List\[torch.\_C.Generator], NoneType] = Nonelatents: typing.Optional\[torch.FloatTensor] = Noneoutput\_type: typing.Optional\[str] = 'pil'callback: typing.Union\[typing.Callable\[\[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback\_steps: int = 1return\_dict: bool = True ) → [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) or `tuple`

Parameters

* **prompt** (`str` or `List[str]`) — The prompt or prompts to guide the image generation.
* **image** (`torch.FloatTensor`, `PIL.Image.Image`, `np.ndarray`, `List[torch.FloatTensor]`, `List[PIL.Image.Image]`, or `List[np.ndarray]`) — `Image`, or tensor representing an image batch, that will be used as the starting point for the process. Can also accept image latents as `image`, if passing latents directly, it will not be encoded again.
* **mask\_image** (`np.array`) — Tensor representing an image batch, to mask `image`. White pixels in the mask will be repainted, while black pixels will be preserved. If `mask_image` is a PIL image, it will be converted to a single channel (luminance) before use. If it’s a tensor, it should contain one color channel (L) instead of 3, so the expected shape would be `(B, H, W, 1)`.
* **negative\_prompt** (`str` or `List[str]`, *optional*) — The prompt or prompts not to guide the image generation. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
* **num\_images\_per\_prompt** (`int`, *optional*, defaults to 1) — The number of images to generate per prompt.
* **num\_inference\_steps** (`int`, *optional*, defaults to 100) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
* **height** (`int`, *optional*, defaults to 512) — The height in pixels of the generated image.
* **width** (`int`, *optional*, defaults to 512) — The width in pixels of the generated image.
* **prior\_guidance\_scale** (`float`, *optional*, defaults to 4.0) — Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality.
* **prior\_num\_inference\_steps** (`int`, *optional*, defaults to 100) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
* **guidance\_scale** (`float`, *optional*, defaults to 4.0) — Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality.
* **generator** (`torch.Generator` or `List[torch.Generator]`, *optional*) — One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic.
* **latents** (`torch.FloatTensor`, *optional*) — Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will ge generated by sampling using the supplied random `generator`.
* **output\_type** (`str`, *optional*, defaults to `"pil"`) — The output format of the generate image. Choose between: `"pil"` (`PIL.Image.Image`), `"np"` (`np.array`) or `"pt"` (`torch.Tensor`).
* **callback** (`Callable`, *optional*) — A function that calls every `callback_steps` steps during inference. The function is called with the following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
* **callback\_steps** (`int`, *optional*, defaults to 1) — The frequency at which the `callback` function is called. If not specified, the callback is called at every step.
* **return\_dict** (`bool`, *optional*, defaults to `True`) — Whether or not to return a [ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) instead of a plain tuple.

Returns

[ImagePipelineOutput](https://huggingface.co/docs/diffusers/v0.21.0/en/api/pipelines/latent_diffusion_uncond#diffusers.ImagePipelineOutput) or `tuple`

Function invoked when calling the pipeline for generation.

Examples:

Copied

```
from diffusers import AutoPipelineForInpainting
from diffusers.utils import load_image
import torch
import numpy as np

pipe = AutoPipelineForInpainting.from_pretrained(
    "kandinsky-community/kandinsky-2-1-inpaint", torch_dtype=torch.float16
)
pipe.enable_model_cpu_offload()

prompt = "A fantasy landscape, Cinematic lighting"
negative_prompt = "low quality, bad quality"

original_image = load_image(
    "https://boincai.com/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"
)

mask = np.zeros((768, 768), dtype=np.float32)
# Let's mask out an area above the cat's head
mask[:250, 250:-250] = 1

image = pipe(prompt=prompt, image=original_image, mask_image=mask, num_inference_steps=25).images[0]
```

**enable\_sequential\_cpu\_offload**

[\<source>](https://github.com/huggingface/diffusers/blob/v0.21.0/src/diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py#L646)

( gpu\_id = 0 )

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 `torch.device('meta') and loaded to GPU only when their specific submodule has its` forward`method called. Note that offloading happens on a submodule basis. Memory savings are higher than with`enable\_model\_cpu\_offload\`, but performance is lower.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://boinc-ai.gitbook.io/diffusers/api/pipelines/kandinsky.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
