Level 5 · research · ~50 h
Implement FlashAttention-style tiled attention in Triton
Write a fused tiled attention forward and backward kernel in Triton that avoids materializing the attention matrix, and verify numerical equivalence with PyTorch's scaled dot-product attention across sequence lengths up to 16k.
Problem statement
- 01Write a fused tiled attention forward and backward kernel in Triton that avoids materializing the attention matrix, and verify numerical equivalence with PyTorch's scaled dot-product attention across sequence lengths up to 16k.
Real-world motivation
- 01Attention is memory-bound for long sequences; IO-aware tiling reduces memory from quadratic to linear and is the foundation of modern long-context models.
Learning objectives
- 01Implement an online-softmax forward kernel
- 02Implement the backward pass with recomputation
- 03Demonstrate linear memory scaling and speedups
Technical architecture
- 01tiles of queries and keys resident in SRAM
- 02running row max and sum for online softmax
- 03recomputed backward with atomic or tiled accumulation
Mathematical foundations
- 01$m_i^{(j)}=\max(m_i^{(j-1)},\max_l S_{i,:j\mathrm{BLOCK}})$
- 02$\ell_i^{(j)}=\ell_i^{(j-1)}e^{m^{(j-1)}-m^{(j)}}+\sum_l e^{S_{il}-m^{(j)}}$
- 03$O_i^{(j)}=O_i^{(j-1)}e^{m^{(j-1)}-m^{(j)}}+e^{-m^{(j)}}\sum_l e^{S_{il}}V_l$
Dataset and sources
- 01synthetic fp16 and bf16 attention tensors
- 02WikiText-103 token sequences for end-to-end validation
Step-by-step tasks
- 01Implement a naive Triton attention kernel as a baseline
- 02Add tiling over keys with online softmax
- 03Add causal masking inside tiles
- 04Implement the backward pass with recomputed scores
- 05Benchmark memory and throughput across sequence lengths
- 06Integrate into a transformer layer and verify training stability
Implementation requirements
- 01Use triton.jit with tunable BLOCK_M and BLOCK_N
- 02Use tl.dot for tensor-core matmuls
- 03Keep accumulators in fp32
Testing requirements
- 01Compare output with F.scaled_dot_product_attention to 1e-3
- 02Gradient-check the full layer
- 03Verify memory grows linearly with sequence length
Evaluation metrics
- 01peak GPU memory
- 02TFLOPs
- 03numerical max error
- 04speedup over PyTorch SDPA
Expected outputs
- 01kernel source
- 02benchmark figures
- 03numerical equivalence report
- 04integration patch
Common failure modes
- 01incorrect softmax rescaling gives silently wrong outputs
- 02fp16 exponent overflow
- 03non-multiple-of-block sequence lengths need masking
Stretch goals
- 01support variable lengths with a block table
- 02add dropout and alibi biases
Research extensions
- 01derive IO complexity and compare with measured traffic
- 02study block-size autotuning across GPUs
GitHub submission requirements
- 01Kernel source with tuning configs
- 02Benchmark script and figures
- 03Numerical verification notebook
- 04Report with roofline analysis
Completion rubric
- 01online softmax mathematically correct
- 02backward pass gradient-checked
- 03linear memory scaling demonstrated
- 04numerical equivalence within tolerance
- 05report explains IO complexity
Submit when every rubric item is satisfied and your repository contains runnable code plus a reproduction command.