Phase 00 / Setup & first steps
Move your experiment to a TPU
Revision v0.18.1 · Updated since your last visit. Review the changes; your progress is saved.
Lesson · about 45 minutes · CPU practice
Use the recorded CPU reference to check arithmetic and placement, then run only a named, source-reviewed TPU target with pinned dependencies and verify its observed backend receipt.
Phase study guide: preparation, milestones and project evidence
Before you start
Before this: Practice with four virtual CPU devices
Hardware: CPU-validated lesson reference; optional single-host v5e1 TPU VM for the named qualification targets only
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.
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements-cpu.txtpy -3 -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements-cpu.txtAfter activation, run the complete worked example from that same folder:
python phases/00-welcome/03-move-your-experiment-to-a-tpu/code/main.py.\.venv\Scripts\python.exe phases/00-welcome/03-move-your-experiment-to-a-tpu/code/main.pyFor 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.
Download this lesson’s notebook. Open it with a Python kernel using the course environment. Run cells from the top after restarting the kernel; experiments depend on earlier definitions.
For local Jupyter, first install the course requirements using the Local Python steps, then use your environment’s Python to start Jupyter:
python -m pip install jupyterlab
python -m jupyter lab.\.venv\Scripts\python.exe -m pip install jupyterlab
.\.venv\Scripts\python.exe -m jupyter labSelect the downloaded welcome-03.ipynb file in JupyterLab. The notebook contains reference solutions; make your prediction and attempt each practice task before opening them. Restart the kernel before changing logical device setup.
The worked reference for this lesson is CPU-validated. Some references explicitly configure logical CPU devices; setting JAX_PLATFORMS does not adapt them to a TPU. Follow the execution matrix and reviewed multi-host example before attempting a target run.
For accelerator preparation, study TPU setup, observed runtime receipts, checkpointed jobs and precision and retained traces. Current receipts cover bounded v5e1 launch, resume, and precision checks; four single-device Pallas cases; and a four-device v5e collective fixture. Each receipt is limited to its recorded source, environment, cases, and hardware. It does not qualify all lessons or TPU configurations. Cloud provisioning is optional for CPU learning.
Check: The recorded CPU reference predicts [0.34375, 0.8125, 1.28125, 1.75, 2.21875, 2.6875, 3.15625, 3.625]. Its report names the actual output device. There is no recorded TPU result.
Revisit the guided CPU setup · Open 19: TPU Setup. Package instructions are CPU instructions; Windows and accelerator environments need their own validation.
What you will be able to do
- Verify the requested platform, observed device, completed prediction, and independent numerical reference.
- Provision a single-host Cloud TPU VM for a named qualification target and verify device discovery without treating it as workload evidence.
- Run the downloadable launcher or precision target with the pinned qualification environment and retain its observed TPU-backend receipt.
- Review each additional program’s source, dependencies, topology, and data ownership before considering a TPU run.
The problem
Your notebook imports JAX successfully, but is the prediction actually running on a TPU? We will build a portable device check with an independent numerical answer, then require the requested backend so an unavailable accelerator cannot silently become a successful CPU demonstration.
The idea
Moving an experiment to a TPU gives us two questions: did it calculate the intended result, and did it actually execute on the requested device? We answer the first with an independent numerical reference and the second with an observed device receipt.
Choose the beginner CPU route or the optional placement branch
If array shapes and transformed functions are new to you, continue from the first array lesson to Arrays & pure functions. Ordinary CPU lessons do not require virtual devices or cloud setup. Return here after JAX transformations and the first distributed-placement lesson. This branch assumes you can name the axis split across devices, explain what a replica contains and distinguish a JIT specialization from a new array value.
For a minimal placement prediction, imagine four logical devices and an array with eight rows. A row partition gives each device two rows; replication gives each device all eight. Inspect the reported shard indices to distinguish those cases. Four logical CPU devices do not create four physical accelerators. A physical TPU target additionally needs a reviewed source, pinned environment, expected backend and expected topology.
Check arithmetic and placement separately
Consider a prediction that adds a scalar bias after a dot product. Reducing the bias by should reduce every prediction by , whatever compatible device performs the computation. That is a numerical invariant you can derive before running.
Now inspect where the completed result resides. A correct NumPy comparison on CPU does not show that a TPU was selected. Conversely, a TPU device label does not show that the formula or input data was correct. Keep both checks.
Wait for the result before recording completion or timing. A returned array handle can precede finished device work. Preserve the requested platform, observed platform, environment and error tolerance alongside the values.
Carry a prediction, not just an installation
The target is a small matrix-vector prediction with eight observation rows and three features per row. The first row is , the fixed weights make its dot product , and adding the bias gives . Derive this before running so the first printed value has an independent check.
The program compares the JAX prediction with a NumPy calculation and records shape, dtype, observed platform and error. A package import alone would establish none of those. Keep the expected CPU run as the reference; the optional target workflow has separate qualification evidence and must be interpreted on its own terms.
Establish the CPU reference first
Save the assembled code as main.py in your course workspace and run it in a fresh process. By default COURSE_EXPECT_PLATFORM is cpu. The program selects that backend explicitly, places both inputs on its first device and waits for the compiled prediction to finish. NumPy independently calculates the same arithmetic. The recorded CPU receipt reports shape , float32, one process, and maximum absolute error ; the first prediction is . Read these as separate checks of output structure, environment, and numerical agreement.
The tolerance allows small floating-point differences; a successful comparison means this prediction agrees within that tolerance. It is separate from accuracy for a larger model. We use float32 inputs deliberately. Implicit dtype changes and reduced-precision matrix multiplication should be separate experiments after the placement check.
Run instructions: Run the CPU reference
# Run the CPU reference using the course Python environment
python main.pyExpected:
The report names cpu, shape [8], float32, a completed output device and a small maximum absolute error. The first prediction is 0.34375.
Set up a single-host TPU VM for reviewed targets
For the first physical TPU practice, use only the qualification targets in the downloadable TPU qualification workspace (jax-tpu-gcp.zip) on a single-host v5e1 TPU VM. This lesson’s main.py is a CPU-validated reference; its recorded output is not a physical TPU result. Other course scripts need their own source, dependency, topology, and data-ownership review before target execution.
A Cloud TPU VM is billable and stays active after Python exits. Choose an approved project and zone, verify access and quota, and set a bounded lifetime before provisioning. Follow the same cleanup steps after collecting the target receipts.
Run instructions: From your laptop: request a bounded Flex Start TPU and inspect the queue
# Use your approved project; this recorded v5e setup uses us-west4-a.
export PROJECT_ID="REPLACE_WITH_YOUR_PROJECT"
export ZONE="us-west4-a"
export TPU_NAME="jax-course-tpu-01"
gcloud alpha compute tpus queued-resources create "$TPU_NAME" \
--project="$PROJECT_ID" --zone="$ZONE" --node-id="$TPU_NAME" \
--accelerator-type=v5litepod-1 --runtime-version=v2-alpha-tpuv5-lite \
--provisioning-model=FLEX_START --max-run-duration=1h \
--valid-until-duration=30m --scopes=https://www.googleapis.com/auth/devstorage.read_only --async
gcloud alpha compute tpus queued-resources describe "$TPU_NAME" \
--project="$PROJECT_ID" --zone="$ZONE" --format="value(state.state)"
# Only after the queue reports ACTIVE:
gcloud compute tpus tpu-vm describe "$TPU_NAME" \
--project="$PROJECT_ID" --zone="$ZONE" --format="value(state)"Expected:
The asynchronous request may report WAITING_FOR_RESOURCES or PROVISIONING before ACTIVE. Repeat describe and wait for ACTIVE and a READY VM before uploading or connecting. The request has a 30-minute queue window and a one-hour run cap. If it expires or fails, inspect its status and delete your queued request using the cleanup command below, even if no VM became active.
Run instructions: From your laptop: upload the qualification workspace and connect
# From the folder containing the downloaded jax-tpu-gcp.zip
gcloud compute tpus tpu-vm scp jax-tpu-gcp.zip "$TPU_NAME:~/jax-tpu-gcp.zip" --project="$PROJECT_ID" --zone="$ZONE"
gcloud compute tpus tpu-vm ssh "$TPU_NAME" --project="$PROJECT_ID" --zone="$ZONE"Expected:
Copy the ZIP from the folder containing the downloaded workspace, then SSH into the TPU VM and keep the session open for the following extraction and installation commands.
Run instructions: Inside the TPU VM: extract the workspace and install the pinned qualification environment
# Run inside the TPU VM after uploading jax-tpu-gcp.zip.
python3 -m zipfile -e ~/jax-tpu-gcp.zip ~/
cd ~/jax-tpu-gcp
python3 -m pip install --user 'uv==0.11.11'
~/.local/bin/uv venv --python 3.12.13 --managed-python .venv
~/.local/bin/uv pip install --python .venv/bin/python \
-r resources/tpu-gcp/requirements-qualification.txt \
--find-links https://storage.googleapis.com/jax-releases/libtpu_releases.html
~/.local/bin/uv pip freeze --python .venv/bin/python > tpu-environment.txt
.venv/bin/python --version
JAX_PLATFORMS=tpu .venv/bin/python -c "import jax; devices=jax.devices(); assert jax.default_backend() == 'tpu' and devices and all(d.platform == 'tpu' for d in devices); print(jax.__version__, jax.local_device_count(), devices)"Expected:
The base image Python extracts the ZIP and installs uv. uv creates the tested Python 3.12.13 environment and installs the pinned qualification packages. Keep tpu-environment.txt. Device discovery asserts the active backend is TPU; discovery alone is not a workload receipt.
Run reviewed TPU targets, retrieve receipts, and delete the queued resource
The lesson’s main.py remains a CPU reference; do not use it as evidence of physical TPU execution. For the reviewed first target run, upload and extract the ZIP workspace, install resources/tpu-gcp/requirements-qualification.txt, then run its launcher and precision target with the TPU backend explicitly required. The launcher receipt should show a started event with backend tpu, a completed run and checkpoint; the precision report should record platform: tpu and its one-device scope. These receipts apply only to the named targets and configurations. They do not qualify another lesson script, a multi-chip collective, a multi-host job, or production model quality.
Run instructions: Inside the TPU VM: run the reviewed launcher and precision targets
# Run inside the TPU VM from ~/jax-tpu-gcp after installing the pinned requirements
set -o pipefail
.venv/bin/python resources/tpu-gcp/launch.py --platform tpu --run-dir runs/tpu-first --steps 4 --timeout 180
.venv/bin/python resources/tpu-gcp/launch.py --platform tpu --run-dir runs/tpu-first --steps 8 --timeout 180 --resume
.venv/bin/python resources/tpu-gcp/precision_profile.py --platform tpu --run-dir runs/precision-tpu --steps 5 --traceExpected:
Inspect runs/tpu-first/events.jsonl and verify the started event names backend: tpu; the final summary should be completed at step 8, with the first attempt retained under history. Inspect runs/precision-tpu/report.json and verify platform is tpu, local_device_count is 1, and the five synchronized samples are present. Copy the evidence and tpu-environment.txt to your laptop before deleting the VM.
Run instructions: From your laptop: retrieve receipts and delete your queued resource
# Run from your laptop after the target commands finish
mkdir -p evidence/tpu-first
gcloud compute tpus tpu-vm scp --recurse "$TPU_NAME:~/jax-tpu-gcp/runs/tpu-first" evidence/tpu-first/ --project="$PROJECT_ID" --zone="$ZONE"
gcloud compute tpus tpu-vm scp --recurse "$TPU_NAME:~/jax-tpu-gcp/runs/precision-tpu" evidence/tpu-first/ --project="$PROJECT_ID" --zone="$ZONE"
gcloud compute tpus tpu-vm scp "$TPU_NAME:~/jax-tpu-gcp/tpu-environment.txt" evidence/tpu-first/ --project="$PROJECT_ID" --zone="$ZONE"
# Delete this request and its VM, including after a failed or expired attempt.
gcloud alpha compute tpus queued-resources delete "$TPU_NAME" \
--project="$PROJECT_ID" --zone="$ZONE" --force --quiet
gcloud alpha compute tpus queued-resources list --project="$PROJECT_ID" --zone="$ZONE" \
--filter="name~'(^|/)${TPU_NAME}$'" --format="value(name)"
gcloud compute tpus tpu-vm list --project="$PROJECT_ID" --zone="$ZONE" \
--filter="name~'(^|/)${TPU_NAME}$'" --format="value(name)"Expected:
Both target folders and the resolved environment are copied locally before cleanup. The forced queued-resource deletion removes this request and its VM. Both name-filtered lists should be empty; repeat the checks if deletion is still in progress. Other workloads in your project may remain active.
Keep CPU lessons separate from reviewed TPU targets
Continue the course using its recorded CPU references. The ZIP launcher and precision target provide bounded physical TPU practice with pinned dependencies and explicit backend receipts; use only the exact programs and configurations described by those receipts. Do not upload arbitrary lesson scripts or infer compatibility from a successful CPU run. Before another TPU workload, review its source, dependencies, backend assertion, topology, and input ownership, then collect target-specific evidence.
Diagnose the boundary that failed
If import fails, check the interpreter and installation. If explicit backend selection fails, check whether you are in the intended runtime and whether its TPU service is available. If device placement succeeds but the independent comparison fails, inspect shapes, dtype, precision and the arithmetic. Do not widen tolerance just to hide a large error.
An old notebook kernel may retain an initialized CPU backend or stale variables. Restart it, set runtime choices before the first device operation and run every cell. A CPU plot from the recorded course run remains a CPU plot when viewed on a machine that happens to have TPU access; only a fresh recorded execution supplies new evidence.
The same prediction checked two ways
Predict before looking: Should the JAX line overlap the independent NumPy points, and would overlap alone prove TPU execution?
Read the figure
The horizontal axis is the observation row index, starting at . The vertical axis is the scalar prediction. The two series use the same inputs and weights: one comes from the completed JAX array, the other from NumPy. They overlap in this recorded CPU example. The first value is ; the constant rise of follows from increasing every feature by while the weights sum to .
Connect it to the computation
The line is a check of arithmetic and ordering for these eight observations. Read the printed platform report to determine placement; a curve cannot identify the device that produced it. The changed-bias exercise translates this line downward, while row reversal changes the order. This recorded figure comes from the CPU path and carries no TPU timing or correctness claim.
Figure data and execution details
Open full-size figureDownload data and provenance
The notebook contains the data experiment, plotting code, and recorded figure output. Rerun it to test your changes.
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 Choose the requested platform
Add this block to main.py after the preceding block. Run the assembled file in a fresh process.
# Step 1 — Choose the requested platform
import os
import json
import platform
import numpy as np
import jax
import jax.numpy as jnpKeep the requested backend, observed output placement and independent arithmetic check together in the final report.
2 Place inputs and compare a completed prediction
Add this block to main.py after the preceding block. Run the assembled file in a fresh process.
# Step 2 — Place inputs and compare a completed prediction
expected = os.environ.get('COURSE_EXPECT_PLATFORM', 'cpu')
if expected not in {'cpu', 'tpu'}:
raise ValueError('COURSE_EXPECT_PLATFORM must be cpu or tpu')
# Selecting a requested backend must fail when it is unavailable.
devices = jax.devices(expected)
assert devices and all(d.platform == expected for d in devices)
x_host = np.arange(24, dtype=np.float32).reshape(8, 3) / 8
w_host = np.array([0.5, -0.25, 1.0], dtype=np.float32)
# Place `x` explicitly onto the target JAX device.
x = jax.device_put(x_host, devices[0])
# Place `w` explicitly onto the target JAX device.
w = jax.device_put(w_host, devices[0])
# Wrap with `jax.jit` (`predict`) so XLA traces and compiles the function.
predict = jax.jit(lambda a, b: a @ b + jnp.float32(0.125))
y = predict(x, w)
# Wait for device work to finish before timing it or comparing its result on the host.
y.block_until_ready()
reference = x_host @ w_host + np.float32(0.125)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(y), reference, rtol=1e-5, atol=1e-5)
assert {d.platform for d in y.devices()} == {expected}
# Transfer the device result to the host for an independent NumPy comparison.
report = dict(expected=expected, output_devices=[str(d) for d in y.devices()],
platform=next(iter(y.devices())).platform, jax=jax.__version__,
python=platform.python_version(), shape=list(y.shape), dtype=str(y.dtype),
max_absolute_error=float(np.max(np.abs(np.asarray(y)-reference))),
process_index=jax.process_index(), process_count=jax.process_count())
print(json.dumps(report, indent=2))
print('Completed prediction:', np.asarray(y).tolist())Keep the requested backend, observed output placement and independent arithmetic check together in the final report.
Explain the APIs in this step
array.reshape(new_shape): Reorganizes tensor axes without changing the total element count (array.size).jax.device_put(x, device=...): Places or transfers an array to the requested device or sharding; dispatch is asynchronous.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.
# Assert that the device output matches the NumPy reference and print the portability report:
assert report["max_absolute_error"] < 1e-6
print(json.dumps(report, indent=2))
print("Completed prediction:", np.asarray(y).tolist())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.
# Step 1 — Choose the requested platform
import os
import json
import platform
import numpy as np
import jax
import jax.numpy as jnp
# Step 2 — Place inputs and compare a completed prediction
expected = os.environ.get('COURSE_EXPECT_PLATFORM', 'cpu')
if expected not in {'cpu', 'tpu'}:
raise ValueError('COURSE_EXPECT_PLATFORM must be cpu or tpu')
# Selecting a requested backend must fail when it is unavailable.
devices = jax.devices(expected)
assert devices and all(d.platform == expected for d in devices)
x_host = np.arange(24, dtype=np.float32).reshape(8, 3) / 8
w_host = np.array([0.5, -0.25, 1.0], dtype=np.float32)
# Place `x` explicitly onto the target JAX device.
x = jax.device_put(x_host, devices[0])
# Place `w` explicitly onto the target JAX device.
w = jax.device_put(w_host, devices[0])
# Wrap with `jax.jit` (`predict`) so XLA traces and compiles the function.
predict = jax.jit(lambda a, b: a @ b + jnp.float32(0.125))
y = predict(x, w)
# Wait for device work to finish before timing it or comparing its result on the host.
y.block_until_ready()
reference = x_host @ w_host + np.float32(0.125)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(y), reference, rtol=1e-5, atol=1e-5)
assert {d.platform for d in y.devices()} == {expected}
# Transfer the device result to the host for an independent NumPy comparison.
report = dict(expected=expected, output_devices=[str(d) for d in y.devices()],
platform=next(iter(y.devices())).platform, jax=jax.__version__,
python=platform.python_version(), shape=list(y.shape), dtype=str(y.dtype),
max_absolute_error=float(np.max(np.abs(np.asarray(y)-reference))),
process_index=jax.process_index(), process_count=jax.process_count())
print(json.dumps(report, indent=2))
print('Completed prediction:', np.asarray(y).tolist())
# Assert that the device output matches the NumPy reference and print the portability report:
assert report["max_absolute_error"] < 1e-6
print(json.dumps(report, indent=2))
print("Completed prediction:", np.asarray(y).tolist())Run the example
python3 phases/00-welcome/03-move-your-experiment-to-a-tpu/code/main.pyAbout 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
# Step 1 — Choose the requested platform
import os
import json
import platform
import numpy as np
import jax
import jax.numpy as jnp
# Step 2 — Place inputs and compare a completed prediction
expected = os.environ.get('COURSE_EXPECT_PLATFORM', 'cpu')
if expected not in {'cpu', 'tpu'}:
raise ValueError('COURSE_EXPECT_PLATFORM must be cpu or tpu')
# Selecting a requested backend must fail when it is unavailable.
devices = jax.devices(expected)
assert devices and all(d.platform == expected for d in devices)
x_host = np.arange(24, dtype=np.float32).reshape(8, 3) / 8
w_host = np.array([0.5, -0.25, 1.0], dtype=np.float32)
# Place `x` explicitly onto the target JAX device.
x = jax.device_put(x_host, devices[0])
# Place `w` explicitly onto the target JAX device.
w = jax.device_put(w_host, devices[0])
# Wrap with `jax.jit` (`predict`) so XLA traces and compiles the function.
predict = jax.jit(lambda a, b: a @ b + jnp.float32(0.125))
y = predict(x, w)
# Wait for device work to finish before timing it or comparing its result on the host.
y.block_until_ready()
reference = x_host @ w_host + np.float32(0.125)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(y), reference, rtol=1e-5, atol=1e-5)
assert {d.platform for d in y.devices()} == {expected}
# Transfer the device result to the host for an independent NumPy comparison.
report = dict(expected=expected, output_devices=[str(d) for d in y.devices()],
platform=next(iter(y.devices())).platform, jax=jax.__version__,
python=platform.python_version(), shape=list(y.shape), dtype=str(y.dtype),
max_absolute_error=float(np.max(np.abs(np.asarray(y)-reference))),
process_index=jax.process_index(), process_count=jax.process_count())
print(json.dumps(report, indent=2))
print('Completed prediction:', np.asarray(y).tolist())Expected result: The recorded CPU reference predicts [0.34375, 0.8125, 1.28125, 1.75, 2.21875, 2.6875, 3.15625, 3.625]. Its report names the actual output device. There is no recorded TPU result.
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.
{
"expected": "cpu",
"output_devices": [
"TFRT_CPU_0"
],
"platform": "cpu",
"jax": "0.9.2",
"python": "3.14.3",
"shape": [
8
],
"dtype": "float32",
"max_absolute_error": 0.0,
"process_index": 0,
"process_count": 1
}
Completed prediction: [0.34375, 0.8125, 1.28125, 1.75, 2.21875, 2.6875, 3.15625, 3.625]
{
"expected": "cpu",
"output_devices": [
"TFRT_CPU_0"
],
"platform": "cpu",
"jax": "0.9.2",
"python": "3.14.3",
"shape": [
8
],
"dtype": "float32",
"max_absolute_error": 0.0,
"process_index": 0,
"process_count": 1
}
Completed prediction: [0.34375, 0.8125, 1.28125, 1.75, 2.21875, 2.6875, 3.15625, 3.625]
{
"expected": "cpu",
"output_devices": [
"TFRT_CPU_0"
],
"platform": "cpu",
"jax": "0.9.2",
"python": "3.14.3",
"shape": [
8
],
"dtype": "float32",
"max_absolute_error": 0.0,
"process_index": 0,
"process_count": 1
}
Completed prediction: [0.34375, 0.8125, 1.28125, 1.75, 2.21875, 2.6875, 3.15625, 3.625]
Mismatched platform report rejected.
Changed bias, first prediction: -0.03125
Row reversal preserved values and requested placement.
PASS: welcome-03
Download execution record · The notebook includes executed cells and figure outputs. Compare your run with these values; timings depend on the machine.
Reject a mismatched report
# Experiment — Reject a mismatched report
def verify_report(record, required):
if record['platform'] != required or record['expected'] != required:
raise ValueError('requested and observed platform disagree')
if record['shape'] != [8] or record['dtype'] != 'float32':
raise ValueError('prediction contract changed')
return True
assert verify_report(report, expected)
wrong = {**report, 'platform': 'cpu' if expected == 'tpu' else 'tpu'}
try:
verify_report(wrong, expected)
except ValueError:
print('Mismatched platform report rejected.')
else:
raise AssertionError('a mismatched report was accepted')Expected: The deliberately inconsistent report raises ValueError; the actual report passes.
Numerical agreement and requested placement are separate requirements. This experiment tests report validation; the main program performs the actual backend selection and array-placement check.
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.
Predict the effect of replacing the bias by minus one quarter. Run a second compiled prediction on the selected device. Compare against the original prediction plus the expected constant shift and an independent NumPy calculation.
Show a writing guide and starter scaffold
How to write this exercise code
Learner-only inputs, outputs, checks and hints
Inputs
- The original inputs, weights, prediction function, and selected device.
- A compiled prediction path and changed bias.
Outputs
- The changed device result, an independent NumPy comparison, and device metadata.
Invariants to check
- The observed result comes from the selected device.
- Check against both a shift-based prediction and an independent calculation.
- Unavailable requested hardware cannot silently pass as CPU evidence.
Hints
- Keep the original model inputs fixed.
- Wait for device work before transferring a result for comparison.
import jax
import jax.numpy as jnp
import numpy as np
def check_changed_bias(x, weight, predict, device):
# TODO: run the changed compiled prediction on the selected device
changed = ...
# TODO: compare with an independent host calculation
raise NotImplementedErrorReveal the reference solution
# Exercise solution: Predict the effect of replacing the bias by minus one quarter.
# Wrap with `jax.jit` (`changed_predict`) so XLA traces and compiles the function.
changed_predict = jax.jit(lambda a, b: a @ b - jnp.float32(0.25))
changed = changed_predict(x, w)
# Wait for device work to finish before timing it or comparing its result on the host.
changed.block_until_ready()
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(changed), reference - 0.375, atol=1e-5, rtol=1e-5)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(changed), x_host @ w_host - 0.25, atol=1e-5, rtol=1e-5)
assert {d.platform for d in changed.devices()} == {expected}
print('Changed bias, first prediction:', float(changed[0]))Test row order without changing the model
Transfer
Reverse the observation rows and predict the result order before execution. Verify the first and last values, the complete vector and device placement. Explain why this is a stronger check than only comparing the vector sum.
Show a writing guide and starter scaffold
How to write this practice solution
Learner-only inputs, outputs, checks and hints
Inputs
- Observation rows, model weights, and the selected device.
- A reversed copy of the input rows.
Outputs
- The reversed prediction vector and a NumPy reference.
Invariants to check
- Output row order follows input row order.
- Compare the full vector, not only its sum.
- The result is placed on the selected device.
Hints
- Reverse a host copy so the original stays unchanged.
- Check endpoints and every value after execution completes.
import jax
import numpy as np
def check_reversed_rows(x_host, weights, device, predict):
# TODO: place reversed rows, predict, and compare the full result
raise NotImplementedErrorShow a hint
A row-wise affine prediction commutes with row permutation. A sum would conceal an ordering bug.
Reveal reference solution and reasoning
# Test row order without changing the model (Transfer)
reversed_x = jax.device_put(x_host[::-1].copy(), devices[0])
reversed_y = predict(reversed_x, w)
# Wait for device work to finish before timing it or comparing its result on the host.
reversed_y.block_until_ready()
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(reversed_y), reference[::-1], atol=1e-5, rtol=1e-5)
assert {d.platform for d in reversed_y.devices()} == {expected}
assert np.isclose(float(reversed_y[0]), 3.625)
assert np.isclose(float(reversed_y[-1]), 0.34375)
print('Row reversal preserved values and requested placement.')Reversing rows should reverse predictions because each observation is evaluated independently. Matching a sum would not reveal a misplaced or reordered prediction.
Diagnose the result
Read the first failing boundary: interpreter/import, explicit backend discovery, output placement or numerical comparison. Keep the original error and requested platform in the report. Restart a stale notebook kernel before trying the target again.
Carry forward
- Keep the recorded CPU baseline separate from physical TPU evidence. For first TPU practice, use the pinned downloadable launcher and precision targets; review other programs before a target run.
- Check an actual completed array, not only the installed package.
- Request TPU explicitly so its absence produces an error, and delete Cloud TPU VMs after copying your evidence.
Keep your evidence
Keep the lesson’s CPU output separate from physical TPU evidence. For TPU practice, retain the launcher events/checkpoint, precision report/trace, and resolved environment from the downloadable qualification workspace. The lesson’s recorded main.py output is CPU-only evidence.
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.
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
- JAX installation and TPU support ↗
- Manage Cloud TPU VMs with gcloud ↗
- Course guide: From a laptop experiment to a TPU training run ↗
- Course guide: TPU generations, precision and profiling ↗
- JAX device placement ↗
Phase 00 Integration Project
Bring this phase together: Check the CPU environment and a saved matrix arithmetic identity
Checkpoint to prove: Explain which device ran your code and reproduce the result in a fresh environment.
Work in projects/welcome-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/welcome-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.