Guard TRL estimate_tokens warnings_issued writes in patched RL trainers#4034
Guard TRL estimate_tokens warnings_issued writes in patched RL trainers#4034danielhanchen wants to merge 1 commit intomainfrom
Conversation
Summary of ChangesHello @danielhanchen, 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 addresses a potential runtime error in TRL trainers when 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 a defensive guard to initialize model.warnings_issued in patched TRL RL trainers. This prevents potential AttributeError exceptions when TRL attempts to write to this attribute, which may be missing or not a dictionary in some environments. The implementation is robust, handling cases where the attribute is missing or has an unexpected non-dictionary type. I have one suggestion to make the exception handling more specific, which is a minor improvement to an already solid patch.
| " elif not isinstance(_warnings_issued, dict):\n" | ||
| " try:\n" | ||
| " model.warnings_issued = dict(_warnings_issued)\n" | ||
| " except Exception:\n" |
There was a problem hiding this comment.
While using a broad except Exception: is very defensive, it's generally better to catch more specific exceptions to avoid masking unrelated errors. The dict() constructor on an invalid input will typically raise TypeError (if the object is not iterable) or ValueError (if items are not key-value pairs). Catching these specific exceptions would make the code's intent clearer.
| " except Exception:\n" | |
| " except (TypeError, ValueError):\n" |
|
Ref: #4032 (comment) |
Summary
warnings_issuedinitializer in the generated Unsloth RL trainer init pathmodel.warnings_issued["estimate_tokens"] = Truewarnings_issuedvalues into a dict fallbackRoot Cause
On Transformers 5.1.0 with some PEFT/model wrappers,
model.warnings_issuedmay be missing or not a dict during trainer initialization. TRL later writes theestimate_tokenskey unconditionally, which can raiseAttributeError.Changes
unsloth/models/rl.py_patch_trl_rl_trainers, after logits checks, inject:getattr(model, "warnings_issued", None)guard{}when missingValidation
python -m compileall unsloth/models/rl.pytemp/validate_unsloth_warnings_patch.logNotes
warnings_issuedis already valid