We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← JAX Foundations step 9 of 25
Vectorize with vmap
Use vmap to turn a function for one pair of vectors into a function for a batch of pairs. Each row of A is paired with the same row of B.
Your task
Implement batched_dot(A, B) using torch.vmap or jax.vmap, without an
explicit Python loop. Both inputs are floating-point matrices of shape
(batch, n) with the same dtype and device. Return shape (batch,),
preserving dtype and device and leaving the inputs unchanged.
output[i] = sum(A[i, j] * B[i, j] over all j)
For A=[[1, 2], [3, 4]] and B=[[5, 6], [7, 8]], return [17, 53]:
the first pair gives 1*5 + 2*6, and the second gives 3*7 + 4*8.
Do not compute all pairs of rows or sum the batch into one scalar.
Define an inner function that takes two vectors of shape (n,) and returns
a scalar dot product. Then call torch.vmap(dot)(A, B) or
jax.vmap(dot)(A, B). The default mapping uses axis 0 of both inputs and
places the mapped results on output axis 0. A batch of one still returns (1,).
Follow the shapes
For two (8, 3) inputs, the inner function sees two (3,) vectors.
Each scalar result contributes one entry to the (8,) output. vmap transforms
the tensor operations to handle the batch; it does not require you to call the
Python function separately for every row.
The diagram starts with both inputs mapped over axis 0. [] denotes a scalar
shape, not an empty array. Its controls explore axis placement; they do not
check whether the inner dot product accepts every selected shape.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Write a function for one example, then apply it to a batch
A vectorizing transformation applies a function across chosen input axes. Suppose
f(a, b) computes the dot product of two vectors. Mapping both inputs over axis
zero pairs row 0 with row 0, row 1 with row 1, and so on. It returns one scalar
per pair, not a matrix of all possible row pairs.
An input that is not mapped is shared across the batch. To compute one gradient per training example, share the model weights and map over the inputs and labels. Keep the resulting example axis if the task asks for per-example gradients.
For scalar weight w = 1 and loss (w*x - y)^2, the gradient is
2 * (w*x - y) * x. With examples (x, y) = (2, 0) and (3, 1), the gradients
are 8 and 12. Their mean is 10, but [8, 12] is the per-example result.
Specify the mapped axes
JAX calls the input-axis argument in_axes; PyTorch's vmap calls it in_dims.
These arguments distinguish batched inputs from shared inputs. See
JAX's vectorization guide
for examples.
Vectorization does not promise execution on multiple devices. It also does not make arbitrary mutation or Python side effects work independently for each example. Data-dependent output sizes can be a problem because the results need to fit into a batch. Keep output shapes consistent and follow the transformation's supported operations.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
JAX reference solution
Sign in to track your attempts toward unlocking the reference solution.