Skip to content

SM120 batch invariance under deterministic algorithms (stacked on #509) - #510

Merged
RayWang96 merged 2 commits into
deepseek-ai:nv_devfrom
lucifer1004:sm120-batch-invariant
Nov 19, 2026
Merged

RayWang96 merged 2 commits into
deepseek-ai:nv_devfrom
lucifer1004:sm120-batch-invariant

Conversation

@lucifer1004

@lucifer1004 lucifer1004 commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

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_splits picks 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.
  • With few MN blocks and a bounded workspace (decode), the splits run as real split-K.
  • Otherwise the GEMM kernel emulates them (kNumEmulatedSplits, new in DeepGEMM-sm120 v0.3.0). It accumulates each split's K range from zero and adds the partials in the order sm120_split_k_reduce does, so it gives bitwise the same result as real split-K, without the workspace. The producer is unchanged.
  • sm120_split_k_reduce adds 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.py passes against its manifest.

Commits:

  • 2bf766d stops splitting K under deterministic algorithms and adds tests/test_sm120_batch_invariance.py.
  • e737f91 splits 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:

    • 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;
    • TF32 HC prenorm.

    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.py passes 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 (2bf766d by itself) costs 2-3x on small-M, large-K shapes.

🤖 Generated with Claude Code

@lucifer1004
lucifer1004 marked this pull request as ready for review November 19, 2026 05:34
@lucifer1004
lucifer1004 force-pushed the sm120-batch-invariant branch from d6054e1 to 4a06f78 Compare November 19, 2026 05:39
lucifer1004 and others added 2 commits November 18, 2026 21:54
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants