Repository navigation
Fix 4.47 issue - #1182
Merged
Merged
Fix 4.47 issue#1182
Conversation
orginal -> original
* Fix DPO, ORPO (#1177) * Fix TRL * Update mistral.py * Patch processing_class * Update tokenizer_utils.py * Update tokenizer_utils.py * Update tokenizer_utils.py * Update tokenizer_utils.py * Update tokenizer_utils.py * Update tokenizer_utils.py * Installation guide (#1165) * chore: update chat_templates.py (#1166) orginal -> original * Disable Flex Attention * Update tokenizer_utils.py * Update _utils.py * n_items * Update cross_entropy_loss.py * Fix DPO, ORPO * Update _utils.py --------- Co-authored-by: timothelaborie <[email protected]> Co-authored-by: Ikko Eltociear Ashimine <[email protected]> * Add warning for missing Unpack and KwargsForCausalLM in older Transformers versions --------- Co-authored-by: Daniel Han <[email protected]> Co-authored-by: timothelaborie <[email protected]> Co-authored-by: Ikko Eltociear Ashimine <[email protected]>
abiswas-realadvice
pushed a commit
to abiswas-realadvice/unsloth
that referenced
this pull request
May 14, 2026
* Fix TRL * Update mistral.py * Patch processing_class * Update tokenizer_utils.py * Update tokenizer_utils.py * Update tokenizer_utils.py * Update tokenizer_utils.py * Update tokenizer_utils.py * Update tokenizer_utils.py * Installation guide (unslothai#1165) * chore: update chat_templates.py (unslothai#1166) orginal -> original * Disable Flex Attention * Update tokenizer_utils.py * Update _utils.py * n_items * Update cross_entropy_loss.py * Fix DPO, ORPO * Update _utils.py * Update _utils.py * fix/transformers-unpack (unslothai#1180) * Fix DPO, ORPO (unslothai#1177) * Fix TRL * Update mistral.py * Patch processing_class * Update tokenizer_utils.py * Update tokenizer_utils.py * Update tokenizer_utils.py * Update tokenizer_utils.py * Update tokenizer_utils.py * Update tokenizer_utils.py * Installation guide (unslothai#1165) * chore: update chat_templates.py (unslothai#1166) orginal -> original * Disable Flex Attention * Update tokenizer_utils.py * Update _utils.py * n_items * Update cross_entropy_loss.py * Fix DPO, ORPO * Update _utils.py --------- Co-authored-by: timothelaborie <[email protected]> Co-authored-by: Ikko Eltociear Ashimine <[email protected]> * Add warning for missing Unpack and KwargsForCausalLM in older Transformers versions --------- Co-authored-by: Daniel Han <[email protected]> Co-authored-by: timothelaborie <[email protected]> Co-authored-by: Ikko Eltociear Ashimine <[email protected]> * Update cross_entropy_loss.py * Update _utils.py * Update _utils.py --------- Co-authored-by: timothelaborie <[email protected]> Co-authored-by: Ikko Eltociear Ashimine <[email protected]> Co-authored-by: Edd <[email protected]>
Aafimalek
pushed a commit
to Aafimalek/unsloth
that referenced
this pull request
Oct 28, 2026
…import unsloth (unslothai#15702) `import unsloth` imports the whole openai SDK (522 modules, plus httpx2 / anyio / sniffio) whenever openai is installed, which it is on Colab and in Studio venvs. Nothing uses it: `unsloth/models/_utils.py` ran `from openai import OpenAI` only so that a broken openai install could flip `transformers.utils.is_openai_available` to False. This PR does the same check on the first call to `transformers.utils.is_openai_available` instead of at import. ## Why the check exists It came in with unslothai#1182 ("Weird Databricks errors"). transformers decides openai is available from the package metadata alone, so a broken install (one that raises on import) still reports True. trl 0.18 to 0.25 then crash: `trl/trainer/judges.py` runs `if is_openai_available(): from openai import OpenAI` at module level, and `import unsloth` imports that module while it patches the online DPO / XPO / Nash-MD trainers. trl 0.26 and later only import openai inside `OpenAIPairwiseJudge.__init__`, after the same check, and trl 1.15 / 1.16 have no judges at all. ## What changes - `transformers.utils.is_openai_available` is wrapped once (a marker attribute keeps a second run from wrapping it again). The wrapper calls the original. If that says True, the first call tries `from openai import OpenAI`, and if the import fails it prints the same `Unsloth: OpenAI failed to import - ignoring for now.` once and returns False from then on. Otherwise it returns the original's value. The first import attempt runs under a lock, so first calls from several threads at once make one attempt and print the message once, as the import-time check did. - Bindings made before unsloth patches are unchanged, as before: `transformers.utils.import_utils.is_openai_available`, `BACKENDS_MAPPING` / `requires_backends`, `is_serve_available` (5.x) and `commands/serving.py` (4.57.6) never saw the old patch either. Modules that read `transformers.utils.is_openai_available` after unsloth (trl judges, `trl env`, `transformers.testing_utils`) get the wrapper, as they got the old replacement. - `except Exception` instead of a bare `except:`, so a Ctrl-C during a later first call is not swallowed. ## Results Fresh process per run, CUDA spoof (`tests/_zoo_aggressive_cuda_spoof.py`), openai 3.40.0 installed, arms interleaved: | | main | this PR | | --- | --- | --- | | modules in `sys.modules` after `import unsloth`, transformers 4.57.6 + trl 1.15.0 | 6047 (522 openai) | 5489 (0 openai) | | same, transformers 5.21.0 + trl 1.15.0 | 6222 (522 openai) | 5698 (0 openai) | | same, transformers 4.57.6 + trl 0.25.1 | 6092 (522 openai) | 6092 (522 openai, imported by trl's judges) | | deferred work: first `is_openai_available()` after `import unsloth` (7 runs) | 0 ms | 371 ms CPU (321 to 437) | | `import unsloth` main-thread CPU, 4.57.6 + trl 1.15.0 (9 pairs) | 13.67 s (13.12 to 15.04) | 13.07 s (12.15 to 13.61) | The paired difference is -0.56 s (median of 9) for 4.57.6 + trl 1.15.0 and -0.53 s for 5.21.0 + trl 1.15.0. The host was heavily loaded, and the gap between consecutive main runs (A/A) was 0.5 to 1.0 s, so the module counts and the deferred-cost row are the solid numbers. With trl 0.25 or older (Studio pins 0.23.1), trl's judges import openai during `import unsloth` anyway, so nothing changes there. ## Behaviour For healthy, missing and broken openai (a fake package with metadata whose `__init__` raises), on main and on this branch, with transformers 4.57.6 + trl 0.25.1, 4.57.6 + trl 1.15.0, 5.17.0 + trl 1.15.0 and 5.21.0 + trl 1.15.0, these are identical between the arms: `transformers.utils.is_openai_available()` (first and second call, value and type), `import_utils.is_openai_available()`, `requires_backends(obj, ["openai"])`, `is_serve_available()`, trl judges import and `OpenAIPairwiseJudge(...)` (constructed when openai is healthy, `ValueError: OpenAI client is not installed` when it is missing or broken), and the warning count (one per process). The only difference is when the broken-install warning prints: at the first `is_openai_available()` call instead of during `import unsloth`. With trl 0.25 or older that call still happens during `import unsloth` (judges), so the warning appears at the same point. With newer trl it appears just before something that needs openai, or not at all if nothing does. A broken install can only be detected by running the import, so a cheap eager check (for example `find_spec`) cannot keep the old timing. ## Tests - New `tests/test_lazy_openai_import_check.py`: - with a fake healthy openai, `import unsloth` must not import it from `_utils.py` module level (fails on main); - with a fake broken openai, `is_openai_available()` returns False twice, the transformers original still returns True, the warning prints once, and the patch wraps exactly one layer; - four threads making the first call at once on a broken openai that takes 1 s to fail get False and print the warning once (without the lock it printed 4 times). - All three pass on this branch and fail on main under transformers 4.57.6 + trl 0.23.1 (the Studio pin), 4.57.6 + trl 0.25.1, 4.57.6 + trl 1.16.0 and 5.21.0 + trl 1.16.0. The first two also passed on the earlier revision under 5.17.0 + trl 1.15.0. - `py_compile` on Python 3.9 and 3.10. ## Not covered - Run on Linux x86_64 with a spoofed CUDA device only. The change is pure Python with no OS or accelerator branches. On a host with no GPU, `import unsloth` takes the CPU init path, which does not import `_utils.py` and never imported openai on main either. - Not tested on Databricks itself; the broken install was simulated with a package that raises `TypeError` on import.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.