[Feat] Add on-policy distillation support#964
[Feat] Add on-policy distillation support#964garrett4wade merged 14 commits intoinclusionAI:mainfrom
Conversation
Summary of ChangesHello, 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 AReaL's capabilities by introducing on-policy knowledge distillation (KD) and a combined KD + Reinforcement Learning (KDRL) framework. This allows student models to learn from a teacher's policy while actively exploring with their own, reducing distribution mismatch and improving training stability. The changes involve new configuration options for teacher models, modifications to the PPO loss function to incorporate distillation, and updates to the RL trainer to manage the teacher model and its contributions to the learning process. Highlights
🧠 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. Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces on-policy knowledge distillation, which is a valuable feature. The implementation looks mostly correct, but I've identified a few areas for improvement. There's a significant code duplication in rl_trainer.py when creating the teacher model, which also contains a bug that will cause failures in SPMD mode. Additionally, there's an inconsistency in the documentation and comments regarding the type of KL divergence used in the loss function in ppo/actor.py. Finally, the new README file contains a broken link to a reference paper. Addressing these points will improve the maintainability and correctness of the new feature.
|
Actually, I would recommend using a rollout engine combined with a custom workflow to address this issue, as no training is required for teacher...? |
|
/gemini review |
There was a problem hiding this comment.
Code Review
The pull request successfully adds support for on-policy knowledge distillation (KD) and combined KD + RL training. The new TeacherConfig dataclass is well-integrated into the PPOConfig, and the distillation loss logic is correctly implemented in areal/trainer/ppo/actor.py. The _create_teacher method in areal/trainer/rl_trainer.py handles the teacher model's initialization, and the provided README.md and example YAML configuration offer clear guidance for using the new feature. The overall implementation is clean and aligns with the stated objective of the PR.
Note: Security Review did not run due to the size of the PR.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Code Review
This pull request introduces on-policy knowledge distillation, which is a valuable addition. The implementation is mostly solid, but I've identified a few areas for improvement in areal/trainer/rl_trainer.py concerning code consistency and maintainability. Specifically, the creation and initialization of the teacher component could be refactored to align with how other components like actor and critic are handled, which would also resolve a bug where a backend is missing. Additionally, the new README.md for distillation contains a potentially confusing explanation of the KL divergence that could be clarified for better understanding.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces on-policy knowledge distillation, which is a great feature. The implementation adds a TeacherConfig, integrates a teacher model into the PPOTrainer, and implements the distillation loss.
My review has identified a few points for improvement:
- There appears to be an issue in the pure knowledge distillation loss calculation where a tensor that should be detached is not, potentially leading to incorrect gradients.
- There is some code duplication in the creation of the
teacherengine, which could be refactored for better maintainability. - The new
README.mdhas a slightly confusing explanation regarding loss minimization that could be clarified.
Overall, the changes are well-structured. Addressing these points will help improve the correctness and maintainability of the new feature.
| where the reward $R_{i,t} = \log \pi_T(o_{i,t}) - \log \pi_\theta(o_{i,t})$. This encourages the student to increase the probability of tokens the teacher prefers and suppress those it deems unlikely. | ||
|
|
||
| - Implementation Detail: | ||
| During pure KD, we need to set `rl_loss_weight` to 0, so the implementation estimates the RKL gradient using importance sampling. The code calculates the reward as teacher_logp - logprobs ($R_{i,t}$) and applies a negative coefficient to the loss to perform minimization (check `areal/trainer/ppo/actor.py`). |
There was a problem hiding this comment.
The phrase "applies a negative coefficient to the loss to perform minimization" is slightly misleading. In gradient-based optimization, applying a negative coefficient to a loss term corresponds to maximizing the objective that the term represents (in this case, the expected reward). For better clarity, you could rephrase this to state that the negative coefficient is used to perform gradient ascent on the reward objective. For example: "...and applies a negative coefficient to the loss, effectively performing gradient ascent to maximize the reward."
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
garrett4wade
left a comment
There was a problem hiding this comment.
LGTM but somes nits
areal/trainer/ppo/actor.py
Outdated
| teacher_kl = teacher_logp - logprobs.detach() | ||
| prob_ratio = torch.exp(logprobs - old_logp) | ||
|
|
||
| loss_per_token = prob_ratio * teacher_kl * loss_mask |
There was a problem hiding this comment.
Why is prob_ratio required here, yet it's ignored in line 460?
garrett4wade
left a comment
There was a problem hiding this comment.
LGTM except for a nit
areal/trainer/ppo/actor.py
Outdated
| stats_tracker.denominator(token=loss_mask) | ||
| stats_tracker.stat( | ||
| rkl_loss=-1 * rkl_weighted_term, | ||
| denominator="token", | ||
| ) |
There was a problem hiding this comment.
nit: L479 has logged n_valid_tokens, so you should remove the denominator log and defer stat logging after L479.
* [feat] Add on-policy knowledge distillation support * Correct file extension * Delete comment on distill_loss_weight Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * explain RKL and Joint loss * Add reference to global README * teacher config refactoring * refactor: remove redundant stats_tracker denominator logging for KD loss --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
* [feat] Add on-policy knowledge distillation support * Correct file extension * Delete comment on distill_loss_weight Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * explain RKL and Joint loss * Add reference to global README * teacher config refactoring * refactor: remove redundant stats_tracker denominator logging for KD loss --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Description
This PR adds support for on-policy knowledge distillation (KD) in AReaL, enabling a student model to learn from a teacher while sampling trajectories from its own policy, thereby reducing distribution mismatch. It also includes combined KD + RL training, allowing the student to imitate the teacher while simultaneously optimizing reward through reinforcement learning.
Related Issue
#960
Type of Change
work as expected)
Checklist
jb build docs/gemini review)Breaking Change Details (if applicable):
Additional Context
Need help? Check the Contributing Guide or ask in
GitHub Discussions!