Skip to content

[Common/PyTorch/JAX] make offset of ClampedSwiGLU configurable#2938

Open
hxbai wants to merge 11 commits into
NVIDIA:mainfrom
hxbai:swiglu_offset
Open

[Common/PyTorch/JAX] make offset of ClampedSwiGLU configurable#2938
hxbai wants to merge 11 commits into
NVIDIA:mainfrom
hxbai:swiglu_offset

Conversation

@hxbai
Copy link
Copy Markdown
Contributor

@hxbai hxbai commented Apr 28, 2026

Description

The previous ClampedSwiGLU follows GPT-OSS, which hard-coded the offset 1.0.
DeepSeek-V4 uses ClampedSwiGLU without alpha and offset.
This PR makes the offset of ClampedSwiGLU configurable to support DeepSeek-V4.

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

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 28, 2026

Greptile Summary

This PR makes the glu_linear_offset parameter of ClampedSwiGLU configurable (default 1.0, matching existing GPT-OSS behavior) to support DeepSeek-V4, which uses glu_linear_offset=0.0. The approach introduces nvte_clamped_swiglu_v2 / nvte_clamped_dswiglu_v2 C API functions to avoid breaking the public ABI while adding the new parameter across all CUDA kernels, PyTorch, and JAX code paths.

  • C layer: Adds glu_linear_offset to ClampedSwiGLUParam; all kernels (vectorized_pointwise.h, gated_fp8.cuh, gated_mxfp8.cuh) now read p.glu_linear_offset instead of the hardcoded 1.0f/1.
  • Python/JAX: ClampedSwiGLU, ScaledClampedQGeGLU, LayerNormMLP, and JAX extension params all accept and propagate the new argument with the correct backward-compatible default.
  • Fusion guard: fuse_grouped_mlp_ops correctly skips the grouped-MLP fusion for non-default glu_linear_offset values (consistent with the pre-existing alpha guard).

Confidence Score: 5/5

Safe to merge; the change is purely additive with a correct backward-compatible default of 1.0.

All CUDA kernels, PyTorch bindings, and JAX FFI handlers correctly propagate the new parameter. The old public C symbols are preserved unchanged. The fusion guard is consistent with the pre-existing alpha guard.

No files require special attention; all changed files are internally consistent.

Important Files Changed

Filename Overview
transformer_engine/common/util/math.h Adds glu_linear_offset field (default 1.0f) to ClampedSwiGLUParam struct; backward-compatible and correctly scoped.
transformer_engine/common/util/vectorized_pointwise.h Both forward and backward kernels now use p.glu_linear_offset instead of hardcoded 1.0f; mathematically correct since offset is a constant in the gradient.
transformer_engine/common/cast/fp8/gated_fp8.cuh Uses p.glu_linear_offset for the linear gate shift; derivative of clamp is offset-independent so dgate_elt logic is unchanged and correct.
transformer_engine/common/cast/mxfp8/gated_mxfp8.cuh Both MXFP8 kernel variants updated to use p.glu_linear_offset; consistent with the FP8 changes.
transformer_engine/common/include/transformer_engine/activation.h Adds nvte_clamped_swiglu_v2 and nvte_clamped_dswiglu_v2 with the offset parameter; old symbols deprecated but preserved, avoiding ABI breakage.
transformer_engine/common/activation/swiglu.cu Old functions hard-code glu_linear_offset=1.0f for backward compat; new _v2 functions pass the configurable value.
transformer_engine/pytorch/ops/_common.py Extends the fusion guard to skip grouped-MLP fusion when glu_linear_offset differs from default; consistent with the pre-existing alpha guard.
transformer_engine/pytorch/ops/basic/swiglu.py Adds glu_linear_offset to ClampedSwiGLU and ScaledClampedQGeGLU; correctly threads it through forward and backward tex calls.
transformer_engine/jax/cpp_extensions/activation.py Updates ClampedSwigluParams dataclass: adds field, updates __hash__ and to_ffi_lowering_dict; clamped_linear lambda uses the configurable offset correctly.
tests/pytorch/test_fusible_ops.py Parametrizes glu_linear_offset over (1.0, 0.0) for both test functions, directly covering the DeepSeek-V4 use case.
tests/jax/test_custom_call_compute.py Uses glu_linear_offset=0.5; exercises a non-default path but does not test the specific 0.0 value that is the stated DeepSeek-V4 use case.

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

