Extend TRL experimental patching and vLLM readiness#3984
Extend TRL experimental patching and vLLM readiness#3984danielhanchen wants to merge 2 commits 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 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
🧠 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.
💡 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: |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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 therequestslibrary, which is already imported, for the vLLM readiness check to simplify the code. - In
unsloth/models/rl.py, I suggest adding optional logging to thetry...exceptblocks that currently fail silently, which will aid in debugging potential patching issues.
| try: | ||
| import urllib.request | ||
|
|
||
| with urllib.request.urlopen( | ||
| "http://localhost:8000/metrics", timeout = 2 | ||
| ) as resp: | ||
| if getattr(resp, "status", None) == 200: | ||
| print( |
There was a problem hiding this comment.
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.
| 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 |
| 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}")) |
There was a problem hiding this comment.
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}")
Summary
Testing