Diffusers documentation

LTX2VideoDiffusionDecoderModel

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

LTX2VideoDiffusionDecoderModel

Lightricks 在 LTX-2.5 中引入的扩散视频解码器。邻域注意力阶段将潜在表示上采样为上下文体积,最终阶段则基于该上下文对像素进行去噪。

它是一个解码器,而非自编码器:编码功能仍由 AutoencoderKLLTX2Video 负责,该解码器直接消费其潜空间,因此潜变量在卷积解码器与此解码器之间可以互换。由于它本身是一个扩散模型,因此由 LTX2VideoDiffusionDecodePipeline 驱动,而不是作为管线的 vae 传入:先以 output_type="latent" 运行任意 LTX-2 管线,然后再进行解码。

import torch
from diffusers import LTX2Pipeline, LTX2VideoDiffusionDecodePipeline, LTX2VideoDiffusionDecoderModel

pipe = LTX2Pipeline.from_pretrained("Lightricks/LTX-2.5-Diffusers", dtype=torch.bfloat16).to("cuda")  # or "mps", "xpu", "cpu"
latents = pipe(prompt="a potter shaping a clay vase", output_type="latent").frames

decoder = LTX2VideoDiffusionDecoderModel.from_pretrained(
    "Lightricks/LTX-2.5-Diffusers", subfolder="diffusion_decoder", dtype=torch.bfloat16
).to("cuda")
decode_pipe = LTX2VideoDiffusionDecodePipeline(diffusion_decoder=decoder, scheduler=pipe.scheduler)

# `denormalize=False`: `output_type="latent"` already applied the latent statistics, so applying them
# again here would scale every channel by its std a second time.
# The decoder also draws the noise it denoises, so decoding is only reproducible with a generator.
video = decode_pipe(
    latents, generator=torch.Generator("cuda").manual_seed(0), denormalize=False
).frames[0]

vae 是解码流水线中的一个可选组件:仅当 denormalize=True 时才会查询其潜在统计量,而解码器自身已包含这些统计量,因此仅执行解码的工作流程无需加载第二个自编码器。

注意力后端

邻域注意力窗口以 BlockMask 的形式表示,因此解码器默认使用 flex 注意力后端,无需额外依赖。除非明确要求,否则 PyTorch 不会编译 flex_attention,而未编译时会实例化完整的分数矩阵——这在完整分辨率的序列长度下是不切实际的。对于这种情况,可以编译解码器,或者切换到 NATTEN 的内核,原始实现也使用了这些内核。处理器通过 kernels 包从 Hub 获取 NATTEN(shi-labs/natten),因此需要 pip install kernels,而不是本地构建 NATTEN:

from diffusers.models.autoencoders.ltx2_diffusion_decoder import LTX2VideoVaeNeighborhoodNattenProcessor

decoder.set_attn_processor(LTX2VideoVaeNeighborhoodNattenProcessor())

从 Hub 获取内核下载代码,因此当全局禁用远程代码(即 DIFFUSERS_DISABLE_REMOTE_CODE=true)时,处理器会抛出异常。

解码器中的每个注意力模块都是相同的邻域注意力(诸如核大小之类的逐阶段差异存在于模块上,而非处理器上),因此 set_attn_processor 会用一个共享实例替换所有这些模块。

将 backend(decoder.set_attention_backend(...))切换为 flex 以外的任何值都会引发错误:没有其他后端接受 BlockMask。请改用上述 NATTEN 处理器。

平铺

decoder.enable_tiling() 通过解码重叠的图块(tiles)并将它们混合回原图来工作,从而将峰值内存限制在图块大小而非视频大小。廉价的早期上采样阶段仍然会看到完整的潜在表示(latent)——只有主导解码内存的最后上采样阶段和扩散阶段按图块运行——因此分块(tiling)仅会在图块边界附近改变输出。由于扩散阶段对每个图块分别进行去噪,分块解码无法精确复现非分块的结果;默认的图块和重叠大小与参考实现一致。邻域注意力(Neighborhood attention)会拒绝任何小于其核(kernel)的网格,因此末尾的残余图块会被合并到其相邻图块中,而不是单独解码。

LTX2VideoDiffusionDecoderModel

类 diffusers.LTX2VideoDiffusionDecoderModel

< >

( out_channels: int = 3latent_channels: int = 128patch_size: int = 4scaling_factor: float = 1.0decoder_head_dim: int = 64decoder_stage_channels: tuple = (2048, 1024, 512, 512, 256)decoder_stage_depths: tuple = (4, 6, 4, 2, 8)decoder_stage_kernels: tuple = ((3, 7, 7), (3, 7, 7), (3, 5, 5), (3, 5, 5))decoder_upsample_strides: tuple = ((1, 2, 2), (2, 1, 1), (2, 2, 2), (2, 2, 2))decoder_upsample_channel_reductions: tuple = (2, 2, 1, 2)decoder_stage5_kernel: tuple = (11, 11, 11)decoder_t_emb_dim: int = 384decoder_timestep_scale_multiplier: float = 1000.0decoder_model_output_type: str = 'x0'decoder_num_inference_steps: int = 1spatial_compression_ratio: int = 32temporal_compression_ratio: int = 8 )

LTX-2.5 中引入的 LTX-2 扩散视频解码器。

这是一个解码器,而非自编码器:它没有编码器,也无法生成潜在表示。编码仍由 AutoencoderKLLTX2Video 负责,本模型直接沿用其潜在空间,因此卷积解码器与本模型之间的潜在表示可以互换使用。

它同样是一个扩散模型,而非确定性解码器——它以由潜在变量构建的上下文体积为条件对像素进行去噪——因此它由 LTX2VideoDiffusionDecodePipeline 驱动,而不是作为流水线的 vae 传入。

潜在统计量在此作为缓冲区传递,以便解码流水线能够进行反归一化,而无需仅为两个向量加载第二个自编码器。

该模型继承自 ModelMixin。请查阅其父类文档,了解为所有模型实现的通用方法(例如下载或保存)。

decode

< >

( z: Tensorgenerator: typing.Optional[torch.Generator] = Nonenum_inference_steps: int | None = Nonereturn_dict: bool = True )

解码一批潜在变量。

z 预期已经过反归一化处理(流水线会应用 latents_mean / latents_std),与 AutoencoderKLLTX2Video 保持一致。由于该解码器执行去噪操作,请传入 generator 以确保可复现性。

enable_tiling

< >

( tile_sample_min_height: int | None = Nonetile_sample_min_width: int | None = Nonetile_sample_min_num_frames: int | None = Nonetile_sample_stride_height: int | None = Nonetile_sample_stride_width: int | None = Nonetile_sample_stride_num_frames: int | None = None )

参数

  • tile_sample_min_height (int, 可选) — 单个解码瓦片的高度,以像素为单位。
  • tile_sample_min_width (int, 可选) — 单个解码瓦片的宽度,以像素为单位。
  • tile_sample_min_num_frames (int, 可选) — 单个解码 tile 的帧数。
  • tile_sample_stride_height (int, 可选) — 两个连续垂直瓦片顶部之间的像素距离;与 tile_sample_min_height 的差值即为混合重叠区域。
  • tile_sample_stride_width (int, 可选) — 两个连续水平瓦片左边缘之间的像素距离。
  • tile_sample_stride_num_frames (int, 可选) — 两个连续时间块起始位置之间的帧数距离。

启用分块解码。最后一个阶段之前的确定性上采样阶段始终处理完整的潜在表示(它们在低分辨率下运行,开销较小);而最后一个阶段以及第 5 阶段的扩散块——它们主导了解码内存——则在重叠的分块上运行,其接缝通过线性混合进行融合。

disable_tiling

< >

( )

禁用分块解码,恢复为一次性解码整个视频。

forward

< >

( z: Tensorgenerator: typing.Optional[torch.Generator] = Nonenum_inference_steps: int | None = Nonereturn_dict: bool = True ) → DecoderOutput or tuple

参数

  • z (torch.Tensor) — 形状为 (B, C, F, H, W) 的潜在变量,预期已经过反归一化处理(流水线会应用 latents_mean / latents_std),与 AutoencoderKLLTX2Video 相匹配。
  • generator (torch.Generator, 可选) — 此解码器执行去噪操作,因此请传入一个生成器以确保解码过程可复现。
  • num_inference_steps (int, 可选) — 去噪步数。默认值为解码器的 decoder_num_inference_steps 配置值。
  • return_dict (bool,可选,默认为 True) — 是否返回 DecoderOutput 而不是普通元组。

返回值

DecoderOutput 或 tuple

tiled_decode

< >

( z: Tensorgenerator: typing.Optional[torch.Generator] = Nonenum_inference_steps: int | None = None )

使用最后一个确定性阶段以及按块运行的扩散阶段,解码一批潜在变量。

Tiles 位于进入最后一个确定性阶段的网格上,在该阶段中,一个单元格映射到固定大小的输出像素块;tile_sample_* 尺寸会被转换到该网格,因此它们应为单元格尺寸的整数倍(在生产配置中,空间方向为 8 px,时间方向为 2 帧)。时间维度的 Tiles 遵循因果帧映射:包含 t=0 的 Tile 会丢弃时间上采样产生的重复首帧,且只有包含视频末尾的 Tile 会携带 NATTEN 边界填充。

Update on GitHub