Repository navigation
Import fixes: disable a torchao too new for the installed torch instead of failing import unsloth - #15850
Conversation
…ade below the zoo torchao floor [skip ci]
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…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.
## 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.
What
A fresh
pip install unslothon torch 2.6.0 cannotimport unsloth. unsloth_zoo requirestorchao>=0.13.0with 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.pyimportstorchao.prototype.safetensors, which reachestorchao/prototype/mx_formats/constants.py), and that module readstorch.float4_e2m1fn_x2, which first appears in torch 2.7: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_specinstudio/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), thenpip installof unsloth_zoo and unsloth from main. torchao resolves to 0.18.0.import unslothraises the error above with both transformers 4.57.6 and 5.17.0.Fix
New
disable_torchao_if_torch_too_old()inunsloth/import_fixes.py, called from_gpu_init.pyafter the existing torchao shims (fix_torchao_torch_symbol_skew,fix_torchao_pytree_register_constant,fix_torchao_without_torch_distributed) and beforeimport unsloth_zoo.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.module 'torch...' has no attribute,cannot import name ... from 'torch...'). Any other torchao failure is left to surface where it did before.disable_torchaudio_if_cuda_mismatchedhandles a broken torchaudio:sys.modules["torchao"] = None, and cachedis_torchao_availableanswers 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.tests/test_torchao_torch_symbol_skew.pyandtests/test_torchaudio_cuda_mismatch.py: same failing IDs before and after (none on torch 2.14).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):hf-internal-testing/tiny-random-LlamaForCausalLMfinishes, andTorchAoConfig(...)raises "requires torchao to be installed".test_torchao_torch_symbol_skew.py, 2 intest_torchaudio_cuda_mismatch.py, all becauseimport unslothfails there) and 1 on this branch. The remaining one,test_the_real_torchao_018_import_line_is_unblocked, fails on main as well.is_torchao_available()in transformers, peft, accelerate and diffusers all return False (including a transformers 5 answer cached as True beforeimport unsloth),importlib.util.find_spec("torchao")returns None,import transformers.quantizers.quantizer_torchaoworks, and a peft LoRA forward and backward runs.find_spec("torchao")with theNoneentry returns None on Python 3.9 to 3.13 (submodule lookups raiseModuleNotFoundError, anImportError).torchaomodules (119 for 0.15.0) and the printed output are identical on main and this branch. The probe imports the same modulesimport unslothalready imports, only earlier.import unslothprints the same output on main and this branch, and the added check costs about 1 ms (find_specplus one metadata read, 3 runs: 0.92 / 0.94 / 0.97 ms).py_compilepasses on Python 3.9 and 3.10.Not covered
sys.modulessentinel.torchao<0.13is below unsloth_zoo'storchao>=0.13.0floor, so pip will report a requirement conflict there.