Skip to content

Conversation

@SangChengC
Copy link
Contributor

No description provided.

@gemini-code-assist
Copy link

Summary of Changes

Hello @SangChengC, 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 introduces a significant performance optimization by integrating FlashInfer's fused add_rmsnorm operation into the core transformer layer inference. This change aims to accelerate models utilizing RMS normalization by combining the residual addition and normalization steps into a single, highly efficient GPU kernel. The implementation is robust, dynamically checking for FlashInfer availability and the specific normalization type used by the model to ensure correct and optimal application.

Highlights

  • FlashInfer Integration: Integrated FlashInfer's fused_add_rmsnorm operation into the transformer layer inference process for performance optimization.
  • Conditional Optimization: The FlashInfer optimization is applied conditionally, only when FlashInfer is available and the model uses RMS normalization.
  • Normalization Type Detection: Added a norm_type attribute to TransformerLayerWeight to dynamically determine if a model uses RMS norm or Layer norm, enabling precise application of the fused operation.
  • Model Support: Extended the fused normalization logic to various model architectures, including DeepSeek2, Gemma3, and Qwen3-MoE, ensuring broader applicability of the performance enhancement.
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

@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 introduces the use of flashinfer's fused normalization and addition operations to optimize performance. While this is a good optimization, I've found several critical correctness issues across the modified files. The main issue is that when the flashinfer fused kernel is used, the input to the subsequent layer (e.g., FFN or MoE gate) is not correctly updated, leading to incorrect computations. There are also instances of copy-paste errors where incorrect variables are used. I've provided detailed comments and suggestions for each issue.

Comment on lines +78 to +85
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
input1 = o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Choose a reason for hiding this comment

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

critical

There is a correctness bug in the if branch. The variable input1 is used as input to self._ffn on line 88. In the else branch, input1 is correctly assigned the output of self._ffn_norm. However, in the if branch, input1 holds the value of o.view(-1, self.embed_dim_), which is the raw attention output, not the normalized tensor. The flashinfer.norm.fused_add_rmsnorm function is expected to update input_embdings in-place with the normalized result of input_embdings + o. Therefore, input1 should be updated to input_embdings after the fused kernel call to ensure the correct tensor is passed to the FFN.

Suggested change
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
input1 = o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
residual = o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
residual, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
input1 = input_embdings
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Comment on lines +105 to +112
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
input1 = o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Choose a reason for hiding this comment

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

critical

This block has the same correctness bug as in context_forward. The input1 variable is not correctly updated in the if branch before being used in self._ffn on line 115. It should be assigned the value of input_embdings after the fused_add_rmsnorm operation.

Suggested change
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
input1 = o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
residual = o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
residual, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
input1 = input_embdings
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Comment on lines +130 to +137
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
input1 = o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Choose a reason for hiding this comment

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

critical

This block has the same correctness bug as in context_forward. The input1 variable is not correctly updated in the if branch before being used in self._tpsp_ffn on line 140. It should be assigned the value of input_embdings after the fused_add_rmsnorm operation.

Suggested change
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
input1 = o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
residual = o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
residual, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
input1 = input_embdings
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Comment on lines +153 to +160
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
input1 = o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Choose a reason for hiding this comment

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

critical

This block has the same correctness bug as in context_forward. The input1 variable is not correctly updated in the if branch before being used in self._tpsp_ffn on line 163. It should be assigned the value of input_embdings after the fused_add_rmsnorm operation.

Suggested change
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
input1 = o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
residual = o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
residual, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
input1 = input_embdings
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Comment on lines 824 to 831
_0_input1 = _0_o.view(-1, self.embed_dim_)
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
flashinfer.norm.fused_add_rmsnorm(
_0_input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(_0_o.view(-1, self.embed_dim_))
_0_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Choose a reason for hiding this comment

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

critical

There is a correctness bug here. The variable _0_input1 is used as input to layer_weight.moe_gate.mm on line 834. In the else branch, _0_input1 is correctly assigned the output of self._ffn_norm. However, in the if branch, _0_input1 is not updated after being initialized to _0_o.view(...). The fused_add_rmsnorm function updates input_embdings in-place. _0_input1 should be set to input_embdings after the fused kernel call to pass the correct normalized tensor to the MoE gate.

Suggested change
_0_input1 = _0_o.view(-1, self.embed_dim_)
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
flashinfer.norm.fused_add_rmsnorm(
_0_input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(_0_o.view(-1, self.embed_dim_))
_0_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
flashinfer.norm.fused_add_rmsnorm(
_0_input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
_0_input1 = input_embdings
else:
input_embdings.add_(_0_o.view(-1, self.embed_dim_))
_0_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Comment on lines +180 to +182
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Choose a reason for hiding this comment

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

critical

This else branch has the same issue as in context_forward. The _ffn_norm result is calculated into input1 which is then discarded, and the normalization is not applied to input_embdings. This makes the logic inconsistent with the if branch which uses a fused add-and-norm operation.

Suggested change
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)
else:
input_embdings.add_(o.view(-1, self.embed_dim_))
input_embdings.copy_(self._ffn_norm(input_embdings.float(), infer_state, layer_weight).to(input_embdings.dtype))

Comment on lines 231 to 237
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
flashinfer.norm.fused_add_rmsnorm(
_0_input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(_0_o.view(-1, self.embed_dim_))
_0_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Choose a reason for hiding this comment

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

critical

There is a correctness bug here. The variable _0_input1 is used as input to layer_weight.moe_gate.mm on line 239. In the else branch, _0_input1 is correctly assigned the output of self._ffn_norm. However, in the if branch, _0_input1 is not updated after being initialized to _0_o.view(...). The fused_add_rmsnorm function updates input_embdings in-place. _0_input1 should be set to input_embdings after the fused kernel call to pass the correct normalized tensor to the MoE gate.

Suggested change
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
flashinfer.norm.fused_add_rmsnorm(
_0_input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(_0_o.view(-1, self.embed_dim_))
_0_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
flashinfer.norm.fused_add_rmsnorm(
_0_input1, input_embdings, layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
_0_input1 = input_embdings
else:
input_embdings.add_(_0_o.view(-1, self.embed_dim_))
_0_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Comment on lines 265 to 271
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
flashinfer.norm.fused_add_rmsnorm(
_1_input1, input_embdings1, weight=layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(_1_o.view(-1, self.embed_dim_))
_1_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Choose a reason for hiding this comment

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

critical

This block has two critical issues:

  1. Similar to other parts of this PR, the if branch has a correctness bug. _1_input1 is not updated after the fused_add_rmsnorm call, causing the wrong tensor to be passed to layer_weight.moe_gate.mm on line 275. It should be set to input_embdings1 after the fused kernel call.
  2. The else branch has a copy-paste error, using input_embdings and infer_state instead of input_embdings1 and infer_state1.
Suggested change
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
flashinfer.norm.fused_add_rmsnorm(
_1_input1, input_embdings1, weight=layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(_1_o.view(-1, self.embed_dim_))
_1_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
flashinfer.norm.fused_add_rmsnorm(
_1_input1, input_embdings1, weight=layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
_1_input1 = input_embdings1
else:
input_embdings1.add_(_1_o.view(-1, self.embed_dim_))
_1_input1 = self._ffn_norm(input_embdings1, infer_state1, layer_weight)

Comment on lines 355 to 361
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
flashinfer.norm.fused_add_rmsnorm(
_0_input1, input_embdings, weight=layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(_0_o.view(-1, self.embed_dim_))
_0_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Choose a reason for hiding this comment

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

critical

There is a correctness bug here. The variable _0_input1 is used as input to layer_weight.moe_gate.mm on line 363. In the else branch, _0_input1 is correctly assigned the output of self._ffn_norm. However, in the if branch, _0_input1 is not updated after being initialized to _0_o.view(...). The fused_add_rmsnorm function updates input_embdings in-place. _0_input1 should be set to input_embdings after the fused kernel call to pass the correct normalized tensor to the MoE gate.

Suggested change
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
flashinfer.norm.fused_add_rmsnorm(
_0_input1, input_embdings, weight=layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(_0_o.view(-1, self.embed_dim_))
_0_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
flashinfer.norm.fused_add_rmsnorm(
_0_input1, input_embdings, weight=layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
_0_input1 = input_embdings
else:
input_embdings.add_(_0_o.view(-1, self.embed_dim_))
_0_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Comment on lines +385 to +392
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
_1_input1 = _1_o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
_1_input1, input_embdings1, weight=layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(_1_o.view(-1, self.embed_dim_))
_1_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)

Choose a reason for hiding this comment

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

critical

This block has two critical issues:

  1. Similar to other parts of this PR, the if branch has a correctness bug. _1_input1 is not updated after the fused_add_rmsnorm call, causing the wrong tensor to be passed to layer_weight.moe_gate.mm on line 396. It should be set to input_embdings1 after the fused kernel call.
  2. The else branch has a copy-paste error, using input_embdings and infer_state instead of input_embdings1 and infer_state1.
Suggested change
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
_1_input1 = _1_o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
_1_input1, input_embdings1, weight=layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
else:
input_embdings.add_(_1_o.view(-1, self.embed_dim_))
_1_input1 = self._ffn_norm(input_embdings, infer_state, layer_weight)
if FLASHINFER_AVAILABLE and layer_weight.norm_type == "rms_norm":
_1_input1 = _1_o.view(-1, self.embed_dim_)
flashinfer.norm.fused_add_rmsnorm(
_1_input1, input_embdings1, weight=layer_weight.ffn_norm_weight_.weight, eps=self.eps_
)
_1_input1 = input_embdings1
else:
input_embdings1.add_(_1_o.view(-1, self.embed_dim_))
_1_input1 = self._ffn_norm(input_embdings1, infer_state1, layer_weight)

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.

2 participants