Skip to content

Add BF16 grouped sReLU fusion with explicit numerical modes - #3937

Open
ZeYang1025 wants to merge 6 commits into
NVIDIA:mainfrom
ZeYang1025:codex/bf16-srelu-fusion
Open

ZeYang1025 wants to merge 6 commits into
NVIDIA:mainfrom
ZeYang1025:codex/bf16-srelu-fusion

Conversation

@ZeYang1025

@ZeYang1025 ZeYang1025 commented Nov 24, 2026 •

Copy link
Copy Markdown

Description

Add the BF16 grouped MLP path for weighted squared ReLU and tanh-clamped squared ReLU on SM100/SM103, using the cuDNN Frontend sReLU forward/backward wrappers.

The default is bf16_act_input="acc", bf16_tanh_mode="approx", and activation_recompute_in_mlp=False: FC2 wgrad consumes the actual forward FC2 input. Both activation-input modes, both tanh implementations, and save/recompute are supported. With saved plus recomputation, FC2 input is bitwise identical to forward under the same tanh configuration. With acc plus recomputation, backward reconstructs from the saved BF16 pre-activation and may change FC2 wgrad; the implementation does not change the selected input mode. This guarantee concerns FC2 input only, not whole-MLP or training equivalence to unfused BF16.

Changes

  • Share BF16 grouped MLP orchestration with the existing GLU path and add unary forward/backward adapters. Keep GLU's existing behavior and restrictions.
  • Support dense and per-expert BF16 weights, empty experts/ranks, main-grad accumulation, delayed wgrad, and CUDA graph capture through the shared path. Unary fusion requires no bias and the existing aligned-dimension/token contracts.
  • Pass TE's live determinism policy explicitly to dSReLU: deterministic dProb when NVTE_ALLOW_NONDETERMINISTIC_ALGO=0 or PyTorch deterministic algorithms are enabled; atomic otherwise.
  • Add the 32 numerical combinations in both weight layouts, independent numerical references, exact FC2-input/wgrad-source checks, reduction-policy replay, empty batches, delayed wgrad and graph tests.
  • Document defaults, numerical boundaries, capability checks, and fallback scope.

Depends on NVIDIA/cudnn-frontend#2884, NVIDIA/cudnn-frontend#2885 and NVIDIA/cudnn-frontend#2886, CuTe DSL >=4.8.0, and the existing canonical BF16 grouped GEMM/wgrad APIs. Capability detection checks the new wrapper options as well as the existing GEMM markers.

Downstream integration: NVIDIA/Megatron-LM#9756.

Validation

GB200 (SM100) and GB300 (SM103) validation on 2026-11-24 used TE revision 26cdc7807df304e0b91985ab81d425d89a011ebe, MCore e57410176cb54de70071745f443f0d7c441d8bc3, and cuDNN Frontend f73ade8a8f33300687d38d80823f8ea676515efe. Source revisions and Python/native import provenance were checked on both platforms. Reruns reused each platform's fresh cuDNN/TE build after verifying unchanged native sources and build settings. Environment: PyTorch 2.12.0a0 (NGC 26.04), CUDA 13.2, cuDNN 9.25, CuTe DSL 4.8.1, TVM FFI 0.1.12.

Passed on each platform, with zero skips in these suites:

  • TestGroupedMLPBf16SReLU: 89 tests, including the full numerical matrix, saved/recomputed FC2-input semantics, dProb policy, delayed/main-grad wgrad, empty experts and CUDA graphs.
  • TestGroupedMLPBf16FusedOp plus TestGroupedMLPDeterminism: 59 tests.
  • Downstream MCore BF16 mode integration: 32 tests.
  • Downstream deterministic EP1/EP2 replay: 16 tests on rank 0 and 16 on rank 1.
  • The isolated native GroupedLinear probe passes on both the PR and pinned main 7d0d5046f4013ad5789beb4744eef97b94f7eb0e: 1 test per revision.

The remaining tests/pytorch/test_grouped_mlp.py regression completed on each platform with 1,387 passed, 3,896 skipped, 0 failures/errors (5,283 collected cases, excluding the three separately tested BF16 sReLU/GLU/determinism classes). All 18 skip-reason categories exist in pinned main and cover unsupported or inapplicable quantization/dtype/layout/bias combinations and duplicate coverage. These skipped cases are not counted as passes. Both regression jobs exited 0.

The earlier native failure was caused by the harness's deprecated global NVTE_CUTEDSL_FUSED_GROUPED_MLP=1 flag forcing 256-token alignment on an ordinary 128-token GroupedLinear case. Removing that flag resolved both isolated probes and the broader regression; fused callers retain their explicit alignment.

Earlier runs on both platforms at TE 7522c2eb7ea94491c1b2da6f4163beebae5f0e50 also passed 30 standalone scaled sReLU/tanh-sReLU activation cases, license and diff checks. Revision 26cdc7807df304e0b91985ab81d425d89a011ebe adds only equivalent dictionary literals to those earlier runs; local pinned Black and AST equivalence checks passed.

Follow-up 929fe65f44c78ab1179c77d359484b6a3ae1bafd fixes the remaining W0613 by naming intentionally ignored activation forward keyword arguments _unused, following adjacent ops. The full Python lint launcher (PYTHON_ONLY=1 TE_PATH=<checkout> bash qa/L0_pytorch_lint/test.sh) passes locally with exit 0, using Python 3.12.15 and the repository-pinned pylint 4.1.3. Changed-file pre-commit also passes. An AST comparison confirms the only code change is that unused local parameter name; GPU suites were not repeated after this lint-only commit.

Validation limits:

  • Complete repository CI, including the full PyTorch L0 suite, and end-to-end training have not been run.

Focused GPU validation is complete on both platforms. The PR remains a draft for human review; full repository CI remains outstanding.

Update (2026-12-03): accurate-tanh wording and DSL gate

  • 88a81fbf (docs only): bf16_tanh_mode="accurate" is now described as the CUDA math library's FP32 tanhf, the function eager PyTorch uses for FP32 torch.tanh on CUDA. cuDNN Frontend's accurate mode switched to it in feat(gemm): add BF16 grouped sReLU forward cudnn-frontend#2885.
  • f99ae0c7: the fusion's capability check asks cuDNN Frontend's cutedsl_requirement_error, which reports the CuTe DSL whose files are actually loaded. It keeps the package-metadata check only for frontends without that function. In images where stale public nvidia-cutlass-dsl 4.6.0 metadata sits over the internal DSL that provides the imported cutlass (the Nemotron Ultra images), the old check made the fusion fall back to the unfused path without notice. The loaded-DSL attribution needs cuDNN Frontend with feat(gemm): add BF16 grouped sReLU forward cudnn-frontend#2885 at 2cd73412 or later.
  • Validation of TE f99ae0c7 with the feat(gemm): add BF16 grouped dSReLU backward cudnn-frontend#2886 head d43e68d2 (after its develop merge) on GB300/SM103, in the Nemotron Ultra image with its internal CuTe DSL (the gate reports nvidia-cutlass-dsl-internal 0.3.0): TestGroupedMLPBf16SReLU, TestGroupedMLPBf16FusedOp and TestGroupedMLPDeterminism 148 passed; MCore BF16 sReLU mode and config tests (-k "srelu or bf16_srelu") 67 passed; two-rank deterministic replay 16 + 16 passed; no skips. TE was built from 929fe65f in that image, with the two changed Python files copied over it. The pinned Black 24.4.2 with the repository's flags passes on both files. GB200/SM100 and the broader test_grouped_mlp.py regression were not rerun for this update.

Update (2026-12-04): skip the pre-activation store without autograd

ea56e483: when the BF16 sReLU forward runs without autograd (for example the no-grad pass of a recomputed expert MLP), TE passes generate_c=False to cuDNN Frontend's wrapper (NVIDIA/cudnn-frontend#2885), so the kernel neither allocates nor stores the pre-activation. Frontends without the option keep the previous call. New test test_forward_without_grad_skips_preactivation. With FE #2886 at 8673c274 on GB300 (Ultra image, internal CuTe DSL): TestGroupedMLPBf16SReLU, TestGroupedMLPBf16FusedOp and TestGroupedMLPDeterminism 149 passed, no skips. The forward kernel without the store is 3.5-3.8% faster at Ultra expert shapes.

Type of change

  • New feature
  • Documentation change
  • Code refactoring

Checklist

  • Read the contributing guidelines
  • Added relevant tests and documentation
  • Focused new and related GPU tests pass on GB200/GB300 (existing configuration skips documented above)
  • Human author has reviewed the complete diff before marking ready

Signed-off-by: Ze Yang <142831877+ZeYang1025@users.noreply.github.com>
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Nov 24, 2026
Signed-off-by: Ze Yang <142831877+ZeYang1025@users.noreply.github.com>
Use the existing _unused convention for intentionally ignored keyword arguments so pylint recognizes them.

Signed-off-by: Ze Yang <142831877+ZeYang1025@users.noreply.github.com>
@ZeYang1025
ZeYang1025 marked this pull request as ready for review November 24, 2026 22:03
@greptile-apps

greptile-apps Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The changes since the previous review appear safe to merge.

Summary

Adds BF16 grouped squared-ReLU fusion with explicit activation-input and tanh modes, saved or recomputed FC2 inputs, and deterministic probability gradients.

  • BF16 grouped MLPs fuse with scaled squared-ReLU activations.
  • Scaled squared-ReLU activations expose BF16 input and tanh choices.
  • sReLU probability gradients follow the live determinism policy.
  • Unused-argument and import spacing cleanup leaves behavior unchanged.

Reviews (3) · Last reviewed commit: "Skip the BF16 sReLU pre-activation store..." · Reviewed by Greptile

ZeYang1025 and others added 3 commits December 3, 2026 09:36
cuDNN Frontend's BF16 sReLU accurate mode now evaluates libdevice tanhf, the
function eager PyTorch uses for FP32 torch.tanh on CUDA.

Signed-off-by: Ze Yang <142831877+ZeYang1025@users.noreply.github.com>

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Package metadata can name a stale public nvidia-cutlass-dsl while an internal
DSL distribution provides the imported cutlass (as in the Nemotron Ultra
images), which made the fusion fall back to the unfused path. Ask cuDNN
Frontend's gate (cutedsl_requirement_error) and keep the metadata check only
for frontends without it.

Signed-off-by: Ze Yang <142831877+ZeYang1025@users.noreply.github.com>

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A forward that runs without autograd, such as the no-grad pass of a
recomputed expert MLP, never reads the pre-activation. When cuDNN Frontend's
BF16 sReLU wrapper accepts generate_c, pass generate_c=False there, so the
kernel neither allocates nor stores it. Older frontends keep the previous call.

Signed-off-by: Ze Yang <142831877+ZeYang1025@users.noreply.github.com>

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution PRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant