Skip to content

[feat] Add gemma RMS AR fusion - #3322

Merged
jiahanc merged 4 commits into
flashinfer-ai:mainfrom
jiahanc:AddGemmaRMSARFusion
May 19, 2026
Merged

jiahanc merged 4 commits into
flashinfer-ai:mainfrom
jiahanc:AddGemmaRMSARFusion

Conversation

@jiahanc

@jiahanc jiahanc commented May 14, 2026

Copy link
Copy Markdown
Collaborator

📌 Description

Verified from framework side. See perf at vllm-project/vllm#42646

Add weight bias to RMS norm AR fusion to support gemma and qwen3.5 RMS

🔍 Related Issues

🚀 Pull Request Checklist

Thank you for contributing to FlashInfer! Before we review your pull request, please make sure the following items are complete.

✅ Pre-commit Checks

  • I have installed pre-commit by running pip install pre-commit (or used your preferred method).
  • I have installed the hooks with pre-commit install.
  • I have run the hooks manually with pre-commit run --all-files and fixed any reported issues.

If you are unsure about how to set up pre-commit, see the pre-commit documentation.

🧪 Tests

  • Tests have been added or updated as needed.
  • All tests are passing (unittest, etc.).

Reviewer Notes

Summary by CodeRabbit

  • New Features

    • Added a GPU benchmark to compare fused vs unfused AllReduce+RMSNorm performance.
    • Introduced an optional weight_bias parameter to AllReduce-fusion APIs to support alternate RMSNorm scaling.
  • Tests

    • Added a distributed correctness test for Gemma/Qwen3.5-style RMSNorm AllReduce fusion.
    • Extended AllReduce fusion tests to validate weight_bias variants (e.g., 0.0 and 1.0).
  • Documentation

    • Updated reference traces and docstrings to describe weight_bias behavior.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 14, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Adds a weight_bias parameter to AllReduce+RMSNorm fusion: CUDA kernels and params accept the bias, Python APIs and custom-op wrappers thread it through, trace/reference code updated, tests parameterized and a distributed benchmark added.

Changes

AllReduce Fusion weight_bias Support

Layer / File(s) Summary
CUDA kernel and device-level RMSNorm updates
include/flashinfer/comm/trtllm_allreduce_fusion.cuh, include/flashinfer/comm/trtllm_mnnvl_allreduce.cuh, include/flashinfer/comm/trtllm_moe_allreduce_fusion.cuh, csrc/trtllm_allreduce_fusion.cu, csrc/trtllm_mnnvl_allreduce.cu, csrc/trtllm_moe_allreduce_fusion.cu
AllReduceFusionParams gains weight_bias/weightBias; kernel signatures and device helpers extended to accept it; RMSNorm output scaling changed to use (weight_bias + gamma) instead of gamma.
Python API threading and custom-op registration
flashinfer/comm/allreduce.py, flashinfer/comm/trtllm_ar.py, flashinfer/comm/trtllm_mnnvl_ar.py
Public allreduce_fusion(...) adds weight_bias; custom-op wrappers and deprecated TRTLLM wrappers accept and forward weight_bias to underlying module/custom-op calls.
Trace templates and reference implementation
flashinfer/trace/templates/comm.py
Reference _allreduce_fusion_reference and trace schema accept weight_bias and compute RMSNorm scaling with (weight_bias + rms_gamma) for kARResidualRMSNorm.
Test coverage for weight_bias across fusion paths
tests/comm/test_allreduce_fusion_moe_unified_api.py, tests/comm/test_trtllm_allreduce_fusion.py, tests/comm/test_gemma_ar_fusion.py
MOE unified and legacy tests parameterized over weight_bias (0.0, 1.0); reference RMSNorm implementations updated; new distributed Gemma RMSNorm fusion correctness test added.
Distributed benchmark for fused vs unfused AllReduce+RMSNorm
benchmarks/bench_gemma_ar_fusion.py
MPI/NCCL benchmark script compares fused allreduce_fusion(..., kARResidualRMSNorm) against unfused all-reduce + gemma_fused_add_rmsnorm, aggregating median timings across ranks and printing speedups.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

op: norm, benchmark

Suggested reviewers

  • yzh119
  • bkryu
  • nv-yunzheq
  • sricketts
  • jimmyzho
  • dhiraj113
  • aleozlx
  • yongwww

Poem

🐰 I hopped through kernels, params in tow,
A tiny bias made RMSNorm glow,
From CUDA banks to Python calls,
Tests and benchmarks heed the calls,
Speedups printed — a rabbit’s show!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 39.39% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title '[feat] Add gemma RMS AR fusion' clearly summarizes the main change: adding Gemma RMS AllReduce fusion support to the codebase.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed PR description includes a brief explanation of changes and completed checklist items, but lacks detailed motivation and the related issues section is empty.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request adds support for Gemma/Qwen3.5 style RMSNorm to AllReduce fusion kernels by introducing a weight_bias parameter across the CUDA kernels and Python API. The implementation is accompanied by new benchmarks and comprehensive tests. Feedback from the reviewer correctly identifies that weight_bias should not be included in the mutates_args list of the register_custom_op decorators in flashinfer/comm/trtllm_ar.py, as it is an input scalar rather than an in-place modified tensor.

