Skip to content

Import fixes: disable a torchao too new for the installed torch instead of failing import unsloth - #15850

Merged
danielhanchen merged 5 commits into
mainfrom
fix/torchao-torch26-import
Oct 29, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
fix/torchao-torch26-import

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

What

A fresh pip install unsloth on torch 2.6.0 cannot import unsloth. unsloth_zoo requires torchao>=0.13.0 with no ceiling, and a requirement cannot depend on the installed torch, so the resolver picks the newest torchao (0.18.0). transformers imports torchao while importing its quantizers (quantizer_torchao.py imports torchao.prototype.safetensors, which reaches torchao/prototype/mx_formats/constants.py), and that module reads torch.float4_e2m1fn_x2, which first appears in torch 2.7:

unsloth.import_fixes.UnslothTorchTooOldError: module 'torch' has no attribute 'float4_e2m1fn_x2'
Unsloth: torchao==0.18.0 uses torch.float4_e2m1fn_x2, and the installed torch==2.6.0+cpu does not have it, ...

The torchao compatibility table (pytorch/ao#2919) lists torch 2.6 as supported up to torchao 0.13.0 (Python API), torch 2.7 up to 0.15, torch 2.8 up to 0.16. The Studio installer already pins torchao per torch (_select_torchao_spec in studio/install_python_stack.py), so this only hits bare pip installs.

Repro

CPU venv, Python 3.12: torch==2.6.0 (CPU wheel) + triton==3.2.0 (what the CUDA 2.6 wheel pins), then pip install of unsloth_zoo and unsloth from main. torchao resolves to 0.18.0. import unsloth raises the error above with both transformers 4.57.6 and 5.17.0.

Fix

New disable_torchao_if_torch_too_old() in unsloth/import_fixes.py, called from _gpu_init.py after the existing torchao shims (fix_torchao_torch_symbol_skew, fix_torchao_pytree_register_constant, fix_torchao_without_torch_distributed) and before import unsloth_zoo.

  • It does nothing unless torchao is newer than the newest release documented for the installed torch minor (2.4/2.5: 0.12, 2.6: 0.13, 2.7: 0.15, 2.8: 0.16; no gate for 2.9+).
  • Inside that gate it imports what transformers and peft import from torchao at module scope (torchao, torchao.quantization, torchao.prototype.safetensors.safetensors_support). If that works, nothing changes, so torchao 0.14 to 0.17 on torch 2.6 keep working as they do today.
  • It only acts on torch's own missing-name wording (module 'torch...' has no attribute, cannot import name ... from 'torch...'). Any other torchao failure is left to surface where it did before.
  • When it acts, torchao is made to read as not installed, the same way disable_torchaudio_if_cuda_mismatched handles a broken torchaudio: sys.modules["torchao"] = None, and cached is_torchao_available answers in transformers, peft and diffusers are reset. It prints one message with both versions and the fix (pip install "torchao<0.14" on torch 2.6, or a newer torch).

Nothing switches to a different numeric path. Code that needs torchao (QAT, torchao float8, TorchAoConfig, torchao-quantized checkpoints) raises its usual "requires torchao" error, and everything else imports and trains.

No dependency metadata change: pip markers cannot see the installed torch, and capping torchao would hold back every newer torch.

Tests

  • tests/test_torchao_too_new_for_torch.py (new, CPU only, simulates the versions with a fake torchao package): 12 tests. On main, 1 fails and 11 error because the function does not exist. On this branch, all 12 pass.
  • Neighbours tests/test_torchao_torch_symbol_skew.py and tests/test_torchaudio_cuda_mismatch.py: same failing IDs before and after (none on torch 2.14).
  • Real torch 2.6.0 CPU venv, import unsloth, torchao 0.13.0 / 0.14.1 / 0.15.0 / 0.16.0 / 0.17.0 / 0.18.0 against transformers 4.57.6 and 5.17.0 (12 cells):
    • main: 10 cells import and the 2 cells with torchao 0.18.0 fail.
    • this branch: all 12 import. The disable message appears only in the 2 cells with torchao 0.18.0.
  • Real torch 2.6.0 + torchao 0.18.0 + transformers 5.17.0: a 3-step CPU LoRA SFT on hf-internal-testing/tiny-random-LlamaForCausalLM finishes, and TorchAoConfig(...) raises "requires torchao to be installed".
  • In the torch 2.6 + torchao 0.18 venv, the neighbours fail 9 tests on main (7 in test_torchao_torch_symbol_skew.py, 2 in test_torchaudio_cuda_mismatch.py, all because import unsloth fails there) and 1 on this branch. The remaining one, test_the_real_torchao_018_import_line_is_unblocked, fails on main as well.
  • Same venv, torchao 0.18.0 hidden, transformers 4.57.6 and 5.17.0, with transformers / accelerate / diffusers / trl imported before or after unsloth: is_torchao_available() in transformers, peft, accelerate and diffusers all return False (including a transformers 5 answer cached as True before import unsloth), importlib.util.find_spec("torchao") returns None, import transformers.quantizers.quantizer_torchao works, and a peft LoRA forward and backward runs. find_spec("torchao") with the None entry returns None on Python 3.9 to 3.13 (submodule lookups raise ModuleNotFoundError, an ImportError).
  • Same venv, torchao 0.15.0 (the Studio pin) and 0.13.0: availability answers, the set of imported torchao modules (119 for 0.15.0) and the printed output are identical on main and this branch. The probe imports the same modules import unsloth already imports, only earlier.
  • torch 2.14 + torchao 0.18 (no probe): import unsloth prints the same output on main and this branch, and the added check costs about 1 ms (find_spec plus one metadata read, 3 runs: 0.92 / 0.94 / 0.97 ms).
  • py_compile passes on Python 3.9 and 3.10.

Not covered

  • GPU runs. The change runs before any device work, and the Colab L4 report that found this used the same torch 2.6 + torchao 0.18 import path, but this branch was not rerun there.
  • vLLM's architecture-inspection subprocess, which imports torchao on its own and does not inherit the sys.modules sentinel.
  • The MLX path, where unsloth_zoo does not install torchao.
  • torch 2.4 / 2.5 / 2.7 / 2.8 with a too-new torchao were not run. Only the gate logic for those versions is unit tested. On torch 2.4 / 2.5 the suggested torchao<0.13 is below unsloth_zoo's torchao>=0.13.0 floor, so pip will report a requirement conflict there.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-29T10:36:56.945958Z 119d60a Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 673f3e5d20

ℹ️ 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. Delightful!

Reviewed commit: 119d60a742

ℹ️ 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 marked this pull request as ready for review October 29, 2026 10:36
@danielhanchen
danielhanchen merged commit ef8bc87 into main Oct 29, 2026
4 of 33 checks passed
@danielhanchen
danielhanchen deleted the fix/torchao-torch26-import branch October 29, 2026 10:37
danielhanchen added a commit that referenced this pull request Oct 29, 2026
…Grouper CUDA test on a real card (#15948)

## Summary

Two more checks are red on main itself, so every PR based on it today
inherits them (seen on staging for #15859 and #15936, neither of which
touches these files):

1.
`tests/security/test_lint_risky_loaders.py::test_the_tree_matches_the_baseline`
(workflow-trigger lint):
   ```
   1 risky loader call site(s) not in the baseline:
     unsloth/import_fixes.py:1040  [dynamic-import] import_module
   ```
#15850 added a fourth `importlib.import_module(module_name)` in
`unsloth/import_fixes.py` (the torchao import-time probe). The baseline
keys a call on its source text, so the existing entry for that text
(count 3) now sees 4 and reports the overflow at the first site. The new
call iterates the fixed `_TORCHAO_IMPORT_TIME_MODULES` tuple, so it is
the same trusted shape as the other three. The product is fine; the
baseline needed the count and the reason updated.

2.
`tests/python/test_accelerator_skip_guards.py::test_no_skip_guard_reads_a_spoofable_accelerator_probe`
(Repo tests, CPU):
   ```
   test_prefix_grouper_layout.py:199: torch.cuda.is_available()
   ```
#15835 added `test_cuda_layout_reads_the_device_once_and_copies_once`
gated on `torch.cuda.is_available()`, which
`tests/_zoo_aggressive_cuda_spoof.py` patches to `True` process-wide, so
on a CPU runner sharing a session with the compat suites the test
un-skips and calls `.cuda()`. Here the guard is right and the new test
was wrong: it now gates on `has_real_cuda()` like the other CUDA-only
tests.

## What changed

- `scripts/risky_loaders_baseline.json`: the `59e72c01edb14c86` entry
goes from count 3 to 4 (regenerated with `scripts/lint_risky_loaders.py
--update`, only that line moved), and its reason names
`_TORCHAO_IMPORT_TIME_MODULES` beside the other fixed tables.
- `tests/test_prefix_grouper_layout.py`: `skipif(not has_real_cuda(),
...)` instead of `skipif(not torch.cuda.is_available(), ...)`.

## Verification

- Base (main at `a4aee21490`): both tests fail as above.
- Head: `test_lint_risky_loaders.py`, `test_accelerator_skip_guards.py`
and `test_prefix_grouper_layout.py` give 75 passed, 1 skipped on a
CPU-only run.
- On a real GPU (torch 2.13 + CUDA 13.0) all 6 PrefixGrouper tests run,
including the CUDA one, so the new gate does not skip it on real
hardware.
- Each guard still fails without its fix: putting
`torch.cuda.is_available()` back makes the skip-guard test fail, and
putting the baseline count back to 3 makes the loader lint fail.
danielhanchen added a commit that referenced this pull request Oct 29, 2026
## Summary

`Repo tests (CPU, rest)` is red on main since #15850:

```
tests/test_source_read_encoding.py
AssertionError: 1 file reads in the test trees touch a checked-in file with the platform default encoding, so they break on Windows as soon as that file gains a non-ASCII character
  'tests/test_torchao_too_new_for_torch.py:139: read_text()'
```

#15850 added
`test_it_runs_after_the_torchao_shims_and_before_unsloth_zoo`, which
reads `unsloth/_gpu_init.py` with `read_text()`. The guard is right: on
Windows that read uses the locale codec (cp1252) and breaks once the
file gains a non-ASCII character. The fix is the test's, not the
guard's.

## What changed

`GPU_INIT.read_text(encoding = "utf-8")`.

## Verification

- Base (main at `0edfda6e1e`): `tests/test_source_read_encoding.py` 1
failed, 15 passed.
- Head: `tests/test_source_read_encoding.py` and
`tests/test_torchao_too_new_for_torch.py` 32 passed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant