Training Systems · Local CPULaptop CPULesson 5 of 5 in Phase 08

Phase 08 / Performance diagnosis

Memory, rematerialization, and optimization tradeoffs

Revision v0.18.1 ·

Lesson · about 100 minutes · CPU practice

Explain why reverse-mode differentiation needs forward intermediates. Compare ordinary and rematerialized gradients with an independent reverse recurrence.

Phase study guide: preparation, milestones and project evidence

Before you start

Before this: Reason with HLO and the roofline model
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/08-performance/05-memory-rematerialization-and-optimization-tradeoffs/code/main.py

PowerShell command · repository folder
.\.venv\Scripts\python.exe phases/08-performance/05-memory-rematerialization-and-optimization-tradeoffs/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: Both gradients agree with the independent reverse recurrence. The saved-residual descriptions differ; the small tested CPU program reports equal temporary-memory estimates. Timings vary and no speedup is asserted.

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

  • Explain why reverse-mode differentiation needs forward intermediates.
  • Compare ordinary and rematerialized gradients with an independent reverse recurrence.
  • Inspect saved residual descriptions and compiled memory estimates without equating them.
  • Measure warm gradient calls and explain why a memory benefit is workload-dependent.

The problem

A model runs out of memory during differentiation. Someone suggests checkpointing every layer. Will that change the answer, and will it necessarily reduce this program’s memory? We will compare gradients, inspect saved residuals and measure compiled estimates and timings before making that decision.

The idea

Reverse-mode differentiation needs information from forward execution. Rematerialization recomputes some of that information instead of retaining it. This exchanges storage for work inside a computation and is distinct from saving a training checkpoint to disk.

Follow an intermediate from creation to last use

During reverse-mode differentiation, a backward rule may need a value produced earlier. Keeping it saves recomputation but extends its lifetime; discarding it can shorten retention if the program recomputes it when needed. Trace one value from creation to last use to predict what a checkpoint boundary can change.

A residual description, a compiler temporary-byte estimate, and peak device memory are separate quantities. The plot has two panels precisely because they need different units and can respond differently.

Follow one layer backward

For h=tanh⁡(xW)h=\tanh(xW), the local tanh derivative uses the forward output: 1−h21-h^2. Given an incoming cotangent, multiply by that factor and then by the transposed weight matrix to propagate sensitivity to xx. Keeping hh lets the backward pass reuse it; rematerializing can reconstruct selected values from the input and weights.

The independent NumPy recurrence checks this chain explicitly, giving a numerical reference for the automatic gradient.

xˉ=W⊤(hˉ⊙(1−h2))\bar x=W^\top\bigl(\bar h\odot(1-h^2)\bigr)

Choose a boundary around pure work

The example wraps a pure layer in jax.checkpoint inside a four-layer objective that sums squared final outputs. Both policies use the same input and weights, so they compare differentiation choices under one workload. Mutation, hidden randomness or side effects would need explicit treatment because recomputation executes the function again.

Choose a boundary around meaningful repeated work. Rematerializing too much can redo most of the objective without removing the values you meant to release.

Inspect what autodiff saves

print_saved_residuals names values retained for differentiation, including a constant 16×1616\times16 weight matrix and selected 16-element intermediates. In the recorded example, ordinary differentiation lists nine descriptions and layer rematerialization lists six. The decrease confirms that the reported autodiff residual set changed, but entries are not bytes and have different shapes.

Read the shapes as well as the count before predicting storage impact.

Read compiled memory estimates separately

The compiled gradient's memory report separates argument, output, temporary and alias bytes. In the recorded CPU run, both policies estimate 256 temporary bytes, despite the residual-description count changing from nine to six. That comparison shows no reduction in this compiler-reported category for these input shapes.

Compiler fusion and buffer reuse mediate between autodiff's saved values and compiled storage. If a backend does not provide this analysis, report it as unavailable rather than treating missing data as zero.

Measure the tradeoff you actually care about

The independent recurrence agrees with both gradients, so the numerical comparison passes for this fixture. Synchronized runtime samples are reported separately; rematerialization can add arithmetic, and these tiny CPU timings vary enough that a strict speed ordering is not a useful decision rule.

To choose a policy for training memory, compare the full representative step on the target device, including optimizer state and inputs. Ask whether it enables the intended batch size while preserving the objective, then verify runtime and model quality.

Read the figure

The upper panel's y-axis counts saved autodiff residual descriptions: ordinary differentiation has 9 and layer rematerialization has 6, including a shared constant matrix. These are entries, not bytes. The lower panel's y-axis is compiler-estimated temporary storage in bytes; both recorded bars are 256 bytes for this CPU example. Equal lower bars mean no estimated temporary-byte reduction in this category.

Connect it to the computation

The independently checked reverse recurrence supports that both policies compute the same gradient on this fixture. The two panels measure different views: rematerialization changes which values autodiff names as residuals, while the compiler still reports 256 temporary bytes for each program. Neither panel measures peak accelerator allocation or total training memory. Next inspect a representative full training step and its target-device memory before choosing a policy.

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 two differentiation policies

Create a Python file in the course environment. Add this block after the preceding block, then run the assembled file.

main.py · append build step 1
# Step 1 — Define two differentiation policies
import contextlib
import io
import time
import numpy as np
import jax
import jax.numpy as jnp
from jax.ad_checkpoint import print_saved_residuals
rng=np.random.default_rng(18)
w=jnp.asarray((rng.normal(size=(16,16))*.1).astype(np.float32))
x=jnp.asarray(np.linspace(-.5,.5,16,dtype=np.float32))
def layer(v):return jnp.tanh(w@v)
def objective(v,rematerialize=False):
    operation=jax.checkpoint(layer) if rematerialize else layer
    for _ in range(4):v=operation(v)
    return jnp.sum(v*v)
plain=lambda v:objective(v,False)
remat=lambda v:objective(v,True)
def residual_report(fn):
    stream=io.StringIO()
    with contextlib.redirect_stdout(stream):print_saved_residuals(fn,x)
    lines=[line for line in stream.getvalue().splitlines() if line.strip()]
    return lines
reports=[residual_report(plain),residual_report(remat)]

Both functions compute the same pure objective; only the recomputation boundary differs. The diagnostic records descriptions rather than claiming physical byte savings.

Explain the APIs in this step
  • jnp.sum(x, axis=...): Adds JAX array values along the selected axis.

2 Verify with an independent reverse pass

Create a Python file in the course environment. Add this block after the preceding block, then run the assembled file.

main.py · append build step 2
# Independent reverse recurrence through the same explicitly specified network.
host_w=np.asarray(w)
values=[np.asarray(x)]
for _ in range(4):values.append(np.tanh(host_w@values[-1]))
cotangent=2*values[-1]
for i in range(4,0,-1):cotangent=host_w.T@(cotangent*(1-values[i]**2))
for fn in (plain,remat):
    np.testing.assert_allclose(jax.grad(fn)(x),cotangent,rtol=3e-5,atol=1e-7)
np.testing.assert_allclose(jax.grad(plain)(x),jax.grad(remat)(x),rtol=1e-6,atol=1e-7)
executables=[jax.jit(jax.grad(fn)).lower(x).compile() for fn in (plain,remat)]
memory=[]
times=[]
for executable in executables:
    analysis=executable.memory_analysis()
    memory.append(None if analysis is None else {name:getattr(analysis,name) for name in ('argument_size_in_bytes','output_size_in_bytes','temp_size_in_bytes','alias_size_in_bytes')})
    # Wait for device work to finish before timing it or comparing its result on the host.
    # Wait for device work to finish before timing it or comparing its result on the host.
    executable(x).block_until_ready()
    samples=[]
    for _ in range(5):
        # Wait for device work to finish before timing it or comparing its result on the host.
        # Wait for device work to finish before timing it or comparing its result on the host.
        # Wait for device work to finish before timing it or comparing its result on the host.
        start=time.perf_counter()
        executable(x).block_until_ready()
        samples.append(time.perf_counter()-start)
    times.append(samples)
print('Saved residual descriptions:')
for name,lines in zip(['plain','remat'],reports):print(name,'\n'+'\n'.join(lines))
print('Compiler memory estimates:',memory)
print('Synchronized gradient samples:',times)
print('Independent reverse recurrence agrees with both gradients.')

