Skip to content

[trl] use non lora model as base for RL - #3895

Merged
danielhanchen merged 2 commits into
unslothai:mainfrom
Datta0:rl_ref_trl
Jan 15, 2026
Merged

danielhanchen merged 2 commits into
unslothai:mainfrom
Datta0:rl_ref_trl

Conversation

@Datta0

@Datta0 Datta0 commented Jan 15, 2026 •

Copy link
Copy Markdown
Contributor

TRL updated the RL trainers to use Lora weights as reference model when 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:
image

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

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @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 target_parameters, thereby improving the robustness and accuracy of RL fine-tuning.

Highlights

  • Corrected RL Reference Model: The pull request ensures that Reinforcement Learning (RL) trainers use the original base model as a reference, rather than a LoRA-adapted (SFT) model, when a PEFT model is loaded.
  • PEFT Compatibility Fix: This change resolves an incompatibility issue where PEFT does not allow multiple adapters when target_parameters are used, which is particularly relevant for MoE (Mixture of Experts) models.
  • Dynamic Code Modification: The implementation involves dynamically commenting out the add_adapter("ref") block within the patch_functions method in unsloth/models/rl.py using regular expressions, ensuring the base model's logits are used.

🧠 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 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. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 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.

Comment thread unsloth/models/rl.py
stripped = line.lstrip()
leading_ws = line[: len(line) - len(stripped)]
commented_lines.append(f"{leading_ws}# {stripped}")
else:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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."
)

@danielhanchen
danielhanchen merged commit 56ce1bc into unslothai:main Jan 15, 2026
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
[trl] use non lora model as base for RL
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