| 493 | |
| 494 | |
| 495 | class LastLayer(nn.Module): |
| 496 | def __init__(self, hidden_size: int, patch_size: int, out_channels: int): |
| 497 | super().__init__() |
| 498 | self.norm_final = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) |
| 499 | self.linear = nn.Linear(hidden_size, patch_size * patch_size * out_channels, bias=True) |
| 500 | self.adaLN_modulation = nn.Sequential(nn.SiLU(), nn.Linear(hidden_size, 2 * hidden_size, bias=True)) |
| 501 | |
| 502 | def forward(self, x: Tensor, vec: Tensor) -> Tensor: |
| 503 | shift, scale = self.adaLN_modulation(vec).chunk(2, dim=1) |
| 504 | x = (1 + scale[:, None, :]) * self.norm_final(x) + shift[:, None, :] |
| 505 | x = self.linear(x) |
| 506 | return x |
| 507 | |
| 508 | |
| 509 | if __name__ == '__main__': |