Splitting one model across many machines: tensor parallelism from scratch
A 70-billion-parameter model does not fit on one GPU. The weights alone are bigger than any single
device’s memory, so in production the model is split across several — each holds a slice, computes
its piece, and they combine. This week I built that from scratch: tensor parallelism. It’s the week
the two halves of my last two years finally met — the distributed-systems instincts (sharding,
coordination, the cost of agreement) turned out to be exactly the right tools, pointed at a model
instead of a database. It’s also the last of the six core weeks.
What I built
A tensor-parallel GPT-2: each layer’s weight matrices are sharded across nw workers, with one
collective (all-reduce) per sublayer to combine their partial results. Plus a small replica router,
because there are two ways to distribute inference and it’s worth seeing both. The headline is an
invariant, not a speedup:
sharded output vs single-process:
nw=2: max|diff| = 2.8e-13 nw=4: 4.5e-13 nw=12: 3.8e-13 (argmax identical)
Bit-for-bit identical. Splitting the model across a dozen workers changes nothing about what it says.
The concepts that made it work
- Two axes of distribution. Replication runs N full copies behind a router — more requests per
second. Tensor parallelism splits ONE model so it fits at all. Different problems; production
combines them. - Column-then-row is the whole trick. The first matmul is split by columns, the second by rows, so
each worker produces a full-width partial and the partials simply sum. That alignment is what
lets a single all-reduce suffice per sublayer — split the axes the other way and you’d need two
collectives, or get a wrong answer. - Attention shards for free, because heads are independent. Multi-head attention is already a bunch
of parallel computations bolted together; give each worker a subset of heads and there’s nothing to
coordinate until the output projection. - Compute scales out; coordination is the tax. Each worker holds and computes ~
1/nwof the model
— but every sublayer needs an all-reduce, and the communication grows with the worker count. That is
the exact same shape as consensus and sharding in a database: the work divides cleanly; the
agreement between the parts is what costs you.
The thing that bit me
My first sharded run was off — not by noise, by a clean, suspicious amount. With 12 workers the logits
were wrong in a way that scaled with the number of workers. The bug: I added the output-projection
bias inside each worker’s partial, so when the all-reduce summed the twelve partials, it summed the
bias twelve times too. The fix is one line — add the bias once, after the reduce — but the lesson is
sharper than the fix. Tensor parallelism has a brutal, wonderful property: a correct split is
bit-identical to the single-device run, so any deviation at all is a real bug with a real cause. No
“close enough.” That exactness is the best debugging tool a distributed system can give you, and it’s
why I pinned the invariant in a test from the start.
Tradeoffs & the road not taken
- Tensor parallelism only; no pipeline parallelism. The complementary axis splits the model by
layers (stages) rather than within each layer, and pipelines microbatches through them. A clean
stretch, and the other half of how real clusters are carved up. - In-process, so no actual speedup. I model the workers and count the communication; I don’t run
them on separate devices. The point was the sharding math and the compute/comm tradeoff, not wall-clock. - Simplified all-reduce. I count data volume, not a precise ring-all-reduce schedule.
Where this sits — six weeks, one system
That completes the core sprint, and it reads as one sentence: a model that reads (the forward pass),
reads fast (a KV-cache), finds relevant text (a vector index), answers from data (RAG, with
an honest eval harness), acts (an agent loop), and now runs at scale (sharded across workers).
All from scratch, in numpy, each layer pinned by a correctness test and explained with its own real
numbers. The machine that felt like magic six weeks ago is, piece by piece, no longer a black box.
What’s left is a capstone that ties it together, and the parts I deliberately deferred — running a
real model locally (quantization, then llama.cpp / Ollama / MLX), vision and multimodal, and the one
genuine gap: actually training one. The same spine will carry those too: one system, a layer at a time.
Part of a series building a modern AI stack from scratch, told visually. (Last time: a ReAct agent loop.)