Training Systems · Local CPULaptop CPULesson 4 of 6 in Phase 06

Phase 06 / Data & checkpoint recovery

Recover data position and resume

Revision v0.18.1 ·

Lesson · about 110 minutes · CPU practice

Persist model/optimizer/key and Grain iterator position at one boundary.

Phase study guide: preparation, milestones and project evidence

Before you start

Before this: Save model, optimizer, and random state
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 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.

macOS / Linux terminal · repository folder
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements-cpu.txt

PowerShell · repository folder
py -3 -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements-cpu.txt

After activation, run the complete worked example from that same folder:

macOS / Linux terminal · activated environment
python phases/06-recovery/04-recover-data-position-and-resume/code/main.py

PowerShell command · repository folder
.\.venv\Scripts\python.exe phases/06-recovery/04-recover-data-position-and-resume/code/main.py

For 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.

Check: The next four ID batches and losses match; parameters, Adam state, continuation key and step agree at step six. One epoch boundary is crossed.

Revisit the guided CPU setup · Open 19: TPU Setup. Package instructions are CPU instructions; Windows and accelerator environments need their own validation.

Stage 1 of 5 · Understand the MechanismCore mental model, equations & visual walkthrough

What you will be able to do

  • Persist model/optimizer/key and Grain iterator position at one boundary.
  • Replay across an epoch transition and compare every future batch.
  • Diagnose reader-only and model-only restores.
  • Reject changed pipeline configuration and state the tested recovery limits.

The problem

Let’s bring model recovery and data recovery together. We’ll interrupt training after two batches, save both the model state and the next-read position, then continue. Comparing the remaining batches with an uninterrupted run will show whether we really resumed at the same point, including across an epoch boundary.

The idea

Exact recovery joins numerical state and input-pipeline state at one completed update. Mixing boundaries can repeat or skip examples while producing a plausible training curve.

Restore one consistent training boundary
  1. Before updatemodel + optimizer + key + data position

    Identify the state needed to choose and process the next batch.

  2. Completed snapshotsave these values together

    Wait for the local checkpoint operation to finish.

  3. After restoresame next batch → same next transition

    Compare the restored transition with uninterrupted training.

This illustrates the lesson’s completed local snapshot boundary. It is verified separately from power-loss durability, asynchronous failure recovery or multi-host coordination.

Conceptual illustration. Verify the computation in the lesson’s Python experiments.

Read the data and model transition before the serialization helpers

The learner-authored transition consumes one batch, updates the training state from that batch, and returns both the updated state and iterator. This is the boundary to understand before reading packing and Orbax helpers: model state and data position must advance together. The full program adds a real Grain iterator and verifies the same boundary across restore.

Align model time with data time

After two batches, the checkpoint is at completed step 22, and the next IDs are [4,10,11,3][4,10,11,3]. The retained run compares four future batches across the epoch boundary: [4,10,11,3][4,10,11,3], [2,0,11,1][2,0,11,1], [5,7,6,8][5,7,6,8], then [4,3,10,9][4,3,10,9]. The uninterrupted and restored loss values match at steps 33 through 66, and full state agrees at step six.

If weights include the update for a batch but the iterator points before it, that batch is repeated. If the iterator points beyond the consumed batch, it is skipped. Save parameters, Adam state, key, step, and next-unconsumed iterator position at one completed-update boundary; similar first losses do not repair a misaligned cursor.

Each row names one state value at the accepted boundary and the next transition that consumes it. Read downward to see that parameters and optimizer state belong after update (t), the key is the next unused random value, the iterator is at the next unconsumed batch, and the counter records (t) completed. The horizontal reading across a row explains its next use. An older iterator repeats data and a later iterator skips it. Compare the diagram with the observed step-2 next IDs before restoring; the table represents logical alignment, not elapsed time.

Open full-size diagram
Ct=(Θt,Ot,kt,t,It,c),(Θt+1,Ot+1,kt+1,It+1)=Fc(Θt,Ot,kt,It)C_t=(\Theta_t,O_t,k_t,t,I_t,c),\qquad (\Theta_{t+1},O_{t+1},k_{t+1},I_{t+1})=F_c(\Theta_t,O_t,k_t,I_t)

Commit one logical boundary

Each consume call takes one batch, applies its update, and returns the updated state with the advanced iterator. After two calls, the model step is (2) and the iterator is ready for batch three. Store both in the same Orbax payload so the restored parameters and cursor describe the same point.

This example uses a serial loader and synchronous checkpointing. A prefetched pipeline may have requested records beyond those consumed by training, so define the snapshot boundary from that loader's supported state semantics rather than from a guessed queue count.

read batch 1 → update 1
read batch 2 → update 2 → save {state_2,next_read_3}
restore → read batch 3 → update 3

Persist opaque iterator bytes without editing them

Grain returns the iterator snapshot as bytes. The example stores them in a (4096)-byte uint8 buffer plus a length so the restore target has fixed shape, then passes the recovered bytes back to Grain unchanged. Oversized snapshots fail rather than being truncated.

This packing is a teaching device, not a substitute for Grain's supported handlers. Restore is checked for the tested source, sampler, batch, and worker configuration; a changed seed or batch size is an incompatibility to detect.

Audit the future, including an epoch transition

The fixture has 12 records per epoch, in batches of four. From the checkpoint after two batches, the next four batches include the last batch of epoch zero and all three batches of epoch one. The plotted losses overlap at steps (3,4,5,6): approximately (0.54963), (0.83905), (0.07169), and (0.32428) for both paths.

Those are four scalar comparisons, not a full proof by themselves. The script also compares each batch's IDs and every final state leaf, catching a skipped/repeated record or an optimizer/key reset that a similar loss could hide.

epoch 0: batch 1 | batch 2 | checkpoint | batch 3
epoch 1: batch 4 | batch 5 | batch 6
replay compares IDs + losses + params + Adam + key + step

Define the limits of this recovery claim

The local contract binds the run to the dataset digest, source revision, record count, ordering settings, batch size, update configuration, PRNG implementation, and package versions. The retained checks reject changed seed, changed data digest, and a different batch configuration rather than silently resuming.

The writer is synchronous, storage is local, and restore runs in the same process environment. This demonstrates the tested continuation path only; it does not qualify power-loss behavior, remote storage, worker loss, multi-host restart, or arbitrary configuration migration.

Read the figure

The horizontal axis is the training step after the checkpoint, from 3 through 6; the vertical axis is batch loss. The uninterrupted and restored series overlap at approximately (0.54963), (0.83905), (0.07169), and (0.32428). These are four measured scalar losses, not a full state comparison.

Connect it to the computation

The overlap shows the restored trajectory follows the reference for the remaining batches, including the epoch transition. Use the separate batch-ID ledger and state-leaf assertions to tell whether the same data and optimizer transition produced those values. On a changed configuration, first investigate the cursor and contract rather than assuming a close loss means a correct resume.

Figure data and execution details

Recorded environment: JAX 0.9.2 · 1 CPU device

The notebook contains the data experiment, plotting code, and recorded figure output. Rerun it to test your changes.

Stage 2 of 5 · Build & Run the ReferenceStep-by-step code construction & verified execution output

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 Learner transition: commit the iterator cursor with the update

Run the compact state/data trace first. Predict which IDs follow the checkpoint after two consumed batches. The full Grain and Orbax program below adds reviewed source, version, packing, and restore support.

main.py · append build step 1
batches = [[4, 10, 11, 3], [2, 0, 11, 1], [5, 7, 6, 8], [4, 3, 10, 9]]
def learner_consume(state, cursor):
    ids = batches[cursor]
    next_state = {"step": state["step"] + 1, "last_ids": ids}
    return next_state, cursor + 1, ids
# This is the recorded continuation after the first two updates.
checkpoint = {"training": {"step": 2, "last_ids": "omitted"}, "next_batch": 0}
state, cursor, next_ids = learner_consume(checkpoint["training"], checkpoint["next_batch"])
assert state["step"] == 3 and next_ids == [4, 10, 11, 3]
print("Restored boundary:", checkpoint["training"]["step"], "next IDs:", next_ids)

A checkpoint stores the model transition and the cursor after consuming the same batch. This compact trace makes the boundary visible; the full Grain example tests the library iterator state and future numerical updates.

View the cumulative build in main.py

These blocks append in order to your separate practice file.

main.py · cumulative Python build
batches = [[4, 10, 11, 3], [2, 0, 11, 1], [5, 7, 6, 8], [4, 3, 10, 9]]
def learner_consume(state, cursor):
    ids = batches[cursor]
    next_state = {"step": state["step"] + 1, "last_ids": ids}
    return next_state, cursor + 1, ids
# This is the recorded continuation after the first two updates.
checkpoint = {"training": {"step": 2, "last_ids": "omitted"}, "next_batch": 0}
state, cursor, next_ids = learner_consume(checkpoint["training"], checkpoint["next_batch"])
assert state["step"] == 3 and next_ids == [4, 10, 11, 3]
print("Restored boundary:", checkpoint["training"]["step"], "next IDs:", next_ids)

Run the example

