Repository navigation
Support canonical/natrual gate_then_up SwiGLU weights in cuDNN grouped MLP fusion - #3851
zhongbozhu wants to merge 24 commits into
Conversation
96cb2f1 to
8ebd628
Compare
gate_then_up SwiGLU weights in cuDNN grouped MLP fusion
de72640 to
7ae8f7f
Compare
7ae8f7f to
04bdbd5
Compare
Confidence Score: 5/5
[Medium impact] The PR appears safe to merge; no new actionable defects were found. SummaryAdds canonical
Previous-thread context:
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
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: |
There was a problem hiding this comment.
It may be helpful to memoize this function. That said, it's only called during op fusion so it's not perf critical.
There was a problem hiding this comment.
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
4ee0be9 to
a766166
Compare
|
/te-ci pytorch L1 |
vthumbe1503
left a comment
There was a problem hiding this comment.
LGTM. We should test this with cudnn 1.34 after it is released, before getting the PR merged.
|
/te-ci pytorch L1 |
|
Pipeline against CUDNN nightly build: 85258106 |
|
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 |
|
@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. |
|
/te-ci pytorch L1 |
1 similar comment
|
/te-ci pytorch L1 |
7b469ec to
9cd9efa
Compare
Signed-off-by: zhongboz <[email protected]>
Signed-off-by: zhongboz <[email protected]>
Signed-off-by: zhongboz <[email protected]>
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]>
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]>
Signed-off-by: zhongboz <[email protected]>
9cd9efa to
fcaf8e9
Compare
There was a problem hiding this comment.
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.
Signed-off-by: Zhongbo Zhu <[email protected]>
Signed-off-by: Zhongbo Zhu <[email protected]>
Signed-off-by: Zhongbo Zhu <[email protected]>
Signed-off-by: Zhongbo Zhu <[email protected]>
|
/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 |
There was a problem hiding this comment.
note that this would not work on GPT OSS with hidden dim 2880. cc: @vasunvidia
There was a problem hiding this comment.
they can always do interleave if they want, also gpt-oss is very old
| 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) |
There was a problem hiding this comment.
cudnn 1.34 is released. So most of this conditions can just be abstracted out based on cudnn 1.34 gating right?
There was a problem hiding this comment.
I don't prefer specific version gating either, once we bump up cudnn-fe version in TE packages, all of them can be removed
There was a problem hiding this comment.
This is more CPU overheads. Atleast version gating is cleaner. And also easier to clean up
\
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
I think that's just an api design choice that folks want to keep kernel impl general
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
although there is some room to improve cpu overhead here as well, but more like a issue in main already
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]>
Signed-off-by: zhongboz <[email protected]>
|
/te-ci pytorch L1 |
timmoon10
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
This fixes an orthogonal test failure introduced by #3758.
|
/te-ci pytorch L1 |
Description
Depends on
Blackwell NVIDIA/cudnn-frontend#2455 & NVIDIA/cudnn-frontend#3086
Rubin NVIDIA/cudnn-frontend#2926
Fixes # (issue)
Type of change
Changes
Please list the changes introduced in this PR:
Checklist: