Course · State, randomness & control flowPhase 03 · Study guide

Have you saved everything needed to determine the next step?

Core Foundation · Local CPULaptop CPU4 lessons (~4.1h)

Revision v0.7.0 ·

A program’s state includes random keys, counters and structured values as well as model parameters. Explicit ownership lets you reproduce a transition, run a batch safely, and later recover an interrupted experiment.

Preparation: JAX transformations

Runtime: CPU

Check your starting point

Two simulated examples receive the same random key and run the same sampling function. Should you expect independently allocated draws?

Compare your reasoning

No. The same key and operation replay the same draw in the tested environment. Split or derive distinct keys at the ownership boundary; unequal observed samples alone do not prove independence.

If this is unfamiliar, work through Random keys without surprises before attempting the project.

Build in stages

1. Name and structure the state

Draw the key ownership tree and identify parameter, optimizer and metadata leaves. Check both tree structure and leaf values.

2. Audit repeated and conditional transitions

Implement a recurrence with fixed carry shapes. Save per-step observations as outputs, compare with a Python loop, then test both conditional branches and their boundary.

Try a changed condition

Make a prediction before opening the reasoning. Keep your answer and the evidence you would collect.

Resume a simulation halfway through with the saved position but a newly created key. What must you compare to determine whether it is a true continuation?

Compare an approach

Compare the next draw and next state, then the remaining trajectory, against an uninterrupted reference. Restore the original key and counter as well as position. A similar final average is not a continuation check.

Diagnose a likely failure

Symptom: A compiled loop fails when its history grows each step.

Check next: Keep carry structure, shapes and dtypes fixed. Return the per-step history through scan outputs.

Decide what is ready

Use foundation-toolkit stage 4. Save and reload state in a fresh process and verify the next transition and full remaining trajectory, not only the final mean.

Where to put your project code

Write your code in: projects/foundation-toolkit/my_toolkit.py (copy projects/foundation-toolkit/starter/toolkit.py first: cp projects/foundation-toolkit/starter/toolkit.py projects/foundation-toolkit/my_toolkit.py).

Verify your implementation: python3 projects/foundation-toolkit/tests/check.py --stage all --impl projects/foundation-toolkit/my_toolkit.py

Run the staged project checks on your implementation in projects/foundation-toolkit/my_toolkit.py and record your transfer experiment results in your portfolio.

Open A deterministic batched simulation

What still needs further work

Local deterministic state is separate from concurrent-writer or multi-host recovery; the recovery and operations phases extend these boundaries.