Back to paths

SPMD — One Program Across Every Host

How one jitted program runs on every host of a multi-host slice. You declare how tensors are sharded across a device mesh; the compiler (Shardy) inserts every collective. Data parallelism as the worked example, the Auto/Explicit/Manual control spectrum, and why the first failure is always at rendezvous — a networking bug, not a TPU bug.

intermediate~14 min#ml-systems#parallelism#software
200Single-slice execution & the "no black box" mandateLesson 2 of 2Show

Chapter 2 of 2 in TPU Training Infrastructure

    1. SPMD — One Program Across Every HostYou're here

Prerequisites: The JAX/XLA Stack — No Black Boxes, TPU Topologies — the two networks & the shapes, Provisioning & the TPU VM


The one-line summary

L200 opened the compiler black box on one host (2.1). Now the slice grows: the same binary runs on every host, and the trick is that you never write the networking. You declare how each tensor is split across the devices, and the compiler — — inserts every cross-device communication. The whole lesson in one sentence: describe the layout, the compiler generates the comms. And when a multi-host job breaks, it breaks first at rendezvous — which makes it a networking bug, not a TPU bug.

One program, every host (SPMD)

A multi-host slice doesn't run a driver that farms work out to workers. It runs : every host executes the identical main.py, JAX stitches their local chips into one global device grid, and each host quietly operates on its own shard of the data.

Declaring a sharding

You glue the devices into a — a named grid — and then say how a tensor's dimensions map onto its axes:

mesh = jax.make_mesh((2, 4), ('data', 'model'))   # 8 devices, 2 named axes
sharding = jax.sharding.NamedSharding(mesh, jax.sharding.PartitionSpec('data', None))
x = jax.device_put(batch, sharding)                # batch split across 'data', features replicated

A PartitionSpec is just a per-dimension rule: each entry is None (replicated — every device holds the whole thing), a mesh-axis name (split along that axis), or a tuple of axes. That's the entire declaration. You never said how to communicate — only how the data is laid out.

takes it from there: it propagates your shardings through the jitted program and inserts the (all-reduce, all-gather, reduce-scatter) that make the math come out right on split data.

Pick a mesh and a PartitionSpec below and watch which chip owns which shard, which collective Shardy has to insert, and how many bytes it puts on the wire:

Simulation: spmd-shard-explorer

Data parallelism: the canonical case

The first sharding everyone reaches for is data parallelism: split the batch across the data axis, replicate the parameters on every device. Each device runs the forward and backward pass (the backward pass is just where the gradients come from — no calculus needed here) on its slice of the batch — and then the gradients have to be reconciled, or the replicas drift apart. That reconciliation is exactly one : sum the gradients across every shard, hand everyone the same result, then step the optimizer.

That all-reduce is the dominant communication in data-parallel training, and it rides the fast ring inside a slice — which is why the topology lesson cared so much about putting a long torus axis where the all-reduce lives.

The control spectrum: Auto → Explicit → Manual

"Declare the layout, the compiler does the comms" describes the two declarative modes — and that's where you should start. JAX gives you a spectrum of control:

  • Auto — you annotate a few shardings; Shardy propagates them and inserts every collective. The least code; jax.sharding.Mesh uses this.
  • Explicit ("sharding in types") — the sharding becomes part of a value's JAX type, so it's visible and checkable rather than inferred. jax.make_mesh uses this — so the first code block above is Explicit. (Both modes are declarative: you annotate, the compiler inserts the collectives.)
  • Manual — : you write the per-device code and the collectives (jax.lax.psum, …) yourself. Maximum control, maximum rope.

Rendezvous is a networking step

Before any chip does math, the hosts have to find each other. One call — jax.distributed.initialize() (no arguments on Cloud TPU; it auto-detects) — points every process at a (process 0) and waits at a barrier until all of them check in. Then the program runs.

This is the multi-host trap. The rendezvous rides the — the same host-to-host network with the MTU-8896 and firewall requirements from the provisioning lesson — not the ICI. So the classic first failure is a barrier timeout at startup: one host can't reach the coordinator. The chips are idle; nothing compiled wrong. You look at DCN reachability — firewall rules, the coordinator's IP, MTU — exactly the gang-scheduling / leader-election failure you already know from Kubernetes, not a TPU fault.

And it stays all-or-nothing after startup: an SPMD job is gang-scheduled, so if a host or its DCN link dies mid-run, the whole job goes down — blast radius is the entire slice. You don't lose the run, though: you restart from the last checkpoint (Orbax, the next lesson's star). Plan capacity and checkpoint cadence around that all-or-nothing failure domain.

Why this matters

Every megascale job you'll operate is SPMD: one binary, sharded data, compiler-inserted comms. The engineer who treats the network as magic guesses when it stalls; the one who knows that you declared the layout and Shardy generated the collectives can read where the bytes go, size the all-reduce against ICI, and — when it hangs at the barrier — look at the DCN instead of blaming the chips. That's the difference between a multi-host job that takes an afternoon to bring up and one that takes a week.

Put it together

Steps
  1. Read a sharding off the mesh

    In the explorer, set a 2×4 mesh and try PartitionSpec('data', None), then (None, 'model'), then fully replicated. For each, say which chips hold which shard and which collective Shardy inserts.

  2. Place the all-reduce

    Explain why data-parallel training needs an all-reduce on the backward pass, and which network (ICI vs DCN) you want it to ride on a single slice.

  3. Pick a point on the spectrum

    Name the three control modes (Auto, Explicit, Manual) and say when you'd drop from Auto down to shard_map — and what you give up by doing so.

  4. Debug the hang

    A fresh multi-host job times out at the startup barrier before any step runs. State the single most likely cause and the first three things you'd check.

Review

Flashcards1 / 4

The SPMD contract in one line

Check yourself

Quick exam1 / 3 · 0 correct

Which partitioner does JAX use to insert collectives today?

Your notes on this lesson