Diffusers documentation

Kandinsky 5.0 视频

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Kandinsky 5.0 视频

Kandinsky 5.0 是一个用于视频和图像生成的扩散模型系列。

Kandinsky 5.0 Lite 系列轻量级视频生成模型(2B 参数),在同类开源模型中排名第一。其性能优于更大的模型,并且在开源生态系统中对俄语概念的理解能力最佳。

Kandinsky 5.0 Pro 系列包含大型高质量视频生成模型(19B 参数)。它支持 HD 高质量生成,并提供诸如 I2V 等更多生成格式。

该模型引入了几项关键创新:

  • 采用 Flow Matching 的 Latent diffusion pipeline,以提升训练稳定性
  • 以 Diffusion Transformer (DiT) 作为主要生成骨干网络,并通过交叉注意力机制与文本嵌入进行交互
  • 使用 Qwen2.5-VL 和 CLIP 进行双重文本编码,以实现全面的文本理解
  • 用于高效视频编码和解码的 HunyuanVideo 3D VAE
  • 用于高效长序列处理的稀疏注意力机制(NABLA)

原始代码库可在 kandinskylab/Kandinsky-5 中找到。

在 Hub 上查看 Kandinsky Lab 组织,以获取用于文本到视频生成的官方模型检查点,包括预训练、SFT、无 CFG 和蒸馏变体。

可用模型

Kandinsky 5.0 T2V Pro:

model_id描述用例
kandinskylab/Kandinsky-5.0-T2V-Pro-sft-5s-Diffusers5 秒 Text-to-Video Pro 模型高质量的文本到视频生成
kandinskylab/Kandinsky-5.0-I2V-Pro-sft-5s-Diffusers5 秒 Image-to-Video Pro 模型高质量图像到视频生成

Kandinsky 5.0 T2V Lite: | model_id | Description | Use Cases | |------------|-------------|-----------| | kandinskylab/Kandinsky-5.0-T2V-Lite-sft-5s-Diffusers | 5 秒监督微调模型 | 生成质量最高 | | kandinskylab/Kandinsky-5.0-T2V-Lite-sft-10s-Diffusers | 10 秒监督微调模型 | 生成质量最高 | | kandinskylab/Kandinsky-5.0-T2V-Lite-nocfg-5s-Diffusers | 5 秒 Classifier-Free Guidance 蒸馏模型 | 推理速度提升 2 倍 | | kandinskylab/Kandinsky-5.0-T2V-Lite-nocfg-10s-Diffusers | 10 秒 Classifier-Free Guidance 蒸馏模型 | 推理速度提升 2 倍 | | kandinskylab/Kandinsky-5.0-T2V-Lite-distilled16steps-5s-Diffusers | 5 秒 Diffusion 蒸馏至 16 步模型 | 推理速度提升 6 倍,质量损失极小 | | kandinskylab/Kandinsky-5.0-T2V-Lite-distilled16steps-10s-Diffusers | 10 秒 Diffusion 蒸馏至 16 步模型 | 推理速度提升 6 倍,质量损失极小 | | kandinskylab/Kandinsky-5.0-T2V-Lite-pretrain-5s-Diffusers | 5 秒基础预训练模型 | 研究与微调 | | kandinskylab/Kandinsky-5.0-T2V-Lite-pretrain-10s-Diffusers | 10 秒基础预训练模型 | 研究与微调 |

使用示例

基础文本到视频生成

Pro

⚠️ 警告!所有 Pro 模型都应使用 pipeline.enable_model_cpu_offload() 进行推理

import torch
from diffusers import Kandinsky5T2VPipeline
from diffusers.utils import export_to_video

# Load the pipeline
model_id = "kandinskylab/Kandinsky-5.0-T2V-Pro-sft-5s-Diffusers"
pipe = Kandinsky5T2VPipeline.from_pretrained(model_id, dtype=torch.bfloat16)

pipe = pipe.to("cuda")  # or "mps", "xpu", "cpu"
pipeline.transformer.set_attention_backend("flex")                            # <--- Set attention backend to Flex
pipeline.enable_model_cpu_offload()                                           # <--- Enable cpu offloading for single GPU inference
pipeline.transformer.compile(mode="max-autotune-no-cudagraphs", dynamic=True) # <--- Compile with max-autotune-no-cudagraphs

