| 319 | |
| 320 | |
| 321 | class Head(nn.Module): |
| 322 | |
| 323 | def __init__(self, dim, out_dim, patch_size, eps=1e-6): |
| 324 | super().__init__() |
| 325 | self.dim = dim |
| 326 | self.out_dim = out_dim |
| 327 | self.patch_size = patch_size |
| 328 | self.eps = eps |
| 329 | |
| 330 | # layers |
| 331 | out_dim = math.prod(patch_size) * out_dim |
| 332 | self.norm = WanLayerNorm(dim, eps) |
| 333 | self.head = nn.Linear(dim, out_dim) |
| 334 | |
| 335 | # modulation |
| 336 | self.modulation = nn.Parameter(torch.randn(1, 2, dim) / dim**0.5) |
| 337 | |
| 338 | def forward(self, x, e): |
| 339 | r""" |
| 340 | Args: |
| 341 | x(Tensor): Shape [B, L1, C] |
| 342 | e(Tensor): Shape [B, C] |
| 343 | """ |
| 344 | assert e.dtype == torch.float32 |
| 345 | with amp.autocast(dtype=torch.float32): |
| 346 | e = (self.modulation.to(e.device) + e.unsqueeze(1)).chunk(2, dim=1) |
| 347 | x = (self.head(self.norm(x) * (1 + e[1]) + e[0])) |
| 348 | return x |
| 349 | |
| 350 | |
| 351 | class MLPProj(torch.nn.Module): |