Phase 09 / Distributed training
Resilient distributed training
Revision v0.18.1 · Updated since your last visit. Review the changes; your progress is saved.
Lesson · about 105 minutes · Logical CPU devices
Restore a complete training state with explicit target sharding. Compare next sample IDs, optimizer state and losses across an epoch boundary.
Phase study guide: preparation, milestones and project evidence
Before you start
Before this: Communication-efficient algorithms
Hardware: 4 logical CPU devices; target cluster execution unverified
Setup help: Python, notebook, or Cloud TPU VM
Use the tested CPU packages (the recorded environment uses Python 3.14.3). Other Python versions require separate checks. Hardware receipts apply only to their recorded source, environment, hardware, and cases; they do not qualify every lesson or TPU configuration. See the TPU performance and hardware evidence guide. The optional Cloud TPU specialization explains target-specific execution boundaries. Logical CPU devices practice partitioning; they do not simulate TPU performance.
Download the repository ZIP or use your existing checkout. Extract it and open a terminal in the folder containing requirements-cpu.txt. The beginner ZIP is separate; these lessons use the full repository folder.
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements-cpu.txtpy -3 -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements-cpu.txtAfter activation, run the complete worked example from that same folder:
python phases/09-distributed/04-resilient-distributed-training/code/main.py.\.venv\Scripts\python.exe phases/09-distributed/04-resilient-distributed-training/code/main.pyFor an incremental build, create main.py in your own practice folder, append each build block in order, and run python main.py with this environment active. Keep that file separate from the downloaded reference solution.
Download this lesson’s notebook. Open it with a Python kernel using the course environment. Run cells from the top after restarting the kernel; experiments depend on earlier definitions.
For local Jupyter, first install the course requirements using the Local Python steps, then use your environment’s Python to start Jupyter:
python -m pip install jupyterlab
python -m jupyter lab.\.venv\Scripts\python.exe -m pip install jupyterlab
.\.venv\Scripts\python.exe -m jupyter labSelect the downloaded distributed-04.ipynb file in JupyterLab. The notebook contains reference solutions; make your prediction and attempt each practice task before opening them. Restart the kernel before changing logical device setup.
The worked reference for this lesson is CPU-validated. Some references explicitly configure logical CPU devices; setting JAX_PLATFORMS does not adapt them to a TPU. Follow the execution matrix and reviewed multi-host example before attempting a target run.
For accelerator preparation, study TPU setup, observed runtime receipts, checkpointed jobs and precision and retained traces. Current receipts cover bounded v5e1 launch, resume, and precision checks; four single-device Pallas cases; and a four-device v5e collective fixture. Each receipt is limited to its recorded source, environment, cases, and hardware. It does not qualify all lessons or TPU configurations. Cloud provisioning is optional for CPU learning.
Check: On the four-device CPU fixture, restoration starts at completed step 1 with cursor 8. The next three sample-ID lists and complete states agree with uninterrupted execution. Reference losses are approximately 0.5070, 0.1964 and 0.1798. The real checkpoint path is printed.
Revisit the guided CPU setup · Open 19: TPU Setup. Package instructions are CPU instructions; Windows and accelerator environments need their own validation.
What you will be able to do
- Restore a complete training state with explicit target sharding.
- Compare next sample IDs, optimizer state and losses across an epoch boundary.
- Explain single-controller evidence and the additional requirements of a real multi-controller restart.
The problem
A restarted sharded run loads the right weights, yet its next update differs. Which missing state could explain that? We will checkpoint a real sharded training step, restore its parameters, momentum, random key and data position, then compare three subsequent updates across an epoch boundary.
The idea
Distributed recovery must reconstruct a consistent global state. Participants cannot independently load arbitrary local snapshots and assume the pieces describe one completed update. Checkpoint identity and step meaning must agree.
Agree on one global recovery boundary
Two local files from different steps can each load successfully while forming no valid global checkpoint. A shared manifest should identify which pieces belong together and what happens if a required piece is missing.
After restore, compare the next global computation with an uninterrupted reference, including input position and random-state ownership. Silent partial recovery changes the computation instead of continuing it.
The CPU replay checks this bounded fixture's continuation. It is separate from multi-host failure handling, storage durability or accelerator networking. Those require additional operational evidence when deploying the design.
Name the values that determine the next transition
The state contains weights , momentum , a random key, the current example permutation, the cursor into that permutation and the completed step. A batch is selected from the saved order, then a global mean gradient updates momentum and weights. After the final batch in an epoch, a new split key creates the next order. Saving only the current permutation can therefore reproduce the first resumed batch but fail at the next epoch.
Checkpoint one completed global step
The example partitions examples across four logical CPU devices and keeps its small parameter state replicated. It waits for the step loss, saves the entire state with a real Orbax StandardCheckpointer, waits for saving to finish and restores against a target whose arrays carry the intended mesh and sharding. Every restored leaf is compared with the saved snapshot.
The dataset is a deterministic tiny NumPy fixture. Every controller can reconstruct it from the same declared seed, dimensions and target rule. Real input systems need data identity, preprocessing configuration and iterator recovery, as covered in the recovery phase; matching a seed alone is not enough for a changing dataset. Changing the number of devices here changes fixture dimensions and is deliberately outside this restart contract.
Prove the next updates, including the next epoch
After restoring step 1, compare the next three transitions in order. The recorded sample IDs are , then , then . The second batch begins a new shuffled epoch, so matching it checks the saved random key as well as the current permutation and cursor.
The pre-update batch losses match at about , and every state leaf is also compared after each update. A separate NumPy gradient formula checks the update itself, since two runs could reproduce the same bug. If momentum were omitted, the same next sample IDs would still appear but the weights would differ; if the key were wrong, the current-epoch batch could match and the next-epoch IDs would expose it.
Move the protocol to multiple controllers explicitly
The default run has one controller and four logical CPU devices. It does not kill a worker or test cross-host storage failure. The optional COURSE_MULTIPROCESS branch calls jax.distributed.initialize before any device query and builds a global mesh. Every controller must run the same script, take the same branches and call save and restore in the same order. The tiny fixture is intentionally duplicated on each host; the callback supplies only the slices required by each addressable shard.
On a supported existing multi-host TPU environment, give every worker the same new shared checkpoint directory and run the command below on every worker. The launcher must supply the environment that JAX uses for initialization. A path local to only one worker is not shared storage. For a fresh-process restart of this drill, run every controller again with COURSE_RESUME=1 and the same checkpoint directory. Resume skips saving and restores the existing accepted step-one snapshot; the script recomputes the deterministic step-one baseline only to verify this teaching fixture. A general training job must select and validate its own accepted step. Restart the whole worker group according to your job manager; this lesson does not implement elastic membership or automatic worker replacement. This target branch is executable but has not been run on a multi-host cluster here.
Run instructions: Distribute the script and launch across every worker of a multi-host TPU slice
# Distribute the script and launch across every worker of a multi-host TPU slice
gcloud compute tpus tpu-vm scp exercises/distributed-04.py $TPU_NAME:~/jax-tpu-lab/ --zone=$ZONE --worker=all
gcloud compute tpus tpu-vm ssh $TPU_NAME --zone=$ZONE --worker=all \
--command="COURSE_MULTIPROCESS=1 JAX_PLATFORMS=tpu COURSE_CHECKPOINT_DIR=/shared/new-recovery-run ~/jax-tpu-lab/.venv/bin/python ~/jax-tpu-lab/distributed-04.py"Expected:
All workers initialize distributed JAX, coordinate, save and restore through the same shared path, and report tpu with a process count greater than one. This command needs a configured multi-host TPU slice and shared storage.
Run instructions: Resume the same accepted checkpoint across every TPU slice worker
# Resume the same accepted checkpoint across every TPU slice worker
gcloud compute tpus tpu-vm ssh $TPU_NAME --zone=$ZONE --worker=all \
--command="COURSE_MULTIPROCESS=1 COURSE_RESUME=1 JAX_PLATFORMS=tpu COURSE_CHECKPOINT_DIR=/shared/new-recovery-run ~/jax-tpu-lab/.venv/bin/python ~/jax-tpu-lab/distributed-04.py"Expected:
The worker group loads its existing step-one checkpoint without overwriting it, then verifies subsequent updates across the epoch boundary.
Reject an inconsistent recovery point
The fault-injection checks change one restored value at a time. Removing momentum changes weights by about on the next update despite matching sample IDs; replacing the key preserves the current batch but changes the next epoch's permutation. Those outcomes show why a checkpoint boundary includes optimizer and random state, not only parameters.
This replay uses one controller and four logical CPU devices. It validates the saved fixture and its next transitions; it does not exercise a worker crash, shared-storage durability or elastic membership. For a multi-controller run, all workers must restore the same accepted step and data identity before any update, then compare a global reference rather than treating readable local files as proof of a consistent checkpoint.
Restored updates reproduce the reference trajectory
Predict before looking: Where might two apparently equal restarts first diverge if the next epoch needs an unsaved random key?
Read the figure
The horizontal axis counts the first, second and third updates after the saved step. The vertical axis is batch mean squared loss in squared target units before each update. The uninterrupted and restored lines overlap at , and on this CPU fixture. The second point is the first batch of a newly shuffled epoch, so its match follows a new-key transition, not merely a continuation through the old permutation.
Connect it to the computation
The line comparison is a quick replay check; the code also matches sample IDs and every state leaf, and compares each update against an independent NumPy gradient. If the random key were wrong, the first point could still match but the second batch and its loss would diverge. If momentum were missing, IDs would match while the updated state would not. The plot says nothing about multi-host failure recovery or throughput.
Figure data and execution details
Open full-size figureDownload data and provenance
The notebook contains the data experiment, plotting code, and recorded figure output. Rerun it to test your changes.
Your build: Create a separate main.py in your practice folder. Append each block in order and run python main.py in the lesson environment. The downloaded script is the complete reference, so keep it separate from your attempt.
1 Initialize the runtime before device access
Add this block after the preceding block in main.py and run the assembled file in a fresh process.
# Step 1 — Initialize the runtime before device access
import os
import tempfile
from pathlib import Path
import jax
# Read `multi` from environment configuration (with a default fallback).
multi = os.environ.get('COURSE_MULTIPROCESS') == '1'
# Read `resume` from environment configuration (with a default fallback).
resume = os.environ.get('COURSE_RESUME') == '1'
if multi:
# Every controller runs this before querying devices or creating arrays.
jax.distributed.initialize()
else:
jax.config.update('jax_platforms', 'cpu')
jax.config.update('jax_num_cpu_devices', 4)
import jax.numpy as jnp
import numpy as np
import orbax.checkpoint as ocp
from jax.sharding import Mesh, NamedSharding, PartitionSpec as PEvery controller must use consistent state, data and collective order; the default exercise runs on four logical CPU devices.
2 Define the global fixture and placements
Add this block after the preceding block in main.py and run the assembled file in a fresh process.
# Step 2 — Define the global fixture and placements
count = jax.device_count()
assert count >= 2, 'Use at least two devices for the placement exercise'
mesh = Mesh(np.asarray(jax.devices()), ('data',))
rep = NamedSharding(mesh, P())
row = NamedSharding(mesh, P('data', None))
label = NamedSharding(mesh, P('data'))
batch_size, size, width = 2 * count, 4 * count, 2 * count
# Identical tiny synthetic fixture on each controller; not distributed data ingestion.
data = np.random.default_rng(41).normal(size=(size, width)).astype(np.float32)
truth = np.linspace(-0.3, 0.4, width, dtype=np.float32)
targets = data @ truthEvery controller must use consistent state, data and collective order; the default exercise runs on four logical CPU devices.
Explain the APIs in this step
Mesh(devices, axis_names): Names device axes in a mesh used to describe array placement.NamedSharding(mesh, spec): Combines a device mesh and partition specification to describe array placement.
3 Carry complete transition state
Add this block after the preceding block in main.py and run the assembled file in a fresh process.
# Step 3 — Carry complete transition state
def placed(value, sharding=rep):
# Transfer the device result to the host for an independent NumPy comparison.
a = np.asarray(value)
return jax.make_array_from_callback(a.shape, sharding, lambda index: a[index])Every controller must use consistent state, data and collective order; the default exercise runs on four logical CPU devices.
4 Checkpoint and verify replay
Add this block after the preceding block in main.py and run the assembled file in a fresh process.
# Step 4 — Checkpoint and verify replay
def initial():
# Create or split explicit PRNG key(s) (`(key, order_key)`) for reproducible randomness.
key, order_key = jax.random.split(jax.random.PRNGKey(17))
return { 'weights': placed(np.zeros(width, np.float32)),
'momentum': placed(np.zeros(width, np.float32)),
'key': placed(np.asarray(key)),
'order': placed(np.asarray(jax.random.permutation(order_key, size))),
'cursor': placed(np.int32(0)), 'step': placed(np.int32(0)) }Every controller must use consistent state, data and collective order; the default exercise runs on four logical CPU devices.
Explain the APIs in this step
jax.random.split(key, num=...): Derives new keys for independent random operations; using the same key again repeats its random stream.jax.random.PRNGKey(seed): Creates a legacy-format explicit PRNG key from a seed.
5 Checkpoint and verify replay
Add this block after the preceding block in main.py and run the assembled file in a fresh process.
# Step 5 — Checkpoint and verify replay
# Define and JIT-compile `update(w, velocity, x, y)` so XLA traces and fuses the operations:
@jax.jit
def update(w, velocity, x, y):
loss, grad = jax.value_and_grad(lambda a: jnp.mean((x @ a - y)**2))(w)
v = 0.8 * velocity + grad
return w - 0.04 * v, v, lossEvery controller must use consistent state, data and collective order; the default exercise runs on four logical CPU devices.
Explain the APIs in this step
jax.jit(fn) / @jax.jit: Traces a function for its input signatures and compiles it; changed shapes, dtypes, or static arguments can trigger another compilation.jax.value_and_grad(loss_fn)(params, ...): Transforms a pure function to return its value and gradient(value, grads)from the same evaluation.jnp.mean(x, axis=..., keepdims=...): Averages JAX array values along the selected axis.
6 Checkpoint and verify replay
Add this block after the preceding block in main.py and run the assembled file in a fresh process.
# Step 6 — Checkpoint and verify replay
def transition(state):
cursor = int(state['cursor'])
# Transfer the device result to the host for an independent NumPy comparison.
order = np.asarray(state['order'])
key = state['key']
if cursor == size:
key, shuffle_key = jax.random.split(key)
order = np.asarray(jax.random.permutation(shuffle_key, size))
cursor = 0
ids = order[cursor:cursor + batch_size]
x, y = placed(data[ids], row), placed(targets[ids], label)
w, v, loss = update(state['weights'], state['momentum'], x, y)
# Wait for device work to finish before timing it or comparing its result on the host.
loss.block_until_ready()
# Independent global derivative and momentum update on the host.
old_w, old_v = np.asarray(state['weights']), np.asarray(state['momentum'])
g = 2 * data[ids].T @ (data[ids] @ old_w - targets[ids]) / batch_size
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(v), 0.8 * old_v + g, rtol=2e-5, atol=2e-6)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(w), old_w - 0.04 * np.asarray(v), rtol=2e-5, atol=2e-6)
# Transfer the device result to the host for an independent NumPy comparison.
new = dict(weights=w, momentum=v, key=placed(np.asarray(key)),
order=placed(order), cursor=placed(np.int32(cursor+batch_size)),
step=placed(np.int32(int(state['step'])+1)))
return new, ids.copy(), float(loss)Every controller must use consistent state, data and collective order; the default exercise runs on four logical CPU devices.
Explain the APIs in this step
jax.random.split(key, num=...): Derives new keys for independent random operations; using the same key again repeats its random stream.output.block_until_ready(): Waits for asynchronous device work on this array to finish, which is needed before measuring completed computation time.np.testing.assert_allclose(actual, desired, rtol=..., atol=...): Checks a host-side numerical expectation and raisesAssertionErrorwhen values differ beyond tolerance.
7 Checkpoint and verify replay
Add this block after the preceding block in main.py and run the assembled file in a fresh process.
# Step 7 — Checkpoint and verify replay
state = initial()
state, first_ids, _ = transition(state)
# Shared path is mandatory for real multi-controller execution.
if multi:
root = Path(os.environ['COURSE_CHECKPOINT_DIR']).resolve()
else:
root = Path(os.environ['COURSE_CHECKPOINT_DIR']).resolve() if os.environ.get('COURSE_CHECKPOINT_DIR') else Path(tempfile.mkdtemp(prefix='jax-distributed-recovery-'))
checkpoint_path = root / 'step-one'
if resume and not checkpoint_path.exists():
raise FileNotFoundError('Resume needs an existing step-one checkpoint')
if not resume and checkpoint_path.exists():
raise FileExistsError('Use a new folder or set COURSE_RESUME=1 for the accepted checkpoint')
with ocp.StandardCheckpointer() as checkpointer:
if not resume:
checkpointer.save(checkpoint_path, state)
checkpointer.wait_until_finished()
# Target carries the current mesh/sharding, rather than inferring it from old metadata.
restored = checkpointer.restore(checkpoint_path, target=initial())
for name in state:
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(np.asarray(restored[name]), np.asarray(state[name]))
baseline, resumed = state, restored
baseline_losses, resumed_losses, replay_ids = [], [], []
for _ in range(3):
baseline, ids_a, loss_a = transition(baseline)
resumed, ids_b, loss_b = transition(resumed)
np.testing.assert_array_equal(ids_a, ids_b)
for name in baseline:
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(baseline[name]), np.asarray(resumed[name]), rtol=2e-5, atol=2e-6)
baseline_losses.append(loss_a)
resumed_losses.append(loss_b)
replay_ids.append(ids_a.tolist())
print('Recorded backend/processes/devices:', jax.default_backend(), jax.process_count(), count)
print('Restored step/cursor:', int(restored['step']), int(restored['cursor']))
print('Next sample IDs across epoch boundary:', replay_ids)
print('Uninterrupted losses:', baseline_losses)
print('Restored losses:', resumed_losses)
print('Checkpoint:', checkpoint_path)Every controller must use consistent state, data and collective order; the default exercise runs on four logical CPU devices.
Explain the APIs in this step
np.testing.assert_allclose(actual, desired, rtol=..., atol=...): Checks a host-side numerical expectation and raisesAssertionErrorwhen values differ beyond tolerance.
View the cumulative build in main.py
These blocks append in order to your separate practice file.
# Step 1 — Initialize the runtime before device access
import os
import tempfile
from pathlib import Path
import jax
# Read `multi` from environment configuration (with a default fallback).
multi = os.environ.get('COURSE_MULTIPROCESS') == '1'
# Read `resume` from environment configuration (with a default fallback).
resume = os.environ.get('COURSE_RESUME') == '1'
if multi:
# Every controller runs this before querying devices or creating arrays.
jax.distributed.initialize()
else:
jax.config.update('jax_platforms', 'cpu')
jax.config.update('jax_num_cpu_devices', 4)
import jax.numpy as jnp
import numpy as np
import orbax.checkpoint as ocp
from jax.sharding import Mesh, NamedSharding, PartitionSpec as P
# Step 2 — Define the global fixture and placements
count = jax.device_count()
assert count >= 2, 'Use at least two devices for the placement exercise'
mesh = Mesh(np.asarray(jax.devices()), ('data',))
rep = NamedSharding(mesh, P())
row = NamedSharding(mesh, P('data', None))
label = NamedSharding(mesh, P('data'))
batch_size, size, width = 2 * count, 4 * count, 2 * count
# Identical tiny synthetic fixture on each controller; not distributed data ingestion.
data = np.random.default_rng(41).normal(size=(size, width)).astype(np.float32)
truth = np.linspace(-0.3, 0.4, width, dtype=np.float32)
targets = data @ truth
# Step 3 — Carry complete transition state
def placed(value, sharding=rep):
# Transfer the device result to the host for an independent NumPy comparison.
a = np.asarray(value)
return jax.make_array_from_callback(a.shape, sharding, lambda index: a[index])
# Step 4 — Checkpoint and verify replay
def initial():
# Create or split explicit PRNG key(s) (`(key, order_key)`) for reproducible randomness.
key, order_key = jax.random.split(jax.random.PRNGKey(17))
return { 'weights': placed(np.zeros(width, np.float32)),
'momentum': placed(np.zeros(width, np.float32)),
'key': placed(np.asarray(key)),
'order': placed(np.asarray(jax.random.permutation(order_key, size))),
'cursor': placed(np.int32(0)), 'step': placed(np.int32(0)) }
# Step 5 — Checkpoint and verify replay
# Define and JIT-compile `update(w, velocity, x, y)` so XLA traces and fuses the operations:
@jax.jit
def update(w, velocity, x, y):
loss, grad = jax.value_and_grad(lambda a: jnp.mean((x @ a - y)**2))(w)
v = 0.8 * velocity + grad
return w - 0.04 * v, v, loss
# Step 6 — Checkpoint and verify replay
def transition(state):
cursor = int(state['cursor'])
# Transfer the device result to the host for an independent NumPy comparison.
order = np.asarray(state['order'])
key = state['key']
if cursor == size:
key, shuffle_key = jax.random.split(key)
order = np.asarray(jax.random.permutation(shuffle_key, size))
cursor = 0
ids = order[cursor:cursor + batch_size]
x, y = placed(data[ids], row), placed(targets[ids], label)
w, v, loss = update(state['weights'], state['momentum'], x, y)
# Wait for device work to finish before timing it or comparing its result on the host.
loss.block_until_ready()
# Independent global derivative and momentum update on the host.
old_w, old_v = np.asarray(state['weights']), np.asarray(state['momentum'])
g = 2 * data[ids].T @ (data[ids] @ old_w - targets[ids]) / batch_size
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(v), 0.8 * old_v + g, rtol=2e-5, atol=2e-6)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(w), old_w - 0.04 * np.asarray(v), rtol=2e-5, atol=2e-6)
# Transfer the device result to the host for an independent NumPy comparison.
new = dict(weights=w, momentum=v, key=placed(np.asarray(key)),
order=placed(order), cursor=placed(np.int32(cursor+batch_size)),
step=placed(np.int32(int(state['step'])+1)))
return new, ids.copy(), float(loss)
# Step 7 — Checkpoint and verify replay
state = initial()
state, first_ids, _ = transition(state)
# Shared path is mandatory for real multi-controller execution.
if multi:
root = Path(os.environ['COURSE_CHECKPOINT_DIR']).resolve()
else:
root = Path(os.environ['COURSE_CHECKPOINT_DIR']).resolve() if os.environ.get('COURSE_CHECKPOINT_DIR') else Path(tempfile.mkdtemp(prefix='jax-distributed-recovery-'))
checkpoint_path = root / 'step-one'
if resume and not checkpoint_path.exists():
raise FileNotFoundError('Resume needs an existing step-one checkpoint')
if not resume and checkpoint_path.exists():
raise FileExistsError('Use a new folder or set COURSE_RESUME=1 for the accepted checkpoint')
with ocp.StandardCheckpointer() as checkpointer:
if not resume:
checkpointer.save(checkpoint_path, state)
checkpointer.wait_until_finished()
# Target carries the current mesh/sharding, rather than inferring it from old metadata.
restored = checkpointer.restore(checkpoint_path, target=initial())
for name in state:
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(np.asarray(restored[name]), np.asarray(state[name]))
baseline, resumed = state, restored
baseline_losses, resumed_losses, replay_ids = [], [], []
for _ in range(3):
baseline, ids_a, loss_a = transition(baseline)
resumed, ids_b, loss_b = transition(resumed)
np.testing.assert_array_equal(ids_a, ids_b)
for name in baseline:
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(baseline[name]), np.asarray(resumed[name]), rtol=2e-5, atol=2e-6)
baseline_losses.append(loss_a)
resumed_losses.append(loss_b)
replay_ids.append(ids_a.tolist())
print('Recorded backend/processes/devices:', jax.default_backend(), jax.process_count(), count)
print('Restored step/cursor:', int(restored['step']), int(restored['cursor']))
print('Next sample IDs across epoch boundary:', replay_ids)
print('Uninterrupted losses:', baseline_losses)
print('Restored losses:', resumed_losses)
print('Checkpoint:', checkpoint_path)Run the example
python3 phases/09-distributed/04-resilient-distributed-training/code/main.pyAbout this command
This shortcut uses an activated macOS/Linux course environment in the repository folder. For Windows, use the explicit interpreter command in “Setup help” above. The script includes worked examples and reference solutions.
Inspect the complete assembled example
# Step 1 — Initialize the runtime before device access
import os
import tempfile
from pathlib import Path
import jax
# Read `multi` from environment configuration (with a default fallback).
multi = os.environ.get('COURSE_MULTIPROCESS') == '1'
# Read `resume` from environment configuration (with a default fallback).
resume = os.environ.get('COURSE_RESUME') == '1'
if multi:
# Every controller runs this before querying devices or creating arrays.
jax.distributed.initialize()
else:
jax.config.update('jax_platforms', 'cpu')
jax.config.update('jax_num_cpu_devices', 4)
import jax.numpy as jnp
import numpy as np
import orbax.checkpoint as ocp
from jax.sharding import Mesh, NamedSharding, PartitionSpec as P
# Step 2 — Define the global fixture and placements
count = jax.device_count()
assert count >= 2, 'Use at least two devices for the placement exercise'
mesh = Mesh(np.asarray(jax.devices()), ('data',))
rep = NamedSharding(mesh, P())
row = NamedSharding(mesh, P('data', None))
label = NamedSharding(mesh, P('data'))
batch_size, size, width = 2 * count, 4 * count, 2 * count
# Identical tiny synthetic fixture on each controller; not distributed data ingestion.
data = np.random.default_rng(41).normal(size=(size, width)).astype(np.float32)
truth = np.linspace(-0.3, 0.4, width, dtype=np.float32)
targets = data @ truth
# Step 3 — Carry complete transition state
def placed(value, sharding=rep):
# Transfer the device result to the host for an independent NumPy comparison.
a = np.asarray(value)
return jax.make_array_from_callback(a.shape, sharding, lambda index: a[index])
# Step 4 — Checkpoint and verify replay
def initial():
# Create or split explicit PRNG key(s) (`(key, order_key)`) for reproducible randomness.
key, order_key = jax.random.split(jax.random.PRNGKey(17))
return { 'weights': placed(np.zeros(width, np.float32)),
'momentum': placed(np.zeros(width, np.float32)),
'key': placed(np.asarray(key)),
'order': placed(np.asarray(jax.random.permutation(order_key, size))),
'cursor': placed(np.int32(0)), 'step': placed(np.int32(0)) }
# Step 5 — Checkpoint and verify replay
# Define and JIT-compile `update(w, velocity, x, y)` so XLA traces and fuses the operations:
@jax.jit
def update(w, velocity, x, y):
loss, grad = jax.value_and_grad(lambda a: jnp.mean((x @ a - y)**2))(w)
v = 0.8 * velocity + grad
return w - 0.04 * v, v, loss
# Step 6 — Checkpoint and verify replay
def transition(state):
cursor = int(state['cursor'])
# Transfer the device result to the host for an independent NumPy comparison.
order = np.asarray(state['order'])
key = state['key']
if cursor == size:
key, shuffle_key = jax.random.split(key)
order = np.asarray(jax.random.permutation(shuffle_key, size))
cursor = 0
ids = order[cursor:cursor + batch_size]
x, y = placed(data[ids], row), placed(targets[ids], label)
w, v, loss = update(state['weights'], state['momentum'], x, y)
# Wait for device work to finish before timing it or comparing its result on the host.
loss.block_until_ready()
# Independent global derivative and momentum update on the host.
old_w, old_v = np.asarray(state['weights']), np.asarray(state['momentum'])
g = 2 * data[ids].T @ (data[ids] @ old_w - targets[ids]) / batch_size
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(v), 0.8 * old_v + g, rtol=2e-5, atol=2e-6)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(w), old_w - 0.04 * np.asarray(v), rtol=2e-5, atol=2e-6)
# Transfer the device result to the host for an independent NumPy comparison.
new = dict(weights=w, momentum=v, key=placed(np.asarray(key)),
order=placed(order), cursor=placed(np.int32(cursor+batch_size)),
step=placed(np.int32(int(state['step'])+1)))
return new, ids.copy(), float(loss)
# Step 7 — Checkpoint and verify replay
state = initial()
state, first_ids, _ = transition(state)
# Shared path is mandatory for real multi-controller execution.
if multi:
root = Path(os.environ['COURSE_CHECKPOINT_DIR']).resolve()
else:
root = Path(os.environ['COURSE_CHECKPOINT_DIR']).resolve() if os.environ.get('COURSE_CHECKPOINT_DIR') else Path(tempfile.mkdtemp(prefix='jax-distributed-recovery-'))
checkpoint_path = root / 'step-one'
if resume and not checkpoint_path.exists():
raise FileNotFoundError('Resume needs an existing step-one checkpoint')
if not resume and checkpoint_path.exists():
raise FileExistsError('Use a new folder or set COURSE_RESUME=1 for the accepted checkpoint')
with ocp.StandardCheckpointer() as checkpointer:
if not resume:
checkpointer.save(checkpoint_path, state)
checkpointer.wait_until_finished()
# Target carries the current mesh/sharding, rather than inferring it from old metadata.
restored = checkpointer.restore(checkpoint_path, target=initial())
for name in state:
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(np.asarray(restored[name]), np.asarray(state[name]))
baseline, resumed = state, restored
baseline_losses, resumed_losses, replay_ids = [], [], []
for _ in range(3):
baseline, ids_a, loss_a = transition(baseline)
resumed, ids_b, loss_b = transition(resumed)
np.testing.assert_array_equal(ids_a, ids_b)
for name in baseline:
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(baseline[name]), np.asarray(resumed[name]), rtol=2e-5, atol=2e-6)
baseline_losses.append(loss_a)
resumed_losses.append(loss_b)
replay_ids.append(ids_a.tolist())
print('Recorded backend/processes/devices:', jax.default_backend(), jax.process_count(), count)
print('Restored step/cursor:', int(restored['step']), int(restored['cursor']))
print('Next sample IDs across epoch boundary:', replay_ids)
print('Uninterrupted losses:', baseline_losses)
print('Restored losses:', resumed_losses)
print('Checkpoint:', checkpoint_path)Expected result: On the four-device CPU fixture, restoration starts at completed step 1 with cursor 8. The next three sample-ID lists and complete states agree with uninterrupted execution. Reference losses are approximately 0.5070, 0.1964 and 0.1798. The real checkpoint path is printed.
Recorded output and runtime details
Reference output captured from executing the complete lesson script (code/main.py) and its assertions in the course CPU environment.
Recorded backend/processes/devices: cpu 1 4
Restored step/cursor: 1 8
Next sample IDs across epoch boundary: [[12, 5, 4, 1, 8, 13, 2, 11], [9, 0, 7, 15, 11, 13, 8, 14], [1, 6, 5, 4, 3, 12, 2, 10]]
Uninterrupted losses: [0.5069587826728821, 0.19637340307235718, 0.1798383891582489]
Restored losses: [0.5069587826728821, 0.19637340307235718, 0.1798383891582489]
Checkpoint: /var/folders/mp/_mq7srqx2y10p5nhjz9m7hj40000gn/T/jax-distributed-recovery-9_msq6j2/step-one
Recorded backend/processes/devices: cpu 1 4
Restored step/cursor: 1 8
Next sample IDs across epoch boundary: [[12, 5, 4, 1, 8, 13, 2, 11], [9, 0, 7, 15, 11, 13, 8, 14], [1, 6, 5, 4, 3, 12, 2, 10]]
Uninterrupted losses: [0.5069587826728821, 0.19637340307235718, 0.1798383891582489]
Restored losses: [0.5069587826728821, 0.19637340307235718, 0.1798383891582489]
Checkpoint: /var/folders/mp/_mq7srqx2y10p5nhjz9m7hj40000gn/T/jax-distributed-recovery-yxa817hp/step-one
Same next sample IDs, different weights after missing momentum: 0.0195157527923584
Five resumed transitions and all addressable parameter replicas agree.
Current-epoch batch matches; next-epoch batch exposes the wrong key.
PASS: distributed-04
Download execution record · The notebook includes executed cells and figure outputs. Compare your run with these values; timings depend on the machine.
Keep weights but discard momentum
# Experiment — Keep weights but discard momentum: The data stream is correct in this injected failure.
incomplete = {**restored, 'momentum': placed(np.zeros(width, np.float32))}
wrong_next, wrong_ids, _ = transition(incomplete)
right_next, right_ids, _ = transition(restored)
np.testing.assert_array_equal(wrong_ids, right_ids)
weight_gap = float(np.max(np.abs(np.asarray(wrong_next['weights']) - np.asarray(right_next['weights']))))
assert weight_gap > 1e-5
print('Same next sample IDs, different weights after missing momentum:', weight_gap)Expected: The next sample IDs match but the updated weights differ.
The data stream is correct in this injected failure. The missing optimizer memory explains the different update, so restarting the data loader would not repair it.
Make it yours
Where to put your code: Modify your own main.py, or use the Make it yours cell in your downloaded notebook. Run your attempt in the declared environment and verify your prediction before opening the reference solution.
Extend uninterrupted and restored execution by two additional steps. Verify sample IDs, complete state and every local parameter replica. Explain which comparison would fail if a single replica held a different parameter vector.
Show a writing guide and starter scaffold
How to write this exercise code
Learner-only inputs, outputs, checks and hints
Inputs
- An uninterrupted training run and a checkpoint taken before the final two updates.
Outputs
- Sample IDs and full training state after two extra steps on both paths.
- Per-replica parameter comparisons.
Invariants to check
- Restored and uninterrupted paths consume the same subsequent IDs.
- Parameters, optimizer, key, cursor, and step agree after each update.
Hints
- Advance both paths by exactly two updates.
- Compare tree leaves and each local replica, not only the primary state.
import numpy as np
def compare_two_step_resume(uninterrupted, checkpoint, train_step):
# TODO: extend the uninterrupted and restored paths by two updates.
raise NotImplementedError("Verify complete-state replay across two additional steps")Reveal the reference solution
# Exercise solution: Extend uninterrupted and restored execution by two additional steps.
for _ in range(2):
baseline, ids_a, _ = transition(baseline)
resumed, ids_b, _ = transition(resumed)
np.testing.assert_array_equal(ids_a, ids_b)
for name in baseline:
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(baseline[name]), np.asarray(resumed[name]), rtol=2e-5, atol=2e-6)
for shard in resumed['weights'].addressable_shards:
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(shard.data), np.asarray(baseline['weights']), rtol=2e-5, atol=2e-6)
assert int(resumed['step']) == 6
print('Five resumed transitions and all addressable parameter replicas agree.')Find a delayed random-state failure
Transfer
Restore the saved order and cursor but replace the saved key with a different key. Compare the first resumed batch and the following epoch’s first batch. Predict where replay will first fail.
Show a writing guide and starter scaffold
How to write this practice solution
Learner-only inputs, outputs, checks and hints
Inputs
- A checkpoint with saved sample order, cursor, key, parameters, optimizer state, and step.
- A deliberately replaced random key.
Outputs
- First resumed batch and following epoch first batch for both key choices.
Invariants to check
- Restored sample order/cursor initially agree even if later randomness diverges.
- Compare all replay state, not only model parameters.
Hints
- Hold checkpoint fields fixed one at a time.
- Inspect the next epoch boundary where the key affects a fresh permutation.
import numpy as np
def compare_replay_keys(checkpoint, alternate_key):
# TODO: restore twice, changing only the random key.
raise NotImplementedError("Locate the first replay divergence caused by the changed key")Show a hint
The saved order supplies the remaining current-epoch batch. The key is consumed when the next permutation is generated.
Reveal reference solution and reasoning
# Find a delayed random-state failure (Transfer)
# Create or split explicit PRNG key(s) (`wrong_key_state`) for reproducible randomness.
wrong_key_state = {**restored, 'key': placed(np.asarray(jax.random.PRNGKey(999)))}
correct_a, ids_a, _ = transition(restored)
wrong_a, ids_b, _ = transition(wrong_key_state)
np.testing.assert_array_equal(ids_a, ids_b)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(correct_a['weights']), np.asarray(wrong_a['weights']), rtol=2e-5, atol=2e-6)
correct_b, ids_c, _ = transition(correct_a)
wrong_b, ids_d, _ = transition(wrong_a)
assert not np.array_equal(ids_c, ids_d)
print('Current-epoch batch matches; next-epoch batch exposes the wrong key.')A successful first resumed update is separate from future replay when later branches consume state that was not yet used. Crossing the epoch boundary reveals the changed key.
Diagnose the result
Matching IDs with differing weights suggests parameter or optimizer-state mismatch. Differing IDs suggests order, cursor, data identity or shuffle-key mismatch. On multiple hosts also inspect consistent configuration, shared checkpoint access and collective call order; a local CPU pass is distinct from those conditions.
Carry forward
- Restore parameters, optimizer, random and input-position state together.
- Compare more than one resumed update and cross a state-consuming boundary.
- Keep single-host correctness evidence separate from multi-host fault-tolerance evidence.
Keep your evidence
Keep the checkpoint path, saved/restored full state, subsequent sample IDs and losses through a new epoch. Diagnose missing momentum and a changed shuffle key; state the untested multi-host boundary.
Save your modified code, its output, and an explanation of your prediction. Checkpoint answers and exercise evidence are tracked separately.
Saved in this browser. Export or back up your portfolio anytime in My learning.
Reference execution: CPU validation only. This reference configures logical CPU devices to practice placement and sharding. Logical devices do not measure TPU performance. Use the execution matrix to review device selection and data ownership before adapting a separate target program. Execution matrix and target qualification.
Primary references
Phase 09 Integration Project
Bring this phase together: A resilient sharded training experiment
Checkpoint to prove: Explain partitioning, communication costs, and recovery behavior with measured evidence.
This connected project also uses performance and distributed lessons. Work through its prerequisites before the full integration check; return here with the completed evidence. Copy projects/sharded-training/starter/model.py to projects/sharded-training/my_model.py and write your code in projects/sharded-training/my_model.py. Run python3 projects/sharded-training/tests/check.py --implementation projects/sharded-training/my_model.py --stage 1 from the top-level folder to verify stages 1, 2, 3, 4.