TensorTonic
Problems
Batched Attention with vmap
JAX
Medium

Compute scaled dot-product attention for a batch of examples with several heads.

For one head of one example, the queries QQQ have shape (Tq, D), the keys KKK have shape (Tk, D) and the values VVV have shape (Tk, Dv), and the output is

Attention⁡(Q,K,V)=softmax⁡(QK⊤D)V,\operatorname{Attention}(Q, K, V) = \operatorname{softmax}\left(\frac{Q K^{\top}}{\sqrt{D}}\right) V ,Attention(Q,K,V)=softmax(D​QK⊤​)V,

where the softmax runs over the Tk keys separately for each query row. The input q has shape (B, H, Tq, D), k has shape (B, H, Tk, D) and v has shape (B, H, Tk, Dv), where B is the batch size, H is the number of heads, Tq is the number of queries, Tk is the number of keys, D is the shared query and key width and Dv is the value width. Example b and head h use the blocks q[b, h], k[b, h] and v[b, h].

Return a JAX array of shape (B, H, Tq, Dv) with the dtype of q.

Examples

Input: q = [[[[1,0],[0,1]],[[1,1],[0,0]]]], k = [[[[1,0],[0,1]],[[1,0],[0,1]]]], v = [[[[1,2],[3,4]],[[0,1],[1,0]]]]

Output: [[[[1.660477,2.660477],[2.339523,3.339523]],[[0.5,0.5],[0.5,0.5]]]]

Explanation: In head 0, the query (1, 0) scores 0.7071 against the first key and 0 against the second, so its weights are 0.6698 and 0.3302 and its output is 0.6698 times (1, 2) plus 0.3302 times (3, 4). In head 1 every query scores the two keys equally, so every output is the plain average (0.5, 0.5) of the two value rows.

Input: q = [[[[2,0]]],[[[0,-1]]]], k = [[[[1,0],[0,1],[-1,0]]],[[[1,0],[0,1],[-1,0]]]], v = [[[[1,0],[0,1],[1,1]]],[[[2,0],[0,2],[2,2]]]]

Output: [[[[0.813306,0.232082]]],[[[1.604448,1.197776]]]]

Input: q = [[[[1,1,0,0],[0,0,1,-1]]]], k = [[[[1,0,1,0],[0,1,0,1],[1,1,1,1]]]], v = [[[[3],[-1],[0.5]]]]

Output: [[[[0.774069],[1.486715]]]]

Hint 1

jax.vmap(f) maps over axis 0, and jax.vmap(jax.vmap(f)) maps over two leading axes.

Hint 2

jax.nn.softmax(scores, axis=-1) normalizes over the keys.

Hint 3

Divide by jnp.sqrt(d).astype(q.dtype) to keep bfloat16 inputs in bfloat16.

Requirements

  • Write the rule for a single (Tq, D) block of queries and apply it to the full arrays with two jax.vmap calls, one nested inside the other, so that one maps over the head axis and the other maps over the batch axis.
  • The function must also give the same result when it is wrapped in jax.jit, so it must not convert array values to Python numbers or NumPy arrays or branch in Python on them.
  • Keep the softmax finite for large scores.

Constraints

  • B, H, Tq and Tk are between 1 and 5, D is between 1 and 8, and Dv is between 1 and 4.
  • q, k and v share one dtype, float32 or bfloat16, and inputs from the Run panel are float32.
  • All values are finite, and some scores exceed 100 in magnitude.
Try Similar Problems
Pairwise Distances with Nested vmapMediumModel Ensemble with in_axesEasyPad and Mask to Bucket LengthMediumHessian-Vector ProductMediumPer-Example Gradient ClippingMediumStraight-Through Estimator with Custom VJPMediumTraced Learning Rate ScheduleMediumExponential Moving Average with ScanMediumSpectral Norm by Power IterationMediumGlobal Norm Gradient ClippingMediumAdamW from ScratchMediumMixed Precision with Loss ScalingMedium

Accepts: array

Accepts: array

Accepts: array

You must run your code first.
Batched Attention with vmap

Batched Attention with vmap

JAX
Medium

Compute scaled dot-product attention for a batch of examples with several heads.

For one head of one example, the queries QQQ have shape (Tq, D), the keys KKK have shape (Tk, D) and the values VVV have shape (Tk, Dv), and the output is

Attention⁡(Q,K,V)=softmax⁡(QK⊤D)V,\operatorname{Attention}(Q, K, V) = \operatorname{softmax}\left(\frac{Q K^{\top}}{\sqrt{D}}\right) V ,Attention(Q,K,V)=softmax(D​QK⊤​)V,

where the softmax runs over the Tk keys separately for each query row. The input q has shape (B, H, Tq, D), k has shape (B, H, Tk, D) and v has shape (B, H, Tk, Dv), where B is the batch size, H is the number of heads, Tq is the number of queries, Tk is the number of keys, D is the shared query and key width and Dv is the value width. Example b and head h use the blocks q[b, h], k[b, h] and v[b, h].

Return a JAX array of shape (B, H, Tq, Dv) with the dtype of q.

Examples

Input: q = [[[[1,0],[0,1]],[[1,1],[0,0]]]], k = [[[[1,0],[0,1]],[[1,0],[0,1]]]], v = [[[[1,2],[3,4]],[[0,1],[1,0]]]]

Output: [[[[1.660477,2.660477],[2.339523,3.339523]],[[0.5,0.5],[0.5,0.5]]]]

Explanation: In head 0, the query (1, 0) scores 0.7071 against the first key and 0 against the second, so its weights are 0.6698 and 0.3302 and its output is 0.6698 times (1, 2) plus 0.3302 times (3, 4). In head 1 every query scores the two keys equally, so every output is the plain average (0.5, 0.5) of the two value rows.

Input: q = [[[[2,0]]],[[[0,-1]]]], k = [[[[1,0],[0,1],[-1,0]]],[[[1,0],[0,1],[-1,0]]]], v = [[[[1,0],[0,1],[1,1]]],[[[2,0],[0,2],[2,2]]]]

Output: [[[[0.813306,0.232082]]],[[[1.604448,1.197776]]]]

Input: q = [[[[1,1,0,0],[0,0,1,-1]]]], k = [[[[1,0,1,0],[0,1,0,1],[1,1,1,1]]]], v = [[[[3],[-1],[0.5]]]]

Output: [[[[0.774069],[1.486715]]]]

Hint 1

jax.vmap(f) maps over axis 0, and jax.vmap(jax.vmap(f)) maps over two leading axes.

Hint 2

jax.nn.softmax(scores, axis=-1) normalizes over the keys.

Hint 3

Divide by jnp.sqrt(d).astype(q.dtype) to keep bfloat16 inputs in bfloat16.

Requirements

  • Write the rule for a single (Tq, D) block of queries and apply it to the full arrays with two jax.vmap calls, one nested inside the other, so that one maps over the head axis and the other maps over the batch axis.
  • The function must also give the same result when it is wrapped in jax.jit, so it must not convert array values to Python numbers or NumPy arrays or branch in Python on them.
  • Keep the softmax finite for large scores.

Constraints

  • B, H, Tq and Tk are between 1 and 5, D is between 1 and 8, and Dv is between 1 and 4.
  • q, k and v share one dtype, float32 or bfloat16, and inputs from the Run panel are float32.
  • All values are finite, and some scores exceed 100 in magnitude.
Try Similar Problems
Pairwise Distances with Nested vmapMediumModel Ensemble with in_axesEasyPad and Mask to Bucket LengthMediumHessian-Vector ProductMediumPer-Example Gradient ClippingMediumStraight-Through Estimator with Custom VJPMediumTraced Learning Rate ScheduleMediumExponential Moving Average with ScanMediumSpectral Norm by Power IterationMediumGlobal Norm Gradient ClippingMediumAdamW from ScratchMediumMixed Precision with Loss ScalingMedium

Accepts: array

Accepts: array

Accepts: array

You must run your code first.
Problems
Loading...
1 / 1