Phase 09 / Distributed training
Arrays, meshes, and sharding
Revision v0.18.1 · Updated since your last visit. Review the changes; your progress is saved.
Lesson · about 80 minutes · Logical CPU devices
Partition an array over a device mesh and inspect its local shards.
Phase study guide: preparation, milestones and project evidence
Before you start
Before this: Data & checkpoint recovery; Performance diagnosis
Hardware: 4 logical CPU devices on one host
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/09-distributed/01-arrays-meshes-and-sharding/code/main.py.\.venv\Scripts\python.exe phases/09-distributed/01-arrays-meshes-and-sharding/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 distributed-01.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: Four row shards ; four column shards . Column sums .
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
- Translate a PartitionSpec into local shard shapes.
- Inspect shard indices and verify values.
- Explain communication needs of a global reduction.
- Repair uneven batch statistics with padding and masks.
The problem
We can split examples across devices, split features across devices, or keep a full copy on each one. Let’s make those choices visible on four logical CPU devices. You’ll inspect the pieces and see why some reductions must combine results. No cluster is needed, and these observations do not measure accelerator performance.
The idea
Sharding places one global array across a named mesh. Global shape, local shard shape and replication describe different views of the same computation. Make ownership explicit before reasoning about communication.
- Global view
rows 0…7The program sees an eight-row array.
- Partition rows
CPU 0: 0–1 | CPU 1: 2–3 | CPU 2: 4–5 | CPU 3: 6–7A four-device mesh splits the chosen axis.
- Global operation
reduce across all rowsLocal storage and global array semantics are different views.
Conceptual layout for eight rows and four logical CPU devices. All devices share one host; this explains partitioning rather than simulating accelerator speed or network behavior.
Conceptual illustration. Verify the computation in the lesson’s Python experiments.
Compare the global array with each local piece
The recorded global array has shape , not four-by-four: it contains rows 0 through 7 and four columns. On four logical CPU devices, row sharding assigns two complete rows to each device, while column sharding assigns one complete column. Both layouts preserve the same 32 values; they change which values are local.
For example, device 0 owns rows 0 and 1 in the row layout, but column 0 across all eight rows in the column layout. Replication would instead give every device all 32 values. Keep those three ownership patterns distinct when predicting the shape of local work.
The ownership heatmaps show device IDs, not matrix values or speed. Read one cell at a time: the upper panel's first two rows are device 0, while every cell in its first column belongs to device 0 in the lower panel. The execution confirms local shapes and .
Read the mapping from left to right
Start with the executed global array and a one-dimensional mesh of four devices named data. P("data", None) divides the first axis into four two-row slices and leaves all four columns together, so each local shape is . P(None, "data") leaves all eight rows together and divides the second axis into four one-column slices, so each local shape is .
None means that this array axis is not partitioned by the spec; P() instead replicates the full array. If the row count changed from 8 to 10 while the mesh still had four devices, the even split would no longer hold. The lesson's divisibility check makes that changed condition visible instead of silently describing equal shards.
Array (8,4) → P(data,None) → 4 pieces (2,4)
Array (8,4) → P(None,data) → 4 pieces (8,1)
Array (8,4) → P() → 4 full copies (8,4)Inspect indices rather than guessing ordering
addressable_shards lists pieces this process can access. Each piece carries a device, global index and data. For row partitioning the indices select successive two-row slices; for column partitioning they select individual columns. Compare host[shard.index] with shard.data. This catches a mismatch between the intended assignment and the actual data.
All four devices are addressable here because we have one process. In a multi-process program, the global device set may include devices another process owns. np.asarray on a global array that is not fully addressable is not a general multi-host gathering recipe. This lesson deliberately exercises only the single-process case.
A reduction has a communication requirement
The array is arange(32).reshape(8,4), so row 0 is and row 1 is . Under row sharding, device 0 can sum those rows locally to ; device 1 contributes , with devices 2 and 3 contributing the remaining partial sums. Combining them gives the executed global column sums .
The first result, 112, is the sum of the first column's values . No row shard contains all eight terms, so a replicated result requires combining partial sums across the mesh. JAX executes the placement-aware sum; the values establish the needed global result, while this CPU fixture does not measure how a particular accelerator lowers or times that communication.
partial sums device 0: [4,6,8,10]
partial sums device 1: [20,22,24,26]
partial sums device 2: [36,38,40,42]
partial sums device 3: [52,54,56,58]
combine → [112,120,128,136]Choose placement for the operation you will perform and run it on a TPU mesh
Elementwise functions naturally preserve local independence. Reductions across a partitioned dimension and matrix multiplications can require communication. Choosing columns instead of rows changes which values are locally available; it does not alter the mathematical answer.
device_put from a host NumPy array places that array according to the requested sharding. Moving an already placed JAX array to a different sharding is a resharding operation. Host reconstruction is useful for our tiny verification case but should not be inserted into a real training loop. The experiments compare two layouts for correctness, not speed. Save global shapes, specs and shard tables as the evidence needed to reason about a future workload.
To run this exact mesh and NamedSharding contract on a real 4-chip TPU VM (for example v5litepod-4 or v6e-4, provisioned via lesson-welcome-03.html and tpu-gcp.html), remove the jax_platforms=cpu override so jax.devices() discovers the four physical TPU chips. On a multi-host TPU slice (v5litepod-8 or larger), call jax.distributed.initialize() at process startup and launch across all workers with --worker=all.
Run instructions: Run the 4-device sharding check on a 4-chip TPU VM (single host)
gcloud compute tpus tpu-vm scp exercises/distributed-01.py $TPU_NAME:~/jax-tpu-lab/ --zone=$ZONE
gcloud compute tpus tpu-vm ssh $TPU_NAME --zone=$ZONE \
--command="sed 's/jax.config.update(\"jax_platforms\", \"cpu\")/# use default TPU backend/; s/jax.devices(\"cpu\")/jax.devices()[:4]/' ~/jax-tpu-lab/distributed-01.py > ~/jax-tpu-lab/distributed-01-tpu.py && JAX_PLATFORMS=tpu ~/jax-tpu-lab/.venv/bin/python ~/jax-tpu-lab/distributed-01-tpu.py"Expected:
Places the (8, 4) array across four TPU chips, verifies row-shard (2, 4) and column-shard (8, 1) local shapes, and checks the replicated column sum [112, 120, 128, 136].
Row sharding and column sharding place the same array differently
Predict before looking: Which dimension is split across devices in each panel?
Read the figure
Both heatmaps have array row on the vertical axis and array column on the horizontal axis; each cell is colored and labeled by its logical CPU device ID. The upper, row-partition panel has two-row bands: device 0 owns rows 0 and 1. The lower, column-partition panel has one-column bands: device 0 owns column 0 across all eight rows. The displayed IDs are categorical ownership labels, not array values.
Connect it to the computation
The actual local shapes are in the upper panel and below, so each device owns eight values in either layout. For the row-0 sum, , row sharding makes two terms local to each device and the global reduction combines four partials; column sharding leaves the whole first column local to one device. The next decision is therefore to match the partitioned axis to the operation, then check whether the chosen output must be combined or replicated.
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 Start a fresh CPU runtime
Create main.py in your course workspace. Paste this block first. If using a notebook, restart its kernel before running any cell.
# Step 1 — Start a fresh CPU runtime
import jax
jax.config.update("jax_platforms", "cpu")
# Select the CPU backend and virtual device count before initializing JAX.
jax.config.update("jax_num_cpu_devices", 4)
import jax.numpy as jnp
import numpy as np
from jax.sharding import Mesh, NamedSharding, PartitionSpec as P
devices = jax.devices("cpu")
if len(devices) != 4:
raise RuntimeError("Expected four CPU devices. Restart the notebook kernel, then Run all; do not run an earlier JAX cell first.")
mesh = Mesh(np.array(devices), ("data",))
rows = NamedSharding(mesh, P("data", None))
replicated = NamedSharding(mesh, P())Configuration happens before devices() or array creation initializes a backend. This file explicitly chooses CPU even on a GPU machine.
Explain the APIs in this step
Mesh(devices, axis_names): Names device axes in a mesh used to describe array placement.NamedSharding(mesh, spec): Combines a device mesh and partition specification to describe array placement.
2 Place and compute
Append this block in the same file. Predict the shapes and values before running.
# Step 2 — Place and compute: Mesh axis names describe placement.
host = np.arange(32, dtype=np.float32).reshape(8,4)
# Place `x` explicitly onto the target JAX device.
x = jax.device_put(host, rows)
columns = NamedSharding(mesh, P(None, "data"))
# Place `x_columns` explicitly onto the target JAX device.
x_columns = jax.device_put(host, columns)
# Wrap with `jax.jit` (`sum_rows`) so XLA traces and compiles the function.
sum_rows = jax.jit(lambda a:a.sum(axis=0), in_shardings=rows, out_shardings=replicated)(x)
# Wait for device work to finish before timing it or comparing its result on the host.
sum_rows.block_until_ready()Mesh axis names describe placement. They are separate from the numerical array dimensions.
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.NamedSharding(mesh, spec): Combines a device mesh and partition specification to describe array placement.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.
3 Inspect and verify
Append the checks, save the file and run python main.py with the setup lesson environment.
# Step 3 — Inspect and verify
np.testing.assert_array_equal(np.asarray(sum_rows), np.array([112,120,128,136],dtype=np.float32))
for shard in x.addressable_shards:
# Check tensor shape invariant: `shard.data.shape == (2,4)`
assert shard.data.shape == (2,4)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(np.asarray(shard.data), host[shard.index])
for shard in x_columns.addressable_shards:
# Check tensor shape invariant: `shard.data.shape == (8,1)`
assert shard.data.shape == (8,1)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(np.asarray(shard.data), host[shard.index])
print("Row-shard shapes:", [s.data.shape for s in x.addressable_shards])
print("Column-shard shapes:", [s.data.shape for s in x_columns.addressable_shards])
print("Column sums:", np.asarray(sum_rows))Assertions compare against host calculations or hand-derived values; printing a sharding object alone is separate from correctness.
View the cumulative build in main.py
These blocks append in order to your separate practice file.
# Step 1 — Start a fresh CPU runtime
import jax
jax.config.update("jax_platforms", "cpu")
# Select the CPU backend and virtual device count before initializing JAX.
jax.config.update("jax_num_cpu_devices", 4)
import jax.numpy as jnp
import numpy as np
from jax.sharding import Mesh, NamedSharding, PartitionSpec as P
devices = jax.devices("cpu")
if len(devices) != 4:
raise RuntimeError("Expected four CPU devices. Restart the notebook kernel, then Run all; do not run an earlier JAX cell first.")
mesh = Mesh(np.array(devices), ("data",))
rows = NamedSharding(mesh, P("data", None))
replicated = NamedSharding(mesh, P())
# Step 2 — Place and compute: Mesh axis names describe placement.
host = np.arange(32, dtype=np.float32).reshape(8,4)
# Place `x` explicitly onto the target JAX device.
x = jax.device_put(host, rows)
columns = NamedSharding(mesh, P(None, "data"))
# Place `x_columns` explicitly onto the target JAX device.
x_columns = jax.device_put(host, columns)
# Wrap with `jax.jit` (`sum_rows`) so XLA traces and compiles the function.
sum_rows = jax.jit(lambda a:a.sum(axis=0), in_shardings=rows, out_shardings=replicated)(x)
# Wait for device work to finish before timing it or comparing its result on the host.
sum_rows.block_until_ready()
# Step 3 — Inspect and verify
np.testing.assert_array_equal(np.asarray(sum_rows), np.array([112,120,128,136],dtype=np.float32))
for shard in x.addressable_shards:
# Check tensor shape invariant: `shard.data.shape == (2,4)`
assert shard.data.shape == (2,4)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(np.asarray(shard.data), host[shard.index])
for shard in x_columns.addressable_shards:
# Check tensor shape invariant: `shard.data.shape == (8,1)`
assert shard.data.shape == (8,1)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(np.asarray(shard.data), host[shard.index])
print("Row-shard shapes:", [s.data.shape for s in x.addressable_shards])
print("Column-shard shapes:", [s.data.shape for s in x_columns.addressable_shards])
print("Column sums:", np.asarray(sum_rows))Run the example
python3 phases/09-distributed/01-arrays-meshes-and-sharding/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 — Start a fresh CPU runtime
import jax
jax.config.update("jax_platforms", "cpu")
# Select the CPU backend and virtual device count before initializing JAX.
jax.config.update("jax_num_cpu_devices", 4)
import jax.numpy as jnp
import numpy as np
from jax.sharding import Mesh, NamedSharding, PartitionSpec as P
devices = jax.devices("cpu")
if len(devices) != 4:
raise RuntimeError("Expected four CPU devices. Restart the notebook kernel, then Run all; do not run an earlier JAX cell first.")
mesh = Mesh(np.array(devices), ("data",))
rows = NamedSharding(mesh, P("data", None))
replicated = NamedSharding(mesh, P())
# Step 2 — Place and compute: Mesh axis names describe placement.
host = np.arange(32, dtype=np.float32).reshape(8,4)
# Place `x` explicitly onto the target JAX device.
x = jax.device_put(host, rows)
columns = NamedSharding(mesh, P(None, "data"))
# Place `x_columns` explicitly onto the target JAX device.
x_columns = jax.device_put(host, columns)
# Wrap with `jax.jit` (`sum_rows`) so XLA traces and compiles the function.
sum_rows = jax.jit(lambda a:a.sum(axis=0), in_shardings=rows, out_shardings=replicated)(x)
# Wait for device work to finish before timing it or comparing its result on the host.
sum_rows.block_until_ready()
# Step 3 — Inspect and verify
np.testing.assert_array_equal(np.asarray(sum_rows), np.array([112,120,128,136],dtype=np.float32))
for shard in x.addressable_shards:
# Check tensor shape invariant: `shard.data.shape == (2,4)`
assert shard.data.shape == (2,4)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(np.asarray(shard.data), host[shard.index])
for shard in x_columns.addressable_shards:
# Check tensor shape invariant: `shard.data.shape == (8,1)`
assert shard.data.shape == (8,1)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(np.asarray(shard.data), host[shard.index])
print("Row-shard shapes:", [s.data.shape for s in x.addressable_shards])
print("Column-shard shapes:", [s.data.shape for s in x_columns.addressable_shards])
print("Column sums:", np.asarray(sum_rows))Expected result: Four row shards ; four column shards . Column sums .
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.
Row-shard shapes: [(2, 4), (2, 4), (2, 4), (2, 4)]
Column-shard shapes: [(8, 1), (8, 1), (8, 1), (8, 1)]
Column sums: [112. 120. 128. 136.]
Row-shard shapes: [(2, 4), (2, 4), (2, 4), (2, 4)]
Column-shard shapes: [(8, 1), (8, 1), (8, 1), (8, 1)]
Column sums: [112. 120. 128. 136.]
Resharded values preserved
Row sums: [ 6. 22. 38. 54. 70. 86. 102. 118.]
Expected indivisible batch
Expected indivisible batch
PASS: distributed-01
Download execution record · The notebook includes executed cells and figure outputs. Compare your run with these values; timings depend on the machine.
Reshard the same global values
# Experiment — Reshard the same global values
moved = jax.device_put(x, columns)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(np.asarray(moved), host)
# Check tensor shape invariant: `all(s.data.shape == (8,1) for s in moved.addressable_shards)`
assert all(s.data.shape == (8,1) for s in moved.addressable_shards)
print("Resharded values preserved")Expected: Global array unchanged; local shards now .
Resharding changes ownership of values, not their mathematical identity.
Reduce a dimension that is not partitioned
# Experiment — Reduce a dimension that is not partitioned
vector_rows = NamedSharding(mesh, P("data"))
# Wrap with `jax.jit` (`per_row`) so XLA traces and compiles the function.
per_row = jax.jit(lambda a:a.sum(axis=1), in_shardings=rows, out_shardings=vector_rows)(x)
np.testing.assert_array_equal(np.asarray(per_row), np.arange(6,119,16,dtype=np.float32))
# Check tensor shape invariant: `all(s.data.shape == (2,) for s in per_row.addressable_shards)`
assert all(s.data.shape == (2,) for s in per_row.addressable_shards)
print("Row sums:", np.asarray(per_row))Expected: Row sums ; local shapes .
All four columns for a row are already local in row partitioning; this reduction does not mathematically require combining data-axis pieces.
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.
Compare row and column layouts of a array, and repair a ten-row mean with padding plus an explicit mask.
Show a writing guide and starter scaffold
How to write this exercise code
Learner-only inputs, outputs, checks and hints
Inputs
- Four logical CPU devices; row and column NamedSharding objects.
- A (12, 8) matrix and a separate ten-observation, four-feature batch.
Outputs
- Per-device local shapes and values for both (12, 8) layouts.
- A masked ten-row column mean plus an unmasked padded mean for diagnosis.
Invariants to check
- Both placements preserve the same global matrix and each shard matches its indexed host slice.
- Padded rows contribute to neither the masked numerator nor the valid-row denominator.
- The masked mean agrees with the original ten-row host mean.
Hints
- Read each PartitionSpec against the axis it partitions.
- Use shard.index for local-value comparisons; mask padded rows before summing and divide by valid rows.
import numpy as np
import jax
import jax.numpy as jnp
def verify_layouts_and_masked_mean(host, uneven_batch, row_sharding, column_sharding):
# TODO: compare both placements and their indexed shard values.
# Then pad the ten-row batch, place a validity mask, and compare masked/unmasked means.
raise NotImplementedError("Verify both shard layouts and mask-preserved batch statistics")Reveal the reference solution
# Exercise solution
other = np.arange(96,dtype=np.float32).reshape(12,8)
for spec, expected in [(rows,(3,8)),(columns,(12,2))]:
# Place `placed` explicitly onto the target JAX device.
placed = jax.device_put(other,spec)
for shard in placed.addressable_shards:
# Check tensor shape invariant: `shard.data.shape == expected`
assert shard.data.shape == expected
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(np.asarray(shard.data),other[shard.index])
original = np.arange(40,dtype=np.float32).reshape(10,4)
try:
jax.device_put(original, rows)
except ValueError:
print("Expected indivisible batch")
else:
raise AssertionError("Expected divisibility failure")
# Place `padded` explicitly onto the target JAX device.
padded = jax.device_put(np.pad(original,((0,2),(0,0))),rows)
mask_sharding = NamedSharding(mesh,P("data"))
mask = jax.device_put(np.array([1]*10+[0]*2,dtype=np.float32),mask_sharding)
# Wrap with `jax.jit` (`masked_mean`) so XLA traces and compiles the function.
masked_mean = jax.jit(lambda a,m:(a*m[:,None]).sum(axis=0)/m.sum(),in_shardings=(rows,mask_sharding),out_shardings=replicated)(padded,mask)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(masked_mean),original.mean(axis=0),rtol=1e-6)
assert not np.allclose(np.asarray(padded.mean(axis=0)), original.mean(axis=0))Change both dimensions
Challenge
Use a array. Verify row and column layouts and predict every local shape.
Show a writing guide and starter scaffold
How to write this practice solution
Learner-only inputs, outputs, checks and hints
Inputs
- A (12,8) host matrix and four logical CPU devices.
- Row and column shardings from the lesson.
Outputs
- Exact per-device shapes for both placements.
Invariants to check
- Every shard contains the indexed global slice.
- The global matrix is unchanged by placement.
Hints
- Divide the partitioned dimension by four.
- Check shard.index as well as shard.data.shape.
import numpy as np
import jax
def verify_changed_layout(host, row_sharding, column_sharding):
# TODO: place the matrix under both shardings and check each local slice.
raise NotImplementedError("Verify both local shapes and indexed values")Show a hint
Four devices divide twelve rows or eight columns.
Reveal reference solution and reasoning
# Change both dimensions (Challenge)
other = np.arange(96,dtype=np.float32).reshape(12,8)
for spec, expected in [(rows,(3,8)),(columns,(12,2))]:
# Place `placed` explicitly onto the target JAX device.
placed = jax.device_put(other,spec)
for shard in placed.addressable_shards:
# Check tensor shape invariant: `shard.data.shape == expected`
assert shard.data.shape == expected
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(np.asarray(shard.data),other[shard.index])The row layout owns three complete feature vectors per device; the column layout owns two features for every observation.
Pad an uneven batch without corrupting its mean
Challenge
Ten observations cannot be row-partitioned over four devices. Pad to twelve and use a mask to recover the true column mean. Demonstrate the wrong unmasked mean.
Show a writing guide and starter scaffold
How to write this practice solution
Learner-only inputs, outputs, checks and hints
Inputs
- A ten-row host batch with four features and a four-device row sharding.
Outputs
- Masked column mean with shape (4,).
- Unmasked padded mean as a diagnostic.
Invariants to check
- Padding contributes zero to both the numerator and valid-count denominator.
- The masked result matches the original ten-row host mean.
Hints
- Pad only the row axis to a device-divisible length.
- Mask invalid rows before summing and divide by valid rows.
import numpy as np
import jax
import jax.numpy as jnp
def masked_column_mean(batch, row_sharding):
# TODO: pad rows, place the values and mask, then compute both means.
# Return device results as host arrays for comparison.
raise NotImplementedError("Implement masked statistics for the ten valid rows")Show a hint
The divisor is the sum of the mask, not padded length.
Reveal reference solution and reasoning
# Pad an uneven batch without corrupting its mean (Challenge): Padding repairs the shape but adds artificial rows.
original = np.arange(40,dtype=np.float32).reshape(10,4)
try:
jax.device_put(original, rows)
except ValueError:
print("Expected indivisible batch")
else:
raise AssertionError("Expected divisibility failure")
# Place `padded` explicitly onto the target JAX device.
padded = jax.device_put(np.pad(original,((0,2),(0,0))),rows)
mask_sharding = NamedSharding(mesh,P("data"))
mask = jax.device_put(np.array([1]*10+[0]*2,dtype=np.float32),mask_sharding)
# Wrap with `jax.jit` (`masked_mean`) so XLA traces and compiles the function.
masked_mean = jax.jit(lambda a,m:(a*m[:,None]).sum(axis=0)/m.sum(),in_shardings=(rows,mask_sharding),out_shardings=replicated)(padded,mask)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(np.asarray(masked_mean),original.mean(axis=0),rtol=1e-6)
assert not np.allclose(np.asarray(padded.mean(axis=0)), original.mean(axis=0))Padding repairs the shape but adds artificial rows. The mask keeps them out of both numerator and denominator. Checking only divisibility would miss this statistical error.
Diagnose the result
Padding repairs the shape but adds artificial rows. The mask keeps them out of both numerator and denominator. Checking only divisibility would miss this statistical error.
Carry forward
- Read the mapping from left to right
- Inspect indices rather than guessing ordering
- A reduction has a communication requirement
- Choose placement for the operation you will perform
Keep your evidence
Padding repairs the shape but adds artificial rows. The mask keeps them out of both numerator and denominator. Checking only divisibility would miss this statistical error.
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 reference configures logical CPU devices to practice placement and sharding. Logical devices do not measure TPU performance. Use the execution matrix to review device selection and data ownership before adapting a separate target program. Execution matrix and target qualification.