Skip to content

Conversation

@hjh0119
Copy link
Collaborator

@hjh0119 hjh0119 commented Dec 16, 2025

Summary

This PR fixes GKD (Generalized Knowledge Distillation) trainer issues with multimodal models (e.g., Qwen2.5-VL) and simplifies the template encoding logic by removing the dedicated gkd mode.

Changes

1. Remove gkd mode from Template (base.py)

  • Removed _gkd_encode method and _gkd_data_collator method
  • Removed gkd from mode type definitions and related branching logic
  • GKD now uses standard train mode for full message encoding and pt mode for prompt-only encoding

2. Refactor GKDTrainer encoding logic (gkd_trainer.py)

  • Added encode_prompt_only parameter to _prepare_batch_inputs method
  • When encode_prompt_only=True: uses pt mode and removes assistant response content for generation scenarios (on-policy/seq_kd)
  • When encode_prompt_only=False: uses train mode for full message encoding (offline dataset/vLLM generated)
  • Simplified generate_on_policy_outputs to use input_ids directly instead of separate prompts field
  • Added unwrapped_model.eval() call in seq_kd branch for consistency
  • Renamed local variable inputs to encoded_inputs for better code clarity

3. Extend _template_context in RolloutTrainerMixin (rollout_mixin.py)

  • Added optional mode parameter to support template mode switching
  • Added optional max_length parameter for flexible length control

4. Update mode mapping (rlhf.py)

  • Changed GKD mode mapping from 'gkd' to 'train'

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @hjh0119, 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 refactors the Generalized Knowledge Distillation (GKD) training process by deprecating a dedicated 'gkd' template mode. It streamlines the encoding pipeline within the GKDTrainer to intelligently utilize existing 'train' and 'pt' template modes, which resolves issues with multimodal models and simplifies template management. The changes also introduce more dynamic control over template parameters, enhancing the system's flexibility and maintainability.

Highlights

  • GKD Mode Removal: The dedicated 'gkd' mode, its encoding, and data collator methods have been removed from the template system, simplifying the overall template logic.
  • Unified Template Usage for GKD: Generalized Knowledge Distillation (GKD) now leverages the standard 'train' mode for full message encoding and 'pt' mode for prompt-only encoding, eliminating the need for a specialized 'gkd' template mode.
  • Refactored GKDTrainer Encoding Logic: The GKDTrainer's _prepare_batch_inputs method has been enhanced with an encode_prompt_only parameter, allowing dynamic switching between encoding full messages or just prompts, which is crucial for on-policy generation and sequential knowledge distillation scenarios.
  • Flexible Template Context: The _template_context in RolloutTrainerMixin now supports optional mode and max_length parameters, providing greater control over template settings for specific operations.
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 is a nice refactoring that simplifies the GKD trainer by removing the dedicated 'gkd' mode and reusing the existing 'train' and 'pt' modes. This improves code clarity and maintainability, especially for multimodal models. The changes are well-structured and align with the description. I've found one critical issue and one medium-severity issue that should be addressed.

@hjh0119 hjh0119 merged commit 851ff69 into modelscope:main Dec 16, 2025
2 of 3 checks passed
@hjh0119 hjh0119 deleted the fix-gkd branch December 16, 2025 03:45
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