python3 phases/06-recovery/04-recover-data-position-and-resume/code/main.py
About 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
main.py · complete Python example
# Step 1 — Prepare the data and state: This setup makes the dataset and software assumptions explicit.
import jax
# Select the CPU backend and virtual device count before initializing JAX.
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
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):
    if len(raw)>capacity:
        raise ValueError("Snapshot exceeds tutorial fixed-byte capacity")
    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"])
    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)):
        # Transfer the device result to the host for an independent NumPy comparison.
        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)
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)
    value,gradient = jax.value_and_grad(objective)(state["params"])
    updates,opt_state = tx.update(gradient,state["optimizer"],state["params"])
    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))
    if actual != expected:
        raise ValueError("Checkpoint contract differs: verify dataset, optimizer configuration, key implementation and package versions")
workspace = tempfile.TemporaryDirectory()
root = Path(workspace.name)

import numpy as np
import grain.python as grain
# Module/container class `TutorialSource` encapsulating model parameters and forward pass:
class TutorialSource:
    def __init__(self,count=12):
        self.count = count
    def __len__(self):
        return self.count
    def __getitem__(self,index):
        value = np.float32(index/10)
        return {"id":np.int64(index),"x":value,"y":np.float32(2*value-1)}
    def __repr__(self):
        return f"TutorialSource(v1,n={self.count})"
def make_loader(seed=42,batch_size=4,count=12,drop_remainder=False):
    return grain.DataLoader(
        data_source=TutorialSource(count),
        sampler=grain.IndexSampler(num_records=count,num_epochs=2,shuffle=True,seed=seed),
        operations=[grain.Batch(batch_size,drop_remainder=drop_remainder)],
        worker_count=0)

# Step 2 — Build the pipeline or state transition
def data_contract(seed=42):
    source = TutorialSource()
    values = np.array([[source[i]["x"],source[i]["y"]] for i in range(len(source))],dtype=np.float32)
    return {"schema":1,"seed":seed,"batch_size":4,"count":12,"source":"TutorialSource(v1,n=12)",
            "sha256":hashlib.sha256(values.tobytes()).hexdigest(),
            "learning_rate":0.05,"optimizer":"adam","key_impl":"threefry2x32",
            "versions":dict(package_versions(),grain=__import__("importlib.metadata",fromlist=["version"]).version("grain"))}
def consume(state,iterator,count):
    losses=[]
    orders=[]
    for _ in range(count):
        batch = next(iterator)
        orders.append(batch["id"].copy())
        state,value = step(state,jnp.asarray(batch["x"]),jnp.asarray(batch["y"]))
        losses.append(float(value))
    return state,np.array(losses),orders
contract = data_contract()
reference_iterator = iter(make_loader())
reference_state,_,_ = consume(initial_state(),reference_iterator,2)
payload = {"training":reference_state,"pipeline":pack_bytes(reference_iterator.get_state()),"contract":encode_contract(contract)}
resume_path = root/"completed_step_2"
with ocp.StandardCheckpointer() as cp:
    cp.save(resume_path,payload)
    cp.wait_until_finished()
    restored_payload = cp.restore(resume_path,target={"training":initial_state(),"pipeline":pack_bytes(b""),"contract":encode_contract(contract)})
validate_contract(restored_payload["contract"],contract)
resumed_iterator = iter(make_loader())
resumed_iterator.set_state(unpack_bytes(restored_payload["pipeline"]))

# Step 3 — Run the comparison
reference_final,reference_losses,reference_ids = consume(reference_state,reference_iterator,4)
resumed_final,resumed_losses,resumed_ids = consume(restored_payload["training"],resumed_iterator,4)
for a,b in zip(reference_ids,resumed_ids):np.testing.assert_array_equal(a,b)
np.testing.assert_allclose(reference_losses,resumed_losses,rtol=1e-6,atol=1e-7)
same_tree(reference_final,resumed_final)
assert int(resumed_final["step"])==6
print("Restored next four ID batches:",[a.tolist() for a in resumed_ids])
print("Restored next four losses:",resumed_losses.tolist())
print("Full state agrees at step six")

Expected result: The next four ID batches and losses match; parameters, Adam state, continuation key and step agree at step six. One epoch boundary is crossed.

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.

2026-10-27T17:05:05.152023+00:00 · Python 3.14.3 · JAX 0.9.2 · 1 CPU device

