Skip to content

Conversation

@iiiiLllllzx
Copy link

[WIP]Add NPU support for the fused_norm_gate operator

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 20, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
Copy link
Contributor

Summary of Changes

Hello @iiiiLllllzx, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the fused_norm_gate operator by adding specialized support for Neural Processing Units (NPUs). It introduces highly optimized Triton kernels for both forward and backward passes of fused Layer Normalization and RMS Normalization, combined with various gated activation functions. The changes also include a utility for NPU core detection and thorough unit tests to ensure functional correctness and performance on NPU hardware.

Highlights

  • NPU Support for Fused Norm-Gate Operator: Introduced dedicated NPU (Neural Processing Unit) support for the fused_norm_gate operator, leveraging Triton kernels for optimized performance on NPU devices.
  • Triton Kernel Implementations: Added several Triton kernels (layer_norm_gated_fwd_kernel, layer_norm_gated_bwd_kernel, and their kernel1 variants) to handle the forward and backward passes of fused Layer Normalization/RMS Normalization with gated activations (swish, silu, sigmoid).
  • New Utility Function for NPU Core Count: A new utility function get_multiprocessor_count was added to fla/utils.py to dynamically retrieve the number of vector cores on NPU devices, which is essential for configuring Triton kernel launches.
  • Comprehensive Testing: Included new unit tests in tests/modules/test_layernorm_gated_npu.py to validate the correctness of the NPU-specific fused LayerNorm and RMSNorm gated implementations against standard PyTorch, covering various configurations and activations.
  • Fused Linear Operations: Provided LayerNormGatedLinearFunction and corresponding nn.Module wrappers (FusedLayerNormGatedLinear, FusedRMSNormGatedLinear) that fuse the gated normalization with a linear layer for further optimization.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 NPU support for the fused_norm_gate operator. While the initiative is great, there are a few critical issues that need to be addressed.
First, the new file fla/modules/fused_norm_gate_npu.py is almost a complete duplicate of fla/modules/fused_norm_gate.py, which poses a significant maintainability problem. This duplication should be avoided by introducing device-specific configurations within the original file.
Second, there's a critical bug in fla/modules/fused_norm_gate_npu.py due to a missing import for autotune_cache_kwargs, which will cause a NameError.
Lastly, the changes in fla/utils.py introduce a regression by incorrectly implementing get_multiprocessor_count, which would cripple performance on non-NPU backends like CUDA. It also re-defines an existing function.
Please see the detailed comments for suggestions on how to fix these issues.

import triton
import triton.language as tl

from fla.utils import get_multiprocessor_count, input_guard
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The variable autotune_cache_kwargs is used in this file (e.g., on line 28), but it is not imported. This will lead to a NameError at runtime. Please add it to the import statement.

Suggested change
from fla.utils import get_multiprocessor_count, input_guard
from fla.utils import autotune_cache_kwargs, get_multiprocessor_count, input_guard

Comment on lines +547 to +551
def get_multiprocessor_count(tensor_idx: int = 0) -> int:
if triton.runtime.driver.active.get_current_target().backend == 'npu':
return triton.runtime.driver.active.utils.get_device_properties(tensor_idx)['num_vectorcore']
else:
return 1
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This implementation of get_multiprocessor_count is incorrect for non-NPU devices. It returns 1 for all other backends, including CUDA. This will cause the grid size for Triton kernels to be fixed at 1, preventing parallel execution across multiprocessors and leading to a severe performance degradation.

Additionally, adding this function here redefines the existing get_multiprocessor_count at line 416. You should merge the NPU-specific logic into a single, correct function.

Here is a suggested implementation that correctly handles both NPU and other backends:

Suggested change
def get_multiprocessor_count(tensor_idx: int = 0) -> int:
if triton.runtime.driver.active.get_current_target().backend == 'npu':
return triton.runtime.driver.active.utils.get_device_properties(tensor_idx)['num_vectorcore']
else:
return 1
def get_multiprocessor_count(tensor_idx: int = 0) -> int:
try:
if triton.runtime.driver.active.get_current_target().backend == 'npu':
return triton.runtime.driver.active.utils.get_device_properties(tensor_idx)['num_vectorcore']
return triton.runtime.driver.active.utils.get_device_properties(tensor_idx)['multiprocessor_count']
except Exception:
return 1

@@ -0,0 +1,1244 @@
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This file is almost an exact copy of fla/modules/fused_norm_gate.py. Duplicating large files for minor device-specific changes makes the code harder to maintain. Any bug fix or feature enhancement would need to be applied in two places.

The only significant difference seems to be the autotune configuration for layer_norm_gated_fwd_kernel. This can be handled conditionally within the original fused_norm_gate.py file.

I recommend removing this file and modifying fla/modules/fused_norm_gate.py to support NPU-specific configurations. You can use fla.utils.device_platform to check the device and adjust the autotune parameters accordingly.

For example:

from fla.utils import device_platform

if device_platform == 'npu':
    BT_VALUES = [32, 64]
else:
    BT_VALUES = [16, 32, 64]

@triton.autotune(
    configs=[
        triton.Config({'BT': BT}, num_warps=num_warps)
        for BT in BT_VALUES
        for num_warps in [4, 8, 16]
    ],
    ...
)
def layer_norm_gated_fwd_kernel(...):
    ...

This will make the codebase much cleaner and easier to maintain.

@zhiyuan1i zhiyuan1i force-pushed the main branch 2 times, most recently from 2b3db51 to 53dda79 Compare January 22, 2026 07:04
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.

1 participant