wanvae ¶
Classes¶
fastvideo.models.vaes.wanvae.AutoencoderKLWan ¶
Bases: Module, ParallelTiledVAE
A VAE model with KL loss for encoding videos into latents and decoding latent representations into videos. Introduced in [Wan 2.1].
Source code in fastvideo/models/vaes/wanvae.py
Methods:¶
fastvideo.models.vaes.wanvae.AutoencoderKLWan.forward ¶
forward(sample: Tensor, sample_posterior: bool = False, generator: Generator | None = None) -> Tensor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample | `torch.Tensor` | Input sample. | required |
return_dict | `bool`, *optional*, defaults to `True` | Whether or not to return a [ | required |
Source code in fastvideo/models/vaes/wanvae.py
fastvideo.models.vaes.wanvae.AutoencoderKLWan.optimize_memory_format ¶
Optional CUDA-only weight layout optimization used by MG3 LightVAE.
Source code in fastvideo/models/vaes/wanvae.py
fastvideo.models.vaes.wanvae.AutoencoderKLWan.streaming_decode ¶
streaming_decode(z: Tensor, cache: list[Tensor | None], is_first_chunk: bool = False) -> tuple[Tensor, list[Tensor | None]]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z | `torch.Tensor` | Latent tensor of shape [B, C, T, H, W]. | required |
cache | `list[torch.Tensor | None]` | The VAE cache. | required |
is_first_chunk | `bool` | Whether this is the first chunk in the sequence. | False |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, list[Tensor | None]] | A tuple of (decoded_frames, updated_cache). |
Source code in fastvideo/models/vaes/wanvae.py
fastvideo.models.vaes.wanvae.WanAttentionBlock ¶
Bases: Module
Causal self-attention with a single head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim | int | The number of channels in the input tensor. | required |
Source code in fastvideo/models/vaes/wanvae.py
fastvideo.models.vaes.wanvae.WanCausalConv3d ¶
WanCausalConv3d(in_channels: int, out_channels: int, kernel_size: int | tuple[int, int, int], stride: int | tuple[int, int, int] = 1, padding: int | tuple[int, int, int] = 0)
Bases: Conv3d
A custom 3D causal convolution layer with feature caching support.
This layer extends the standard Conv3D layer by ensuring causality in the time dimension and handling feature caching for efficient inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels | int | Number of channels in the input image | required |
out_channels | int | Number of channels produced by the convolution | required |
kernel_size | int or tuple | Size of the convolving kernel | required |
stride | int or tuple | Stride of the convolution. Default: 1 | 1 |
padding | int or tuple | Zero-padding added to all three sides of the input. Default: 0 | 0 |
Source code in fastvideo/models/vaes/wanvae.py
fastvideo.models.vaes.wanvae.WanDecoder3d ¶
WanDecoder3d(dim=128, z_dim=4, dim_mult=(1, 2, 4, 4), num_res_blocks=2, attn_scales=(), temperal_upsample=(False, True, True), dropout=0.0, non_linearity: str = 'silu', out_channels: int = 3, is_residual: bool = False)
Bases: Module
A 3D decoder module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim | int | The base number of channels in the first layer. | 128 |
z_dim | int | The dimensionality of the latent space. | 4 |
dim_mult | list of int | Multipliers for the number of channels in each block. | (1, 2, 4, 4) |
num_res_blocks | int | Number of residual blocks in each block. | 2 |
attn_scales | list of float | Scales at which to apply attention mechanisms. | () |
temperal_upsample | list of bool | Whether to upsample temporally in each block. | (False, True, True) |
dropout | float | Dropout rate for the dropout layers. | 0.0 |
non_linearity | str | Type of non-linearity to use. | 'silu' |
Source code in fastvideo/models/vaes/wanvae.py
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 | |
fastvideo.models.vaes.wanvae.WanEncoder3d ¶
WanEncoder3d(in_channels: int = 3, dim=128, z_dim=4, dim_mult=(1, 2, 4, 4), num_res_blocks=2, attn_scales=(), temperal_downsample=(True, True, False), dropout=0.0, non_linearity: str = 'silu', is_residual: bool = False)
Bases: Module
A 3D encoder module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim | int | The base number of channels in the first layer. | 128 |
z_dim | int | The dimensionality of the latent space. | 4 |
dim_mult | list of int | Multipliers for the number of channels in each block. | (1, 2, 4, 4) |
num_res_blocks | int | Number of residual blocks in each block. | 2 |
attn_scales | list of float | Scales at which to apply attention mechanisms. | () |
temperal_downsample | list of bool | Whether to downsample temporally in each block. | (True, True, False) |
dropout | float | Dropout rate for the dropout layers. | 0.0 |
non_linearity | str | Type of non-linearity to use. | 'silu' |
Source code in fastvideo/models/vaes/wanvae.py
fastvideo.models.vaes.wanvae.WanMidBlock ¶
Bases: Module
Middle block for WanVAE encoder and decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim | int | Number of input/output channels. | required |
dropout | float | Dropout rate. | 0.0 |
non_linearity | str | Type of non-linearity to use. | 'silu' |
Source code in fastvideo/models/vaes/wanvae.py
fastvideo.models.vaes.wanvae.WanRMS_norm ¶
Bases: Module
A custom RMS normalization layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim | int | The number of dimensions to normalize over. | required |
channel_first | bool | Whether the input tensor has channels as the first dimension. Default is True. | True |
images | bool | Whether the input represents image data. Default is True. | True |
bias | bool | Whether to include a learnable bias term. Default is False. | False |
Source code in fastvideo/models/vaes/wanvae.py
fastvideo.models.vaes.wanvae.WanResample ¶
Bases: Module
A custom resampling module for 2D and 3D data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim | int | The number of input/output channels. | required |
mode | str | The resampling mode. Must be one of: - 'none': No resampling (identity operation). - 'upsample2d': 2D upsampling with nearest-exact interpolation and convolution. - 'upsample3d': 3D upsampling with nearest-exact interpolation, convolution, and causal 3D convolution. - 'downsample2d': 2D downsampling with zero-padding and convolution. - 'downsample3d': 3D downsampling with zero-padding, convolution, and causal 3D convolution. | required |
Source code in fastvideo/models/vaes/wanvae.py
fastvideo.models.vaes.wanvae.WanResidualBlock ¶
Bases: Module
A custom residual block module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_dim | int | Number of input channels. | required |
out_dim | int | Number of output channels. | required |
dropout | float | Dropout rate for the dropout layer. Default is 0.0. | 0.0 |
non_linearity | str | Type of non-linearity to use. Default is "silu". | 'silu' |
Source code in fastvideo/models/vaes/wanvae.py
fastvideo.models.vaes.wanvae.WanResidualUpBlock ¶
WanResidualUpBlock(in_dim: int, out_dim: int, num_res_blocks: int, dropout: float = 0.0, temperal_upsample: bool = False, up_flag: bool = False, non_linearity: str = 'silu')
Bases: Module
A block that handles upsampling for the WanVAE decoder. Args: in_dim (int): Input dimension out_dim (int): Output dimension num_res_blocks (int): Number of residual blocks dropout (float): Dropout rate temperal_upsample (bool): Whether to upsample on temporal dimension up_flag (bool): Whether to upsample or not non_linearity (str): Type of non-linearity to use
Source code in fastvideo/models/vaes/wanvae.py
Methods:¶
fastvideo.models.vaes.wanvae.WanResidualUpBlock.forward ¶
Forward pass through the upsampling block. Args: x (torch.Tensor): Input tensor feat_cache (list, optional): Feature cache for causal convolutions feat_idx (list, optional): Feature index for cache management Returns: torch.Tensor: Output tensor
Source code in fastvideo/models/vaes/wanvae.py
fastvideo.models.vaes.wanvae.WanUpBlock ¶
WanUpBlock(in_dim: int, out_dim: int, num_res_blocks: int, dropout: float = 0.0, upsample_mode: str | None = None, non_linearity: str = 'silu')
Bases: Module
A block that handles upsampling for the WanVAE decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_dim | int | Input dimension | required |
out_dim | int | Output dimension | required |
num_res_blocks | int | Number of residual blocks | required |
dropout | float | Dropout rate | 0.0 |
upsample_mode | str | Mode for upsampling ('upsample2d' or 'upsample3d') | None |
non_linearity | str | Type of non-linearity to use | 'silu' |
Source code in fastvideo/models/vaes/wanvae.py
Methods:¶
fastvideo.models.vaes.wanvae.WanUpBlock.forward ¶
Forward pass through the upsampling block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Tensor | Input tensor | required |
feat_cache | list | Feature cache for causal convolutions | required |
feat_idx | list | Feature index for cache management | required |
Returns:
| Type | Description |
|---|---|
| torch.Tensor: Output tensor |
Source code in fastvideo/models/vaes/wanvae.py
fastvideo.models.vaes.wanvae.WanUpsample ¶
Bases: Upsample
Perform upsampling while ensuring the output tensor has the same data type as the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Tensor | Input tensor to be upsampled. | required |
Returns:
| Type | Description |
|---|---|
| torch.Tensor: Upsampled tensor with the same data type as the input. |