Skip to content

support qkv repeat#1203

Merged
hiworldwzj merged 2 commits intomainfrom
support_qkv_repeat
Feb 6, 2026
Merged

support qkv repeat#1203
hiworldwzj merged 2 commits intomainfrom
support_qkv_repeat

Conversation

@WANDY666
Copy link
Contributor

@WANDY666 WANDY666 commented Feb 5, 2026

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @WANDY666, 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 tensor parallelism implementation within the ROWMMWeight class by introducing separate handling for query (Q) and key/value (KV) head configurations. This change allows for greater flexibility and correctness when distributing model weights, particularly for architectures like grouped-query attention where KV heads might be repeated or padded differently across tensor parallelism ranks. The modifications ensure that weight loading and slicing logic are appropriately applied to Q and KV parameters independently.

Highlights

  • Separate Q and KV Repeat Times: Introduced distinct q_repeat_times and kv_repeat_times for query and key/value heads, respectively, replacing a single repeat_times variable to allow for more granular control over head repetition.
  • Flexible KV Head Tensor Parallelism: Modified the tensor parallelism assertion for kv_head_num to support cases where tp_world_size_ is divisible by kv_head_num, enabling more flexible configurations for key/value heads in distributed setups.
  • Dedicated Parameter Slicers: Implemented separate parameter slicers (q_param_slicer and kv_param_slicer) for query and key/value weights, allowing for independent slicing logic during weight loading based on their specific configurations.
  • Dynamic Slicer Selection for Weight Loading: Refactored weight, bias, and scale loading methods (_load_weight, _load_bias, _load_weight_scale, _load_weight_zero_point) to dynamically select the appropriate parameter slicer (Q or KV) based on the sub_child_index of the parameter being loaded.
  • New TP Padded Head Number Calculation: Added a new helper method _get_tp_padded_head_num to correctly calculate the tensor parallelism padded head number, specifically addressing scenarios where tp_world_size_ is divisible by head_num for KV heads.

🧠 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.

Changelog
  • lightllm/common/basemodel/layer_weights/meta_weights/mm_weight/rowmm_weight.py
    • Replaced self.repeat_times with self.q_repeat_times and self.kv_repeat_times.
    • Updated the assertion for kv_head_num to allow tp_world_size_ to be divisible by kv_head_num.
    • Modified the calculation of kv_hidden_size to use the new _get_tp_padded_head_num method.
    • Split the single self.param_slicer into self.q_param_slicer and self.kv_param_slicer.
    • Introduced a new method _get_param_slicer to return the correct slicer based on sub_child_index.
    • Overrode _load_weight, _load_bias, _load_weight_scale, and _load_weight_zero_point to utilize the dynamic slicer selection.
    • Added the _get_tp_padded_head_num helper method to manage padded head numbers for tensor parallelism.
Activity
  • No specific activity (comments, reviews, etc.) has been recorded for this pull request yet.
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 introduces support for repeating key and value heads in QKV weight loading, which is essential for models using Grouped-Query Attention (GQA) with tensor parallelism. The implementation correctly adjusts the logic for sharding heads. My review focuses on improving code maintainability. I've pointed out areas with significant code duplication that could be refactored, a method with non-obvious side effects, and a docstring that should be translated for consistency. Addressing these points will make the code cleaner and easier to maintain.

Comment on lines 160 to 199
def _load_weight(
self, param_name: Union[str, List[str]], weights: Dict[str, torch.Tensor], sub_child_index: int
) -> None:
# 复用基类逻辑,但根据 sub_child_index 选择对应的 slicer
quanted_param_name = self.quanted_weight_names[sub_child_index]
if quanted_param_name in weights:
param_name = quanted_param_name
if param_name in weights:
slicer = self._get_param_slicer(sub_child_index)
weight = slicer._slice_weight(weights[param_name])
self.quant_method.load_weight(weight, self.mm_param_list[sub_child_index])
return

def _load_bias(
self, param_name: Union[str, List[str]], weights: Dict[str, torch.Tensor], sub_child_index: int
) -> None:
if param_name in weights:
slicer = self._get_param_slicer(sub_child_index)
bias = slicer._slice_bias(weights[param_name])
self.bias_list[sub_child_index].copy_(bias)
self.bias_list[sub_child_index].load_ok = True
return

def _load_weight_scale(
self, param_name: Union[str, List[str]], weights: Dict[str, torch.Tensor], sub_child_index: int
) -> None:
if param_name in weights:
slicer = self._get_param_slicer(sub_child_index)
weight_scale = slicer._slice_weight_scale(weights[param_name])
self.quant_method.load_weight_scale(weight_scale, self.mm_param_list[sub_child_index])
return

def _load_weight_zero_point(
self, param_name: Union[str, List[str]], weights: Dict[str, torch.Tensor], sub_child_index: int
) -> None:
if param_name in weights:
slicer = self._get_param_slicer(sub_child_index)
weight_zero_point = slicer._slice_weight_zero_point(weights[param_name])
self.quant_method.load_weight_zero_point(weight_zero_point, self.mm_param_list[sub_child_index])
return
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

These _load_* methods (_load_weight, _load_bias, _load_weight_scale, _load_weight_zero_point) are almost identical to their counterparts in the base class MMWeightTpl, with the only difference being how the slicer is obtained. This introduces significant code duplication and makes the code harder to maintain.

A better approach would be to modify the base class to use a method like _get_param_slicer, which this class can then override. This would follow the Template Method design pattern and avoid overriding all four loading methods.

If modifying the base class is not an option, the duplication within this class should still be addressed, perhaps with a private helper method.

Comment on lines +201 to +171
def _get_tp_padded_head_num(self, head_num: int):
if head_num % self.tp_world_size_ == 0:
return head_num // self.tp_world_size_
elif self.tp_world_size_ % head_num == 0:
self.kv_repeat_times = self.tp_world_size_ // head_num
return self.kv_repeat_times * head_num // self.tp_world_size_
else:
raise ValueError(
f"head_num must be divisible by tp_world_size_ or "
f"tp_world_size_ must be divisible by head_num, "
f"but found: {head_num} % {self.tp_world_size_}"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This method has a side effect: it modifies self.kv_repeat_times on line 205. Methods with get in their name are generally expected to be free of side effects, which can be surprising for future maintainers. Consider refactoring this logic to make the side effect more explicit. For example, you could move this logic into the __init__ method, or rename the method to something like _setup_kv_sharding_and_get_heads to make its behavior clearer.

@WANDY666 WANDY666 force-pushed the support_qkv_repeat branch 2 times, most recently from 20f2e63 to c77c837 Compare February 5, 2026 10:32
@hiworldwzj hiworldwzj merged commit a2a61ae into main Feb 6, 2026
1 check passed
@hiworldwzj hiworldwzj deleted the support_qkv_repeat branch February 6, 2026 02:23
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