# Kandinsky 5.0 Image

[Kandinsky 5.0](https://arxiv.org/abs/2511.14993) là một họ mô hình khuếch tán cho việc tạo Video & Ảnh.

Kandinsky 5.0 Image Lite là một mô hình tạo ảnh nhẹ (6B tham số).

Mô hình giới thiệu một số đổi mới chính:
- **Pipeline khuếch tán tiềm ẩn** với **Flow Matching** để cải thiện độ ổn định huấn luyện
- **Diffusion Transformer (DiT)** làm khung xương sinh chính với cross-attention đến text embeddings
- Mã hóa văn bản kép sử dụng **Qwen2.5-VL** và **CLIP** để hiểu văn bản toàn diện
- **Flux VAE** để mã hóa và giải mã hình ảnh hiệu quả

Mã nguồn gốc có thể được tìm thấy tại [kandinskylab/Kandinsky-5](https://github.com/kandinskylab/Kandinsky-5).

> [!TIP]
> Xem tổ chức [Kandinsky Lab](https://huggingface.co/kandinskylab) trên Hub để tìm các checkpoint mô hình chính thức cho việc tạo video từ văn bản, bao gồm các biến thể được huấn luyện trước, SFT, no-CFG và distilled.

## Các Mô hình Có sẵn

Kandinsky 5.0 Image Lite:

| model_id | Mô tả | Các trường hợp sử dụng |
|------------|-------------|-----------|
| [**kandinskylab/Kandinsky-5.0-T2I-Lite-sft-Diffusers**](https://huggingface.co/kandinskylab/Kandinsky-5.0-T2I-Lite-sft-Diffusers) | Mô hình 6B hình ảnh được tinh chỉnh có giám sát | Chất lượng sinh cao nhất |
| [**kandinskylab/Kandinsky-5.0-I2I-Lite-sft-Diffusers**](https://huggingface.co/kandinskylab/Kandinsky-5.0-I2I-Lite-sft-Diffusers) | Mô hình Supervised Fine-Tuned chỉnh sửa ảnh 6B | Chất lượng sinh cao nhất |
| [**kandinskylab/Kandinsky-5.0-T2I-Lite-pretrain-Diffusers**](https://huggingface.co/kandinskylab/Kandinsky-5.0-T2I-Lite-pretrain-Diffusers) | Mô hình được huấn luyện trước cơ sở hình ảnh 6B | Nghiên cứu và tinh chỉnh |
| [**kandinskylab/Kandinsky-5.0-I2I-Lite-pretrain-Diffusers**](https://huggingface.co/kandinskylab/Kandinsky-5.0-I2I-Lite-pretrain-Diffusers) | Mô hình được huấn luyện trước cơ bản cho chỉnh sửa ảnh 6B | Nghiên cứu và tinh chỉnh |

## Ví dụ sử dụng

### Tạo hình ảnh từ văn bản cơ bản

```python
import torch
from diffusers import Kandinsky5T2IPipeline

# Load the pipeline
model_id = "kandinskylab/Kandinsky-5.0-T2I-Lite-sft-Diffusers"
pipe = Kandinsky5T2IPipeline.from_pretrained(model_id)
_ = pipe.to(device='cuda',dtype=torch.bfloat16)  # or "mps", "xpu", "cpu"

# Generate image
prompt = "A fluffy, expressive cat wearing a bright red hat with a soft, slightly textured fabric. The hat should look cozy and well-fitted on the cat’s head. On the front of the hat, add clean, bold white text that reads “SWEET”, clearly visible and neatly centered. Ensure the overall lighting highlights the hat’s color and the cat’s fur details."

output = pipe(
    prompt=prompt,
    negative_prompt="",
    height=1024,
    width=1024,
    num_inference_steps=50,
    guidance_scale=3.5,
).image[0]
```

### Tạo ảnh từ ảnh cơ bản

```python
import torch
from diffusers import Kandinsky5I2IPipeline
from diffusers.utils import load_image 
# Load the pipeline
model_id = "kandinskylab/Kandinsky-5.0-I2I-Lite-sft-Diffusers"
pipe = Kandinsky5I2IPipeline.from_pretrained(model_id)

_ = pipe.to(device='cuda',dtype=torch.bfloat16)  # or "mps", "xpu", "cpu"
pipe.enable_model_cpu_offload()                                               # <--- Enable CPU offloading for single GPU inference

# Edit the input image
image = load_image(
    "https://huggingface.co/kandinsky-community/kandinsky-3/resolve/main/assets/title.jpg?download=true"
)

prompt = "Change the background from a winter night scene to a bright summer day. Place the character on a sandy beach with clear blue sky, soft sunlight, and gentle waves in the distance. Replace the winter clothing with a light short-sleeved T-shirt (in soft pastel colors) and casual shorts. Ensure the character’s fur reflects warm daylight instead of cold winter tones. Add small beach details such as seashells, footprints in the sand, and a few scattered beach toys nearby. Keep the oranges in the scene, but place them naturally on the sand."
negative_prompt = ""

output = pipe(
    image=image,
    prompt=prompt,
    negative_prompt=negative_prompt,
    guidance_scale=3.5,
).image[0]
```

## Kandinsky5T2IPipeline[[diffusers.Kandinsky5T2IPipeline]]

#### diffusers.Kandinsky5T2IPipeline[[diffusers.Kandinsky5T2IPipeline]]

```python
diffusers.Kandinsky5T2IPipeline(transformer: Kandinsky5Transformer3DModel, vae: AutoencoderKL, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2VLProcessor, text_encoder_2: CLIPTextModel, tokenizer_2: CLIPTokenizer, scheduler: FlowMatchEulerDiscreteScheduler)
```

[Nguồn](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky5/pipeline_kandinsky_t2i.py#L120)

**Tham số:**

transformer (`Kandinsky5Transformer3DModel`) : Transformer có điều kiện để khử nhiễu các latents ảnh đã mã hóa.

vae ([AutoencoderKL](/docs/diffusers/main/vi/api/models/autoencoderkl#diffusers.AutoencoderKL)) : Mô hình Variational Auto-Encoder [black-forest-labs/FLUX.1-dev (vae)](https://huggingface.co/black-forest-labs/FLUX.1-dev) để mã hóa và giải mã video sang và từ các biểu diễn ẩn.

text_encoder (`Qwen2_5_VLForConditionalGeneration`) : Bộ mã hóa văn bản đóng băng [Qwen2.5-VL](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct).

tokenizer (`AutoProcessor`) : Bộ phân từ cho Qwen2.5-VL.

text_encoder_2 (`CLIPTextModel`) : [CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel) được đóng băng, cụ thể là biến thể [clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14).

tokenizer_2 (`CLIPTokenizer`) : Tokenizer cho CLIP.

scheduler ([FlowMatchEulerDiscreteScheduler](/docs/diffusers/main/vi/api/schedulers/flow_match_euler_discrete#diffusers.FlowMatchEulerDiscreteScheduler)) : Một trình lập lịch được sử dụng kết hợp với `transformer` để khử nhiễu các latent ảnh đã mã hóa.

Pipeline để tạo ảnh từ văn bản sử dụng Kandinsky 5.0.

Mô hình này kế thừa từ [DiffusionPipeline](/docs/diffusers/main/vi/api/pipelines/overview#diffusers.DiffusionPipeline). Xem tài liệu của lớp cha để biết các phương thức chung được triển khai cho tất cả các pipeline (tải xuống, lưu, chạy trên một thiết bị cụ thể, v.v.).

#### __call__[[diffusers.Kandinsky5T2IPipeline.__call__]]

```python
__call__(prompt: str | list[str] = None, negative_prompt: str | list[str] | None = None, height: int = 1024, width: int = 1024, num_inference_steps: int = 50, guidance_scale: float = 3.5, num_images_per_prompt: int | None = 1, generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = None, latents: typing.Optional[torch.Tensor] = None, prompt_embeds_qwen: typing.Optional[torch.Tensor] = None, prompt_embeds_clip: typing.Optional[torch.Tensor] = None, negative_prompt_embeds_qwen: typing.Optional[torch.Tensor] = None, negative_prompt_embeds_clip: typing.Optional[torch.Tensor] = None, prompt_cu_seqlens: typing.Optional[torch.Tensor] = None, negative_prompt_cu_seqlens: typing.Optional[torch.Tensor] = None, output_type: str | None = 'pil', return_dict: bool = True, callback_on_step_end: typing.Optional[typing.Callable[[int, int, NoneType], diffusers.callbacks.PipelineCallback | diffusers.callbacks.MultiPipelineCallbacks]] = None, callback_on_step_end_tensor_inputs: list = ['latents'], max_sequence_length: int = 512)
```

[Nguồn](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky5/pipeline_kandinsky_t2i.py#L534)

**Tham số:**

prompt (`str` hoặc `list[str]`, *tùy chọn*) : Prompt hoặc các prompt để hướng dẫn việc tạo ảnh. Nếu không được định nghĩa, hãy truyền `prompt_embeds` thay thế.

negative_prompt (`str` hoặc `list[str]`, *tùy chọn*) : Prompt hoặc các prompt cần tránh trong quá trình tạo ảnh. Nếu không được định nghĩa, hãy truyền `negative_prompt_embeds` thay thế. Bỏ qua khi không sử dụng guidance (`guidance_scale` < `1`).

height (`int`, mặc định là `1024`) : Chiều cao của hình ảnh được tạo ra, tính bằng pixel.

width (`int`, mặc định là `1024`) : Độ rộng của hình ảnh được tạo ra, tính bằng pixel.

num_inference_steps (`int`, mặc định là `50`) : Số bước khử nhiễu.

guidance_scale (`float`, mặc định là `5.0`) : Thang độ dẫn hướng (guidance scale) như được định nghĩa trong classifier-free guidance.

num_images_per_prompt (`int`, *tuỳ chọn*, mặc định là 1) : Số lượng hình ảnh cần tạo cho mỗi prompt.

generator (`torch.Generator` hoặc `list[torch.Generator]`, *tùy chọn*) : Một torch generator để đảm bảo quá trình sinh là tất định.

latents (`torch.Tensor`, *tùy chọn*) : Các latents nhiễu đã được tạo trước.

prompt_embeds_qwen (`torch.Tensor`, *tùy chọn*) : Các embedding văn bản Qwen đã được tạo trước.

prompt_embeds_clip (`torch.Tensor`, *tùy chọn*) : Các embedding văn bản CLIP đã được tạo trước.

negative_prompt_embeds_qwen (`torch.Tensor`, *tùy chọn*) : Các embedding văn bản âm của Qwen đã được tạo trước.

negative_prompt_embeds_clip (`torch.Tensor`, *tùy chọn*) : Các embedding văn bản âm (negative text embeddings) của CLIP đã được tạo trước.

prompt_cu_seqlens (`torch.Tensor`, *tùy chọn*) : Độ dài chuỗi tích lũy đã tạo trước cho prompt tích cực của Qwen.

negative_prompt_cu_seqlens (`torch.Tensor`, *tùy chọn*) : Độ dài chuỗi tích lũy đã tạo trước cho prompt âm của Qwen.

output_type (`str`, *tùy chọn*, mặc định là `"pil"`) : Định dạng đầu ra của hình ảnh được tạo.

return_dict (`bool`, *tùy chọn*, mặc định là `True`) : Có trả về một `KandinskyImagePipelineOutput` hay không.

callback_on_step_end (`Callable`, `PipelineCallback`, `MultiPipelineCallbacks`, *tuỳ chọn*) : Một hàm được gọi vào cuối mỗi bước khử nhiễu.

callback_on_step_end_tensor_inputs (`List`, *tùy chọn*) : Danh sách các đầu vào tensor cho hàm `callback_on_step_end`.

max_sequence_length (`int`, mặc định là `512`) : Độ dài tối đa của chuỗi cho việc mã hóa văn bản.

**Trả về:** `~KandinskyImagePipelineOutput` hoặc `tuple`

Nếu `return_dict` là `True`, `KandinskyImagePipelineOutput` sẽ được trả về, ngược lại một `tuple` sẽ được trả về trong đó phần tử đầu tiên là một danh sách chứa các hình ảnh đã được tạo.

Hàm gọi call của pipeline để tạo ảnh từ văn bản.

Ví dụ:

```python
>>> import torch
>>> from diffusers import Kandinsky5T2IPipeline

>>> # Available models:
>>> # kandinskylab/Kandinsky-5.0-T2I-Lite-sft-Diffusers
>>> # kandinskylab/Kandinsky-5.0-T2I-Lite-pretrain-Diffusers

>>> model_id = "kandinskylab/Kandinsky-5.0-T2I-Lite-sft-Diffusers"
>>> pipe = Kandinsky5T2IPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
>>> pipe = pipe.to("cuda")

>>> prompt = "A cat and a dog baking a cake together in a kitchen."

>>> output = pipe(
...     prompt=prompt,
...     negative_prompt="",
...     height=1024,
...     width=1024,
...     num_inference_steps=50,
...     guidance_scale=3.5,
... ).frames[0]
```

#### check_inputs[[diffusers.Kandinsky5T2IPipeline.check_inputs]]

```python
check_inputs(prompt, negative_prompt, height, width, prompt_embeds_qwen = None, prompt_embeds_clip = None, negative_prompt_embeds_qwen = None, negative_prompt_embeds_clip = None, prompt_cu_seqlens = None, negative_prompt_cu_seqlens = None, callback_on_step_end_tensor_inputs = None, max_sequence_length = None)
```

[Nguồn](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky5/pipeline_kandinsky_t2i.py#L380)

**Tham số:**

prompt : Prompt đầu vào

negative_prompt : Prompt tiêu cực để điều hướng

height : Chiều cao của ảnh

width : Độ rộng ảnh

prompt_embeds_qwen : Nhúng prompt Qwen đã tính trước

prompt_embeds_clip : Nhúng prompt CLIP đã tính trước

negative_prompt_embeds_qwen : Nhúng (embeddings) của prompt tiêu cực Qwen đã được tính trước

negative_prompt_embeds_clip : Nhúng (embeddings) của prompt tiêu cực CLIP đã được tính trước

prompt_cu_seqlens : Độ dài chuỗi tích lũy đã tính trước cho Qwen positive prompt

negative_prompt_cu_seqlens : Độ dài chuỗi tích lũy đã tính trước cho Qwen negative prompt

callback_on_step_end_tensor_inputs : Đầu vào tensor của Callback

**Lỗi:** ``ValueError``

- ``ValueError`` -- Nếu các đầu vào không hợp lệ

Xác thực các tham số đầu vào cho pipeline.

#### encode_prompt[[diffusers.Kandinsky5T2IPipeline.encode_prompt]]

```python
encode_prompt(prompt: str | list[str], num_images_per_prompt: int = 1, max_sequence_length: int = 512, device: typing.Optional[torch.device] = None, dtype: typing.Optional[torch.dtype] = None)
```

[Nguồn](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky5/pipeline_kandinsky_t2i.py#L289)

**Tham số:**

prompt (`str` hoặc `list[str]`) : Prompt cần được mã hóa.

num_images_per_prompt (`int`, *tuỳ chọn*, mặc định là 1) : Số lượng hình ảnh cần tạo cho mỗi prompt.

max_sequence_length (`int`, *tuỳ chọn*, mặc định là 512) : Độ dài tối đa của chuỗi cho việc mã hóa văn bản. Phải nhỏ hơn 1024

device (`torch.device`, *tùy chọn*) : Thiết bị Torch.

dtype (`torch.dtype`, *tùy chọn*) : Torch dtype.

**Trả về:** tuple[torch.Tensor, torch.Tensor, torch.Tensor]

- Qwen text embeddings có shape (batch_size * num_images_per_prompt, sequence_length, embedding_dim)
- CLIP pooled embeddings có shape (batch_size * num_images_per_prompt, clip_embedding_dim)
- Chiều dài chuỗi tích lũy (`cu_seqlens`) cho Qwen embeddings có shape (batch_size * num_images_per_prompt + 1,)

Mã hóa một prompt duy nhất (tích cực hoặc tiêu cực) thành các trạng thái ẩn của text encoder.

Phương pháp này kết hợp các embedding từ cả hai bộ mã hóa văn bản Qwen2.5-VL và CLIP để tạo ra các biểu diễn văn bản toàn diện cho việc tạo hình ảnh.

#### prepare_latents[[diffusers.Kandinsky5T2IPipeline.prepare_latents]]

```python
prepare_latents(batch_size: int, num_channels_latents: int = 16, height: int = 1024, width: int = 1024, dtype: typing.Optional[torch.dtype] = None, device: typing.Optional[torch.device] = None, generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = None, latents: typing.Optional[torch.Tensor] = None)
```

[Nguồn](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky5/pipeline_kandinsky_t2i.py#L469)

**Tham số:**

batch_size (int) : Số lượng hình ảnh cần tạo

num_channels_latents (int) : Số lượng kênh trong không gian tiềm ẩn

height (int) : Chiều cao của hình ảnh được tạo

width (int) : Độ rộng của hình ảnh được tạo

dtype (torch.dtype) : Kiểu dữ liệu cho các biến ẩn

device (torch.device) : Thiết bị để tạo latents

generator (torch.Generator) : Bộ tạo số ngẫu nhiên

latents (torch.Tensor) : Các latents có sẵn để sử dụng

**Trả về:** `torch.Tensor`

Tensor latent đã được chuẩn bị

Chuẩn bị các biến tiềm ẩn ban đầu cho việc tạo ảnh từ văn bản.

Phương pháp này tạo ra các latent nhiễu ngẫu nhiên

## Kandinsky5I2IPipeline[[diffusers.Kandinsky5I2IPipeline]]

#### diffusers.Kandinsky5I2IPipeline[[diffusers.Kandinsky5I2IPipeline]]

```python
diffusers.Kandinsky5I2IPipeline(transformer: Kandinsky5Transformer3DModel, vae: AutoencoderKL, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2VLProcessor, text_encoder_2: CLIPTextModel, tokenizer_2: CLIPTokenizer, scheduler: FlowMatchEulerDiscreteScheduler)
```

[Nguồn](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky5/pipeline_kandinsky_i2i.py#L120)

**Tham số:**

transformer (`Kandinsky5Transformer3DModel`) : Transformer có điều kiện để khử nhiễu các latents ảnh đã mã hóa.

vae ([AutoencoderKL](/docs/diffusers/main/vi/api/models/autoencoderkl#diffusers.AutoencoderKL)) : Mô hình Variational Auto-Encoder [black-forest-labs/FLUX.1-dev (vae)](https://huggingface.co/black-forest-labs/FLUX.1-dev) để mã hóa và giải mã video sang và từ các biểu diễn ẩn.

text_encoder (`Qwen2_5_VLForConditionalGeneration`) : Bộ mã hóa văn bản đóng băng [Qwen2.5-VL](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct).

tokenizer (`AutoProcessor`) : Bộ phân từ cho Qwen2.5-VL.

text_encoder_2 (`CLIPTextModel`) : [CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel) được đóng băng, cụ thể là biến thể [clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14).

tokenizer_2 (`CLIPTokenizer`) : Tokenizer cho CLIP.

scheduler ([FlowMatchEulerDiscreteScheduler](/docs/diffusers/main/vi/api/schedulers/flow_match_euler_discrete#diffusers.FlowMatchEulerDiscreteScheduler)) : Một trình lập lịch được sử dụng kết hợp với `transformer` để khử nhiễu các latent ảnh đã mã hóa.

Pipeline để tạo ảnh từ ảnh sử dụng Kandinsky 5.0.

Mô hình này kế thừa từ [DiffusionPipeline](/docs/diffusers/main/vi/api/pipelines/overview#diffusers.DiffusionPipeline). Xem tài liệu của lớp cha để biết các phương thức chung được triển khai cho tất cả các pipeline (tải xuống, lưu, chạy trên một thiết bị cụ thể, v.v.).

#### __call__[[diffusers.Kandinsky5I2IPipeline.__call__]]

```python
__call__(image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]], prompt: str | list[str] = None, negative_prompt: str | list[str] | None = None, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, guidance_scale: float = 3.5, num_images_per_prompt: int | None = 1, generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = None, latents: typing.Optional[torch.Tensor] = None, prompt_embeds_qwen: typing.Optional[torch.Tensor] = None, prompt_embeds_clip: typing.Optional[torch.Tensor] = None, negative_prompt_embeds_qwen: typing.Optional[torch.Tensor] = None, negative_prompt_embeds_clip: typing.Optional[torch.Tensor] = None, prompt_cu_seqlens: typing.Optional[torch.Tensor] = None, negative_prompt_cu_seqlens: typing.Optional[torch.Tensor] = None, output_type: str | None = 'pil', return_dict: bool = True, callback_on_step_end: typing.Optional[typing.Callable[[int, int, NoneType], diffusers.callbacks.PipelineCallback | diffusers.callbacks.MultiPipelineCallbacks]] = None, callback_on_step_end_tensor_inputs: list = ['latents'], max_sequence_length: int = 1024)
```

[Nguồn](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky5/pipeline_kandinsky_i2i.py#L567)

**Tham số:**

image (`PipelineImageInput`) : Ảnh đầu vào để điều kiện cho quá trình tạo. Phải là một ảnh, một danh sách các ảnh hoặc một `torch.Tensor`.

prompt (`str` hoặc `list[str]`, *tùy chọn*) : Prompt hoặc các prompt để hướng dẫn việc tạo ảnh. Nếu không được định nghĩa, hãy truyền `prompt_embeds` thay thế.

negative_prompt (`str` hoặc `list[str]`, *tùy chọn*) : Prompt hoặc các prompt cần tránh trong quá trình tạo ảnh. Nếu không được định nghĩa, hãy truyền `negative_prompt_embeds` thay thế. Bỏ qua khi không sử dụng guidance (`guidance_scale` < `1`).

height (`int`) : Chiều cao của hình ảnh được tạo ra, tính bằng pixel.

width (`int`) : Độ rộng của hình ảnh được tạo ra, tính bằng pixel.

num_inference_steps (`int`, mặc định là `50`) : Số bước khử nhiễu.

guidance_scale (`float`, mặc định là `5.0`) : Thang độ dẫn hướng (guidance scale) như được định nghĩa trong classifier-free guidance.

num_images_per_prompt (`int`, *tuỳ chọn*, mặc định là 1) : Số lượng hình ảnh cần tạo cho mỗi prompt.

generator (`torch.Generator` hoặc `list[torch.Generator]`, *tùy chọn*) : Một torch generator để đảm bảo quá trình sinh là tất định.

latents (`torch.Tensor`, *tùy chọn*) : Các latents nhiễu đã được tạo trước.

prompt_embeds_qwen (`torch.Tensor`, *tùy chọn*) : Các embedding văn bản Qwen đã được tạo trước.

prompt_embeds_clip (`torch.Tensor`, *tùy chọn*) : Các embedding văn bản CLIP đã được tạo trước.

negative_prompt_embeds_qwen (`torch.Tensor`, *tùy chọn*) : Các embedding văn bản âm của Qwen đã được tạo trước.

negative_prompt_embeds_clip (`torch.Tensor`, *tùy chọn*) : Các embedding văn bản âm (negative text embeddings) của CLIP đã được tạo trước.

prompt_cu_seqlens (`torch.Tensor`, *tùy chọn*) : Độ dài chuỗi tích lũy đã tạo trước cho prompt tích cực của Qwen.

negative_prompt_cu_seqlens (`torch.Tensor`, *tùy chọn*) : Độ dài chuỗi tích lũy đã tạo trước cho prompt âm của Qwen.

output_type (`str`, *tùy chọn*, mặc định là `"pil"`) : Định dạng đầu ra của hình ảnh được tạo.

return_dict (`bool`, *tùy chọn*, mặc định là `True`) : Có trả về một `KandinskyImagePipelineOutput` hay không.

callback_on_step_end (`Callable`, `PipelineCallback`, `MultiPipelineCallbacks`, *tuỳ chọn*) : Một hàm được gọi vào cuối mỗi bước khử nhiễu.

callback_on_step_end_tensor_inputs (`List`, *tùy chọn*) : Danh sách các đầu vào tensor cho hàm `callback_on_step_end`.

max_sequence_length (`int`, mặc định là `1024`) : Độ dài tối đa của chuỗi cho việc mã hóa văn bản và hình ảnh của qwen. Phải nhỏ hơn 1024

**Trả về:** `~KandinskyImagePipelineOutput` hoặc `tuple`

Nếu `return_dict` là `True`, `KandinskyImagePipelineOutput` sẽ được trả về, ngược lại một `tuple` sẽ được trả về trong đó phần tử đầu tiên là một danh sách chứa các hình ảnh đã được tạo.

Hàm gọi call của pipeline để tạo ảnh từ ảnh.

Ví dụ:

```python
>>> import torch
>>> from diffusers import Kandinsky5I2IPipeline

>>> # Available models:
>>> # kandinskylab/Kandinsky-5.0-I2I-Lite-sft-Diffusers
>>> # kandinskylab/Kandinsky-5.0-I2I-Lite-pretrain-Diffusers

>>> model_id = "kandinskylab/Kandinsky-5.0-I2I-Lite-sft-Diffusers"
>>> pipe = Kandinsky5I2IPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
>>> pipe = pipe.to("cuda")

>>> prompt = "A cat and a dog baking a cake together in a kitchen."

>>> output = pipe(
...     prompt=prompt,
...     negative_prompt="",
...     height=1024,
...     width=1024,
...     num_inference_steps=50,
...     guidance_scale=3.5,
... ).frames[0]
```

#### check_inputs[[diffusers.Kandinsky5I2IPipeline.check_inputs]]

```python
check_inputs(prompt, negative_prompt, image, height, width, prompt_embeds_qwen = None, prompt_embeds_clip = None, negative_prompt_embeds_qwen = None, negative_prompt_embeds_clip = None, prompt_cu_seqlens = None, negative_prompt_cu_seqlens = None, callback_on_step_end_tensor_inputs = None, max_sequence_length = None)
```

[Nguồn](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky5/pipeline_kandinsky_i2i.py#L388)

**Tham số:**

prompt : Prompt đầu vào

negative_prompt : Prompt tiêu cực để điều hướng

image : Ảnh đầu vào để điều kiện

height : Chiều cao của ảnh

width : Độ rộng ảnh

prompt_embeds_qwen : Nhúng prompt Qwen đã tính trước

prompt_embeds_clip : Nhúng prompt CLIP đã tính trước

negative_prompt_embeds_qwen : Nhúng (embeddings) của prompt tiêu cực Qwen đã được tính trước

negative_prompt_embeds_clip : Nhúng (embeddings) của prompt tiêu cực CLIP đã được tính trước

prompt_cu_seqlens : Độ dài chuỗi tích lũy đã tính trước cho Qwen positive prompt

negative_prompt_cu_seqlens : Độ dài chuỗi tích lũy đã tính trước cho Qwen negative prompt

callback_on_step_end_tensor_inputs : Đầu vào tensor của Callback

**Lỗi:** ``ValueError``

- ``ValueError`` -- Nếu các đầu vào không hợp lệ

Xác thực các tham số đầu vào cho pipeline.

#### encode_prompt[[diffusers.Kandinsky5I2IPipeline.encode_prompt]]

```python
encode_prompt(prompt: str | list[str], image: Tensor, num_images_per_prompt: int = 1, max_sequence_length: int = 1024, device: typing.Optional[torch.device] = None, dtype: typing.Optional[torch.dtype] = None)
```

[Nguồn](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky5/pipeline_kandinsky_i2i.py#L295)

**Tham số:**

prompt (`str` hoặc `list[str]`) : Prompt cần được mã hóa.

num_images_per_prompt (`int`, *tuỳ chọn*, mặc định là 1) : Số lượng hình ảnh cần tạo cho mỗi prompt.

max_sequence_length (`int`, *tuỳ chọn*, mặc định là 1024) : Độ dài tối đa của chuỗi cho việc mã hóa văn bản. Phải nhỏ hơn 1024

device (`torch.device`, *tùy chọn*) : Thiết bị Torch.

dtype (`torch.dtype`, *tùy chọn*) : Torch dtype.

**Trả về:** tuple[torch.Tensor, torch.Tensor, torch.Tensor]

- Qwen text embeddings có shape (batch_size * num_images_per_prompt, sequence_length, embedding_dim)
- CLIP pooled embeddings có shape (batch_size * num_images_per_prompt, clip_embedding_dim)
- Chiều dài chuỗi tích lũy (`cu_seqlens`) cho Qwen embeddings có shape (batch_size * num_images_per_prompt + 1,)

Mã hóa một prompt duy nhất (tích cực hoặc tiêu cực) thành các trạng thái ẩn của text encoder.

Phương pháp này kết hợp các embedding từ cả hai bộ mã hóa văn bản Qwen2.5-VL và CLIP để tạo ra các biểu diễn văn bản toàn diện cho việc tạo hình ảnh.

#### prepare_latents[[diffusers.Kandinsky5I2IPipeline.prepare_latents]]

```python
prepare_latents(image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]], batch_size: int, num_channels_latents: int = 16, height: int = 1024, width: int = 1024, dtype: typing.Optional[torch.dtype] = None, device: typing.Optional[torch.device] = None, generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = None, latents: typing.Optional[torch.Tensor] = None)
```

[Nguồn](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/kandinsky5/pipeline_kandinsky_i2i.py#L482)

**Tham số:**

image (PipelineImageInput) : Ảnh đầu vào để điều kiện hóa quá trình tạo

batch_size (int) : Số lượng hình ảnh cần tạo

num_channels_latents (int) : Số lượng kênh trong không gian tiềm ẩn

height (int) : Chiều cao của hình ảnh được tạo

width (int) : Độ rộng của hình ảnh được tạo

dtype (torch.dtype) : Kiểu dữ liệu cho các biến ẩn

device (torch.device) : Thiết bị để tạo latents

generator (torch.Generator) : Bộ tạo số ngẫu nhiên

latents (torch.Tensor) : Các latents có sẵn để sử dụng

**Trả về:** `torch.Tensor`

Tensor latent đã chuẩn bị với ảnh được mã hóa

Chuẩn bị các biến tiềm ẩn ban đầu cho việc tạo ảnh từ ảnh.

Phương pháp này tạo ra các latent nhiễu ngẫu nhiên với ảnh đã được mã hóa,

## Trích dẫn
```bibtex
@misc{kandinsky2025,
    author = {Alexander Belykh and Alexander Varlamov and Alexey Letunovskiy and Anastasia Aliaskina and Anastasia Maltseva and Anastasiia Kargapoltseva and Andrey Shutkin and Anna Averchenkova and Anna Dmitrienko and Bulat Akhmatov and Denis Dimitrov and Denis Koposov and Denis Parkhomenko and Dmitrii and Ilya Vasiliev and Ivan Kirillov and Julia Agafonova and Kirill Chernyshev and Kormilitsyn Semen and Lev Novitskiy and Maria Kovaleva and Mikhail Mamaev and Mikhailov and Nikita Kiselev and Nikita Osterov and Nikolai Gerasimenko and Nikolai Vaulin and Olga Kim and Olga Vdovchenko and Polina Gavrilova and Polina Mikhailova and Tatiana Nikulina and Viacheslav Vasilev and Vladimir Arkhipkin and Vladimir Korviakov and Vladimir Polovnikov and Yury Kolabushin},
    title = {Kandinsky 5.0: A family of diffusion models for Video & Image generation},
    howpublished = {\url{https://github.com/kandinskylab/Kandinsky-5}},
    year = 2025
}
```

