| 46 | |
| 47 | |
| 48 | class CausalContinuousVideoTokenizer(nn.Module): |
| 49 | def __init__(self, z_channels: int, z_factor: int, embedding_dim: int, **kwargs) -> None: |
| 50 | super().__init__() |
| 51 | self.name = kwargs.get("name", "CausalContinuousVideoTokenizer") |
| 52 | self.embedding_dim = embedding_dim |
| 53 | self.spatial_compression = kwargs['spatial_compression'] |
| 54 | self.temporal_compression = kwargs['temporal_compression'] |
| 55 | self.sigma_data = SIGMA_DATA |
| 56 | self.encoder = EncoderFactorized(z_channels=z_factor * z_channels, **kwargs) |
| 57 | self.decoder = DecoderFactorized(z_channels=z_channels, **kwargs) |
| 58 | |
| 59 | self.quant_conv = CausalConv3d(z_factor * z_channels, embedding_dim, kernel_size=1, padding=0) |
| 60 | self.post_quant_conv = CausalConv3d(embedding_dim, z_channels, kernel_size=1, padding=0) |
| 61 | |
| 62 | latent_temporal_chunk = 16 |
| 63 | self.latent_mean = nn.Parameter(torch.zeros([self.embedding_dim * latent_temporal_chunk], dtype=torch.float32)) |
| 64 | self.latent_std = nn.Parameter(torch.ones([self.embedding_dim * latent_temporal_chunk], dtype=torch.float32)) |
| 65 | |
| 66 | def encode(self, x): |
| 67 | h = self.encoder(x) |
| 68 | z = self.quant_conv(h) |
| 69 | latent_ch = z.shape[1] |
| 70 | latent_t = z.shape[2] |
| 71 | dtype = z.dtype |
| 72 | mean = self.latent_mean.view(latent_ch, -1)[:, : latent_t].reshape([1, latent_ch, -1, 1, 1]).to(dtype=dtype, device=z.device) |
| 73 | std = self.latent_std.view(latent_ch, -1)[:, : latent_t].reshape([1, latent_ch, -1, 1, 1]).to(dtype=dtype, device=z.device) |
| 74 | return ((z - mean) / std) * self.sigma_data |
| 75 | |
| 76 | def decode(self, z): |
| 77 | in_dtype = z.dtype |
| 78 | latent_ch = z.shape[1] |
| 79 | latent_t = z.shape[2] |
| 80 | mean = self.latent_mean.view(latent_ch, -1)[:, : latent_t].reshape([1, latent_ch, -1, 1, 1]).to(dtype=in_dtype, device=z.device) |
| 81 | std = self.latent_std.view(latent_ch, -1)[:, : latent_t].reshape([1, latent_ch, -1, 1, 1]).to(dtype=in_dtype, device=z.device) |
| 82 | z = z / self.sigma_data |
| 83 | z = z * std + mean |
| 84 | z = self.post_quant_conv(z) |
| 85 | return self.decoder(z) |
| 86 | |
| 87 | |
| 88 | def load_custom_video_vae(path): |
no outgoing calls
no test coverage detected