The final layer of DiT.
| 447 | return x |
| 448 | |
| 449 | class ParallelFinalLayer(nn.Module): |
| 450 | """ |
| 451 | The final layer of DiT. |
| 452 | """ |
| 453 | def __init__(self, hidden_size, patch_size, out_channels): |
| 454 | super().__init__() |
| 455 | self.norm_final = nn.LayerNorm( |
| 456 | hidden_size, elementwise_affine=False, eps=1e-6, |
| 457 | ) |
| 458 | self.linear = ColumnParallelLinear( |
| 459 | hidden_size, patch_size * patch_size * out_channels, bias=True, |
| 460 | init_method=nn.init.zeros_, gather_output=True, |
| 461 | ) |
| 462 | self.adaLN_modulation = nn.Sequential( |
| 463 | nn.SiLU(), |
| 464 | ColumnParallelLinear( |
| 465 | min(hidden_size, 1024), 2 * hidden_size, bias=True, |
| 466 | init_method=nn.init.zeros_, gather_output=True, |
| 467 | ), |
| 468 | ) |
| 469 | |
| 470 | def forward(self, x, c): |
| 471 | shift, scale = self.adaLN_modulation(c).chunk(2, dim=1) |
| 472 | x = modulate(self.norm_final(x), shift, scale) |
| 473 | x = self.linear(x) |
| 474 | return x |
| 475 | |
| 476 | |
| 477 | class DiT_Llama(nn.Module): |