| 101 | |
| 102 | |
| 103 | class CogVideoXSpatialNorm3D(torch.nn.Module): |
| 104 | def __init__(self, f_channels, zq_channels, groups): |
| 105 | super().__init__() |
| 106 | self.norm_layer = torch.nn.GroupNorm(num_channels=f_channels, num_groups=groups, eps=1e-6, affine=True) |
| 107 | self.conv_y = torch.nn.Conv3d(zq_channels, f_channels, kernel_size=1, stride=1) |
| 108 | self.conv_b = torch.nn.Conv3d(zq_channels, f_channels, kernel_size=1, stride=1) |
| 109 | |
| 110 | |
| 111 | def forward(self, f: torch.Tensor, zq: torch.Tensor) -> torch.Tensor: |
| 112 | if f.shape[2] > 1 and f.shape[2] % 2 == 1: |
| 113 | f_first, f_rest = f[:, :, :1], f[:, :, 1:] |
| 114 | f_first_size, f_rest_size = f_first.shape[-3:], f_rest.shape[-3:] |
| 115 | z_first, z_rest = zq[:, :, :1], zq[:, :, 1:] |
| 116 | z_first = torch.nn.functional.interpolate(z_first, size=f_first_size) |
| 117 | z_rest = torch.nn.functional.interpolate(z_rest, size=f_rest_size) |
| 118 | zq = torch.cat([z_first, z_rest], dim=2) |
| 119 | else: |
| 120 | zq = torch.nn.functional.interpolate(zq, size=f.shape[-3:]) |
| 121 | |
| 122 | norm_f = self.norm_layer(f) |
| 123 | new_f = norm_f * self.conv_y(zq) + self.conv_b(zq) |
| 124 | return new_f |
| 125 | |
| 126 | |
| 127 | |