# Generate video
prompt = "A cat and a dog baking a cake together in a kitchen."
negative_prompt = "Static, 2D cartoon, cartoon, 2d animation, paintings, images, worst quality, low quality, ugly, deformed, walking backwards"

output = pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    height=768,
    width=1024,
    num_frames=121,  # ~5 seconds at 24fps
    num_inference_steps=50,
    guidance_scale=5.0,
).frames[0]

export_to_video(output, "output.mp4", fps=24, quality=9)

Lite

import torch
from diffusers import Kandinsky5T2VPipeline
from diffusers.utils import export_to_video

# Load the pipeline
model_id = "kandinskylab/Kandinsky-5.0-T2V-Lite-sft-5s-Diffusers"
pipe = Kandinsky5T2VPipeline.from_pretrained(model_id, dtype=torch.bfloat16)
pipe = pipe.to("cuda")  # or "mps", "xpu", "cpu"

# Generate video
prompt = "A cat and a dog baking a cake together in a kitchen."
negative_prompt = "Static, 2D cartoon, cartoon, 2d animation, paintings, images, worst quality, low quality, ugly, deformed, walking backwards"

output = pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    height=512,
    width=768,
    num_frames=121,  # ~5 seconds at 24fps
    num_inference_steps=50,
    guidance_scale=5.0,
).frames[0]

export_to_video(output, "output.mp4", fps=24, quality=9)

10 秒模型

⚠️ 警告!所有 10 秒模型都应配合 Flex attention 和 max-autotune-no-cudagraphs 编译使用:

pipe = Kandinsky5T2VPipeline.from_pretrained(
    "kandinskylab/Kandinsky-5.0-T2V-Lite-sft-10s-Diffusers", 
    dtype=torch.bfloat16
)
pipe = pipe.to("cuda")  # or "mps", "xpu", "cpu"

pipe.transformer.set_attention_backend(
    "flex"
)                                       # <--- Set attention backend to Flex
pipe.transformer.compile(
    mode="max-autotune-no-cudagraphs", 
    dynamic=True
)                                       # <--- Compile with max-autotune-no-cudagraphs

prompt = "A cat and a dog baking a cake together in a kitchen."
negative_prompt = "Static, 2D cartoon, cartoon, 2d animation, paintings, images, worst quality, low quality, ugly, deformed, walking backwards"

output = pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    height=512,
    width=768,
    num_frames=241,
    num_inference_steps=50,
    guidance_scale=5.0,
).frames[0]

export_to_video(output, "output.mp4", fps=24, quality=9)

Diffusion Distilled 模型

⚠️ 警告!所有 nocfg 和 diffusion distilled 模型都应使用无 CFG 的方式进行推理(guidance_scale=1.0):

model_id = "kandinskylab/Kandinsky-5.0-T2V-Lite-distilled16steps-5s-Diffusers"
pipe = Kandinsky5T2VPipeline.from_pretrained(model_id, dtype=torch.bfloat16)
pipe = pipe.to("cuda")  # or "mps", "xpu", "cpu"

output = pipe(
    prompt="A beautiful sunset over mountains",
    num_inference_steps=16,  # <--- Model is distilled in 16 steps
    guidance_scale=1.0,      # <--- no CFG
).frames[0]

export_to_video(output, "output.mp4", fps=24, quality=9)

基础图像到视频生成

⚠️ 警告!所有 Pro 模型都应使用 pipeline.enable_model_cpu_offload() 进行推理

import torch
from diffusers import Kandinsky5T2VPipeline
from diffusers.utils import export_to_video

# Load the pipeline
model_id = "kandinskylab/Kandinsky-5.0-I2V-Pro-sft-5s-Diffusers"
pipe = Kandinsky5T2VPipeline.from_pretrained(model_id, dtype=torch.bfloat16)

