Skip to content

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 introduces targeted fixes for the apo_zero_unpaired loss type within the KTO (Kahneman-Tversky Optimization) training framework. The changes ensure that the data encoding process correctly handles the absence of rejected samples and that dataset preparation logic is appropriately applied, or bypassed, based on the specific requirements of the apo_zero_unpaired loss, thereby enhancing the robustness and correctness of KTO training.

Highlights

  • Flexible RLHF Encoding: The _rlhf_encode function now includes a check_rejected parameter, allowing it to gracefully handle situations where a rejected sample might be absent without raising an error.
  • KTO Unpaired Data Handling: The _kto_encode function has been updated to call _rlhf_encode with check_rejected=False, specifically accommodating KTO training scenarios where rejected samples may not be provided, such as with unpaired data.
  • Conditional KTO Dataset Preparation: The prepare_kto_dataset function now conditionally skips batch size validation and KL dataset preparation steps when the loss_type is apo_zero_unpaired, optimizing the data pipeline for this specific KTO variant.
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 a fix for KTO training with the apo_zero_unpaired loss type. The changes involve modifying _rlhf_encode to conditionally handle cases where inputs.rejected is None, and skipping KL dataset preparation in prepare_kto_dataset when using this loss type. The changes are logical and correct. I have one suggestion to improve code readability.

Comment on lines 364 to 369
if check_rejected and inputs.rejected is None:
raise ValueError('inputs.rejected is None')
if inputs.rejected is None:
rejected_encoded = {}
else:
rejected_encoded = self._encode_truncated(inputs.rejected)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The logic for handling inputs.rejected can be simplified to improve readability. The current implementation uses two separate if statements to check for inputs.rejected is None, which can be combined into a single, clearer if/else block.

Suggested change
if check_rejected and inputs.rejected is None:
raise ValueError('inputs.rejected is None')
if inputs.rejected is None:
rejected_encoded = {}
else:
rejected_encoded = self._encode_truncated(inputs.rejected)
if inputs.rejected is None:
if check_rejected:
raise ValueError('inputs.rejected is None')
rejected_encoded = {}
else:
rejected_encoded = self._encode_truncated(inputs.rejected)

@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 a fix for KTO training with apo_zero_unpaired loss, which handles datasets without paired rejected responses. The changes correctly modify the data encoding and collation process to skip the creation and processing of synthetic rejected samples when they are not present. My main concern is with the implementation for Megatron. The changes in swift/megatron/trainers/kto_trainer.py introduce a dependency on self.args.calculate_KL, which is not a valid attribute and will cause a runtime error. I have provided a detailed comment and suggestion to fix this critical issue. The other changes in swift/llm/template/base.py and swift/llm/train/kto.py look good.

@Jintao-Huang Jintao-Huang changed the title [kto] fix kto apo_zero_unpaired [kto] fix kto loss_type=apo_zero_unpaired Nov 14, 2025
@Jintao-Huang Jintao-Huang merged commit ea58fce into modelscope:main Nov 14, 2025
1 of 2 checks passed
vx120 pushed a commit to vx120/ms-swift that referenced this pull request Nov 19, 2025
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