Llama-style MLP: gate/up are column-parallel and down is row-parallel.
| 28 | |
| 29 | |
| 30 | class MLPBlock(torch.nn.Module): |
| 31 | """Llama-style MLP: gate/up are column-parallel and down is row-parallel.""" |
| 32 | |
| 33 | def __init__(self): |
| 34 | super().__init__() |
| 35 | self.gate_proj = torch.nn.Linear(HIDDEN_DIM, INTERMEDIATE_DIM, bias=False) |
| 36 | self.up_proj = torch.nn.Linear(HIDDEN_DIM, INTERMEDIATE_DIM, bias=False) |
| 37 | self.down_proj = torch.nn.Linear(INTERMEDIATE_DIM, HIDDEN_DIM, bias=False) |
| 38 | |
| 39 | def forward(self, x): |
| 40 | # The residual is what makes this block interesting for the pass: x feeds the two |
| 41 | # column-parallel matmuls and the addition, and only the matmuls may be routed through the |
| 42 | # backward all-reduce. Reducing the residual gradient too would scale it by the TP size. |
| 43 | return x + self.down_proj(torch.nn.functional.silu(self.gate_proj(x)) * self.up_proj(x)) |
| 44 | |
| 45 | |
| 46 | class MLPModel(torch.nn.Module): |