pipe = pipe.to("cuda")  # or "mps", "xpu", "cpu"
pipeline.transformer.set_attention_backend("flex")                            # <--- Set attention backend to Flex
pipeline.enable_model_cpu_offload()                                           # <--- Enable cpu offloading for single GPU inference
pipeline.transformer.compile(mode="max-autotune-no-cudagraphs", dynamic=True) # <--- Compile with max-autotune-no-cudagraphs

# Generate video
image = load_image(
    "https://huggingface.co/kandinsky-community/kandinsky-3/resolve/main/assets/title.jpg?download=true"
)
height = 896
width = 896
image = image.resize((width, height))

prompt = "An funny furry creture smiles happily and holds a sign that says 'Kandinsky'"
negative_prompt = ""

output = pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    height=height,
    width=width,
    num_frames=121,  # ~5 seconds at 24fps
    num_inference_steps=50,
    guidance_scale=5.0,
).frames[0]

export_to_video(output, "output.mp4", fps=24, quality=9)

Kandinsky 5.0 Pro 并排评估

imageimage
与 Veo 3 的对比与 Veo 3 fast 的对比
imageimage
与 Wan 2.2 A14B 文本到视频模式的比较与 Wan 2.2 A14B 图生视频模式的对比

Kandinsky 5.0 Lite 并排评估

评估基于 Movie Gen benchmark 中扩展后的提示词,这些提示词位于 benchmark/moviegen_bench.csv 文件的 expanded_prompt 列中。

Kandinsky 5.0 Lite Distill 并排评估

Kandinsky5T2VPipeline

class diffusers.Kandinsky5T2VPipeline

< >

( transformer: Kandinsky5Transformer3DModelvae: AutoencoderKLHunyuanVideotext_encoder: Qwen2_5_VLForConditionalGenerationtokenizer: Qwen2VLProcessortext_encoder_2: CLIPTextModeltokenizer_2: CLIPTokenizerscheduler: FlowMatchEulerDiscreteScheduler )

参数

  • transformer (Kandinsky5Transformer3DModel) — 用于对编码后的视频潜变量进行去噪的条件 Transformer。
  • vae (AutoencoderKLHunyuanVideo) — 变分自编码器模型 hunyuanvideo-community/HunyuanVideo (vae),用于将视频编码为潜在表示,以及从潜在表示解码为视频。
  • text_encoder (Qwen2_5_VLForConditionalGeneration) — 冻结的文本编码器 Qwen2.5-VL。
  • tokenizer(AutoProcessor)—— Qwen2.5-VL 的分词器。
  • text_encoder_2 (CLIPTextModel) — 冻结的 CLIP, 具体为 clip-vit-large-patch14 变体。
  • tokenizer_2 (CLIPTokenizer) — CLIP 的分词器。
  • scheduler (FlowMatchEulerDiscreteScheduler) — 用于与 transformer 配合,对编码后的视频潜变量进行去噪的调度器。

使用 Kandinsky 5.0 进行文本到视频生成的 Pipeline。

该模型继承自 DiffusionPipeline。有关为所有 pipeline 实现的通用方法(下载、保存、在特定设备上运行等),请参阅父类文档。

__call__

< >

( prompt: str | list[str] = Nonenegative_prompt: str | list[str] | None = Noneheight: int = 512width: int = 768num_frames: int = 121num_inference_steps: int = 50guidance_scale: float = 5.0num_videos_per_prompt: int | None = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Noneprompt_embeds_qwen: typing.Optional[torch.Tensor] = Noneprompt_embeds_clip: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds_qwen: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds_clip: typing.Optional[torch.Tensor] = Noneprompt_cu_seqlens: typing.Optional[torch.Tensor] = Nonenegative_prompt_cu_seqlens: typing.Optional[torch.Tensor] = Noneoutput_type: str | None = 'pil'return_dict: bool = Truecallback_on_step_end: typing.Union[typing.Callable[[int, int], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = Nonecallback_on_step_end_tensor_inputs: list = ['latents']max_sequence_length: int = 512 ) → ~KandinskyPipelineOutput or tuple

参数

  • prompt (str 或 list[str],可选) — 用于引导视频生成的提示词或提示词列表。如果未定义,请改为传入 prompt_embeds。
  • negative_prompt (str 或 list[str],可选) — 视频生成过程中需要避免的提示词。如果未定义,请改为传入 negative_prompt_embeds。在未使用引导(guidance_scale < 1)时将被忽略。
  • height (int,默认为 512) — 生成视频的高度(以像素为单位)。
  • width (int,默认为 768) — 生成视频的宽度(以像素为单位)。
  • num_frames (int,默认为 25) — 生成视频的帧数。
  • num_inference_steps (int,默认为 50) — 去噪步数。
  • guidance_scale (float,默认为 5.0) — 无分类器引导中定义的引导尺度。
  • num_videos_per_prompt (int,可选,默认为 1) — 每个提示词生成的视频数量。
  • generator (torch.Generator 或 list[torch.Generator],可选) — 用于使生成过程确定性的 torch 生成器。
  • latents (torch.Tensor, 可选) — 预生成的含噪潜变量。
  • prompt_embeds_qwen (torch.Tensor, 可选) — 由 Qwen 文本编码器预生成的文本嵌入。
  • prompt_embeds_clip (torch.Tensor, 可选) — 由 CLIP 文本编码器预生成的文本嵌入。
  • negative_prompt_embeds_qwen (torch.Tensor,可选) — 由 Qwen 文本编码器预生成的负向文本嵌入。
  • negative_prompt_embeds_clip (torch.Tensor, 可选) — 由 CLIP 文本编码器预生成的负向文本嵌入。
  • prompt_cu_seqlens (torch.Tensor, 可选) — Qwen 提示嵌入的累积序列长度,用于变长注意力。
  • negative_prompt_cu_seqlens (torch.Tensor, 可选) — Qwen 负提示嵌入的累积序列长度,用于变长注意力。
  • output_type (str,可选,默认为 "pil") — 生成视频的输出格式。
  • return_dict (bool,可选,默认为 True) — 是否返回 KandinskyPipelineOutput。
  • callback_on_step_end (Callable, PipelineCallback, MultiPipelineCallbacks, 可选) — 在每个去噪步骤结束时调用的函数。
  • callback_on_step_end_tensor_inputs (list, 可选) — callback_on_step_end 函数的张量输入列表。
  • max_sequence_length (int,默认为 512) — 文本编码的最大序列长度。

返回值

~KandinskyPipelineOutput 或 tuple

如果 return_dict 为 True,则返回 KandinskyPipelineOutput,否则返回一个 tuple,其中第一个元素是包含生成图像的列表。

用于生成的 pipeline 的调用函数。

示例:

>>> import torch
>>> from diffusers import Kandinsky5T2VPipeline
>>> from diffusers.utils import export_to_video

>>> # Available models:
>>> # kandinskylab/Kandinsky-5.0-T2V-Pro-sft-5s-Diffusers
>>> # kandinskylab/Kandinsky-5.0-T2V-Lite-sft-5s-Diffusers
>>> # kandinskylab/Kandinsky-5.0-T2V-Lite-nocfg-5s-Diffusers
>>> # kandinskylab/Kandinsky-5.0-T2V-Lite-distilled16steps-5s-Diffusers
>>> # kandinskylab/Kandinsky-5.0-T2V-Lite-pretrain-5s-Diffusers
>>> # kandinskylab/Kandinsky-5.0-T2V-Lite-sft-10s-Diffusers
>>> # kandinskylab/Kandinsky-5.0-T2V-Lite-nocfg-10s-Diffusers
>>> # kandinskylab/Kandinsky-5.0-T2V-Lite-distilled16steps-10s-Diffusers
>>> # kandinskylab/Kandinsky-5.0-T2V-Lite-pretrain-10s-Diffusers

>>> model_id = "kandinskylab/Kandinsky-5.0-T2V-Lite-sft-5s-Diffusers"
>>> pipe = Kandinsky5T2VPipeline.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."
>>> negative_prompt = "Static, 2D cartoon, cartoon, 2d animation, paintings, images, worst quality, low quality, ugly, deformed, walking backwards"

>>> output = pipe(
...     prompt=prompt,
...     negative_prompt=negative_prompt,
...     height=512,
...     width=768,
...     num_frames=121,
...     num_inference_steps=50,
...     guidance_scale=5.0,
... ).frames[0]

>>> export_to_video(output, "output.mp4", fps=24, quality=9)

check_inputs

< >

( promptnegative_promptheightwidthprompt_embeds_qwen = Noneprompt_embeds_clip = Nonenegative_prompt_embeds_qwen = Nonenegative_prompt_embeds_clip = Noneprompt_cu_seqlens = Nonenegative_prompt_cu_seqlens = Nonecallback_on_step_end_tensor_inputs = Nonemax_sequence_length = None )

参数

  • prompt — 输入提示
  • negative_prompt — 用于引导的负面提示
  • height — 视频高度
  • width — 视频宽度
  • prompt_embeds_qwen — 预计算的 Qwen 提示嵌入
  • prompt_embeds_clip — 预计算的 CLIP 提示嵌入
  • negative_prompt_embeds_qwen — 预计算的 Qwen 负提示嵌入
  • negative_prompt_embeds_clip — 预计算的 CLIP 负提示嵌入
  • prompt_cu_seqlens — Qwen 正向提示的预计算累积序列长度
  • negative_prompt_cu_seqlens — Qwen 负面提示的预计算累积序列长度
  • callback_on_step_end_tensor_inputs — 回调张量输入

引发

ValueError

  • ValueError — 如果输入无效

验证 pipeline 的输入参数。

encode_prompt

< >

( prompt: str | list[str]num_videos_per_prompt: int = 1max_sequence_length: int = 512device: typing.Optional[torch.device] = Nonedtype: typing.Optional[torch.dtype] = None ) → tuple[torch.Tensor, torch.Tensor, torch.Tensor]

参数

  • prompt (str 或 list[str]) — 要编码的提示。
  • num_videos_per_prompt (int,可选,默认为 1) — 每个提示词生成的视频数量。
  • max_sequence_length (int,可选,默认为 512) — 文本编码的最大序列长度。
  • device (torch.device,可选) — Torch 设备。
  • dtype (torch.dtype,可选) — Torch 数据类型。

返回值

tuple[torch.Tensor, torch.Tensor, torch.Tensor]

  • 形状为 (batch_size * num_videos_per_prompt, sequence_length, embedding_dim) 的 Qwen 文本嵌入
  • 形状为 (batch_size * num_videos_per_prompt, clip_embedding_dim) 的 CLIP 池化嵌入
  • Qwen 嵌入的累积序列长度(cu_seqlens),形状为 (batch_size * num_videos_per_prompt + 1,)

将单个提示(正向或负向)编码为文本编码器的隐藏状态。

该方法结合了 Qwen2.5-VL 和 CLIP 文本编码器的嵌入,以创建用于视频生成的全面文本表示。

fast_sta_nabla

< >

( T: intH: intW: intwT: int = 3wH: int = 3wW: int = 3device = 'cuda' ) → torch.Tensor

参数

  • T (int) — 时间帧数
  • H (int) — 潜在空间中的高度
  • W (int) — 潜在空间中的宽度
  • wT (int) — 时间注意力窗口大小
  • wH (int) — 高度注意力窗口大小
  • wW (int) — 宽度注意力窗口大小
  • device (str) — 用于创建张量的设备

返回值

torch.Tensor

形状为 (THW, THW) 的稀疏注意力掩码

创建稀疏时间注意力(STA)掩码,以实现高效的视频生成。

此方法生成一个掩码,将注意力限制在邻近的帧和空间位置上,从而降低视频生成的计算复杂度。

get_sparse_params

< >

( sampledevice ) → Dict

参数

  • sample (torch.Tensor) — 输入样本张量
  • device (torch.device) — 用于放置张量的设备

返回值

Dict

包含稀疏注意力参数的字典

根据样本维度为 Transformer 生成稀疏注意力参数。

此方法计算在 transformer 模型中高效处理视频所需的稀疏注意力配置。

prepare_latents

< >

( batch_size: intnum_channels_latents: int = 16height: int = 480width: int = 832num_frames: int = 81dtype: typing.Optional[torch.dtype] = Nonedevice: typing.Optional[torch.device] = Nonegenerator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = None ) → torch.Tensor

参数

  • batch_size (int) — 要生成的视频数量
  • num_channels_latents (int) — 潜在空间中的通道数
  • height (int) — 生成视频的高度
  • width (int) — 生成视频的宽度
  • num_frames (int) — 视频中的帧数
  • dtype (torch.dtype) — 潜变量的数据类型
  • device (torch.device) — 用于创建潜变量的设备
  • generator (torch.Generator) — 随机数生成器
  • latents (torch.Tensor) — 要使用的现有潜在变量

返回值

torch.Tensor

准备好的潜在张量

为视频生成准备初始潜在变量。

此方法会创建随机噪声潜变量,或使用提供的潜变量作为去噪过程的起点。

Kandinsky5I2VPipeline

class diffusers.Kandinsky5I2VPipeline

< >

( transformer: Kandinsky5Transformer3DModelvae: AutoencoderKLHunyuanVideotext_encoder: Qwen2_5_VLForConditionalGenerationtokenizer: Qwen2VLProcessortext_encoder_2: CLIPTextModeltokenizer_2: CLIPTokenizerscheduler: FlowMatchEulerDiscreteScheduler )

参数

  • transformer (Kandinsky5Transformer3DModel) — 用于对编码后的视频潜变量进行去噪的条件 Transformer。
  • vae (AutoencoderKLHunyuanVideo) — 变分自编码器模型 hunyuanvideo-community/HunyuanVideo (vae),用于将视频编码为潜在表示,以及从潜在表示解码为视频。
  • text_encoder (Qwen2_5_VLForConditionalGeneration) — 冻结的文本编码器 Qwen2.5-VL。
  • tokenizer(AutoProcessor)—— Qwen2.5-VL 的分词器。
  • text_encoder_2 (CLIPTextModel) — 冻结的 CLIP, 具体为 clip-vit-large-patch14 变体。
  • tokenizer_2 (CLIPTokenizer) — CLIP 的分词器。
  • scheduler (FlowMatchEulerDiscreteScheduler) — 用于与 transformer 配合,对编码后的视频潜变量进行去噪的调度器。

使用 Kandinsky 5.0 进行图像到视频生成的 Pipeline。

该模型继承自 DiffusionPipeline。有关为所有 pipeline 实现的通用方法(下载、保存、在特定设备上运行等),请参阅父类文档。

__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] = Nonenegative_prompt: str | list[str] | None = Noneheight: int = 512width: int = 768num_frames: int = 121num_inference_steps: int = 50guidance_scale: float = 5.0num_videos_per_prompt: int | None = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Noneprompt_embeds_qwen: typing.Optional[torch.Tensor] = Noneprompt_embeds_clip: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds_qwen: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds_clip: typing.Optional[torch.Tensor] = Noneprompt_cu_seqlens: typing.Optional[torch.Tensor] = Nonenegative_prompt_cu_seqlens: typing.Optional[torch.Tensor] = Noneoutput_type: str | None = 'pil'return_dict: bool = Truecallback_on_step_end: typing.Optional[typing.Callable[[int, int, NoneType], diffusers.callbacks.PipelineCallback | diffusers.callbacks.MultiPipelineCallbacks]] = Nonecallback_on_step_end_tensor_inputs: list = ['latents']max_sequence_length: int = 512 ) → ~KandinskyPipelineOutput or tuple

