Skip to content

Conversation

@poryfly
Copy link
Contributor

@poryfly poryfly commented Nov 20, 2025

What does this PR do?

#1610

Fixes # (issue)

Before submitting

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @poryfly, 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 training capabilities by adding support for advanced preference optimization techniques such as DPO, ORPO, and SIMPO. It achieves this by integrating a specialized DPO trainer and by refactoring the core accelerator logic to allow for more flexible model management and device placement. The internal configuration for training modes has also been generalized to better support these diverse training approaches.

Highlights

  • DPO Trainer Integration: Introduced KTDporainer, a custom trainer inheriting from trl.DPOTrainer, to support Direct Preference Optimization (DPO) and potentially other preference-based training methods like ORPO and SIMPO.
  • Generalized Training Mode: Refactored the internal GLOBAL_CONFIG._config["mod"] from a specific 'sft' (Supervised Fine-Tuning) mode to a more general 'train' mode across several files, accommodating various training paradigms.
  • Custom Accelerator Handling: Centralized and customized the KAccelerator class, which extends accelerate.Accelerator, to provide fine-grained control over model device placement and wrapping, crucial for KTransformers' specific model handling.
  • LoRA Adapter Saving: Modified the save_model method in KTDporainer to specifically save only LoRA adapters, including adapter_config.json, optimizing storage for fine-tuned models.
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 DPO by adding a new KTDporainer and refactors some existing code for better reusability and generality, such as moving KAccelerator to a shared utility module and changing the training mode from sft to a more general train. While the refactoring is a good step, the new KTDporainer has several issues that need to be addressed. These include a typo in the class name, using print instead of a logger, a risky try...except Exception: pass block that can hide errors, and a critical bug in post_training_step that fails to return the correctly processed tensor. Addressing these points will significantly improve the code's quality, maintainability, and correctness.

Comment on lines 187 to 190
def post_training_step(self, loss):
if loss.device != self.args.device:
ret = loss.to(self.args.device, non_blocking=True)
return loss
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

There is a bug in this method. The loss tensor is moved to self.args.device and the result is stored in ret. However, the original loss tensor is returned, so the tensor is not actually on the correct device when returned. The value of ret is never used.

Suggested change
def post_training_step(self, loss):
if loss.device != self.args.device:
ret = loss.to(self.args.device, non_blocking=True)
return loss
def post_training_step(self, loss):
if loss.device != self.args.device:
loss = loss.to(self.args.device, non_blocking=True)
return loss

Comment on lines +123 to +128
try:
self.accelerator.state.device_ids = [0]
self.accelerator.state.num_processes = 1
self.accelerator.state.num_gpus = 1
except Exception:
pass
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Using a broad except Exception: pass is risky because it silences all errors, including system-level ones like KeyboardInterrupt or SystemExit, which can make debugging extremely difficult. If an exception is expected, it should be caught specifically. At the very least, the exception should be logged to provide visibility into potential issues. The hardcoded values for device_ids, num_processes, and num_gpus also seem to assume a single-GPU setup and could cause problems in a distributed environment.

Suggested change
try:
self.accelerator.state.device_ids = [0]
self.accelerator.state.num_processes = 1
self.accelerator.state.num_gpus = 1
except Exception:
pass
try:
self.accelerator.state.device_ids = [0]
self.accelerator.state.num_processes = 1
self.accelerator.state.num_gpus = 1
except Exception as e:
logger.warning("Could not override accelerator state. This may be expected in a multi-GPU environment. Error: %s", e)


logger = logging.get_logger(__name__)

class KTDporainer(DPOTrainer):
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There is a typo in the class name KTDporainer. It should likely be KTDpoTrainer to follow common naming conventions and improve readability.

Suggested change
class KTDporainer(DPOTrainer):
class KTDpoTrainer(DPOTrainer):

self.model.save_pretrained(output_dir)

def _move_model_to_device(self, model, device):
print("[KTrainer] Due to the placement feature in KTransformers, skip moving model to", device)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Using print for logging is generally discouraged in a library. It's better to use the logging module, which is already imported as logger. This provides more control over log levels and output streams, and allows users of the library to configure logging as they see fit.

Suggested change
print("[KTrainer] Due to the placement feature in KTransformers, skip moving model to", device)
logger.info("[KTrainer] Due to the placement feature in KTransformers, skip moving model to %s", device)

@JimmyPeilinLi JimmyPeilinLi marked this pull request as draft November 20, 2025 05:42
@poryfly poryfly marked this pull request as ready for review November 20, 2025 11:19
@poryfly poryfly marked this pull request as draft November 20, 2025 11:20
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.

1 participant