Fix crash when trl.experimental.openenv is unavailable#3787
Fix crash when trl.experimental.openenv is unavailable#3787danielhanchen merged 3 commits intounslothai:mainfrom
Conversation
Summary of ChangesHello @Fizza-Mukhtar, 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 critical compatibility issue where Unsloth's DDP fine-tuning could crash if 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. 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 correctly addresses a crash that occurs when trl.experimental.openenv is unavailable by making its usage optional. The changes guard the import and downstream logic, preventing runtime failures and improving compatibility with different TRL versions. I have one suggestion to simplify the implementation by removing an unnecessary flag, which will make the code more concise and Pythonic.
unsloth/models/rl_replacements.py
Outdated
| openenv_available = False | ||
| try: | ||
| import trl.experimental.openenv.utils as openenv_utils | ||
| import trl.experimental.openenv as openenv | ||
|
|
||
| openenv_available = True | ||
| except ImportError as e: | ||
| logger.info(f"Unsloth: Failed to import trl openenv: {e}") | ||
|
|
||
| if not openenv_available: | ||
| logger.info( | ||
| "Unsloth: trl.experimental.openenv not available — skipping RL openenv patches." | ||
| ) |
There was a problem hiding this comment.
The use of the openenv_available flag makes the code more verbose than necessary. You can simplify this logic by moving the second log message and the return statement into the except block. This avoids the need for the flag and the subsequent if check, making the code cleaner and more direct.
| openenv_available = False | |
| try: | |
| import trl.experimental.openenv.utils as openenv_utils | |
| import trl.experimental.openenv as openenv | |
| openenv_available = True | |
| except ImportError as e: | |
| logger.info(f"Unsloth: Failed to import trl openenv: {e}") | |
| if not openenv_available: | |
| logger.info( | |
| "Unsloth: trl.experimental.openenv not available — skipping RL openenv patches." | |
| ) | |
| try: | |
| import trl.experimental.openenv.utils as openenv_utils | |
| import trl.experimental.openenv as openenv | |
| except ImportError as e: | |
| logger.info(f"Unsloth: Failed to import trl openenv: {e}") | |
| logger.info( | |
| "Unsloth: trl.experimental.openenv not available — skipping RL openenv patches." | |
| ) | |
| return |
e8bfdf3 to
2b1350f
Compare
for more information, see https://pre-commit.ci
|
Thanks @Fizza-Mukhtar ! |
What this PR does
Makes usage of
trl.experimental.openenvfully optional by guarding imports and downstream logic.Why
Some TRL versions do not provide
trl.experimental.openenv, causing DDP fine-tuning to fail at runtime.This ensures Unsloth skips RL openenv patches safely when unavailable.
Result
Fixes #3722