TensorTonic
Problems
FlashAttention Forward Kernel in Triton
TransformersGPU Programming
Hard

Implement the forward pass of FlashAttention as a single fused Triton kernel. 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

The standard three-pass formulation writes the full N×NN \times NN×N attention matrix to HBM, dominating bandwidth for large NNN. FlashAttention tiles QQQ into row-blocks and streams over KKK, VVV in column-blocks, holding the per-row running max mim_imi​ and running denominator lil_ili​ in registers. Each inner step rescales the accumulator oio_ioi​ when the running max changes, so the output is always normalized with respect to the maximum seen so far.

After the last KKK, VVV block, dividing oio_ioi​ by lil_ili​ produces the exact softmax-weighted output. This is the capstone of the section: it composes tile loops, online softmax, masking, and reduction primitives into one kernel.

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

Keep three accumulators per query row: running max (init to −∞-\infty−∞), running denominator (init to 000), running output (init to 000). Update them together on each KKK, VVV block.

Hint 2

When a new block produces a higher max mnewm_\text{new}mnew​, multiply both the running denominator and the running output by exp⁡(m−mnew)\exp(m - m_\text{new})exp(m−mnew​) before adding the block's contribution. The exponential of the difference is the rescaling factor.

Hint 3

After the loop divide the running output by the running denominator once and store. Both the last query tile and the last key tile may be partial, so the row and column masks both matter.

Requirements

  • Use one Triton program per query row-block; load that row-block of QQQ once and keep it in registers
  • Stream over KKK and VVV in column-blocks while maintaining a running max, running denominator, and running output
  • Scale by 1/D1/\sqrt{D}1/D​ inside the kernel before the running-max update
  • Mask the row and column tails on every load and the final store
  • 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. No batch dimension, single head.
  • Tolerance: combined atol=1e-2 + rtol=1e-2 * abs(expected) for N≥64N \ge 64N≥64 to absorb the drift from online softmax rescaling.
  • The N×NN \times NN×N score matrix must never be written to global memory.
Try Similar Problems
Scaled Dot-Product Attention in TritonMediumRotary Position Embedding in TritonMediumOnline Softmax Kernel in TritonMediumKV Cache Append in TritonEasyScaled Dot-Product Attention in CUDAHardLayerNorm Backward in TritonHardSplit-K Matmul in TritonHardGrouped Program-ID Matmul in TritonHardRow-Wise LogSumExp in TritonMediumBlock-Pointer Matmul in TritonMediumSingle-Pass Mean and Variance in TritonMediumAutotuned Matrix Multiplication in TritonMedium

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

FlashAttention Forward Kernel in Triton

FlashAttention Forward Kernel in Triton

TransformersGPU Programming
Hard

Implement the forward pass of FlashAttention as a single fused Triton kernel. 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

The standard three-pass formulation writes the full N×NN \times NN×N attention matrix to HBM, dominating bandwidth for large NNN. FlashAttention tiles QQQ into row-blocks and streams over KKK, VVV in column-blocks, holding the per-row running max mim_imi​ and running denominator lil_ili​ in registers. Each inner step rescales the accumulator oio_ioi​ when the running max changes, so the output is always normalized with respect to the maximum seen so far.

After the last KKK, VVV block, dividing oio_ioi​ by lil_ili​ produces the exact softmax-weighted output. This is the capstone of the section: it composes tile loops, online softmax, masking, and reduction primitives into one kernel.

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

Keep three accumulators per query row: running max (init to −∞-\infty−∞), running denominator (init to 000), running output (init to 000). Update them together on each KKK, VVV block.

Hint 2

When a new block produces a higher max mnewm_\text{new}mnew​, multiply both the running denominator and the running output by exp⁡(m−mnew)\exp(m - m_\text{new})exp(m−mnew​) before adding the block's contribution. The exponential of the difference is the rescaling factor.

Hint 3

After the loop divide the running output by the running denominator once and store. Both the last query tile and the last key tile may be partial, so the row and column masks both matter.

Requirements

  • Use one Triton program per query row-block; load that row-block of QQQ once and keep it in registers
  • Stream over KKK and VVV in column-blocks while maintaining a running max, running denominator, and running output
  • Scale by 1/D1/\sqrt{D}1/D​ inside the kernel before the running-max update
  • Mask the row and column tails on every load and the final store
  • 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. No batch dimension, single head.
  • Tolerance: combined atol=1e-2 + rtol=1e-2 * abs(expected) for N≥64N \ge 64N≥64 to absorb the drift from online softmax rescaling.
  • The N×NN \times NN×N score matrix must never be written to global memory.
Try Similar Problems
Scaled Dot-Product Attention in TritonMediumRotary Position Embedding in TritonMediumOnline Softmax Kernel in TritonMediumKV Cache Append in TritonEasyScaled Dot-Product Attention in CUDAHardLayerNorm Backward in TritonHardSplit-K Matmul in TritonHardGrouped Program-ID Matmul in TritonHardRow-Wise LogSumExp in TritonMediumBlock-Pointer Matmul in TritonMediumSingle-Pass Mean and Variance in TritonMediumAutotuned Matrix Multiplication in TritonMedium

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