Comment on lines 339 to 341
* \param[in] glu_linear_offset Offset added to the linear component after clamping (default 1.0).
* \param[in] stream CUDA stream used for the operation.
*/
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.

P1 Breaking public C API change

nvte_clamped_swiglu and nvte_clamped_dswiglu are public symbols declared in a versioned public header. Inserting glu_linear_offset before cudaStream_t is an ABI-breaking change: any external binary or shared library compiled against the old header will silently pass the stream pointer as the offset and a garbage value as the stream, leading to undefined behavior at runtime rather than a clean compile error if called via a pre-compiled library. This should be acknowledged as a breaking change in the PR checklist, and — if this library follows semantic versioning or a compatibility guarantee — a deprecation/transition path or version bump is needed.

Copy link
Copy Markdown
Collaborator

@timmoon10 timmoon10 left a comment

Choose a reason for hiding this comment

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

The fused op for grouped MLP is hard-coded for GPT-OSS, so we should make sure not to fuse if glu_linear_offset != 1:

elif isinstance(window[1], ScaledClampedQGeGLU) and (
abs(window[1]._clamped.alpha - 1.702) > 0.001
or not _nvidia_cudnn_frontend_supports_scaled_clamped_qgeglu()
):

@timmoon10
Copy link
Copy Markdown
Collaborator

/te-ci

Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
@hxbai hxbai marked this pull request as draft April 29, 2026 00:28
Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
@hxbai hxbai marked this pull request as ready for review April 29, 2026 01:01

void nvte_clamped_swiglu(const NVTETensor input, NVTETensor output, float limit, float alpha,
cudaStream_t stream) {
float glu_linear_offset, cudaStream_t stream) {
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.

Can we define new APIs named nvte_clamped_swiglu_v2 and nvte_clamped_dswiglu_v2
and deprecate this API here to not break backward compatibility?

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.

rewrited this part

vthumbe1503 and others added 3 commits May 6, 2026 11:38
Signed-off-by: vthumbe1503 <vthumbe@nvidia.com>
Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
@vthumbe1503
Copy link
Copy Markdown
Collaborator

/te-ci

hxbai added 2 commits May 12, 2026 15:13
Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
@vthumbe1503
Copy link
Copy Markdown
Collaborator

/te-ci

Copy link
Copy Markdown
Collaborator

@jberchtold-nvidia jberchtold-nvidia left a comment

Choose a reason for hiding this comment

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

Overall looks pretty good from the JAX side, thanks for adding the JAX changes too! Left a couple small comments

::xla::ffi::StructMember<float>("limit"),
::xla::ffi::StructMember<float>("alpha"));
::xla::ffi::StructMember<float>("alpha"),
::xla::ffi::StructMember<float>("glu_linear_offset"));
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.

can we add a default value for users on HLO from a previous version? Would glu_linear_offset=1 be the same as the current behavior on main?

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.

Yes, glu_linear_offset=1 is consistent with the current behavior.

Could you point me on how to add the default value on HLO? Thanks.

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.

@hxbai So I had thought this was easy to add a default value for, but I realized it's a different case where it's a function argument attribute, not a struct field, where we have supported default values in XLA FFIs in TE/JAX previously.

I reached out to the XLA team and heard using std::optional may be supported. Can you try this?

struct XXXX {
  ...
  std::optional<float> glu_linear_offset;
};

then when using the value glu_linear_offset_value = glu_linear_offset.value_or(1.0f)

If it doesn't work, then let me know we can keep it without a default and I'll approve the PR from the JAX side. Thanks!

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.

Added the fix

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.

Tests failed due to the changes. It seems optional is not supported and I reverted. Is it OK?

Comment thread transformer_engine/jax/cpp_extensions/activation.py
Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
@jberchtold-nvidia
Copy link
Copy Markdown
Collaborator

/te-ci

hxbai and others added 2 commits May 16, 2026 06:38
Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 16, 2026

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants