Skip to content

Support canonical/natrual gate_then_up SwiGLU weights in cuDNN grouped MLP fusion - #3851

Open
zhongbozhu wants to merge 24 commits into
NVIDIA:mainfrom
zhongbozhu:codex/canonical-glu-grouped-gemm-pr
Open

zhongbozhu wants to merge 24 commits into
NVIDIA:mainfrom
zhongbozhu:codex/canonical-glu-grouped-gemm-pr

Conversation

@zhongbozhu

@zhongbozhu zhongbozhu commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Depends on

Blackwell NVIDIA/cudnn-frontend#2455 & NVIDIA/cudnn-frontend#3086
Rubin NVIDIA/cudnn-frontend#2926

Fixes # (issue)

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Please list the changes introduced in this PR:

  • Change A
  • Change B

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Nov 11, 2026
@zhongbozhu
zhongbozhu force-pushed the codex/canonical-glu-grouped-gemm-pr branch from 96cb2f1 to 8ebd628 Compare November 11, 2026 04:17
@zhongbozhu zhongbozhu changed the title Support canonical SwiGLU weights in cuDNN grouped MLP fusion Support canonical/natrual gate_then_up SwiGLU weights in cuDNN grouped MLP fusion Nov 11, 2026
@zhongbozhu
zhongbozhu force-pushed the codex/canonical-glu-grouped-gemm-pr branch 3 times, most recently from de72640 to 7ae8f7f Compare November 11, 2026 12:00
@zhongbozhu
zhongbozhu force-pushed the codex/canonical-glu-grouped-gemm-pr branch from 7ae8f7f to 04bdbd5 Compare November 16, 2026 19:53
@zhongbozhu
zhongbozhu marked this pull request as ready for review November 16, 2026 20:05
@greptile-apps

greptile-apps Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge; no new actionable defects were found.

Summary

Adds canonical [gate | up] SwiGLU weights to the cuDNN grouped MLP fused path.

  • SwiGLU can fuse grouped MLPs with gate-then-up weights.
  • The grouped MLP benchmark compares layouts across precisions and timing modes.
  • The cross-entropy test keeps its package imports in a new order.

Previous-thread context:

  • zhongbozhu rejected the tile-test adjustment because the test was over-defensive. The test and benchmark tile control are absent from the current changes.
  • zhongbozhu explained that regular and specialized canonical UE5M3 support landed together. Greptile withdrew that finding; the current test also checks the requested scale format.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[GroupedLinear + ScaledSwiGLU + GroupedLinear] --> B{Weight layout}
  B -->|Interleaved blocks of 32| C[Existing fusion checks]
  B -->|Gate then up| D{Dimensions and backend supported?}
  D -->|No| E[Separate operations]
  D -->|Yes| F[Canonical fused GLU]
  F --> G{Specialized RHT supported?}
  G -->|Yes| H[Fuse RHT]
  G -->|No| I[Keep RHT separate]
  C --> J[FC2 and backward]
  H --> J
  I --> J
Loading

Reviews (22) · Last reviewed commit: "Merge branch 'main' into codex/canonical..." · Reviewed by Greptile



def validate_grouped_mlp_dims(fc1, activation_op, fc2) -> None:
def _cudnn_kernel_supports_gate_then_up(kernel, precision: str) -> bool:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It may be helpful to memoize this function. That said, it's only called during op fusion so it's not perf critical.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

keeping the old version because caching it might involves the question when to disable cache, since it's not in the critical path, it's okay to keep

Comment thread tests/pytorch/test_grouped_mlp.py Resolved
Comment thread tests/pytorch/test_grouped_mlp.py Resolved
Comment thread tests/pytorch/test_grouped_mlp.py Resolved
Comment thread tests/pytorch/test_grouped_mlp.py Resolved
@zhongbozhu
zhongbozhu force-pushed the codex/canonical-glu-grouped-gemm-pr branch from 4ee0be9 to a766166 Compare November 24, 2026 07:40
@zhongbozhu

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L1

@vthumbe1503 vthumbe1503 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. We should test this with cudnn 1.34 after it is released, before getting the PR merged.

Comment thread tests/pytorch/test_grouped_mlp.py Resolved
@zhongbozhu

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L1

@vthumbe1503

Copy link
Copy Markdown
Collaborator

Pipeline against CUDNN nightly build: 85258106

@vthumbe1503

Copy link
Copy Markdown
Collaborator

grouped mlp test generally take a lot of time due to the compilation of cute dsl kernels. We should try not to duplicate these cases. Here is what the agent told me

  These are the changes I would recommend:

  - Remove four genuinely duplicate cases. In test_canonical_glu_fusion_selection, custom_precision only matters when interleave_size is None. For layouts 32 and 64, both precision values execute identical checks. Split
    the custom-recipe portion or use explicit cases: 12 → 8 cases. See tests/pytorch/test_grouped_mlp.py:1253.

  - Consolidate repeated training scenarios. Of the 32 cases in test_glu_layout_training, five ordinary interleaved numerical configurations already exist in test_grouped_mlp; another interleaved NVFP4 accumulation/
    delayed-wgrad configuration exists in test_grouped_mlp_mcore_integrations. Preserve the additional wrapper assertions by adding them to existing coverage rather than rerunning those configurations. Do not remove all
    interleaved cases: unquantized-weight and bias-plus-delayed-wgrad combinations add coverage. See tests/pytorch/test_grouped_mlp.py:1320 and tests/pytorch/test_grouped_mlp.py:3066.

  - Make the RHT matrix more purposeful—and assert the optimized path. For interleave32, removing only the gate_then_up capability leaves dispatch unchanged, so two cases repeat baseline execution. They are useful only if
    intentionally checking capability independence. More importantly, supported RHT cases currently accept any forward wrapper: disabling RHT+quant fusion could still pass through ordinary GLU plus separate operations.
    Require fwd_hadamard_quant for the supported unit-scale recipe, while retaining fallback assertions for unsupported cases. See tests/pytorch/test_grouped_mlp.py:1421 and tests/pytorch/test_grouped_mlp.py:1474.

  - Keep the genuinely different contracts. Canonical CUDA-graph coverage, scalar activation scaling, sharded-weight rematerialization, and external main_grad refresh exercise different failure modes. Shared setup can be
    consolidated, but they are not interchangeable tests.

  Bottom line: trim repeated parameter combinations and numerical runs; retain legacy-API coverage and strengthen assertions that the intended optimized wrapper actually runs.

@zhongbozhu

Copy link
Copy Markdown
Collaborator Author

@vthumbe1503 I agree on the duplicate test cases part, maybe it can be simplified a bit.

On the RHT comment, I did that on purpose because I believe whether the RHT + quant fusion is enabled or not, it's an implementation detail which varies based on cudnn-fe version installed and I just want to check the correct numerics for this.

Comment thread tests/pytorch/test_grouped_mlp.py Resolved
@zhongbozhu

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L1

1 similar comment
@zhongbozhu

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L1

@zhongbozhu
zhongbozhu force-pushed the codex/canonical-glu-grouped-gemm-pr branch from 7b469ec to 9cd9efa Compare December 2, 2026 11:06
Skip input-unit-scale NVFP4 RHT+quant coverage before cuDNN FE 1.33.0, which includes the column-scale packing fix in NVIDIA/cudnn-frontend#2484. Allow ordinary RHT training to use either the fused kernel or the subsequent quantizer while retaining numerical and layout checks.

Signed-off-by: zhongboz <[email protected]>
Disable the affected fusion before FE 1.33.0, where columnwise RHT scale packing corrupts weight gradients. Preserve the existing separate RHT/quantization fallback and both GLU layouts.

Signed-off-by: Zhongbo Zhu <[email protected]>
Check that canonical GLU training bypasses the fused RHT wrapper when its required layout capability is missing, retaining numerical and layout checks.

Signed-off-by: Zhongbo Zhu <[email protected]>
Require architecture-specific cuDNN Frontend capabilities and consume preferred launch tiles. Preserve fallback with older FE and retain unsupported deterministic and UE5M3 limits.

Validate on SM107 with the Rubin FE branch: 1506 passed, 3958 skipped across the complete grouped-MLP test file; 163 focused tests also passed. Extend existing capability/fallback coverage and assert actual RHT fused dispatch.

Signed-off-by: Zhongbo Zhu <[email protected]>
Temporarily remove the current-device tile hint so an explicit benchmark tile takes precedence, and restore the original hints on exit. Remove TE tests that prescribe concrete frontend tile choices; retain fusion and numerical coverage.

Validated Python syntax, Black formatting, and diff checks. No production dispatch or cuDNN FE changes; GPU tests were not rerun for this follow-up.

Signed-off-by: Zhongbo Zhu <[email protected]>
Require explicit FE scale-format capabilities for canonical GLU and dGLU. Qualify RHT+quant independently, preserve second-level scaling, and use separate RHT when the fused wrapper cannot interpret UE5M3. Keep the cuBLAS version requirement on cuBLAS paths.

Extend numerical, gradient, changed-input graph, old-capability fallback and standard-recipe coverage. FE-only strict-fusion tests check their actual backend prerequisites; graph references copy parameters without pickling CustomRecipe factories.

Validated on SM107 / CUDA 13.4 / CuTe DSL 4.8.0 with matching FE UE5M3 changes: 120 passed, 4 existing NVFP4 packed-weight skips. Python compilation, Black and git diff checks passed. No new performance or distributed qualification.

Signed-off-by: Zhongbo Zhu <[email protected]>
Pass the UE5M3 override to the specialized wrapper capability check before injecting missing capabilities. Preserve the dedicated fallback test and all production code.

Validated six isolated CPU prerequisite cases using the actual AST and capability helper, plus Python compilation, Black and diff checks. GPU numerical tests were not rerun for this test-only change.

Signed-off-by: Zhongbo Zhu <[email protected]>
@zhongbozhu
zhongbozhu force-pushed the codex/canonical-glu-grouped-gemm-pr branch from 9cd9efa to fcaf8e9 Compare December 3, 2026 00:12
Comment thread tests/pytorch/test_grouped_mlp.py Resolved
Comment thread tests/pytorch/test_grouped_mlp.py Resolved
Comment thread tests/pytorch/test_grouped_mlp.py Resolved
Comment thread tests/pytorch/test_grouped_mlp.py Resolved
Comment thread tests/pytorch/test_grouped_mlp.py Resolved

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is already a fairly expensive test suite, and adding a bunch of tests to handle unsupported cuDNN FE versions is not an effective use of test time. If we really want to catch problems with old CFE versions, it would be better to have an L2 or L3 test that runs test_grouped_mlp.py with multiple CFE releases (like the L3 test for Flash Attention versions). This would be more realistic and would not slow down everyday CI pipelines.

Comment thread tests/pytorch/test_grouped_mlp.py Resolved
@zhongbozhu

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L1

FC2 takes the counts and the same weights for its bias. ``expert_out`` is already
weighted; the subsequent combine must not apply the routing weights again.

The uninterleaved fused path requires an FFN dimension divisible by 128. With

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.

note that this would not work on GPT OSS with hidden dim 2880. cc: @vasunvidia

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

they can always do interleave if they want, also gpt-oss is very old

Comment on lines +1015 to +1048
and get_device_compute_capability()
in getattr(kernel, "supports_gate_then_up_archs", ((10, 0), (10, 3)))
and (
sf_fp8_dtype_override is None
or (
precision == "nvfp4"
and get_device_compute_capability() == (10, 7)
and sf_fp8_dtype_override
in getattr(kernel, "supports_gate_then_up_sf_fp8_dtype_overrides", ())
)
)
)


def _cudnn_frontend_supports_gate_then_up(
precision: str = "mxfp8", sf_fp8_dtype_override=None
) -> bool:
"""Whether both GLU kernels accept canonical gate/up storage in a precision.
``supports_canonical_layouts`` describes tensor ranks and strides, not GLU
channel order. New gate/up fusion requires both capabilities; legacy
interleave32 callers retain their existing tensor-layout compatibility.
"""
if get_device_compute_capability() not in ((10, 0), (10, 3), (10, 7)):
return False
try:
from cudnn import ( # pylint: disable=import-outside-toplevel,no-name-in-module
grouped_gemm_glu_wrapper_sm100,
grouped_gemm_dglu_wrapper_sm100,
)
except ImportError:
return False
return all(
_cudnn_kernel_supports_gate_then_up(kernel, precision, sf_fp8_dtype_override)

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.

cudnn 1.34 is released. So most of this conditions can just be abstracted out based on cudnn 1.34 gating right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I don't prefer specific version gating either, once we bump up cudnn-fe version in TE packages, all of them can be removed

@vthumbe1503 vthumbe1503 Dec 4, 2026 •

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.

This is more CPU overheads. Atleast version gating is cleaner. And also easier to clean up
\

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

the rubin PR couldn't make it till the 1.34 release

if not unit_activation_scale:
if unit_activation_scale and kernel_impl != "gemm_act":
# The specialized RHT wrappers require an explicit probability tensor.
fc1_prob_tensor = get_cached_ones_tensor(in_shape[0], torch.float32, device)

@vthumbe1503 vthumbe1503 Dec 4, 2026 •

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.

Shouldnt we allow the RHT kernels in shared expert to also handle this and not having ot create ones tensor? cc: @sraman-rgb. Not a blocker for this PR

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think that's just an api design choice that folks want to keep kernel impl general

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.

there was an optimization where we dont have to pass the unit scales to the kernel. And the kernel actually performed better in case of shared experts. That is the reason unit_activation_scale was introduced as per my understanding. Otherwise we could have passed the ones tensor directly from Mcore into MLP, no need to handle here.

@sraman-rgb is my understanding right?

if use_nvfp4:
# NVFP4 stores the physical transpose, packed along GEMM K.
fc2_w_data = fc2_w_data.view(
1, fc2_weight_shape[1], fc2_weight_shape[0] // 2

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.

Why is this extra 1 needed at the start by the cudnn kernel? If it is the normal (fc2_weight_shape[1], fc2_weight_shape[0]//2) then we potentially dont even need to do these views. We can just handle it in grouped tensor creation of weights.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

this is only triggered under group=1 case so that you are using grouped mlp but for shared expert

for single weight + routed expert, there is no need to do so, for discrete weight, it's also not needed

@zhongbozhu zhongbozhu Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

although there is some room to improve cpu overhead here as well, but more like a issue in main already

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

fixed in commit 3c9be3c

Keep Hadamard wrapper tracing and expected call dictionaries consistent
with sf_fp8_dtype_override. Mock the compute capability in the recipe
selection test so its result does not depend on the CI GPU architecture.

Signed-off-by: zhongboz <[email protected]>
Import transformer_engine.pytorch before transformer_engine_torch so
package initialization can register the native extension when it is not
directly discoverable on the Python module search path.

Signed-off-by: zhongboz <[email protected]>
@zhongbozhu

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L1

@timmoon10 timmoon10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. I look forward to the glorious future where we propagate similar changes to the rest of the cuDNN Frontend and uninterleaved GLU becomes the default.


import transformer_engine_torch as tex
import transformer_engine.pytorch as te
import transformer_engine_torch as tex

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This fixes an orthogonal test failure introduced by #3758.

@zhongbozhu

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L1

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.

4 participants