Phase 06 / Data & checkpoint recovery
Save model, optimizer, and random state
Revision v0.7.0 · Updated since your last visit. Review the changes; your progress is saved.
Lesson · about 100 minutes · CPU practice
Checkpoint complete training state with Orbax and replay the next update.
Phase study guide: preparation, milestones and project evidence
Before you start
Before this: Load, batch, and checkpoint order with Grain
Hardware: CPU
Setup help: Python, notebook, or Cloud TPU VM
Use the tested CPU packages (the recorded environment uses Python 3.14.3). Other Python versions and accelerator environments require separate checks. The optional Cloud TPU specialization explains the 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/06-recovery/03-save-model-optimizer-and-random-state/code/main.py.\.venv\Scripts\python.exe phases/06-recovery/03-save-model-optimizer-and-random-state/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 recovery-03.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. These target paths remain unqualified on physical TPU hardware until an actual receipt is recorded. Cloud provisioning is optional for CPU learning.
Check: Saved/restored training step and Adam count ; next step , loss and full state agree within stated tolerances. Actual checkpoint path is temporary.
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
- Checkpoint complete training state with Orbax and replay the next update.
- Explain Adam memory and continuation-key requirements.
- Restore known shapes/structure and validate version/data/configuration identity.
- Distinguish tested local synchronous saving from production durability.
The problem
A saved file is useful only if it contains what you need to continue. We’ll train a tiny regression model, save its weights, optimizer state, random key and step with Orbax, and check the next update after restoring. Then we’ll leave out pieces on purpose to see why they matter.
The idea
A training checkpoint needs enough state to reproduce the next update. Weights alone do not capture optimizer memory or random-state ownership. Think of the checkpoint as the complete input to a future state transition.
Save the inputs to the next update
Two runs with identical weights but different Adam moments can receive the same gradient and take different next steps. Different random keys can change augmentation or dropout before the gradient is computed.
The existing diagram merges state components into one snapshot. Its arrows mean membership in the snapshot, not a sequence in which parameters must be computed before moments or randomness. Every component must agree on the completed-step boundary.
Loading bytes successfully checks serialization. Test continuation by restoring into a fresh process and comparing the next transition with an uninterrupted run. Add iterator state through the data-recovery procedure that follows this lesson.
Inventory everything the next step reads
step reads parameters, Adam memory and key_data, then consumes and . It returns new parameters, new optimizer state, the continuation key and an incremented step count. Adam includes first and second gradient moments plus a count for bias correction. Reinitializing it from saved parameters discards those moments.
The noisy targets deliberately make randomness matter, so a key reset cannot pass invisibly. We serialize key_data as uint32 words and explicitly reconstruct a threefry2x32 key. Raw words alone do not identify every possible PRNG implementation; the contract records that choice. The learning rate and Adam identity are recorded because optimizer state does not encode every update rule.
params + Adam moments/count + continuation key + step
+ next batch → next updateSave to a real local checkpoint directory
StandardCheckpointer may perform asynchronous work internally. We call wait_until_finished immediately after save and before restore. save writes an actual Orbax checkpoint under an absolute path in a TemporaryDirectory, then restore reads it back. Calling save alone is not our completion claim. No restore is accepted just because the directory exists: we compare the complete state and its next transition. All files are removed when the temporary workspace is cleaned up; copy a learner-run checkpoint to your chosen evidence folder if you want to keep it.
This example does not implement a cloud retention policy, an asynchronous manager, multi-process synchronization or protection against every storage failure. A successful synchronous local save demonstrates the tested library path and numerical replay. It is not evidence of production durability under a machine or disk failure.
Restore into an explicit state template
The target supplies a known model dictionary and the Adam namedtuple/pytree structure initialized by the same optimizer definition. That prevents relying on an untyped collection of arrays as if it were a complete model. We then compare pytree structure and every leaf, including the continuation key and count.
The compatibility contract is encoded as JSON bytes in a fixed-size uint8 buffer with a separate length. This keeps the restore target shapes known without pickling Python objects. The 4096-byte capacity is a tutorial simplification: oversized metadata fails explicitly. For a different model shape or optimizer, define a migration deliberately rather than claiming a generic restore.
Version and data checks belong to the application
The payload includes schema version, package versions, learning rate, optimizer identity, key implementation and a digest of this exact input/target pair. validate_contract rejects any mismatch before continuing training. A checksum can identify changed data; it does not repair it or authenticate a hostile checkpoint.
The strongest local recovery check is the next loss and full updated state against an uninterrupted transition at the same boundary. We use float32 and allclose with rtol=1e-6, atol=1e-7 for numerical leaves. Keys and counters are integer state whose expected values should remain identical. Changing hardware, reductions or library versions needs new validation; this lesson establishes only the tested CPU environment.
Parameter parity: uninterrupted step vs. Orbax-restored step
Predict before looking: Predict the maximum absolute difference between the uninterrupted next state and the Orbax-restored next state.
Read the figure
Adjacent bars for each parameter leaf have identical L2 norms between the uninterrupted run and the Orbax-restored run.
Connect it to the computation
Verifying exact leaf equality after deserialization guarantees that the restored checkpoint reproduces the exact next training step.
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.
Where to put & run this code: Work in phases/06-recovery/03-save-model-optimizer-and-random-state/code/main.py (or downloaded exercises/recovery-03.py / exercises/recovery-03.ipynb) and execute from the repository root with python3 phases/06-recovery/03-save-model-optimizer-and-random-state/code/main.py.
1 Prepare the data and state
Create main.py in your course workspace. Use the tested course environment and add this block first.
# Step 1 — Prepare the data and state: This setup makes the dataset and software assumptions explicit.
import jax
# Update state in place with the new values.
jax.config.update("jax_platforms","cpu")
import jax.numpy as jnp
import numpy as np
import optax
import orbax.checkpoint as ocp
import tempfile
import json
import hashlib
from pathlib import Path
# Configure or step the Optax optimizer state (`tx`).
tx = optax.adam(0.05)
def initial_state():
params = {"weight":jnp.array(0.,jnp.float32),"bias":jnp.array(0.,jnp.float32)}
return {"params":params,"optimizer":tx.init(params),
"key_data":jax.random.key_data(jax.random.key(7,impl="threefry2x32")),
"step":jnp.array(0,jnp.int32)}
def pack_bytes(raw,capacity=4096):
# Guard input contract (`len(raw) > capacity`) and fail fast if violated.
if len(raw)>capacity:
raise ValueError("Snapshot exceeds tutorial fixed-byte capacity")
# Allocate initialized array `buffer` with the specified shape and dtype.
buffer = np.zeros(capacity,dtype=np.uint8)
buffer[:len(raw)] = np.frombuffer(raw,dtype=np.uint8)
return {"buffer":buffer,"length":np.array(len(raw),dtype=np.int32)}
def unpack_bytes(packed):
length = int(packed["length"])
# Guard input contract (`not 0 <= length <= len(packed['buffer'])`) and fail fast if violated.
if not 0 <= length <= len(packed["buffer"]):
raise ValueError("Invalid snapshot length")
return np.asarray(packed["buffer"],dtype=np.uint8)[:length].tobytes()
def same_tree(left,right):
assert jax.tree.structure(left)==jax.tree.structure(right)
for a,b in zip(jax.tree.leaves(left),jax.tree.leaves(right)):
# Convert `(host_a, host_b)` to a host NumPy array for inspection or verification.
host_a,host_b = np.asarray(a),np.asarray(b)
# Check tensor shape invariant: `host_a.shape==host_b.shape and host_a.dtype==host_b.dtype`
assert host_a.shape==host_b.shape and host_a.dtype==host_b.dtype
if np.issubdtype(host_a.dtype,np.integer):
np.testing.assert_array_equal(host_a,host_b)
else:
np.testing.assert_allclose(host_a,host_b,rtol=1e-6,atol=1e-7)
# Define `step(state, x, y)` to evaluate the objective and its automatic derivatives:
def step(state,x,y):
# Sample deterministic random values into `key` using an explicit PRNG key.
key = jax.random.wrap_key_data(state["key_data"],impl="threefry2x32")
# Create or split explicit PRNG key(s) (`(next_key, sample_key)`) for reproducible randomness.
next_key,sample_key = jax.random.split(key)
# Sample deterministic random values into `target_noise` using an explicit PRNG key.
target_noise = 0.01*jax.random.normal(sample_key,y.shape)
def objective(params):
residual = params["weight"]*x+params["bias"]-(y+target_noise)
return jnp.mean(residual**2)
# Evaluate both scalar loss and parameter gradients in one pass (`(value, gradient)`).
value,gradient = jax.value_and_grad(objective)(state["params"])
updates,opt_state = tx.update(gradient,state["optimizer"],state["params"])
# Initialize explicit deterministic PRNG key `next_state`.
next_state = {"params":optax.apply_updates(state["params"],updates),
"optimizer":opt_state,"key_data":jax.random.key_data(next_key),
"step":state["step"]+1}
return next_state,value
def package_versions():
import importlib.metadata
return {name:importlib.metadata.version(name) for name in ("jax","numpy","optax","orbax-checkpoint")}
def encode_contract(contract):
return pack_bytes(json.dumps(contract,sort_keys=True).encode())
def validate_contract(saved,expected):
actual = json.loads(unpack_bytes(saved))
# Guard input contract (`actual != expected`) and fail fast if violated.
if actual != expected:
raise ValueError("Checkpoint contract differs: verify dataset, optimizer configuration, key implementation and package versions")
workspace = tempfile.TemporaryDirectory()
root = Path(workspace.name)Code building blocks in this step
jnp.array(values, dtype=...)— Constructs an immutable device-backed JAX array from Python/NumPy values.x.sum(axis=...) / x.mean(axis=..., keepdims=...)— Reduces values along the namedaxis(the axis you name is collapsed unlesskeepdims=True).jnp.allclose(actual, expected, rtol=..., atol=...)— Checks that two arrays match elementwise within floating-point tolerance.jax.value_and_grad(loss_fn)(params, ...)— Evaluates both the scalar loss and its gradient PyTree(loss_val, grads)in a single forward+backward pass.
This setup makes the dataset and software assumptions explicit. No external dataset or accelerator is required.
2 Build the pipeline or state transition
Append this block to the same file; follow the named state objects through each function.
# Step 2 — Build the pipeline or state transition: The function boundaries expose which inputs determine the next...
x = jnp.array([0.,0.5,1.],dtype=jnp.float32)
y = 2*x-1
# Convert `contract` to a host NumPy array for inspection or verification.
contract = {"schema":1,"learning_rate":0.05,"optimizer":"adam",
"key_impl":"threefry2x32","versions":package_versions(),
"data_sha256":hashlib.sha256(np.asarray(x).tobytes()+np.asarray(y).tobytes()).hexdigest()}
state = initial_state()
for _ in range(3):
state,_ = step(state,x,y)
checkpoint_path = root/"step_3"
payload = {"training":state,"contract":encode_contract(contract)}
# Enter `ocp.StandardCheckpointer()` context block:
with ocp.StandardCheckpointer() as checkpointer:
checkpointer.save(checkpoint_path,payload)
checkpointer.wait_until_finished()
restored = checkpointer.restore(checkpoint_path,target={"training":initial_state(),"contract":encode_contract(contract)})
validate_contract(restored["contract"],contract)Code building blocks in this step
jnp.array(values, dtype=...)— Constructs an immutable device-backed JAX array from Python/NumPy values.
The function boundaries expose which inputs determine the next output and which state must be preserved.
3 Run the comparison
Append the checks, save main.py and run python main.py. Predict what should agree before running.
# Step 3 — Run the comparison: The comparison checks the next behavior, not merely whether a save...
same_tree(state,restored["training"])
expected_next,expected_loss = step(state,x,y)
restored_next,restored_loss = step(restored["training"],x,y)
same_tree(expected_next,restored_next)
# Convert `` to a host NumPy array for inspection or verification.
np.testing.assert_allclose(np.asarray(expected_loss),np.asarray(restored_loss),rtol=1e-6)
assert int(restored_next["step"])==4
print("Restored step:",int(restored["training"]["step"]))
print("Optimizer count:",int(restored["training"]["optimizer"][0].count))
print("Next update and loss agree; checkpoint exists:",checkpoint_path.exists())Code building blocks in this step
jnp.allclose(actual, expected, rtol=..., atol=...)— Checks that two arrays match elementwise within floating-point tolerance.
The comparison checks the next behavior, not merely whether a save call succeeded.
View the cumulative build in main.py
These blocks append in order to the same file.
# Step 1 — Prepare the data and state: This setup makes the dataset and software assumptions explicit.
import jax
# Update state in place with the new values.
jax.config.update("jax_platforms","cpu")
import jax.numpy as jnp
import numpy as np
import optax
import orbax.checkpoint as ocp
import tempfile
import json
import hashlib
from pathlib import Path
# Configure or step the Optax optimizer state (`tx`).
tx = optax.adam(0.05)
def initial_state():
params = {"weight":jnp.array(0.,jnp.float32),"bias":jnp.array(0.,jnp.float32)}
return {"params":params,"optimizer":tx.init(params),
"key_data":jax.random.key_data(jax.random.key(7,impl="threefry2x32")),
"step":jnp.array(0,jnp.int32)}
def pack_bytes(raw,capacity=4096):
# Guard input contract (`len(raw) > capacity`) and fail fast if violated.
if len(raw)>capacity:
raise ValueError("Snapshot exceeds tutorial fixed-byte capacity")
# Allocate initialized array `buffer` with the specified shape and dtype.
buffer = np.zeros(capacity,dtype=np.uint8)
buffer[:len(raw)] = np.frombuffer(raw,dtype=np.uint8)
return {"buffer":buffer,"length":np.array(len(raw),dtype=np.int32)}
def unpack_bytes(packed):
length = int(packed["length"])
# Guard input contract (`not 0 <= length <= len(packed['buffer'])`) and fail fast if violated.
if not 0 <= length <= len(packed["buffer"]):
raise ValueError("Invalid snapshot length")
return np.asarray(packed["buffer"],dtype=np.uint8)[:length].tobytes()
def same_tree(left,right):
assert jax.tree.structure(left)==jax.tree.structure(right)
for a,b in zip(jax.tree.leaves(left),jax.tree.leaves(right)):
# Convert `(host_a, host_b)` to a host NumPy array for inspection or verification.
host_a,host_b = np.asarray(a),np.asarray(b)
# Check tensor shape invariant: `host_a.shape==host_b.shape and host_a.dtype==host_b.dtype`
assert host_a.shape==host_b.shape and host_a.dtype==host_b.dtype
if np.issubdtype(host_a.dtype,np.integer):
np.testing.assert_array_equal(host_a,host_b)
else:
np.testing.assert_allclose(host_a,host_b,rtol=1e-6,atol=1e-7)
# Define `step(state, x, y)` to evaluate the objective and its automatic derivatives:
def step(state,x,y):
# Sample deterministic random values into `key` using an explicit PRNG key.
key = jax.random.wrap_key_data(state["key_data"],impl="threefry2x32")
# Create or split explicit PRNG key(s) (`(next_key, sample_key)`) for reproducible randomness.
next_key,sample_key = jax.random.split(key)
# Sample deterministic random values into `target_noise` using an explicit PRNG key.
target_noise = 0.01*jax.random.normal(sample_key,y.shape)
def objective(params):
residual = params["weight"]*x+params["bias"]-(y+target_noise)
return jnp.mean(residual**2)
# Evaluate both scalar loss and parameter gradients in one pass (`(value, gradient)`).
value,gradient = jax.value_and_grad(objective)(state["params"])
updates,opt_state = tx.update(gradient,state["optimizer"],state["params"])
# Initialize explicit deterministic PRNG key `next_state`.
next_state = {"params":optax.apply_updates(state["params"],updates),
"optimizer":opt_state,"key_data":jax.random.key_data(next_key),
"step":state["step"]+1}
return next_state,value
def package_versions():
import importlib.metadata
return {name:importlib.metadata.version(name) for name in ("jax","numpy","optax","orbax-checkpoint")}
def encode_contract(contract):
return pack_bytes(json.dumps(contract,sort_keys=True).encode())
def validate_contract(saved,expected):
actual = json.loads(unpack_bytes(saved))
# Guard input contract (`actual != expected`) and fail fast if violated.
if actual != expected:
raise ValueError("Checkpoint contract differs: verify dataset, optimizer configuration, key implementation and package versions")
workspace = tempfile.TemporaryDirectory()
root = Path(workspace.name)
# Step 2 — Build the pipeline or state transition: The function boundaries expose which inputs determine the next...
x = jnp.array([0.,0.5,1.],dtype=jnp.float32)
y = 2*x-1
# Convert `contract` to a host NumPy array for inspection or verification.
contract = {"schema":1,"learning_rate":0.05,"optimizer":"adam",
"key_impl":"threefry2x32","versions":package_versions(),
"data_sha256":hashlib.sha256(np.asarray(x).tobytes()+np.asarray(y).tobytes()).hexdigest()}
state = initial_state()
for _ in range(3):
state,_ = step(state,x,y)
checkpoint_path = root/"step_3"
payload = {"training":state,"contract":encode_contract(contract)}
# Enter `ocp.StandardCheckpointer()` context block:
with ocp.StandardCheckpointer() as checkpointer:
checkpointer.save(checkpoint_path,payload)
checkpointer.wait_until_finished()
restored = checkpointer.restore(checkpoint_path,target={"training":initial_state(),"contract":encode_contract(contract)})
validate_contract(restored["contract"],contract)
# Step 3 — Run the comparison: The comparison checks the next behavior, not merely whether a save...
same_tree(state,restored["training"])
expected_next,expected_loss = step(state,x,y)
restored_next,restored_loss = step(restored["training"],x,y)
same_tree(expected_next,restored_next)
# Convert `` to a host NumPy array for inspection or verification.
np.testing.assert_allclose(np.asarray(expected_loss),np.asarray(restored_loss),rtol=1e-6)
assert int(restored_next["step"])==4
print("Restored step:",int(restored["training"]["step"]))
print("Optimizer count:",int(restored["training"]["optimizer"][0].count))
print("Next update and loss agree; checkpoint exists:",checkpoint_path.exists())Run the example
python3 phases/06-recovery/03-save-model-optimizer-and-random-state/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 — Prepare the data and state: This setup makes the dataset and software assumptions explicit.
import jax
# Update state in place with the new values.
jax.config.update("jax_platforms","cpu")
import jax.numpy as jnp
import numpy as np
import optax
import orbax.checkpoint as ocp
import tempfile
import json
import hashlib
from pathlib import Path
# Configure or step the Optax optimizer state (`tx`).
tx = optax.adam(0.05)
def initial_state():
params = {"weight":jnp.array(0.,jnp.float32),"bias":jnp.array(0.,jnp.float32)}
return {"params":params,"optimizer":tx.init(params),
"key_data":jax.random.key_data(jax.random.key(7,impl="threefry2x32")),
"step":jnp.array(0,jnp.int32)}
def pack_bytes(raw,capacity=4096):
# Guard input contract (`len(raw) > capacity`) and fail fast if violated.
if len(raw)>capacity:
raise ValueError("Snapshot exceeds tutorial fixed-byte capacity")
# Allocate initialized array `buffer` with the specified shape and dtype.
buffer = np.zeros(capacity,dtype=np.uint8)
buffer[:len(raw)] = np.frombuffer(raw,dtype=np.uint8)
return {"buffer":buffer,"length":np.array(len(raw),dtype=np.int32)}
def unpack_bytes(packed):
length = int(packed["length"])
# Guard input contract (`not 0 <= length <= len(packed['buffer'])`) and fail fast if violated.
if not 0 <= length <= len(packed["buffer"]):
raise ValueError("Invalid snapshot length")
return np.asarray(packed["buffer"],dtype=np.uint8)[:length].tobytes()
def same_tree(left,right):
assert jax.tree.structure(left)==jax.tree.structure(right)
for a,b in zip(jax.tree.leaves(left),jax.tree.leaves(right)):
# Convert `(host_a, host_b)` to a host NumPy array for inspection or verification.
host_a,host_b = np.asarray(a),np.asarray(b)
# Check tensor shape invariant: `host_a.shape==host_b.shape and host_a.dtype==host_b.dtype`
assert host_a.shape==host_b.shape and host_a.dtype==host_b.dtype
if np.issubdtype(host_a.dtype,np.integer):
np.testing.assert_array_equal(host_a,host_b)
else:
np.testing.assert_allclose(host_a,host_b,rtol=1e-6,atol=1e-7)
# Define `step(state, x, y)` to evaluate the objective and its automatic derivatives:
def step(state,x,y):
# Sample deterministic random values into `key` using an explicit PRNG key.
key = jax.random.wrap_key_data(state["key_data"],impl="threefry2x32")
# Create or split explicit PRNG key(s) (`(next_key, sample_key)`) for reproducible randomness.
next_key,sample_key = jax.random.split(key)
# Sample deterministic random values into `target_noise` using an explicit PRNG key.
target_noise = 0.01*jax.random.normal(sample_key,y.shape)
def objective(params):
residual = params["weight"]*x+params["bias"]-(y+target_noise)
return jnp.mean(residual**2)
# Evaluate both scalar loss and parameter gradients in one pass (`(value, gradient)`).
value,gradient = jax.value_and_grad(objective)(state["params"])
updates,opt_state = tx.update(gradient,state["optimizer"],state["params"])
# Initialize explicit deterministic PRNG key `next_state`.
next_state = {"params":optax.apply_updates(state["params"],updates),
"optimizer":opt_state,"key_data":jax.random.key_data(next_key),
"step":state["step"]+1}
return next_state,value
def package_versions():
import importlib.metadata
return {name:importlib.metadata.version(name) for name in ("jax","numpy","optax","orbax-checkpoint")}
def encode_contract(contract):
return pack_bytes(json.dumps(contract,sort_keys=True).encode())
def validate_contract(saved,expected):
actual = json.loads(unpack_bytes(saved))
# Guard input contract (`actual != expected`) and fail fast if violated.
if actual != expected:
raise ValueError("Checkpoint contract differs: verify dataset, optimizer configuration, key implementation and package versions")
workspace = tempfile.TemporaryDirectory()
root = Path(workspace.name)
# Step 2 — Build the pipeline or state transition: The function boundaries expose which inputs determine the next...
x = jnp.array([0.,0.5,1.],dtype=jnp.float32)
y = 2*x-1
# Convert `contract` to a host NumPy array for inspection or verification.
contract = {"schema":1,"learning_rate":0.05,"optimizer":"adam",
"key_impl":"threefry2x32","versions":package_versions(),
"data_sha256":hashlib.sha256(np.asarray(x).tobytes()+np.asarray(y).tobytes()).hexdigest()}
state = initial_state()
for _ in range(3):
state,_ = step(state,x,y)
checkpoint_path = root/"step_3"
payload = {"training":state,"contract":encode_contract(contract)}
# Enter `ocp.StandardCheckpointer()` context block:
with ocp.StandardCheckpointer() as checkpointer:
checkpointer.save(checkpoint_path,payload)
checkpointer.wait_until_finished()
restored = checkpointer.restore(checkpoint_path,target={"training":initial_state(),"contract":encode_contract(contract)})
validate_contract(restored["contract"],contract)
# Step 3 — Run the comparison: The comparison checks the next behavior, not merely whether a save...
same_tree(state,restored["training"])
expected_next,expected_loss = step(state,x,y)
restored_next,restored_loss = step(restored["training"],x,y)
same_tree(expected_next,restored_next)
# Convert `` to a host NumPy array for inspection or verification.
np.testing.assert_allclose(np.asarray(expected_loss),np.asarray(restored_loss),rtol=1e-6)
assert int(restored_next["step"])==4
print("Restored step:",int(restored["training"]["step"]))
print("Optimizer count:",int(restored["training"]["optimizer"][0].count))
print("Next update and loss agree; checkpoint exists:",checkpoint_path.exists())Expected result: Saved/restored training step and Adam count ; next step , loss and full state agree within stated tolerances. Actual checkpoint path is temporary.
Recorded reference execution
Reference output captured from executing the complete lesson script (code/main.py) and its assertions in the course CPU environment.
Restored step: 3
Optimizer count: 3
Next update and loss agree; checkpoint exists: True
Restored step: 3
Optimizer count: 3
Next update and loss agree; checkpoint exists: True
Model-only checkpoint produces a different Adam update
Reset key changes the noisy objective and continuation stream
Full restore Adam count: 4; reset memory count: 1; next parameters differ.
Expected changed configuration rejection
Expected exact key-data mismatch
PASS: recovery-03
Download execution record · The notebook includes executed cells and figure outputs. Compare your run with these values; timings depend on the machine.
Model-only restoration loses Adam history
# Experiment — Model-only restoration loses Adam history: Saved weights give the same pre-update predictions but not...
model_only = dict(restored["training"])
model_only["optimizer"] = tx.init(model_only["params"])
wrong_next,_ = step(model_only,x,y)
assert any(not np.allclose(np.asarray(a),np.asarray(b),rtol=1e-6,atol=1e-7) for a,b in zip(jax.tree.leaves(wrong_next["params"]),jax.tree.leaves(expected_next["params"])))
assert int(wrong_next["optimizer"][0].count)==1
print("Model-only checkpoint produces a different Adam update")Expected: Fresh optimizer count becomes and parameters differ from the full-state continuation.
Saved weights give the same pre-update predictions but not necessarily the same optimizer transition.
Reset the random stream
# Experiment — Reset the random stream: A seed identifies the initial stream; a saved continuation key...
reset_random = dict(restored["training"])
reset_random["key_data"] = initial_state()["key_data"]
reset_next,reset_loss = step(reset_random,x,y)
assert not np.isclose(float(reset_loss),float(expected_loss),rtol=1e-6,atol=1e-7)
assert not np.array_equal(np.asarray(reset_next["key_data"]),np.asarray(expected_next["key_data"]))
print("Reset key changes the noisy objective and continuation stream")Expected: The noisy objective and continuation key differ.
A seed identifies the initial stream; a saved continuation key identifies the current boundary.
Make it yours
Where to put your code: Append your solution at the bottom of phases/06-recovery/03-save-model-optimizer-and-random-state/code/main.py (or inside the Make it yours cell of exercises/recovery-03.ipynb) and run python3 phases/06-recovery/03-save-model-optimizer-and-random-state/code/main.py to verify your prediction before opening the reference solution.
Advance to step five, save to a new directory, restore and verify step six against uninterrupted execution.
Show a writing guide and starter scaffold
How to write this exercise code: step-by-step recipe & starter scaffold
1. Key functions & syntax to use:
step(...)— Callstepwith your updated parameters or inputs from this lesson's workspace.ocp.StandardCheckpointer(...)— Callocp.StandardCheckpointerwith your updated parameters or inputs from this lesson's workspace.assert condition— Verify that the observed output shape, status, or numerical value satisfies the contract.
2. Step-by-step implementation plan:
- Enter
ocp.StandardCheckpointer()context block:
# Exercise solution: Advance to step five, save to a new directory, restore and verify step...
later = ... # TODO: compute later
for _ in range(2):later,_=step(later,x,y)
# Enter `ocp.StandardCheckpointer()` context block:
with ocp.StandardCheckpointer() as cp:
later_path = Path(...) # TODO: compute later_path
cp.save(later_path,{"training":later,"contract":encode_contract(contract)})
cp.wait_until_finished()
recovered = cp.restore(...) # TODO: compute recovered
validate_contract(recovered["contract"],contract)
left,_ = step(...) # TODO: compute left,_
right,_ = step(...) # TODO: compute right,_
same_tree(left,right)
assert int(right["step"]) # TODO: complete assertion check
Reveal the reference solution
# Exercise solution: Advance to step five, save to a new directory, restore and verify step...
later = state
for _ in range(2):later,_=step(later,x,y)
# Enter `ocp.StandardCheckpointer()` context block:
with ocp.StandardCheckpointer() as cp:
later_path = Path(tempfile.mkdtemp(dir=root))/"step_5"
cp.save(later_path,{"training":later,"contract":encode_contract(contract)})
cp.wait_until_finished()
recovered = cp.restore(later_path,target={"training":initial_state(),"contract":encode_contract(contract)})
validate_contract(recovered["contract"],contract)
left,_=step(later,x,y)
right,_=step(recovered["training"],x,y)
same_tree(left,right)
assert int(right["step"])==6Show what weights-only recovery loses
Transfer
Keep the restored parameters but reset Adam’s memory. Compare the next update with the full-state restore. Keep data and random state identical so the optimizer memory is the only changed factor.
Show a writing guide and starter scaffold
How to write this practice solution: step-by-step recipe & starter scaffold
1. Key functions & syntax to use:
jnp.allclose(actual, expected, rtol=..., atol=...)— Checks that two arrays match elementwise within floating-point tolerance.
2. Step-by-step implementation plan:
- Set up the changed input or configuration parameters requested in the prompt.
- Run the target function or transformation and assert that the output matches your prediction.
# Show what weights-only recovery loses (Transfer): A checkpoint can return plausible predictions yet fail to...
full = ... # TODO: compute full
weights_only = dict(...) # TODO: compute weights_only
full_next,_ = step(...) # TODO: compute full_next,_
reset_next,_ = step(...) # TODO: compute reset_next,_
assert int(full_next['optimizer'][0].count) # TODO: complete assertion check
assert int(reset_next['optimizer'][0].count) # TODO: complete assertion check
assert any(not np.allclose(a,b,rtol=1e-6,atol=1e-7) for a,b # TODO: complete assertion check
print('Full restore Adam count: 4; reset memory count: 1; next parameters differ.')
Show a hint
Create a new mapping and replace only its optimizer entry.
Reveal reference solution and reasoning
# Show what weights-only recovery loses (Transfer): A checkpoint can return plausible predictions yet fail to...
full=restored['training']
weights_only=dict(full,optimizer=tx.init(full['params']))
full_next,_=step(full,x,y)
reset_next,_=step(weights_only,x,y)
assert int(full_next['optimizer'][0].count)==4
assert int(reset_next['optimizer'][0].count)==1
assert any(not np.allclose(a,b,rtol=1e-6,atol=1e-7) for a,b in zip(jax.tree.leaves(full_next['params']),jax.tree.leaves(reset_next['params'])))
print('Full restore Adam count: 4; reset memory count: 1; next parameters differ.')A checkpoint can return plausible predictions yet fail to resume the original training trajectory. Adam’s moments and count affect the next update even when the weights and batch match.
Reject an incompatible update configuration
Challenge
Attempt to restore the contract under learning rate . Show the rejection, then validate the original configuration. Also inspect shape and dtype equality of restored parameter leaves.
Show a writing guide and starter scaffold
How to write this practice solution: step-by-step recipe & starter scaffold
1. Key functions & syntax to use:
configuration(...)— Callconfigurationwith your updated parameters or inputs from this lesson's workspace.validate_contract(...)— Callvalidate_contractwith your updated parameters or inputs from this lesson's workspace.assert condition— Verify that the observed output shape, status, or numerical value satisfies the contract.
2. Step-by-step implementation plan:
- Run the boundary check and catch the expected exception:
- Check tensor shape invariant:
a.shape==b.shape and a.dtype==b.dtype - Integer PRNG words must be checked exactly, not with relative float tolerance.
- Run the boundary check and catch the expected exception:
# Reject an incompatible update configuration (Challenge): Array restoration does not verify that a learning rate, data...
changed_contract = dict(...) # TODO: compute changed_contract
# Run the boundary check and catch the expected exception:
try:
validate_contract(restored["contract"],changed_contract)
except ValueError:
print("Expected changed configuration rejection")
else:
raise AssertionError("Expected contract mismatch")
validate_contract(restored["contract"],contract)
for a,b in zip(jax.tree.leaves(state["params"]),jax.tree.leaves(restored["training"]["params"])):
# Check tensor shape invariant: `a.shape==b.shape and a.dtype==b.dtype`
assert a.shape # TODO: complete assertion check
# Integer PRNG words must be checked exactly, not with relative float tolerance.
changed_key = dict(...) # TODO: compute changed_key
changed_key["key_data"] = ... # TODO: compute changed_key["key_data"]
# Run the boundary check and catch the expected exception:
try:
same_tree(state,changed_key)
except AssertionError:
print("Expected exact key-data mismatch")
else:
raise AssertionError("Expected changed PRNG word rejection")
Show a hint
A compatible pytree can still represent the wrong update rule.
Reveal reference solution and reasoning
# Reject an incompatible update configuration (Challenge): Array restoration does not verify that a learning rate, data...
changed_contract = dict(contract,learning_rate=0.1)
# Run the boundary check and catch the expected exception:
try:
validate_contract(restored["contract"],changed_contract)
except ValueError:
print("Expected changed configuration rejection")
else:
raise AssertionError("Expected contract mismatch")
validate_contract(restored["contract"],contract)
for a,b in zip(jax.tree.leaves(state["params"]),jax.tree.leaves(restored["training"]["params"])):
# Check tensor shape invariant: `a.shape==b.shape and a.dtype==b.dtype`
assert a.shape==b.shape and a.dtype==b.dtype
# Integer PRNG words must be checked exactly, not with relative float tolerance.
changed_key = dict(state)
changed_key["key_data"] = state["key_data"].at[0].add(np.uint32(1))
# Run the boundary check and catch the expected exception:
try:
same_tree(state,changed_key)
except AssertionError:
print("Expected exact key-data mismatch")
else:
raise AssertionError("Expected changed PRNG word rejection")Array restoration does not verify that a learning rate, data revision or PRNG implementation agrees with the saved experiment. Refuse an incompatible continuation before applying an update.
Diagnose the result
Array restoration does not verify that a learning rate, data revision or PRNG implementation agrees with the saved experiment. Refuse an incompatible continuation before applying an update.
Carry forward
- Inventory everything the next step reads
- Save to a real local checkpoint directory
- Restore into an explicit state template
- Version and data checks belong to the application
Keep your evidence
Keep Orbax save/wait/restore commands, snapshot step, Adam count/moments, exact PRNG words, numerical next-update replay and the parameter-only restart counterexample.
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.
Verified on local CPU. Stage to a Cloud TPU VM via Cloud TPU setup to record accelerator execution receipts (TODO: add automated Cloud TPU CI execution receipts).