Repository navigation
[WS1][CUDA][MiniMax-H3] Add batch-invariant full attention - #553
Conversation
Add a strict BF16 CUDA path for MiniMax-H3 non-causal attention with bounded query-chunk workspace and deterministic QKV gradients. Register the backend, reference, validation coverage, documentation, and reproducible benchmark entry for the single-rank 56-head profile. Refs RL-Align#420 Signed-off-by: RichApple123 <[email protected]>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Comment |
Flink-ddd
left a comment
There was a problem hiding this comment.
please resolve the code conflicts first, Thank you.
Signed-off-by: RichApple123 <[email protected]>
|
Thanks for the review. I merged the latest test-h3 and resolved the registry conflict, preserving both operator registrations. The H3 suite passed all 36 tests on H20, along with the shared output/QKV-gradient checks. The PR is now mergeable, with the original 12-file scope unchanged. |
Flink-ddd
left a comment
There was a problem hiding this comment.
Thanks for resolving the conflicts. The implementation and test coverage look good to me.
Flink-ddd
left a comment
There was a problem hiding this comment.
cc @maxiaosong1124 PTAL, Thank you.
Signed-off-by: Wenbo Ji <[email protected]>
review only Signed-off-by: Wenbo Ji <[email protected]>
…full_attention Fixed-order Triton flash attention with the same scope and fail-closed contract as #553 (BF16 BSHD, 56x128, non-causal, first derivatives). Optional backend; does not modify or replace H3FullAttentionOp. Signed-off-by: Wenbo Ji <[email protected]>
…nd time the full model CandidateOps attention=auto uses H3FullAttentionOp when the strict _H3_C extension is built (bounded workspace, same #240 arithmetic) and the interim DeterministicAttentionOp otherwise; h3_full requires it, interim forces the old op. tools/validation/models/h3_full_speed.py times one full step. Signed-off-by: Wenbo Ji <[email protected]>
…and refiner The block and refiner graphs ran o_proj, ffn_gate_up and ffn_down on the interim FP32-K-loop GEMM. They now bind their rows' operators: H3AttentionOGemmOp and H3FFNGateUpGemmOp (#554) and H3FFNDownGemmOp (#535), with the rows' logical (batch row, position) keys and one parameter id per block weight. Weight gradients are reduced in a canonical backward session: the backward replay opens one per evaluation, full_model_execution one per context. The operators run on SM90 only. CandidateOps(gemm_rows=...) binds them on SM90 ("auto", the default), always ("row", raises elsewhere) or never ("interim"). The nodes' static binding is the row; h3_block.bindings(ops) reports per node what ran, and the block, refiner and full-model reports, the replay tool and the operator trace use it. Attention is reported the same way: H3FullAttentionOp (#553) when its extension loads, which the graph already ran, and the interim op otherwise. Signed-off-by: Wenbo Ji <[email protected]>
review only Signed-off-by: Wenbo Ji <[email protected]>
Implements the CUDA
h3_full_attentionwork item from #420 against the restructuredtest-h3branch.The existing strict attention path materializes quadratic score/probability tensors. This change bounds that workspace with query chunks, recomputes probabilities during backward, stages shared operands, and fuses dK/dV loads. Each output and gradient retains the #240 FP32 reduction order across chunk boundaries. The opt-in
_H3_Cextension isolates strict compiler flags from other operators.Scope: BF16 contiguous BSHD
[B,S,56,128], non-causal MHA, first derivatives, single GPU / TP1 / CP1. QK RMSNorm and partial three-axis RoPE remain caller-owned. Unsupported profiles fail closed. No full-model, multi-GPU, ROCm or double-backward acceptance is claimed.Reuse assessment
Reviewed FlashAttention, CUTLASS FMHA, FlashInfer, vLLM batch invariance, and SGLang's attention integration; also surveyed Miles, Verl and VIME at the framework/documentation level. These are source surveys, not qualification of every upstream implementation.
This contribution reuses RL-Kernel's strict attention arithmetic and adopts established tiling, shared-memory reuse and backward recomputation techniques. Preserving #240 bytes is this implementation's additional design constraint, not a universal requirement of batch invariance. Deterministic backward alone does not establish batch invariance; inference BI paths also require qualification of all QKV gradients for this task. A separately qualified full-backward BI implementation could use another arithmetic profile. No speed advantage over mainstream FlashAttention is claimed.
Validation
0.0038691 / 0.0019531 / 0.0039063 / 0.0039063.memcheck,racecheck,initcheck,synccheck: passed on the unchanged CUDA core.H20 performance
PyTorch 2.9.1+cu130, CUDA 13.0; eager forward + all QKV gradients, including layout copies and allocations. CUDA events, 2 warmups, median of 6 paired samples with rotated execution order. Candidate uses chunk128; peak memory is allocation above resident inputs. All outputs and gradients match the strict native baseline bitwise in this matrix.
At B1/S4096: 2.29x faster and 90.4% less extra peak allocation than strict native. Short S129 uses more extra memory; computation remains quadratic in sequence length.
xychart-beta title "H20 B1/S4096 forward + backward latency (ms)" x-axis ["Strict native", "CUDA chunk128"] y-axis "ms" 0 --> 1300 bar [1231.334, 537.786]xychart-beta title "B2/S129 maximum absolute error vs independent reference" x-axis ["Output", "dQ", "dK", "dV"] y-axis "Absolute error" 0 --> 0.005 bar [0.0038691, 0.0019531, 0.0039063, 0.0039063]Reproduction commands are documented in
docs/operators/attention.md; the existing benchmark acceptsh3 --output <directory>and records raw samples, source/binary hashes and backend provenance. Experimental logs and model weights are outside this patch.Refs #420.