Build your own Triton attention kernel and ship it as a torch custom op
Colab T4 · free~10 hWrite a fused attention forward kernel in Triton (online softmax, causal mask, fp16 inputs with fp32 accumulation), register it as a torch.library custom operator with a fake implementation, prove it runs under torch.compile with no graph breaks, and benchmark it against F.scaled_dot_product_attention on a T4. This is the Phase-6 kernel loop in miniature, from blank file to installable package with numbers.
Scope
- A Triton kernel attn_fwd with BLOCK_M query tiles, a loop over BLOCK_N key tiles, running max and sum, and a causal flag.
- An autotune config list over block sizes, num_warps and num_stages, with the winning config logged per sequence length.
- A torch.library.custom_op registration (schema, CUDA implementation, fake implementation, explicit non-differentiable) and torch.library.opcheck in the tests.
- A pytest suite comparing against SDPA at N in (128, 512, 1024, 2048, 4096), heads 8, d 64, causal and non-causal, fp16 and fp32.
- A torch.compile(fullgraph=True, dynamic=True) test over three sequence lengths with TORCH_LOGS=graph_breaks captured.
- A bench.py producing a latency and TFLOP/s table and plot versus SDPA, plus a pip-installable package.
Out of scope
- Backward pass or training.
- Paged KV or block tables (the medium and large capstones and the custom-attention build cover those).
- Hopper-only features such as TMA or wgmma.
What to measure
- Numerical agreement with SDPA
- How · torch.testing.assert_close over all test shapes, fp16 atol 2e-2, fp32 atol 1e-4
- Target · all pass
- Peak memory scaling
- How · torch.cuda.max_memory_allocated at N 2048 versus 4096
- Target · less than 2x growth (no materialized N by N scores)
- Speed relative to SDPA on T4
- How · CUDA-event median of 50 runs at N 2048 and 4096 fp16, batch 4, heads 8
- Target · 0.5x to 1.2x of SDPA; report the ratio
- torch.compile integration
- How · count of graph breaks in TORCH_LOGS=graph_breaks for the compiled wrapper module
- Target · 0 graph breaks
- Clean-environment install
- How · pip install the package in a fresh Colab runtime and run pytest
- Target · exit 0
Pay attention to
- The rescaling factor exp(m_old minus m_new) must be applied to both the running sum and the accumulator; a kernel that is right for one tile and wrong for two is the classic bug.
- Causal masking must be applied before the running max update, and tiles entirely above the diagonal should be skipped, not masked.
- T4 (sm_75) has fp16 tensor cores but no bf16; use fp16 inputs and fp32 accumulators and do not quote bf16 numbers.
- The fake implementation must return the output shape from symbolic sizes, not from running the kernel; opcheck will catch this.
- Warm up and lock clocks as far as Colab allows; report medians, not minimums.
Acceptance (all must pass)
- ☐pytest passes all correctness shapes for causal and non-causal in fp16 and fp32.
- ☐torch.library.opcheck passes and the compiled module shows zero graph breaks at three lengths including one unseen at compile time.
- ☐Peak memory at N 4096 is less than 2x that at N 2048.
- ☐README has the benchmark table with the SDPA ratio and the chosen autotune configs.
- ☐A fresh runtime can pip install the package and pass the tests.