-
Notifications
You must be signed in to change notification settings - Fork 284
add flashinfer fused_norm_add op #1105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @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 Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this 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.
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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) |
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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) |
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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) |
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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) |
| _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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| _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) |
| else: | ||
| input_embdings.add_(o.view(-1, self.embed_dim_)) | ||
| input1 = self._ffn_norm(input_embdings, infer_state, layer_weight) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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)) |
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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) |
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block has two critical issues:
- Similar to other parts of this PR, the
ifbranch has a correctness bug._1_input1is not updated after thefused_add_rmsnormcall, causing the wrong tensor to be passed tolayer_weight.moe_gate.mmon line 275. It should be set toinput_embdings1after the fused kernel call. - The
elsebranch has a copy-paste error, usinginput_embdingsandinfer_stateinstead ofinput_embdings1andinfer_state1.
| 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) |
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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) |
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block has two critical issues:
- Similar to other parts of this PR, the
ifbranch has a correctness bug._1_input1is not updated after thefused_add_rmsnormcall, causing the wrong tensor to be passed tolayer_weight.moe_gate.mmon line 396. It should be set toinput_embdings1after the fused kernel call. - The
elsebranch has a copy-paste error, usinginput_embdingsandinfer_stateinstead ofinput_embdings1andinfer_state1.
| 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) |
No description provided.