Add batch-invariant h_aggregate kernel - #366
Open
nodeeeeee wants to merge 3 commits into
Open
Conversation
feat(cuda): promote deterministic cross-config runtime and kernel validation to main
nodeeeeee
requested review from
Flink-ddd,
KJLdefeated,
bitborne and
inaniloquentee
as code owners
August 30, 2026 13:32
|
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 configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Comment |
Collaborator
|
Thanks for your contribution! Upon a quick scan, please add:
after that i will review the new files by each line. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a CUDA implementation of the MHC H Aggregate operation:
For every token and hidden position, the kernel computes:
The change only covers
mhc_pre / h_aggregate. It does not modify Sinkhorn,GEMM, RMSNorm, MHC Post, or residual RMSNorm.
Batch invariance
The arithmetic path is fixed for every output element:
__fmul_rnand__fadd_rnoperations;(0 + 1) + (2 + 3)addition tree;Even hidden sizes use
__nv_bfloat162loads and stores. Odd hidden sizes use ascalar fallback. The two values in each BF16 pair are still calculated
independently.
Correctness
tests/test_mhc_pre_h_aggregate.pydirectly callsrl_engine._C.mhc_pre_h_aggregateand checks:T=129batch and 129 one-token calls produce byte-identical output.atol=5e-2, rtol=1e-2.T=129intentionally exercises different launch configurations: the fullbatch uses 512 threads per block, while each one-token call uses 1024 threads.
This verifies that changing the batch partition does not change output bytes.
Additional H100 checks covered
(T, H)values from(1, 4096)through(1024, 7168)and the odd-size scalar fallback(3, 1279). All cases werebyte-identical across 100 calls and matched the PyTorch reference. The maximum
absolute difference from the original TileLang stage was
0.015625.Performance
Environment: NVIDIA H100 80GB HBM3, Torch
2.13.0+cu129, CUDA toolkit12.8,BF16 residual/output, and FP32 pre/accumulator. The baseline is the original
TileLang H Aggregate stage. Timing used CUDA Graph replay, cold L2, and the
FlashInfer timing helper with its CUDA Event fallback because CUPTI was not
available.
Model run
The model-level test uses Hugging Face
DeepseekV4ForCausalLMwith random BF16weights. The only architectural size change is
num_hidden_layers, from 43 to1; the retained layer keeps the official hidden size, attention dimensions,
MHC expansion, and MoE dimensions from
deepseek-ai/DeepSeek-V4-Flash. Theresulting model has
7,635,547,771 parameters and 14.22 GiB of BF16 weights.
The model and test live in the vLLM workspace rather than this repository:
The test replaces both MHC collapse sites inside the decoder layer and the
final HyperHead collapse with
rl_engine._C.mhc_pre_h_aggregate.The model test verified:
The full-batch and per-sample model logits were not byte-identical and had a
maximum absolute difference of
0.03125. The isolated H Aggregate test isbyte-identical under the same partition change, so this model-level drift comes
from other batch-sensitive operations in the complete layer.
This is an integration test, not a model-quality evaluation. The official
DeepSeek V4 Flash checkpoint has hundreds of billions of parameters and cannot
run on the single 80GB H100 test machine.
Commands and results
The extension build completed successfully:
The lightweight RL-Kernel operator test passed:
The heavier model run was executed from the vLLM repository:
cd /home/ubuntu/vllm RL_KERNEL_ROOT=/home/ubuntu/RL-Kernel \ .venv/bin/python tests/models/run_deepseek_v4_mhc_h_aggregate.py