Restored boundary: 2 next IDs: [4, 10, 11, 3]
Restored next four ID batches: [[4, 10, 11, 3], [2, 0, 11, 1], [5, 7, 6, 8], [4, 3, 10, 9]]
Restored next four losses: [0.5496272444725037, 0.8390463590621948, 0.07169045507907867, 0.32427603006362915]
Full state agrees at step six
Missing iterator restore repeats earlier IDs and changes the next loss
Correct IDs alone do not restore the training transition
Rejected incompatible seed
Rejected incompatible sha256
Original run contract remains valid.
Expected batch-configuration mismatch
PASS: recovery-04

Download execution record · The notebook includes executed cells and figure outputs. Compare your run with these values; timings depend on the machine.

Stage 3 of 5 · Controlled ExperimentsPredict before running & test boundary conditions

Restart only the reader

Restart only the reader · Python experiment
# Experiment — Restart only the reader: A model step counter does not automatically seek a data iterator.
fresh = iter(make_loader())
wrong_state,wrong_losses,wrong_ids = consume(restored_payload["training"],fresh,1)
assert not np.array_equal(wrong_ids[0],reference_ids[0])
assert not np.isclose(wrong_losses[0],reference_losses[0],rtol=1e-6,atol=1e-7)
print("Missing iterator restore repeats earlier IDs and changes the next loss")

Expected: The first batch repeats instead of the saved next batch; the next loss differs.

A model step counter does not automatically seek a data iterator.

Restore only the reader

Restore only the reader · Python experiment
# Experiment — Restore only the reader
reader_only = iter(make_loader())
reader_only.set_state(unpack_bytes(restored_payload["pipeline"]))
wrong_model,wrong_model_losses,correct_ids = consume(initial_state(),reader_only,1)
np.testing.assert_array_equal(correct_ids[0],reference_ids[0])
assert not np.isclose(wrong_model_losses[0],reference_losses[0],rtol=1e-6,atol=1e-7)
assert int(wrong_model["step"])==1
print("Correct IDs alone do not restore the training transition")

Expected: IDs agree but loss and step differ.

Input replay and model-state replay are complementary requirements; either alone can look superficially successful.

Stage 4 of 5 · Hands-On Exercise & CheckpointModify the code yourself, solve transfer tasks & test your understanding
Hands-On Exercise · Modify & Verify Run in the declared lesson environment

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.

Repeat the protocol after one batch and compare the next five batches through step six. Before running, predict the next batch IDs from the saved cursor. Then interpret the replayed IDs and losses, including the epoch transition, and explain why the reader-only negative control repeats earlier IDs. Save to a distinct directory.

Show a writing guide and starter scaffold

How to write this exercise code

Learner-only inputs, outputs, checks and hints

Inputs

  • A saved data/training contract and proposed resume configuration.

Outputs

  • A validated continuation that resumes at the exact next sample.

Invariants to check

  • Reject changed seed, data fingerprint, or batch size before consuming data.

Hints

  • Validate metadata before constructing or advancing the data iterator.
  • Retain the original contract while testing each incompatible field separately.
Your attempt · complete the TODOs
import jax
import jax.numpy as jnp
import numpy as np
import optax
import orbax.checkpoint as ocp
import json
import hashlib

def validate_resume(saved_contract, current_contract, iterator):
    """A validated continuation that resumes at the exact next sample."""
    # TODO: Validate dataset/sampler/batch metadata before restoring the next data position.
    raise NotImplementedError
Reveal the reference solution
Reference solution
# Exercise solution
other_iterator = iter(make_loader())
other_state,_,_ = consume(initial_state(),other_iterator,1)
other_payload = {"training":other_state,"pipeline":pack_bytes(other_iterator.get_state()),"contract":encode_contract(contract)}
with ocp.StandardCheckpointer() as cp:
    other_path = Path(tempfile.mkdtemp(dir=root))/"completed_step_1"
    cp.save(other_path,other_payload)
    cp.wait_until_finished()
    other_saved = cp.restore(other_path,target={"training":initial_state(),"pipeline":pack_bytes(b""),"contract":encode_contract(contract)})
other_resumed = iter(make_loader())
other_resumed.set_state(unpack_bytes(other_saved["pipeline"]))
a,loss_a,ids_a = consume(other_state,other_iterator,5)
b,loss_b,ids_b = consume(other_saved["training"],other_resumed,5)
for l,r in zip(ids_a,ids_b):np.testing.assert_array_equal(l,r)
np.testing.assert_allclose(loss_a,loss_b,rtol=1e-6,atol=1e-7)
same_tree(a,b)
assert int(b["step"])==6

Reject an incompatible resume before consuming data

Transfer

Try the saved contract with a changed sampler seed and then a changed dataset fingerprint. Both must fail before training resumes. Verify that the unchanged contract still passes.

Show a writing guide and starter scaffold

How to write this practice solution

