Training Systems · Local CPULaptop CPULesson 2 of 5 in Phase 05

Phase 05 / Neural network training

Model and state with Flax NNX

Revision v0.18.1 ·

Lesson · about 80 minutes · CPU practice

Express a model and inspect the distinction between parameters and other state.

Phase study guide: preparation, milestones and project evidence

Before you start

Before this: Build a tiny multilayer perceptron
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/05-networks/02-model-and-state-with-flax-nnx/code/main.py

PowerShell command · repository folder
.\.venv\Scripts\python.exe phases/05-networks/02-model-and-state-with-flax-nnx/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: Trainable scalars: 1313; other state: 11; original and cloned counters are 11 and 22.

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

  • Move the explicit equations into a module
  • Separate parameter state from diagnostic state
  • Reconstruct an independent graph from a snapshot
  • Check a library forward pass independently

The problem

A model library can organize weights for us, but we still want to see what is being learned. We’ll build a small Flax NNX module, compare its predictions with NumPy, and inspect its state. A diagnostic counter will help us distinguish trainable parameters from other values a model carries.

The idea

A model has a computation structure and state values. Some state is trainable; other state may record statistics or randomness. Flax NNX helps manage these objects, but we still need to understand which identities are shared and which values an update changes.

Distinguish shared structure from changing values

Think of nnx.split(model) as separating the model's wiring from the arrays currently attached to it. The unfiltered split returns exactly two objects, a GraphDef and complete State. Here that state has 13 Param scalars plus the calls variable. Filtering state for Param and Not(Param) inspects those groups; it does not change the two-result unfiltered call.

The identity variant in the diagram gives left and right the same hidden layer object. Replacing its bias through one reference is visible through the other because both names reach the same variable. To get an independent snapshot, copy state leaves before merging; equal initial values alone do not imply separate mutable state. The tested run confirms the distinction: the cloned counter advances to 2 while the original stays at 1.

Read the upper GraphDef node as the module wiring and named edges; the side node states the alias relation left is right. The middle State node lists hidden.kernel, hidden.bias, out.kernel, out.bias, and calls. Arrows from both inputs to nnx.merge(GraphDef, State) show that structure and current values are needed to rebuild the model. The two lower arrows contrast reusing mutable Variable objects with copying state leaves first. Test the distinction by changing a bias through one alias and by incrementing a cloned counter; the retained run gives source/clone counters 1 and 2.

Open full-size diagram
(G,S)=nnx.split⁡(M),M′=nnx.merge⁡(G,S′)(\mathcal G,S)=\operatorname{nnx.split}(M),\qquad M^{\prime}=\operatorname{nnx.merge}(\mathcal G,S^{\prime})

Move the explicit equations into a module

nnx.Linear(2, 3) stores a 2×32\times3 kernel and a length-three bias; the output layer stores 3×13\times1 weights and one bias. The parameter total is 6+3+3+1=136+3+3+1=13, matching the printed count. A batch of one or five rows changes the leading batch dimension, not these stored arrays.

The forward pass remains the earlier computation: affine map, tanh, then affine map. Extract the named kernels and biases and compute those operations in NumPy to check that the module wiring matches the explicit equations. An input with the wrong feature count violates the constructor's fixed input contract.

Separate parameter state from diagnostic state

The calls variable is incremented only when this example is called with record=True. It is ordinary mutable state, not a learned weight, so nnx.Optimizer(..., wrt=nnx.Param) excludes it from parameter updates. Exclusion from gradients does not freeze a value that the forward method changes.

Real models add other non-parameter state. BatchNorm stores running statistics, and Dropout uses RNG state; later evaluation must specify whether those collections are allowed to change. For this toy module, inspect Param and its complement and check the counter before and after each call.

model
├─ hidden: kernel, bias [Param]
├─ out: kernel, bias [Param]
└─ calls: diagnostic counter [Variable]

Reconstruct an independent graph from a snapshot

GraphDef carries module structure and State carries current variable values. Merging them reconstructs the same graph shape, but in the tested Flax version a merge with the original State may reuse its mutable Variable objects. Copy the state leaves before merging when you need an isolated clone, then test identity through behavior: call the clone with record=True and confirm its counter changes while the source counter remains 11.

The chart counts the four named parameter paths: 6 values in hidden.kernel, 3 in hidden.bias, 3 in out.kernel, and 1 in out.bias. It omits calls, which is the single non-Param value. The diagram's left/right alias case illustrates a reference-identity variant, not extra layers in that 13-parameter count. This is an in-memory ownership check, not a durable checkpoint format.

Check a library forward pass independently

Read hidden.kernel, hidden.bias, out.kernel, and out.bias from the model, then apply the affine, tanh, and affine operations in NumPy to several inputs. Matching logits checks the computation and the final squeeze. It does not test whether initialization is good or whether a classifier generalizes.

A useful algebraic follow-up is to add one to the output bias: every logit should rise by exactly one, while hidden activations stay fixed. The script's clone check separately shows that the copied state can change without changing the original parameter values.

Read the figure

The horizontal axis lists four named NNX parameter paths; bar height is the number of scalar parameter elements at that path. The observed counts are 6 for hidden.kernel, 3 for hidden.bias, 3 for out.kernel, and 1 for out.bias, summing to the printed 13 trainable scalars. The separate calls variable is not a parameter and is absent.

Connect it to the computation

Use the named counts to reconcile the module with its forward pass: the hidden kernel has shape 2×32\times3, and the output kernel has shape 3×13\times1. Then inspect the graph/state diagram to find the counter, and check that a copied-state clone can advance its counter without changing the source.

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 Define typed state and the model

Create main.py. From the repository root, run python3 -m pip install -r requirements-cpu.txt in your activated environment; the course pins Flax 0.12.6. Copy the model before adding any optimizer.

main.py · append build step 1
# Step 1 : 1. Define typed state and the model: nnx.Rngs manages initializer keys.
from flax import nnx
import jax
import jax.numpy as jnp
import numpy as np
# Define `TinyMLP` module / container with explicit state and forward pass:
class TinyMLP(nnx.Module):
    def __init__(self,seed=0):
        rngs=nnx.Rngs(seed)
        self.hidden=nnx.Linear(2,3,rngs=rngs)
        self.out=nnx.Linear(3,1,rngs=rngs)
        self.calls=nnx.Variable(jnp.array(0,dtype=jnp.int32))
    def __call__(self,x,record=False):
        if record:self.calls[...] += 1
        return self.out(nnx.tanh(self.hidden(x))).squeeze(-1)
model=TinyMLP()
batch=jnp.array([[1.,2.],[-1.,.5],[0.,0.]])

nnx.Rngs manages initializer keys. The optional diagnostic counter changes only when explicitly requested.

Explain the APIs in this step
  • nnx.Rngs(seed): Provides named random streams used to initialize and operate on NNX modules.
  • nnx.Linear(in_features, out_features, ...): Creates a learned affine layer in Flax NNX.
  • jnp.array(values, dtype=...): Constructs a JAX array from Python/NumPy values.

2 Verify equations and inspect the filters

Append an independent NumPy computation. Count only nnx.Param leaves, not the counter.

main.py · append build step 2
# Step 2 : 2. Verify equations and inspect the filters: The matching forward pass verifies the network wiring.
scores=model(batch,record=True)
# Transfer the device result to the host for an independent NumPy comparison.
h=np.tanh(np.asarray(batch)@np.asarray(model.hidden.kernel[...])+np.asarray(model.hidden.bias[...]))
# Transfer the device result to the host for an independent NumPy comparison.
expected=(h@np.asarray(model.out.kernel[...])+np.asarray(model.out.bias[...])).squeeze(-1)
np.testing.assert_allclose(scores,expected,rtol=1e-5,atol=1e-6)
param_state=nnx.state(model,nnx.Param)
other_state=nnx.state(model,nnx.Not(nnx.Param))
assert sum(a.size for a in jax.tree.leaves(param_state))==13
assert sum(a.size for a in jax.tree.leaves(other_state))==1
assert int(model.calls[...])==1

The matching forward pass verifies the network wiring. The filters identify 1313 learned scalars and one unlearned state scalar.

Explain the APIs in this step
  • np.testing.assert_allclose(actual, desired, rtol=..., atol=...): Checks a host-side numerical expectation and raises AssertionError when values differ beyond tolerance.

3 Split, merge, and test independence

Append the clone test. Predict which counter changes when the clone executes.

main.py · append build step 3
# Step 3 : 3. Split, merge, and test independence: nnx.grad differentiates Param variables by default.
graphdef,state=nnx.split(model)
clone=nnx.merge(graphdef,jax.tree.map(lambda a:jnp.array(a,copy=True),state))
np.testing.assert_allclose(clone(batch),model(batch),rtol=1e-6)
clone(batch,record=True)
assert int(clone.calls[...])==2 and int(model.calls[...])==1
grads=nnx.grad(lambda m:jnp.mean(m(batch)**2))(model)
assert sum(a.size for a in jax.tree.leaves(grads))==13
assert 'calls' not in grads
print('Trainable scalars: 13; other state: 1; clone counters:',int(model.calls[...]),int(clone.calls[...]))

nnx.grad differentiates Param variables by default. Counter independence is a state-ownership invariant, not a performance measurement.

Explain the APIs in this step
  • jax.tree.map(fn, tree, ...): Applies a function leaf-by-leaf across matching PyTrees.
  • jnp.array(values, dtype=...): Constructs a JAX array from Python/NumPy values.
  • np.testing.assert_allclose(actual, desired, rtol=..., atol=...): Checks a host-side numerical expectation and raises AssertionError when values differ beyond tolerance.
  • jnp.mean(x, axis=..., keepdims=...): Averages JAX array values along the selected axis.
View the cumulative build in main.py

These blocks append in order to your separate practice file.

main.py · cumulative Python build
# Step 1 : 1. Define typed state and the model: nnx.Rngs manages initializer keys.
from flax import nnx
import jax
import jax.numpy as jnp
import numpy as np
# Define `TinyMLP` module / container with explicit state and forward pass:
class TinyMLP(nnx.Module):
    def __init__(self,seed=0):
        rngs=nnx.Rngs(seed)
        self.hidden=nnx.Linear(2,3,rngs=rngs)
        self.out=nnx.Linear(3,1,rngs=rngs)
        self.calls=nnx.Variable(jnp.array(0,dtype=jnp.int32))
    def __call__(self,x,record=False):
        if record:self.calls[...] += 1
        return self.out(nnx.tanh(self.hidden(x))).squeeze(-1)
model=TinyMLP()
batch=jnp.array([[1.,2.],[-1.,.5],[0.,0.]])

# Step 2 : 2. Verify equations and inspect the filters: The matching forward pass verifies the network wiring.
scores=model(batch,record=True)
# Transfer the device result to the host for an independent NumPy comparison.
h=np.tanh(np.asarray(batch)@np.asarray(model.hidden.kernel[...])+np.asarray(model.hidden.bias[...]))
# Transfer the device result to the host for an independent NumPy comparison.
expected=(h@np.asarray(model.out.kernel[...])+np.asarray(model.out.bias[...])).squeeze(-1)
np.testing.assert_allclose(scores,expected,rtol=1e-5,atol=1e-6)
param_state=nnx.state(model,nnx.Param)
other_state=nnx.state(model,nnx.Not(nnx.Param))
assert sum(a.size for a in jax.tree.leaves(param_state))==13
assert sum(a.size for a in jax.tree.leaves(other_state))==1
assert int(model.calls[...])==1

# Step 3 : 3. Split, merge, and test independence: nnx.grad differentiates Param variables by default.
graphdef,state=nnx.split(model)
clone=nnx.merge(graphdef,jax.tree.map(lambda a:jnp.array(a,copy=True),state))
np.testing.assert_allclose(clone(batch),model(batch),rtol=1e-6)
clone(batch,record=True)
assert int(clone.calls[...])==2 and int(model.calls[...])==1
grads=nnx.grad(lambda m:jnp.mean(m(batch)**2))(model)
assert sum(a.size for a in jax.tree.leaves(grads))==13
assert 'calls' not in grads
print('Trainable scalars: 13; other state: 1; clone counters:',int(model.calls[...]),int(clone.calls[...]))

Run the example

python3 phases/05-networks/02-model-and-state-with-flax-nnx/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 : 1. Define typed state and the model: nnx.Rngs manages initializer keys.
from flax import nnx
import jax
import jax.numpy as jnp
import numpy as np
# Define `TinyMLP` module / container with explicit state and forward pass:
class TinyMLP(nnx.Module):
    def __init__(self,seed=0):
        rngs=nnx.Rngs(seed)
        self.hidden=nnx.Linear(2,3,rngs=rngs)
        self.out=nnx.Linear(3,1,rngs=rngs)
        self.calls=nnx.Variable(jnp.array(0,dtype=jnp.int32))
    def __call__(self,x,record=False):
        if record:self.calls[...] += 1
        return self.out(nnx.tanh(self.hidden(x))).squeeze(-1)
model=TinyMLP()
batch=jnp.array([[1.,2.],[-1.,.5],[0.,0.]])

# Step 2 : 2. Verify equations and inspect the filters: The matching forward pass verifies the network wiring.
scores=model(batch,record=True)
# Transfer the device result to the host for an independent NumPy comparison.
h=np.tanh(np.asarray(batch)@np.asarray(model.hidden.kernel[...])+np.asarray(model.hidden.bias[...]))
# Transfer the device result to the host for an independent NumPy comparison.
expected=(h@np.asarray(model.out.kernel[...])+np.asarray(model.out.bias[...])).squeeze(-1)
np.testing.assert_allclose(scores,expected,rtol=1e-5,atol=1e-6)
param_state=nnx.state(model,nnx.Param)
other_state=nnx.state(model,nnx.Not(nnx.Param))
assert sum(a.size for a in jax.tree.leaves(param_state))==13
assert sum(a.size for a in jax.tree.leaves(other_state))==1
assert int(model.calls[...])==1

# Step 3 : 3. Split, merge, and test independence: nnx.grad differentiates Param variables by default.
graphdef,state=nnx.split(model)
clone=nnx.merge(graphdef,jax.tree.map(lambda a:jnp.array(a,copy=True),state))
np.testing.assert_allclose(clone(batch),model(batch),rtol=1e-6)
clone(batch,record=True)
assert int(clone.calls[...])==2 and int(model.calls[...])==1
grads=nnx.grad(lambda m:jnp.mean(m(batch)**2))(model)
assert sum(a.size for a in jax.tree.leaves(grads))==13
assert 'calls' not in grads
print('Trainable scalars: 13; other state: 1; clone counters:',int(model.calls[...]),int(clone.calls[...]))

Expected result: Trainable scalars: 1313; other state: 11; original and cloned counters are 11 and 22.

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:26:30.847038+00:00 · Python 3.14.3 · JAX 0.9.2 · 1 CPU device

Trainable scalars: 13; other state: 1; clone counters: 1 2
Trainable scalars: 13; other state: 1; clone counters: 1 2
Clone logits shifted by one; original parameters unchanged.
PASS: networks-02

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

Bias shifts every score equally

Bias shifts every score equally · Python experiment
# Experiment : Bias shifts every score equally
before=np.asarray(model(batch))
saved=model.out.bias[...]
model.out.bias[...] = saved+.25
np.testing.assert_allclose(model(batch),before+.25,atol=1e-6)
model.out.bias[...] = saved

Expected: All logits increase by 0.250.25.

An affine output bias changes the decision threshold uniformly; the hidden representation is unchanged.

Repeated construction reproduces state

Repeated construction reproduces state · Python experiment
# Experiment : Repeated construction reproduces state: Replay is checked in one pinned environment.
replay=TinyMLP(0)
for a,b in zip(jax.tree.leaves(nnx.state(replay,nnx.Param)),jax.tree.leaves(param_state)):
    np.testing.assert_array_equal(a,b)
different=TinyMLP(1)
assert not np.array_equal(np.asarray(different.hidden.kernel[...]),np.asarray(model.hidden.kernel[...]))

Expected: Equal seeds reproduce the parameter arrays; a changed seed changes the hidden kernel.

Replay is checked in one pinned environment. Seeds alone do not promise identical bytes across package versions or backends.

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.

Apply the model to one new row and to five rows. Verify the scalar parameter count stays 1313 and the batch axis survives.

Show a writing guide and starter scaffold

How to write this exercise code

Learner-only inputs, outputs, checks and hints

Inputs

  • The lesson’s Flax NNX model and one input row plus a five-row batch.

Outputs

  • Predictions whose leading dimensions match the input batch sizes, and the trainable scalar count.

Invariants to check

  • Applying the same model to one row and five rows preserves the leading batch axis; the parameter count remains unchanged.

Hints

  • Use inputs shaped (1, 2) and (5, 2), then inspect output shapes.
  • Count only nnx.Param leaves, not the non-parameter call counter.
Your attempt · complete the TODOs
from flax import nnx
import jax
import jax.numpy as jnp

def check_model_batch_contract(model, single_row, five_rows):
    # TODO: Apply the model to both batches and verify output shapes and trainable scalar count.
    raise NotImplementedError
Reveal the reference solution
Reference solution
# Exercise solution: Apply the model to one new row and to five rows.
single=jnp.array([[.3,-.7]])
# Check tensor shape invariant: `model(single).shape==(1,)`
assert model(single).shape==(1,)
# Check tensor shape invariant: `model(jnp.repeat(single,5,axis=0)).shape==(5,)`
assert model(jnp.repeat(single,5,axis=0)).shape==(5,)
assert sum(a.size for a in jax.tree.leaves(nnx.state(model,nnx.Param)))==13

Change a clone without changing its source

Transfer

Increase only the cloned model’s output bias by one. Predict the change in every logit and verify the original model’s parameter leaves remain unchanged.

Show a writing guide and starter scaffold

How to write this practice solution

Learner-only inputs, outputs, checks and hints

Inputs

  • Increase only the cloned model’s output bias by one. Predict the change in every logit and verify the original model’s parameter leaves remain unchanged.

Outputs

  • Logit shift on the clone and an unchanged source snapshot.

Invariants to check

  • The clone owns independent variable state rather than aliasing the original module.

Hints

  • Snapshot the original parameters before editing clone.out.bias.
Your attempt · complete the TODOs
from flax import nnx
import jax.numpy as jnp

def practice_change_a_clone_without_changing_its_source(model, cloned_model):
    """Implement the change a clone without changing its source check."""
    # TODO: Clone model state, increment only the clone output bias, and compare all source parameter leaves before and after.
    raise NotImplementedError
Show a hint

Snapshot the original parameters before editing clone.out.bias.

Reveal reference solution and reasoning
Change a clone without changing its source · Python solution
# Change a clone without changing its source (Transfer)
original_leaves=[np.array(a,copy=True) for a in jax.tree.leaves(nnx.state(model,nnx.Param))]
# Transfer the device result to the host for an independent NumPy comparison.
old_clone=np.asarray(clone(batch)).copy()
clone.out.bias[...] = clone.out.bias[...] + 1.
np.testing.assert_allclose(clone(batch),old_clone+1.,rtol=1e-5,atol=1e-6)
for before,after in zip(original_leaves,jax.tree.leaves(nnx.state(model,nnx.Param))):
    np.testing.assert_array_equal(before,after)
print('Clone logits shifted by one; original parameters unchanged.')

The bias is broadcast over observations, so every logit shifts equally. Comparing original leaves detects accidental aliasing; equal parameter counts cannot detect that bug.

Diagnose a shared module reference

Intermediate

Assign shared = model, increment its counter, and demonstrate why this is not a snapshot. Then reconstruct an independent module.

Show a writing guide and starter scaffold

How to write this practice solution

Learner-only inputs, outputs, checks and hints

Inputs

  • Assign shared = model, increment its counter, and demonstrate why this is not a snapshot. Then reconstruct an independent module.

Outputs

  • The shared-reference mutation and an isolated copy check.

Invariants to check

  • Assignment preserves object identity; a snapshot must preserve values without shared mutable state.

Hints

  • Assignment changes which name refers to the same object.
Your attempt · complete the TODOs
from flax import nnx

def practice_diagnose_a_shared_module_reference(model):
    """Implement the diagnose a shared module reference check."""
    # TODO: Assign an alias, mutate its counter, compare the original object, and then construct an independent copy.
    raise NotImplementedError
Show a hint

Assignment changes which name refers to the same object.

Reveal reference solution and reasoning
Diagnose a shared module reference · Python solution
# Diagnose a shared module reference (Intermediate)
saved_count=int(model.calls[...])
shared=model
shared(batch,record=True)
assert int(model.calls[...])==saved_count+1
graph,state=nnx.split(model)
independent=nnx.merge(graph,jax.tree.map(lambda a:jnp.array(a,copy=True),state))
independent(batch,record=True)
assert int(model.calls[...])==saved_count+1
assert int(independent.calls[...])==saved_count+2

Sharing a graph is useful for tied layers but wrong for an independent evaluation copy. Split, copy state, and merge supply independent variables in this example.

Check your understanding

What does excluding the counter from nnx.Param guarantee?

Choose one answer

Diagnose the result

If evaluation changes state, inspect every nonparameter variable and forward flag. If a clone changes the original, inspect shared references. If an NNX module fails under a raw JAX transform, use nnx transformations or split it into graph/state deliberately instead of hiding the error.

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

Carry forward

  • Move the explicit equations into a module
  • Separate parameter state from diagnostic state
  • Reconstruct an independent graph from a snapshot
  • Check a library forward pass independently

Keep your evidence

Download evidence template

Keep the NumPy forward comparison, count of 13 trainable scalars and one diagnostic counter, counter independence after copying state, and logits for batch size one and five.

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