Skip to content

Tell users to upgrade the dependency, not transformers, when a --no-deps install leaves a floor unmet - #8212

Merged
danielhanchen merged 7 commits into
unslothai:mainfrom
danielhanchen:fix/transformers-dep-floor-guard
Aug 9, 2026
Merged

danielhanchen merged 7 commits into
unslothai:mainfrom
danielhanchen:fix/transformers-dep-floor-guard

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

The problem

Notebooks that need a bleeding-edge model install transformers from git with --no-deps, deliberately, so pip cannot re-resolve torch:

!pip install --no-deps git+https://github.com/huggingface/transformers.git

--no-deps also means pip never enforces what that transformers actually requires. pip prints a warning and the install "succeeds":

transformers 5.15.0.dev0 requires safetensors>=0.8.0, but you have safetensors 0.7.0 which is incompatible.
Successfully installed click-8.4.2 hf-xet-1.6.0 huggingface_hub-1.27.0

The failure lands two cells later, at import:

Exception: safetensors>=0.8.0 is required for a normal functioning of this module, but found safetensors==0.7.0.
Try: `pip install transformers -U` or `pip install -e '.[dev]'` if you're working with git main

This killed Liquid_LFM2-Conversational and Kaggle-Liquid_LFM2_(1.2B)-Conversational on Kaggle T4. Kaggle ships safetensors 0.7.0; transformers main wants 0.8.0.

The load-bearing part is the remedy transformers suggests. pip install transformers -U is exactly the wrong move for someone who is deliberately on main: it either downgrades them off the model support they installed main for, or it loops. What they actually need is pip install -U safetensors. The notebooks were patched to floor hub, safetensors and tokenizers, but that only covers the notebooks we already know broke.

What this adds

check_transformers_dependency_versions() in unsloth/import_fixes.py, called from _gpu_init.py before anything imports transformers. It compares the installed versions of transformers' own declared requirements against what transformers declares, and reports the shortfalls with the correct remedy:

Unsloth: transformers==5.15.0.dev0 declares dependencies that your environment does not satisfy:
    huggingface-hub<2.0,>=1.5.0 is required, but found huggingface-hub==0.36.2
    safetensors>=0.8.0 is required, but found safetensors==0.7.0

Upgrade the dependencies, not transformers:
    pip install --upgrade "huggingface-hub<2.0,>=1.5.0" "safetensors>=0.8.0"

transformers may suggest `pip install transformers -U` instead. Ignore that if you installed
transformers from git main on purpose ... Set UNSLOTH_SKIP_TRANSFORMERS_DEPENDENCY_CHECK=1 to silence this.

Design notes:

  • The requirement set comes from the installed distribution's own metadata (importlib.metadata.requires("transformers")), never a hardcoded table and never the network. A table would rot into a second copy of the bug. It also has to be metadata: the base requirements genuinely differ across the supported range, 4.57.6 declares huggingface-hub<1.0,>=0.34.0 and safetensors>=0.4.3, 5.14.1 declares huggingface-hub<2.0,>=1.5.0 and safetensors>=0.8.0 and drops filelock and requests while adding typer.
  • Comparison goes through packaging, not strings, so 0.10.0 against >=0.8.0 is a pass and not a false report.
  • Environment markers are evaluated with extra = "", so extras (extra == "torch", extra == "ja") and inapplicable python_version / sys_platform gates are skipped. Otherwise the check would nag about packages the user is right not to have.
  • Requirements whose package is absent are skipped; transformers imports those lazily and their absence is not a floor violation.
  • Every parse is wrapped: an unparseable requirement line, an undecidable marker, a non-PEP 440 installed version or missing transformers metadata all return quietly. The check must never raise and never be slow, since it runs on every import unsloth.
  • Pure importlib.metadata plus packaging, so no shelling out and no path assumptions; correct on Linux, macOS, Windows and WSL.

Warn, not raise

It warns. By the time transformers is imported the mismatch is already fatal and the user sees transformers' own message anyway, so a warning landing just before it adds the correct remedy rather than replacing an error with another error. The floors are also read generically, and a given floor may be advisory for the code path a particular user is on, so turning any of them into a hard import failure would break environments that work today. This matches check_fbgemm_gpu_version, which degrades rather than stops; torchvision_compatibility_check raises, but only because it checks a curated (torch, torchvision) table where a stable mismatch guarantees broken operators.

Verification

  • 22 tests in tests/test_transformers_dependency_floor.py drive the real functions: satisfied sets for both 4.57.6 and 5.14.1 metadata (silent), the Kaggle violation (reported, naming safetensors as the thing to upgrade, and contradicting pip install transformers -U), the 0.10.0 vs 0.8.0 string-comparison trap, multiple violations in one command, prereleases, inapplicable markers skipped, applicable markers still checked, absent optional packages not reported, five flavours of unparseable input, missing transformers metadata, the skip env var, a live run against the real environment, and a guard that the check is actually wired into _gpu_init.py.
  • Sabotage-checked: reverting the marker filter and the packaging comparison to a string compare turns 5 of the new tests red (exit 1); removing the _gpu_init.py call turns the registration test red (exit 1). Restored, the file is 22 passed, exit 0.
  • Full suite, same selection before and after, on a clean worktree at the same base and on this branch: 18 failed / 4075 passed / 5 errors versus 18 failed / 4098 passed / 5 errors. The failure and error sets are byte-identical; the 23 extra passes are the new tests.
  • import unsloth still works. The check costs about 22 ms, against a 14.5 s import; median wall time with and without it is 14.55 s and 15.02 s, so it is under the run-to-run noise.

…eps install leaves a floor unmet

Notebooks that need a bleeding-edge model install transformers from git with
--no-deps on purpose so pip cannot re-resolve torch. pip then never enforces
what that transformers requires; it prints a warning and the install succeeds,
and the break lands later at import. transformers' own message points at the
wrong remedy for that situation.

Read the requirement set from the installed distribution's metadata, compare
with packaging, and warn naming the dependency to upgrade.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Aug 9, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 43e7340c9a

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: df8d816c80

ℹ️ 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".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 9, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 6cfb5992dc

ℹ️ 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".

@danielhanchen
danielhanchen merged commit f652f84 into unslothai:main Aug 9, 2026
46 of 51 checks passed
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