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

Phase 08 / Performance diagnosis

Benchmark asynchronous work correctly

Revision v0.18.1 ·

Lesson · about 80 minutes · CPU practice

Warm up a computation and synchronize results before reporting timing.

Phase study guide: preparation, milestones and project evidence

Before you start

Before this: Neural network training
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/01-benchmark-asynchronous-work-correctly/code/main.py

PowerShell command · repository folder
.\.venv\Scripts\python.exe phases/08-performance/01-benchmark-asynchronous-work-correctly/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: A JSON benchmark report: input_shape [64,32][64, 32], weight_shape [32,16][32, 16], dtype float32, seven nonnegative warmed samples, and a checked maximum error. Times vary by run; there is no required speedup.

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

  • Draw the interval a timing actually measures.
  • Exclude initial placement from a device-resident benchmark.
  • Check an independent NumPy result before comparing timings.
  • Produce a report containing first-call and warmed sample distributions.

The problem

A first timing suggests a tenfold speedup. Before celebrating, let’s ask when the timer started and stopped. We’ll separate the first call from warm calls, wait for JAX to finish, and compare numerical results. By the end, you’ll have a timing report another learner can reproduce.

The idea

JAX can return an array handle before device work finishes. A benchmark must define what starts the interval and what counts as completion. First-call preparation, warm execution and end-to-end requests answer different performance questions.

Time a completed result

A JAX call can return a handle before its device work is complete, so a timer that stops at return may measure dispatch rather than the finished result. Wait on the output before stopping the clock when the question is completed-result latency.

Separate first-call preparation from repeated calls, and name the boundary you care about. This lesson starts with inputs already placed; an end-to-end request would need a different interval that includes whichever preparation and transfers belong to that request.

Read left to right: already-ready inputs reach the host, the host dispatches a call, and the device-work lane proceeds while the host waits. The timer bracket starts at dispatch and ends at result readiness, so the wait is inside the measurement. The arrows show dependency and elapsed ordering, not measured durations; input preparation and optional compilation need their own boundary if the question includes them.

Open full-size diagram
Tfirst=tfirst,ready−tfirst,start,Twarm=median⁡r(tr,ready−tr,start)T_{\rm first}=t_{\rm first,ready}-t_{\rm first,start},\qquad T_{\rm warm}=\operatorname{median}_r(t_{r,\rm ready}-t_{r,\rm start})

Draw the boundary before starting the clock

The workload maps (64,32)(64,32) times (32,16)(32,16) to (64,16)(64,16), then applies tanh. Both inputs are placed on CPU and ready before the timer starts; the timer stops after the output is ready. Thus the interval includes host dispatch and completion waiting, while input creation, initial placement and conversion to NumPy are outside it.

Use this boundary for the question “how long until this placed-input result is ready?” It is not a kernel-only duration or a full serving request.

prepare host arrays → place → wait | start → call → result handle → wait → stop | inspect host values

A handle is not proof of completion

Python can receive a JAX array while its values are still being computed. Shape and dtype are metadata; block_until_ready waits for the result, while conversion to NumPy also materializes values on the host. Put the wait inside the timed interval when reporting completed-result latency.

This CPU workload may make an unsynchronized and synchronized timing look similar. That observation does not turn a handle-return timer into a portable completion measurement, and subtracting separate calls cannot isolate exact queue or execution time.

First call and steady state answer different questions

The first jitted call can trace Python, lower the program, obtain an executable and run it. The first-call timer includes that combined interval. Later calls with the same signature can reuse the executable, so the warm samples answer a different question: how long does repeated completed work take in this process?

For a worked conversion, suppose a first call took 4040 milliseconds and a warm call took 2020 microseconds. Converting both to seconds gives 0.040.04 and 0.000020.00002, a ratio of 20002000. These illustrative values explain the unit conversion, not a promised speedup. Nor would their difference isolate compilation: the first interval can include other first-use work.

Keep every warm sample beside the median. Seven observations show variation but do not establish a confidence interval or deployment behavior. The plot and the script receipt are separate captures; use each capture's own samples when calculating its summary.

Correctness comes before a speed ratio

The NumPy reference uses the same float32 inputs, matrix product and tanh. The recorded maximum absolute error is about 2.38×10−72.38\times10^{-7}, within the declared tolerance. Check this equivalence before comparing timing.

If dtype, shape or operation differs, a lower time answers a different question. Keep the workload contract and numerical error next to the timing report.

Read the figure

The upper panel has one bar for the first synchronized call; the lower panel has seven points for repeated synchronized calls of the same signature. Both vertical axes are seconds, but they use independent scales. Read the tick values and any scientific-notation multiplier before comparing heights across panels.

The first-call interval is on the order of hundredths of a second in this local capture, while the warm points are much smaller. Each lower-panel dot is a repeated timing sample, not a training step, and its vertical movement is timing variation rather than an improving objective. Find the minimum and maximum dots, then locate the median among all seven values. One unusually low sample is not a repeatable latency estimate.

Connect it to the computation

Inputs were already placed before both measurements, and each interval ends when the output is ready. The first interval can include preparation plus execution; the warm interval measures reuse of the same compiled signature. The separate execution receipt is another run of that protocol, so its individual timings need not equal the plotted capture.

Now change the question: if a user supplies a fresh NumPy input for every request, which preparation or placement costs belong inside the timer? Draw that new boundary before comparing it with this resident-input result. A valid timing comparison needs equivalent work as well as a common unit.

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 Prepare and place the workload

Create benchmark.py in your activated course environment and add this block.

main.py · append build step 1
# Step 1 — Prepare and place the workload: CPU placement and waiting occur before timing.
import json
import platform
import time
import statistics
import numpy as np
import jax
import jax.numpy as jnp
cpu = jax.devices("cpu")[0]
rng = np.random.default_rng(4)
x_host = rng.normal(size=(64, 32)).astype(np.float32)
w_host = rng.normal(size=(32, 16)).astype(np.float32)
# Place `x` explicitly onto the target JAX device.
x = jax.device_put(x_host, cpu)
# Place `w` explicitly onto the target JAX device.
w = jax.device_put(w_host, cpu)
# Wait for device work to finish before timing it or comparing its result on the host.
jax.block_until_ready((x, w))

CPU placement and waiting occur before timing. The deterministic host inputs make the numerical check reproducible.

Explain the APIs in this step
  • jax.device_put(x, device=...): Places or transfers an array to the requested device or sharding; dispatch is asynchronous.
  • jax.block_until_ready(tree): Waits for asynchronous device work on every array in a PyTree to finish.

2 Define the contract and timer

Append this block to the same file. Run the assembled file with python benchmark.py.

main.py · append build step 2
# Step 2 — Define the contract and timer
def predict(x, w):
    return jnp.tanh(x @ w)
# Wrap with `jax.jit` (`compiled_predict`) so XLA traces and compiles the function.
compiled_predict = jax.jit(predict)
def synchronized_samples(fn, args, repeats=7):
    if repeats < 1:
        raise ValueError("at least one repeat is required")
    samples = []
    for _ in range(repeats):
        start = time.perf_counter()
        result = fn(*args)
        # Wait for device work to finish before timing it or comparing its result on the host.
        jax.block_until_ready(result)
        samples.append(time.perf_counter() - start)
    return result, samples

The timer accepts arbitrary result pytrees because jax.block_until_ready waits across their array leaves. It collects observations without asserting which implementation is faster.

Explain the APIs in this step
  • 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.
  • jax.block_until_ready(tree): Waits for asynchronous device work on every array in a PyTree to finish.

3 Separate first call from warm calls

Append this block to the same file. Run the assembled file with python benchmark.py.

main.py · append build step 3
# Step 3 — Separate first call from warm calls: The report prints variable measured times.
start = time.perf_counter()
first = compiled_predict(x, w)
# Wait for device work to finish before timing it or comparing its result on the host.
first.block_until_ready()
first_call_seconds = time.perf_counter() - start
result, samples = synchronized_samples(compiled_predict, (x, w))
reference = np.tanh(x_host @ w_host)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(result), reference, atol=2e-5, rtol=2e-5)
report = {
    "backend": str(cpu), "jax": jax.__version__, "numpy": np.__version__,
    "python": platform.python_version(), "input_shape": list(x.shape),
    "weight_shape": list(w.shape), "dtype": str(x.dtype),
    "boundary": "input already placed; call plus output synchronization",
    "first_call_seconds": first_call_seconds,
    "warm_samples_seconds": samples, "warm_median_seconds": statistics.median(samples),
    "max_abs_error": float(np.max(np.abs(np.asarray(result) - reference))),
}
assert len(samples) == 7 and all(t >= 0 for t in samples)
print(json.dumps(report, indent=2))

The report prints variable measured times. Redirect the full run output to a text file and keep the benchmark script with it.

Explain the APIs in this step
  • output.block_until_ready(): Waits for asynchronous device work on this array to finish, which is needed before measuring completed computation time.
  • np.testing.assert_allclose(actual, desired, rtol=..., atol=...): Checks a host-side numerical expectation and raises AssertionError when values differ beyond tolerance.
View the cumulative build in main.py

These blocks append in order to your separate practice file.

main.py · cumulative Python build
# Step 1 — Prepare and place the workload: CPU placement and waiting occur before timing.
import json
import platform
import time
import statistics
import numpy as np
import jax
import jax.numpy as jnp
cpu = jax.devices("cpu")[0]
rng = np.random.default_rng(4)
x_host = rng.normal(size=(64, 32)).astype(np.float32)
w_host = rng.normal(size=(32, 16)).astype(np.float32)
# Place `x` explicitly onto the target JAX device.
x = jax.device_put(x_host, cpu)
# Place `w` explicitly onto the target JAX device.
w = jax.device_put(w_host, cpu)
# Wait for device work to finish before timing it or comparing its result on the host.
jax.block_until_ready((x, w))

# Step 2 — Define the contract and timer
def predict(x, w):
    return jnp.tanh(x @ w)
# Wrap with `jax.jit` (`compiled_predict`) so XLA traces and compiles the function.
compiled_predict = jax.jit(predict)
def synchronized_samples(fn, args, repeats=7):
    if repeats < 1:
        raise ValueError("at least one repeat is required")
    samples = []
    for _ in range(repeats):
        start = time.perf_counter()
        result = fn(*args)
        # Wait for device work to finish before timing it or comparing its result on the host.
        jax.block_until_ready(result)
        samples.append(time.perf_counter() - start)
    return result, samples

# Step 3 — Separate first call from warm calls: The report prints variable measured times.
start = time.perf_counter()
first = compiled_predict(x, w)
# Wait for device work to finish before timing it or comparing its result on the host.
first.block_until_ready()
first_call_seconds = time.perf_counter() - start
result, samples = synchronized_samples(compiled_predict, (x, w))
reference = np.tanh(x_host @ w_host)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(result), reference, atol=2e-5, rtol=2e-5)
report = {
    "backend": str(cpu), "jax": jax.__version__, "numpy": np.__version__,
    "python": platform.python_version(), "input_shape": list(x.shape),
    "weight_shape": list(w.shape), "dtype": str(x.dtype),
    "boundary": "input already placed; call plus output synchronization",
    "first_call_seconds": first_call_seconds,
    "warm_samples_seconds": samples, "warm_median_seconds": statistics.median(samples),
    "max_abs_error": float(np.max(np.abs(np.asarray(result) - reference))),
}
assert len(samples) == 7 and all(t >= 0 for t in samples)
print(json.dumps(report, indent=2))

Run the example

python3 phases/08-performance/01-benchmark-asynchronous-work-correctly/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 and place the workload: CPU placement and waiting occur before timing.
import json
import platform
import time
import statistics
import numpy as np
import jax
import jax.numpy as jnp
cpu = jax.devices("cpu")[0]
rng = np.random.default_rng(4)
x_host = rng.normal(size=(64, 32)).astype(np.float32)
w_host = rng.normal(size=(32, 16)).astype(np.float32)
# Place `x` explicitly onto the target JAX device.
x = jax.device_put(x_host, cpu)
# Place `w` explicitly onto the target JAX device.
w = jax.device_put(w_host, cpu)
# Wait for device work to finish before timing it or comparing its result on the host.
jax.block_until_ready((x, w))
# Step 2 — Define the contract and timer
def predict(x, w):
    return jnp.tanh(x @ w)
# Wrap with `jax.jit` (`compiled_predict`) so XLA traces and compiles the function.
compiled_predict = jax.jit(predict)
def synchronized_samples(fn, args, repeats=7):
    if repeats < 1:
        raise ValueError("at least one repeat is required")
    samples = []
    for _ in range(repeats):
        start = time.perf_counter()
        result = fn(*args)
        # Wait for device work to finish before timing it or comparing its result on the host.
        jax.block_until_ready(result)
        samples.append(time.perf_counter() - start)
    return result, samples
# Step 3 — Separate first call from warm calls: The report prints variable measured times.
start = time.perf_counter()
first = compiled_predict(x, w)
# Wait for device work to finish before timing it or comparing its result on the host.
first.block_until_ready()
first_call_seconds = time.perf_counter() - start
result, samples = synchronized_samples(compiled_predict, (x, w))
reference = np.tanh(x_host @ w_host)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(result), reference, atol=2e-5, rtol=2e-5)
report = {
    "backend": str(cpu), "jax": jax.__version__, "numpy": np.__version__,
    "python": platform.python_version(), "input_shape": list(x.shape),
    "weight_shape": list(w.shape), "dtype": str(x.dtype),
    "boundary": "input already placed; call plus output synchronization",
    "first_call_seconds": first_call_seconds,
    "warm_samples_seconds": samples, "warm_median_seconds": statistics.median(samples),
    "max_abs_error": float(np.max(np.abs(np.asarray(result) - reference))),
}
assert len(samples) == 7 and all(t >= 0 for t in samples)
print(json.dumps(report, indent=2))

Expected result: A JSON benchmark report: input_shape [64,32][64, 32], weight_shape [32,16][32, 16], dtype float32, seven nonnegative warmed samples, and a checked maximum error. Times vary by run; there is no required speedup.

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

{
  "backend": "TFRT_CPU_0",
  "jax": "0.9.2",
  "numpy": "2.4.4",
  "python": "3.14.3",
  "input_shape": [
    64,
    32
  ],
  "weight_shape": [
    32,
    16
  ],
  "dtype": "float32",
  "boundary": "input already placed; call plus output synchronization",
  "first_call_seconds": 0.057142124976962805,
  "warm_samples_seconds": [
    0.00012704217806458473,
    5.745887756347656e-05,
    4.9666035920381546e-05,
    1.4457851648330688e-05,
    1.058308407664299e-05,
    1.0292045772075653e-05,
    9.458977729082108e-06
  ],
  "warm_median_seconds": 1.4457851648330688e-05,
  "max_abs_error": 2.384185791015625e-07
}
{
  "backend": "TFRT_CPU_0",
  "jax": "0.9.2",
  "numpy": "2.4.4",
  "python": "3.14.3",
  "input_shape": [
    64,
    32
  ],
  "weight_shape": [
    32,
    16
  ],
  "dtype": "float32",
  "boundary": "input already placed; call plus output synchronization",
  "first_call_seconds": 0.030218500178307295,
  "warm_samples_seconds": [
    0.00024641724303364754,
    0.00013183290138840675,
    0.0004402496851980686,
    0.00034912489354610443,
    0.00017945794388651848,
    9.325006976723671e-05,
    0.0001166253350675106
  ],
  "warm_median_seconds": 0.00017945794388651848,
  "max_abs_error": 2.384185791015625e-07
}
Handle-only seconds: 3.370782360434532e-05
Complete-call samples: [3.7457793951034546e-05, 2.9458198696374893e-05, 1.7083249986171722e-05]
Structured samples: [0.0005777501501142979, 0.00021449988707900047, 0.00011362507939338684]
Changed workload: (96, 32) [7.704179733991623e-05, 2.133287489414215e-05, 3.7250109016895294e-05, 8.712522685527802e-05, 4.124967381358147e-05]
Energy shape and samples: (64,) [9.020790457725525e-05, 8.579203858971596e-05, 3.191595897078514e-05, 1.8458813428878784e-05, 1.4250166714191437e-05]
{'boundary': 'placed inputs; warm call plus output wait', 'samples': [4.637520760297775e-05, 0.00023041712120175362, 0.0006031668744981289, 0.00011795805767178535, 7.745902985334396e-05]}
PASS: performance-01

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

Compare handle-only and synchronized boundaries

Compare handle-only and synchronized boundaries · Python experiment
# Experiment — Compare handle-only and synchronized boundaries
start = time.perf_counter()
handle = compiled_predict(x, w)
handle_seconds = time.perf_counter() - start
handle.block_until_ready()  # drain this call before the separate comparison
_, complete_samples = synchronized_samples(compiled_predict, (x, w), repeats=3)
print("Handle-only seconds:", handle_seconds)
print("Complete-call samples:", complete_samples)

Expected: One handle-only interval and three complete-call intervals, all observed rather than fixed.

The separate intervals have different measurement boundaries and noise. Only synchronized samples guarantee completed output before the timer stops; no strict timing ordering is asserted.

Return structured results without host copies

Return structured results without host copies · Python experiment
# Experiment — Return structured results without host copies
# Wrap with `jax.jit` (`structured`) so XLA traces and compiles the function.
structured = jax.jit(lambda a, b: {"prediction": jnp.tanh(a @ b), "mean": jnp.mean(jnp.tanh(a @ b))})
# Wait for device work to finish before timing it or comparing its result on the host.
jax.block_until_ready(structured(x, w))
structured_result, structured_times = synchronized_samples(structured, (x, w), 3)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(structured_result["prediction"]), reference, atol=2e-5, rtol=2e-5)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(structured_result["mean"]), reference.mean(), atol=2e-5, rtol=2e-5)
print("Structured samples:", structured_times)

Expected: Three timings and independently verified prediction/mean leaves.

A tree-level wait makes the benchmark contract explicit for every array leaf. Host inspection occurs after the timer, so the report describes device-ready computation rather than transfer-inclusive output consumption.

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 benchmark with 9696 rows while preserving the feature and output dimensions. Keep a separate report rather than overwriting the original workload label.

Show a writing guide and starter scaffold

How to write this exercise code

Learner-only inputs, outputs, checks and hints

Inputs

  • A JAX function, fixed workload, warmup count, and repeat count.

Outputs

  • Raw synchronized timings with compilation separated from warmed execution.

Invariants to check

  • Every sample waits for completed output; workload shape/dtype and timing boundary are recorded.

Hints

  • Call block_until_ready inside each measured interval.
  • Use an independent NumPy result for the changed equivalent workload.
Your attempt · complete the TODOs
import jax.numpy as jnp
import time
import statistics

def synchronized_benchmark(function, arguments, warmup, repeats):
    """Raw synchronized timings with compilation separated from warmed execution."""
    # TODO: Warm the selected JAX function and synchronize every timed result while retaining raw samples.
    raise NotImplementedError
Reveal the reference solution
Reference solution
# Exercise solution
larger_host = rng.normal(size=(96, 32)).astype(np.float32)
# Place `larger` explicitly onto the target JAX device.
larger = jax.device_put(larger_host, cpu)
# Wait for device work to finish before timing it or comparing its result on the host.
larger.block_until_ready()
# Wait for device work to finish before timing it or comparing its result on the host.
compiled_predict(larger, w).block_until_ready()
larger_result, larger_times = synchronized_samples(compiled_predict, (larger, w), 5)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(larger_result), np.tanh(larger_host @ w_host), atol=2e-5, rtol=2e-5)
print("Changed workload:", larger.shape, larger_times)

Benchmark a different equivalent workload

Transfer

Replace tanh with a row-wise sum of squared predictions, returning one scalar per row. Derive the NumPy reference, warm this new function, collect five synchronized samples and record the changed output shape.

Show a writing guide and starter scaffold

How to write this practice solution

Learner-only inputs, outputs, checks and hints

Inputs

  • Replace tanh with a row-wise sum of squared predictions, returning one scalar per row. Derive the NumPy reference, warm this new function, collect five synchronized samples and record the changed output shape.

Outputs

  • Per-row scalar outputs, NumPy parity, and five completed timing samples.

Invariants to check

  • The benchmark synchronizes each output and records the changed output shape.

Hints

  • Square before reducing the output-feature axis; the result has shape (64,)(64,).
Your attempt · complete the TODOs
import jax.numpy as jnp
import numpy as np
import time
import statistics

def practice_benchmark_a_different_equivalent_workload(features, weights):
    """Implement the benchmark a different equivalent workload check."""
    # TODO: Implement row-wise squared-sum predictions, derive a NumPy reference, warm the changed function, and collect five synchronized samples.
    raise NotImplementedError
Show a hint

Square before reducing the output-feature axis; the result has shape (64,)(64,).

Reveal reference solution and reasoning
Benchmark a different equivalent workload · Python solution
# Benchmark a different equivalent workload (Transfer): Changing the computation changes the performance question.
# Wrap with `jax.jit` (`energy`) so XLA traces and compiles the function.
energy = jax.jit(lambda a, b: jnp.sum((a @ b)**2, axis=1))
# Wait for device work to finish before timing it or comparing its result on the host.
energy(x, w).block_until_ready()
energy_result, energy_times = synchronized_samples(energy, (x, w), 5)
# Reduce along axis=1 to compute `energy_reference`.
energy_reference = np.sum((x_host @ w_host)**2, axis=1)
# Check tensor shape invariant: `energy_result.shape == (64,)`
assert energy_result.shape == (64,)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(energy_result), energy_reference, atol=2e-3, rtol=2e-5)
print("Energy shape and samples:", energy_result.shape, energy_times)

Changing the computation changes the performance question. The shape and independent numerical check prevent a benchmark from accidentally timing an unrelated scalar reduction.

Repair a timer that includes setup but omits completion

Diagnosis

Inspect the broken timer below conceptually: start; create inputs; call jit(predict)(inputs); stop. Write a repaired runner and show its explicit boundary. Explain which stages the broken interval mixes and misses.

Show a writing guide and starter scaffold

How to write this practice solution

Learner-only inputs, outputs, checks and hints

Inputs

  • Inspect the broken timer below conceptually: start; create inputs; call jit(predict)(inputs); stop. Write a repaired runner and show its explicit boundary. Explain which stages the broken interval mixes and misses.

Outputs

  • A runner with separately stated setup and completed-work timing boundaries.

Invariants to check

  • The measured interval contains the declared workload and waits for its result.

Hints

  • Prepare and wait for inputs, reuse one compiled callable, warm it, then synchronize the returned output inside every measured interval.
Your attempt · complete the TODOs
import jax.numpy as jnp
import time

def practice_repair_a_timer_that_includes_setup_but_omits_completion(function, arguments):
    """Implement the repair a timer that includes setup but omits completion check."""
    # TODO: Move input creation outside the timed region and block on the output before stopping the clock.
    raise NotImplementedError
Show a hint

Prepare and wait for inputs, reuse one compiled callable, warm it, then synchronize the returned output inside every measured interval.

Reveal reference solution and reasoning
Repair a timer that includes setup but omits completion · Python solution
# Repair a timer that includes setup but omits completion (Diagnosis)
def repaired_benchmark(fn, args):
    # Wait for device work to finish before timing it or comparing its result on the host.
    jax.block_until_ready(args)
    # Wait for device work to finish before timing it or comparing its result on the host.
    jax.block_until_ready(fn(*args))
    result, measured = synchronized_samples(fn, args, 5)
    return {"boundary": "placed inputs; warm call plus output wait", "samples": measured}, result
fixed_report, fixed_result = repaired_benchmark(compiled_predict, (x, w))
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(fixed_result), reference, atol=2e-5, rtol=2e-5)
assert len(fixed_report["samples"]) == 5
print(fixed_report)

The repaired boundary excludes setup, separates warmup and waits before stopping. A speedup cannot be reconstructed from the flawed interval; rerun both implementations under the same contract.

Check your understanding

What does the synchronized warmed timer in this lesson measure?

Choose one answer

Diagnose the result

If samples vary, first check workload identity, dtype, the warmup call, where synchronization occurs and system contention. Do not change a timer until its start/stop boundary answers the intended question.

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

Carry forward

  • Keep first-call latency separate from warmed samples.
  • Wait for results before reporting completed computation.
  • Report numerical equivalence and workload details beside times.

Keep your evidence

Download evidence template

Keep benchmark.py, complete observed JSON/sample output, workload shapes/dtypes, numerical tolerance/error, first-call versus warmed boundaries, changed-workload report and repaired timer explanation.

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 reference explicitly selects CPU. Uploading the same file or setting JAX_PLATFORMS does not adapt it to TPU. Use the execution matrix to review device selection and data ownership before adapting a separate target program. Execution matrix and target qualification.

Primary references