Skip to content

Comments

Extend TRL experimental patching and vLLM readiness#3984

Open
danielhanchen wants to merge 2 commits intomainfrom
fix/trl-experimental-compat
Open

Extend TRL experimental patching and vLLM readiness#3984
danielhanchen wants to merge 2 commits intomainfrom
fix/trl-experimental-compat

Conversation

@danielhanchen
Copy link
Contributor

Summary

  1. Patch TRL experimental trainers and already-imported experimental modules for backward compatibility.
  2. Make RL trainer patching safer with optional logging and DataCollatorForPreference handling.
  3. Add a vLLM readiness fallback using the metrics endpoint.

Testing

  1. Ran Llama3 8B ORPO notebook with transformers 4.57.6 and 5.0.0 using trl 0.27.1.
  2. Ran Meta Synthetic Data Llama3 2 3B notebook with transformers 4.57.6 and 5.0.0 using trl 0.27.1.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 enhances the Unsloth library by improving compatibility and robustness with the TRL library and vLLM. It addresses issues related to patching TRL trainers, especially experimental ones, and provides a more reliable way to determine vLLM readiness. The changes also incorporate safety measures such as optional logging and better data collation handling.

Highlights

  • TRL Trainer Patching: Extends patching to TRL experimental trainers and ensures backward compatibility for already imported experimental modules.
  • RL Trainer Safety: Improves the safety of RL trainer patching with optional logging and handling for DataCollatorForPreference.
  • vLLM Readiness: Adds a fallback mechanism for vLLM readiness detection using the metrics endpoint.

🧠 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
  • unsloth/dataprep/synthetic.py
    • Added a fallback mechanism to check vLLM readiness using the /metrics endpoint if the standard log detection fails.
  • unsloth/models/rl.py
    • Patches TRL trainers to unwrap models for generation.
    • Extends patching to trl.experimental modules and handles already imported experimental modules.
    • Adds logging for RL trainer patching and handles DataCollatorForPreference.
    • Adds logic to skip patching if a module is unavailable in the current TRL version.
  • unsloth/trainer.py
    • Patches TRL modules for backward compatibility, focusing on trainer initialization.
    • Extends patching to trl.experimental modules for comprehensive coverage.
Activity
  • Ran Llama3 8B ORPO notebook with transformers 4.57.6 and 5.0.0 using trl 0.27.1.
  • Ran Meta Synthetic Data Llama3 2 3B notebook with transformers 4.57.6 and 5.0.0 using trl 0.27.1.
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.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1fb7054f53

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

with urllib.request.urlopen(
"http://localhost:8000/metrics", timeout = 2
) as resp:
if getattr(resp, "status", None) == 200:

Choose a reason for hiding this comment

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

P1 Badge Validate metrics source before marking vLLM as ready

This fallback treats any 200 from http://localhost:8000/metrics as proof that the newly spawned vLLM is ready, but it does not verify that the response comes from self.vllm_process (or even from vLLM at all). If another local service is already bound to port 8000 and the launched vLLM never reaches readiness, ready is still flipped to true and the method proceeds as if startup succeeded, which can route subsequent synthetic-data requests to the wrong endpoint and break runs in a hard-to-diagnose way.

Useful? React with 👍 / 👎.

Copy link
Contributor

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

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 introduces several improvements to TRL patching and vLLM integration. The changes make the patching process more robust by extending it to experimental TRL modules, replacing unsafe eval and exec calls with safer alternatives like importlib and setattr, and adding more resilient error handling. A fallback for vLLM readiness detection using the metrics endpoint is also a nice addition.

My review includes a couple of suggestions for improvement:

  • In unsloth/dataprep/synthetic.py, I recommend using the requests library, which is already imported, for the vLLM readiness check to simplify the code.
  • In unsloth/models/rl.py, I suggest adding optional logging to the try...except blocks that currently fail silently, which will aid in debugging potential patching issues.

Comment on lines +283 to +290
try:
import urllib.request

with urllib.request.urlopen(
"http://localhost:8000/metrics", timeout = 2
) as resp:
if getattr(resp, "status", None) == 200:
print(
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Using urllib.request here is a bit verbose, while requests is already imported at the top of the file. Using requests would make the code cleaner and more consistent. Also, catching a specific requests.exceptions.RequestException is better practice than a broad except Exception: pass.

Suggested change
try:
import urllib.request
with urllib.request.urlopen(
"http://localhost:8000/metrics", timeout = 2
) as resp:
if getattr(resp, "status", None) == 200:
print(
try:
resp = requests.get("http://localhost:8000/metrics", timeout=2)
if resp.status_code == 200:
print("vLLM metrics endpoint detected; proceeding without ready log.")
ready = True
except requests.exceptions.RequestException:
pass

Comment on lines +1231 to +1258
return

# Patch Trainer
exec(
f"trl.{RLTrainer_name} = created_module.Unsloth{RLTrainer_name}",
locals(),
globals(),
)
exec(
f"trl.trainer.{RLTrainer_name} = created_module.Unsloth{RLTrainer_name}",
locals(),
globals(),
)
exec(
f"trl.trainer.{trainer_file}.{RLTrainer_name} = created_module.Unsloth{RLTrainer_name}",
locals(),
globals(),
)
try:
setattr(
trl, RLTrainer_name, getattr(created_module, f"Unsloth{RLTrainer_name}")
)
except Exception:
pass
try:
root_module = importlib.import_module(module_prefix)
setattr(
root_module,
RLTrainer_name,
getattr(created_module, f"Unsloth{RLTrainer_name}"),
)
except Exception:
pass
try:
setattr(
trainer, RLTrainer_name, getattr(created_module, f"Unsloth{RLTrainer_name}")
)
except Exception:
pass

# Patch Config
exec(
f"trl.{RLConfig_name} = created_module.Unsloth{RLConfig_name}",
locals(),
globals(),
)
exec(
f"trl.trainer.{RLConfig_name} = created_module.Unsloth{RLConfig_name}",
locals(),
globals(),
)
exec(
f"trl.trainer.{trainer_file}.{RLConfig_name} = created_module.Unsloth{RLConfig_name}",
locals(),
globals(),
)
try:
setattr(trl, RLConfig_name, getattr(created_module, f"Unsloth{RLConfig_name}"))
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

These try...except Exception: pass blocks are very broad and completely silent. While this makes the patching robust against failures, it can also hide subtle bugs, such as a typo in a name causing an AttributeError that gets swallowed. This would lead to a silent patching failure which can be hard to debug. Consider logging these exceptions using _log_optional to make debugging easier if a patch doesn't apply as expected. For example:

try:
    setattr(trl, RLTrainer_name, getattr(created_module, f"Unsloth{RLTrainer_name}"))
except Exception as e:
    _log_optional(f"Unsloth: Failed to patch {RLTrainer_name} in trl: {e}")

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.

1 participant