TensorTonic
Study Plans
Study Plan
Named-Dimension Batched Attention Scores
Language ModelsTransformers
Easy

Compute scaled query-key dot products independently for multiple attention heads, without a softmax or mask. Queries have shape (B, S_q, D), keys have shape (B, S_k, D), and each token's model-width axis contains H consecutive head blocks.

dh=D/Hd_h=D/Hdh​=D/H Ab,h,i,j=∑r=1dhQb,h,i,rKb,h,j,rdhA_{b,h,i,j}=\frac{\sum_{r=1}^{d_h}Q_{b,h,i,r}K_{b,h,j,r}}{\sqrt{d_h}}Ab,h,i,j​=dh​​∑r=1dh​​Qb,h,i,r​Kb,h,j,r​​

Here B is batch size, S_q and S_k are the query and key lengths, D is model width, H is num_heads, and d_h is head width. Q and K denote the query and key values within those head blocks. Indices b and h select a batch and head, i and j select query and key positions, r selects a coordinate within a head, and A contains the scores.

Return a PyTorch tensor of shape (B, H, S_q, S_k), preserving the shared input dtype and device.

Loading visualization...

Examples

Input: q = [[[1,0,0,1],[0,1,1,0]]], k = [[[1,0,1,0],[0,1,0,1]]], num_heads = 2

Output: [[[[0.707107,0],[0,0.707107]],[[0,0.707107],[0.707107,0]]]]

Explanation: Each 4-value token becomes two heads of width 2. Every head computes its own query-key dot products and divides them by sqrt(2).

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

Output: [[[[0.707107,1.414214]],[[2.12132,2.828427]]]]

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

Output: [[[[1.414214]]]]

Hint 1

torch.reshape can separate a dimension even when the input is noncontiguous.

Hint 2

torch.transpose exchanges two tensor axes.

Hint 3

In torch.einsum, a label omitted from the output is summed over.

Requirements

  • Use PyTorch and torch.einsum for the query-key contraction.
  • Preserve the shared float32 or float64 dtype and device; public examples and custom Run inputs use float32.
  • Do not modify either input tensor.

Constraints

  • q and k are rank-three PyTorch tensors with the same batch size, model width, floating-point dtype, and device.
  • Batch size, query length, key length, and model width are positive.
  • num_heads is a positive Python integer that divides the model width.
  • All supplied values are finite; noncontiguous inputs are valid.
Try Similar Problems
Transformer Training FLOP EstimatorEasyPatchify a Batch of ImagesEasyMixed-Precision Training Memory AccountantEasyGradient Accumulation EquivalenceEasyAssign Multimodal RoPE CoordinatesMediumRoPE: Rotary Query and Key EmbeddingsMediumDecode Attention with a KV CacheHardCausal Grouped-Query AttentionHardCalculate KV Cache Memory for MHA, MQA, GQA, and MLAEasyImplement Scaled Dot-Product AttentionEasyViT PatchifyEasyMasked Language ModelingEasy

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Named-Dimension Batched Attention Scores

Language ModelsTransformers
Easy

Compute scaled query-key dot products independently for multiple attention heads, without a softmax or mask. Queries have shape (B, S_q, D), keys have shape (B, S_k, D), and each token's model-width axis contains H consecutive head blocks.

dh=D/Hd_h=D/Hdh​=D/H Ab,h,i,j=∑r=1dhQb,h,i,rKb,h,j,rdhA_{b,h,i,j}=\frac{\sum_{r=1}^{d_h}Q_{b,h,i,r}K_{b,h,j,r}}{\sqrt{d_h}}Ab,h,i,j​=dh​​∑r=1dh​​Qb,h,i,r​Kb,h,j,r​​

Here B is batch size, S_q and S_k are the query and key lengths, D is model width, H is num_heads, and d_h is head width. Q and K denote the query and key values within those head blocks. Indices b and h select a batch and head, i and j select query and key positions, r selects a coordinate within a head, and A contains the scores.

Return a PyTorch tensor of shape (B, H, S_q, S_k), preserving the shared input dtype and device.

Loading visualization...

Examples

Input: q = [[[1,0,0,1],[0,1,1,0]]], k = [[[1,0,1,0],[0,1,0,1]]], num_heads = 2

Output: [[[[0.707107,0],[0,0.707107]],[[0,0.707107],[0.707107,0]]]]

Explanation: Each 4-value token becomes two heads of width 2. Every head computes its own query-key dot products and divides them by sqrt(2).

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

Output: [[[[0.707107,1.414214]],[[2.12132,2.828427]]]]

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

Output: [[[[1.414214]]]]

Hint 1

torch.reshape can separate a dimension even when the input is noncontiguous.

Hint 2

torch.transpose exchanges two tensor axes.

Hint 3

In torch.einsum, a label omitted from the output is summed over.

Requirements

  • Use PyTorch and torch.einsum for the query-key contraction.
  • Preserve the shared float32 or float64 dtype and device; public examples and custom Run inputs use float32.
  • Do not modify either input tensor.

Constraints

  • q and k are rank-three PyTorch tensors with the same batch size, model width, floating-point dtype, and device.
  • Batch size, query length, key length, and model width are positive.
  • num_heads is a positive Python integer that divides the model width.
  • All supplied values are finite; noncontiguous inputs are valid.
Try Similar Problems
Transformer Training FLOP EstimatorEasyPatchify a Batch of ImagesEasyMixed-Precision Training Memory AccountantEasyGradient Accumulation EquivalenceEasyAssign Multimodal RoPE CoordinatesMediumRoPE: Rotary Query and Key EmbeddingsMediumDecode Attention with a KV CacheHardCausal Grouped-Query AttentionHardCalculate KV Cache Memory for MHA, MQA, GQA, and MLAEasyImplement Scaled Dot-Product AttentionEasyViT PatchifyEasyMasked Language ModelingEasy

Accepts: array

Accepts: array

Accepts: number

You must run your code first.