Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Trim comments in macOS torch 2.13 route and tests
[skip ci]
  • Loading branch information
danielhanchen committed Nov 19, 2026
commit 217eb567566cef08d5cfa6cd32b51599ae015334
7 changes: 2 additions & 5 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4365,8 +4365,7 @@ TORCHVISION_CONSTRAINT="torchvision>=0.19,<${_TORCHVISION_CEILING}"
TORCHAUDIO_CONSTRAINT="torchaudio>=2.4,<${_TORCHAUDIO_CEILING}"
_CU130_TORCH_CEILING="2.15.0"
_CU130_NEW_INSTALL_TORCH="torch>=2.13.0,<2.14.0"
# Apple Silicon: torch 2.13 is the first with a fused MPS (prefill) attention kernel, and mflux's pin
# declares torch>=2.13. 2.15 has no cp310 wheel and no torchvision 0.30, so new installs take 2.13.
# Apple Silicon: 2.13 is the first with fused MPS attention (mflux needs >=2.13); 2.15 lacks cp310/torchvision.
_MACOS_TORCH_CEILING="2.15.0"
_MACOS_NEW_INSTALL_TORCH="torch>=2.13.0,<2.14.0"

Expand Down Expand Up @@ -5945,16 +5944,14 @@ _cu130_torch213_platform() {
[ "$_ctr_py" = "3.13" ] && echo "yes" || echo "no"
}

# The local half of the Apple Silicon route, as _cu130_torch213_platform: the default /cpu leaf on
# arm64 macOS with a Python torch 2.13 has wheels for (3.10 - 3.14). It alone sets the preservation window.
# /cpu leaf on arm64 macOS with a Python 2.13 has wheels for; alone sets the preservation window.
_macos_torch213_platform() {
[ "$(_torch_index_url_leaf "$1")" = "cpu" ] || { echo "no"; return; }
[ "$OS" = "macos" ] && [ "$_ARCH" = "arm64" ] || { echo "no"; return; }
_mtr_py=$("$VENV_DIR/bin/python" -c "import sys; print('%d.%d' % sys.version_info[:2])" 2>/dev/null || echo "")
case "$_mtr_py" in 3.10|3.11|3.12|3.13|3.14) echo "yes" ;; *) echo "no" ;; esac
}

# Major macOS release, empty when unreadable.
_macos_product_major() {
_mpm=$(sw_vers -productVersion 2>/dev/null) || _mpm=""
_mpm="${_mpm%%.*}"
Expand Down
14 changes: 4 additions & 10 deletions studio/backend/tests/test_mps_attention_families.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""MPS SDPA against a CPU reference at every image family's attention shape.

torch below 2.13 runs MPS SDPA through MPSGraph, which builds the full score matrix and returns
wrong values past a size (pytorch#179352; Studio bounds only Qwen-Image-2.1, #13188). torch 2.13+
runs a fused prefill kernel for the head dims in ``MPS_PREFILL_HEAD_DIMS``. The small cells run
everywhere MPS exists; the large ones need GBs on an old torch, so they run on CI
(``GITHUB_ACTIONS``) or with ``UNSLOTH_MPS_LARGE_ATTENTION=1`` (``0`` turns them off). Each cell
emits one ``MPS-ATTN`` warning, so even a passing ``-q`` run's log carries the record.
"""MPS SDPA vs a CPU reference at each image family's attention shape.

torch <2.13 MPSGraph SDPA is wrong past a size (pytorch#179352); 2.13+ uses a fused prefill kernel.
Large cells run on CI or with UNSLOTH_MPS_LARGE_ATTENTION=1 (0 disables).
"""

from __future__ import annotations
Expand Down Expand Up @@ -68,7 +64,6 @@ def _qkv(


def _reference_rows(q, k, v, heads, rows):
"""fp64 CPU attention for the chosen heads and query rows only (cheap at any size)."""
q = q[:, heads][:, :, rows].double()
scores = q @ k[:, heads].double().transpose(-1, -2) / math.sqrt(q.shape[-1])
return torch.softmax(scores, dim = -1) @ v[:, heads].double()
Expand Down Expand Up @@ -176,7 +171,6 @@ def test_key_padding_mask_matches_cpu():

@mps
def test_prefill_engagement_follows_the_torch_version():
"""2.13+ takes the prefill kernel (no score matrix); older torch materialises it."""
heads, head_dim, text = FAMILIES["flux2-klein-4b"]
tokens = _tokens(1024, text)
q, k, v = _dev(*_qkv(heads, tokens, head_dim, torch.bfloat16))
Expand Down
2 changes: 0 additions & 2 deletions tests/python/test_macos_torch213_pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ def test_macos_route_is_one_minor_with_its_torchvision():


def test_macos_preservation_ceiling_matches_cu130():
# Both routes keep a resident 2.4 - 2.14 release; install_python_stack mirrors the cu130 one.
assert _assignment("_MACOS_TORCH_CEILING") == _assignment("_CU130_TORCH_CEILING") == "2.15.0"
assert _ips()._CU130_PRESERVE_TORCH_CEILING_MINOR == 15


def test_generic_ceiling_is_unchanged():
# Linux / Windows CUDA, ROCm and CPU defaults stay below 2.12.
assert _assignment("_TORCH_CEILING") == "2.12.0"
assert _ips()._CUDA_TORCH_PKG_SPEC[0] == "torch>=2.4,<2.12.0"

Expand Down
3 changes: 1 addition & 2 deletions tests/sh/test_macos_torch213_route.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ unset UNSLOTH_TORCH_UPGRADE

VENV_DIR=$(mktemp -d)
mkdir -p "$VENV_DIR/bin"
# 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.
# GIL probe -> $2, version probe -> $1, anything else -> real python3.
_stub_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"
Expand Down