Repository navigation
SM120 batch invariance under deterministic algorithms (stacked on #509) - #510
Merged
Merged
Conversation
lucifer1004
marked this pull request as ready for review
November 19, 2026 05:34
lucifer1004
force-pushed
the
sm120-batch-invariant
branch
from
November 19, 2026 05:39
d6054e1 to
4a06f78
Compare
Deterministic mode is also batch invariant (as for the SM100 Mega Gate). The SM120 FP8/FP4 GEMM heuristic splits K when there are fewer MN blocks than half the SMs, so whether K is split depends on M, and a split K is summed in a different order: a row's result depended on the batch it was computed in. Under `use_deterministic_algorithms(True)`, never split K. Swap-AB, BLOCK_M, BLOCK_N and BLOCK_K still vary with M; they do not change a row's result, because every kernel accumulates K in its MMA accumulators in the same order. test_sm120_batch_invariance.py computes the same rows in batches of 1 to 4096 and requires bitwise-equal results, for every API DeepInfer calls on SM120: dense FP8 GEMM (with and without accumulation), BF16 GEMM, M-grouped contiguous FP8xFP4 and BF16 GEMM (psum layout, varying rows per expert), `bhr,hdr->bhd` FP8 einsum, and TF32 HC prenorm. Without this change, 9 of the 10 dense FP8 cases fail. Cost: for small-M, small-N, large-K shapes, disabling split-K is 2-3x slower (e.g. N=512, K=7168, M<=512: 4-17 us -> 10-37 us); shapes that do not split are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous commit stops splitting K under deterministic algorithms, which makes small-M, small-N, large-K GEMMs (decode) 2-3x slower. Split K again, with splits that do not depend on M: - `get_batch_invariant_num_splits` picks the number of K splits from the weight's N (`desc.m` under swap-AB) and K only, as the split-K heuristic would for one block row. Every split is a multiple of both BLOCK_K candidates (64, 128) and of the packed SF tile (4 * gran_k), so every layout splits K at the same points. - With few MN blocks and a bounded workspace, those splits run as real split-K; otherwise the GEMM kernel emulates them (`kNumEmulatedSplits`): each tile accumulates every split's K range from zero and adds the partials in turn, in the order `sm120_split_k_reduce` sums them, so both give bitwise the same result. The producer is unchanged: it loads every K block in order either way. - `sm120_split_k_reduce` now adds and scales with `__fadd_rn` and `__fmul_rn`, so the alpha scale is never contracted into the C add. The emulated kernels use the same registers as the unsplit ones (168 at launch, 232 for the math warps; no new spills). On the DeepSeek-V4.1-Flash FP8 linear shapes, M from 1 to 8192, deterministic mode takes 0.82-1.28x the time of the default heuristics (1.00-1.06x at M >= 2048), against 2-3x without split-K. test_sm120_batch_invariance.py passes: small batches split for real and large ones emulate the same splits. The two device files match DeepGEMM-sm120 v0.3.0 (release/v0.3.0, 6aa68ae). Co-Authored-By: Claude Opus 5.5 <[email protected]>
lucifer1004
force-pushed
the
sm120-batch-invariant
branch
from
November 19, 2026 06:03
4a06f78 to
e737f91
Compare
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
Makes SM120 GEMMs batch invariant under
deep_gemm.use_deterministic_algorithms(True): every row gets bitwise the same result whatever batch (M) it is computed in. DeepInfer's batch-invariant mode relies on this (as on SM100, where deterministic mode keeps the Mega Gate from splitting K).On SM120, only split-K changes a row's result. Every kernel accumulates K in its MMA accumulators in one order whatever BLOCK_M, BLOCK_N, BLOCK_K and swap-AB the heuristics pick. But the FP8/FP4 GEMM heuristic splits K when there are fewer MN blocks than half the SMs, so whether and how K is split depends on M.
Under deterministic algorithms the number of K splits now depends on the weight's N and K only:
get_batch_invariant_num_splitspicks the splits the split-K heuristic would pick for one block row. Every split is a multiple of both BLOCK_K candidates and of the packed SF tile, so every layout splits K at the same points.kNumEmulatedSplits, new in DeepGEMM-sm120 v0.3.0). It accumulates each split's K range from zero and adds the partials in the ordersm120_split_k_reducedoes, so it gives bitwise the same result as real split-K, without the workspace. The producer is unchanged.sm120_split_k_reduceadds and scales with__fadd_rn/__fmul_rn, so the alpha scale is never contracted into the C add.Default (non-deterministic) heuristics are unchanged. The two device files match DeepGEMM-sm120 v0.3.0;
check_vendor.pypasses against its manifest.Commits:
2bf766dstops splitting K under deterministic algorithms and addstests/test_sm120_batch_invariance.py.e737f91splits K again with M-independent splits, real or emulated, which removes the slowdown the first commit causes.Validation
SM120 (RTX PRO 6000 Blackwell), CUDA 13.2, torch 2.13.0+cu132.
tests/test_sm120_batch_invariance.py(23 cases) computes the same rows in batches of 1 to 4096 and requires bitwise-equal results, for every API DeepInfer calls on SM120:bhr,hdr->bhdFP8 einsum;Without these commits, 9 of the 10 dense FP8 cases fail.
The full SM120 suite passes: 735 tests.
Registers: the emulated kernels use the same registers as the unsplit ones (168 at launch, 232 for the math warps after
setmaxnreg), with no new spills.End to end: DeepInfer's
tests/models/test_batch_invariant.pypasses on 8 GPUs (DeepSeek-V4.1-Flash, EP8, DSpark speculative decoding with 5 draft tokens). It runs 64 sequences per DP rank, batched and one at a time, and requires identical tokens, logprobs and top-5 logprobs.Cost: on the DeepSeek-V4.1-Flash FP8 linear shapes, M from 1 to 8192, deterministic mode takes 0.82-1.28x the time of the default heuristics, and 1.00-1.06x at M >= 2048. Disabling split-K alone (
2bf766dby itself) costs 2-3x on small-M, large-K shapes.🤖 Generated with Claude Code