Skip to content

jsd implement for opd#499

Open
kokolerk wants to merge 3 commits intoagentscope-ai:mainfrom
kokolerk:main
Open

jsd implement for opd#499
kokolerk wants to merge 3 commits intoagentscope-ai:mainfrom
kokolerk:main

Conversation

@kokolerk
Copy link
Contributor

@kokolerk kokolerk commented Feb 6, 2026

Description

  1. Implement the Jensen-Shannon Divergence for on-policy distillation.
  2. Add the opd_gsm8k_jsd.yaml in examples
  3. Add the description in the README

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has passed all tests
  • Docstrings have been added/updated in Google Style
  • Documentation has been updated
  • Code is ready for review

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @kokolerk, 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 expands the on-policy distillation framework by integrating Jensen-Shannon Divergence (JSD) as a new method for calculating advantages. This provides a robust, symmetric, and smoothed alternative to Kullback-Leibler divergence for guiding student model training. The changes include the core mathematical implementation of JSD, its registration within the system, and a practical example configuration to facilitate its immediate use and testing.

Highlights

  • Jensen-Shannon Divergence (JSD) Implementation: Implemented Jensen-Shannon Divergence as a new advantage function (JSDAdvantage) for on-policy distillation, allowing for an alternative method to measure the difference between student and teacher models.
  • New Configuration Example: Added a new configuration file, opd_gsm8k_jsd.yaml, demonstrating how to use the JSD-based on-policy distillation for the GSM8K task, complete with student and teacher model specifications.
  • Documentation Update: Updated the README.md in the examples/opd_gsm8k directory to include a new 'Algorithm Variants' section, detailing both KL-based and JSD-based on-policy distillation, along with instructions for running the new JSD example.
Changelog
  • examples/opd_gsm8k/README.md
    • Introduced a new 'Algorithm Variants' section to distinguish between KL-based and JSD-based on-policy distillation.
    • Provided a detailed description of the JSD algorithm, its advantage computation formula, and configurable parameters (kl_coef, lambda_coef).
    • Added specific command-line instructions for running the new JSD-based OPD example.
  • examples/opd_gsm8k/opd_gsm8k_jsd.yaml
    • Created a new YAML configuration file for JSD-based on-policy distillation.
    • Set algorithm_type to jsd and configured advantage_fn_args with kl_coef and lambda_coef.
    • Specified student and teacher models (Qwen/Qwen2.5-1.5B-Instruct and Qwen/Qwen2.5-Math-7B-Instruct) and the on_policy_distill_math_workflow.
  • trinity/algorithm/advantage_fn/init.py
    • Registered the new jsd advantage function, mapping it to the JSDAdvantage class, making it discoverable by the system.
  • trinity/algorithm/advantage_fn/jsd_advantage.py
    • Added a new file implementing the JSDAdvantage class, which computes Jensen-Shannon Divergence.
    • Included the _js_divergence_per_token method for calculating JSD based on student and teacher log probabilities.
    • Defined the __call__ method to compute advantages as negative JSD, apply response masks, and return relevant metrics.
Activity
  • No specific activity (comments, reviews, etc.) has been recorded for this pull request yet.
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 Jensen-Shannon Divergence (JSD) for on-policy distillation, including the core advantage function, an example configuration, and documentation updates. The overall structure is sound. However, I've identified a critical issue in the mathematical implementation of the JSD calculation in jsd_advantage.py. The formula for per-token KL divergence appears to be incorrect, which would lead to an inaccurate JSD computation and could negatively impact model training.

@hiyuchang hiyuchang requested a review from garyzhang99 February 6, 2026 08:43
@pan-x-c
Copy link
Collaborator

pan-x-c commented Feb 6, 2026

Please update the "Supported Algorithms" section in the README.md, README_zh.md, docs/sphinx_doc/source/main.md, and docs/sphinx_doc/source_zh/main.md files accordingly.

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