We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Flax NNX step 89 of 100
Repeat NNX Linear Initialization
A newly constructed NNX Linear layer can reproduce another layer's initial parameters when both use the same configuration and fresh RNG streams with the same seed. This exercise checks the resulting forward calculation. It does not read checkpoint data or perform a sharded restore.
Start each RNG stream at the same seed
first = nnx.Linear(D, F, rngs=nnx.Rngs(seed))
second = nnx.Linear(D, F, rngs=nnx.Rngs(seed))
output = second(x)
In the same environment, these constructors produce matching kernel and
bias values. They create separate model objects. Reusing one advancing
nnx.Rngs object for both constructors is different: the second layer
receives later keys from the stream.
Reproducibility depends on the layer configuration, initializers, RNG behavior and software environment. It is not a promise of bit-for-bit equality across all versions or backends.
Initialization cannot recover learned parameters
If training changes first.kernel or first.bias, constructing second
from the original seed recreates the initial values. It does not restore
the changes. A real checkpoint restores saved values and may also contain
optimizer state, counters and other state needed to resume training.
NNX separates model structure from array state through operations such as
nnx.split and nnx.merge. Restoring compatible state into a model is a
different operation from constructing fresh random parameters. Real
sharded restoration also needs appropriate array types and placement;
none of those steps is tested by this exercise.
Comparing one forward output cannot prove that every parameter matches. For example, zero input hides all kernel differences in a Linear layer. Actual restore validation should check state structure and arrays as well as suitable model outputs, using the expected dtype and numerical tolerance.
Problem
Implement orbax_load_simulate(seed, x, features):
- Convert seed and output width
Fto integers; takeD = x.shape[-1]. - Construct two default
nnx.Linear(D, F)layers, each with its own freshly creatednnx.Rngs(seed), to compare initialization locally. - Apply the second layer to
xwith no activation function. - Return the result flattened in row-major order.
x is a finite float32 JAX matrix of shape (B, D), with positive B
and D. Width is a positive integer-valued scalar and seed is nonnegative
and integer-valued; both may arrive as floats. Keep default initializers
and biases. Leave x unchanged.
The output is a JAX vector of shape (B * F,) representing
x @ kernel + bias, with each row's features kept together. Negative
outputs stay negative. Tests check these seeded values; they cannot prove
that two models were constructed or that any checkpoint was restored.
The diagram below is an independent array-placement example, not a step
performed by the function.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
A newly constructed NNX Linear layer can reproduce another layer's initial parameters when both use the same configuration and fresh RNG streams with the same seed. This exercise checks the resulting forward calculation. It does not read checkpoint data or perform a sharded restore.
Start each RNG stream at the same seed
first = nnx.Linear(D, F, rngs=nnx.Rngs(seed))
second = nnx.Linear(D, F, rngs=nnx.Rngs(seed))
output = second(x)
In the same environment, these constructors produce matching kernel and
bias values. They create separate model objects. Reusing one advancing
nnx.Rngs object for both constructors is different: the second layer
receives later keys from the stream.
Reproducibility depends on the layer configuration, initializers, RNG behavior and software environment. It is not a promise of bit-for-bit equality across all versions or backends.
Initialization cannot recover learned parameters
If training changes first.kernel or first.bias, constructing second
from the original seed recreates the initial values. It does not restore
the changes. A real checkpoint restores saved values and may also contain
optimizer state, counters and other state needed to resume training.
NNX separates model structure from array state through operations such as
nnx.split and nnx.merge. Restoring compatible state into a model is a
different operation from constructing fresh random parameters. Real
sharded restoration also needs appropriate array types and placement;
none of those steps is tested by this exercise.
Comparing one forward output cannot prove that every parameter matches. For example, zero input hides all kernel differences in a Linear layer. Actual restore validation should check state structure and arrays as well as suitable model outputs, using the expected dtype and numerical tolerance.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
JAX reference solution
Sign in to track your attempts toward unlocking the reference solution.