The NumPy recurrence propagates cotangents through saved host activations, providing an oracle independent of JAX differentiation.

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.
  • jax.grad(loss_fn)(params, ...): Transforms a scalar-output function into one returning the gradient PyTree with the same structure as params.
  • jax.jit(fn) / @jax.jit: Traces a function for its input signatures and compiles it; changed shapes, dtypes, or static arguments can trigger another compilation.
  • output.block_until_ready(): Waits for asynchronous device work on this array to finish, which is needed before measuring completed computation time.

3 Step 3: Verify invariants on the completed state

Run the final shape and numerical assertions to confirm the state built in Steps 1 and 2.

main.py · append build step 3
# Inspect residual live tensors and compare compiler memory and wall-clock gradient samples:
for name, lines in zip(["plain", "remat"], reports):
    print(name, "\n" + "\n".join(lines))
print("Compiler memory estimates:", memory)
print("Synchronized gradient samples:", times)

Checking these invariants confirms the computation is ready for the full worked experiment.

View the cumulative build in main.py

These blocks append in order to your separate practice file.

main.py · cumulative Python build
# Step 1 — Define two differentiation policies
import contextlib
import io
import time
import numpy as np
import jax
import jax.numpy as jnp
from jax.ad_checkpoint import print_saved_residuals
rng=np.random.default_rng(18)
w=jnp.asarray((rng.normal(size=(16,16))*.1).astype(np.float32))
x=jnp.asarray(np.linspace(-.5,.5,16,dtype=np.float32))
def layer(v):return jnp.tanh(w@v)
def objective(v,rematerialize=False):
    operation=jax.checkpoint(layer) if rematerialize else layer
    for _ in range(4):v=operation(v)
    return jnp.sum(v*v)
plain=lambda v:objective(v,False)
remat=lambda v:objective(v,True)
def residual_report(fn):
    stream=io.StringIO()
    with contextlib.redirect_stdout(stream):print_saved_residuals(fn,x)
    lines=[line for line in stream.getvalue().splitlines() if line.strip()]
    return lines
reports=[residual_report(plain),residual_report(remat)]

# Independent reverse recurrence through the same explicitly specified network.
host_w=np.asarray(w)
values=[np.asarray(x)]
for _ in range(4):values.append(np.tanh(host_w@values[-1]))
cotangent=2*values[-1]
for i in range(4,0,-1):cotangent=host_w.T@(cotangent*(1-values[i]**2))
for fn in (plain,remat):
    np.testing.assert_allclose(jax.grad(fn)(x),cotangent,rtol=3e-5,atol=1e-7)
np.testing.assert_allclose(jax.grad(plain)(x),jax.grad(remat)(x),rtol=1e-6,atol=1e-7)
executables=[jax.jit(jax.grad(fn)).lower(x).compile() for fn in (plain,remat)]
memory=[]
times=[]
for executable in executables:
    analysis=executable.memory_analysis()
    memory.append(None if analysis is None else {name:getattr(analysis,name) for name in ('argument_size_in_bytes','output_size_in_bytes','temp_size_in_bytes','alias_size_in_bytes')})
    # Wait for device work to finish before timing it or comparing its result on the host.
    # Wait for device work to finish before timing it or comparing its result on the host.
    executable(x).block_until_ready()
    samples=[]
    for _ in range(5):
        # Wait for device work to finish before timing it or comparing its result on the host.
        # Wait for device work to finish before timing it or comparing its result on the host.
        # Wait for device work to finish before timing it or comparing its result on the host.
        start=time.perf_counter()
        executable(x).block_until_ready()
        samples.append(time.perf_counter()-start)
    times.append(samples)
print('Saved residual descriptions:')
for name,lines in zip(['plain','remat'],reports):print(name,'\n'+'\n'.join(lines))
print('Compiler memory estimates:',memory)
print('Synchronized gradient samples:',times)
print('Independent reverse recurrence agrees with both gradients.')

# Inspect residual live tensors and compare compiler memory and wall-clock gradient samples:
for name, lines in zip(["plain", "remat"], reports):
    print(name, "\n" + "\n".join(lines))
print("Compiler memory estimates:", memory)
print("Synchronized gradient samples:", times)

Run the example

python3 phases/08-performance/05-memory-rematerialization-and-optimization-tradeoffs/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 — Define two differentiation policies
import contextlib
import io
import time
import numpy as np
import jax
import jax.numpy as jnp
from jax.ad_checkpoint import print_saved_residuals
rng=np.random.default_rng(18)
w=jnp.asarray((rng.normal(size=(16,16))*.1).astype(np.float32))
x=jnp.asarray(np.linspace(-.5,.5,16,dtype=np.float32))
def layer(v):return jnp.tanh(w@v)
def objective(v,rematerialize=False):
    operation=jax.checkpoint(layer) if rematerialize else layer
    for _ in range(4):v=operation(v)
    return jnp.sum(v*v)
plain=lambda v:objective(v,False)
remat=lambda v:objective(v,True)
def residual_report(fn):
    stream=io.StringIO()
    with contextlib.redirect_stdout(stream):print_saved_residuals(fn,x)
    lines=[line for line in stream.getvalue().splitlines() if line.strip()]
    return lines
reports=[residual_report(plain),residual_report(remat)]

# Independent reverse recurrence through the same explicitly specified network.
host_w=np.asarray(w)
values=[np.asarray(x)]
for _ in range(4):values.append(np.tanh(host_w@values[-1]))
cotangent=2*values[-1]
for i in range(4,0,-1):cotangent=host_w.T@(cotangent*(1-values[i]**2))
for fn in (plain,remat):
    np.testing.assert_allclose(jax.grad(fn)(x),cotangent,rtol=3e-5,atol=1e-7)
np.testing.assert_allclose(jax.grad(plain)(x),jax.grad(remat)(x),rtol=1e-6,atol=1e-7)
executables=[jax.jit(jax.grad(fn)).lower(x).compile() for fn in (plain,remat)]
memory=[]
times=[]
for executable in executables:
    analysis=executable.memory_analysis()
    memory.append(None if analysis is None else {name:getattr(analysis,name) for name in ('argument_size_in_bytes','output_size_in_bytes','temp_size_in_bytes','alias_size_in_bytes')})
    # Wait for device work to finish before timing it or comparing its result on the host.
    # Wait for device work to finish before timing it or comparing its result on the host.
    executable(x).block_until_ready()
    samples=[]
    for _ in range(5):
        # Wait for device work to finish before timing it or comparing its result on the host.
        # Wait for device work to finish before timing it or comparing its result on the host.
        # Wait for device work to finish before timing it or comparing its result on the host.
        start=time.perf_counter()
        executable(x).block_until_ready()
        samples.append(time.perf_counter()-start)
    times.append(samples)
print('Saved residual descriptions:')
for name,lines in zip(['plain','remat'],reports):print(name,'\n'+'\n'.join(lines))
print('Compiler memory estimates:',memory)
print('Synchronized gradient samples:',times)
print('Independent reverse recurrence agrees with both gradients.')

Expected result: Both gradients agree with the independent reverse recurrence. The saved-residual descriptions differ; the small tested CPU program reports equal temporary-memory estimates. Timings vary and no speedup is asserted.

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

