Repository navigation
[trl] use non lora model as base for RL - #3895
Conversation
Summary of ChangesHello @Datta0, 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 refines the behavior of RL trainers to correctly utilize the base model as the reference when working with PEFT models. By preventing the LoRA-adapted model from being used as the reference, it addresses a critical issue that could lead to incorrect training outcomes and resolves a technical limitation within PEFT regarding multiple adapters and 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. 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 modifies the RL trainer to use the base model as a reference instead of the LoRA-adapted model. This is achieved by monkey-patching the trainer's __init__ method at runtime to comment out the code block responsible for creating the 'ref' adapter. This change is particularly important for MoE models where having multiple adapters can be problematic.
The implementation uses a regular expression to find and modify the target code, which is consistent with the existing patching style in the codebase. My review includes one suggestion to improve the robustness of this patch. I've recommended adding a check to verify that the substitution was successful and to log a warning if it fails. This will help detect when upstream changes in the TRL library break this patch, preventing silent failures.
| stripped = line.lstrip() | ||
| leading_ws = line[: len(line) - len(stripped)] | ||
| commented_lines.append(f"{leading_ws}# {stripped}") | ||
| else: |
There was a problem hiding this comment.
The monkey-patching relies on a specific regex pattern which could break if the underlying TRL code changes. If the pattern doesn't match, re.sub will fail silently, and the old behavior (using the LoRA model as reference) will persist. This could lead to subtle bugs that are hard to debug.
To make this more robust, I suggest checking if the substitution was successful and logging a warning if it wasn't. This will alert developers if an upstream change in TRL breaks this patch.
| else: | |
| init_before_patch = init | |
| init = re.sub(add_adapter_block_pattern, comment_out_block, init, flags=re.DOTALL) | |
| if init == init_before_patch: | |
| logger.warning( | |
| "Unsloth: Failed to patch RL trainer to use base model as reference. This might be due to an update in TRL." | |
| ) |
[trl] use non lora model as base for RL
TRL updated the RL trainers to use Lora weights as
reference modelwhen loading a PEFT model. We would ideally want to avoid that and use the base model. If in case someone wants to use the SFT model as the reference model for RL, they might want to merge the weights back to the base model and use it.UnslothGRPOTrainer After the fix:

Side Note that having two adapters with target_parameters, especially for MoE models, is problematic because peft doesn't allow it.