Megatron Column- and Row-Parallel MLP
Megatron-LM · arXiv 2019 · Medium · 40 minutes
Align hidden-channel shards, apply local GeLU, and reduce second-layer outputs.
A single-process tensor-parallel two-layer MLP including biases and gradients, with tanh-approximate GeLU and no distributed communication.
Functions to implement
parallel_mlp(x: Tensor, w1: Tensor, b1: Tensor, w2: Tensor, b2: Tensor, parts: int) -> Tensor
Megatron Column- and Row-Parallel MLP
1. Problem
Align hidden-channel shards, apply local GeLU, and reduce second-layer outputs.
A single-process tensor-parallel two-layer MLP including biases and gradients, with tanh-approximate GeLU and no distributed communication.
Source: Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism · Section 3, Figure 3: column-partitioned first GEMM and row-partitioned second GEMM.
2. Core equations
$$ Y=\sum_{r=0}^{P-1}\operatorname{GeLU}(XA_r+a_r)B_r+b. $$
3. From equations to code
parallel_mlp(x: Tensor, w1: Tensor, b1: Tensor, w2: Tensor, b2: Tensor, parts: int) -> Tensor— (N,O) output with autograd through every floating input.
Each shard sees all input features and owns only a hidden-channel slice. GeLU sits between local matrix products. Sum second-layer contributions without averaging.
4. Conventions
x:(N,D), w1:(D,H), b1:(H,), w2:(H,O), b2:(O,). H is divisible by parts. Partition first-layer columns and corresponding second-layer rows. Add b2 once after reduction. Preserve gradients, dtype and input values.
5. Experiment view
Use fixed 9×3 inputs and eight hidden channels; train output weights with two shards for 30 steps and compare loss, weights and gradients against dense execution.
The chart is generated by the actual submitted kernel during T3 and by the reference during export. Values are synthetic teaching measurements, not GPU throughput or full-model quality claims.
6. Run your implementation
Complete the functions in the Python editor on the right, then click Run tests. Inspect each failed check, update your implementation, and run again. Passing every required check marks the problem as solved and unlocks the notes.
7. Further study
Read the cited section and explain which dimensions or scheduling constraints the kernel preserves. Then extend the experiment beyond the stated fixture while retaining the existing contract. Larger models, real devices, and communication costs require separate measurement.