Hand-rolled TP MLP on two CPU processes
CPU · your Macscript150 minrequiredGoal · Implement ColumnParallelLinear and RowParallelLinear with gloo all-reduce and show a two-rank TP MLP matches a single-process MLP bit-for-bit within float tolerance.
- 1Create tp_mlp.py; init_process_group(backend="gloo") and read rank/world_size from torchrun environment variables.
- 2On rank 0 build a reference nn.Sequential(Linear(d, 4d), GELU(), Linear(4d, d)) with a fixed seed and broadcast its weights to all ranks.
- 3Implement ColumnParallelLinear that keeps columns [rank*4d/ws, (rank+1)*4d/ws) of W1 and the matching bias slice.
- 4Implement RowParallelLinear that keeps the matching rows of W2, computes a partial product, then calls dist.all_reduce(out, op=SUM) and adds the bias only on one rank or divides it by world_size.
- 5Run the sharded forward and compare with the reference forward on rank 0 using torch.allclose(atol=1e-5).
- 6Count bytes passed through all_reduce per forward and print them; vary d in {256, 1024, 4096} and batch in {1, 8, 64}.
- 7Launch with torchrun --nproc_per_node=2 tp_mlp.py and then with 4 processes.
- Deliverable
- tp_mlp.py and a table of (d, batch, world_size, bytes communicated, max abs error).
- Check
- allclose passes for all configurations; bytes communicated equal batch * d * 4 per all-reduce for fp32 and are independent of world_size.