Skip to content

[WS1][CUDA][MiniMax-H3] Add batch-invariant full attention - #553

Merged
maxiaosong1124 merged 2 commits into
RL-Align:test-h3from
RichApple123:feat/h3-full-attention
Oct 20, 2026
Merged

maxiaosong1124 merged 2 commits into
RL-Align:test-h3from
RichApple123:feat/h3-full-attention

Conversation

@RichApple123

Copy link
Copy Markdown

Implements the CUDA h3_full_attention work item from #420 against the restructured test-h3 branch.

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_C extension 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

  • Final H3 suite on NVIDIA H20: 36 passed. Includes independent FP32/FP64 accuracy, all QKV gradients, exact strict-native parity, repeat/batch/chunk invariance, checkpoint and stream behavior, registration and invalid-profile rejection.
  • Shared operator validation: output/dQ/dK/dV passed the repository's BF16 tolerances at B2/S129. Maximum absolute errors: 0.0038691 / 0.0019531 / 0.0039063 / 0.0039063.
  • Clean strict build, source/binary identity audit, and Compute Sanitizer memcheck, racecheck, initcheck, synccheck: passed on the unchanged CUDA core.
  • Local H3/runtime/contracts/operator regression: 475 passed, 22 skipped. Black, isort, flake8 and whitespace checks passed.
  • Broader GPU regression: 1111 passed, 30 skipped, 3 failed. The same three failures were reproduced on the unchanged upstream baseline: two existing attention-dispatch tests and the WS1 evidence test requiring the ordinary extension's SM90 GEMM build option. This is not a claim of a clean full-repository run.

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.

B / S Strict native (ms) CUDA (ms) Strict extra peak (GiB) CUDA extra peak (GiB)
1 / 129 1.423 0.892 0.0213 0.0281
1 / 512 19.870 8.866 0.1642 0.0948
1 / 1025 77.431 35.596 0.5481 0.1835
1 / 4096 1231.334 537.786 7.4384 0.7172
2 / 1024 156.330 68.651 1.0942 0.3661
4 / 1024 312.333 136.013 2.1884 0.7323

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]
Loading
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]
Loading

Reproduction commands are documented in docs/operators/attention.md; the existing benchmark accepts h3 --output <directory> and records raw samples, source/binary hashes and backend provenance. Experimental logs and model weights are outside this patch.

Refs #420.

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]>
@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4bd00267-69e4-492f-8cc6-dc4d66f20a2a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@Flink-ddd Flink-ddd added platform: cuda Specific optimizations or bugs in NVIDIA graphics cards (such as FlashInfer, TMA optimizations) new-model MiniMax-H3 WS1 labels Oct 13, 2026

@Flink-ddd Flink-ddd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please resolve the code conflicts first, Thank you.

@RichApple123

Copy link
Copy Markdown
Author

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 Flink-ddd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for resolving the conflicts. The implementation and test coverage look good to me.

@Flink-ddd Flink-ddd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @maxiaosong1124 PTAL, Thank you.

@maxiaosong1124 maxiaosong1124 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@maxiaosong1124
maxiaosong1124 merged commit 97a7ab9 into RL-Align:test-h3 Oct 20, 2026
2 checks passed
fusheng-ji added a commit to fusheng-ji/RL-Kernel that referenced this pull request Oct 22, 2026
fusheng-ji added a commit to fusheng-ji/RL-Kernel that referenced this pull request Oct 23, 2026
Flink-ddd pushed a commit that referenced this pull request Oct 25, 2026
…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]>
maxiaosong1124 pushed a commit that referenced this pull request Nov 9, 2026
…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]>
maxiaosong1124 pushed a commit that referenced this pull request Nov 25, 2026
…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]>
fusheng-ji added a commit to fusheng-ji/RL-Kernel that referenced this pull request Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

MiniMax-H3 new-model platform: cuda Specific optimizations or bugs in NVIDIA graphics cards (such as FlashInfer, TMA optimizations) WS1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants