Build your own tensor-parallel GPT-2 on CPU ranks
CPU · your Mac~10 hTake the Phase 1 GPT-2 engine and shard it across 2 and 4 torch.distributed gloo processes with column- and row-parallel linears and head-parallel attention, so every rank holds only its slice of the weights and KV cache and the fleet produces logits identical to the single-process model. You then measure exactly how many bytes cross between ranks per token and show that the count matches the formula. This proves you understand how real engines split a model, not just that you can set tensor_parallel_size.
Scope
- ColumnParallelLinear, RowParallelLinear and a head-parallel attention module with a per-rank KV cache, each unit-tested against the dense equivalent.
- A sharded weight loader that reads the Hugging Face GPT-2 state dict and keeps only the rank's slice, splitting the fused c_attn by head.
- A torchrun entry point that generates 64 greedy tokens for a set of prompts at world sizes 1, 2 and 4 and saves logits per step.
- A communication accountant that wraps dist collectives and reports calls and bytes per decode step, compared with the closed form.
- A bench.py that reports tokens per second and communication share per world size, with a README explaining why gloo on CPU does not scale and what would change on NVLink.
Out of scope
- Pipeline parallelism or combining TP with PP.
- Any GPU or NCCL code; this is CPU fp32 on one laptop.
- Training, sequence parallelism or fused kernels.
What to measure
- Logit agreement with the single-process model
- How · torch.allclose on every decode step for 5 prompts and 64 tokens, atol 1e-4, at world 2 and 4
- Target · all pass
- Per-rank parameter count
- How · sum of numel over the rank's tensors compared with the formula embeddings + (124,439,808 - embeddings) / world
- Target · exact match at world 2 and 4
- All-reduce bytes per decode step
- How · counter on dist.all_reduce during one decode step at batch 1
- Target · exactly 24 calls and 24 * 768 * 4 = 73,728 bytes
- Per-rank KV cache bytes at 132 tokens
- How · sum of cache tensor bytes on one rank
- Target · 9,732,096 / world exactly
- Tokens per second at world 1, 2 and 4
- How · 128 greedy tokens after a 32-token prompt, median of 5, threads pinned per rank
- Target · report; explain the trend with the measured communication share
Pay attention to
- Split c_attn per head, not by contiguous columns; a column split silently mixes Q, K and V across ranks and still runs.
- Embeddings and layer norms are replicated; do not count them as sharded in the parameter formula.
- Dropping the all-reduce after the row-parallel layer produces plausible but wrong logits; the logit test must compare every step, not only argmax tokens.
- Pin torch.set_num_threads per rank so 4 ranks do not fight for the same cores and make the benchmark meaningless.
- The per-rank KV cache must store only the rank's heads; storing all heads passes the logit test but fails the memory target.
Acceptance (all must pass)
- ☐pytest passes the logit-agreement, parameter-count and byte-count tests at world 1, 2 and 4.
- ☐Greedy tokens at world 4 equal Hugging Face generate(do_sample=False) for 3 prompts over 64 tokens.
- ☐The communication accountant's per-step bytes match the closed form in the README table.
- ☐README contains the tokens-per-second table and a paragraph on the communication-to-compute ratio on CPU versus NVLink.