Skip to content

[Common] Support scaled & clamped swiglu, srelu for BF16 - #3132

Merged
vthumbe1503 merged 18 commits into
NVIDIA:mainfrom
zhongbozhu:add_support_fused_swiglu
Jul 14, 2026
Merged

[Common] Support scaled & clamped swiglu, srelu for BF16 #3132
vthumbe1503 merged 18 commits into
NVIDIA:mainfrom
zhongbozhu:add_support_fused_swiglu

Conversation

@zhongbozhu

@zhongbozhu zhongbozhu commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Description

Support Mega-C++ with Cublas BF16 Grouped GEMM backend: #3099

Kernel Perf:
image
image
image

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 Jun 16, 2026
@zhongbozhu
zhongbozhu marked this pull request as ready for review June 16, 2026 07:32
@zhongbozhu
zhongbozhu requested a review from ptrendx as a code owner June 16, 2026 07:32
@greptile-apps

greptile-apps Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds new CUDA kernels (scaled_swiglu, scaled_clamped_swiglu, scaled_srelu) that fuse the activation with a per-row scale multiplication and optionally compute the scale gradient in a single pass, supporting BF16/FP16/FP32 data and scale types, including an interleaved GLU layout for compatibility with grouped-GEMM backends.

  • Kernel design: Four templated host launchers (launch_scaled_gated_forward/backward, launch_scaled_unary_forward/backward) instantiated for (Empty, silu), (ClampedSwiGLUParam, clamped_silu), and (Empty, srelu). The backward kernels use a one-block-per-row scheme with a shared-memory warp reduction to accumulate grad_act_scales when requested; ComputeScaleGrad is a compile-time template flag that gates the reduction path.
  • Vectorization: row_vector_alignment checks pointer alignment and falls back to scalar (nvec=1) when the segment size or pointers are not aligned; the glu_interleave_size % 32 validation ensures vector paths are always reachable for standard interleave values.
  • Test coverage: A new test_scaled_activation.cu exercises forward + backward for all three activations across BF16/FP32/FP16 data types, contiguous and interleaved GLU layouts, and optional scale-gradient computation.

Confidence Score: 5/5

The change is purely additive — new source files, a new public header section, and new tests — with no modifications to existing kernels or data paths. The math is correct and the forward/backward formulas are consistent with each other and with the test references.

All new launch code is guarded by early-return dimension checks and type-dispatch macros; the reduction kernel uses correct smem sizing bounded by kReductionThreads. The one finding (ClampedSwiGLU reference hardcoding alpha) is a test-coverage concern rather than a runtime bug.

test_scaled_activation.cu: the ClampedSwiGLU backward reference should explicitly thread kClampedAlpha through qgelu/dqgelu calls rather than relying on the 1.702f default.

Important Files Changed

Filename Overview
transformer_engine/common/activation/scaled_activation.cu Core kernel file implementing gated and unary forward/backward kernels for scaled activations; math is correct and block-reduce smem sizing is consistent with kReductionThreads.
transformer_engine/common/activation/scaled_activation.h Header declaring launch templates and shared utilities; alignment fallback handles non-vectorizable cases cleanly.
transformer_engine/common/activation/scaled_swiglu.cu Thin wrapper dispatching to launch_scaled_gated_forward/backward for SwiGLU and ClampedSwiGLU; all API functions correctly wired.
transformer_engine/common/activation/scaled_srelu.cu Thin wrapper dispatching to launch_scaled_unary_forward/backward for SReLU; correct and minimal.
transformer_engine/common/include/transformer_engine/activation.h Six new C API declarations added inside extern C; documentation is thorough and parameter semantics match the implementation.
tests/cpp/operator/test_scaled_activation.cu New test suite covering all three activations across BF16/FP32/FP16 types and interleaved layouts. ClampedSwiGLU reference hardcodes alpha=1.702f via test::qgelu rather than using kClampedAlpha explicitly.
transformer_engine/common/CMakeLists.txt New source files added to both standard and fast-math build lists symmetrically.
tests/cpp/operator/CMakeLists.txt test_scaled_activation.cu added to the test executable correctly.

Reviews (9): Last reviewed commit: "[pre-commit.ci] auto fixes from pre-comm..." | Re-trigger Greptile

Comment thread tests/cpp/operator/test_scaled_activation.cu
@zhongbozhu

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

@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. Left minor comments

Comment thread transformer_engine/common/activation/scaled_activation.cu Outdated
Comment thread transformer_engine/common/activation/scaled_activation.cu Outdated
Comment thread tests/cpp/operator/test_scaled_activation.cu Outdated
Comment thread transformer_engine/common/include/transformer_engine/activation.h Outdated
Comment thread tests/cpp/operator/test_scaled_activation.cu Outdated
Comment thread tests/cpp/operator/test_scaled_activation.cu Outdated
Comment thread tests/cpp/operator/test_scaled_activation.cu
Comment thread tests/cpp/operator/test_scaled_activation.cu Outdated
Comment thread transformer_engine/common/activation/scaled_activation.cu Outdated
Comment thread transformer_engine/common/activation/scaled_activation.cu Outdated
Comment thread transformer_engine/common/activation/scaled_activation.cu Outdated
* kernels with nvec=1, i.e. regular elementwise loads/stores.
*/

#include <transformer_engine/activation.h>

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.

A general comment - I think that limiting this to only FP32/FP16 is not needed and you could very
easily have tensor-scaled FP8 support here.

@zhongbozhu zhongbozhu Jul 9, 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.

I agree but it's gonna be challenging to extend to blockwise scaling (when it comes to GLU interleaved gated activation, and non-gated activation should be fine), and I do think that for fp8 fp4, we do expect a fused gemm to be built so it's directly fused into the GEMM epilogue.

zhongbozhu and others added 7 commits July 9, 2026 11:54
Signed-off-by: zhongboz <zhongboz@nvidia.com>
Signed-off-by: Zhongbo Zhu <zhongboz@nvidia.com>
Signed-off-by: Zhongbo Zhu <zhongboz@nvidia.com>
Signed-off-by: Zhongbo Zhu <zhongboz@nvidia.com>
Signed-off-by: Zhongbo Zhu <zhongboz@nvidia.com>
Signed-off-by: vthumbe1503 <vthumbe@nvidia.com>
@zhongbozhu
zhongbozhu force-pushed the add_support_fused_swiglu branch from 60e5ecb to c8c19a9 Compare July 9, 2026 18:55
zhongbozhu and others added 8 commits July 9, 2026 14:01
Signed-off-by: Zhongbo Zhu <zhongboz@nvidia.com>
Signed-off-by: Zhongbo Zhu <zhongboz@nvidia.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
@greptile-apps

greptile-apps Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

@vthumbe1503

Copy link
Copy Markdown
Collaborator

/te-ci

@vthumbe1503

Copy link
Copy Markdown
Collaborator

/te-ci

@vthumbe1503
vthumbe1503 merged commit 215e15e into NVIDIA:main Jul 14, 2026
10 of 16 checks passed
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