|
| 1 | +# SPDX-License-Identifier: AGPL-3.0-only |
| 2 | +# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. |
| 3 | + |
| 4 | +"""torch / torchcodec ABI guardrails (unslothai/unsloth#7225).""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import importlib.util |
| 9 | +import re |
| 10 | +import sys |
| 11 | +import types |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | + |
| 15 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 16 | +PYPROJECT = REPO_ROOT / "pyproject.toml" |
| 17 | +IMPORT_FIXES_PATH = REPO_ROOT / "unsloth" / "import_fixes.py" |
| 18 | + |
| 19 | + |
| 20 | +def _load_import_fixes_module(): |
| 21 | + spec = importlib.util.spec_from_file_location( |
| 22 | + "unsloth_import_fixes_under_test", |
| 23 | + IMPORT_FIXES_PATH, |
| 24 | + ) |
| 25 | + assert spec and spec.loader |
| 26 | + mod = importlib.util.module_from_spec(spec) |
| 27 | + spec.loader.exec_module(mod) |
| 28 | + return mod |
| 29 | + |
| 30 | + |
| 31 | +def test_pyproject_declares_torch210_audio_extra_with_python_gate(): |
| 32 | + text = PYPROJECT.read_text(encoding = "utf-8") |
| 33 | + assert "audio-torch210 = [" in text |
| 34 | + assert "torchcodec>=0.10.0,<0.11.0" in text |
| 35 | + assert "python_version >= '3.10'" in text |
| 36 | + assert "audio-torch290 = [" in text |
| 37 | + assert "audio-torch280 = [" in text |
| 38 | + assert "\naudio = [" not in text |
| 39 | + |
| 40 | + |
| 41 | +def _stub_torch(monkeypatch, version: str): |
| 42 | + torch_mod = types.ModuleType("torch") |
| 43 | + torch_mod.__version__ = version |
| 44 | + monkeypatch.setitem(sys.modules, "torch", torch_mod) |
| 45 | + |
| 46 | + |
| 47 | +def test_torch210_extras_bundle_audio_torch210(): |
| 48 | + text = PYPROJECT.read_text(encoding = "utf-8") |
| 49 | + for extra in ( |
| 50 | + "cu128-torch2100", |
| 51 | + "cu126-ampere-torch2100", |
| 52 | + "rocm72-torch2100", |
| 53 | + ): |
| 54 | + match = re.search(rf"^{extra} = \[(.*?)^\]", text, re.MULTILINE | re.DOTALL) |
| 55 | + assert match is not None, extra |
| 56 | + assert "unsloth[audio-torch210]" in match.group(1) |
| 57 | + |
| 58 | + |
| 59 | +def test_torchcodec_matrix_matches_notebook_validator(): |
| 60 | + from scripts import notebook_validator as nv |
| 61 | + fixes = _load_import_fixes_module() |
| 62 | + assert fixes._TORCH_TORCHCODEC_MINORS == nv.TORCH_TORCHCODEC |
| 63 | + |
| 64 | + |
| 65 | +def test_torchcodec_exclusive_upper_bound(): |
| 66 | + fixes = _load_import_fixes_module() |
| 67 | + assert fixes._torchcodec_exclusive_upper("0.10") == "<0.11.0" |
| 68 | + assert fixes._torchcodec_exclusive_upper("0.9") == "<0.10.0" |
| 69 | + |
| 70 | + |
| 71 | +def test_torch290_rejects_torchcodec_07(monkeypatch): |
| 72 | + import importlib.metadata |
| 73 | + |
| 74 | + fixes = _load_import_fixes_module() |
| 75 | + _stub_torch(monkeypatch, "2.9.0+cu128") |
| 76 | + monkeypatch.setattr(importlib.metadata, "version", lambda _name: "0.7.0") |
| 77 | + |
| 78 | + hint = fixes._torchcodec_version_mismatch_hint() |
| 79 | + assert hint is not None |
| 80 | + assert "audio-torch210" not in hint |
| 81 | + |
| 82 | + |
| 83 | +def test_torch280_accepts_torchcodec_07(monkeypatch): |
| 84 | + import importlib.metadata |
| 85 | + |
| 86 | + fixes = _load_import_fixes_module() |
| 87 | + _stub_torch(monkeypatch, "2.8.0+cu128") |
| 88 | + monkeypatch.setattr(importlib.metadata, "version", lambda _name: "0.7.0") |
| 89 | + |
| 90 | + assert fixes._torchcodec_version_mismatch_hint() is None |
| 91 | + |
| 92 | + |
| 93 | +def test_torch210_rejects_torchcodec_011(monkeypatch): |
| 94 | + import importlib.metadata |
| 95 | + |
| 96 | + fixes = _load_import_fixes_module() |
| 97 | + _stub_torch(monkeypatch, "2.10.0+cu128") |
| 98 | + monkeypatch.setattr( |
| 99 | + importlib.metadata, |
| 100 | + "version", |
| 101 | + lambda _name: "0.11.0", |
| 102 | + ) |
| 103 | + |
| 104 | + hint = fixes._torchcodec_version_mismatch_hint() |
| 105 | + assert hint is not None |
| 106 | + assert "torchcodec 0.11.0" in hint |
| 107 | + assert "audio-torch210" in hint |
| 108 | + assert "<0.11.0" in hint |
| 109 | + assert "<11.0" not in hint |
| 110 | + |
| 111 | + |
| 112 | +def test_torch210_accepts_torchcodec_010(monkeypatch): |
| 113 | + import importlib.metadata |
| 114 | + |
| 115 | + fixes = _load_import_fixes_module() |
| 116 | + _stub_torch(monkeypatch, "2.10.0+cu128") |
| 117 | + monkeypatch.setattr( |
| 118 | + importlib.metadata, |
| 119 | + "version", |
| 120 | + lambda _name: "0.10.0+cu128", |
| 121 | + ) |
| 122 | + |
| 123 | + assert fixes._torchcodec_version_mismatch_hint() is None |
| 124 | + |
| 125 | + |
| 126 | +def test_import_fixes_loads_on_python39_syntax(): |
| 127 | + """Regression: module must import on 3.9 (postponed annotations for str | None).""" |
| 128 | + fixes = _load_import_fixes_module() |
| 129 | + assert callable(fixes._torchcodec_version_mismatch_hint) |
0 commit comments