Skip to content

[megatron] megatron support padding_free false#7205

Merged
Jintao-Huang merged 13 commits intomodelscope:mainfrom
Jintao-Huang:support_padding_free_false
Dec 25, 2025
Merged

[megatron] megatron support padding_free false#7205
Jintao-Huang merged 13 commits intomodelscope:mainfrom
Jintao-Huang:support_padding_free_false

Conversation

@Jintao-Huang
Copy link
Collaborator

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 significantly enhances the flexibility of the Megatron framework by enabling DPO and KTO training types to operate without strictly enforcing a padding_free configuration. This change broadens the applicability of these reinforcement learning from human feedback (RLHF) methods, allowing them to be integrated into a wider range of training setups. The modifications primarily involve adjusting internal checks and the handling of sample counts to ensure robust behavior regardless of the padding strategy employed.

Highlights

  • Relaxed Padding-Free Requirement: The strict requirement for padding_free mode has been removed for both DPO (Direct Preference Optimization) and KTO (Kahneman-Tversky Optimization) training types, allowing these RLHF methods to be used in configurations where padding_free is set to false.
  • Improved Sample Count Handling: The logic for determining and passing num_samples to the get_logps function has been refined. This ensures correct calculation of log probabilities, especially when packed_seq_params might be None or when operating without padding-free 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 aims to add support for padding_free=False in Megatron's DPO and KTO trainers. The changes involve removing assertions that enforce padding_free=True and modifying how the number of samples is calculated and passed to the get_logps function. However, I've found a critical issue in swift/megatron/trainers/rlhf_mixin.py. The get_logps function is not correctly implemented for the padding_free=False case, which will lead to a runtime error. I've provided a code suggestion to fix this. Please review the suggested change.

@Jintao-Huang
Copy link
Collaborator Author

/gemini review

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 support for padding_free=False in Megatron training, enabling the use of standard padded batches. The changes are extensive, touching several trainer classes and utility functions to handle both padded and padding-free data processing. The implementation is mostly correct, but I've identified a couple of bugs. One is a high-severity issue in the DPO trainer's RPO loss calculation, and the other is a critical bug in the sequence classification loss function that would cause a crash. My review includes code suggestions to fix these issues.

@Jintao-Huang
Copy link
Collaborator Author

/gemini review

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 training with padding_free=False in Megatron, which is a valuable enhancement for handling padded sequences. The changes primarily involve adding logic to correctly process both padded and padding-free data across different trainer components. My review has identified a potential bug in how the last token index is derived from the attention mask for padded sequences, which could lead to incorrect behavior. Additionally, I've pointed out an opportunity to refactor some duplicated code to improve maintainability.

@Jintao-Huang
Copy link
Collaborator Author

/gemini review

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 training with padding_free=false in the Megatron integration, which is a valuable enhancement for flexibility. The changes are well-implemented, removing previous assertions and adding conditional logic to correctly handle both padded and padding-free data across various trainers (DPO, KTO, Reward Model, etc.). The introduction of helper functions like get_last_tokens is a good refactoring choice that improves code clarity. Additionally, the PR includes a bug fix for attention mask creation and adds a new example script for testing. Overall, this is a solid contribution. I have one minor suggestion to remove a leftover comment.

if self.mode == 'rlhf':
encoded = self._rlhf_encode(inputs)
for prefix in ['chosen', 'rejected']:
for prefix in ['chosen', 'rejected']: # rm
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 line contains a leftover comment # rm, which appears to be a remnant from debugging or a temporary note. It should be removed to maintain code cleanliness.

Suggested change
for prefix in ['chosen', 'rejected']: # rm
for prefix in ['chosen', 'rejected']:

@Jintao-Huang Jintao-Huang merged commit 8382fb0 into modelscope:main Dec 25, 2025
3 checks passed
meichangsu1 pushed a commit to tpx818/ms-swift that referenced this pull request Jan 22, 2026
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