Skip to content

TanH Sq-relu and SiTU GLU - #7003

Merged
abhinav-khattar merged 11 commits into
NVIDIA:mainfrom
abhinav-khattar:tanh-activations
Sep 3, 2026
Merged

abhinav-khattar merged 11 commits into
NVIDIA:mainfrom
abhinav-khattar:tanh-activations

Conversation

@abhinav-khattar

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do?

TanH Sq-relu and SiTU GLU activations

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact @NVIDIA/mcore-oncall.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue:

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment @NVIDIA/mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge your PR.

@abhinav-khattar
abhinav-khattar requested review from a team as code owners August 31, 2026 22:48
@copy-pr-bot

copy-pr-bot Bot commented Aug 31, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft August 31, 2026 22:48
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically converted to draft because all PRs must start as drafts.

When you are ready for review, click Ready for Review to begin the review process. This will:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@abhinav-khattar
abhinav-khattar marked this pull request as ready for review September 1, 2026 18:20
@abhinav-khattar
abhinav-khattar requested a review from a team as a code owner September 1, 2026 18:20
@wdykas

wdykas commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

/claude review

@@ -996,26 +998,41 @@
bias_parallel is None
), "Bias is not supported with fused weighted squared relu."
intermediate_parallel = weighted_squared_relu_impl(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what's the plan for fused kernel support when enabling use_transformer_engine_op_fuser?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we may be silently dropping the tanh part here when going through this path?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think with use_transformer_engine_op_fuser are constructing ScaledSwiGLU or ScaledSReLU without reading either tanh scale.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

edited to disable use_transformer_engine_op_fuser if tanh_clamp is set. we can update once we have the cudnn fe kernel ready


def _get_activation_func(activation_type: ActivationType, fused_quant: bool = False) -> Callable:
def _get_activation_func(
activation_type: ActivationType, fused_quant: bool = False, clamp_scale: Optional[float] = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we have **activation_kwargs as a generic interface to pass activation function specific args like clamp_scale?

@claude

claude Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Light review. Two findings.

1. activation_func_tanh_clamp_scale is silently ignored on the TE op-fuser paths

The PR is careful to hard-fail on the inference backends that can't express the clamp (_flashinfer_forward assert, vllm_fused_moe assert, _get_activation_func NotImplementedError), but the two training TE op-fuser paths have no such guard and will silently train the wrong activation:

  • TEGroupedMLP._make_fused_ops (megatron/core/transformer/moe/experts.py:551-627) builds ScaledSwiGLU / ScaledClampedQGeGLU / ScaledSReLU purely from activation_func + activation_func_clamp_value. None of those TE ops take a tanh clamp scale, so with use_transformer_engine_op_fuser=True the clamp / SiTU-GLU is dropped and the forward runs plain SwiGLU or squared-ReLU.
  • FusedSharedExpertMLP._make_fused_grouped_swiglu_ops (megatron/core/transformer/moe/shared_experts.py:509-516) has the same problem under use_grouped_gemm_for_shared_expert=True.

The new __post_init__ validation in transformer_config.py rejects use_te_activation_func and unsupported bias_activation_fusion, but doesn't cover either of these flags. Suggest extending the validation block (~transformer_config.py:2634):

if self.use_transformer_engine_op_fuser:
    raise ValueError(
        "activation_func_tanh_clamp_scale is not supported with "
        "use_transformer_engine_op_fuser; the TE ScaledSwiGLU / ScaledClampedQGeGLU / "
        "ScaledSReLU fused ops have no tanh clamp."
    )
if self.use_grouped_gemm_for_shared_expert:
    raise ValueError(
        "activation_func_tanh_clamp_scale is not supported with "
        "use_grouped_gemm_for_shared_expert; FusedSharedExpertMLP builds TE "
        "ScaledSwiGLU / ScaledClampedQGeGLU, which cannot apply the clamp."
    )

(Or add the checks to TEGroupedMLP._is_fused_impl_supported / _validate_fused_grouped_swiglu if you'd rather keep the failure local to the modules.)

2. No test coverage for the training-side kernels

All five new tests exercise inference Triton kernels (test_moe_permute.py, test_vllm_fused_moe.py). The training-side additions are untested:

  • fused_bias_swiglu.py: situ_glu, bias_situ_glu, weighted_situ_glu and their hand-derived backwards (_situ_glu_grads, situ_glu_back, bias_situ_glu_back, weighted_situ_glu_back).
  • fused_weighted_squared_relu.py: weighted_clamped_squared_relu + weighted_clamped_squared_relu_back.
  • The new __post_init__ validation errors.

The hand-written gradients are the highest-risk part of the diff — dy_1 = (sech2 + c * (1 - sig)) * sig * lin * g is easy to get subtly wrong and nothing here would catch it. tests/unit_tests/fusions/test_swiglu_fusion.py and tests/unit_tests/fusions/test_weighted_squared_relu_fusion.py already have the right shape for this: compare the fused impl (fwd + bwd) against an autograd reference built from megatron.core.activations.situ_glu / squared_relu(tanh_soft_clamp(x, s)), plus a clamp_scale=None case asserting bit-identity with the existing unclamped path. test_swiglu_fusion.py::test_clamped_swiglu_config_* is likewise the pattern to follow for the new config validation.

.to(intermediate_parallel.dtype)
)

def _is_fused_impl_supported(self) -> bool:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please change this function accordingly if needed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

@wdykas wdykas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I left a few comments but dont want my review to block anything so will put the accept here assuming everything is addressed

out_dtype = x.dtype
t = _tanh_relu_over_scale(x, clamp_scale)

c = (clamp_scale * t).to(out_dtype)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is odd down casting I am assuming like other things is for training and inference parity? we might want to note it

@abhinav-khattar abhinav-khattar Sep 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

its to ensure the fused/unfused paths are identical, since the unfused path does a cast to out_dtype after the tanh activation which is a separate activation function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed the intermediate downcasts

@@ -996,26 +998,41 @@
bias_parallel is None
), "Bias is not supported with fused weighted squared relu."
intermediate_parallel = weighted_squared_relu_impl(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we may be silently dropping the tanh part here when going through this path?

@@ -996,26 +998,41 @@
bias_parallel is None
), "Bias is not supported with fused weighted squared relu."
intermediate_parallel = weighted_squared_relu_impl(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think with use_transformer_engine_op_fuser are constructing ScaledSwiGLU or ScaledSReLU without reading either tanh scale.

@santhnm2

santhnm2 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

/ok to test 49cd794

abhinav-khattar and others added 6 commits September 1, 2026 15:15
Signed-off-by: Abhinav Khattar <akhattar@nvidia.com>
Signed-off-by: Abhinav Khattar <akhattar@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Abhinav Khattar <akhattar@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Abhinav Khattar <akhattar@nvidia.com>
@abhinav-khattar

Copy link
Copy Markdown
Contributor Author

/ok to test fa69213

Signed-off-by: Abhinav Khattar <akhattar@nvidia.com>
@abhinav-khattar

Copy link
Copy Markdown
Contributor Author

/ok to test d40cecb

Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
@santhnm2

santhnm2 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

/ok to test 68e6005

@abhinav-khattar
abhinav-khattar added this pull request to the merge queue Sep 3, 2026
@nemo-automation-bot

Copy link
Copy Markdown

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/33740494110

Merged via the queue into NVIDIA:main with commit 9d8183b Sep 3, 2026
102 of 103 checks passed
@abhinav-khattar
abhinav-khattar deleted the tanh-activations branch September 3, 2026 11:18

This branch was previously deployed

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

Labels

Approved All necessary approvals have been made complexity: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants