Explore the research collection
7 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).
- Paper
You Need Better Attention Priors
Generalizes attention through the lens of Entropic Optimal Transport (EOT): standard attention corresponds to a transport problem regularized by an implicit uniform prior. GOAT (Generalized Optimal Transport Attention with Trainable priors) replaces that naive assumption with a learnable, continuous prior that decouples spatial geometry from token semantics — standard positional encodings (e.g., RoPE) entangle position with meaning and corrupt semantic representations when forced to handle structural biases like attention sinks. The prior maintains full compatibility with optimized kernels like FlashAttention (zero compute overhead). GOAT also provides an EOT-based explanation of attention sinks and a solution that avoids standard attention's representational trade-offs. By absorbing spatial information into the core attention computation, GOAT learns an extrapolatable prior combining the flexibility of learned positional embeddings with the length generalization of fixed encodings — strong long-context extrapolation beyond training length.
- Paper
SageAttention: Accurate 8-Bit Attention for Plug-and-play Inference Acceleration
PTQ attention: subtract the token-mean of K to smooth channel outliers so QK can be INT8, and keep PV in FP16 with an FP16 accumulator (INT8 PV is too noisy on some layers). Triton kernels on RTX4090/3090; peak ~341 TOPS at headdim 64/128 vs FlashAttention2 ~165. ~2.1× FA2 and ~2.7× xformers. Plug-and-play: Llama2-7B WikiText 5.824 vs FP 5.823, MMLU 0.46=FP; CogVideoX/Unidiffuser/UltraPixel/TIMM/Llava1.6 near-parity. ICLR 2025. Code https://github.com/thu-ml/SageAttention.
- Paper
DualKV: Shared-Prompt Flash Attention for Efficient RL Training with Large Rollouts and Long Contexts
Standard FA2 packs N same-prompt rollouts as N(P+R) tokens, recomputing the prompt N times. DualKV repacks to P+NR and splits attention into one prompt self-attention plus a fused kernel that reads shared KV once and accumulates N-way dK/dV with fp32 atomics; mathematically equivalent, no approximation. On Qwen3-8B GRPO (8xH100, N=32, 8K-context) policy-update is 1.63–2.09x faster, MFU 36%→76%; DAPO 2.47x / 77% MFU. At 30B MoE on 16xH100, 3.82x policy-update and 3.38x step vs FA2 that needs 4-way Ulysses SP. Also supports Gemma-4 hybrid d=512 / sliding-window at 64K. Code https://github.com/amazon-science/dualkv-flash-attn-for-rl.
- Model
Parallax: Parameterized Local Linear Attention for Language Modeling
Parallax parameterizes LLA's local-linear correction as ρ=W_R x, dropping the CG solve and the unstable boundary-amplification factor. A FlashAttention-style streaming kernel reuses the KV stream and raises arithmetic intensity; a Hopper decode prototype matches or beats FA2/3. Pretrained at 0.6B (78.6B tokens) and 1.7B (157.2B) on Ultra-FineWeb with a Qwen-3 backbone: under Muon, Parallax beats softmax on LAMBADA/WikiText and zero-shot avg (0.6B 55.99 vs Transformer 54.54; 1.7B 62.45 vs 61.43). Gains hold under parameter-matched and compute-matched controls. Under AdamW the correction branch collapses and the gap vanishes — first reported architecture–optimizer codesign for attention. Code https://github.com/Yifei-Zuo/Parallax.
- Code
xattn
Efficient attention modules for the xLLM framework, with Python APIs for use in other PyTorch projects. Features high-precision FP32 attention output state for backward (reducing BF16 gradient error) and flexible segment-aware masking (segment IDs or BOS masks) for packed sequences. Supports causal Flash Attention, sliding window, sliding chunk, and SoftDelta attention variants; FP16/BF16 inputs; MHA, GQA, and MQA.