Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6135,18 +6135,27 @@ _torch_release_in_window() {
echo "no"
}

# "yes" when $1 is the cu130 index on Linux/WSL x86_64 with a Python 3.13 venv: the only route the torch 2.13/2.14 prebuilt wheels cover.
# "yes" when $1 is the cu130 index on Linux/WSL x86_64 or aarch64 with a Python 3.13 venv: the only route the torch 2.13/2.14 prebuilt wheels cover.
_cu130_torch213_route() {
[ "$(_cu130_torch213_platform "$1")" = "yes" ] || { echo "no"; return; }
[ "$(_cu130_torch213_host_ok)" = "yes" ] || { echo "no"; return; }
_pypi_unsloth_admits_torch "2.13.0"
}

# New installs only: torch 2.13 has no cp313t wheel, and the kernel wheels need glibc 2.35.
_cu130_torch213_host_ok() {
_cth_gil=$("$VENV_DIR/bin/python" -c "import sysconfig; print('gil-disabled' if sysconfig.get_config_var('Py_GIL_DISABLED') else 'gil-enabled')" 2>/dev/null || echo "")
[ "$_cth_gil" = "gil-enabled" ] || { echo "no"; return; }
_cth_glibc=$(_uv_glibc_minor) || { echo "no"; return; }
[ "$_cth_glibc" -ge 35 ] 2>/dev/null && echo "yes" || echo "no"
}

# The local half of the route, with no network: it alone decides the preservation window, so a
# PyPI outage on a re-run can never shrink it and downgrade an existing 2.13/2.14 install.
_cu130_torch213_platform() {
[ "$(_torch_index_url_leaf "$1")" = "cu130" ] || { echo "no"; return; }
case "$OS" in linux|wsl) ;; *) echo "no"; return ;; esac
case "$_ARCH" in x86_64|amd64) ;; *) echo "no"; return ;; esac
case "$_ARCH" in x86_64|amd64|aarch64|arm64) ;; *) echo "no"; return ;; esac

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Gate the ARM torch 2.13 route on glibc 2.35

On supported aarch64 hosts with glibc 2.28–2.34, this enables torch 2.13 even though the linux_aarch64 kernel wheels are built on Ubuntu 22.04 with a documented practical glibc 2.35 floor (.github/scripts/prebuilt_wheels.py:84-89) and are tagged generically enough for pip to accept them. Their import verification then rejects them; for a Mamba model the fallback source build lacks the workflow-only C++20 patch required by torch 2.13 and ensure_ssm_runtime raises, whereas the former torch 2.11 route could build that source. Either retain the old route below glibc 2.35 or publish wheels carrying a compatible manylinux floor.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 22a760b. The new-install choice now also needs glibc 2.35 or newer (read the same way as the uv installer check; musl or an unreadable libc answers no), on x86_64 and aarch64 alike since both builds share that floor. Below it a fresh install stays on the default range. The preservation window is unchanged, so an existing 2.13/2.14 is still kept.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Exclude free-threaded Python 3.13 from the ARM route

When --python points at a free-threaded aarch64 Python 3.13 interpreter, the new ARM branch accepts it because the following probe checks only sys.version_info, then requests torch 2.13; the cu130 index has cp313 aarch64 wheels but no cp313t build for torch 2.13 (official torch index), so a fresh install fails resolution. The repository's ARM kernel resolver likewise explicitly supports only GIL-enabled cp313, so this route should also inspect Py_GIL_DISABLED before opting in.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 22a760b. The new-install choice now probes Py_GIL_DISABLED and requires a GIL-enabled interpreter; torch 2.13 has no cp313t wheel on any platform, so a free-threaded 3.13 stays on the default range.

_ctr_py=$("$VENV_DIR/bin/python" -c "import sys; print('%d.%d' % sys.version_info[:2])" 2>/dev/null || echo "")
[ "$_ctr_py" = "3.13" ] && echo "yes" || echo "no"
}
Expand Down Expand Up @@ -7263,6 +7272,7 @@ if [ "$SKIP_TORCH" = false ] && [ "$(_cu130_torch213_platform "$TORCH_INDEX_URL"
# The PyPI check reads unsloth's metadata, so a --package other than unsloth keeps the default.
if [ "$(printf '%s' "$PACKAGE_NAME" | tr '[:upper:]' '[:lower:]')" = "unsloth" ] \
&& { [ "$_EXISTING_INSTALL" = false ] || [ "${UNSLOTH_TORCH_UPGRADE:-0}" = "1" ]; } \
&& [ "$(_cu130_torch213_host_ok)" = "yes" ] \
&& [ "$(_pypi_unsloth_admits_torch "2.13.0")" = "yes" ]; then
_CU130_NEW_INSTALL_ROUTE=true
fi
Expand Down
29 changes: 27 additions & 2 deletions studio/backend/tests/test_cu130_repair_torch_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ def _route(
("2.14.0", ("torch==2.14.0", "torchvision==0.29.*", "torchaudio==2.11.*")),
],
)
@pytest.mark.parametrize("host", [{"platform": "linux"}, {"platform": "win32", "machine": "AMD64"}])
@pytest.mark.parametrize(
"host",
[
{"platform": "linux"},
{"platform": "linux", "machine": "aarch64"},
{"platform": "win32", "machine": "AMD64"},
],
)
def test_a_resident_release_is_kept(monkeypatch, mod, resident, expected, host):
_route(monkeypatch, mod, torch = resident, **host)
assert mod._cuda_repair_torch_specs(CU130, mod._CUDA_TORCH_PKG_SPEC) == expected
Expand All @@ -71,6 +78,23 @@ def test_no_keepable_release_repairs_to_the_default_range(monkeypatch, mod, resi
assert mod._cuda_repair_torch_specs(CU130, default) is default


@pytest.mark.parametrize("machine", ["aarch64", "arm64"])
def test_aarch64_torch_2_9_repairs_to_the_default_range(monkeypatch, mod, machine):
# cu130 serves aarch64 torch 2.9 but no torchvision 0.24 / torchaudio 2.9 for it.
_route(monkeypatch, mod, torch = "2.9.1", machine = machine)
for default in (mod._CUDA_TORCH_PKG_SPEC, mod._TORCH_FLAVOR_REPAIR_PKG_SPEC):
assert mod._cuda_repair_torch_specs(CU130, default) is default


def test_x86_64_torch_2_9_is_kept(monkeypatch, mod):
_route(monkeypatch, mod, torch = "2.9.1")
assert mod._cuda_repair_torch_specs(CU130, mod._CUDA_TORCH_PKG_SPEC) == (
"torch==2.9.1",
"torchvision==0.24.*",
"torchaudio==2.9.*",
)


@pytest.mark.parametrize(
"index_url, kwargs",
[
Expand All @@ -83,7 +107,8 @@ def test_no_keepable_release_repairs_to_the_default_range(monkeypatch, mod, resi
(CU130, {"platform": "win32", "machine": "AMD64", "woa": True}),
(CU130, {"platform": "win32", "machine": "AMD64", "py": (3, 12)}),
(CU130, {"platform": "darwin"}),
(CU130, {"machine": "aarch64"}),
(CU130, {"machine": "armv7l"}),
(CU130, {"platform": "darwin", "machine": "arm64"}),
(CU130, {"py": (3, 12)}),
(CU130, {"py": (3, 11)}),
(None, {}),
Expand Down
24 changes: 16 additions & 8 deletions studio/install_python_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,21 +568,29 @@ def _torch_index_leaf(url: str) -> str:
_CU130_FIRST_TORCH_MINOR = 9 # download.pytorch.org/whl/cu130 starts at torch 2.9.0


def _is_native_x86_64() -> bool:
"""Linux x86_64, or Windows x64 on an x64 machine. An emulated x64 Python on Windows on ARM
is excluded: install.ps1 keys its torch 2.13 route on the machine as well as the interpreter."""
if platform.machine().lower() not in ("x86_64", "amd64"):
return False
def _cu130_first_torch_minor() -> int:
"""cu130 has no aarch64 torchvision 0.24 / torchaudio 2.9, so aarch64 starts at 2.10."""
if sys.platform.startswith("linux") and platform.machine().lower() in ("aarch64", "arm64"):
return 10
return _CU130_FIRST_TORCH_MINOR


def _is_cu130_route_machine() -> bool:
"""Linux x86_64 or aarch64, or Windows x64 on an x64 machine. An emulated x64 Python on Windows
on ARM is excluded: install.ps1 keys its torch 2.13 route on the machine as well as the interpreter."""
machine = platform.machine().lower()
if sys.platform.startswith("linux"):
return True
return machine in ("x86_64", "amd64", "aarch64", "arm64")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Exclude ARM torch 2.9 from the preservation route

On a Linux aarch64 Python 3.13 environment with resident torch 2.9, this newly enables _cuda_repair_torch_specs, which returns torchaudio==2.9.*; both CUDA repair call sites install the complete trio, but the cu130 index publishes torch 2.9 for aarch64 and no matching torchaudio 2.9 aarch64 wheel (torch index, torchaudio index). Consequently, a repair that previously used the default range now becomes unresolvable, so the ARM route needs an architecture-aware lower bound or companion-package handling.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 22a760b. The cu130 index has aarch64 torch 2.9 but no torchvision 0.24 / torchaudio 2.9 for it, so on Linux aarch64 the repair only keeps a resident 2.10 or newer; 2.9 there falls back to the default range as before. x86_64 still keeps 2.9. Tests cover both.

if machine not in ("x86_64", "amd64"):
return False
return sys.platform == "win32" and not _is_windows_arm64()


def _is_cu130_torch213_route(index_url: str | None) -> bool:
return (
bool(index_url)
and _torch_index_leaf(index_url) == "cu130"
and _is_native_x86_64()
and _is_cu130_route_machine()
and sys.version_info[:2] == (3, 13)
)

Expand All @@ -608,7 +616,7 @@ def _cuda_repair_torch_specs(
release = _resident_torch_release()
if release is not None:
minor = int(release.split(".")[1])
if _CU130_FIRST_TORCH_MINOR <= minor < _CU130_PRESERVE_TORCH_CEILING_MINOR:
if _cu130_first_torch_minor() <= minor < _CU130_PRESERVE_TORCH_CEILING_MINOR:
# torchaudio 2.11 is the last release (stable ABI), so newer minors pair with it.
audio_minor = min(minor, 11)
return (
Expand Down
33 changes: 28 additions & 5 deletions tests/sh/test_cu130_torch213_route.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
INSTALL_SH="${1:-$SCRIPT_DIR/../../install.sh}"
_FUNC_FILE=$(mktemp)
{
for fn in _mirror_uv_project_config _mirror_configured _run_bounded _pypi_unsloth_admits_torch _torch_index_url_leaf _cu130_torch213_route _cu130_torch213_platform _torchaudio_for_torch_minor _torch_release_in_window _previous_torch_pin _dir_has_entries; do
for fn in _mirror_uv_project_config _mirror_configured _run_bounded _pypi_unsloth_admits_torch _torch_index_url_leaf _cu130_torch213_route _cu130_torch213_platform _cu130_torch213_host_ok _torchaudio_for_torch_minor _torch_release_in_window _previous_torch_pin _dir_has_entries; do
sed -n "/^${fn}()/,/^}/p" "$INSTALL_SH"
echo ""
done
Expand All @@ -23,19 +23,23 @@ unset UNSLOTH_TORCH_UPGRADE

VENV_DIR=$(mktemp -d)
mkdir -p "$VENV_DIR/bin"
# Answers the version probe (-c) with $1 and runs anything else (the PyPI gate) on a real python.
# Answers the GIL probe with $2 (default gil-enabled), the version probe (-c) with $1, and runs
# anything else (the PyPI gate) on a real python.
_stub_python() {
printf '#!/bin/sh\nif [ "$1" = "-c" ]; then echo %s; else exec python3 "$@"; fi\n' "$1" > "$VENV_DIR/bin/python"
printf '#!/bin/sh\ncase "$2" in *Py_GIL_DISABLED*) echo %s; exit 0 ;; esac\nif [ "$1" = "-c" ]; then echo %s; else exec python3 "$@"; fi\n' "${2:-gil-enabled}" "$1" > "$VENV_DIR/bin/python"
chmod +x "$VENV_DIR/bin/python"
}
# The host's glibc minor; empty = not a readable glibc (musl).
GLIBC_MINOR=35
_uv_glibc_minor() { [ -n "$GLIBC_MINOR" ] || return 1; echo "$GLIBC_MINOR"; }
CU130="https://download.pytorch.org/whl/cu130"
_pypi_fixture() {
printf '{"info": {"requires_dist": ["numpy", "%s", "torchvision"]}}' "$1" > "$VENV_DIR/pypi.json"
export UNSLOTH_PYPI_JSON_URL="file://$VENV_DIR/pypi.json"
}
_pypi_fixture "torch<2.15.0,>=2.4.0"

echo "=== route: only cu130 + Linux/WSL + x86_64 + Python 3.13 ==="
echo "=== route: only cu130 + Linux/WSL + x86_64/aarch64 + Python 3.13 ==="
_stub_python 3.13
OS=linux; _ARCH=x86_64
assert_eq "linux x86_64 cu130 py3.13" "yes" "$(_cu130_torch213_route "$CU130")"
Expand All @@ -49,12 +53,31 @@ done
OS=macos
assert_eq "macOS never" "no" "$(_cu130_torch213_route "$CU130")"
OS=linux; _ARCH=aarch64
assert_eq "aarch64 never (no wheels)" "no" "$(_cu130_torch213_route "$CU130")"
assert_eq "linux aarch64 cu130 py3.13" "yes" "$(_cu130_torch213_route "$CU130")"
_ARCH=armv7l
assert_eq "32-bit ARM never" "no" "$(_cu130_torch213_route "$CU130")"
_ARCH=x86_64
for py in 3.11 3.12 3.14; do
_stub_python "$py"
assert_eq "Python $py never (cp313 wheels only)" "no" "$(_cu130_torch213_route "$CU130")"
done
_stub_python 3.13 gil-disabled
assert_eq "free-threaded 3.13 never (no cp313t torch 2.13)" "no" "$(_cu130_torch213_route "$CU130")"
assert_eq "free-threaded 3.13 keeps the preservation window" "yes" "$(_cu130_torch213_platform "$CU130")"
_stub_python 3.13
for minor in 28 31 34; do
GLIBC_MINOR=$minor
for arch in x86_64 aarch64; do
_ARCH=$arch
assert_eq "glibc 2.$minor $arch never (wheels need 2.35)" "no" "$(_cu130_torch213_route "$CU130")"
assert_eq "glibc 2.$minor $arch keeps the preservation window" "yes" "$(_cu130_torch213_platform "$CU130")"
done
done
GLIBC_MINOR=""
assert_eq "musl / unreadable glibc never" "no" "$(_cu130_torch213_route "$CU130")"
GLIBC_MINOR=39
assert_eq "glibc 2.39 aarch64" "yes" "$(_cu130_torch213_route "$CU130")"
GLIBC_MINOR=35; _ARCH=x86_64
rm -f "$VENV_DIR/bin/python"
assert_eq "no venv python never" "no" "$(_cu130_torch213_route "$CU130")"

Expand Down