Activation + GroupedLinear Fusion for MOE and other MOE optimizations#3238
Activation + GroupedLinear Fusion for MOE and other MOE optimizations#3238vthumbe1503 wants to merge 13 commits into
Conversation
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
for more information, see https://pre-commit.ci
Greptile SummaryThis PR introduces a fused
Confidence Score: 4/5Safe to merge for normal training configurations; the two new fused ops have no known correctness issues for the common path where both linear and activation contexts require gradients. The offset pre-computation refactor and the new fused activation+linear ops are structurally sound. The unfused fallback paths are preserved and the saved-tensor layout contract is consistently maintained across all callers. The only issues found are in low-traffic branches: input_requires_grad is always set True (wastes computation but not incorrect in the fully-fused path), and the activation scales tensor is not released after the backward runs. Files Needing Attention: forward_activation_grouped_linear.py and backward_activation_grouped_linear.py warrant a careful second look on the early-return branch (not linear_ctx.requires_grad) and the tensor-clear coverage. Important Files Changed
Sequence DiagramsequenceDiagram
participant FW as ForwardScaledActivationGroupedLinear
participant ACT as _grouped_scaled_activation (C++ kernel)
participant LIN as GroupedLinear._fuser_forward_grouped_tensor
participant BW as BackwardScaledActivationGroupedLinear
participant DACT as _grouped_scaled_dactivation (C++ kernel)
participant DLIN as GroupedLinear._fuser_backward_grouped_tensor
note over FW,LIN: Forward pass
FW->>FW: splits_to_offsets_multi → [split_pts, base_offs, in_offs, out_offs]
FW->>ACT: input_, scales, input_quantizer, split_sizes, in_offs
ACT-->>FW: grouped_x (quantized GroupedTensorStorage)
FW->>FW: activation_ctx.save_for_backward(input_, scales)
FW->>LIN: grouped_x, split_sizes, out_offs, ...
LIN-->>FW: out tensor + tensors_to_save (5-entry prefix layout)
FW->>FW: linear.fuser_forward_save_ctx(linear_ctx, tensors_to_save)
note over BW,DLIN: Backward pass
BW->>BW: read activation_ctx.saved_tensors → input_, scales
BW->>BW: read linear_ctx.saved_tensors[4] → output_tensor_offsets
BW->>DACT: grad_output, input_, scales, grad_output_quantizer, out_offs
DACT-->>BW: grouped_dy (quantized), dense_dy, grad_scales
BW->>DLIN: grouped_dy, dense_dy as grad_output
DLIN-->>BW: grad_input, grad_params, grad_extra_inputs
BW-->>BW: return grad_input, weight_grads, scale_grads
Reviews (7): Last reviewed commit: "Merge branch 'main' into grouped_linear_..." | 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 |
Description
Please include a brief summary of the changes, relevant motivation and context.
Fixes # (issue)
Type of change
Changes
Please list the changes introduced in this PR:
Checklist: