TensorTonic
Problems
Scaled Dot-Product Attention in Triton
TransformersLinear AlgebraGPU Programming
Medium

Implement naive three-stage scaled dot-product attention in Triton. Given query, key, and value matrices QQQ, KKK, VVV all of shape (N,D)(N, D)(N,D):

out=softmax ⁣(QK⊤D)V\texttt{out} = \mathrm{softmax}\!\left(\frac{Q K^\top}{\sqrt{D}}\right) Vout=softmax(D​QK⊤​)V

Unlike FlashAttention, this implementation materializes the full N×NN \times NN×N score matrix in a scratch tensor and runs three separate kernels: a tiled matmul for QK⊤Q K^\topQK⊤, a row-wise softmax over the score matrix, and a second tiled matmul attn⋅V\mathrm{attn} \cdot Vattn⋅V. Each kernel reads and writes the score tensor to HBM, which is exactly the bandwidth cost that FlashAttention avoids by fusing the three stages. Same math, very different memory footprint.

Loading visualization...

Examples

Input: Q = [[1.0, 0.0], [0.0, 1.0]], K = [[1.0, 0.0], [0.0, 1.0]], V = [[1.0, 2.0], [3.0, 4.0]]

Output: [[1.6604, 2.6604], [2.3396, 3.3396]]

Input: Q = [[0.0, 0.0]], K = [[1.0, 1.0], [2.0, 2.0]], V = [[5.0, 5.0], [10.0, 10.0]]

Output: [[7.5, 7.5]]

Hint 1

Stage 1 is a tiled QK⊤Q K^\topQK⊤ scaled by 1/D1/\sqrt{D}1/D​. Each program writes one tile of the score matrix to HBM.

Hint 2

Stage 2 is row-wise softmax on the score matrix. One program per row: subtract the row max, exponentiate, divide by the sum. Same structure as the fused-softmax problem.

Hint 3

Stage 3 is attn⋅V\mathrm{attn} \cdot Vattn⋅V, structurally identical to stage 1 but with the materialized score matrix as the left operand. Three kernels, three HBM round-trips - that is the bandwidth cost.

Requirements

  • Use three separate Triton kernels: scaled QK⊤Q K^\topQK⊤, row-wise softmax, then attn⋅V\mathrm{attn} \cdot Vattn⋅V
  • Allocate the N×NN \times NN×N score matrix as a scratch tensor inside the launcher
  • Apply the 1/D1/\sqrt{D}1/D​ scale inside the first matmul kernel
  • Mask the row and column tails on every load and store; subtract the row max before exponentiating for stability
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤5121 \le N \le 5121≤N≤512, D∈{8,16,32,64}D \in \{8, 16, 32, 64\}D∈{8,16,32,64}.
  • All tensors are torch.float32 on cuda. Single head, no batch dimension.
  • Tolerance: combined atol=1e-2 + rtol=1e-2 * abs(expected) for N≥64N \ge 64N≥64 due to two sequential matmuls accumulating drift.
  • The N×NN \times NN×N scratch tensor is allocated and written; this is the explicit contrast with FlashAttention.
Try Similar Problems
Rotary Position Embedding in TritonMediumOnline Softmax Kernel in TritonMediumKV Cache Append in TritonEasyFlashAttention Forward Kernel in TritonHardScaled Dot-Product Attention in CUDAHardBlock-Pointer Matmul in TritonMediumAutotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumGEMV: Matrix Vector Product in TritonMediumFused Matmul + Bias + ReLU in TritonMediumTiled Matrix Multiplication in TritonMediumSplit-K 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 Triton

Scaled Dot-Product Attention in Triton

TransformersLinear AlgebraGPU Programming
Medium

Implement naive three-stage scaled dot-product attention in Triton. Given query, key, and value matrices QQQ, KKK, VVV all of shape (N,D)(N, D)(N,D):

out=softmax ⁣(QK⊤D)V\texttt{out} = \mathrm{softmax}\!\left(\frac{Q K^\top}{\sqrt{D}}\right) Vout=softmax(D​QK⊤​)V

Unlike FlashAttention, this implementation materializes the full N×NN \times NN×N score matrix in a scratch tensor and runs three separate kernels: a tiled matmul for QK⊤Q K^\topQK⊤, a row-wise softmax over the score matrix, and a second tiled matmul attn⋅V\mathrm{attn} \cdot Vattn⋅V. Each kernel reads and writes the score tensor to HBM, which is exactly the bandwidth cost that FlashAttention avoids by fusing the three stages. Same math, very different memory footprint.

Loading visualization...

Examples

Input: Q = [[1.0, 0.0], [0.0, 1.0]], K = [[1.0, 0.0], [0.0, 1.0]], V = [[1.0, 2.0], [3.0, 4.0]]

Output: [[1.6604, 2.6604], [2.3396, 3.3396]]

Input: Q = [[0.0, 0.0]], K = [[1.0, 1.0], [2.0, 2.0]], V = [[5.0, 5.0], [10.0, 10.0]]

Output: [[7.5, 7.5]]

Hint 1

Stage 1 is a tiled QK⊤Q K^\topQK⊤ scaled by 1/D1/\sqrt{D}1/D​. Each program writes one tile of the score matrix to HBM.

Hint 2

Stage 2 is row-wise softmax on the score matrix. One program per row: subtract the row max, exponentiate, divide by the sum. Same structure as the fused-softmax problem.

Hint 3

Stage 3 is attn⋅V\mathrm{attn} \cdot Vattn⋅V, structurally identical to stage 1 but with the materialized score matrix as the left operand. Three kernels, three HBM round-trips - that is the bandwidth cost.

Requirements

  • Use three separate Triton kernels: scaled QK⊤Q K^\topQK⊤, row-wise softmax, then attn⋅V\mathrm{attn} \cdot Vattn⋅V
  • Allocate the N×NN \times NN×N score matrix as a scratch tensor inside the launcher
  • Apply the 1/D1/\sqrt{D}1/D​ scale inside the first matmul kernel
  • Mask the row and column tails on every load and store; subtract the row max before exponentiating for stability
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤5121 \le N \le 5121≤N≤512, D∈{8,16,32,64}D \in \{8, 16, 32, 64\}D∈{8,16,32,64}.
  • All tensors are torch.float32 on cuda. Single head, no batch dimension.
  • Tolerance: combined atol=1e-2 + rtol=1e-2 * abs(expected) for N≥64N \ge 64N≥64 due to two sequential matmuls accumulating drift.
  • The N×NN \times NN×N scratch tensor is allocated and written; this is the explicit contrast with FlashAttention.
Try Similar Problems
Rotary Position Embedding in TritonMediumOnline Softmax Kernel in TritonMediumKV Cache Append in TritonEasyFlashAttention Forward Kernel in TritonHardScaled Dot-Product Attention in CUDAHardBlock-Pointer Matmul in TritonMediumAutotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumGEMV: Matrix Vector Product in TritonMediumFused Matmul + Bias + ReLU in TritonMediumTiled Matrix Multiplication in TritonMediumSplit-K 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