TensorTonic
Problems
Scaled Dot-Product Attention in CUDA
TransformersLinear AlgebraGPU Programming
Hard

Implement single-head scaled dot-product attention without masking. Given query, key, and value matrices QQQ, KKK, VVV, each of shape (N,D)(N, D)(N,D), compute the attention output of shape (N,D)(N, D)(N,D) in three stages.

First the scaled score matrix:

scores=QK⊤D\text{scores} = \frac{Q K^\top}{\sqrt{D}}scores=D​QK⊤​

Then a numerically stable row-wise softmax:

attni,j=exp⁡(scoresi,j−max⁡kscoresi,k)∑lexp⁡(scoresi,l−max⁡kscoresi,k)\text{attn}_{i, j} = \frac{\exp(\text{scores}_{i, j} - \max_k \text{scores}_{i, k})}{\sum_l \exp(\text{scores}_{i, l} - \max_k \text{scores}_{i, k})}attni,j​=∑l​exp(scoresi,l​−maxk​scoresi,k​)exp(scoresi,j​−maxk​scoresi,k​)​

Finally the value aggregation:

output=attn V\text{output} = \text{attn}\, Voutput=attnV

All matrices are row-major. The softmax is taken over the last axis (each query row independently).

Loading visualization...

Examples

Input: N = 2, D = 2, Q = [[1, 0], [0, 1]], K = [[1, 0], [0, 1]], V = [[2, 0], [0, 3]]

Output: [[1.3395, 0.9907], [0.6605, 2.0093]]

Each query attends slightly more to its matching key, so the output is a weighted blend of the two value rows.

Input: N = 2, D = 2, Q = [[0, 0], [0, 0]], K = [[1, 2], [3, 4]], V = [[1, 1], [2, 2]]

Output: [[1.5, 1.5], [1.5, 1.5]]

Zero queries give uniform attention, so each output row is the mean of the value rows.

Hint 1

Decompose the problem into three kernels: scores, row-wise softmax, then value aggregation. Allocate an N×NN \times NN×N scratch buffer for the scores.

Hint 2

The middle step is exactly the row-wise softmax you have already built; reuse the shared-memory max and sum-exp reduction over each row.

Hint 3

The scale factor is 1.0f / sqrtf((float)D)\texttt{1.0f / sqrtf((float)D)}1.0f / sqrtf((float)D), applied to each score before the softmax.

Requirements

  • Scale the raw scores by 1/D1 / \sqrt{D}1/D​
  • Apply a numerically stable row-wise softmax (subtract the row max)
  • Aggregate the values with the attention weights
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤10241 \le N \le 10241≤N≤1024
  • 1≤D≤5121 \le D \le 5121≤D≤512
  • QQQ, KKK, VVV, and the output are device pointers of shape (N,D)(N, D)(N,D)
  • A scratch buffer of N×NN \times NN×N floats may be allocated inside solve\texttt{solve}solve
  • Values fit in 32-bit float
Try Similar Problems
Cosine Similarity as a CUDA KernelMediumScaled Dot-Product Attention in TritonMediumTiled Matrix Multiplication as a CUDA KernelHardRMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumMatrix Multiplication as a CUDA KernelMediumL1 Normalization as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMediumL2 Normalization as a CUDA KernelMediumMatrix-Vector Multiplication as a CUDA KernelMediumSplit-K Matmul in TritonHardGrouped Program-ID Matmul in TritonHard

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Scaled Dot-Product Attention in CUDA

Scaled Dot-Product Attention in CUDA

TransformersLinear AlgebraGPU Programming
Hard

Implement single-head scaled dot-product attention without masking. Given query, key, and value matrices QQQ, KKK, VVV, each of shape (N,D)(N, D)(N,D), compute the attention output of shape (N,D)(N, D)(N,D) in three stages.

First the scaled score matrix:

scores=QK⊤D\text{scores} = \frac{Q K^\top}{\sqrt{D}}scores=D​QK⊤​

Then a numerically stable row-wise softmax:

attni,j=exp⁡(scoresi,j−max⁡kscoresi,k)∑lexp⁡(scoresi,l−max⁡kscoresi,k)\text{attn}_{i, j} = \frac{\exp(\text{scores}_{i, j} - \max_k \text{scores}_{i, k})}{\sum_l \exp(\text{scores}_{i, l} - \max_k \text{scores}_{i, k})}attni,j​=∑l​exp(scoresi,l​−maxk​scoresi,k​)exp(scoresi,j​−maxk​scoresi,k​)​

Finally the value aggregation:

output=attn V\text{output} = \text{attn}\, Voutput=attnV

All matrices are row-major. The softmax is taken over the last axis (each query row independently).

Loading visualization...

Examples

Input: N = 2, D = 2, Q = [[1, 0], [0, 1]], K = [[1, 0], [0, 1]], V = [[2, 0], [0, 3]]

Output: [[1.3395, 0.9907], [0.6605, 2.0093]]

Each query attends slightly more to its matching key, so the output is a weighted blend of the two value rows.

Input: N = 2, D = 2, Q = [[0, 0], [0, 0]], K = [[1, 2], [3, 4]], V = [[1, 1], [2, 2]]

Output: [[1.5, 1.5], [1.5, 1.5]]

Zero queries give uniform attention, so each output row is the mean of the value rows.

Hint 1

Decompose the problem into three kernels: scores, row-wise softmax, then value aggregation. Allocate an N×NN \times NN×N scratch buffer for the scores.

Hint 2

The middle step is exactly the row-wise softmax you have already built; reuse the shared-memory max and sum-exp reduction over each row.

Hint 3

The scale factor is 1.0f / sqrtf((float)D)\texttt{1.0f / sqrtf((float)D)}1.0f / sqrtf((float)D), applied to each score before the softmax.

Requirements

  • Scale the raw scores by 1/D1 / \sqrt{D}1/D​
  • Apply a numerically stable row-wise softmax (subtract the row max)
  • Aggregate the values with the attention weights
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤10241 \le N \le 10241≤N≤1024
  • 1≤D≤5121 \le D \le 5121≤D≤512
  • QQQ, KKK, VVV, and the output are device pointers of shape (N,D)(N, D)(N,D)
  • A scratch buffer of N×NN \times NN×N floats may be allocated inside solve\texttt{solve}solve
  • Values fit in 32-bit float
Try Similar Problems
Cosine Similarity as a CUDA KernelMediumScaled Dot-Product Attention in TritonMediumTiled Matrix Multiplication as a CUDA KernelHardRMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumMatrix Multiplication as a CUDA KernelMediumL1 Normalization as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMediumL2 Normalization as a CUDA KernelMediumMatrix-Vector Multiplication as a CUDA KernelMediumSplit-K Matmul in TritonHardGrouped Program-ID Matmul in TritonHard

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1