参数

  • image (PipelineImageInput) — 用于条件生成的输入图像。必须为图像、图像列表或 torch.Tensor。
  • prompt (str 或 list[str],可选) — 用于引导视频生成的提示词或提示词列表。如果未定义,请改为传入 prompt_embeds。
  • negative_prompt (str 或 list[str],可选) — 视频生成过程中需要避免的提示词。如果未定义,请改为传入 negative_prompt_embeds。在未使用引导(guidance_scale < 1)时将被忽略。
  • height (int,默认为 512) — 生成视频的高度(以像素为单位)。
  • width (int,默认为 768) — 生成视频的宽度(以像素为单位)。
  • num_frames (int,默认为 121) — 生成视频的帧数。
  • num_inference_steps (int,默认为 50) — 去噪步数。
  • guidance_scale (float,默认为 5.0) — 无分类器引导中定义的引导尺度。
  • num_videos_per_prompt (int,可选,默认为 1) — 每个提示词生成的视频数量。
  • generator (torch.Generator 或 list[torch.Generator],可选) — 用于使生成过程确定性的 torch 生成器。
  • latents (torch.Tensor, 可选) — 预生成的含噪潜变量。
  • prompt_embeds_qwen (torch.Tensor, 可选) — 预生成的 Qwen 文本嵌入。
  • prompt_embeds_clip (torch.Tensor, 可选) — 预生成的 CLIP 文本嵌入。
  • negative_prompt_embeds_qwen (torch.Tensor, 可选) — 预生成的 Qwen 负向文本嵌入。
  • negative_prompt_embeds_clip (torch.Tensor, 可选) — 预生成的 CLIP 负向文本嵌入。
  • prompt_cu_seqlens (torch.Tensor,可选) — 为 Qwen 正向提示词预生成的累积序列长度。
  • negative_prompt_cu_seqlens (torch.Tensor, 可选) — 为 Qwen 负面提示词预生成的累积序列长度。
  • output_type (str,可选,默认为 "pil") — 生成视频的输出格式。
  • return_dict (bool,可选,默认为 True) — 是否返回 KandinskyPipelineOutput。
  • callback_on_step_end (Callable, PipelineCallback, MultiPipelineCallbacks, 可选) — 在每个去噪步骤结束时调用的函数。
  • callback_on_step_end_tensor_inputs (List, 可选) — callback_on_step_end 函数的张量输入列表。
  • max_sequence_length (int,默认为 512) — 文本编码的最大序列长度。

返回值

~KandinskyPipelineOutput 或 tuple

如果 return_dict 为 True,则返回 KandinskyPipelineOutput,否则返回一个 tuple,其中第一个元素是包含生成视频的列表。

用于图像到视频生成的 pipeline 的调用函数。

示例:

>>> import torch
>>> from diffusers import Kandinsky5I2VPipeline
>>> from diffusers.utils import export_to_video, load_image

>>> # Available models:
>>> # kandinskylab/Kandinsky-5.0-I2V-Pro-sft-5s-Diffusers

>>> model_id = "kandinskylab/Kandinsky-5.0-I2V-Pro-sft-5s-Diffusers"
>>> pipe = Kandinsky5I2VPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
>>> pipe = pipe.to("cuda")

>>> image = load_image(
...     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"
... )
>>> prompt = "An astronaut floating in space with Earth in the background, cinematic shot"
>>> negative_prompt = "Static, 2D cartoon, cartoon, 2d animation, paintings, images, worst quality, low quality, ugly, deformed, walking backwards"

>>> output = pipe(
...     image=image,
...     prompt=prompt,
...     negative_prompt=negative_prompt,
...     height=512,
...     width=768,
...     num_frames=121,
...     num_inference_steps=50,
...     guidance_scale=5.0,
... ).frames[0]

>>> export_to_video(output, "output.mp4", fps=24, quality=9)

check_inputs

< >

( promptnegative_promptimageheightwidthprompt_embeds_qwen = Noneprompt_embeds_clip = Nonenegative_prompt_embeds_qwen = Nonenegative_prompt_embeds_clip = Noneprompt_cu_seqlens = Nonenegative_prompt_cu_seqlens = Nonecallback_on_step_end_tensor_inputs = Nonemax_sequence_length = None )

参数

  • prompt — 输入提示
  • negative_prompt — 用于引导的负面提示
  • image — 用于条件控制的输入图像
  • height — 视频高度
  • width — 视频宽度
  • prompt_embeds_qwen — 预计算的 Qwen 提示嵌入
  • prompt_embeds_clip — 预计算的 CLIP 提示嵌入
  • negative_prompt_embeds_qwen — 预计算的 Qwen 负提示嵌入
  • negative_prompt_embeds_clip — 预计算的 CLIP 负提示嵌入
  • prompt_cu_seqlens — Qwen 正向提示的预计算累积序列长度
  • negative_prompt_cu_seqlens — Qwen 负面提示的预计算累积序列长度
  • callback_on_step_end_tensor_inputs — 回调张量输入

引发

ValueError

  • ValueError — 如果输入无效

验证 pipeline 的输入参数。

encode_prompt

< >

( prompt: str | list[str]num_videos_per_prompt: int = 1max_sequence_length: int = 512device: typing.Optional[torch.device] = Nonedtype: typing.Optional[torch.dtype] = None ) → tuple[torch.Tensor, torch.Tensor, torch.Tensor]

参数

  • prompt (str 或 list[str]) — 要编码的提示。
  • num_videos_per_prompt (int,可选,默认为 1) — 每个提示词生成的视频数量。
  • max_sequence_length (int,可选,默认为 512) — 文本编码的最大序列长度。
  • device (torch.device,可选) — Torch 设备。
  • dtype (torch.dtype,可选) — Torch 数据类型。

返回值

tuple[torch.Tensor, torch.Tensor, torch.Tensor]

  • 形状为 (batch_size * num_videos_per_prompt, sequence_length, embedding_dim) 的 Qwen 文本嵌入
  • 形状为 (batch_size * num_videos_per_prompt, clip_embedding_dim) 的 CLIP 池化嵌入
  • Qwen 嵌入的累积序列长度(cu_seqlens),形状为 (batch_size * num_videos_per_prompt + 1,)

将单个提示(正向或负向)编码为文本编码器的隐藏状态。

该方法结合了 Qwen2.5-VL 和 CLIP 文本编码器的嵌入,以创建用于视频生成的全面文本表示。

fast_sta_nabla

< >

( T: intH: intW: intwT: int = 3wH: int = 3wW: int = 3device = 'cuda' ) → torch.Tensor

参数

  • T (int) — 时间帧数
  • H (int) — 潜在空间中的高度
  • W (int) — 潜在空间中的宽度
  • wT (int) — 时间注意力窗口大小
  • wH (int) — 高度注意力窗口大小
  • wW (int) — 宽度注意力窗口大小
  • device (str) — 用于创建张量的设备

返回值

torch.Tensor

形状为 (THW, THW) 的稀疏注意力掩码

创建稀疏时间注意力(STA)掩码,以实现高效的视频生成。

此方法生成一个掩码,将注意力限制在邻近的帧和空间位置上,从而降低视频生成的计算复杂度。

get_sparse_params

< >

( sampledevice ) → Dict

参数

  • sample (torch.Tensor) — 输入样本张量
  • device (torch.device) — 用于放置张量的设备

返回值

Dict

包含稀疏注意力参数的字典

根据样本维度为 Transformer 生成稀疏注意力参数。

此方法计算在 transformer 模型中高效处理视频所需的稀疏注意力配置。

prepare_latents

< >

( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]]batch_size: intnum_channels_latents: int = 16height: int = 480width: int = 832num_frames: int = 81dtype: typing.Optional[torch.dtype] = Nonedevice: typing.Optional[torch.device] = Nonegenerator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = None ) → torch.Tensor

参数

  • image (PipelineImageInput) — 用于条件生成的输入图像
  • batch_size (int) — 要生成的视频数量
  • num_channels_latents (int) — 潜在空间中的通道数
  • height (int) — 生成视频的高度
  • width (int) — 生成视频的宽度
  • num_frames (int) — 视频中的帧数
  • dtype (torch.dtype) — 潜变量的数据类型
  • device (torch.device) — 用于创建潜变量的设备
  • generator (torch.Generator) — 随机数生成器
  • latents (torch.Tensor) — 要使用的现有潜在变量

返回值

torch.Tensor

以编码图像作为第一帧的预准备潜在张量

为图像到视频生成准备初始潜在变量。

此方法会为除第一帧之外的所有帧创建随机噪声潜变量,第一帧则被替换为编码后的输入图像。

引用

@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
}
Update on GitHub