Richael
← Back to blog

2026-11-22

Flash Attention, Revisited: The Card Was Fine

10th kernel: flash attention again, this time with a backward, and a correction. Week 3 ran at about 0.5 TFLOPs on a T4, ~25x behind torch, and I blamed the card, because Turing has no cp.async. That was wrong. This version adds a backward in three kernels with no atomics, recomputing the probabilities from Q, K and the saved logsumexp instead of storing them. Output and all three gradients match SDPA to about 5e-4 relative, with essentially no exact zeros.

On a Colab T4 with Triton 3.1.0 and torch 2.5.1, two runs each: the forward does 9.5 TFLOPs at N=512, 1.69x SDPA, and 19.7-20.1 at N=4096, 1.47-1.49x. Forward plus backward beats SDPA at every size, from 1.57x at N=512 down to 1.39x at N=4096. The N=2048 forward came out 39% apart between runs, so I'm not quoting it. An Ampere comparison was single-run and isn't reported.

The catch is that Colab's default today is Triton 3.8.0 with torch 2.14, and on that the identical file does 0.41-0.56 TFLOPs. Week 3's number. Swapping only Triton on the same T4, N=4096: 3.1.0 gives 20.1, 3.2.0 gives 20.3, 3.3.1 fails to compile, 3.4.0 to 3.8.0 give 0.46-0.53. So the slowdown follows the Triton version, somewhere between 3.2.0 and 3.4.0 on sm_75. I never recorded week 3's Triton version, so its result is consistent with this, not proven by it, and why the newer compiler is slow on Turing I haven't established. Copy this kernel into a fresh Colab and you get the slow one.

Next is a fused backward, FlashAttention-2 style, one kernel instead of three: right now dK/dV and dQ each read Q, K and V again.

Two charts for one Triton flash attention kernel on a Tesla T4, B=4, H=8, D=64, fp16, forward only. Left, TFLOPs on a log scale against sequence length 512 to 4096: the kernel on Triton 3.1.0 sits at about 9.5 TFLOPs at 512, 11.4 at 1024 and 20 at 4096, above torch SDPA on torch 2.5.1 at about 5.7, 12.9 and 13.4, with points missing where two runs disagreed; the same kernel on Triton 3.8.0 is a flat line near 0.4 to 0.56 TFLOPs. Right, bars of TFLOPs at N=4096 by Triton version: 20.1 for 3.1.0 and 20.3 for 3.2.0, a compile error for 3.3.1, then 0.46, 0.53, 0.51, 0.52 and 0.50 for 3.4.0 through 3.8.0.