HunyuanVideo: A Systematic Framework For Large Video Generation Model
Formatting hyvideo/vae/vae.py
KimbingNg49449a3084fd
diff --git a/hyvideo/vae/vae.py b/hyvideo/vae/vae.pyindex d6e098e..4002d1f 100644--- a/hyvideo/vae/vae.py+++ b/hyvideo/vae/vae.py@@ -51,8 +51,7 @@ class EncoderCausal3D(nn.Module):super().__init__()self.layers_per_block = layers_per_block- self.conv_in = CausalConv3d(- in_channels, block_out_channels[0], kernel_size=3, stride=1)+ self.conv_in = CausalConv3d(in_channels, block_out_channels[0], kernel_size=3, stride=1)self.mid_block = Noneself.down_blocks = nn.ModuleList([])@@ -62,30 +61,27 @@ class EncoderCausal3D(nn.Module):input_channel = output_channeloutput_channel = block_out_channels[i]is_final_block = i == len(block_out_channels) - 1- num_spatial_downsample_layers = int(- np.log2(spatial_compression_ratio))+ num_spatial_downsample_layers = int(np.log2(spatial_compression_ratio))num_time_downsample_layers = int(np.log2(time_compression_ratio))if time_compression_ratio == 4:- add_spatial_downsample = bool(- i < num_spatial_downsample_layers)- add_time_downsample = bool(i >= (- len(block_out_channels) - 1 - num_time_downsample_layers) and not is_final_block)+ add_spatial_downsample = bool(i < num_spatial_downsample_layers)+ add_time_downsample = bool(+ i >= (len(block_out_channels) - 1 - num_time_downsample_layers)+ and not is_final_block+ )else:- raise ValueError(- f"Unsupported time_compression_ratio: {time_compression_ratio}")+ raise ValueError(f"Unsupported time_compression_ratio: {time_compression_ratio}.")downsample_stride_HW = (2, 2) if add_spatial_downsample else (1, 1)- downsample_stride_T = (2, ) if add_time_downsample else (1, )- downsample_stride = tuple(- downsample_stride_T + downsample_stride_HW)+ downsample_stride_T = (2,) if add_time_downsample else (1,)+ downsample_stride = tuple(downsample_stride_T + downsample_stride_HW)down_block = get_down_block3d(down_block_type,num_layers=self.layers_per_block,in_channels=input_channel,out_channels=output_channel,- add_downsample=bool(- add_spatial_downsample or add_time_downsample),+ add_downsample=bool(add_spatial_downsample or add_time_downsample),downsample_stride=downsample_stride,resnet_eps=1e-6,downsample_padding=0,@@ -110,13 +106,11 @@ class EncoderCausal3D(nn.Module):)# out- self.conv_norm_out = nn.GroupNorm(- num_channels=block_out_channels[-1], num_groups=norm_num_groups, eps=1e-6)+ self.conv_norm_out = nn.GroupNorm(num_channels=block_out_channels[-1], num_groups=norm_num_groups, eps=1e-6)self.conv_act = nn.SiLU()conv_out_channels = 2 * out_channels if double_z else out_channels- self.conv_out = CausalConv3d(- block_out_channels[-1], conv_out_channels, kernel_size=3)+ self.conv_out = CausalConv3d(block_out_channels[-1], conv_out_channels, kernel_size=3)def forward(self, sample: torch.FloatTensor) -> torch.FloatTensor:r"""The forward method of the `EncoderCausal3D` class."""@@ -161,8 +155,7 @@ class DecoderCausal3D(nn.Module):super().__init__()self.layers_per_block = layers_per_block- self.conv_in = CausalConv3d(- in_channels, block_out_channels[-1], kernel_size=3, stride=1)+ self.conv_in = CausalConv3d(in_channels, block_out_channels[-1], kernel_size=3, stride=1)self.mid_block = Noneself.up_blocks = nn.ModuleList([])@@ -188,23 +181,21 @@ class DecoderCausal3D(nn.Module):prev_output_channel = output_channeloutput_channel = reversed_block_out_channels[i]is_final_block = i == len(block_out_channels) - 1- num_spatial_upsample_layers = int(- np.log2(spatial_compression_ratio))+ num_spatial_upsample_layers = int(np.log2(spatial_compression_ratio))num_time_upsample_layers = int(np.log2(time_compression_ratio))if time_compression_ratio == 4:add_spatial_upsample = bool(i < num_spatial_upsample_layers)- add_time_upsample = bool(i >= len(- block_out_channels) - 1 - num_time_upsample_layers and not is_final_block)+ add_time_upsample = bool(+ i >= len(block_out_channels) - 1 - num_time_upsample_layers+ and not is_final_block+ )else:- raise ValueError(- f"Unsupported time_compression_ratio: {time_compression_ratio}")-- upsample_scale_factor_HW = (- 2, 2) if add_spatial_upsample else (1, 1)- upsample_scale_factor_T = (2, ) if add_time_upsample else (1, )- upsample_scale_factor = tuple(- upsample_scale_factor_T + upsample_scale_factor_HW)+ raise ValueError(f"Unsupported time_compression_ratio: {time_compression_ratio}.")++ upsample_scale_factor_HW = (2, 2) if add_spatial_upsample else (1, 1)+ upsample_scale_factor_T = (2,) if add_time_upsample else (1,)+ upsample_scale_factor = tuple(upsample_scale_factor_T + upsample_scale_factor_HW)up_block = get_up_block3d(up_block_type,num_layers=self.layers_per_block + 1,@@ -225,14 +216,11 @@ class DecoderCausal3D(nn.Module):# outif norm_type == "spatial":- self.conv_norm_out = SpatialNorm(- block_out_channels[0], temb_channels)+ self.conv_norm_out = SpatialNorm(block_out_channels[0], temb_channels)else:- self.conv_norm_out = nn.GroupNorm(- num_channels=block_out_channels[0], num_groups=norm_num_groups, eps=1e-6)+ self.conv_norm_out = nn.GroupNorm(num_channels=block_out_channels[0], num_groups=norm_num_groups, eps=1e-6)self.conv_act = nn.SiLU()- self.conv_out = CausalConv3d(- block_out_channels[0], out_channels, kernel_size=3)+ self.conv_out = CausalConv3d(block_out_channels[0], out_channels, kernel_size=3)self.gradient_checkpointing = False@@ -242,7 +230,7 @@ class DecoderCausal3D(nn.Module):latent_embeds: Optional[torch.FloatTensor] = None,) -> torch.FloatTensor:r"""The forward method of the `DecoderCausal3D` class."""- assert len(sample.shape) == 5, "The input tensor should have 5 dimensions"+ assert len(sample.shape) == 5, "The input tensor should have 5 dimensions."sample = self.conv_in(sample)@@ -276,15 +264,13 @@ class DecoderCausal3D(nn.Module):else:# middlesample = torch.utils.checkpoint.checkpoint(- create_custom_forward(- self.mid_block), sample, latent_embeds+ create_custom_forward(self.mid_block), sample, latent_embeds)sample = sample.to(upscale_dtype)# upfor up_block in self.up_blocks:- sample = torch.utils.checkpoint.checkpoint(- create_custom_forward(up_block), sample, latent_embeds)+ sample = torch.utils.checkpoint.checkpoint(create_custom_forward(up_block), sample, latent_embeds)else:# middlesample = self.mid_block(sample, latent_embeds)