Skip to content

Official repository for LTX-Video

VAE: Support new architectures

Yoav HaCohen23e83b15b4ca
diff --git a/ltx_video/models/autoencoders/causal_conv3d.py b/ltx_video/models/autoencoders/causal_conv3d.py
index 146dea1..98249c2 100644
--- a/ltx_video/models/autoencoders/causal_conv3d.py
+++ b/ltx_video/models/autoencoders/causal_conv3d.py
@@ -13,6 +13,7 @@ class CausalConv3d(nn.Module):
stride: Union[int, Tuple[int]] = 1,
dilation: int = 1,
groups: int = 1,
+ spatial_padding_mode: str = "zeros",
**kwargs,
):
super().__init__()
@@ -36,7 +37,7 @@ class CausalConv3d(nn.Module):
stride=stride,
dilation=dilation,
padding=padding,
- padding_mode="zeros",
+ padding_mode=spatial_padding_mode,
groups=groups,
)
diff --git a/ltx_video/models/autoencoders/causal_video_autoencoder.py b/ltx_video/models/autoencoders/causal_video_autoencoder.py
index c059d4a..60893cb 100644
--- a/ltx_video/models/autoencoders/causal_video_autoencoder.py
+++ b/ltx_video/models/autoencoders/causal_video_autoencoder.py
@@ -133,9 +133,12 @@ class CausalVideoAutoencoder(AutoencoderKLWrapper):
"latent_log_var", "per_channel" if double_z else "none"
)
use_quant_conv = config.get("use_quant_conv", True)
+ normalize_latent_channels = config.get("normalize_latent_channels", False)
- if use_quant_conv and latent_log_var == "uniform":
- raise ValueError("uniform latent_log_var requires use_quant_conv=False")
+ if use_quant_conv and latent_log_var in ["uniform", "constant"]:
+ raise ValueError(
+ f"latent_log_var={latent_log_var} requires use_quant_conv=False"
+ )
encoder = Encoder(
dims=config["dims"],
@@ -145,6 +148,8 @@ class CausalVideoAutoencoder(AutoencoderKLWrapper):
patch_size=config.get("patch_size", 1),
latent_log_var=latent_log_var,
norm_layer=config.get("norm_layer", "group_norm"),
+ base_channels=config.get("encoder_base_channels", 128),
+ spatial_padding_mode=config.get("spatial_padding_mode", "zeros"),
)
decoder = Decoder(
@@ -156,6 +161,8 @@ class CausalVideoAutoencoder(AutoencoderKLWrapper):
norm_layer=config.get("norm_layer", "group_norm"),
causal=config.get("causal_decoder", False),
timestep_conditioning=config.get("timestep_conditioning", False),
+ base_channels=config.get("decoder_base_channels", 128),
+ spatial_padding_mode=config.get("spatial_padding_mode", "zeros"),
)
dims = config["dims"]
@@ -165,6 +172,7 @@ class CausalVideoAutoencoder(AutoencoderKLWrapper):
latent_channels=config["latent_channels"],
dims=dims,
use_quant_conv=use_quant_conv,
+ normalize_latent_channels=normalize_latent_channels,
)
@property
@@ -185,6 +193,7 @@ class CausalVideoAutoencoder(AutoencoderKLWrapper):
use_quant_conv=self.use_quant_conv,
causal_decoder=self.decoder.causal,
timestep_conditioning=self.decoder.timestep_conditioning,
+ normalize_latent_channels=self.normalize_latent_channels,
)
@property
@@ -202,7 +211,13 @@ class CausalVideoAutoencoder(AutoencoderKLWrapper):
[
block
for block in self.encoder.blocks_desc
- if block[0] in ["compress_space", "compress_all"]
+ if block[0]
+ in [
+ "compress_space",
+ "compress_all",
+ "compress_all_res",
+ "compress_space_res",
+ ]
]
)
* self.encoder.patch_size
@@ -214,7 +229,13 @@ class CausalVideoAutoencoder(AutoencoderKLWrapper):
[
block
for block in self.encoder.blocks_desc
- if block[0] in ["compress_time", "compress_all"]
+ if block[0]
+ in [
+ "compress_time",
+ "compress_all",
+ "compress_all_res",
+ "compress_space_res",
+ ]
]
)
@@ -236,7 +257,7 @@ class CausalVideoAutoencoder(AutoencoderKLWrapper):
if not key.startswith(PER_CHANNEL_STATISTICS_PREFIX)
}
- model_keys = set(name for name, _ in self.named_parameters())
+ model_keys = set(name for name, _ in self.named_modules())
key_mapping = {
".resnets.": ".res_blocks.",
@@ -248,7 +269,8 @@ class CausalVideoAutoencoder(AutoencoderKLWrapper):
for k, v in key_mapping.items():
key = key.replace(k, v)
- if "norm" in key and key not in model_keys:
+ key_prefix = ".".join(key.split(".")[:-1])
+ if "norm" in key and key_prefix not in model_keys:
logger.info(
f"Removing key {key} from state_dict as it is not present in the model"
)
@@ -311,7 +333,7 @@ class Encoder(nn.Module):
norm_layer (`str`, *optional*, defaults to `group_norm`):
The normalization layer to use. Can be either `group_norm` or `pixel_norm`.
latent_log_var (`str`, *optional*, defaults to `per_channel`):
- The number of channels for the log variance. Can be either `per_channel`, `uniform`, or `none`.
+ The number of channels for the log variance. Can be either `per_channel`, `uniform`, `constant` or `none`.
"""
def __init__(
@@ -325,6 +347,7 @@ class Encoder(nn.Module):
patch_size: Union[int, Tuple[int]] = 1,
norm_layer: str = "group_norm", # group_norm, pixel_norm
latent_log_var: str = "per_channel",
+ spatial_padding_mode: str = "zeros",
):
super().__init__()
self.patch_size = patch_size
@@ -344,6 +367,7 @@ class Encoder(nn.Module):
stride=1,
padding=1,
causal=True,
+ spatial_padding_mode=spatial_padding_mode,
)
self.down_blocks = nn.ModuleList([])
@@ -361,6 +385,7 @@ class Encoder(nn.Module):
resnet_eps=1e-6,
resnet_groups=norm_num_groups,
norm_layer=norm_layer,
+ spatial_padding_mode=spatial_padding_mode,
)
elif block_name == "res_x_y":
output_channel = block_params.get("multiplier", 2) * output_channel
@@ -371,6 +396,7 @@ class Encoder(nn.Module):
eps=1e-6,
groups=norm_num_groups,
norm_layer=norm_layer,
+ spatial_padding_mode=spatial_padding_mode,
)
elif block_name == "compress_time":
block = make_conv_nd(
@@ -380,6 +406,7 @@ class Encoder(nn.Module):
kernel_size=3,
stride=(2, 1, 1),
causal=True,
+ spatial_padding_mode=spatial_padding_mode,
)
elif block_name == "compress_space":
block = make_conv_nd(
@@ -389,6 +416,7 @@ class Encoder(nn.Module):
kernel_size=3,
stride=(1, 2, 2),
causal=True,
+ spatial_padding_mode=spatial_padding_mode,
)
elif block_name == "compress_all":
block = make_conv_nd(
@@ -398,6 +426,7 @@ class Encoder(nn.Module):
kernel_size=3,
stride=(2, 2, 2),
causal=True,
+ spatial_padding_mode=spatial_padding_mode,
)
elif block_name == "compress_all_x_y":
output_channel = block_params.get("multiplier", 2) * output_channel
@@ -408,6 +437,34 @@ class Encoder(nn.Module):
kernel_size=3,
stride=(2, 2, 2),
causal=True,
+ spatial_padding_mode=spatial_padding_mode,
+ )
+ elif block_name == "compress_all_res":
+ output_channel = block_params.get("multiplier", 2) * output_channel
+ block = SpaceToDepthDownsample(
+ dims=dims,
+ in_channels=input_channel,
+ out_channels=output_channel,
+ stride=(2, 2, 2),
+ spatial_padding_mode=spatial_padding_mode,
+ )
+ elif block_name == "compress_space_res":
+ output_channel = block_params.get("multiplier", 2) * output_channel
+ block = SpaceToDepthDownsample(
+ dims=dims,
+ in_channels=input_channel,
+ out_channels=output_channel,
+ stride=(1, 2, 2),
+ spatial_padding_mode=spatial_padding_mode,
+ )
+ elif block_name == "compress_time_res":
+ output_channel = block_params.get("multiplier", 2) * output_channel
+ block = SpaceToDepthDownsample(
+ dims=dims,
+ in_channels=input_channel,
+ out_channels=output_channel,
+ stride=(2, 1, 1),
+ spatial_padding_mode=spatial_padding_mode,
)
else:
raise ValueError(f"unknown block: {block_name}")
@@ -431,10 +488,18 @@ class Encoder(nn.Module):
conv_out_channels *= 2
elif latent_log_var == "uniform":
conv_out_channels += 1
+ elif latent_log_var == "constant":
+ conv_out_channels += 1
elif latent_log_var != "none":
raise ValueError(f"Invalid latent_log_var: {latent_log_var}")
self.conv_out = make_conv_nd(
- dims, output_channel, conv_out_channels, 3, padding=1, causal=True
+ dims,
+ output_channel,
+ conv_out_channels,
+ 3,
+ padding=1,
+ causal=True,
+ spatial_padding_mode=spatial_padding_mode,
)
self.gradient_checkpointing = False
@@ -476,6 +541,15 @@ class Encoder(nn.Module):
sample = torch.cat([sample, repeated_last_channel], dim=1)
else:
raise ValueError(f"Invalid input shape: {sample.shape}")
+ elif self.latent_log_var == "constant":
+ sample = sample[:, :-1, ...]
+ approx_ln_0 = (
+ -30
+ ) # this is the minimal clamp value in DiagonalGaussianDistribution objects
+ sample = torch.cat(
+ [sample, torch.ones_like(sample, device=sample.device) * approx_ln_0],
+ dim=1,
+ )
return sample
@@ -518,6 +592,7 @@ class Decoder(nn.Module):
norm_layer: str = "group_norm",
causal: bool = True,
timestep_conditioning: bool = False,
+ spatial_padding_mode: str = "zeros",
):
super().__init__()
self.patch_size = patch_size
@@ -543,6 +618,7 @@ class Decoder(nn.Module):
stride=1,
padding=1,
causal=True,
+ spatial_padding_mode=spatial_padding_mode,
)
self.up_blocks = nn.ModuleList([])
@@ -562,6 +638,7 @@ class Decoder(nn.Module):
norm_layer=norm_layer,
inject_noise=block_params.get("inject_noise", False),
timestep_conditioning=timestep_conditioning,
+ spatial_padding_mode=spatial_padding_mode,
)
elif block_name == "attn_res_x":
block = UNetMidBlock3D(
@@ -573,6 +650,7 @@ class Decoder(nn.Module):
inject_noise=block_params.get("inject_noise", False),
timestep_conditioning=timestep_conditioning,
attention_head_dim=block_params["attention_head_dim"],
+ spatial_padding_mode=spatial_padding_mode,
)
elif block_name == "res_x_y":
output_channel = output_channel // block_params.get("multiplier", 2)
@@ -585,14 +663,21 @@ class Decoder(nn.Module):
norm_layer=norm_layer,
inject_noise=block_params.get("inject_noise", False),
timestep_conditioning=False,
+ spatial_padding_mode=spatial_padding_mode,
)
elif block_name == "compress_time":
block = DepthToSpaceUpsample(
- dims=dims, in_channels=input_channel, stride=(2, 1, 1)
+ dims=dims,
+ in_channels=input_channel,
+ stride=(2, 1, 1),
+ spatial_padding_mode=spatial_padding_mode,
)
elif block_name == "compress_space":
block = DepthToSpaceUpsample(
- dims=dims, in_channels=input_channel, stride=(1, 2, 2)
+ dims=dims,
+ in_channels=input_channel,
+ stride=(1, 2, 2),
+ spatial_padding_mode=spatial_padding_mode,
)
elif block_name == "compress_all":
output_channel = output_channel // block_params.get("multiplier", 1)
@@ -602,6 +687,7 @@ class Decoder(nn.Module):
stride=(2, 2, 2),
residual=block_params.get("residual", False),
out_channels_reduction_factor=block_params.get("multiplier", 1),
+ spatial_padding_mode=spatial_padding_mode,
)
else:
raise ValueError(f"unknown layer: {block_name}")
@@ -619,7 +705,13 @@ class Decoder(nn.Module):
self.conv_act = nn.SiLU()
self.conv_out = make_conv_nd(
- dims, output_channel, out_channels, 3, padding=1, causal=True
+ dims,
+ output_channel,
+ out_channels,
+ 3,
+ padding=1,
+ causal=True,
+ spatial_padding_mode=spatial_padding_mode,
)
self.gradient_checkpointing = False
@@ -745,6 +837,7 @@ class UNetMidBlock3D(nn.Module):
inject_noise: bool = False,
timestep_conditioning: bool = False,
attention_head_dim: int = -1,
+ spatial_padding_mode: str = "zeros",
):
super().__init__()
resnet_groups = (
@@ -769,6 +862,7 @@ class UNetMidBlock3D(nn.Module):
norm_layer=norm_layer,
inject_noise=inject_noise,
timestep_conditioning=timestep_conditioning,
+ spatial_padding_mode=spatial_padding_mode,
)
for _ in range(num_layers)
]
@@ -876,9 +970,62 @@ class UNetMidBlock3D(nn.Module):
return hidden_states
+class SpaceToDepthDownsample(nn.Module):
+ def __init__(self, dims, in_channels, out_channels, stride, spatial_padding_mode):
+ super().__init__()
+ self.stride = stride
+ self.group_size = in_channels * np.prod(stride) // out_channels
+ self.conv = make_conv_nd(
+ dims=dims,
+ in_channels=in_channels,
+ out_channels=out_channels // np.prod(stride),
+ kernel_size=3,
+ stride=1,
+ causal=True,
+ spatial_padding_mode=spatial_padding_mode,
+ )
+
+ def forward(self, x, causal: bool = True):
+ if self.stride[0] == 2:
+ x = torch.cat(
+ [x[:, :, :1, :, :], x], dim=2
+ ) # duplicate first frames for padding
+
+ # skip connection
+ x_in = rearrange(
+ x,
+ "b c (d p1) (h p2) (w p3) -> b (c p1 p2 p3) d h w",
+ p1=self.stride[0],
+ p2=self.stride[1],
+ p3=self.stride[2],
+ )
+ x_in = rearrange(x_in, "b (c g) d h w -> b c g d h w", g=self.group_size)
+ x_in = x_in.mean(dim=2)
+
+ # conv
+ x = self.conv(x, causal=causal)
+ x = rearrange(
+ x,
+ "b c (d p1) (h p2) (w p3) -> b (c p1 p2 p3) d h w",
+ p1=self.stride[0],
+ p2=self.stride[1],
+ p3=self.stride[2],
+ )
+
+ x = x + x_in
+
+ return x
+
+
class DepthToSpaceUpsample(nn.Module):
def __init__(
- self, dims, in_channels, stride, residual=False, out_channels_reduction_factor=1
+ self,
+ dims,
+ in_channels,
+ stride,
+ residual=False,
+ out_channels_reduction_factor=1,
+ spatial_padding_mode="zeros",
):
super().__init__()
self.stride = stride
@@ -892,6 +1039,7 @@ class DepthToSpaceUpsample(nn.Module):
kernel_size=3,
stride=1,
causal=True,
+ spatial_padding_mode=spatial_padding_mode,
)
self.residual = residual
self.out_channels_reduction_factor = out_channels_reduction_factor
@@ -961,6 +1109,7 @@ class ResnetBlock3D(nn.Module):
norm_layer: str = "group_norm",
inject_noise: bool = False,
timestep_conditioning: bool = False,
+ spatial_padding_mode: str = "zeros",
):
super().__init__()
self.in_channels = in_channels
@@ -987,6 +1136,7 @@ class ResnetBlock3D(nn.Module):
stride=1,
padding=1,
causal=True,
+ spatial_padding_mode=spatial_padding_mode,
)
if inject_noise:
@@ -1011,6 +1161,7 @@ class ResnetBlock3D(nn.Module):
stride=1,
padding=1,
causal=True,
+ spatial_padding_mode=spatial_padding_mode,
)
if inject_noise:
@@ -1156,30 +1307,28 @@ def unpatchify(x, patch_size_hw, patch_size_t=1):
return x
-def create_video_autoencoder_config(
+def create_video_autoencoder_demo_config(
latent_channels: int = 64,
):
encoder_blocks = [
- ("res_x", {"num_layers": 4}),
- ("compress_all_x_y", {"multiplier": 3}),
- ("res_x", {"num_layers": 4}),
- ("compress_all_x_y", {"multiplier": 2}),
- ("res_x", {"num_layers": 4}),
- ("compress_all", {}),
- ("res_x", {"num_layers": 3}),
- ("res_x", {"num_layers": 4}),
+ ("res_x", {"num_layers": 2}),
+ ("compress_space_res", {"multiplier": 2}),
+ ("res_x", {"num_layers": 2}),
+ ("compress_time_res", {"multiplier": 2}),
+ ("res_x", {"num_layers": 1}),
+ ("compress_all_res", {"multiplier": 2}),
+ ("res_x", {"num_layers": 1}),
+ ("compress_all_res", {"multiplier": 2}),
+ ("res_x", {"num_layers": 1}),
]
decoder_blocks = [
- ("res_x", {"num_layers": 4}),
- ("compress_all", {"residual": True}),
- ("res_x_y", {"multiplier": 3}),
- ("res_x", {"num_layers": 3}),
- ("compress_all", {"residual": True}),
- ("res_x_y", {"multiplier": 2}),
- ("res_x", {"num_layers": 3}),
- ("compress_all", {"residual": True}),
- ("res_x", {"num_layers": 3}),
- ("res_x", {"num_layers": 4}),
+ ("res_x", {"num_layers": 2, "inject_noise": False}),
+ ("compress_all", {"residual": True, "multiplier": 2}),
+ ("res_x", {"num_layers": 2, "inject_noise": False}),
+ ("compress_all", {"residual": True, "multiplier": 2}),
+ ("res_x", {"num_layers": 2, "inject_noise": False}),
+ ("compress_all", {"residual": True, "multiplier": 2}),
+ ("res_x", {"num_layers": 2, "inject_noise": False}),
]
return {
"_class_name": "CausalVideoAutoencoder",
@@ -1193,6 +1342,7 @@ def create_video_autoencoder_config(
"use_quant_conv": False,
"causal_decoder": False,
"timestep_conditioning": True,
+ "spatial_padding_mode": "replicate",
}
@@ -1207,7 +1357,7 @@ def test_vae_patchify_unpatchify():
def demo_video_autoencoder_forward_backward():
# Configuration for the VideoAutoencoder
- config = create_video_autoencoder_config()
+ config = create_video_autoencoder_demo_config()
# Instantiate the VideoAutoencoder with the specified configuration
video_autoencoder = CausalVideoAutoencoder.from_config(config)
@@ -1242,11 +1392,11 @@ def demo_video_autoencoder_forward_backward():
image_latent, target_shape=image_latent.shape, timestep=timestep
).sample
- # first_frame_latent = latent[:, :, :1, :, :]
+ first_frame_latent = latent[:, :, :1, :, :]
- # assert torch.allclose(image_latent, first_frame_latent, atol=1e-6)
+ assert torch.allclose(image_latent, first_frame_latent, atol=1e-6)
# assert torch.allclose(reconstructed_image, reconstructed_videos[:, :, :1, :, :], atol=1e-6)
- # assert (image_latent == first_frame_latent).all()
+ # assert torch.allclose(image_latent, first_frame_latent, atol=1e-6)
# assert (reconstructed_image == reconstructed_videos[:, :, :1, :, :]).all()
# Calculate the loss (e.g., mean squared error)
diff --git a/ltx_video/models/autoencoders/conv_nd_factory.py b/ltx_video/models/autoencoders/conv_nd_factory.py
index 5bc0c2e..718c69b 100644
--- a/ltx_video/models/autoencoders/conv_nd_factory.py
+++ b/ltx_video/models/autoencoders/conv_nd_factory.py
@@ -17,7 +17,11 @@ def make_conv_nd(
groups=1,
bias=True,
causal=False,
+ spatial_padding_mode="zeros",
+ temporal_padding_mode="zeros",
):
+ if not (spatial_padding_mode == temporal_padding_mode or causal):
+ raise NotImplementedError("spatial and temporal padding modes must be equal")
if dims == 2:
return torch.nn.Conv2d(
in_channels=in_channels,
@@ -28,6 +32,7 @@ def make_conv_nd(
dilation=dilation,
groups=groups,
bias=bias,
+ padding_mode=spatial_padding_mode,
)
elif dims == 3:
if causal:
@@ -40,6 +45,7 @@ def make_conv_nd(
dilation=dilation,
groups=groups,
bias=bias,
+ spatial_padding_mode=spatial_padding_mode,
)
return torch.nn.Conv3d(
in_channels=in_channels,
@@ -50,6 +56,7 @@ def make_conv_nd(
dilation=dilation,
groups=groups,
bias=bias,
+ padding_mode=spatial_padding_mode,
)
elif dims == (2, 1):
return DualConv3d(
@@ -59,6 +66,7 @@ def make_conv_nd(
stride=stride,
padding=padding,
bias=bias,
+ padding_mode=spatial_padding_mode,
)
else:
raise ValueError(f"unsupported dimensions: {dims}")
diff --git a/ltx_video/models/autoencoders/dual_conv3d.py b/ltx_video/models/autoencoders/dual_conv3d.py
index 6bd54c0..dcf8892 100644
--- a/ltx_video/models/autoencoders/dual_conv3d.py
+++ b/ltx_video/models/autoencoders/dual_conv3d.py
@@ -18,11 +18,13 @@ class DualConv3d(nn.Module):
dilation: Union[int, Tuple[int, int, int]] = 1,
groups=1,
bias=True,
+ padding_mode="zeros",
):
super(DualConv3d, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
+ self.padding_mode = padding_mode
# Ensure kernel_size, stride, padding, and dilation are tuples of length 3
if isinstance(kernel_size, int):
kernel_size = (kernel_size, kernel_size, kernel_size)
@@ -108,6 +110,7 @@ class DualConv3d(nn.Module):
self.padding1,
self.dilation1,
self.groups,
+ padding_mode=self.padding_mode,
)
if skip_time_conv:
@@ -122,6 +125,7 @@ class DualConv3d(nn.Module):
self.padding2,
self.dilation2,
self.groups,
+ padding_mode=self.padding_mode,
)
return x
@@ -137,7 +141,16 @@ class DualConv3d(nn.Module):
stride1 = (self.stride1[1], self.stride1[2])
padding1 = (self.padding1[1], self.padding1[2])
dilation1 = (self.dilation1[1], self.dilation1[2])
- x = F.conv2d(x, weight1, self.bias1, stride1, padding1, dilation1, self.groups)
+ x = F.conv2d(
+ x,
+ weight1,
+ self.bias1,
+ stride1,
+ padding1,
+ dilation1,
+ self.groups,
+ padding_mode=self.padding_mode,
+ )
_, _, h, w = x.shape
@@ -154,7 +167,16 @@ class DualConv3d(nn.Module):
stride2 = self.stride2[0]
padding2 = self.padding2[0]
dilation2 = self.dilation2[0]
- x = F.conv1d(x, weight2, self.bias2, stride2, padding2, dilation2, self.groups)
+ x = F.conv1d(
+ x,
+ weight2,
+ self.bias2,
+ stride2,
+ padding2,
+ dilation2,
+ self.groups,
+ padding_mode=self.padding_mode,
+ )
x = rearrange(x, "(b h w) c d -> b c d h w", b=b, h=h, w=w)
return x
diff --git a/ltx_video/models/autoencoders/vae.py b/ltx_video/models/autoencoders/vae.py
index 0bcab52..5b22217 100644
--- a/ltx_video/models/autoencoders/vae.py
+++ b/ltx_video/models/autoencoders/vae.py
@@ -36,12 +36,14 @@ class AutoencoderKLWrapper(ModelMixin, ConfigMixin):
dims: int = 2,
sample_size=512,
use_quant_conv: bool = True,
+ normalize_latent_channels: bool = False,
):
super().__init__()
# pass init params to Encoder
self.encoder = encoder
self.use_quant_conv = use_quant_conv
+ self.normalize_latent_channels = normalize_latent_channels
# pass init params to Decoder
quant_dims = 2 if dims == 2 else 3
@@ -56,6 +58,14 @@ class AutoencoderKLWrapper(ModelMixin, ConfigMixin):
else:
self.quant_conv = nn.Identity()
self.post_quant_conv = nn.Identity()
+
+ if normalize_latent_channels:
+ if dims == 2:
+ self.latent_norm_out = nn.BatchNorm2d(latent_channels, affine=False)
+ else:
+ self.latent_norm_out = nn.BatchNorm3d(latent_channels, affine=False)
+ else:
+ self.latent_norm_out = nn.Identity()
self.use_z_tiling = False
self.use_hw_tiling = False
self.dims = dims
@@ -248,9 +258,35 @@ class AutoencoderKLWrapper(ModelMixin, ConfigMixin):
return AutoencoderKLOutput(latent_dist=posterior)
+ def _normalize_latent_channels(self, z: torch.FloatTensor) -> torch.FloatTensor:
+ if isinstance(self.latent_norm_out, nn.BatchNorm3d):
+ _, c, _, _, _ = z.shape
+ z = torch.cat(
+ [
+ self.latent_norm_out(z[:, : c // 2, :, :, :]),
+ z[:, c // 2 :, :, :, :],
+ ],
+ dim=1,
+ )
+ elif isinstance(self.latent_norm_out, nn.BatchNorm2d):
+ raise NotImplementedError("BatchNorm2d not supported")
+ return z
+
+ def _unnormalize_latent_channels(self, z: torch.FloatTensor) -> torch.FloatTensor:
+ if isinstance(self.latent_norm_out, nn.BatchNorm3d):
+ running_mean = self.latent_norm_out.running_mean.view(1, -1, 1, 1, 1)
+ running_var = self.latent_norm_out.running_var.view(1, -1, 1, 1, 1)
+ eps = self.latent_norm_out.eps
+
+ z = z * torch.sqrt(running_var + eps) + running_mean
+ elif isinstance(self.latent_norm_out, nn.BatchNorm3d):
+ raise NotImplementedError("BatchNorm2d not supported")
+ return z
+
def _encode(self, x: torch.FloatTensor) -> AutoencoderKLOutput:
h = self.encoder(x)
moments = self.quant_conv(h)
+ moments = self._normalize_latent_channels(moments)
return moments
def _decode(
@@ -259,6 +295,7 @@ class AutoencoderKLWrapper(ModelMixin, ConfigMixin):
target_shape=None,
timestep: Optional[torch.Tensor] = None,
) -> Union[DecoderOutput, torch.FloatTensor]:
+ z = self._unnormalize_latent_channels(z)
z = self.post_quant_conv(z)
if "timestep" in self.decoder_params:
dec = self.decoder(z, target_shape=target_shape, timestep=timestep)