Guard optional vLLM imports when extension is broken#4068
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 significantly improves the robustness of Unsloth's import mechanism, particularly when users have an installed vLLM library with a broken or incompatible native extension. By gracefully handling import errors related to vLLM's binary components and switching to lazy loading for vLLM utilities, Unsloth can now import successfully without being blocked by an optional, misconfigured dependency. This ensures a smoother user experience, allowing core Unsloth functionalities to remain accessible even if vLLM is not perfectly set up, while still providing clear warnings when vLLM-specific features might be unavailable. 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
|
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Code Review
This pull request provides a robust solution to prevent import unsloth from crashing when an optional dependency, vLLM, is installed but has a broken native extension. The changes are well-implemented, using lazy loading for vLLM utilities to defer imports until they are actually needed, and adding careful error handling to catch and gracefully manage import failures. The new logic correctly distinguishes between critical configuration mismatches (like with transformers) that should still fail, and ABI-incompatibility issues that can be safely bypassed with a warning. The addition of a new test suite with clever use of mocking and AST parsing ensures the new import guarding mechanisms are thoroughly validated. Overall, this is an excellent improvement that significantly enhances the robustness and user experience of the library.
| f"Original error: {error}" | ||
| ) from error | ||
|
|
||
| def _is_broken_vllm_extension(error): |
There was a problem hiding this comment.
Why are these scoped within fix_vllm_guided_decoding_params and not outside?
* Guard optional vLLM imports when extension is broken * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove vLLM import guard tests from PR scope * Block broken vLLM imports like causal_conv1d --------- Co-authored-by: Daniel Hanchen <danielhanchen@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Summary
disable_broken_causal_conv1dvllm*entries fromsys.modulesimportlib.util.find_specto hidevllmsys.meta_pathblocker that raisesModuleNotFoundErrorforvllm*fix_vllm_guided_decoding_paramsto trigger this blocker on broken-extensionImportError/OSErrordisable_broken_vllm()early inunsloth/__init__.pyso broken optional vLLM does not crashimport unslothWhy
In environments where
vllm/_C.abi3.sois incompatible with the installed torch/CUDA stack, import-time failures should degrade gracefully for non-vLLM workflows, similar to the causal-conv1d fallback behavior.Validation
PYTHONPATH=<patched-branch> python -u -c "import importlib.util, unsloth; print(importlib.util.find_spec('vllm')); import vllm"import unslothsucceedsfind_spec('vllm')returnsNoneimport vllmraisesModuleNotFoundError