Comment thread flashinfer/comm/trtllm_ar.py Outdated
@@ -242,6 +242,7 @@ def trtllm_custom_all_reduce(
"scale_factor",
"layout_code",
"block_quant_group_size",
"weight_bias",

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.

medium

The weight_bias parameter is an input scalar and should not be included in the mutates_args list of the register_custom_op decorator. This list is intended for arguments that are modified in-place by the operation.

Comment thread flashinfer/comm/trtllm_ar.py Outdated
@@ -317,6 +320,7 @@ def trtllm_allreduce_fusion(
"norm_out",
"quant_out",
"scale_out",
"weight_bias",

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.

medium

The weight_bias parameter is an input scalar and should not be included in the mutates_args list. Only tensors that are modified in-place should be listed here.

Comment thread flashinfer/comm/trtllm_ar.py Outdated
Comment on lines +375 to +381
mutates_args=[
"residual_out",
"norm_out",
"quant_out",
"scale_out",
"weight_bias",
],

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.

medium

The weight_bias parameter is an input scalar and should not be included in mutates_args. Additionally, the reformatting of this list into multiple lines is unnecessary and deviates from the style used for other similar operations in this repository.

        mutates_args=["residual_out", "norm_out", "quant_out", "scale_out"],

@jiahanc

jiahanc commented May 14, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@flashinfer-bot

Copy link
Copy Markdown
Collaborator

GitLab MR !669 has been created, and the CI pipeline #51291920 is currently running. I'll report back once the pipeline job completes.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
tests/comm/test_allreduce_fusion_moe_unified_api.py (1)

267-275: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Add unsupported-architecture skip gating in these test entrypoints.

These tests only gate on GPU count; they should also skip on unsupported SM targets via flashinfer.utils capability checks to avoid false failures on unsupported architectures.

As per coding guidelines "tests/**/*.py: Skip tests on unsupported GPU architectures using flashinfer.utils check functions like is_sm90a_supported(), is_sm100a_supported(), and get_compute_capability()."

Also applies to: 404-415

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/comm/test_allreduce_fusion_moe_unified_api.py` around lines 267 - 275,
The test entrypoint function test_moe_finalize_allreduce_unified_api currently
only skips based on GPU count; add an architecture capability check at the start
of this function (and the other similar test functions around lines 404-415)
using flashinfer.utils helpers (e.g., call get_compute_capability() and/or
is_sm90a_supported()/is_sm100a_supported()) and call pytest.skip(...) when the
device compute capability is unsupported; ensure you import the needed helpers
from flashinfer.utils and perform the skip before allocating or using torch.cuda
to avoid running the test on unsupported SM targets.
tests/comm/test_trtllm_allreduce_fusion.py (1)

470-481: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Gate this suite on supported compute capability, not just GPU count.

Please add an architecture capability skip using flashinfer.utils checks in the shared execution path for this suite, so unsupported SMs are skipped consistently.

As per coding guidelines "tests/**/*.py: Skip tests on unsupported GPU architectures using flashinfer.utils check functions like is_sm90a_supported(), is_sm100a_supported(), and get_compute_capability()."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/comm/test_trtllm_allreduce_fusion.py` around lines 470 - 481, The test
currently only skips by GPU count; add a compute-capability gate in
test_trtllm_allreduce_fusion using flashinfer.utils so unsupported SMs are
skipped consistently: import and call is_sm90a_supported(),
is_sm100a_supported() or get_compute_capability() at the start of the
test_trtllm_allreduce_fusion function (before running GPU-dependent code) and
call pytest.skip(...) when the current device compute capability is not
supported; ensure you reference the flashinfer.utils helpers
(is_sm90a_supported, is_sm100a_supported, get_compute_capability) and keep the
existing world_size vs torch.cuda.device_count() check after the capability
check so tests on unsupported architectures are skipped early.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@flashinfer/comm/trtllm_mnnvl_ar.py`:
- Around line 302-303: The custom-op wrapper's parameter weight_bias currently
has type Optional[float] = None but is always forwarded to the CUDA op; change
its signature to use a numeric default (e.g., float = 0.0) so deprecated callers
that omit it continue to receive 0.0 instead of None. Update the function/method
signature that declares weight_bias (in the wrapper around the RMSNorm/custom op
in trtllm_mnnvl_ar.py) from Optional[float] = None to float = 0.0 and adjust any
type hints or callers if necessary to match the non-optional float default.

In `@tests/comm/test_gemma_ar_fusion.py`:
- Around line 164-169: In test_gemma_rmsnorm_ar_fusion, add GPU architecture
checks using flashinfer.utils to skip the test on unsupported SMs: import and
call get_compute_capability() and the helper predicates (is_sm90a_supported(),
is_sm100a_supported()) and if the current compute capability isn't supported,
call pytest.skip(...) similar to the existing world_size/GPU-count guard; ensure
checks are performed before heavy GPU usage so the test exits early when the
device SM is unsupported.

---

Outside diff comments:
In `@tests/comm/test_allreduce_fusion_moe_unified_api.py`:
- Around line 267-275: The test entrypoint function
test_moe_finalize_allreduce_unified_api currently only skips based on GPU count;
add an architecture capability check at the start of this function (and the
other similar test functions around lines 404-415) using flashinfer.utils
helpers (e.g., call get_compute_capability() and/or
is_sm90a_supported()/is_sm100a_supported()) and call pytest.skip(...) when the
device compute capability is unsupported; ensure you import the needed helpers
from flashinfer.utils and perform the skip before allocating or using torch.cuda
to avoid running the test on unsupported SM targets.

In `@tests/comm/test_trtllm_allreduce_fusion.py`:
- Around line 470-481: The test currently only skips by GPU count; add a
compute-capability gate in test_trtllm_allreduce_fusion using flashinfer.utils
so unsupported SMs are skipped consistently: import and call
is_sm90a_supported(), is_sm100a_supported() or get_compute_capability() at the
start of the test_trtllm_allreduce_fusion function (before running GPU-dependent
code) and call pytest.skip(...) when the current device compute capability is
not supported; ensure you reference the flashinfer.utils helpers
(is_sm90a_supported, is_sm100a_supported, get_compute_capability) and keep the
existing world_size vs torch.cuda.device_count() check after the capability
check so tests on unsupported architectures are skipped early.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 73515f0e-c1c5-44e0-a6e1-14c4b2be1ac7

📥 Commits

Reviewing files that changed from the base of the PR and between ed0f5f8 and 81323a86b4b50fd4525b63ff6490d478022cf0f4.

📒 Files selected for processing (14)
  • benchmarks/bench_gemma_ar_fusion.py
  • csrc/trtllm_allreduce_fusion.cu
  • csrc/trtllm_mnnvl_allreduce.cu
  • csrc/trtllm_moe_allreduce_fusion.cu
  • flashinfer/comm/allreduce.py
  • flashinfer/comm/trtllm_ar.py
  • flashinfer/comm/trtllm_mnnvl_ar.py
  • flashinfer/trace/templates/comm.py
  • include/flashinfer/comm/trtllm_allreduce_fusion.cuh
  • include/flashinfer/comm/trtllm_mnnvl_allreduce.cuh
  • include/flashinfer/comm/trtllm_moe_allreduce_fusion.cuh
  • tests/comm/test_allreduce_fusion_moe_unified_api.py
  • tests/comm/test_gemma_ar_fusion.py
  • tests/comm/test_trtllm_allreduce_fusion.py

Comment thread flashinfer/comm/trtllm_mnnvl_ar.py
Comment on lines +164 to +169
def test_gemma_rmsnorm_ar_fusion(world_size):
"""End-to-end Gemma RMSNorm correctness for fused AllReduce path."""
available_gpus = torch.cuda.device_count()
if world_size > available_gpus:
pytest.skip(f"world_size {world_size} > available GPUs {available_gpus}")

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.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Add compute-capability skip for unsupported GPUs.

This test should include flashinfer.utils architecture checks (in addition to GPU-count checks) to prevent unsupported-SM failures.

As per coding guidelines "tests/**/*.py: Skip tests on unsupported GPU architectures using flashinfer.utils check functions like is_sm90a_supported(), is_sm100a_supported(), and get_compute_capability()."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/comm/test_gemma_ar_fusion.py` around lines 164 - 169, In
test_gemma_rmsnorm_ar_fusion, add GPU architecture checks using flashinfer.utils
to skip the test on unsupported SMs: import and call get_compute_capability()
and the helper predicates (is_sm90a_supported(), is_sm100a_supported()) and if
the current compute capability isn't supported, call pytest.skip(...) similar to
the existing world_size/GPU-count guard; ensure checks are performed before
heavy GPU usage so the test exits early when the device SM is unsupported.

jiahanc added 2 commits May 18, 2026 01:18
Signed-off-by: jiahanc <173873397+jiahanc@users.noreply.github.com>
Signed-off-by: jiahanc <173873397+jiahanc@users.noreply.github.com>
@jiahanc
jiahanc force-pushed the AddGemmaRMSARFusion branch from 81323a8 to e7b9cdf Compare May 18, 2026 08:18
@jiahanc
jiahanc requested a review from dhiraj113 as a code owner May 18, 2026 08:18
Signed-off-by: jiahanc <173873397+jiahanc@users.noreply.github.com>
@samuellees samuellees self-assigned this May 19, 2026
Comment thread flashinfer/comm/trtllm_ar.py
Comment thread tests/comm/test_gemma_ar_fusion.py
Signed-off-by: jiahanc <173873397+jiahanc@users.noreply.github.com>

@samuellees samuellees 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

@jiahanc
jiahanc enabled auto-merge (squash) May 19, 2026 09:38
@jiahanc
jiahanc merged commit 7b9e054 into flashinfer-ai:main May 19, 2026
56 of 57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants