Skip to content

[HIP] [Activation] Add standalone relu2 (Squared ReLU) elementwise kernel for non-gated MoE MLPs - #5689

Merged
yzhou103 merged 6 commits into
ROCm:mainfrom
shantipriya-amd:feature/relu2-activation
Sep 23, 2026
Merged

yzhou103 merged 6 commits into
ROCm:mainfrom
shantipriya-amd:feature/relu2-activation

Conversation

@shantipriya-amd

@shantipriya-amd shantipriya-amd commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

NemotronH's non-gated MoE MLP layers use Squared ReLU (relu(x)^2) as their activation function. aiter currently only implements gated activations (silu_and_mul, gelu_and_mul, gelu_tanh_and_mul) and a plain (non-gated) gelu_fast, but has no plain Squared ReLU op.

Improving performance for the NVIDIA-Nemotron-3-Ultra-550B-A55B-BF16 model requires enabling relu2, fused MoE GEMM support, and vLLM AITER backend integration. This PR's scope is limited to enabling relu2.

This PR adds a standalone relu2 elementwise kernel so non-gated MoE architectures like NemotronH can use aiter's fused activation path instead of falling back to eager PyTorch.

This is Tier 1 of a 3-PR series adding end-to-end relu2 support:

  1. This PR standalone aiter elementwise kernel (no dependencies)
  2. aiter CK-Tile fused MoE GEMM support for relu2 (depends on this PR). The draft PR is ready.
  3. vLLM AITER backend integration (depends on the Tier 2 PR above)

Note: For end-to-end results and performance, refer to the related Tier 2 and Tier 3 PRs.

Technical Details

Mirrors the existing gelu_fast pattern end-to-end:

  • Declared the relu2(out, input) op signature alongside the other elementwise activations
  • Implemented relu2_kernel<T> (r = max(x, 0); return r * r, computed in fp32) and its launcher, using the same vectorized launch macro as the other elementwise activations
  • Added explicit tensor validation (GPU, contiguous, matching numel/dtype/device) before dispatch
  • Added the pybind11 binding so the op is exposed to Python
  • Added the Python-side @compile_ops(...) stub (JIT-compiled on first call, same as all other activation ops)
  • Added test_relu2 plus a driver loop matching the existing test_gelu_fast benchmark pattern

No changes to gated/MoE-fused activation paths, this is a plain elementwise op only.

Test Plan

Validated on MI350 (gfx950):

  • Correctness: compared aiter.relu2(out, x) against a torch.nn.functional.relu(x.float())**2 reference across 4 shapes:
    (1024,4096), (1024,8192), (4096,4096), (4096,8192), bf16 dtype.
  • Performance: benchmarked aiter.relu2 vs. the same eager PyTorch reference across the same 4 shapes.

Test Result

M N aiter_us torch_us speedup max_abs_diff result
1024 4096 8.038 22.051 2.74x 0.000000 PASS
1024 8192 7.858 31.297 3.98x 0.000000 PASS
4096 4096 13.158 55.536 4.22x 0.000000 PASS
4096 8192 21.431 107.907 5.04x 0.000000 PASS

Exact-match correctness (max_abs_diff: 0.0) and 2.74x–5.04x speedup
vs. eager PyTorch across all tested shapes.

Submission Checklist

Essential Elements of an Effective PR Description Checklist

  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • [] (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model. (N/A -- no user-facing docs/config affected by this internal dispatch fix)

@shantipriya-amd
shantipriya-amd requested review from a team and a lite review from Copilot September 19, 2026 23:39
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
multigpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 5689 --add-label <label>

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to opt this PR out.

@github-actions github-actions Bot changed the title [Activation] Add standalone relu2 (Squared ReLU) elementwise kernel for non-gated MoE MLPs [HIP] [Activation] Add standalone relu2 (Squared ReLU) elementwise kernel for non-gated MoE MLPs Sep 19, 2026
@github-actions github-actions Bot added the HIP label Sep 19, 2026

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

ActivationType::Relu2 is added in the C++ enum but is not exported in the core pybind ActivationType enum (module_aiter_core), so ActivationType.Relu2 won’t actually be available from Python as implied.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Medium severity

Open (2)
What changed in this PR

Adds a standalone elementwise Squared ReLU (relu(x)^2) activation kernel and exposes it through the existing HIP activation module plumbing, primarily to support non-gated MoE MLPs (e.g., NemotronH) without falling back to eager PyTorch.

Changes:

  • Implemented a new vectorized relu2 HIP kernel (fp32 math, cast back to input dtype) and its launcher.
  • Added C++/pybind and Python compile_ops stubs to expose relu2 to Python.
  • Added a test_relu2 benchmark + summary reporting alongside the existing activation benchmarks.
File Description
op_tests/​test_activation.py Adds relu2 benchmark + PyTorch reference and reporting.
csrc/​kernels/​activation_kernels.cu Implements relu2_kernel<T> and relu2 launcher using the existing vectorized activation macro.
csrc/​include/​rocm_ops.hpp Exposes relu2(out, input) via pybind in the activation bindings.
csrc/​include/​aiter_enum.h Adds ActivationType::Relu2 enum value.
csrc/​include/​activation.h Declares relu2(out, input) C++ API.
aiter/​ops/​activation.py Adds Python @compile_ops stub for relu2.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread csrc/include/aiter_enum.h Resolved
Comment thread op_tests/test_activation.py Resolved
Copilot AI review requested due to automatic review settings September 20, 2026 00:02

Copilot AI 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.

Copilot review overview

🟢 Approval recommended

The new relu2 path follows existing gelu_fast patterns end-to-end (C++ kernel + binding + Python stub + benchmark) without introducing unresolved correctness, API, or integration issues in the reviewed changes.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
Resolved since last review (1)

@zufayu
zufayu requested review from yzhou103 and zufayu September 20, 2026 01:11
@shantipriya-amd

Copy link
Copy Markdown
Contributor Author

@amd-ruitang3 : Could you review this PR when you free ? It adds a standalone relu2 (Squared ReLU) activation kernel for NemotronH's non-gated MoE MLPs — Tier 1 of a 3-part enablement effort. CI is green and the Copilot review comments have already been addressed. Thanks!

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Exporting ActivationType.Relu2 introduces an API surface that is not yet consistently handled by existing ActivationType dispatch/mapping code paths (risking runtime errors and/or switch-enum build warnings).

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Medium severity

Open (2)

Comment thread csrc/include/rocm_ops.hpp Resolved
Copilot AI review requested due to automatic review settings September 21, 2026 13:34
@github-actions github-actions Bot changed the title [HIP] [Activation] Add standalone relu2 (Squared ReLU) elementwise kernel for non-gated MoE MLPs [HIP] [CK] [Activation] Add standalone relu2 (Squared ReLU) elementwise kernel for non-gated MoE MLPs Sep 21, 2026
@github-actions github-actions Bot added the CK label Sep 21, 2026

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

The new relu2 launcher lacks required shape/dtype/contiguity/device validation and can misbehave or write out of bounds if called with a mismatched out tensor.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity · 1 Medium severity

Open (2)
Resolved since last review (1)

Comment thread csrc/kernels/activation_kernels.cu Resolved
Copilot AI review requested due to automatic review settings September 21, 2026 14:47

Copilot AI 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.

Copilot review overview

🟢 Approval recommended

The relu2 kernel is implemented and wired end-to-end (C++/pybind/Python/test) with appropriate safety checks and a clear fused-path guardrail.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
Resolved since last review (1)

@yzhou103 yzhou103 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.

please fix the code style check

Copilot AI review requested due to automatic review settings September 22, 2026 08:50
@github-actions github-actions Bot changed the title [HIP] [CK] [Activation] Add standalone relu2 (Squared ReLU) elementwise kernel for non-gated MoE MLPs [HIP] [Activation] Add standalone relu2 (Squared ReLU) elementwise kernel for non-gated MoE MLPs Sep 22, 2026
@github-actions github-actions Bot removed the CK label Sep 22, 2026

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

There is at least one confirmed correctness/API concern (NaN semantics in relu2_kernel and an exported enum value that isn’t handled by downstream activation switches), which should be resolved before approval.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Medium severity

Open (2)
Previously missed (1)

In code that hasn't changed since last review

Medium severity relu2_kernel fmaxf masks NaN inputs instead of preserving them

csrc/​kernels/​activation_kernels.cu:2072

relu2_kernel uses fmaxf(f, 0.0f), which does not propagate NaNs (fmaxf(NaN, 0) returns 0). PyTorch ReLU preserves NaNs, so this can produce divergent results for NaN inputs and silently mask them. Use a comparison-based clamp that preserves NaNs instead of fmaxf.

Comment thread csrc/include/rocm_ops.hpp Resolved
Comment thread csrc/include/aiter_enum.h Resolved
Copilot AI review requested due to automatic review settings September 22, 2026 09:29

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

The PR description claims ActivationType::Relu2 was added, but the current branch does not define/export that enum value, so the implementation and stated scope are out of sync.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
Resolved since last review (2)

Comment thread csrc/include/rocm_ops.hpp Resolved
Copilot AI review requested due to automatic review settings September 22, 2026 12:12

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

The PR description claims ActivationType::Relu2 was added/exposed, but the repo state shown lacks that enum value and its pybind export, creating a concrete mismatch that will block enum-based relu2 selection in follow-on work.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Medium severity

Open (2)

Comment thread csrc/include/rocm_ops.hpp Resolved
@shantipriya-amd

Copy link
Copy Markdown
Contributor Author

@yzhou103 : Code issues and CI has been resolved. May I request to review once and suggest for any update ?.

@yzhou103
yzhou103 merged commit 4927f8f into ROCm:main Sep 23, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants