Diffusers documentation

CogVideoXTransformer3DModel

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

CogVideoXTransformer3DModel

A Diffusion Transformer model for 3D data from CogVideoX was introduced in CogVideoX: Text-to-Video Diffusion Models with An Expert Transformer by Tsinghua University & ZhipuAI.

可以使用以下代码片段加载该模型。

from diffusers import CogVideoXTransformer3DModel

transformer = CogVideoXTransformer3DModel.from_pretrained("THUDM/CogVideoX-2b", subfolder="transformer", dtype=torch.float16).to("cuda")  # or "mps", "xpu", "cpu"

CogVideoXTransformer3DModel

class diffusers.CogVideoXTransformer3DModel

< >

( num_attention_heads: int = 30attention_head_dim: int = 64in_channels: int = 16out_channels: int | None = 16flip_sin_to_cos: bool = Truefreq_shift: int = 0time_embed_dim: int = 512ofs_embed_dim: int | None = Nonetext_embed_dim: int = 4096num_layers: int = 30dropout: float = 0.0attention_bias: bool = Truesample_width: int = 90sample_height: int = 60sample_frames: int = 49patch_size: int = 2patch_size_t: int | None = Nonetemporal_compression_ratio: int = 4max_text_seq_length: int = 226activation_fn: str = 'gelu-approximate'timestep_activation_fn: str = 'silu'norm_elementwise_affine: bool = Truenorm_eps: float = 1e-05spatial_interpolation_scale: float = 1.875temporal_interpolation_scale: float = 1.0use_rotary_positional_embeddings: bool = Falseuse_learned_positional_embeddings: bool = Falsepatch_bias: bool = True )

参数

  • num_attention_heads (int,默认为 30) — 用于多头注意力的注意力头数量。
  • attention_head_dim (int,默认为 64) — 每个头中的通道数。
  • in_channels (int,默认为 16) — 输入数据的通道数。
  • out_channels (int,可选,默认为 16) — 输出中的通道数。
  • flip_sin_to_cos (bool,默认为 True) — 是否将时间嵌入中的 sin 翻转为 cos。
  • time_embed_dim (int,默认为 512) — 时间步嵌入的输出维度。
  • ofs_embed_dim (int,默认为 512) — CogVideoX-5b-I2B 1.5 版本中使用的 “ofs” 嵌入的输出维度
  • text_embed_dim (int,默认为 4096) — 来自文本编码器的文本嵌入的输入维度。
  • num_layers (int,默认为 30) — 要使用的 Transformer 块层数。
  • dropout (float,默认为 0.0) — 要使用的 dropout 概率。
  • attention_bias (bool,默认为 True) — 是否在注意力投影层中使用偏置。
  • sample_width (int,默认为 90) — 输入潜变量的宽度。
  • sample_height (int,默认为 60) — 输入潜变量的高度。
  • sample_frames (int,默认为 49) — 输入潜变量中的帧数。请注意,由于 CogVideoX 在其默认和推荐设置中一次处理 13 个潜变量帧,因此该参数被错误地初始化为 49 而不是 13。但为了确保向后兼容性,无法将其更改为正确的值。若要创建一个具有 K 个潜变量帧的 transformer,此处应传入的正确值为:((K - 1) * temporal_compression_ratio + 1)。
  • patch_size (int,默认为 2) — 用于 patch 嵌入层的 patch 大小。
  • temporal_compression_ratio (int,默认为 4) — 时间维度上的压缩比。请参阅 sample_frames 的文档。
  • max_text_seq_length (int,默认为 226) — 输入文本嵌入的最大序列长度。
  • activation_fn (str,默认为 "gelu-approximate") — 前馈网络中使用的激活函数。
  • timestep_activation_fn (str,默认为 "silu") — 在生成时间步嵌入时使用的激活函数。
  • norm_elementwise_affine (bool,默认为 True) — 是否在归一化层中使用逐元素仿射变换。
  • norm_eps (float,默认为 1e-5) — 归一化层中使用的 epsilon 值。
  • spatial_interpolation_scale (float,默认为 1.875) — 应用于 3D 位置嵌入中空间维度的缩放因子。
  • temporal_interpolation_scale (float,默认为 1.0) — 在 3D 位置嵌入中沿时间维度应用的缩放因子。

用于 CogVideoX 中类视频数据的 Transformer 模型。

forward

< >

( hidden_states: Tensorencoder_hidden_states: Tensortimestep: typing.Union[int, float, torch.LongTensor]timestep_cond: typing.Optional[torch.Tensor] = Noneofs: typing.Union[int, float, torch.LongTensor, NoneType] = Noneimage_rotary_emb: tuple[torch.Tensor, torch.Tensor] | None = Noneattention_kwargs: dict[str, typing.Any] | None = Nonereturn_dict: bool = True )

参数

  • hidden_states(形状为 (batch_size, num_frames, channels, height, width) 的 torch.Tensor)— 输入的 hidden_states。
  • encoder_hidden_states (torch.Tensor,形状为 (batch_size, sequence_len, embed_dims)) — 要使用的条件嵌入(从输入条件(例如提示词)计算得出的嵌入)。
  • timestep (torch.LongTensor) — 用于指示去噪步骤。
  • timestep_cond (torch.Tensor, 可选) — 时间步的条件嵌入。如果提供,这些嵌入将与通过 self.time_embedding 层处理的样本相加,以得到最终的时间步嵌入。
  • ofs (torch.Tensor,可选) — 在 CogVideoX-5b-I2V 中使用的偏移嵌入。
  • image_rotary_emb (tuple of torch.Tensor, 可选) — 预计算的旋转位置嵌入。
  • attention_kwargs (dict, 可选) — 一个 kwargs 字典,如果指定,将传递给 diffusers.models.attention_processor 中定义的 AttentionProcessor,该处理器在 self.processor 下定义。
  • return_dict (bool,可选,默认为 True) — 是否返回 ~models.transformer_2d.Transformer2DModelOutput 而不是普通的元组。

CogVideoXTransformer3DModel 的前向方法。

fuse_qkv_projections

< >

( )

启用融合的 QKV 投影。对于自注意力模块,所有投影矩阵(即 query、key、value)均会被融合。对于交叉注意力模块,key 和 value 投影矩阵会被融合。

> 此 API 处于 🧪 实验阶段。

unfuse_qkv_projections

< >

( )

如果已启用,则禁用融合的 QKV 投影。

> 此 API 处于 🧪 实验阶段。

Transformer2DModelOutput

类 diffusers.models.modeling_outputs.Transformer2DModelOutput

< >

( sample: torch.Tensor )

参数

  • sample(形状为 (batch_size, num_channels, height, width) 的 torch.Tensor,或者如果 Transformer2DModel 是离散的,则为 (batch size, num_vector_embeds - 1, num_latent_pixels))— 以 encoder_hidden_states 输入为条件的隐藏状态输出。如果是离散的,则返回未加噪潜在像素的概率分布。

Transformer2DModel 的输出。

Update on GitHub