Activation + GroupedLinear Fusion for MOE and other MOE optimizations - #3238
Activation + GroupedLinear Fusion for MOE and other MOE optimizations#3238vthumbe1503 wants to merge 17 commits into
Conversation
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
for more information, see https://pre-commit.ci
Greptile SummaryThis PR introduces three related MoE performance optimizations: (1) new C++ tex APIs for scaled activations (SReLU, SwiGLU, clamped SwiGLU) replacing multi-step PyTorch compositions; (2)
Confidence Score: 5/5Safe to merge. The core fusion logic — tensor-offset reuse, grouped-scaled activation dispatch, and the new backward fused op — is internally consistent and confirmed correct by existing tests including the new CUTEDSL=0 run. The forward and backward fused ops use precomputed tensor offsets that exactly match the layout recorded in forward, and the saved-tensor index contract is consistently documented across both the split-quantize and graph-safe paths. The C++ template helpers follow the existing TE extension pattern and pybind signatures match. The logic is stress-tested via the newly added NVTE_CUTEDSL_FUSED_GROUPED_MLP=0 test run that exercises the non-cuTe path end-to-end. Files Needing Attention: tests/pytorch/test_grouped_mlp.py: the act_grouped_linear_fusion_expected flag couples both forward and backward fusion assertions with a single AND of fc1+fc2 support; if they differ the assertion could produce a false negative, worth a follow-up. Important Files Changed
Sequence DiagramsequenceDiagram
participant Input
participant FwdFusion as ForwardScaledActivationGroupedLinear
participant GroupQ as GroupQuantize (tex.grouped_scaled_*)
participant FC2 as GroupedLinear FC2
participant BwdFusion as BackwardScaledActivationGroupedLinear
participant DActivation as _grouped_scaled_dactivation
participant FC1Bwd as FC1._fuser_backward_grouped_tensor
Note over Input,FC2: Forward pass
Input->>FwdFusion: pre-activation tensor + scales
FwdFusion->>GroupQ: compute + quantize activation (grouped_tensor_offsets[2])
GroupQ-->>FwdFusion: grouped_x (quantized)
FwdFusion->>FC2: grouped_x + split metadata
FC2-->>FwdFusion: output + saved_tensors[0..4]
FwdFusion-->>Input: output
Note over BwdFusion,FC1Bwd: Backward pass (fused fc1 + act)
BwdFusion->>DActivation: grad_output + saved input/scales
Note right of DActivation: tensor_offsets = linear_ctx.saved_tensors[4] (output_tensor_offsets from FC1)
DActivation-->>BwdFusion: grouped_dy (quantized), dense_dy, grad_scales
BwdFusion->>FC1Bwd: dense_dy (wgrad) + grouped_dy (dgrad)
FC1Bwd-->>BwdFusion: grad_input, grad_params, grad_extra_inputs
BwdFusion-->>BwdFusion: return grad_input, [fc1_grads, ()], [fc1_extra, (grad_scales,)]
Reviews (10): Last reviewed commit: "better name" | Re-trigger Greptile |
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
for more information, see https://pre-commit.ci
| ) | ||
|
|
||
|
|
||
| class ForwardScaledActivationGroupedLinear(FusedOperation): |
There was a problem hiding this comment.
There is a trade-off in creating a partially fused module, since we already have FC1 - ACT - FC2 fused module, We need to justify the value of creating a FC1-ACT fusion, instead of just adding features to the grouped_mlp instead.
There was a problem hiding this comment.
This essentially enables the infrastructure to enable (Act + GroupQuant from FC2) fusion, and enables to add fused kernels if possible in the future. cc: @timmoon10
There was a problem hiding this comment.
The other important thought i had in mind is the fact that ScaledActivation today doesnt take in m_splits and it is going to be a lot of upstream disruption to allow for that. And at the same time ScaledActivation can be used after Dense Layers as well and not necessarily after a GroupedLinear layer(and so it might not always need m_splits).
Allowing for this fusion, we allow the m_splits information to be also consumed in the scaled_activation + grouped quantization fusion. Right now the activation kernel that we are using isnt even using the m_splits, but for paged stashing optimization we might need that and we can potentially add a new kernel for that in the future.
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
for more information, see https://pre-commit.ci
|
/te-ci pytorch |
…ns for precomputed tensor offsets in backward Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
for more information, see https://pre-commit.ci
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
…ansformerEngine into grouped_linear_act_fusion
|
/te-ci pytorch |
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Description
Better Kernels for Scaled Activations
Activation + GroupQuantize Fusion
Precomputed Tensor Offsets Optimization
BF16 Grouped MLP Performance for num_groups=8 and swiglu activation
MXFP8 Grouped MLP performance for num_groups = 8 and swiglu activation
With CuteDSL fusions
Without CuteDSL fusions
Fixes #2988
Type of change
Changes
Please list the changes introduced in this PR:
Checklist: