Explore the research collection
2 entries · Follow this search
- Paper
Broken Symmetry in BF16 Attention: Why FlashAttention Gradients Blow Up Late in Training
Diagnoses a late-training blow-up training a 450M transformer on 50B tokens with FlashAttention-3 in BF16: healthy for 25B tokens, then gradient norm grows 1000x and loss ends 0.2 nats above FP32 attention — with no NaNs at all. Recomputing just two layers' attention backward in FP32 removes almost all excess gradient. Part of the cause is known (a fused multiply-add in the forward softmax, never fixed in FA3); repairing it stops the blow-up but leaves the query gradient wrong by more than its own size (219% median error). The deeper cause is a broken conservation law: the softmax score gradient dS sums to zero along every row, making dQ blind to where the keys sit as a group; rounding dS to BF16 leaves a small nonzero sum that leaks the mean key into dQ — and the leak grows exactly as late training makes keys large and attention sharp. Fix: GProj (gauge projection), which restores the zero row sum after the cast with two rank-one corrections per row — cuts median dQ/dK errors from 219%/13% to 0.34%/0.37% (FP32-attention level) for 4.7% more step time; in matched from-scratch runs it reaches the same loss as FP32 attention while FA3 and key smoothing destabilize. KohakuBlueleaf replied on the thread reporting the same class of precision bug in FA4/cuDNN/flex attention and their KohakuFA fix. If a BF16 run blows up late with no NaNs, check the attention backward. Directly relevant to the user's LLM training-run debugging work.
- Code
KohakuFA: Attention Kernel with Fixed Backward-Pass Precision
New attention kernel claiming severe backward-pass precision bugs in cuDNN SDPA, FlashAttention-4, and flex attention: when attention logits get huge (DINOv3 reportedly hits ~1e5 out of the box), backward gradients can be wrong by 10-1000x or become inf, while forward and loss look completely normal — so instability surfaces only late in training when reruns are expensive. Implication: some reported fp16 instability, QKNorm benefits, or 'failed runs' may be kernel artifacts, not model-design flaws. KohakuFA fixes 3 precision bugs, keeping gradients at the fp16 error floor for ~5% extra cost; fast for ViT/DiT/video. Figures: DINOv3 ViT-B/16 fine-tuning backward error over training — FA4 highest error (~0.05-0.6), KohakuFA tracks the fp64 correct-kernel floor; whole-model fp16 gradient vs fp64 across 50k-200k steps — cuDNN SDPA gradients collapse after ~100k-125k steps (cosine→0, relative error >10x), KohakuFA stays near-correct. Blackwell (SM100) only for now; sm120 support planned (flex attention reportedly has the same issue on sm120). Directly relevant to user's training-run debugging (CUDA OOMs aside, kernel-precision bugs are the same class of hard-to-diagnose training instability).