-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[bugfix] fix minimax tp #7788
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
[bugfix] fix minimax tp #7788
Conversation
Summary of ChangesHello @Jintao-Huang, 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 primarily addresses a bug in the Minimax M2 model related to tensor parallelism, ensuring that query and key layernorm weights are correctly sized and distributed across devices. It also updates the model's capabilities by removing a previous restriction on training for the Highlights
🧠 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 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 a bugfix for tensor parallelism (TP) in the Minimax M2 model. The changes correctly adjust the hidden size for QK layernorms to account for TP sharding and ensure their weights are split correctly. While the core fix for TP appears correct, I have a couple of concerns. Firstly, a change in the model bridge reduces flexibility by hardcoding weight key names, which might break compatibility with some model variants. Secondly, the removal of MinimaxTextLoader for the minimax_m2 model could be a regression for users running it on multiple GPUs without Megatron, as it removes custom device mapping logic. My review includes suggestions to address these points.
| Model('MiniMax/MiniMax-M2.1', 'MiniMaxAI/MiniMax-M2.1') | ||
| ]), | ||
| ], | ||
| MinimaxTextLoader, |
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.
Removing MinimaxTextLoader from the minimax_m2 model registration will cause it to fall back to the default ModelLoader. The MinimaxTextLoader contains specific logic for device mapping in multi-GPU setups. This change may break or degrade performance for users running minimax_m2 on multiple GPUs in non-Megatron environments. Please clarify if this is intended. If so, it might be worth documenting this change in behavior.
|
/gemini review |
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 addresses a bug in the tensor parallelism (TP) implementation for the Minimax model. The changes in swift/megatron/model/gpts/minimax_m2.py correctly implement gathering and scattering of tensors around the QK normalization to ensure correctness under tensor parallelism. Additionally, the code for loading weights is simplified. The modifications in swift/model/models/minimax.py remove a custom loader for minimax_m2, which is appropriate as it avoids conflicts with Megatron's own parallelism management. The changes are logical and effectively resolve the bug. I have one minor suggestion to improve code readability.
| args = get_args() | ||
| query, key, value = super().get_query_key_value_tensors(*_args, **kwargs) | ||
| query = query.reshape(*query.shape[:-2], -1) | ||
| key = key.reshape(*key.shape[:-2], -1) | ||
| if args.tensor_model_parallel_size > 1: | ||
| query = gather_from_tensor_model_parallel_region(query) | ||
| key = gather_from_tensor_model_parallel_region(key) | ||
| query = self.q_norm(query) | ||
| key = self.k_norm(key) | ||
| if args.tensor_model_parallel_size > 1: | ||
| query = scatter_to_tensor_model_parallel_region(query) | ||
| key = scatter_to_tensor_model_parallel_region(key) | ||
| query = query.view(*query.shape[:2], -1, self.hidden_size_per_attention_head) | ||
| key = key.view(*key.shape[:2], -1, self.hidden_size_per_attention_head) | ||
| return query, key, value |
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.
To improve readability and avoid re-evaluating the same condition twice, you could store the result of args.tensor_model_parallel_size > 1 in a boolean variable. This makes the code's intent clearer and is slightly more efficient.
| args = get_args() | |
| query, key, value = super().get_query_key_value_tensors(*_args, **kwargs) | |
| query = query.reshape(*query.shape[:-2], -1) | |
| key = key.reshape(*key.shape[:-2], -1) | |
| if args.tensor_model_parallel_size > 1: | |
| query = gather_from_tensor_model_parallel_region(query) | |
| key = gather_from_tensor_model_parallel_region(key) | |
| query = self.q_norm(query) | |
| key = self.k_norm(key) | |
| if args.tensor_model_parallel_size > 1: | |
| query = scatter_to_tensor_model_parallel_region(query) | |
| key = scatter_to_tensor_model_parallel_region(key) | |
| query = query.view(*query.shape[:2], -1, self.hidden_size_per_attention_head) | |
| key = key.view(*key.shape[:2], -1, self.hidden_size_per_attention_head) | |
| return query, key, value | |
| args = get_args() | |
| query, key, value = super().get_query_key_value_tensors(*_args, **kwargs) | |
| query = query.reshape(*query.shape[:-2], -1) | |
| key = key.reshape(*key.shape[:-2], -1) | |
| use_tp = args.tensor_model_parallel_size > 1 | |
| if use_tp: | |
| query = gather_from_tensor_model_parallel_region(query) | |
| key = gather_from_tensor_model_parallel_region(key) | |
| query = self.q_norm(query) | |
| key = self.k_norm(key) | |
| if use_tp: | |
| query = scatter_to_tensor_model_parallel_region(query) | |
| key = scatter_to_tensor_model_parallel_region(key) | |
| query = query.view(*query.shape[:2], -1, self.hidden_size_per_attention_head) | |
| key = key.view(*key.shape[:2], -1, self.hidden_size_per_attention_head) | |
| return query, key, value |
No description provided.