Learner-only inputs, outputs, checks and hints

Inputs

  • Try the saved contract with a changed sampler seed and then a changed dataset fingerprint. Both must fail before training resumes. Verify that the unchanged contract still passes.

Outputs

  • Two early compatibility failures and a successful unchanged-contract check.

Invariants to check

  • A failed preflight consumes no training examples.

Hints

  • Use validate_contract on copies of the expected contract; do not rewrite the saved payload.
Your attempt · complete the TODOs
import jax
import jax.numpy as jnp
import numpy as np
import optax
import orbax.checkpoint as ocp
import json
import hashlib

def practice_reject_an_incompatible_resume_before_consuming_data(saved_contract, current_contract, iterator):
    """Implement the reject an incompatible resume before consuming data check."""
    # TODO: Check changed sampler seed and changed dataset digest against the saved contract before constructing the iterator.
    raise NotImplementedError
Show a hint

Use validate_contract on copies of the expected contract; do not rewrite the saved payload.

Reveal reference solution and reasoning
Reject an incompatible resume before consuming data · Python solution
# Reject an incompatible resume before consuming data (Transfer)
for field,value in [('seed',43),('sha256','different-dataset')]:
    incompatible=dict(contract,**{field:value})
    try:validate_contract(restored_payload['contract'],incompatible)
    except ValueError:print('Rejected incompatible',field)
    else:raise AssertionError('Incompatible resume was accepted')
validate_contract(restored_payload['contract'],contract)
print('Original run contract remains valid.')

Exact state replay only makes sense under a compatible data and update contract. Rejecting a mismatch before consuming a batch keeps an invalid resume from looking like a successful run.

Reject changed batch size before consuming

Challenge

Pretend the resumed application now uses batch size three. Reject the saved contract before a reader is used, then restore the original configuration and check the next IDs.

Show a writing guide and starter scaffold

How to write this practice solution

Learner-only inputs, outputs, checks and hints

Inputs

  • Pretend the resumed application now uses batch size three. Reject the saved contract before a reader is used, then restore the original configuration and check the next IDs.

Outputs

  • Early size rejection and replayed next IDs under the matching batch size.

Invariants to check

  • The reader is not advanced until the saved and current contracts match.

Hints

  • The stored cursor is interpreted in a particular pipeline configuration.
Your attempt · complete the TODOs
import jax
import jax.numpy as jnp
import numpy as np
import optax
import orbax.checkpoint as ocp
import json
import hashlib

def practice_reject_changed_batch_size_before_consuming(saved_contract, changed_batch_size, iterator):
    """Implement the reject changed batch size before consuming check."""
    # TODO: Preflight batch size three against the saved contract, then resume with the original size and inspect the next IDs.
    raise NotImplementedError
Show a hint

The stored cursor is interpreted in a particular pipeline configuration.

Reveal reference solution and reasoning
Reject changed batch size before consuming · Python solution
# Reject changed batch size before consuming (Challenge): Changing batching changes consumption semantics.
changed = dict(contract,batch_size=3)
try:
    validate_contract(restored_payload["contract"],changed)
except ValueError:
    print("Expected batch-configuration mismatch")
else:
    raise AssertionError("Expected incompatible restore rejection")
validate_contract(restored_payload["contract"],contract)
fixed_reader = iter(make_loader())
fixed_reader.set_state(unpack_bytes(restored_payload["pipeline"]))
np.testing.assert_array_equal(next(fixed_reader)["id"],reference_ids[0])

Changing batching changes consumption semantics. Refuse the mismatched configuration instead of assuming that training step two corresponds to the same next examples.

Check your understanding

Which evidence supports a consistent restart boundary?

Choose one answer

Diagnose the result

Changing batching changes consumption semantics. Refuse the mismatched configuration instead of assuming that training step two corresponds to the same next examples.

Stage 5 of 5 · Keep Your EvidenceRecord your verification receipt & carry takeaways forward

Carry forward

  • Commit one logical boundary
  • Persist opaque iterator bytes without editing them
  • Audit the future, including an epoch transition
  • Define the limits of this recovery claim

Keep your evidence

Download evidence template

Keep the committed model/optimizer/key/iterator snapshot, uninterrupted and resumed batch-ID ledger across the epoch boundary, exact keys/counts, declared floating tolerances and inconsistent model-only/reader-only restart failures.

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.

Save work for this lesson

Reference execution: CPU validation only. This worked reference is validated on CPU. Accelerator compatibility requires a separate source review and target execution receipt. Use the execution matrix to review device selection and data ownership before adapting a separate target program. Execution matrix and target qualification.

Primary references