Saved residual descriptions:
plain 
f32[16,16] from a constant
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of sub from <string>:15:20 (layer)
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of sub from <string>:15:20 (layer)
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of sub from <string>:15:20 (layer)
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of sub from <string>:15:20 (layer)
remat 
f32[16,16] from a constant
f32[16] from the argument v
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of tanh from <string>:15:20 (layer)
Compiler memory estimates: [{'argument_size_in_bytes': 64, 'output_size_in_bytes': 64, 'temp_size_in_bytes': 256, 'alias_size_in_bytes': 0}, {'argument_size_in_bytes': 64, 'output_size_in_bytes': 64, 'temp_size_in_bytes': 256, 'alias_size_in_bytes': 0}]
Synchronized gradient samples: [[6.245775148272514e-05, 5.400041118264198e-05, 4.358310252428055e-05, 3.929203376173973e-05, 3.899959847331047e-05], [4.158308729529381e-05, 4.2833853513002396e-05, 6.041722372174263e-05, 5.133310332894325e-05, 3.700004890561104e-05]]
Independent reverse recurrence agrees with both gradients.
plain 
f32[16,16] from a constant
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of sub from <string>:15:20 (layer)
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of sub from <string>:15:20 (layer)
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of sub from <string>:15:20 (layer)
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of sub from <string>:15:20 (layer)
remat 
f32[16,16] from a constant
f32[16] from the argument v
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of tanh from <string>:15:20 (layer)
f32[16] output of tanh from <string>:15:20 (layer)
Compiler memory estimates: [{'argument_size_in_bytes': 64, 'output_size_in_bytes': 64, 'temp_size_in_bytes': 256, 'alias_size_in_bytes': 0}, {'argument_size_in_bytes': 64, 'output_size_in_bytes': 64, 'temp_size_in_bytes': 256, 'alias_size_in_bytes': 0}]
Synchronized gradient samples: [[6.245775148272514e-05, 5.400041118264198e-05, 4.358310252428055e-05, 3.929203376173973e-05, 3.899959847331047e-05], [4.158308729529381e-05, 4.2833853513002396e-05, 6.041722372174263e-05, 5.133310332894325e-05, 3.700004890561104e-05]]
Saved residual descriptions:
plain 
f32[16,16] from a constant
f32[16] output of tanh from <string>:81:20 (layer)
f32[16] output of sub from <string>:81:20 (layer)
f32[16] output of tanh from <string>:81:20 (layer)
f32[16] output of sub from <string>:81:20 (layer)
f32[16] output of tanh from <string>:81:20 (layer)
f32[16] output of sub from <string>:81:20 (layer)
f32[16] output of tanh from <string>:81:20 (layer)
f32[16] output of sub from <string>:81:20 (layer)
remat 
f32[16,16] from a constant
f32[16] from the argument v
f32[16] output of tanh from <string>:81:20 (layer)
f32[16] output of tanh from <string>:81:20 (layer)
f32[16] output of tanh from <string>:81:20 (layer)
f32[16] output of tanh from <string>:81:20 (layer)
Compiler memory estimates: [{'argument_size_in_bytes': 64, 'output_size_in_bytes': 64, 'temp_size_in_bytes': 256, 'alias_size_in_bytes': 0}, {'argument_size_in_bytes': 64, 'output_size_in_bytes': 64, 'temp_size_in_bytes': 256, 'alias_size_in_bytes': 0}]
Synchronized gradient samples: [[6.358325481414795e-05, 3.262516111135483e-05, 3.1750183552503586e-05, 2.270890399813652e-05, 9.291805326938629e-06], [1.237494871020317e-05, 8.792150765657425e-06, 7.83335417509079e-06, 1.7334241420030594e-05, 1.2707896530628204e-05]]
Independent reverse recurrence agrees with both gradients.
Whole-objective residuals: ['f32[16,16] from a constant', 'f32[16] from the argument v']
Whole-objective memory estimate: CompiledMemoryStats(generated_code_size_in_bytes=0, argument_size_in_bytes=64, output_size_in_bytes=64, alias_size_in_bytes=0, temp_size_in_bytes=256, host_generated_code_size_in_bytes=0, host_argument_size_in_bytes=0, host_output_size_in_bytes=0, host_alias_size_in_bytes=0, host_temp_size_in_bytes=0)
Directional derivative finite/automatic: 0.007404538337141275 0.00740453926846385 epsilon 0.001
Higher-order directional derivatives agree.
PASS: performance-05

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

Move the boundary to the entire objective

Move the boundary to the entire objective · Python experiment
# Experiment — Move the boundary to the entire objective: Correctness is necessary but does not choose the best policy.
whole=jax.checkpoint(plain)
np.testing.assert_allclose(jax.grad(whole)(x),cotangent,rtol=3e-5,atol=1e-7)
print('Whole-objective residuals:',residual_report(whole))
whole_executable=jax.jit(jax.grad(whole)).lower(x).compile()
print('Whole-objective memory estimate:',whole_executable.memory_analysis())

Expected: The derivative remains correct; residual and compiled-memory reports describe this additional boundary.

Correctness is necessary but does not choose the best policy. The boundary determines what is retained and recomputed; inspect actual memory and timing for the intended workload.

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.

Use the direction v=(1,…,1)v=(1,\ldots,1) and central differences to independently check a directional derivative of the original objective at the existing input. Compare it with the gradient dot direction and report the perturbation.

Show a writing guide and starter scaffold

How to write this exercise code

Learner-only inputs, outputs, checks and hints

Inputs

  • The original objective, its existing evaluation point, an all-ones direction, and the finite-difference perturbation.

Outputs

  • The central-difference directional derivative, gradient dot direction, and their discrepancy at the stated perturbation.

Invariants to check

  • The derivative uses the original objective and existing input; the finite-difference direction is the requested all-ones vector.

Hints

  • Evaluate the scalar objective at the point plus and minus epsilon times the direction.
  • Compute the autodiff gradient once and take its dot product with the same direction.
Your attempt · complete the TODOs
import jax
import jax.numpy as jnp

def check_directional_derivative(objective, point, direction, epsilon):
    # TODO: Compare the directional central difference with the gradient dot direction.
    raise NotImplementedError
Reveal the reference solution
Reference solution
# Exercise solution
direction=jnp.ones_like(x)
eps=.001
finite=(float(plain(x+eps*direction))-float(plain(x-eps*direction)))/(2*eps)
automatic=float(jnp.vdot(jax.grad(plain)(x),direction))
np.testing.assert_allclose(finite,automatic,rtol=3e-3,atol=1e-6)
print('Directional derivative finite/automatic:',finite,automatic,'epsilon',eps)

Check higher-order differentiation

Transfer

Compare Hessian-vector products of the ordinary and rematerialized objectives along a nonuniform direction. Explain why equal first derivatives alone would be a weaker test.

Show a writing guide and starter scaffold

How to write this practice solution

Learner-only inputs, outputs, checks and hints

Inputs

  • Compare Hessian-vector products of the ordinary and rematerialized objectives along a nonuniform direction. Explain why equal first derivatives alone would be a weaker test.

Outputs

  • Both HVP vectors and an independent parity check.

Invariants to check

  • The comparison uses the same point, direction, and objective for both differentiation paths.

Hints

  • Apply jvp to each gradient function with the same primal point and tangent.
Your attempt · complete the TODOs
import jax
import jax.numpy as jnp
import numpy as np
from jax.ad_checkpoint import print_saved_residuals

def practice_check_higher_order_differentiation(objective, point, direction):
    """Implement the check higher-order differentiation check."""
    # TODO: Compare ordinary and rematerialized Hessian-vector products along the nonuniform direction.
    raise NotImplementedError
Show a hint

Apply jvp to each gradient function with the same primal point and tangent.

Reveal reference solution and reasoning
Check higher-order differentiation · Python solution
# Check higher-order differentiation (Transfer)
direction=jnp.linspace(-1.,1.,len(x))
_,hvp_plain=jax.jvp(jax.grad(plain),(x,),(direction,))
_,hvp_remat=jax.jvp(jax.grad(remat),(x,),(direction,))
np.testing.assert_allclose(hvp_plain,hvp_remat,rtol=3e-5,atol=1e-7)
assert np.isfinite(np.asarray(hvp_plain)).all()
print('Higher-order directional derivatives agree.')

Recomputation should preserve the differentiable function in this pure example, including its higher-order directional behavior. This still is separate from a device-memory improvement.

Check your understanding

Rematerialization reduces the number of saved residual descriptions but the compiled temporary-byte estimates are equal. What is the supported conclusion?

Choose one answer

Diagnose the result

If gradients differ, first compare inputs, weights, random keys and any hidden effects. If memory does not improve, inspect the checkpoint boundary, residual shapes, compiler buffer estimates and full-step peak measurements before adding more checkpoint wrappers.

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

Carry forward

  • Recomputation trades retained intermediate values for extra work.
  • Verify the derivative independently before evaluating a policy.
  • Saved values, estimated buffers and measured peak memory must remain distinct.

Keep your evidence

Download evidence template

Keep the plain/rematerialized gradient and HVP comparisons, saved-residual counts, compiler-memory observations and synchronized timings. Explain why fewer residuals do not guarantee lower measured memory.

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

Phase 08 Integration Project

Bring this phase together: Measure CPU JIT latency and changed-shape traces with retained JAXPR evidence

Checkpoint to prove: Separate compilation, execution, input stalls, and synchronization before claiming a speedup.

Work in projects/performance-audit/starter/model.py. Complete stages 1 and 2 for independent calculations and changed-condition diagnosis, then retain the stage 3 evidence with --evidence-dir runs/performance-audit. Stage 4 integrates the checks. Learner commands explicitly select the starter; --implementation solution is instructor reference execution. This bounded practice does not certify the entire phase or target hardware.