Trl 0.27.0 update#3965
Conversation
Summary of ChangesHello @pluesclues, 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 focuses on updating the Unsloth library to maintain seamless compatibility with the latest versions of the 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
|
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e09f7165d7
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| # to ensure backwards compatibility with trl 0.15.2 and maybe even 0.17 | ||
| loss, completion_length, mean_kl = grpo_accumulated_loss( | ||
| loss, completion_length, mean_kl, coef_1 = grpo_accumulated_loss( | ||
| trainer = self, |
There was a problem hiding this comment.
Preserve backward-compatibility return arity
The backward-compatibility branch now unpacks four values from grpo_accumulated_loss, but that path exists specifically for older TRL versions (“0.15.2 and maybe even 0.17”) where the function historically returned only three values. In those environments, this will raise ValueError: not enough values to unpack at runtime, defeating the compatibility fallback. Consider guarding on the returned tuple length or only unpacking coef_1 when the underlying implementation provides it.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Code Review
This pull request updates unsloth to be compatible with trl version 0.27.0. The changes primarily involve monkey-patching TRL's trainers (GRPOTrainer) and configuration classes to handle API changes and remove conflicting logic, especially for newer TRL versions. The modifications are done through string manipulation of source code, which is consistent with the existing patching mechanism in the codebase.
My review found a critical runtime error due to the use of undefined functions (nanmin, nanmax) and a medium-severity issue related to code style and robustness in version detection logic. I've provided suggestions to fix these issues.
|
|
||
| gathered_low_clip = self.accelerator.gather(low_clip) | ||
| self._metrics[mode]["clip_ratio/low_mean"].append( | ||
| gathered_low_clip.nanmean().item() |
There was a problem hiding this comment.
The functions nanmin and nanmax are used here without being defined or imported. This will lead to a NameError at runtime. You should use torch.nanmin and torch.nanmax instead, as torch is available in the execution context of this code.
| gathered_low_clip = self.accelerator.gather(low_clip) | |
| self._metrics[mode]["clip_ratio/low_mean"].append( | |
| gathered_low_clip.nanmean().item() | |
| self._metrics[mode]["clip_ratio/low_min"].append(torch.nanmin(gathered_low_clip).item()) | |
| gathered_high_clip = self.accelerator.gather(high_clip) | |
| self._metrics[mode]["clip_ratio/high_mean"].append(gathered_high_clip.nanmean().item()) | |
| self._metrics[mode]["clip_ratio/high_max"].append(torch.nanmax(gathered_high_clip).item()) |
| try: | ||
| trl_version = Version(trl_version_raw) | ||
| except Exception: | ||
| try: | ||
| trl_version = Version(importlib_version("trl")) | ||
| except Exception: | ||
| trl_version = Version("0.0.0") |
There was a problem hiding this comment.
Using a broad except Exception: can hide unexpected errors and make debugging harder. It's better to catch more specific exceptions. For example, Version() might raise a ValueError, and importlib_version() can raise PackageNotFoundError (which you'd need to import). Using more specific exceptions would make this code more robust and maintainable.
for more information, see https://pre-commit.ci
* Update rl_replacements.py * Update rl_replacements.py * Update rl.py * Update rl_replacements.py * Update rl_replacements.py * Update rl.py * Update rl.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update rl_replacements.py * Update rl.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update rl_replacements.py, remove chat template from codexes commits * Update rl.py, got rid of gradient checkpointing code that did not work --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This PR allows for grpo to be compatible with trl 0.27.0 it assumes formulation of notebooks just like the ones in unsloth notebooks repo. Relies on unslothai/unsloth-zoo#457.