Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Build the prebuilt CUDA 13 kernel wheels for Windows and ARM (win_amd64, linux_aarch64, win_arm64) (#13261)

Daniel Han0fbda969a87c
diff --git a/.github/scripts/patch_mamba_msvc.py b/.github/scripts/patch_mamba_msvc.py
new file mode 100644
index 00000000000..c9282649ca9
--- /dev/null
+++ b/.github/scripts/patch_mamba_msvc.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Make mamba-ssm's selective-scan kernels preprocess with MSVC as nvcc's host compiler.
+
+Windows only. An `#ifndef USE_ROCM` block sits inside the lambda passed to BOOL_SWITCH (a macro
+argument); gcc accepts that, MSVC does not (`error: "#" not expected here`). It is replaced by its
+CUDA branch, which is what a CUDA build preprocesses to anyway. Same contract as
+patch_mamba_cxx20.py: asserts the text, a patched tree is a no-op, anything else fails loudly.
+
+Usage: patch_mamba_msvc.py <mamba checkout root>
+"""
+
+from __future__ import annotations
+
+import re
+import sys
+from pathlib import Path
+
+FILES = (
+ "csrc/selective_scan/selective_scan_fwd_kernel.cuh",
+ "csrc/selective_scan/selective_scan_bwd_kernel.cuh",
+)
+
+# The in-lambda block: deeper than function scope (the function-scope ones sit at 4 spaces).
+NESTED_ROCM = re.compile(
+ r"^[ \t]{12,}#ifndef USE_ROCM[ \t]*\n"
+ r"(?P<cuda>.*?)"
+ r"^[ \t]+#else[ \t]*\n"
+ r".*?"
+ r"^[ \t]+#endif[ \t]*\n",
+ re.MULTILINE | re.DOTALL,
+)
+
+# What the CUDA branch sets, so a patched file is recognisable without a marker comment.
+CUDA_CALL = "cudaFuncSetAttribute(\n"
+
+
+def patch_source(source: str, name: str) -> tuple[str | None, str]:
+ """(patched text, or None when already patched; message). Raises ValueError on drift."""
+ nested = list(NESTED_ROCM.finditer(source))
+ if not nested and CUDA_CALL in source:
+ return None, f"{name}: already patched, nothing to do"
+ if len(nested) != 1:
+ raise ValueError(
+ f"{name}: expected 1 USE_ROCM block inside a launch lambda, found {len(nested)}"
+ )
+ match = nested[0]
+ patched = source[: match.start()] + match.group("cuda") + source[match.end() :]
+ return patched, f"{name}: in-lambda USE_ROCM block reduced to its CUDA branch"
+
+
+def main(argv: list[str]) -> int:
+ if len(argv) != 2:
+ print("usage: patch_mamba_msvc.py <mamba checkout root>", file = sys.stderr)
+ return 2
+ root = Path(argv[1])
+ for rel in FILES:
+ path = root / rel
+ if not path.is_file():
+ print(f"::error::{path} does not exist", file = sys.stderr)
+ return 1
+ try:
+ patched, message = patch_source(path.read_text(encoding = "utf-8"), rel)
+ except ValueError as exc:
+ print(
+ f"::error::{exc}. Upstream changed the kernels; re-read them before bumping the pin.",
+ file = sys.stderr,
+ )
+ return 1
+ if patched is not None:
+ path.write_text(patched, encoding = "utf-8")
+ print(message)
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main(sys.argv))
diff --git a/.github/scripts/prebuilt_wheels.py b/.github/scripts/prebuilt_wheels.py
index 70f09e49515..de4cf515b57 100644
--- a/.github/scripts/prebuilt_wheels.py
+++ b/.github/scripts/prebuilt_wheels.py
@@ -19,14 +19,15 @@ raw input is never used again.
Subcommands:
- matrix read UW_PACKAGES / UW_TORCH_VERSIONS / UW_PYTHON_VERSIONS from the environment
- and print the `include` list for the build matrix as JSON.
+ matrix read UW_PACKAGES / UW_TORCH_VERSIONS / UW_PYTHON_VERSIONS / UW_PLATFORMS from
+ the environment and print the `include` list for the build matrix as JSON.
wheel-name print the single upstream-style filename for one cell.
notes read `sha256 filename` lines on stdin and print the release body.
+ sums read `sha256 filename` lines on stdin and print SHA256SUMS for the wheels.
Usage:
prebuilt_wheels.py matrix
- prebuilt_wheels.py wheel-name --package flash-attn --torch 2.13.0 --python 3.13
+ prebuilt_wheels.py wheel-name --package flash-attn --torch 2.13.0 --python 3.13 [--platform win_amd64]
prebuilt_wheels.py notes --tag prebuilt-wheels-cu13 --repo unslothai/unsloth < SHA256SUMS
"""
@@ -53,12 +54,84 @@ CUDA_TOOLKIT = "13.0"
# that pattern-matches upstream names has to find it here too.
CXX11_ABI = "TRUE"
-# Linux x86_64 only. The wheel is tagged linux_x86_64 rather than manylinux_*, exactly as
-# upstream tags its own, so pip installs it on any glibc without a floor check; the practical
-# floor is the runner's glibc, which is why the build runs on ubuntu-22.04 (glibc 2.35) and
-# not on ubuntu-latest.
+# The Linux tag. The wheel is tagged linux_x86_64 rather than manylinux_*, exactly as upstream
+# tags its own, so pip installs it on any glibc without a floor check; the practical floor is the
+# runner's glibc, which is why the build runs on ubuntu-22.04 (glibc 2.35) and not on
+# ubuntu-latest.
PLATFORM_TAG = "linux_x86_64"
+# Keyed by the wheel's pip platform tag. No upstream release has ever shipped a Windows wheel.
+# `abi` is torch._C._GLIBCXX_USE_CXX11_ABI, which upstream setup.py copies into the name; the
+# Windows cu130 torch reports True too, and the build asserts it. `pytorch_nvcc` goes into
+# build.ninja unquoted and is the only way to cache nvcc on Windows (_wrap_compiler is a no-op
+# there), hence the space-free paths.
+PLATFORMS = {
+ "linux_x86_64": {
+ "os": "linux",
+ "runner": "ubuntu-22.04",
+ "abi": "TRUE",
+ "label": "",
+ "pytorch_nvcc": "ccache /usr/local/cuda-13.0/bin/nvcc",
+ },
+ "win_amd64": {
+ "os": "windows",
+ # Visual Studio 2022 (MSVC 14.4x), a host compiler CUDA 13.0's Windows guide supports.
+ "runner": "windows-2022",
+ "abi": "TRUE",
+ "label": " / win_amd64",
+ "pytorch_nvcc": "C:/ccache/ccache.exe C:/cuda/bin/nvcc.exe",
+ },
+ # Upstream's aarch64 wheels stop at torch 2.10. ubuntu-22.04-arm keeps the glibc 2.35 floor
+ # the x86_64 build has, and the build asserts it.
+ "linux_aarch64": {
+ "os": "linux",
+ "runner": "ubuntu-22.04-arm",
+ "abi": "TRUE",
+ "label": " / linux_aarch64",
+ "pytorch_nvcc": "ccache /usr/local/cuda-13.0/bin/nvcc",
+ },
+ # NVIDIA publishes flash-attn for Windows on ARM itself (studio resolves it), so only the other
+ # two are built here, against the only CUDA torch that exists for it: NVIDIA's 2.14.0+cu134,
+ # which needs CUDA 13.4, the first toolkit with a Windows ARM64 nvcc.
+ "win_arm64": {
+ "os": "windows",
+ "runner": "windows-11-arm",
+ "abi": "TRUE",
+ "label": " / win_arm64",
+ "pytorch_nvcc": "C:/ccache/ccache.exe C:/cuda/bin/nvcc.exe",
+ "torches": ("2.14.0",),
+ "packages": ("causal-conv1d", "mamba-ssm"),
+ "torch_local": "cu134",
+ "torch_index_args": "--extra-index-url https://pypi.nvidia.com/nvtorch_oot",
+ },
+}
+
+# Per platform, what differs from the x86_64 defaults below without changing any x86_64 cell.
+_PLATFORM_DEFAULTS = {
+ "torches": None, # every TORCH_VERSIONS entry
+ "packages": None, # every SPECS entry
+ "torch_local": "",
+ "torch_index_args": "--index-url https://download.pytorch.org/whl/cu130",
+}
+# What the build asserts about its own interpreter and output, so an emulated x64 Python on an
+# ARM runner cannot quietly produce an x64 wheel under an arm64 name.
+_PY_PLATFORM = {
+ "linux_x86_64": "linux-x86_64",
+ "win_amd64": "win-amd64",
+ "linux_aarch64": "linux-aarch64",
+ "win_arm64": "win-arm64",
+}
+
+
+def _platform(platform: str, key: str):
+ return PLATFORMS[platform].get(key, _PLATFORM_DEFAULTS.get(key))
+
+
+# torch 2.13 pins triton==3.7.1 and 2.14 triton~=3.8.0 (Linux-only markers); exact pins keep
+# the Windows smoke test reproducible.
+TRITON_WINDOWS = {"2.13": "3.7.1.post27", "2.14": "3.8.0.post29"}
+WINDOWS_SMOKE_EXCLUDE = ("triton", "tilelang", "apache-tvm-ffi", "quack-kernels")
+
# Source revisions, pinned to a commit rather than a branch or a tag.
#
# flash-attn 2.8.4 does not exist as an upstream release: 2.8.3.post1 is the newest tag, and
@@ -146,6 +219,8 @@ PYTHON_VERSIONS = ("3.11", "3.12", "3.13")
DEFAULT_PACKAGES = tuple(SPECS)
DEFAULT_TORCH = TORCH_VERSIONS
DEFAULT_PYTHON = ("3.13",)
+# ARM is opt-in, so an ordinary dispatch builds exactly what it did before ARM existed.
+DEFAULT_PLATFORMS = ("linux_x86_64", "win_amd64")
def torch_minor(torch_version: str) -> str:
@@ -160,12 +235,18 @@ def python_tag(python_version: str) -> str:
return f"cp{major}{minor}"
-def local_version(torch_version: str) -> str:
+def local_version(torch_version: str, platform: str = PLATFORM_TAG) -> str:
"""The `+cu13torch2.13cxx11abiTRUE` segment, byte for byte as upstream writes it."""
- return f"+cu{CUDA_TAG}torch{torch_minor(torch_version)}cxx11abi{CXX11_ABI}"
+ abi = PLATFORMS[platform]["abi"]
+ return f"+cu{CUDA_TAG}torch{torch_minor(torch_version)}cxx11abi{abi}"
-def wheel_name(package: str, torch_version: str, python_version: str) -> str:
+def wheel_name(
+ package: str,
+ torch_version: str,
+ python_version: str,
+ platform: str = PLATFORM_TAG,
+) -> str:
"""The published filename for one cell.
This is the whole point of the local version segment: pip refuses to install a wheel whose
@@ -176,8 +257,8 @@ def wheel_name(package: str, torch_version: str, python_version: str) -> str:
spec = SPECS[package]
tag = python_tag(python_version)
return (
- f"{spec['dist']}-{spec['version']}{local_version(torch_version)}"
- f"-{tag}-{tag}-{PLATFORM_TAG}.whl"
+ f"{spec['dist']}-{spec['version']}{local_version(torch_version, platform)}"
+ f"-{tag}-{tag}-{platform}.whl"
)
@@ -200,54 +281,111 @@ def _resolve(raw: str, allowed, default, label: str) -> list[str]:
return [item for item in allowed if item in wanted]
+def _build_env(spec: dict, platform: str) -> str:
+ """The package's NAME=VALUE pairs for one platform.
+
+ The only per-platform value is flash-attn's FORCE_CXX11_ABI, which makes its setup.py set
+ torch._C._GLIBCXX_USE_CXX11_ABI before it builds and names the wheel. It follows the platform's
+ `abi`, so the name it writes always agrees with the one the build asserts. Today that is TRUE on
+ both platforms, which on a torch already reporting True is a no-op.
+ """
+ abi = PLATFORMS[platform]["abi"]
+ pairs = []
+ for key, value in spec["env"].items():
+ if key.endswith("FORCE_CXX11_ABI"):
+ value = abi
+ pairs.append(f"{key}={value}")
+ return " ".join(pairs)
+
+
+def _smoke(package: str, torch_version: str, platform: str) -> tuple[str, str]:
+ """(dependencies to leave out, extra requirement to add) for the import smoke test."""
+ if PLATFORMS[platform]["os"] != "windows" or package != "mamba-ssm":
+ return "", ""
+ return (
+ " ".join(WINDOWS_SMOKE_EXCLUDE),
+ f"triton-windows=={TRITON_WINDOWS[torch_minor(torch_version)]}",
+ )
+
+
def build_matrix(
packages: str = "",
torches: str = "",
pythons: str = "",
+ platforms: str = "",
) -> list[dict]:
chosen_packages = _resolve(packages, DEFAULT_PACKAGES, DEFAULT_PACKAGES, "package")
chosen_torch = _resolve(torches, TORCH_VERSIONS, DEFAULT_TORCH, "torch version")
chosen_python = _resolve(pythons, PYTHON_VERSIONS, DEFAULT_PYTHON, "python version")
+ chosen_platforms = _resolve(platforms, tuple(PLATFORMS), DEFAULT_PLATFORMS, "platform")
include = []
- for torch_version in chosen_torch:
- for python_version in chosen_python:
- for package in chosen_packages:
- spec = SPECS[package]
- include.append(
- {
- "package": package,
- "dist": spec["dist"],
- "version": spec["version"],
- "repo": spec["repo"],
- "ref": spec["ref"],
- "submodules": "recursive" if spec["submodules"] else "false",
- "patch": spec.get("patch", ""),
- "torch": torch_version,
- "torch_mm": torch_minor(torch_version),
- "python": python_version,
- "python_tag": python_tag(python_version),
- "cuda_tag": CUDA_TAG,
- "abi": CXX11_ABI,
- "max_jobs": spec["max_jobs"],
- "nvcc_threads": spec["nvcc_threads"],
- "build_timeout": spec["build_timeout"],
- "shards": spec.get("shards", 0),
- "build_env": " ".join(
- f"{key}={value}" for key, value in spec["env"].items()
- ),
- "import_names": " ".join(spec["import_names"]),
- "wheel_name": wheel_name(package, torch_version, python_version),
- # Only used for the job name in the Actions UI, where "flash-attn /
- # torch 2.13 / cp313" is the difference between reading the matrix and
- # counting the cells.
- "label": f"{package} / torch {torch_minor(torch_version)} / "
- f"{python_tag(python_version)}",
- }
- )
+ for platform in chosen_platforms:
+ target = PLATFORMS[platform]
+ torches = _platform(platform, "torches") or TORCH_VERSIONS
+ packages_here = _platform(platform, "packages") or tuple(SPECS)
+ local = _platform(platform, "torch_local")
+ for torch_version in chosen_torch:
+ if torch_version not in torches:
+ continue
+ for python_version in chosen_python:
+ for package in chosen_packages:
+ if package not in packages_here:
+ continue
+ spec = SPECS[package]
+ smoke_exclude, smoke_extra = _smoke(package, torch_version, platform)
+ include.append(
+ {
+ "package": package,
+ "dist": spec["dist"],
+ "version": spec["version"],
+ "repo": spec["repo"],
+ "ref": spec["ref"],
+ "submodules": "recursive" if spec["submodules"] else "false",
+ "patch": spec.get("patch", ""),
+ "torch": torch_version,
+ "torch_mm": torch_minor(torch_version),
+ "python": python_version,
+ "python_tag": python_tag(python_version),
+ "cuda_tag": CUDA_TAG,
+ "abi": target["abi"],
+ "max_jobs": spec["max_jobs"],
+ "nvcc_threads": spec["nvcc_threads"],
+ "build_timeout": spec["build_timeout"],
+ "shards": spec.get("shards", 0),
+ "build_env": _build_env(spec, platform),
+ "import_names": " ".join(spec["import_names"]),
+ "wheel_name": wheel_name(
+ package, torch_version, python_version, platform
+ ),
+ # Job name in the Actions UI; Linux keeps its old label.
+ "label": f"{package} / torch {torch_minor(torch_version)} / "
+ f"{python_tag(python_version)}{target['label']}",
+ "platform": platform,
+ "os": target["os"],
+ "runner": target["runner"],
+ "pytorch_nvcc": target["pytorch_nvcc"],
+ "smoke_exclude": smoke_exclude,
+ "smoke_extra": smoke_extra,
+ "torch_spec": f"torch=={torch_version}"
+ + (f"+{local}" if local else ""),
+ "torch_index_args": _platform(platform, "torch_index_args"),
+ "py_platform": _PY_PLATFORM[platform],
+ }
+ )
+ if not include:
+ raise SystemExit(
+ "no cells: none of the chosen platforms builds that package / torch combination "
+ "(win_arm64 builds causal-conv1d and mamba-ssm for torch 2.14.0 only)."
+ )
return include
+def gpu_matrix(include: list[dict]) -> list[dict]:
+ """Cells the (x86_64 Linux) self-hosted GPU runner can load."""
+ return [cell for cell in include if cell["platform"] == "linux_x86_64"]
+
+
def warm_matrix(include: list[dict]) -> list[dict]:
"""One warm job per (cell, shard), for the cells whose package is sharded."""
return [
@@ -265,32 +403,38 @@ def parse_wheel_name(name: str) -> dict | None:
"""Filename back to the facts the notes table needs, or None if it is not one of ours.
Deliberately strict. The release holds SHA256SUMS and .sigstore.json bundles beside the
- wheels, and a loose parse would put them in the table as packages.
+ wheels, and a loose parse would put them in the table as packages. The ABI must match the
+ platform's, so a stray `cxx11abiFALSE` wheel is not one of ours.
"""
- if not name.endswith(f"-{PLATFORM_TAG}.whl"):
+ platform = next((tag for tag in PLATFORMS if name.endswith(f"-{tag}.whl")), None)
+ if platform is None:
return None
- stem = name[: -len(f"-{PLATFORM_TAG}.whl")]
+ stem = name[: -len(f"-{platform}.whl")]
parts = stem.split("-")
if len(parts) != 4:
return None
dist, version_local, tag, abi_tag = parts
if tag != abi_tag or "+" not in version_local:
return None
- _, local = version_local.split("+", 1)
+ version, local = version_local.split("+", 1)
if not local.startswith(f"cu{CUDA_TAG}torch") or "cxx11abi" not in local:
return None
torch_part, abi = local[len(f"cu{CUDA_TAG}torch") :].split("cxx11abi", 1)
+ if abi != PLATFORMS[platform]["abi"]:
+ return None
package = next((key for key, spec in SPECS.items() if spec["dist"] == dist), None)
if package is None:
return None
return {
"package": package,
"dist": dist,
- "version": SPECS[package]["version"],
+ # From the file, not SPECS: an asset left over from an older pin is a different version.
+ "version": version,
"torch": torch_part,
"cuda": CUDA_TAG,
"python": tag,
"abi": abi,
+ "platform": platform,
"name": name,
}
@@ -299,29 +443,79 @@ def _join(items: list[str]) -> str:
return items[0] if len(items) == 1 else ", ".join(items[:-1]) + " and " + items[-1]
+# How the release body names each platform.
+PLATFORM_NAMES = {
+ "linux_x86_64": "Linux x86_64",
+ "win_amd64": "Windows x86_64",
+ "linux_aarch64": "Linux aarch64",
+ "win_arm64": "Windows ARM64",
+}
+
+
+def _coverage(rows: list[dict]) -> str:
+ """`PyTorch 2.13 and 2.14 on Python 3.13` for a set of parsed rows."""
+ torches = sorted({row["torch"] for row in rows}, key = lambda v: tuple(map(int, v.split("."))))
+ # cp313 -> 3.13
+ pythons = [
+ f"{cp[2]}.{cp[3:]}"
+ for cp in sorted({row["python"] for row in rows}, key = lambda cp: int(cp[3:]))
+ ]
+ return f"PyTorch {_join(torches)} on Python {_join(pythons)}"
+
+
+def _packages(rows: list[dict]) -> str:
+ """`flash-attn 2.8.4 and mamba-ssm 2.3.2.post1` for a set of parsed rows, in SPECS order."""
+ order = {package: index for index, package in enumerate(SPECS)}
+ pairs = sorted(
+ {(row["package"], row["version"]) for row in rows}, key = lambda pv: (order[pv[0]], pv[1])
+ )
+ return _join([f"{package} {version}" for package, version in pairs])
+
+
def render_notes(entries: list[tuple[str, str]], tag: str, repo: str) -> str:
"""One-sentence release body from `(sha256, filename)` pairs.
- Regenerated from the release's current assets on every publish rather than appended to, so
- a second run that adds the torch 2.14 half produces a sentence describing both halves.
+ Regenerated from the release's current assets on every publish, so a run that adds one
+ torch minor or one platform still describes everything attached.
"""
parsed = [row for row in (parse_wheel_name(name) for _, name in entries) if row is not None]
if not parsed:
return "No wheels are attached to this release yet.\n"
- present = {row["package"] for row in parsed}
- packages = [
- f"{package} {spec['version']}" for package, spec in SPECS.items() if package in present
- ]
- torches = sorted({row["torch"] for row in parsed}, key = lambda v: tuple(map(int, v.split("."))))
- # cp313 -> 3.13
- pythons = [
- f"{cp[2]}.{cp[3:]}"
- for cp in sorted({row["python"] for row in parsed}, key = lambda cp: int(cp[3:]))
- ]
- return (
- f"Prebuilt Linux x86_64 CUDA {CUDA_TAG} wheels for {_join(packages)}, "
- f"built for PyTorch {_join(torches)} on Python {_join(pythons)}.\n"
+ platforms = [tag for tag in PLATFORMS if any(row["platform"] == tag for row in parsed)]
+ if len(platforms) == 1:
+ return (
+ f"Prebuilt {PLATFORM_NAMES[platforms[0]]} CUDA {CUDA_TAG} wheels for "
+ f"{_packages(parsed)}, built for {_coverage(parsed)}.\n"
+ )
+ # Per platform: separate runs need not cover the same packages or torch minors.
+ per_platform = " ".join(
+ f"{PLATFORM_NAMES[platform]}: {_packages(rows)}, built for {_coverage(rows)}."
+ for platform in platforms
+ for rows in [[row for row in parsed if row["platform"] == platform]]
)
+ return f"Prebuilt CUDA {CUDA_TAG} wheels. {per_platform}\n"
+
+
+def render_sums(entries: list[tuple[str, str]]) -> str:
+ """SHA256SUMS for every wheel on the release, in `sha256sum` format, sorted by filename.
+
+ Built from the whole release, not from one run's output, because runs add to the tag one
+ platform or one torch minor at a time and the file is replaced on every publish: a run that
+ only built Windows wheels must not leave a SHA256SUMS that no longer lists the Linux ones. A
+ digest that is not a 64-digit hex string is refused rather than written, because a checksum
+ file with a placeholder in it is worse than none.
+ """
+ rows = []
+ for digest, name in entries:
+ if not name.endswith(".whl"):
+ continue
+ digest = digest.removeprefix("sha256:").lower()
+ if len(digest) != 64 or any(c not in "0123456789abcdef" for c in digest):
+ raise SystemExit(f"no usable sha256 for {name}: {digest!r}")
+ rows.append((name, digest))
+ if not rows:
+ raise SystemExit("no wheels to list")
+ return "".join(f"{digest} {name}\n" for name, digest in sorted(rows))
def _cmd_matrix(args: argparse.Namespace) -> int:
@@ -329,9 +523,11 @@ def _cmd_matrix(args: argparse.Namespace) -> int:
packages = os.environ.get("UW_PACKAGES", ""),
torches = os.environ.get("UW_TORCH_VERSIONS", ""),
pythons = os.environ.get("UW_PYTHON_VERSIONS", ""),
+ platforms = os.environ.get("UW_PLATFORMS", ""),
)
matrix = json.dumps({"include": include}, separators = (",", ":"))
warm = warm_matrix(include)
+ gpu = gpu_matrix(include)
print(matrix)
# Writing the step output here rather than echoing it in YAML keeps the JSON -- which is
@@ -345,6 +541,9 @@ def _cmd_matrix(args: argparse.Namespace) -> int:
warm_json = json.dumps({"include": warm}, separators = (",", ":"))
handle.write(f"warm_matrix={warm_json}\n")
handle.write(f"warm_count={len(warm)}\n")
+ gpu_json = json.dumps({"include": gpu}, separators = (",", ":"))
+ handle.write(f"gpu_matrix={gpu_json}\n")
+ handle.write(f"gpu_count={len(gpu)}\n")
summary = os.environ.get("GITHUB_STEP_SUMMARY")
if summary:
listing = "\n".join(f"- `{cell['wheel_name']}`" for cell in include)
@@ -360,11 +559,14 @@ def _cmd_wheel_name(args: argparse.Namespace) -> int:
raise SystemExit(f"unknown torch version: {args.torch}")
if args.python not in PYTHON_VERSIONS:
raise SystemExit(f"unknown python version: {args.python}")
- print(wheel_name(args.package, args.torch, args.python))
+ if args.platform not in PLATFORMS:
+ raise SystemExit(f"unknown platform: {args.platform}")
+ print(wheel_name(args.package, args.torch, args.python, args.platform))
return 0
-def _cmd_notes(args: argparse.Namespace) -> int:
+def _read_digest_lines() -> list[tuple[str, str]]:
+ """`digest filename` lines on stdin, in sha256sum's layout or with a single space."""
entries = []
for line in sys.stdin:
line = line.strip()
@@ -374,7 +576,16 @@ def _cmd_notes(args: argparse.Namespace) -> int:
if not name:
digest, _, name = line.partition(" ")
entries.append((digest.strip(), name.strip().lstrip("*")))
- sys.stdout.write(render_notes(entries, tag = args.tag, repo = args.repo))
+ return entries
+
+
+def _cmd_notes(args: argparse.Namespace) -> int:
+ sys.stdout.write(render_notes(_read_digest_lines(), tag = args.tag, repo = args.repo))
+ return 0
+
+
+def _cmd_sums(args: argparse.Namespace) -> int:
+ sys.stdout.write(render_sums(_read_digest_lines()))
return 0
@@ -386,8 +597,8 @@ def main(argv: list[str] | None = None) -> int:
matrix_parser.add_argument(
"--github",
action = "store_true",
- help = "also append matrix/count and warm_matrix/warm_count to $GITHUB_OUTPUT and a "
- "listing to $GITHUB_STEP_SUMMARY",
+ help = "also append matrix/count, warm_matrix/warm_count and gpu_matrix/gpu_count to "
+ "$GITHUB_OUTPUT and a listing to $GITHUB_STEP_SUMMARY",
)
matrix_parser.set_defaults(func = _cmd_matrix)
@@ -395,6 +606,7 @@ def main(argv: list[str] | None = None) -> int:
name_parser.add_argument("--package", required = True)
name_parser.add_argument("--torch", required = True)
name_parser.add_argument("--python", required = True)
+ name_parser.add_argument("--platform", default = PLATFORM_TAG)
name_parser.set_defaults(func = _cmd_wheel_name)
notes_parser = sub.add_parser("notes", help = "print the release body, digests on stdin")
@@ -402,6 +614,9 @@ def main(argv: list[str] | None = None) -> int:
notes_parser.add_argument("--repo", required = True)
notes_parser.set_defaults(func = _cmd_notes)
+ sums_parser = sub.add_parser("sums", help = "print SHA256SUMS for every wheel, digests on stdin")
+ sums_parser.set_defaults(func = _cmd_sums)
+
args = parser.parse_args(argv)
return args.func(args)
diff --git a/.github/scripts/prebuilt_wheels_check_binaries.py b/.github/scripts/prebuilt_wheels_check_binaries.py
new file mode 100644
index 00000000000..f49a13a07bf
--- /dev/null
+++ b/.github/scripts/prebuilt_wheels_check_binaries.py
@@ -0,0 +1,97 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Check every native extension in a wheel was built for the wheel's platform.
+
+An ARM runner can still run an emulated x64 Python or link with an x64 toolchain, and the wheel
+would be named for arm64 either way. This reads the machine field of each .so (ELF e_machine) and
+.pyd (PE/COFF Machine). On Linux it also bounds the newest GLIBC symbol version referenced, because
+a plain `linux_*` tag makes pip skip the glibc check a manylinux tag would have done.
+
+Usage: prebuilt_wheels_check_binaries.py WHEEL PLATFORM [--max-glibc 2.35]
+"""
+
+from __future__ import annotations
+
+import re
+import struct
+import sys
+import zipfile
+
+ELF_MACHINE = {"linux_x86_64": 62, "linux_aarch64": 183} # EM_X86_64, EM_AARCH64
+PE_MACHINE = {"win_amd64": 0x8664, "win_arm64": 0xAA64}
+_GLIBC = re.compile(rb"GLIBC_(\d+)\.(\d+)(?:\.\d+)?\x00")
+
+
+def elf_machine(data: bytes) -> int:
+ if data[:4] != b"\x7fELF":
+ raise ValueError("not an ELF file")
+ endian = "<" if data[5] == 1 else ">"
+ return struct.unpack_from(f"{endian}H", data, 18)[0]
+
+
+def pe_machine(data: bytes) -> int:
+ if data[:2] != b"MZ":
+ raise ValueError("not a PE file")
+ offset = struct.unpack_from("<I", data, 0x3C)[0]
+ if data[offset : offset + 4] != b"PE\x00\x00":
+ raise ValueError("no PE signature")
+ return struct.unpack_from("<H", data, offset + 4)[0]
+
+
+def newest_glibc(data: bytes) -> tuple[int, int] | None:
+ found = [(int(major), int(minor)) for major, minor in _GLIBC.findall(data)]
+ return max(found) if found else None
+
+
+def check(
+ wheel: str,
+ platform: str,
+ max_glibc: tuple[int, int] = (2, 35),
+) -> list[str]:
+ """Problems found, one line each; empty when the wheel is consistent."""
+ problems = []
+ checked = 0
+ with zipfile.ZipFile(wheel) as zf:
+ for name in zf.namelist():
+ if platform in PE_MACHINE and name.endswith(".pyd"):
+ data = zf.read(name)
+ got = pe_machine(data)
+ if got != PE_MACHINE[platform]:
+ problems.append(f"{name}: PE machine {got:#x}, want {PE_MACHINE[platform]:#x}")
+ checked += 1
+ elif platform in ELF_MACHINE and re.search(r"\.so(\.|$)", name):
+ data = zf.read(name)
+ got = elf_machine(data)
+ if got != ELF_MACHINE[platform]:
+ problems.append(f"{name}: ELF machine {got}, want {ELF_MACHINE[platform]}")
+ glibc = newest_glibc(data)
+ print(f"{name}: newest GLIBC {'.'.join(map(str, glibc)) if glibc else 'none'}")
+ if glibc and glibc > max_glibc:
+ problems.append(
+ f"{name}: needs GLIBC {glibc[0]}.{glibc[1]}, floor is {max_glibc[0]}.{max_glibc[1]}"
+ )
+ checked += 1
+ if not checked:
+ problems.append(f"no native extension for {platform} in {wheel}")
+ return problems
+
+
+def main(argv: list[str]) -> int:
+ if len(argv) not in (2, 4) or (len(argv) == 4 and argv[2] != "--max-glibc"):
+ print(__doc__, file = sys.stderr)
+ return 2
+ wheel, platform = argv[:2]
+ if platform not in ELF_MACHINE and platform not in PE_MACHINE:
+ print(f"unknown platform {platform}", file = sys.stderr)
+ return 2
+ max_glibc = tuple(int(part) for part in argv[3].split(".")) if len(argv) == 4 else (2, 35)
+ problems = check(wheel, platform, max_glibc)
+ for problem in problems:
+ print(f"::error::{problem}")
+ return 1 if problems else 0
+
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv[1:]))
diff --git a/.github/scripts/prebuilt_wheels_cuda_redist.py b/.github/scripts/prebuilt_wheels_cuda_redist.py
new file mode 100644
index 00000000000..6de90fb7e26
--- /dev/null
+++ b/.github/scripts/prebuilt_wheels_cuda_redist.py
@@ -0,0 +1,113 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Install a CUDA toolkit subset from NVIDIA's redistributable archives, every byte hash-checked.
+
+Windows ARM64 has no CUDA before 13.4, and its network installer's subpackage names are not the
+x64 ones. The redist archives are what NVIDIA itself lists per platform, each with a sha256 in a
+release manifest. The manifest's own sha256 is pinned by the caller, so the chain is: pinned
+manifest -> per-archive sha256 -> extracted tree. Each archive unpacks to one top-level directory
+holding bin/ include/ lib/ (and nvvm/); those are merged into DEST.
+
+Usage: prebuilt_wheels_cuda_redist.py MANIFEST_URL MANIFEST_SHA256 PLATFORM DEST COMPONENT...
+"""
+
+from __future__ import annotations
+
+import hashlib
+import json
+import shutil
+import sys
+import time
+import urllib.request
+import zipfile
+from pathlib import Path
+
+REDIST_BASE = "https://developer.download.nvidia.com/compute/cuda/redist/"
+
+
+def _fetch(url: str, dest: Path) -> None:
+ for attempt in range(1, 4):
+ try:
+ with urllib.request.urlopen(url, timeout = 300) as response, open(dest, "wb") as out:
+ shutil.copyfileobj(response, out, 1 << 20)
+ return
+ except OSError:
+ if attempt == 3:
+ raise
+ time.sleep(10 * attempt)
+
+
+def _sha256(path: Path) -> str:
+ digest = hashlib.sha256()
+ with open(path, "rb") as handle:
+ for block in iter(lambda: handle.read(1 << 20), b""):
+ digest.update(block)
+ return digest.hexdigest()
+
+
+def plan(manifest: dict, platform: str, components: list[str]) -> list[tuple[str, str]]:
+ """(relative_path, sha256) per component, refusing any the manifest lacks for PLATFORM."""
+ missing = [name for name in components if platform not in manifest.get(name, {})]
+ if missing:
+ raise SystemExit(f"not in the manifest for {platform}: {', '.join(missing)}")
+ return [
+ (manifest[name][platform]["relative_path"], manifest[name][platform]["sha256"])
+ for name in components
+ ]
+
+
+def merge(archive: Path, dest: Path) -> None:
+ """Extract ARCHIVE's single top-level directory into DEST, refusing paths that escape it."""
+ with zipfile.ZipFile(archive) as zf:
+ names = [name for name in zf.namelist() if not name.endswith("/")]
+ tops = {name.split("/", 1)[0] for name in names}
+ if len(tops) != 1:
+ raise SystemExit(
+ f"{archive.name}: expected one top-level directory, got {sorted(tops)}"
+ )
+ root = dest.resolve()
+ for name in names:
+ relative = name.split("/", 1)[1] if "/" in name else ""
+ if not relative or relative == "LICENSE":
+ continue
+ target = (dest / relative).resolve()
+ if root not in target.parents:
+ raise SystemExit(f"{archive.name}: {name} escapes {dest}")
+ target.parent.mkdir(parents = True, exist_ok = True)
+ with zf.open(name) as source, open(target, "wb") as out:
+ shutil.copyfileobj(source, out)
+
+
+def main(argv: list[str]) -> int:
+ if len(argv) < 5:
+ print(__doc__, file = sys.stderr)
+ return 2
+ manifest_url, manifest_sha, platform, dest, *components = argv
+ dest_path = Path(dest)
+ work = dest_path.parent / f"{dest_path.name}-redist"
+ work.mkdir(parents = True, exist_ok = True)
+ manifest_file = work / "manifest.json"
+ _fetch(manifest_url, manifest_file)
+ got = _sha256(manifest_file)
+ if got != manifest_sha:
+ raise SystemExit(f"manifest sha256 {got}, pinned {manifest_sha}")
+ manifest = json.loads(manifest_file.read_text(encoding = "utf-8"))
+ print(f"CUDA {manifest.get('release_label')} ({manifest.get('release_date')}) for {platform}")
+ dest_path.mkdir(parents = True, exist_ok = True)
+ for relative_path, sha in plan(manifest, platform, components):
+ archive = work / Path(relative_path).name
+ _fetch(REDIST_BASE + relative_path, archive)
+ got = _sha256(archive)
+ if got != sha:
+ raise SystemExit(f"{archive.name}: sha256 {got}, manifest says {sha}")
+ merge(archive, dest_path)
+ archive.unlink()
+ print(f" {archive.name} ok")
+ shutil.rmtree(work, ignore_errors = True)
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv[1:]))
diff --git a/.github/scripts/prebuilt_wheels_shard.py b/.github/scripts/prebuilt_wheels_shard.py
index 7153c31d730..022d8745318 100644
--- a/.github/scripts/prebuilt_wheels_shard.py
+++ b/.github/scripts/prebuilt_wheels_shard.py
@@ -5,6 +5,9 @@
"""Compile one shard of a torch extension's objects into ccache, then stop before linking.
Only ninja's targets change; compiler commands must stay identical to the full build.
+
+Windows targets are `D:\\...\\kernel.obj: cuda_compile`, so split on the LAST ": ". ninja with no
+targets builds everything, so an empty inventory is an error and an empty slice skips ninja.
"""
import os
@@ -13,17 +16,27 @@ import subprocess
import sys
-def slice_objects(ninja_targets: str, shard: int, shards: int) -> list[str]:
- """Select every SHARDS-th object from sorted, unique ninja targets, starting at SHARD."""
- if not 0 <= shard < shards:
- raise SystemExit(f"shard {shard} is outside 0..{shards - 1}")
- objects = sorted(
+OBJECT_SUFFIXES = (".o", ".obj")
+
+
+def list_objects(ninja_targets: str) -> list[str]:
+ """Every object target in `ninja -t targets` output, sorted and unique."""
+ return sorted(
{
target
- for target in (line.split(":")[0] for line in ninja_targets.splitlines())
- if target.endswith(".o")
+ for target in (line.rsplit(": ", 1)[0].strip() for line in ninja_targets.splitlines())
+ if target.endswith(OBJECT_SUFFIXES)
}
)
+
+
+def slice_objects(ninja_targets: str, shard: int, shards: int) -> list[str]:
+ """Select every SHARDS-th object from sorted, unique ninja targets, starting at SHARD."""
+ if not 0 <= shard < shards:
+ raise SystemExit(f"shard {shard} is outside 0..{shards - 1}")
+ objects = list_objects(ninja_targets)
+ if not objects:
+ raise SystemExit("ninja listed no object targets; refusing to run a targetless build")
return objects[shard::shards]
@@ -41,8 +54,10 @@ def main() -> None:
check = True,
).stdout
mine = slice_objects(listing, shard, shards)
- total = len(slice_objects(listing, 0, 1))
+ total = len(list_objects(listing))
print(f"shard {shard + 1} of {shards}: {len(mine)} of {total} objects", flush = True)
+ if not mine:
+ raise SystemExit(0)
jobs = os.environ.get("MAX_JOBS", "1")
subprocess.run(["ninja", "-v", "-j", jobs, *mine], cwd = build_directory, check = True)
raise SystemExit(0)
diff --git a/.github/scripts/prebuilt_wheels_timeout.py b/.github/scripts/prebuilt_wheels_timeout.py
new file mode 100644
index 00000000000..274b067052d
--- /dev/null
+++ b/.github/scripts/prebuilt_wheels_timeout.py
@@ -0,0 +1,91 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Run a build under a deadline and, when it passes, stop the WHOLE process tree.
+
+Git Bash's `timeout` only ends the one native process it started; the orphaned ninja/nvcc tree
+keeps the step alive into the six-hour limit, so the warm slice's partial ccache is never saved.
+Windows uses `taskkill /T /F`; POSIX sends SIGINT to the group, then SIGKILL. Exits 124 on timeout.
+
+Usage: prebuilt_wheels_timeout.py <duration, e.g. 300m, 90s, 2h> -- <command> [args...]
+"""
+
+from __future__ import annotations
+
+import os
+import signal
+import subprocess
+import sys
+import time
+
+GRACE_SECONDS = 120
+UNITS = {"s": 1, "m": 60, "h": 3600}
+
+
+def parse_duration(text: str) -> float:
+ text = text.strip()
+ unit = text[-1:] if text[-1:] in UNITS else "s"
+ number = text[:-1] if text[-1:] in UNITS else text
+ try:
+ value = float(number)
+ except ValueError:
+ raise SystemExit(f"not a duration: {text!r}") from None
+ if value <= 0:
+ raise SystemExit(f"not a duration: {text!r}")
+ return value * UNITS[unit]
+
+
+def kill_tree(process: subprocess.Popen) -> None:
+ if os.name == "nt":
+ # /T takes every descendant, /F because nvcc ignores a polite close.
+ subprocess.run(
+ ["taskkill", "/T", "/F", "/PID", str(process.pid)],
+ stdout = subprocess.DEVNULL,
+ stderr = subprocess.DEVNULL,
+ check = False,
+ )
+ return
+ try:
+ os.killpg(process.pid, signal.SIGINT)
+ except ProcessLookupError:
+ return
+ try:
+ process.wait(timeout = GRACE_SECONDS)
+ except subprocess.TimeoutExpired:
+ try:
+ os.killpg(process.pid, signal.SIGKILL)
+ except ProcessLookupError:
+ pass
+
+
+def run(seconds: float, command: list[str]) -> int:
+ if os.name == "nt":
+ process = subprocess.Popen(command, creationflags = subprocess.CREATE_NEW_PROCESS_GROUP)
+ else:
+ process = subprocess.Popen(command, start_new_session = True)
+ deadline = time.monotonic() + seconds
+ try:
+ return process.wait(timeout = max(0.0, deadline - time.monotonic()))
+ except subprocess.TimeoutExpired:
+ print(
+ f"::warning::deadline of {seconds:.0f}s reached; ending the process tree",
+ flush = True,
+ )
+ kill_tree(process)
+ try:
+ process.wait(timeout = GRACE_SECONDS)
+ except subprocess.TimeoutExpired:
+ pass
+ return 124
+
+
+def main(argv: list[str]) -> int:
+ if len(argv) < 4 or argv[2] != "--":
+ print(__doc__.strip().splitlines()[-1], file = sys.stderr)
+ return 2
+ return run(parse_duration(argv[1]), argv[3:])
+
+
+if __name__ == "__main__":
+ raise SystemExit(main(sys.argv))
diff --git a/.github/workflows/prebuilt-cuda-wheels.yml b/.github/workflows/prebuilt-cuda-wheels.yml
index 68fca57f568..3d5327b2cd1 100644
--- a/.github/workflows/prebuilt-cuda-wheels.yml
+++ b/.github/workflows/prebuilt-cuda-wheels.yml
@@ -9,6 +9,9 @@
# five hours of nvcc for flash-attn, on a machine that has to have the CUDA toolkit installed.
# So every torch minor from 2.13 on needs its own wheel set, and this is where they come from.
#
+# win_amd64 too: upstream has never published a Windows wheel of any of the three. OS-specific
+# steps are gated on runner.os; the rest is the same bash on both.
+#
# Dispatch only, once per torch minor, and `publish: false` is the default: a run then builds,
# smoke tests and signs, and leaves everything as artifacts to inspect. Nothing here is reachable
# from a pull request, because the publish job writes a release under our name.
@@ -37,6 +40,10 @@ on:
description: 'packages, comma separated (flash-attn, causal-conv1d, mamba-ssm)'
type: string
default: 'flash-attn,causal-conv1d,mamba-ssm'
+ platforms:
+ description: 'platforms, comma separated (linux_x86_64, win_amd64, linux_aarch64, win_arm64)'
+ type: string
+ default: 'linux_x86_64,win_amd64'
release_tag:
description: 'release tag to publish under'
type: string
@@ -74,6 +81,8 @@ jobs:
count: ${{ steps.plan.outputs.count }}
warm_matrix: ${{ steps.plan.outputs.warm_matrix }}
warm_count: ${{ steps.plan.outputs.warm_count }}
+ gpu_matrix: ${{ steps.plan.outputs.gpu_matrix }}
+ gpu_count: ${{ steps.plan.outputs.gpu_count }}
steps:
- name: Harden runner (egress block)
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
@@ -102,6 +111,7 @@ jobs:
UW_PACKAGES: ${{ inputs.packages }}
UW_TORCH_VERSIONS: ${{ inputs.torch_versions }}
UW_PYTHON_VERSIONS: ${{ inputs.python_versions }}
+ UW_PLATFORMS: ${{ inputs.platforms }}
run: |
set -euo pipefail
# --github writes matrix= and count= into $GITHUB_OUTPUT itself. The JSON is full of
@@ -112,12 +122,15 @@ jobs:
name: warm ${{ matrix.label }}
needs: plan
if: needs.plan.outputs.warm_count != '0'
- runs-on: ubuntu-22.04
+ runs-on: ${{ matrix.runner }}
# Best effort: the build job compiles whatever a warm job did not.
continue-on-error: true
timeout-minutes: 340
permissions:
contents: read
+ defaults:
+ run:
+ shell: bash
strategy:
fail-fast: false
max-parallel: 16
@@ -128,15 +141,26 @@ jobs:
CCACHE_BASEDIR: ${{ github.workspace }}
CCACHE_NOHASHDIR: 'true'
CCACHE_MAXSIZE: 20G
- # torch reads this for the nvcc in build.ninja.
- PYTORCH_NVCC: ccache /usr/local/cuda-13.0/bin/nvcc
+ # torch writes this into build.ninja unquoted, so the Windows value has no spaces.
+ PYTORCH_NVCC: ${{ matrix.pytorch_nvcc }}
steps:
- name: Harden runner (audit)
- # Audit egress because package downloads use changing endpoints.
+ # Audit egress because package downloads use changing endpoints. Its agent is Linux-only.
+ if: runner.os == 'Linux'
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
with:
egress-policy: audit
+ # Before checkout: flash-attn's cutlass submodule has paths past MAX_PATH.
+ - name: Enable long paths (Windows)
+ if: runner.os == 'Windows'
+ shell: pwsh
+ run: |
+ $ErrorActionPreference = 'Stop'
+ git config --system core.longpaths true
+ New-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' `
+ -Name LongPathsEnabled -Value 1 -PropertyType DWORD -Force | Out-Null
+
- name: Checkout Unsloth (build scripts only)
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
@@ -159,6 +183,7 @@ jobs:
# ~25 GB free on a fresh runner. These three directories are ~13 GB of preinstalled
# toolchains this job never touches.
- name: Free up disk space
+ if: runner.os == 'Linux'
run: |
set -euo pipefail
df -h /
@@ -175,16 +200,19 @@ jobs:
# repository uses, because an unbounded apt step does not fail, it spends the job's whole
# budget and is reported as "cancelled" with every later step skipped.
- name: Install the CUDA ${{ matrix.cuda_tag }} toolkit
+ if: runner.os == 'Linux'
timeout-minutes: 25
env:
RETRY_ATTEMPTS: '2'
RETRY_ATTEMPT_TIMEOUT: '600'
+ # NVIDIA's repository for server-class aarch64 (Grace, GB10) is sbsa, not the Jetson one.
+ CUDA_REPO_ARCH: ${{ matrix.platform == 'linux_aarch64' && 'sbsa' || 'x86_64' }}
run: |
set -euo pipefail
keyring=cuda-keyring_1.1-1_all.deb
curl --fail --silent --show-error --location --retry 5 --retry-delay 5 \
-o "$keyring" \
- "https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/$keyring"
+ "https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/$CUDA_REPO_ARCH/$keyring"
sudo dpkg -i "$keyring"
bash unsloth/.github/scripts/retry-with-apt-lock.sh sudo sh -c \
'apt-get update && apt-get install -y --no-install-recommends cuda-toolkit-13-0'
@@ -193,6 +221,7 @@ jobs:
# Cache hits require matching compiler commands and checkout paths in both jobs.
- name: Install ccache
+ if: runner.os == 'Linux'
timeout-minutes: 13
env:
RETRY_ATTEMPTS: '2'
@@ -204,10 +233,166 @@ jobs:
ccache --version | head -1
mkdir -p "$CCACHE_DIR"
+ # NVIDIA's own installer, Authenticode-checked, not a setup action: this compiler's output is
+ # what we sign. 13.0.2 = nvcc 13.0.88, the same as the Linux apt package. C:\cuda is a
+ # junction so build.ninja never sees a space.
+ - name: Install the CUDA ${{ matrix.cuda_tag }} toolkit (Windows)
+ if: matrix.platform == 'win_amd64'
+ timeout-minutes: 25
+ shell: pwsh
+ run: |
+ $ErrorActionPreference = 'Stop'
+ $version = '13.0.2'
+ $url = "https://developer.download.nvidia.com/compute/cuda/$version/network_installers/cuda_${version}_windows_network.exe"
+ $installer = Join-Path $env:RUNNER_TEMP 'cuda_network.exe'
+ for ($attempt = 1; $attempt -le 3; $attempt++) {
+ try { Invoke-WebRequest -Uri $url -OutFile $installer -TimeoutSec 300; break }
+ catch { if ($attempt -eq 3) { throw }; Start-Sleep -Seconds 10 }
+ }
+ $signature = Get-AuthenticodeSignature -FilePath $installer
+ if ($signature.Status -ne 'Valid' -or $signature.SignerCertificate.Subject -notmatch 'O=NVIDIA Corporation') {
+ throw "CUDA installer signature: $($signature.Status) $($signature.SignerCertificate.Subject)"
+ }
+ Write-Host "signed by $($signature.SignerCertificate.Subject)"
+ # Names from the subpackage table in NVIDIA's CUDA 13.0 Windows installation guide.
+ $packages = @(
+ 'nvcc', 'nvvm', 'crt', 'cudart', 'thrust', 'nvtx', 'cuda_profiler_api',
+ 'cublas', 'cublas_dev', 'cusparse', 'cusparse_dev', 'cusolver', 'cusolver_dev',
+ 'curand', 'curand_dev', 'cufft', 'cufft_dev', 'nvrtc', 'nvrtc_dev', 'nvjitlink',
+ 'cuobjdump', 'nvdisasm', 'nvprune'
+ ) | ForEach-Object { "${_}_13.0" }
+ # -s: silent, only the subpackages named; -n: never reboot.
+ $process = Start-Process -FilePath $installer -ArgumentList (@('-s') + $packages + @('-n')) -Wait -PassThru -NoNewWindow
+ if ($process.ExitCode -ne 0) { throw "CUDA installer exited $($process.ExitCode)" }
+ $root = 'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.0'
+ if (-not (Test-Path (Join-Path $root 'bin\nvcc.exe'))) { throw "no nvcc.exe under $root" }
+ New-Item -ItemType Junction -Path 'C:\cuda' -Target $root | Out-Null
+ 'C:\cuda\bin' | Out-File -FilePath $env:GITHUB_PATH -Append -Encoding utf8
+ 'CUDA_HOME=C:\cuda', 'CUDA_PATH=C:\cuda' | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
+ & 'C:\cuda\bin\nvcc.exe' --version
+
+ # CUDA 13.4 is the first toolkit with a Windows ARM64 nvcc and what NVIDIA's torch 2.14+cu134
+ # is built with. From the redist archives: the manifest is pinned by sha256 and lists one for
+ # every archive, so nothing unhashed is unpacked. Installed straight into C:\cuda.
+ - name: Install the CUDA 13.4 toolkit (Windows ARM64)
+ if: matrix.platform == 'win_arm64'
+ timeout-minutes: 25
+ shell: pwsh
+ run: |
+ $ErrorActionPreference = 'Stop'
+ python unsloth/.github/scripts/prebuilt_wheels_cuda_redist.py `
+ 'https://developer.download.nvidia.com/compute/cuda/redist/redistrib_13.4.1.json' `
+ '291178934b2139727407c76f697bb0dfbac4014753a8094d7c290b0f4124a302' `
+ 'windows-arm64' 'C:\cuda' `
+ cuda_nvcc libnvvm cuda_crt cuda_cudart cccl cuda_nvtx cuda_profiler_api `
+ libcublas libcusparse libcusolver libcurand libcufft cuda_nvrtc libnvjitlink `
+ cuda_cuobjdump cuda_nvdisasm cuda_nvprune
+ if ($LASTEXITCODE -ne 0) { throw "CUDA redist install exited $LASTEXITCODE" }
+ # The ARM64 archives put cudart.lib and friends in lib\arm64; torch's cpp_extension links
+ # from CUDA_HOME\lib\x64 on every Windows (measured: LNK1181 cannot open 'cudart.lib').
+ New-Item -ItemType Junction -Path 'C:\cuda\lib\x64' -Target 'C:\cuda\lib\arm64' | Out-Null
+ if (-not (Test-Path 'C:\cuda\lib\x64\cudart.lib')) { throw 'no cudart.lib under C:\cuda\lib\x64' }
+ 'C:\cuda\bin' | Out-File -FilePath $env:GITHUB_PATH -Append -Encoding utf8
+ 'CUDA_HOME=C:\cuda', 'CUDA_PATH=C:\cuda' | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
+ & 'C:\cuda\bin\nvcc.exe' --version
+
+ # vcvars64 exported to later steps (no setup action). DISTUTILS_USE_SDK=1 makes setuptools use
+ # it as-is and turns on flash-attn's MSVC flags.
+ - name: Activate the MSVC ${{ matrix.platform == 'win_arm64' && 'arm64' || 'x64' }} environment (Windows)
+ if: runner.os == 'Windows'
+ shell: pwsh
+ env:
+ MSVC_COMPONENT: ${{ matrix.platform == 'win_arm64' && 'Microsoft.VisualStudio.Component.VC.Tools.ARM64' || 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64' }}
+ VCVARS: ${{ matrix.platform == 'win_arm64' && 'vcvarsarm64.bat' || 'vcvars64.bat' }}
+ NVCC_APPEND_ARM64: ${{ matrix.platform == 'win_arm64' && ' -Xcompiler=/Zc:preprocessor' || '' }}
+ run: |
+ $ErrorActionPreference = 'Stop'
+ $vswhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe'
+ $vs = & $vswhere -latest -products * -requires $env:MSVC_COMPONENT -property installationPath
+ if (-not $vs) { throw "no Visual Studio with $env:MSVC_COMPONENT" }
+ $bat = Join-Path $vs "VC\Auxiliary\Build\$env:VCVARS"
+ $lines = cmd /c "`"$bat`" >nul 2>&1 && set"
+ if ($LASTEXITCODE -ne 0) { throw "$env:VCVARS exited $LASTEXITCODE" }
+ $before = @{}
+ Get-ChildItem env: | ForEach-Object { $before[$_.Name] = $_.Value }
+ $oldPath = $env:Path -split ';'
+ foreach ($line in $lines) {
+ if ($line -notmatch '^([^=]+)=(.*)$') { continue }
+ $name = $Matches[1]; $value = $Matches[2]
+ if ($name -ieq 'Path') {
+ # Only the entries vcvars added, through GITHUB_PATH, so C:\cuda\bin stays put.
+ foreach ($entry in ($value -split ';')) {
+ if ($entry -and $oldPath -notcontains $entry) {
+ $entry | Out-File -FilePath $env:GITHUB_PATH -Append -Encoding utf8
+ }
+ }
+ } elseif ($before[$name] -ne $value) {
+ "$name=$value" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
+ }
+ }
+ 'DISTUTILS_USE_SDK=1' | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
+ # flash-attn's softmax.h and mamba's selective-scan kernels use M_LOG2E, which MSVC's
+ # <cmath> only defines under _USE_MATH_DEFINES (measured: `identifier "M_LOG2E" is
+ # undefined` in both). nvcc appends this to every invocation, host preprocessing included.
+ 'NVCC_APPEND_FLAGS=-D_USE_MATH_DEFINES' + $env:NVCC_APPEND_ARM64 | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
+ # CUDA 13.4's CCCL stops with C1189 under MSVC's traditional preprocessor (measured on
+ # windows-11-arm); CL reaches the cl.exe torch's cpp_extension runs on the .cpp sources.
+ if ($env:NVCC_APPEND_ARM64) { 'CL=/Zc:preprocessor' | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8 }
+ Write-Host "Visual Studio at $vs"
+
+ # compiler_check=content: the default keys on nvcc.exe's mtime, which can differ per runner and
+ # would make every warm slice a miss in the build job.
+ - name: Install ccache (Windows)
+ if: runner.os == 'Windows'
+ timeout-minutes: 10
+ shell: pwsh
+ env:
+ WHEEL_PLATFORM: ${{ matrix.platform }}
+ run: |
+ $ErrorActionPreference = 'Stop'
+ $pinned = @{
+ 'ccache-4.14.1-windows-x86_64' = '6219f3865ca59aec41ee4b678df171d5d35855ecb2b6dbbbd20690b3a68af7b4'
+ 'ccache-4.14.1-windows-aarch64' = '28121851c8c2c8b76214cfc5e119d769fbc39e93d27862e4e3a556905ebd23b2'
+ }
+ $name = if ($env:WHEEL_PLATFORM -eq 'win_arm64') { 'ccache-4.14.1-windows-aarch64' } else { 'ccache-4.14.1-windows-x86_64' }
+ $zip = Join-Path $env:RUNNER_TEMP "$name.zip"
+ Invoke-WebRequest -Uri "https://github.com/ccache/ccache/releases/download/v4.14.1/$name.zip" -OutFile $zip -TimeoutSec 300
+ $hash = (Get-FileHash -Path $zip -Algorithm SHA256).Hash.ToLowerInvariant()
+ if ($hash -ne $pinned[$name]) { throw "ccache sha256 $hash" }
+ Expand-Archive -Path $zip -DestinationPath $env:RUNNER_TEMP -Force
+ New-Item -ItemType Directory -Force -Path 'C:\ccache' | Out-Null
+ Copy-Item -Path (Join-Path $env:RUNNER_TEMP "$name\ccache.exe") -Destination 'C:\ccache\ccache.exe'
+ 'C:\ccache' | Out-File -FilePath $env:GITHUB_PATH -Append -Encoding utf8
+ 'CCACHE_COMPILERCHECK=content' | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
+ New-Item -ItemType Directory -Force -Path $env:CCACHE_DIR | Out-Null
+ & 'C:\ccache\ccache.exe' --version | Select-Object -First 1
+
+ # The Linux swap file's counterpart: windows-2022 has a fixed 2.9 GB page file (18.9 GB commit
+ # limit on 16 GB RAM). NtCreatePagingFile, so no reboot.
+ - name: Add a page file (Windows)
+ if: runner.os == 'Windows'
+ uses: al-cheb/configure-pagefile-action@9b6da52fb72a3c6147c1aad2df22d8d905681adc # v1.5
+ with:
+ minimum-size: 16GB
+ maximum-size: 32GB
+ # windows-11-arm has no D: drive.
+ disk-root: ${{ matrix.platform == 'win_arm64' && 'C:' || 'D:' }}
+
+ - name: Report memory, page file and disk (Windows)
+ if: runner.os == 'Windows'
+ shell: pwsh
+ run: |
+ $os = Get-CimInstance Win32_OperatingSystem
+ $cs = Get-CimInstance Win32_ComputerSystem
+ '{0:N1} GB RAM, {1:N1} GB commit limit, system-managed page file: {2}' -f ($os.TotalVisibleMemorySize / 1MB), ($os.TotalVirtualMemorySize / 1MB), $cs.AutomaticManagedPagefile
+ Get-CimInstance Win32_PageFileUsage | Format-Table Name, AllocatedBaseSize, CurrentUsage -AutoSize
+ Get-PSDrive -PSProvider FileSystem | Format-Table Name, @{n = 'FreeGB'; e = { [math]::Round($_.Free / 1GB, 1) } }, @{n = 'UsedGB'; e = { [math]::Round($_.Used / 1GB, 1) } } -AutoSize
+
# 10 GB of swap. nvcc's peak RSS on the sm_100 flash-attn kernels is the reason MAX_JOBS is
# 1; swap is what keeps a single job that briefly exceeds the runner's 16 GB from being
# OOM-killed four hours in.
- name: Set up swap space
+ if: runner.os == 'Linux'
run: |
set -euo pipefail
sudo fallocate -l 10G /mnt/swapfile
@@ -216,13 +401,14 @@ jobs:
sudo swapon /mnt/swapfile
free -h
- - name: Install torch ${{ matrix.torch }}+cu130
+ - name: Install ${{ matrix.torch_spec }}
run: |
set -euo pipefail
python -m pip install --upgrade pip
python -m pip install 'setuptools>=77' wheel ninja packaging
- python -m pip install --no-cache-dir "torch==${{ matrix.torch }}" \
- --index-url https://download.pytorch.org/whl/cu130
+ # TORCH_INDEX_ARGS is an allowlisted constant from prebuilt_wheels.py; word splitting intended.
+ # shellcheck disable=SC2086
+ python -m pip install --no-cache-dir "$TORCH_SPEC" $TORCH_INDEX_ARGS
nvcc --version
python -c "import torch; print('torch', torch.__version__, 'cuda', torch.version.cuda, 'cxx11abi', torch._C._GLIBCXX_USE_CXX11_ABI)"
python - <<'PY'
@@ -230,14 +416,26 @@ jobs:
want_torch = os.environ["MATRIX_TORCH"]
if torch.__version__.split("+")[0] != want_torch:
sys.exit(f"installed torch {torch.__version__}, wanted {want_torch}")
+ if "+" in os.environ["TORCH_SPEC"] and torch.__version__ != os.environ["TORCH_SPEC"].split("==", 1)[1]:
+ sys.exit(f"installed torch {torch.__version__}, wanted {os.environ['TORCH_SPEC']}")
+ # An emulated x64 interpreter on an ARM runner would build an x64 extension.
+ import sysconfig
+ if sysconfig.get_platform() != os.environ["PY_PLATFORM"]:
+ sys.exit(f"interpreter platform {sysconfig.get_platform()}, wanted {os.environ['PY_PLATFORM']}")
if not torch.version.cuda.startswith("13."):
sys.exit(f"torch reports CUDA {torch.version.cuda}, wanted 13.x")
- # Verify the ABI matches the wheel's cxx11abiTRUE tag.
- if not torch._C._GLIBCXX_USE_CXX11_ABI:
- sys.exit("torch was built with _GLIBCXX_USE_CXX11_ABI=0; the cxx11abiTRUE tag would be wrong")
+ # Verify the ABI matches the wheel's cxx11abi tag, which every upstream setup.py copies
+ # from this attribute. It is True on the Windows cu130 wheels too, not just on Linux.
+ want = os.environ["MATRIX_ABI"]
+ if bool(torch._C._GLIBCXX_USE_CXX11_ABI) != (want == "TRUE"):
+ sys.exit(f"torch reports _GLIBCXX_USE_CXX11_ABI={torch._C._GLIBCXX_USE_CXX11_ABI}; the cxx11abi{want} tag would be wrong")
PY
env:
MATRIX_TORCH: ${{ matrix.torch }}
+ MATRIX_ABI: ${{ matrix.abi }}
+ TORCH_SPEC: ${{ matrix.torch_spec }}
+ TORCH_INDEX_ARGS: ${{ matrix.torch_index_args }}
+ PY_PLATFORM: ${{ matrix.py_platform }}
# The one source edit in this workflow, and only for mamba-ssm. Its setup.py compiles the
# Mamba-1 selective-scan kernels at -std=c++17; torch 2.13's headers require C++20, and
@@ -247,15 +445,19 @@ jobs:
# this step loudly rather than silently building something else.
- name: Patch mamba-ssm for the torch 2.13 C++20 requirement
if: matrix.patch == 'cxx20'
- run: python3 unsloth/.github/scripts/patch_mamba_cxx20.py src/setup.py
+ run: python unsloth/.github/scripts/patch_mamba_cxx20.py src/setup.py
+
+ - name: Patch mamba-ssm for the MSVC host compiler (Windows)
+ if: runner.os == 'Windows' && matrix.package == 'mamba-ssm'
+ run: python unsloth/.github/scripts/patch_mamba_msvc.py src
- name: Restore this slice's ccache from earlier runs
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ccache
- key: prebuilt-ccache-${{ matrix.dist }}-${{ matrix.ref }}-torch${{ matrix.torch }}-${{ matrix.python_tag }}-shard${{ matrix.shard }}of${{ matrix.shards }}-${{ github.run_id }}
+ key: prebuilt-ccache-${{ matrix.platform }}-${{ matrix.dist }}-${{ matrix.ref }}-torch${{ matrix.torch }}-${{ matrix.python_tag }}-shard${{ matrix.shard }}of${{ matrix.shards }}-${{ github.run_id }}
restore-keys: |
- prebuilt-ccache-${{ matrix.dist }}-${{ matrix.ref }}-torch${{ matrix.torch }}-${{ matrix.python_tag }}-shard${{ matrix.shard }}of${{ matrix.shards }}-
+ prebuilt-ccache-${{ matrix.platform }}-${{ matrix.dist }}-${{ matrix.ref }}-torch${{ matrix.torch }}-${{ matrix.python_tag }}-shard${{ matrix.shard }}of${{ matrix.shards }}-
- name: Compile slice ${{ matrix.shard }} of ${{ matrix.shards }}
working-directory: src
@@ -273,18 +475,25 @@ jobs:
echo "export $pair"
done
ccache -z
- timeout --signal=INT --kill-after=120 "$BUILD_TIMEOUT" \
- python ../unsloth/.github/scripts/prebuilt_wheels_shard.py "$SHARD" "$SHARDS"
+ # Windows ends the whole process tree at the deadline; see prebuilt_wheels_timeout.py.
+ if [ "$RUNNER_OS" = "Windows" ]; then
+ python ../unsloth/.github/scripts/prebuilt_wheels_timeout.py "$BUILD_TIMEOUT" -- \
+ python ../unsloth/.github/scripts/prebuilt_wheels_shard.py "$SHARD" "$SHARDS"
+ else
+ timeout --signal=INT --kill-after=120 "$BUILD_TIMEOUT" \
+ python ../unsloth/.github/scripts/prebuilt_wheels_shard.py "$SHARD" "$SHARDS"
+ fi
- name: ccache stats
if: always()
- run: ccache -s
+ run: |
+ if [ "$RUNNER_OS" = "Windows" ]; then ccache -sv; else ccache -s; fi
- name: Hand the slice to the build job
if: always()
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
- name: ccache-${{ matrix.dist }}-torch${{ matrix.torch_mm }}-${{ matrix.python_tag }}-shard${{ matrix.shard }}
+ name: ccache-${{ matrix.platform }}-${{ matrix.dist }}-torch${{ matrix.torch_mm }}-${{ matrix.python_tag }}-shard${{ matrix.shard }}
path: ccache
if-no-files-found: warn
# ccache compresses its own entries.
@@ -299,19 +508,22 @@ jobs:
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ccache
- key: prebuilt-ccache-${{ matrix.dist }}-${{ matrix.ref }}-torch${{ matrix.torch }}-${{ matrix.python_tag }}-shard${{ matrix.shard }}of${{ matrix.shards }}-${{ github.run_id }}-${{ github.run_attempt }}
+ key: prebuilt-ccache-${{ matrix.platform }}-${{ matrix.dist }}-${{ matrix.ref }}-torch${{ matrix.torch }}-${{ matrix.python_tag }}-shard${{ matrix.shard }}of${{ matrix.shards }}-${{ github.run_id }}-${{ github.run_attempt }}
build:
name: ${{ matrix.label }}
needs: [plan, warm]
# Run even if warm was skipped or failed; compile any cache misses here.
if: ${{ !cancelled() && needs.plan.outputs.count != '0' }}
- # Keep the glibc floor at 2.35; pip does not check it for linux_x86_64 wheels.
- runs-on: ubuntu-22.04
+ # Keep the Linux glibc floor at 2.35; pip does not check it for linux_x86_64 wheels.
+ runs-on: ${{ matrix.runner }}
# Allow cleanup after the build step's timeout, below GitHub's 6-hour limit.
timeout-minutes: 340
permissions:
contents: read
+ defaults:
+ run:
+ shell: bash
strategy:
fail-fast: false
max-parallel: 5
@@ -321,15 +533,27 @@ jobs:
CCACHE_BASEDIR: ${{ github.workspace }}
CCACHE_NOHASHDIR: 'true'
CCACHE_MAXSIZE: 20G
- PYTORCH_NVCC: ccache /usr/local/cuda-13.0/bin/nvcc
+ # torch writes this into build.ninja unquoted, so the Windows value has no spaces.
+ PYTORCH_NVCC: ${{ matrix.pytorch_nvcc }}
steps:
# Keep setup through the mamba patch in sync with warm.
- name: Harden runner (audit)
- # Audit egress because package downloads use changing endpoints.
+ # Audit egress because package downloads use changing endpoints. Its agent is Linux-only.
+ if: runner.os == 'Linux'
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
with:
egress-policy: audit
+ # Before checkout: flash-attn's cutlass submodule has paths past MAX_PATH.
+ - name: Enable long paths (Windows)
+ if: runner.os == 'Windows'
+ shell: pwsh
+ run: |
+ $ErrorActionPreference = 'Stop'
+ git config --system core.longpaths true
+ New-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' `
+ -Name LongPathsEnabled -Value 1 -PropertyType DWORD -Force | Out-Null
+
- name: Checkout Unsloth (build scripts only)
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
@@ -349,6 +573,7 @@ jobs:
persist-credentials: false
- name: Free up disk space
+ if: runner.os == 'Linux'
run: |
set -euo pipefail
df -h /
@@ -360,16 +585,19 @@ jobs:
python-version: ${{ matrix.python }}
- name: Install the CUDA ${{ matrix.cuda_tag }} toolkit
+ if: runner.os == 'Linux'
timeout-minutes: 25
env:
RETRY_ATTEMPTS: '2'
RETRY_ATTEMPT_TIMEOUT: '600'
+ # NVIDIA's repository for server-class aarch64 (Grace, GB10) is sbsa, not the Jetson one.
+ CUDA_REPO_ARCH: ${{ matrix.platform == 'linux_aarch64' && 'sbsa' || 'x86_64' }}
run: |
set -euo pipefail
keyring=cuda-keyring_1.1-1_all.deb
curl --fail --silent --show-error --location --retry 5 --retry-delay 5 \
-o "$keyring" \
- "https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/$keyring"
+ "https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/$CUDA_REPO_ARCH/$keyring"
sudo dpkg -i "$keyring"
bash unsloth/.github/scripts/retry-with-apt-lock.sh sudo sh -c \
'apt-get update && apt-get install -y --no-install-recommends cuda-toolkit-13-0'
@@ -377,6 +605,7 @@ jobs:
echo "CUDA_HOME=/usr/local/cuda-13.0" >> "$GITHUB_ENV"
- name: Install ccache
+ if: runner.os == 'Linux'
timeout-minutes: 13
env:
RETRY_ATTEMPTS: '2'
@@ -388,7 +617,163 @@ jobs:
ccache --version | head -1
mkdir -p "$CCACHE_DIR"
+ # NVIDIA's own installer, Authenticode-checked, not a setup action: this compiler's output is
+ # what we sign. 13.0.2 = nvcc 13.0.88, the same as the Linux apt package. C:\cuda is a
+ # junction so build.ninja never sees a space.
+ - name: Install the CUDA ${{ matrix.cuda_tag }} toolkit (Windows)
+ if: matrix.platform == 'win_amd64'
+ timeout-minutes: 25
+ shell: pwsh
+ run: |
+ $ErrorActionPreference = 'Stop'
+ $version = '13.0.2'
+ $url = "https://developer.download.nvidia.com/compute/cuda/$version/network_installers/cuda_${version}_windows_network.exe"
+ $installer = Join-Path $env:RUNNER_TEMP 'cuda_network.exe'
+ for ($attempt = 1; $attempt -le 3; $attempt++) {
+ try { Invoke-WebRequest -Uri $url -OutFile $installer -TimeoutSec 300; break }
+ catch { if ($attempt -eq 3) { throw }; Start-Sleep -Seconds 10 }
+ }
+ $signature = Get-AuthenticodeSignature -FilePath $installer
+ if ($signature.Status -ne 'Valid' -or $signature.SignerCertificate.Subject -notmatch 'O=NVIDIA Corporation') {
+ throw "CUDA installer signature: $($signature.Status) $($signature.SignerCertificate.Subject)"
+ }
+ Write-Host "signed by $($signature.SignerCertificate.Subject)"
+ # Names from the subpackage table in NVIDIA's CUDA 13.0 Windows installation guide.
+ $packages = @(
+ 'nvcc', 'nvvm', 'crt', 'cudart', 'thrust', 'nvtx', 'cuda_profiler_api',
+ 'cublas', 'cublas_dev', 'cusparse', 'cusparse_dev', 'cusolver', 'cusolver_dev',
+ 'curand', 'curand_dev', 'cufft', 'cufft_dev', 'nvrtc', 'nvrtc_dev', 'nvjitlink',
+ 'cuobjdump', 'nvdisasm', 'nvprune'
+ ) | ForEach-Object { "${_}_13.0" }
+ # -s: silent, only the subpackages named; -n: never reboot.
+ $process = Start-Process -FilePath $installer -ArgumentList (@('-s') + $packages + @('-n')) -Wait -PassThru -NoNewWindow
+ if ($process.ExitCode -ne 0) { throw "CUDA installer exited $($process.ExitCode)" }
+ $root = 'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.0'
+ if (-not (Test-Path (Join-Path $root 'bin\nvcc.exe'))) { throw "no nvcc.exe under $root" }
+ New-Item -ItemType Junction -Path 'C:\cuda' -Target $root | Out-Null
+ 'C:\cuda\bin' | Out-File -FilePath $env:GITHUB_PATH -Append -Encoding utf8
+ 'CUDA_HOME=C:\cuda', 'CUDA_PATH=C:\cuda' | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
+ & 'C:\cuda\bin\nvcc.exe' --version
+
+ # CUDA 13.4 is the first toolkit with a Windows ARM64 nvcc and what NVIDIA's torch 2.14+cu134
+ # is built with. From the redist archives: the manifest is pinned by sha256 and lists one for
+ # every archive, so nothing unhashed is unpacked. Installed straight into C:\cuda.
+ - name: Install the CUDA 13.4 toolkit (Windows ARM64)
+ if: matrix.platform == 'win_arm64'
+ timeout-minutes: 25
+ shell: pwsh
+ run: |
+ $ErrorActionPreference = 'Stop'
+ python unsloth/.github/scripts/prebuilt_wheels_cuda_redist.py `
+ 'https://developer.download.nvidia.com/compute/cuda/redist/redistrib_13.4.1.json' `
+ '291178934b2139727407c76f697bb0dfbac4014753a8094d7c290b0f4124a302' `
+ 'windows-arm64' 'C:\cuda' `
+ cuda_nvcc libnvvm cuda_crt cuda_cudart cccl cuda_nvtx cuda_profiler_api `
+ libcublas libcusparse libcusolver libcurand libcufft cuda_nvrtc libnvjitlink `
+ cuda_cuobjdump cuda_nvdisasm cuda_nvprune
+ if ($LASTEXITCODE -ne 0) { throw "CUDA redist install exited $LASTEXITCODE" }
+ # The ARM64 archives put cudart.lib and friends in lib\arm64; torch's cpp_extension links
+ # from CUDA_HOME\lib\x64 on every Windows (measured: LNK1181 cannot open 'cudart.lib').
+ New-Item -ItemType Junction -Path 'C:\cuda\lib\x64' -Target 'C:\cuda\lib\arm64' | Out-Null
+ if (-not (Test-Path 'C:\cuda\lib\x64\cudart.lib')) { throw 'no cudart.lib under C:\cuda\lib\x64' }
+ 'C:\cuda\bin' | Out-File -FilePath $env:GITHUB_PATH -Append -Encoding utf8
+ 'CUDA_HOME=C:\cuda', 'CUDA_PATH=C:\cuda' | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
+ & 'C:\cuda\bin\nvcc.exe' --version
+
+ # vcvars64 exported to later steps (no setup action). DISTUTILS_USE_SDK=1 makes setuptools use
+ # it as-is and turns on flash-attn's MSVC flags.
+ - name: Activate the MSVC ${{ matrix.platform == 'win_arm64' && 'arm64' || 'x64' }} environment (Windows)
+ if: runner.os == 'Windows'
+ shell: pwsh
+ env:
+ MSVC_COMPONENT: ${{ matrix.platform == 'win_arm64' && 'Microsoft.VisualStudio.Component.VC.Tools.ARM64' || 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64' }}
+ VCVARS: ${{ matrix.platform == 'win_arm64' && 'vcvarsarm64.bat' || 'vcvars64.bat' }}
+ NVCC_APPEND_ARM64: ${{ matrix.platform == 'win_arm64' && ' -Xcompiler=/Zc:preprocessor' || '' }}
+ run: |
+ $ErrorActionPreference = 'Stop'
+ $vswhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe'
+ $vs = & $vswhere -latest -products * -requires $env:MSVC_COMPONENT -property installationPath
+ if (-not $vs) { throw "no Visual Studio with $env:MSVC_COMPONENT" }
+ $bat = Join-Path $vs "VC\Auxiliary\Build\$env:VCVARS"
+ $lines = cmd /c "`"$bat`" >nul 2>&1 && set"
+ if ($LASTEXITCODE -ne 0) { throw "$env:VCVARS exited $LASTEXITCODE" }
+ $before = @{}
+ Get-ChildItem env: | ForEach-Object { $before[$_.Name] = $_.Value }
+ $oldPath = $env:Path -split ';'
+ foreach ($line in $lines) {
+ if ($line -notmatch '^([^=]+)=(.*)$') { continue }
+ $name = $Matches[1]; $value = $Matches[2]
+ if ($name -ieq 'Path') {
+ # Only the entries vcvars added, through GITHUB_PATH, so C:\cuda\bin stays put.
+ foreach ($entry in ($value -split ';')) {
+ if ($entry -and $oldPath -notcontains $entry) {
+ $entry | Out-File -FilePath $env:GITHUB_PATH -Append -Encoding utf8
+ }
+ }
+ } elseif ($before[$name] -ne $value) {
+ "$name=$value" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
+ }
+ }
+ 'DISTUTILS_USE_SDK=1' | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
+ # flash-attn's softmax.h and mamba's selective-scan kernels use M_LOG2E, which MSVC's
+ # <cmath> only defines under _USE_MATH_DEFINES (measured: `identifier "M_LOG2E" is
+ # undefined` in both). nvcc appends this to every invocation, host preprocessing included.
+ 'NVCC_APPEND_FLAGS=-D_USE_MATH_DEFINES' + $env:NVCC_APPEND_ARM64 | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
+ # CUDA 13.4's CCCL stops with C1189 under MSVC's traditional preprocessor (measured on
+ # windows-11-arm); CL reaches the cl.exe torch's cpp_extension runs on the .cpp sources.
+ if ($env:NVCC_APPEND_ARM64) { 'CL=/Zc:preprocessor' | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8 }
+ Write-Host "Visual Studio at $vs"
+
+ # compiler_check=content: the default keys on nvcc.exe's mtime, which can differ per runner and
+ # would make every warm slice a miss in the build job.
+ - name: Install ccache (Windows)
+ if: runner.os == 'Windows'
+ timeout-minutes: 10
+ shell: pwsh
+ env:
+ WHEEL_PLATFORM: ${{ matrix.platform }}
+ run: |
+ $ErrorActionPreference = 'Stop'
+ $pinned = @{
+ 'ccache-4.14.1-windows-x86_64' = '6219f3865ca59aec41ee4b678df171d5d35855ecb2b6dbbbd20690b3a68af7b4'
+ 'ccache-4.14.1-windows-aarch64' = '28121851c8c2c8b76214cfc5e119d769fbc39e93d27862e4e3a556905ebd23b2'
+ }
+ $name = if ($env:WHEEL_PLATFORM -eq 'win_arm64') { 'ccache-4.14.1-windows-aarch64' } else { 'ccache-4.14.1-windows-x86_64' }
+ $zip = Join-Path $env:RUNNER_TEMP "$name.zip"
+ Invoke-WebRequest -Uri "https://github.com/ccache/ccache/releases/download/v4.14.1/$name.zip" -OutFile $zip -TimeoutSec 300
+ $hash = (Get-FileHash -Path $zip -Algorithm SHA256).Hash.ToLowerInvariant()
+ if ($hash -ne $pinned[$name]) { throw "ccache sha256 $hash" }
+ Expand-Archive -Path $zip -DestinationPath $env:RUNNER_TEMP -Force
+ New-Item -ItemType Directory -Force -Path 'C:\ccache' | Out-Null
+ Copy-Item -Path (Join-Path $env:RUNNER_TEMP "$name\ccache.exe") -Destination 'C:\ccache\ccache.exe'
+ 'C:\ccache' | Out-File -FilePath $env:GITHUB_PATH -Append -Encoding utf8
+ 'CCACHE_COMPILERCHECK=content' | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
+ New-Item -ItemType Directory -Force -Path $env:CCACHE_DIR | Out-Null
+ & 'C:\ccache\ccache.exe' --version | Select-Object -First 1
+
+ # The Linux swap file's counterpart: windows-2022 has a fixed 2.9 GB page file (18.9 GB commit
+ # limit on 16 GB RAM). NtCreatePagingFile, so no reboot.
+ - name: Add a page file (Windows)
+ if: runner.os == 'Windows'
+ uses: al-cheb/configure-pagefile-action@9b6da52fb72a3c6147c1aad2df22d8d905681adc # v1.5
+ with:
+ minimum-size: 16GB
+ maximum-size: 32GB
+ # windows-11-arm has no D: drive.
+ disk-root: ${{ matrix.platform == 'win_arm64' && 'C:' || 'D:' }}
+
+ - name: Report memory, page file and disk (Windows)
+ if: runner.os == 'Windows'
+ shell: pwsh
+ run: |
+ $os = Get-CimInstance Win32_OperatingSystem
+ $cs = Get-CimInstance Win32_ComputerSystem
+ '{0:N1} GB RAM, {1:N1} GB commit limit, system-managed page file: {2}' -f ($os.TotalVisibleMemorySize / 1MB), ($os.TotalVirtualMemorySize / 1MB), $cs.AutomaticManagedPagefile
+ Get-CimInstance Win32_PageFileUsage | Format-Table Name, AllocatedBaseSize, CurrentUsage -AutoSize
+ Get-PSDrive -PSProvider FileSystem | Format-Table Name, @{n = 'FreeGB'; e = { [math]::Round($_.Free / 1GB, 1) } }, @{n = 'UsedGB'; e = { [math]::Round($_.Used / 1GB, 1) } } -AutoSize
+
- name: Set up swap space
+ if: runner.os == 'Linux'
run: |
set -euo pipefail
sudo fallocate -l 10G /mnt/swapfile
@@ -397,13 +782,14 @@ jobs:
sudo swapon /mnt/swapfile
free -h
- - name: Install torch ${{ matrix.torch }}+cu130
+ - name: Install ${{ matrix.torch_spec }}
run: |
set -euo pipefail
python -m pip install --upgrade pip
python -m pip install 'setuptools>=77' wheel ninja packaging
- python -m pip install --no-cache-dir "torch==${{ matrix.torch }}" \
- --index-url https://download.pytorch.org/whl/cu130
+ # TORCH_INDEX_ARGS is an allowlisted constant from prebuilt_wheels.py; word splitting intended.
+ # shellcheck disable=SC2086
+ python -m pip install --no-cache-dir "$TORCH_SPEC" $TORCH_INDEX_ARGS
nvcc --version
python -c "import torch; print('torch', torch.__version__, 'cuda', torch.version.cuda, 'cxx11abi', torch._C._GLIBCXX_USE_CXX11_ABI)"
python - <<'PY'
@@ -411,24 +797,40 @@ jobs:
want_torch = os.environ["MATRIX_TORCH"]
if torch.__version__.split("+")[0] != want_torch:
sys.exit(f"installed torch {torch.__version__}, wanted {want_torch}")
+ if "+" in os.environ["TORCH_SPEC"] and torch.__version__ != os.environ["TORCH_SPEC"].split("==", 1)[1]:
+ sys.exit(f"installed torch {torch.__version__}, wanted {os.environ['TORCH_SPEC']}")
+ # An emulated x64 interpreter on an ARM runner would build an x64 extension.
+ import sysconfig
+ if sysconfig.get_platform() != os.environ["PY_PLATFORM"]:
+ sys.exit(f"interpreter platform {sysconfig.get_platform()}, wanted {os.environ['PY_PLATFORM']}")
if not torch.version.cuda.startswith("13."):
sys.exit(f"torch reports CUDA {torch.version.cuda}, wanted 13.x")
- # Verify the ABI matches the wheel's cxx11abiTRUE tag.
- if not torch._C._GLIBCXX_USE_CXX11_ABI:
- sys.exit("torch was built with _GLIBCXX_USE_CXX11_ABI=0; the cxx11abiTRUE tag would be wrong")
+ # Verify the ABI matches the wheel's cxx11abi tag, which every upstream setup.py copies
+ # from this attribute. It is True on the Windows cu130 wheels too, not just on Linux.
+ want = os.environ["MATRIX_ABI"]
+ if bool(torch._C._GLIBCXX_USE_CXX11_ABI) != (want == "TRUE"):
+ sys.exit(f"torch reports _GLIBCXX_USE_CXX11_ABI={torch._C._GLIBCXX_USE_CXX11_ABI}; the cxx11abi{want} tag would be wrong")
PY
env:
MATRIX_TORCH: ${{ matrix.torch }}
+ MATRIX_ABI: ${{ matrix.abi }}
+ TORCH_SPEC: ${{ matrix.torch_spec }}
+ TORCH_INDEX_ARGS: ${{ matrix.torch_index_args }}
+ PY_PLATFORM: ${{ matrix.py_platform }}
- name: Patch mamba-ssm for the torch 2.13 C++20 requirement
if: matrix.patch == 'cxx20'
- run: python3 unsloth/.github/scripts/patch_mamba_cxx20.py src/setup.py
+ run: python unsloth/.github/scripts/patch_mamba_cxx20.py src/setup.py
+
+ - name: Patch mamba-ssm for the MSVC host compiler (Windows)
+ if: runner.os == 'Windows' && matrix.package == 'mamba-ssm'
+ run: python unsloth/.github/scripts/patch_mamba_msvc.py src
- name: Download the warm slices
if: matrix.shards > 0
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
- pattern: ccache-${{ matrix.dist }}-torch${{ matrix.torch_mm }}-${{ matrix.python_tag }}-shard*
+ pattern: ccache-${{ matrix.platform }}-${{ matrix.dist }}-torch${{ matrix.torch_mm }}-${{ matrix.python_tag }}-shard*
path: ccache-slices
# The slices are disjoint, so copying them over one another loses nothing.
@@ -436,10 +838,13 @@ jobs:
if: matrix.shards > 0
run: |
set -euo pipefail
+ # Git Bash wants a POSIX path; CCACHE_DIR is a Windows one there.
+ dest="$CCACHE_DIR"
+ if [ "$RUNNER_OS" = "Windows" ]; then dest="$(cygpath -u "$CCACHE_DIR")"; fi
for slice in ccache-slices/*/; do
[ -d "$slice" ] || continue
echo "merging $slice ($(find "$slice" -type f | wc -l) files, $(du -sh "$slice" | cut -f1))"
- cp -a "$slice." "$CCACHE_DIR/"
+ cp -a "$slice." "$dest/"
done
rm -rf ccache-slices
ccache -z
@@ -452,6 +857,7 @@ jobs:
NVCC_THREADS: ${{ matrix.nvcc_threads }}
BUILD_ENV: ${{ matrix.build_env }}
BUILD_TIMEOUT: ${{ matrix.build_timeout }}
+ WHEEL_PLATFORM: ${{ matrix.platform }}
run: |
set -euo pipefail
# Each entry is NAME=VALUE from the allowlisted table in prebuilt_wheels.py, so this is
@@ -461,12 +867,28 @@ jobs:
echo "export $pair"
done
echo "MAX_JOBS=$MAX_JOBS NVCC_THREADS=$NVCC_THREADS timeout=$BUILD_TIMEOUT"
- timeout --signal=INT --kill-after=120 "$BUILD_TIMEOUT" \
- python setup.py bdist_wheel --dist-dir=dist
+ if [ "$RUNNER_OS" = "Windows" ]; then
+ # Git Bash puts /usr/bin first, and its coreutils `link.exe` (hard links) shadows MSVC's
+ # linker: setuptools found it on PATH and every object compiled, then the final link
+ # failed. MSVC's own bin goes back in front for this step.
+ host="Hostx64\\x64"
+ if [ "$WHEEL_PLATFORM" = "win_arm64" ]; then host="Hostarm64\\arm64"; fi
+ PATH="$(cygpath -u "${VCToolsInstallDir}bin\\${host}"):$PATH"
+ export PATH
+ link_exe="$(command -v link.exe)"
+ echo "linker: $link_exe"
+ case "$link_exe" in */usr/bin/*) echo "::error::GNU link.exe still first on PATH"; exit 1 ;; esac
+ python ../unsloth/.github/scripts/prebuilt_wheels_timeout.py "$BUILD_TIMEOUT" -- \
+ python setup.py bdist_wheel --dist-dir=dist
+ else
+ timeout --signal=INT --kill-after=120 "$BUILD_TIMEOUT" \
+ python setup.py bdist_wheel --dist-dir=dist
+ fi
- name: ccache stats
if: always()
- run: ccache -s
+ run: |
+ if [ "$RUNNER_OS" = "Windows" ]; then ccache -sv; else ccache -s; fi
# Upstream's own naming, reproduced rather than approximated: the local version segment is
# what stops pip installing a torch 2.13 wheel into a torch 2.14 environment. Asserting the
@@ -478,11 +900,13 @@ jobs:
DIST: ${{ matrix.dist }}
VERSION: ${{ matrix.version }}
PYTHON_TAG: ${{ matrix.python_tag }}
+ # Not PLATFORM: vcvars exports Platform=x64 and Windows env names are case-insensitive.
+ WHEEL_PLATFORM: ${{ matrix.platform }}
WHEEL_NAME: ${{ matrix.wheel_name }}
run: |
set -euo pipefail
built="$(ls dist/*.whl)"
- expected_raw="dist/${DIST}-${VERSION}-${PYTHON_TAG}-${PYTHON_TAG}-linux_x86_64.whl"
+ expected_raw="dist/${DIST}-${VERSION}-${PYTHON_TAG}-${PYTHON_TAG}-${WHEEL_PLATFORM}.whl"
if [ "$built" != "$expected_raw" ]; then
echo "::error::built $built, expected $expected_raw before renaming"
exit 1
@@ -504,12 +928,29 @@ jobs:
WHEEL_NAME: ${{ matrix.wheel_name }}
IMPORT_NAMES: ${{ matrix.import_names }}
MATRIX_TORCH: ${{ matrix.torch }}
+ SMOKE_EXCLUDE: ${{ matrix.smoke_exclude }}
+ SMOKE_EXTRA: ${{ matrix.smoke_extra }}
+ TORCH_SPEC: ${{ matrix.torch_spec }}
+ TORCH_INDEX_ARGS: ${{ matrix.torch_index_args }}
+ WHEEL_PLATFORM: ${{ matrix.platform }}
run: |
set -euo pipefail
- python -m venv "$RUNNER_TEMP/smoke"
- "$RUNNER_TEMP/smoke/bin/python" -m pip install --quiet --upgrade pip
- "$RUNNER_TEMP/smoke/bin/python" -m pip install --quiet --no-cache-dir \
- "torch==${MATRIX_TORCH}" --index-url https://download.pytorch.org/whl/cu130
+ smoke="$RUNNER_TEMP/smoke"
+ py="$smoke/bin/python"
+ if [ "$RUNNER_OS" = "Windows" ]; then
+ smoke="$(cygpath -u "$RUNNER_TEMP")/smoke"
+ py="$smoke/Scripts/python.exe"
+ # A user's machine has no toolkit, so neither does this check: the extension has to
+ # resolve every DLL from torch's own bundled CUDA runtime, not from C:\cuda\bin.
+ PATH="$(printf '%s' "$PATH" | tr ':' '\n' | grep -v -i -E '^/c/cuda(/|$)' | paste -sd: -)"
+ export PATH
+ unset CUDA_HOME CUDA_PATH
+ fi
+ python unsloth/.github/scripts/prebuilt_wheels_check_binaries.py "src/dist/${WHEEL_NAME}" "$WHEEL_PLATFORM"
+ python -m venv "$smoke"
+ "$py" -m pip install --quiet --upgrade pip
+ # shellcheck disable=SC2086
+ "$py" -m pip install --quiet --no-cache-dir "$TORCH_SPEC" $TORCH_INDEX_ARGS
# The wheel is installed with --no-deps so nothing can drag a second torch into the
# venv; every other runtime dependency the package declares is installed first, read
# from the wheel's own metadata rather than from a hand-kept list. The hand-kept list
@@ -519,18 +960,24 @@ jobs:
# to compile from source because no matching wheel exists on PyPI. The constraints
# file pins torch and triton to what is already in the venv, so a dependency that
# wants a different one fails the step loudly instead of swapping torch under us.
- "$RUNNER_TEMP/smoke/bin/python" -m pip freeze | grep -E '^(torch|triton)==' \
+ #
+ # On Windows the cell also names what to leave out and what to add instead
+ # (SMOKE_EXCLUDE / SMOKE_EXTRA): mamba-ssm's triton, tilelang, tvm-ffi and quack have
+ # no Windows wheels at those versions, and triton-windows, pinned to the release this
+ # torch pairs with, is what provides `import triton` there.
+ "$py" -m pip freeze | grep -E '^(torch|triton)==' \
> "$RUNNER_TEMP/smoke-constraints.txt"
# tvm-ffi 0.1.12 with tilelang 0.1.8 breaks mamba_ssm imports (import test only).
echo 'apache-tvm-ffi==0.1.11' >> "$RUNNER_TEMP/smoke-constraints.txt"
cat "$RUNNER_TEMP/smoke-constraints.txt"
- "$RUNNER_TEMP/smoke/bin/python" - "src/dist/${WHEEL_NAME}" "$RUNNER_TEMP/smoke-constraints.txt" <<'PY'
- import email.parser, pathlib, re, subprocess, sys, zipfile
+ "$py" - "src/dist/${WHEEL_NAME}" "$RUNNER_TEMP/smoke-constraints.txt" <<'PY'
+ import email.parser, os, pathlib, re, subprocess, sys, zipfile
wheel = pathlib.Path(sys.argv[1])
with zipfile.ZipFile(wheel) as zf:
meta = next(n for n in zf.namelist() if n.endswith(".dist-info/METADATA"))
headers = email.parser.Parser().parsestr(zf.read(meta).decode())
excluded = {"torch", "flash-attn", "flash_attn", "causal-conv1d", "causal_conv1d", "mamba-ssm", "mamba_ssm"}
+ excluded |= set(os.environ.get("SMOKE_EXCLUDE", "").lower().split())
deps = []
for req in headers.get_all("Requires-Dist") or []:
if ";" in req: # extras and environment markers: not runtime requirements
@@ -540,24 +987,25 @@ jobs:
continue
deps.append(req.strip())
deps += ["numpy"] # torch wants numpy for its tensor conversions and only warns without it
+ deps += os.environ.get("SMOKE_EXTRA", "").split()
print("runtime dependencies from METADATA:", deps)
subprocess.check_call([sys.executable, "-m", "pip", "install", "--quiet", "--constraint", sys.argv[2], *deps])
PY
- "$RUNNER_TEMP/smoke/bin/python" -m pip install --quiet --no-deps \
+ "$py" -m pip install --quiet --no-deps \
"src/dist/${WHEEL_NAME}"
for name in $IMPORT_NAMES; do
echo "importing $name"
- "$RUNNER_TEMP/smoke/bin/python" -c "
+ "$py" -c "
import importlib, sys, torch
module = importlib.import_module('$name')
print(' ok:', '$name', getattr(module, '__version__', ''), getattr(module, '__file__', ''))
"
done
- "$RUNNER_TEMP/smoke/bin/python" -m pip list | grep -Ei 'torch|flash|mamba|causal'
+ "$py" -m pip list | grep -Ei 'torch|flash|mamba|causal|triton'
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
- name: wheel-${{ matrix.dist }}-torch${{ matrix.torch_mm }}-${{ matrix.python_tag }}
+ name: wheel-${{ matrix.platform }}-${{ matrix.dist }}-torch${{ matrix.torch_mm }}-${{ matrix.python_tag }}
path: src/dist/${{ matrix.wheel_name }}
if-no-files-found: error
# Already a compressed zip. Recompressing 500 MB of it buys nothing and costs minutes.
@@ -571,8 +1019,8 @@ jobs:
gpu-smoke:
name: GPU forward pass
needs: [plan, build]
- # A skipped warm (no flash-attn) would otherwise skip every job downstream of build.
- if: ${{ !cancelled() && inputs.gpu_smoke && needs.build.result == 'success' }}
+ # Linux cells only (gpu_matrix); an empty matrix is an error, so skip when none were built.
+ if: ${{ !cancelled() && inputs.gpu_smoke && needs.build.result == 'success' && needs.plan.outputs.gpu_count != '0' }}
runs-on: [self-hosted, gpu]
timeout-minutes: 90
permissions:
@@ -580,7 +1028,7 @@ jobs:
strategy:
fail-fast: false
max-parallel: 1
- matrix: ${{ fromJSON(needs.plan.outputs.matrix) }}
+ matrix: ${{ fromJSON(needs.plan.outputs.gpu_matrix) }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
@@ -591,7 +1039,7 @@ jobs:
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
- name: wheel-${{ matrix.dist }}-torch${{ matrix.torch_mm }}-${{ matrix.python_tag }}
+ name: wheel-${{ matrix.platform }}-${{ matrix.dist }}-torch${{ matrix.torch_mm }}-${{ matrix.python_tag }}
path: dist
- name: Forward pass on the real card
@@ -751,9 +1199,8 @@ jobs:
name: prebuilt-cuda-wheels-signed
path: wheels
- # Created once and then added to. A run for torch 2.14 must not disturb the torch 2.13
- # wheels already on the tag, so: create if absent, upload with --clobber, and regenerate
- # the notes from what the release holds AFTERWARDS rather than from what this run built.
+ # Created once, then added to: a run for one torch minor or platform must not disturb the
+ # rest. This run's SHA256SUMS is the attestation subject only; the release's is rebuilt below.
#
# --latest=false on both paths. This tag is a wheelhouse, not a product release, and the
# repository's latest release is what the installers and the README resolve to.
@@ -780,13 +1227,11 @@ jobs:
gh release edit "$TAG" --repo "$GITHUB_REPOSITORY" --title "$TITLE" --latest=false --draft="$DRAFT"
fi
gh release upload "$TAG" --repo "$GITHUB_REPOSITORY" --clobber \
- wheels/*.whl wheels/*.sigstore.json wheels/SHA256SUMS
+ wheels/*.whl wheels/*.sigstore.json
- # Regenerated from the release's OWN assets, using the digest GitHub reports per asset
- # rather than the SHA256SUMS this run uploaded. Those agree when nothing went wrong, and
- # when they disagree the table should describe what a user will actually download. It also
- # makes the notes correct after a second run that only added half the matrix.
- - name: Regenerate the release notes from the published assets
+ # From the release's own assets (GitHub's per-asset digest, else download and hash), so a
+ # one-platform publish cannot drop the other platform's lines.
+ - name: Regenerate SHA256SUMS and the release notes from the published assets
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ inputs.release_tag }}
@@ -795,10 +1240,22 @@ jobs:
set -euo pipefail
# Listed rather than looked up by tag: a draft release has no tag object yet.
gh api --paginate "repos/$GITHUB_REPOSITORY/releases?per_page=100" \
- --jq '.[] | select(.tag_name == env.TAG) | .assets[] | select(.name | endswith(".whl")) | "\(.digest // "sha256:unknown" | sub("^sha256:"; "")) \(.name)"' \
- > published.txt
+ --jq '.[] | select(.tag_name == env.TAG) | .assets[] | select(.name | endswith(".whl")) | "\(.digest // "-" | sub("^sha256:"; "")) \(.id) \(.name)"' \
+ > assets.txt
+ : > published.txt
+ while read -r digest id name; do
+ if [ "$digest" = "-" ]; then
+ gh api -H "Accept: application/octet-stream" \
+ "repos/$GITHUB_REPOSITORY/releases/assets/$id" > asset.bin
+ digest="$(sha256sum asset.bin | cut -d' ' -f1)"
+ rm -f asset.bin
+ fi
+ printf '%s %s\n' "$digest" "$name" >> published.txt
+ done < assets.txt
sort -k2 -o published.txt published.txt
cat published.txt
+ python3 .github/scripts/prebuilt_wheels.py sums < published.txt > SHA256SUMS
+ gh release upload "$TAG" --repo "$GITHUB_REPOSITORY" --clobber SHA256SUMS
python3 .github/scripts/prebuilt_wheels.py notes \
--tag "$TAG" --repo "$GITHUB_REPOSITORY" < published.txt > notes.md
gh release edit "$TAG" --repo "$GITHUB_REPOSITORY" --notes-file notes.md --latest=false --draft="$DRAFT"
diff --git a/tests/python/test_prebuilt_cuda_wheels.py b/tests/python/test_prebuilt_cuda_wheels.py
index 0bfe10047db..3575c7efb4b 100644
--- a/tests/python/test_prebuilt_cuda_wheels.py
+++ b/tests/python/test_prebuilt_cuda_wheels.py
@@ -282,15 +282,19 @@ class TestBackwardsCompatible:
class TestMatrix:
def test_defaults_are_both_torch_minors_on_cp313(self):
include = prebuilt_wheels.build_matrix()
- assert len(include) == 6
+ # 3 packages x 2 torch minors, on each of the two platforms.
+ assert len(include) == 12
assert {cell["torch"] for cell in include} == {"2.13.0", "2.14.0"}
assert {cell["python_tag"] for cell in include} == {"cp313"}
assert {cell["package"] for cell in include} == set(prebuilt_wheels.SPECS)
+ assert {cell["platform"] for cell in include} == {"linux_x86_64", "win_amd64"}
+ for platform in ("linux_x86_64", "win_amd64"):
+ assert len([cell for cell in include if cell["platform"] == platform]) == 6
def test_extra_interpreters_are_separate_cells(self):
include = prebuilt_wheels.build_matrix(pythons = "3.11,3.12,3.13")
- assert len(include) == 18
- assert len({cell["wheel_name"] for cell in include}) == 18
+ assert len(include) == 36
+ assert len({cell["wheel_name"] for cell in include}) == 36
def test_one_torch_minor_at_a_time(self):
include = prebuilt_wheels.build_matrix(torches = "2.14.0")
@@ -623,8 +627,11 @@ class TestWorkflow:
def test_the_build_runs_on_the_older_ubuntu(self, workflow):
"""The wheels are tagged linux_x86_64, which pip installs without a glibc check, so the
- runner's glibc is the real compatibility floor."""
- assert workflow["jobs"]["build"]["runs-on"] == "ubuntu-22.04"
+ runner's glibc is the real compatibility floor. The runner now comes from the cell."""
+ assert workflow["jobs"]["build"]["runs-on"] == "${{ matrix.runner }}"
+ assert prebuilt_wheels.PLATFORMS["linux_x86_64"]["runner"] == "ubuntu-22.04"
+ for cell in prebuilt_wheels.build_matrix(platforms = "linux_x86_64"):
+ assert cell["runner"] == "ubuntu-22.04"
def test_the_gpu_job_uses_a_label_that_already_exists(self, workflow):
"""Never invent a runner label: a job with one queues until the run is cancelled."""
@@ -697,6 +704,7 @@ class TestWarmSlices:
"UW_PACKAGES": "flash-attn,mamba-ssm",
"UW_TORCH_VERSIONS": "2.13.0",
"UW_PYTHON_VERSIONS": "",
+ "UW_PLATFORMS": "linux_x86_64",
},
check = True,
capture_output = True,
@@ -709,13 +717,22 @@ class TestWarmSlices:
== str(prebuilt_wheels.SPECS["flash-attn"]["shards"])
)
assert values["count"] == "2"
+ assert values["gpu_count"] == "2"
+ assert (
+ json.loads(values["gpu_matrix"])["include"] == json.loads(values["matrix"])["include"]
+ )
class TestWarmWiring:
def test_warm_and_build_hash_the_same_compiles(self, workflow):
warm, build = workflow["jobs"]["warm"], workflow["jobs"]["build"]
assert warm["env"] == build["env"]
- assert warm["runs-on"] == build["runs-on"] == "ubuntu-22.04"
+ assert warm["runs-on"] == build["runs-on"] == "${{ matrix.runner }}"
+ assert (
+ warm["env"]["PYTORCH_NVCC"]
+ == build["env"]["PYTORCH_NVCC"]
+ == "${{ matrix.pytorch_nvcc }}"
+ )
def test_the_build_waits_for_warm_but_not_on_its_success(self, workflow):
warm, build = workflow["jobs"]["warm"], workflow["jobs"]["build"]
diff --git a/tests/python/test_prebuilt_cuda_wheels_arm.py b/tests/python/test_prebuilt_cuda_wheels_arm.py
new file mode 100644
index 00000000000..126d55fdfbd
--- /dev/null
+++ b/tests/python/test_prebuilt_cuda_wheels_arm.py
@@ -0,0 +1,369 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The ARM half of .github/workflows/prebuilt-cuda-wheels.yml.
+
+* linux_aarch64 builds all three packages for torch 2.13 / 2.14 (upstream's aarch64 wheels stop
+ at 2.10); win_arm64 builds causal-conv1d and mamba-ssm for NVIDIA's torch 2.14.0+cu134 only,
+ since NVIDIA publishes flash-attn for it and no other CUDA torch exists there;
+* ARM is opt-in: the default dispatch plans exactly the x86_64 cells it planned before;
+* the x86_64 GPU runner never gets an aarch64 wheel;
+* the CUDA 13.4 redist installer checks a pinned manifest and every archive against it;
+* the binary check catches an x64 extension under an arm64 name and a glibc above the floor.
+"""
+
+from __future__ import annotations
+
+import hashlib
+import importlib.util
+import io
+import json
+import struct
+import zipfile
+from pathlib import Path
+
+import pytest
+import yaml
+
+REPO = Path(__file__).resolve().parents[2]
+WORKFLOW = REPO / ".github" / "workflows" / "prebuilt-cuda-wheels.yml"
+SCRIPTS = REPO / ".github" / "scripts"
+
+
+def _load(name: str):
+ spec = importlib.util.spec_from_file_location(name, SCRIPTS / f"{name}.py")
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ return module
+
+
+pw = _load("prebuilt_wheels")
+redist = _load("prebuilt_wheels_cuda_redist")
+binaries = _load("prebuilt_wheels_check_binaries")
+
+
[email protected](scope = "module")
+def workflow():
+ return yaml.safe_load(WORKFLOW.read_text(encoding = "utf-8"))
+
+
+class TestMatrix:
+ def test_the_default_dispatch_plans_no_arm_cell(self):
+ cells = pw.build_matrix()
+ assert {cell["platform"] for cell in cells} == {"linux_x86_64", "win_amd64"}
+ assert len(cells) == 12
+
+ def test_linux_aarch64_builds_every_package_for_both_torches(self):
+ cells = pw.build_matrix(platforms = "linux_aarch64")
+ assert sorted(cell["wheel_name"] for cell in cells) == sorted(
+ [
+ "flash_attn-2.8.4+cu13torch2.13cxx11abiTRUE-cp313-cp313-linux_aarch64.whl",
+ "flash_attn-2.8.4+cu13torch2.14cxx11abiTRUE-cp313-cp313-linux_aarch64.whl",
+ "causal_conv1d-1.7.0+cu13torch2.13cxx11abiTRUE-cp313-cp313-linux_aarch64.whl",
+ "causal_conv1d-1.7.0+cu13torch2.14cxx11abiTRUE-cp313-cp313-linux_aarch64.whl",
+ "mamba_ssm-2.3.2.post1+cu13torch2.13cxx11abiTRUE-cp313-cp313-linux_aarch64.whl",
+ "mamba_ssm-2.3.2.post1+cu13torch2.14cxx11abiTRUE-cp313-cp313-linux_aarch64.whl",
+ ]
+ )
+ for cell in cells:
+ assert cell["runner"] == "ubuntu-22.04-arm"
+ assert cell["torch_spec"] == f"torch=={cell['torch']}"
+ assert cell["torch_index_args"] == "--index-url https://download.pytorch.org/whl/cu130"
+ assert cell["py_platform"] == "linux-aarch64"
+ assert cell["smoke_exclude"] == "" and cell["smoke_extra"] == ""
+ assert len(pw.warm_matrix(cells)) == 16
+
+ def test_win_arm64_builds_two_packages_for_nvidias_torch_only(self):
+ cells = pw.build_matrix(platforms = "win_arm64")
+ assert sorted(cell["wheel_name"] for cell in cells) == [
+ "causal_conv1d-1.7.0+cu13torch2.14cxx11abiTRUE-cp313-cp313-win_arm64.whl",
+ "mamba_ssm-2.3.2.post1+cu13torch2.14cxx11abiTRUE-cp313-cp313-win_arm64.whl",
+ ]
+ for cell in cells:
+ assert cell["runner"] == "windows-11-arm"
+ assert cell["torch_spec"] == "torch==2.14.0+cu134"
+ assert (
+ cell["torch_index_args"] == "--extra-index-url https://pypi.nvidia.com/nvtorch_oot"
+ )
+ assert cell["py_platform"] == "win-arm64"
+ assert cell["shards"] == 0
+ mamba = next(cell for cell in cells if cell["package"] == "mamba-ssm")
+ # triton-windows 3.8.0.post29 publishes win_arm64 wheels, the release torch 2.14 pairs with.
+ assert mamba["smoke_extra"] == "triton-windows==3.8.0.post29"
+ assert "triton" in mamba["smoke_exclude"].split()
+
+ def test_win_arm64_adds_interpreters_on_request(self):
+ cells = pw.build_matrix(platforms = "win_arm64", pythons = "3.11,3.12,3.13")
+ assert sorted({cell["python_tag"] for cell in cells}) == ["cp311", "cp312", "cp313"]
+ assert len(cells) == 6
+
+ @pytest.mark.parametrize(
+ "request_",
+ [
+ {"platforms": "win_arm64", "torches": "2.13.0"},
+ {"platforms": "win_arm64", "packages": "flash-attn"},
+ ],
+ )
+ def test_a_request_with_no_buildable_cell_fails(self, request_):
+ with pytest.raises(SystemExit, match = "no cells"):
+ pw.build_matrix(**request_)
+
+ def test_a_mixed_request_keeps_only_the_buildable_cells(self):
+ cells = pw.build_matrix(platforms = "linux_x86_64,win_arm64", packages = "flash-attn")
+ assert {cell["platform"] for cell in cells} == {"linux_x86_64"}
+
+ def test_x86_64_cells_are_unchanged_by_arm(self):
+ before = [cell for cell in pw.build_matrix() if cell["platform"] == "linux_x86_64"]
+ with_arm = [
+ cell
+ for cell in pw.build_matrix(platforms = "linux_x86_64,linux_aarch64,win_arm64")
+ if cell["platform"] == "linux_x86_64"
+ ]
+ assert before == with_arm
+ for cell in before:
+ assert cell["torch_spec"] == f"torch=={cell['torch']}"
+ assert cell["torch_index_args"] == "--index-url https://download.pytorch.org/whl/cu130"
+
+ def test_the_gpu_runner_only_gets_x86_64_linux_wheels(self):
+ cells = pw.build_matrix(platforms = "linux_x86_64,linux_aarch64,win_amd64,win_arm64")
+ assert {cell["platform"] for cell in pw.gpu_matrix(cells)} == {"linux_x86_64"}
+
+ def test_arm_wheel_names_parse_and_render(self):
+ names = [
+ cell["wheel_name"] for cell in pw.build_matrix(platforms = "linux_aarch64,win_arm64")
+ ]
+ for name in names:
+ row = pw.parse_wheel_name(name)
+ assert row is not None and row["platform"] in ("linux_aarch64", "win_arm64")
+ notes = pw.render_notes([("0" * 64, name) for name in names], tag = "t", repo = "o/r")
+ assert (
+ "Linux aarch64: flash-attn 2.8.4, causal-conv1d 1.7.0 and mamba-ssm 2.3.2.post1"
+ in notes
+ )
+ assert (
+ "Windows ARM64: causal-conv1d 1.7.0 and mamba-ssm 2.3.2.post1, built for PyTorch 2.14"
+ in notes
+ )
+
+
+def _manifest_and_archives(tmp_path, components):
+ manifest = {"release_label": "13.4.1", "release_date": "2026-09-09"}
+ for name in components:
+ top = f"{name}-windows-arm64-1.0-archive"
+ buffer = io.BytesIO()
+ with zipfile.ZipFile(buffer, "w") as zf:
+ zf.writestr(f"{top}/", "")
+ zf.writestr(f"{top}/LICENSE", "license")
+ zf.writestr(f"{top}/bin/{name}.exe", name)
+ zf.writestr(f"{top}/include/{name}.h", name)
+ data = buffer.getvalue()
+ relative = f"{name}/windows-arm64/{top}.zip"
+ (tmp_path / "srv" / Path(relative).parent).mkdir(parents = True, exist_ok = True)
+ (tmp_path / "srv" / relative).write_bytes(data)
+ manifest[name] = {
+ "windows-arm64": {"relative_path": relative, "sha256": hashlib.sha256(data).hexdigest()}
+ }
+ raw = json.dumps(manifest).encode()
+ (tmp_path / "srv" / "manifest.json").write_bytes(raw)
+ return hashlib.sha256(raw).hexdigest()
+
+
+def local_redist(tmp_path, monkeypatch):
+ def fetch(url, dest):
+ name = url.rsplit("/", 1)[1] if url.endswith(".json") else url[len(redist.REDIST_BASE) :]
+ dest.write_bytes((tmp_path / "srv" / name).read_bytes())
+
+ monkeypatch.setattr(redist, "_fetch", fetch)
+ return tmp_path
+
+
+class TestCudaRedist:
+ def test_installs_every_component_into_one_tree(self, local_redist):
+ sha = _manifest_and_archives(local_redist, ["cuda_nvcc", "cccl"])
+ dest = local_redist / "cuda"
+ assert (
+ redist.main(
+ ["https://x/manifest.json", sha, "windows-arm64", str(dest), "cuda_nvcc", "cccl"]
+ )
+ == 0
+ )
+ assert sorted(p.relative_to(dest).as_posix() for p in dest.rglob("*") if p.is_file()) == [
+ "bin/cccl.exe",
+ "bin/cuda_nvcc.exe",
+ "include/cccl.h",
+ "include/cuda_nvcc.h",
+ ]
+
+ def test_a_manifest_that_is_not_the_pinned_one_is_refused(self, local_redist):
+ _manifest_and_archives(local_redist, ["cuda_nvcc"])
+ with pytest.raises(SystemExit, match = "manifest sha256"):
+ redist.main(
+ [
+ "https://x/manifest.json",
+ "0" * 64,
+ "windows-arm64",
+ str(local_redist / "c"),
+ "cuda_nvcc",
+ ]
+ )
+
+ def test_an_archive_that_does_not_match_the_manifest_is_refused(self, local_redist):
+ sha = _manifest_and_archives(local_redist, ["cuda_nvcc"])
+ archive = next((local_redist / "srv" / "cuda_nvcc").rglob("*.zip"))
+ archive.write_bytes(archive.read_bytes() + b"tampered")
+ with pytest.raises(SystemExit, match = "manifest says"):
+ redist.main(
+ [
+ "https://x/manifest.json",
+ sha,
+ "windows-arm64",
+ str(local_redist / "c"),
+ "cuda_nvcc",
+ ]
+ )
+
+ def test_a_component_missing_for_the_platform_is_refused(self):
+ with pytest.raises(SystemExit, match = "not in the manifest"):
+ redist.plan({"cuda_nvcc": {"linux-sbsa": {}}}, "windows-arm64", ["cuda_nvcc"])
+
+ def test_an_archive_escaping_dest_is_refused(self, tmp_path):
+ archive = tmp_path / "evil.zip"
+ with zipfile.ZipFile(archive, "w") as zf:
+ zf.writestr("top/../../escape.txt", "x")
+ with pytest.raises(SystemExit, match = "escapes"):
+ redist.merge(archive, tmp_path / "dest")
+
+ def test_the_workflow_pins_the_manifest_it_was_written_against(self, workflow):
+ for job in ("warm", "build"):
+ step = next(
+ s
+ for s in workflow["jobs"][job]["steps"]
+ if s.get("name") == "Install the CUDA 13.4 toolkit (Windows ARM64)"
+ )
+ assert "redistrib_13.4.1.json" in step["run"]
+ assert "291178934b2139727407c76f697bb0dfbac4014753a8094d7c290b0f4124a302" in step["run"]
+ for component in (
+ "cuda_nvcc",
+ "libnvvm",
+ "cuda_crt",
+ "cuda_cudart",
+ "cccl",
+ "libcublas",
+ ):
+ assert component in step["run"].split(), component
+ # torch's cpp_extension links from lib\x64 on every Windows.
+ assert (
+ "New-Item -ItemType Junction -Path 'C:\\cuda\\lib\\x64' -Target 'C:\\cuda\\lib\\arm64'"
+ in step["run"]
+ )
+
+
+def _elf(machine: int, glibc: str = "2.34") -> bytes:
+ header = bytearray(64)
+ header[:4] = b"\x7fELF"
+ header[4] = 2
+ header[5] = 1
+ struct.pack_into("<H", header, 18, machine)
+ return bytes(header) + f"\x00GLIBC_{glibc}\x00GLIBC_2.17\x00".encode()
+
+
+def _pe(machine: int) -> bytes:
+ data = bytearray(0x100)
+ data[:2] = b"MZ"
+ struct.pack_into("<I", data, 0x3C, 0x80)
+ data[0x80:0x84] = b"PE\x00\x00"
+ struct.pack_into("<H", data, 0x84, machine)
+ return bytes(data)
+
+
+def _wheel(tmp_path, files: dict[str, bytes]) -> str:
+ path = tmp_path / "x.whl"
+ with zipfile.ZipFile(path, "w") as zf:
+ for name, data in files.items():
+ zf.writestr(name, data)
+ return str(path)
+
+
+class TestBinaryCheck:
+ def test_matching_arm_extensions_pass(self, tmp_path):
+ assert (
+ binaries.check(
+ _wheel(tmp_path, {"m/a.cpython-313-aarch64-linux-gnu.so": _elf(183)}),
+ "linux_aarch64",
+ )
+ == []
+ )
+ assert (
+ binaries.check(_wheel(tmp_path, {"m/a.cp313-win_arm64.pyd": _pe(0xAA64)}), "win_arm64")
+ == []
+ )
+
+ def test_an_x64_extension_under_an_arm_name_fails(self, tmp_path):
+ assert binaries.check(_wheel(tmp_path, {"m/a.so": _elf(62)}), "linux_aarch64")
+ assert binaries.check(_wheel(tmp_path, {"m/a.pyd": _pe(0x8664)}), "win_arm64")
+
+ def test_a_glibc_above_the_floor_fails(self, tmp_path):
+ problems = binaries.check(_wheel(tmp_path, {"m/a.so": _elf(183, "2.38")}), "linux_aarch64")
+ assert problems and "GLIBC 2.38" in problems[0]
+
+ def test_a_wheel_without_an_extension_fails(self, tmp_path):
+ assert binaries.check(_wheel(tmp_path, {"m/__init__.py": b""}), "win_arm64")
+
+
+class TestWorkflow:
+ def test_linux_cuda_comes_from_the_sbsa_repo_on_aarch64(self, workflow):
+ for job in ("warm", "build"):
+ step = next(
+ s
+ for s in workflow["jobs"][job]["steps"]
+ if s.get("name") == "Install the CUDA ${{ matrix.cuda_tag }} toolkit"
+ )
+ assert (
+ step["env"]["CUDA_REPO_ARCH"]
+ == "${{ matrix.platform == 'linux_aarch64' && 'sbsa' || 'x86_64' }}"
+ )
+ assert "ubuntu2204/$CUDA_REPO_ARCH/" in step["run"]
+
+ def test_both_windows_ccache_builds_are_pinned(self, workflow):
+ for job in ("warm", "build"):
+ run = next(
+ s
+ for s in workflow["jobs"][job]["steps"]
+ if s.get("name") == "Install ccache (Windows)"
+ )["run"]
+ assert "6219f3865ca59aec41ee4b678df171d5d35855ecb2b6dbbbd20690b3a68af7b4" in run
+ assert "28121851c8c2c8b76214cfc5e119d769fbc39e93d27862e4e3a556905ebd23b2" in run
+
+ def test_torch_comes_from_the_cell_and_the_interpreter_is_checked(self, workflow):
+ for job in ("warm", "build"):
+ step = next(
+ s
+ for s in workflow["jobs"][job]["steps"]
+ if s.get("name") == "Install ${{ matrix.torch_spec }}"
+ )
+ assert '"$TORCH_SPEC" $TORCH_INDEX_ARGS' in step["run"]
+ assert 'os.environ["PY_PLATFORM"]' in step["run"]
+ assert step["env"]["PY_PLATFORM"] == "${{ matrix.py_platform }}"
+
+ def test_the_smoke_test_checks_the_binaries_first(self, workflow):
+ run = next(
+ s
+ for s in workflow["jobs"]["build"]["steps"]
+ if s.get("name") == "Smoke test the wheel in a fresh venv"
+ )["run"]
+ assert run.index("prebuilt_wheels_check_binaries.py") < run.index('python -m venv "$smoke"')
+ assert '"$TORCH_SPEC" $TORCH_INDEX_ARGS' in run
+
+ def test_only_win_arm64_switches_msvc_to_the_conforming_preprocessor(self, workflow):
+ for job in ("warm", "build"):
+ step = next(
+ s
+ for s in workflow["jobs"][job]["steps"]
+ if s.get("name", "").startswith("Activate the MSVC")
+ )
+ assert step["env"]["NVCC_APPEND_ARM64"] == (
+ "${{ matrix.platform == 'win_arm64' && ' -Xcompiler=/Zc:preprocessor' || '' }}"
+ )
+ assert "'CL=/Zc:preprocessor'" in step["run"]
+ assert "'NVCC_APPEND_FLAGS=-D_USE_MATH_DEFINES' + $env:NVCC_APPEND_ARM64" in step["run"]
diff --git a/tests/python/test_prebuilt_cuda_wheels_windows.py b/tests/python/test_prebuilt_cuda_wheels_windows.py
new file mode 100644
index 00000000000..1d92dc76b1a
--- /dev/null
+++ b/tests/python/test_prebuilt_cuda_wheels_windows.py
@@ -0,0 +1,699 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The win_amd64 half of .github/workflows/prebuilt-cuda-wheels.yml.
+
+Nobody publishes flash-attn, causal-conv1d or mamba-ssm for Windows: not one Windows asset exists
+across every Dao-AILab and state-spaces release. These pin the parts of the Windows legs that can
+be checked without a Windows runner:
+
+* the Linux cells are byte-for-byte what they were, only gaining keys;
+* Windows names follow each upstream setup.py's own naming (`cxx11abiTRUE`, as torch reports, and `win_amd64`);
+* the shard selector understands Windows ninja targets, and can never run a targetless ninja;
+* the deadline wrapper ends the whole process tree;
+* SHA256SUMS is rebuilt over every wheel on the release, so a one-platform publish keeps the other;
+* the workflow's Windows steps exist in warm and build alike, are gated on the OS, and keep the
+ supply-chain checks (Authenticode on the CUDA installer, a pinned sha256 on ccache).
+"""
+
+from __future__ import annotations
+
+import importlib.util
+import json
+import subprocess
+import sys
+import time
+import types
+from pathlib import Path
+
+import pytest
+import yaml
+
+REPO = Path(__file__).resolve().parents[2]
+WORKFLOW = REPO / ".github" / "workflows" / "prebuilt-cuda-wheels.yml"
+SCRIPTS = REPO / ".github" / "scripts"
+
+
+def _load(name: str, path: Path):
+ spec = importlib.util.spec_from_file_location(name, path)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ return module
+
+
+prebuilt_wheels = _load("prebuilt_wheels_win", SCRIPTS / "prebuilt_wheels.py")
+shard = _load("prebuilt_wheels_shard_win", SCRIPTS / "prebuilt_wheels_shard.py")
+deadline = _load("prebuilt_wheels_timeout_win", SCRIPTS / "prebuilt_wheels_timeout.py")
+
+
+def triggers(doc: dict) -> dict:
+ return doc.get(True) if True in doc else doc.get("on")
+
+
[email protected](scope = "module")
+def workflow():
+ return yaml.safe_load(WORKFLOW.read_text(encoding = "utf-8"))
+
+
+def _steps(workflow, job):
+ return workflow["jobs"][job]["steps"]
+
+
+def _step(workflow, job, name):
+ return next(s for s in _steps(workflow, job) if s.get("name") == name)
+
+
+# Linux cells as published before Windows existed; changing them changes released wheels.
+LINUX_BEFORE = {
+ "flash-attn": {
+ "build_env": "FLASH_ATTENTION_FORCE_BUILD=TRUE FLASH_ATTENTION_FORCE_CXX11_ABI=TRUE "
+ "FLASH_ATTN_CUDA_ARCHS=80;90;100;120",
+ "abi": "TRUE",
+ "shards": 8,
+ "label": "flash-attn / torch 2.13 / cp313",
+ "wheel_name": "flash_attn-2.8.4+cu13torch2.13cxx11abiTRUE-cp313-cp313-linux_x86_64.whl",
+ },
+ "causal-conv1d": {
+ "build_env": "CAUSAL_CONV1D_FORCE_BUILD=TRUE",
+ "abi": "TRUE",
+ "shards": 0,
+ "label": "causal-conv1d / torch 2.13 / cp313",
+ "wheel_name": "causal_conv1d-1.7.0+cu13torch2.13cxx11abiTRUE-cp313-cp313-linux_x86_64.whl",
+ },
+ "mamba-ssm": {
+ "build_env": "MAMBA_FORCE_BUILD=TRUE MAMBA_KEEP_CUDA_BUILD=TRUE",
+ "abi": "TRUE",
+ "shards": 0,
+ "label": "mamba-ssm / torch 2.13 / cp313",
+ "wheel_name": "mamba_ssm-2.3.2.post1+cu13torch2.13cxx11abiTRUE-cp313-cp313-linux_x86_64.whl",
+ },
+}
+
+
+class TestMatrix:
+ def test_linux_cells_are_unchanged(self):
+ cells = prebuilt_wheels.build_matrix(torches = "2.13.0", platforms = "linux_x86_64")
+ assert [c["package"] for c in cells] == list(prebuilt_wheels.SPECS)
+ for cell in cells:
+ for key, value in LINUX_BEFORE[cell["package"]].items():
+ assert cell[key] == value, (cell["package"], key)
+ assert cell["runner"] == "ubuntu-22.04"
+ assert cell["pytorch_nvcc"] == "ccache /usr/local/cuda-13.0/bin/nvcc"
+ assert cell["smoke_exclude"] == cell["smoke_extra"] == ""
+
+ def test_linux_comes_first_so_its_cells_keep_their_order(self):
+ cells = prebuilt_wheels.build_matrix()
+ platforms = [c["platform"] for c in cells]
+ assert platforms == sorted(platforms, key = list(prebuilt_wheels.PLATFORMS).index)
+
+ def test_windows_cells(self):
+ cells = prebuilt_wheels.build_matrix(platforms = "win_amd64")
+ assert len(cells) == 6
+ for cell in cells:
+ assert cell["os"] == "windows"
+ assert cell["runner"] == "windows-2022"
+ assert cell["abi"] == "TRUE"
+ assert cell["wheel_name"].endswith("cxx11abiTRUE-cp313-cp313-win_amd64.whl")
+ assert cell["label"].endswith(" / win_amd64")
+ assert cell["pytorch_nvcc"] == "C:/ccache/ccache.exe C:/cuda/bin/nvcc.exe"
+
+ def test_windows_flash_attn_names_the_abi_torch_reports(self):
+ """The Windows cu130 torch reports _GLIBCXX_USE_CXX11_ABI=True (measured on windows-2022),
+ and FORCE_CXX11_ABI follows the platform's `abi`, so the name and the assert agree."""
+ (cell,) = prebuilt_wheels.build_matrix(
+ packages = "flash-attn", torches = "2.14.0", platforms = "win_amd64"
+ )
+ assert "FLASH_ATTENTION_FORCE_CXX11_ABI=TRUE" in cell["build_env"].split()
+ assert "FLASH_ATTN_CUDA_ARCHS=80;90;100;120" in cell["build_env"].split()
+ assert cell["shards"] == 8
+
+ def test_only_windows_mamba_swaps_its_smoke_dependencies(self):
+ for cell in prebuilt_wheels.build_matrix():
+ if cell["os"] == "windows" and cell["package"] == "mamba-ssm":
+ pin = prebuilt_wheels.TRITON_WINDOWS[cell["torch_mm"]]
+ assert cell["smoke_extra"] == f"triton-windows=={pin}"
+ assert set(cell["smoke_exclude"].split()) == {
+ "triton",
+ "tilelang",
+ "apache-tvm-ffi",
+ "quack-kernels",
+ }
+ else:
+ assert cell["smoke_extra"] == cell["smoke_exclude"] == ""
+
+ def test_every_torch_minor_has_a_triton_windows_pin(self):
+ """torch 2.13 pins triton 3.7.1 and 2.14 pins triton 3.8.x; triton-windows mirrors those."""
+ minors = {prebuilt_wheels.torch_minor(v) for v in prebuilt_wheels.TORCH_VERSIONS}
+ assert set(prebuilt_wheels.TRITON_WINDOWS) == minors
+ assert prebuilt_wheels.TRITON_WINDOWS["2.13"].startswith("3.7.1.post")
+ assert prebuilt_wheels.TRITON_WINDOWS["2.14"].startswith("3.8.0.post")
+
+ def test_pytorch_nvcc_paths_have_no_spaces(self):
+ """torch writes PYTORCH_NVCC into build.ninja verbatim; a space would split a path."""
+ for target in prebuilt_wheels.PLATFORMS.values():
+ words = target["pytorch_nvcc"].split(" ")
+ assert len(words) == 2 and all(words), target
+
+ @pytest.mark.parametrize(
+ "platforms", ["win_arm64ec", "macosx_arm64", "win_amd64; whoami", "$(id)"]
+ )
+ def test_unknown_platforms_are_refused(self, platforms):
+ with pytest.raises(SystemExit):
+ prebuilt_wheels.build_matrix(platforms = platforms)
+
+ def test_the_gpu_runner_only_gets_linux_cells(self):
+ cells = prebuilt_wheels.build_matrix()
+ gpu = prebuilt_wheels.gpu_matrix(cells)
+ assert gpu and all(c["os"] == "linux" for c in gpu)
+ assert len(gpu) == len([c for c in cells if c["platform"] == "linux_x86_64"])
+ assert prebuilt_wheels.gpu_matrix(prebuilt_wheels.build_matrix(platforms = "win_amd64")) == []
+
+ def test_a_windows_only_plan_writes_an_empty_gpu_matrix(self, tmp_path):
+ output = tmp_path / "output"
+ subprocess.run(
+ [sys.executable, str(SCRIPTS / "prebuilt_wheels.py"), "matrix", "--github"],
+ env = {"GITHUB_OUTPUT": str(output), "UW_PLATFORMS": "win_amd64"},
+ check = True,
+ capture_output = True,
+ )
+ values = dict(line.split("=", 1) for line in output.read_text().splitlines())
+ assert values["count"] == "6"
+ assert values["gpu_count"] == "0"
+ assert values["warm_count"] == "16"
+ warm = json.loads(values["warm_matrix"])["include"]
+ assert {w["platform"] for w in warm} == {"win_amd64"}
+
+
+class TestNames:
+ def test_windows_names_are_what_upstream_setup_py_would_write(self):
+ """flash-attn, causal-conv1d and mamba's get_wheel_url() all format
+ `+cu{cuda}torch{mm}cxx11abi{torch._C._GLIBCXX_USE_CXX11_ABI}` and `win_amd64`."""
+ assert (
+ prebuilt_wheels.wheel_name("flash-attn", "2.13.0", "3.13", "win_amd64")
+ == "flash_attn-2.8.4+cu13torch2.13cxx11abiTRUE-cp313-cp313-win_amd64.whl"
+ )
+ assert (
+ prebuilt_wheels.wheel_name("causal-conv1d", "2.14.0", "3.13", "win_amd64")
+ == "causal_conv1d-1.7.0+cu13torch2.14cxx11abiTRUE-cp313-cp313-win_amd64.whl"
+ )
+ assert (
+ prebuilt_wheels.wheel_name("mamba-ssm", "2.14.0", "3.12", "win_amd64")
+ == "mamba_ssm-2.3.2.post1+cu13torch2.14cxx11abiTRUE-cp312-cp312-win_amd64.whl"
+ )
+
+ def test_the_default_platform_is_still_linux(self):
+ assert prebuilt_wheels.wheel_name("flash-attn", "2.13.0", "3.13") == (
+ prebuilt_wheels.wheel_name("flash-attn", "2.13.0", "3.13", "linux_x86_64")
+ )
+
+ def test_wheel_name_cli_takes_a_platform(self):
+ out = subprocess.run(
+ [
+ sys.executable,
+ str(SCRIPTS / "prebuilt_wheels.py"),
+ "wheel-name",
+ "--package",
+ "causal-conv1d",
+ "--torch",
+ "2.13.0",
+ "--python",
+ "3.13",
+ "--platform",
+ "win_amd64",
+ ],
+ check = True,
+ capture_output = True,
+ text = True,
+ ).stdout.strip()
+ assert out == "causal_conv1d-1.7.0+cu13torch2.13cxx11abiTRUE-cp313-cp313-win_amd64.whl"
+
+ def test_round_trip_on_both_platforms(self):
+ for platform in prebuilt_wheels.PLATFORMS:
+ for package in prebuilt_wheels.SPECS:
+ name = prebuilt_wheels.wheel_name(package, "2.14.0", "3.13", platform)
+ parsed = prebuilt_wheels.parse_wheel_name(name)
+ assert parsed["package"] == package
+ assert parsed["platform"] == platform
+ assert parsed["abi"] == prebuilt_wheels.PLATFORMS[platform]["abi"]
+ assert parsed["version"] == prebuilt_wheels.SPECS[package]["version"]
+
+ @pytest.mark.parametrize(
+ "name",
+ [
+ "flash_attn-2.8.4+cu13torch2.13cxx11abiFALSE-cp313-cp313-win_amd64.whl",
+ "flash_attn-2.8.4+cu13torch2.13cxx11abiFALSE-cp313-cp313-linux_x86_64.whl",
+ "flash_attn-2.8.4+cu13torch2.14-cp313-cp313-win_arm64.whl",
+ ],
+ )
+ def test_an_abi_the_platform_does_not_have_is_not_ours(self, name):
+ assert prebuilt_wheels.parse_wheel_name(name) is None
+
+ def test_the_version_comes_from_the_file(self):
+ parsed = prebuilt_wheels.parse_wheel_name(
+ "flash_attn-9.9.9+cu13torch2.13cxx11abiTRUE-cp313-cp313-linux_x86_64.whl"
+ )
+ assert parsed["version"] == "9.9.9"
+
+
+def _entries(platforms, torches = ("2.13.0", "2.14.0")):
+ return [
+ (f"{index:064x}", prebuilt_wheels.wheel_name(package, torch, "3.13", platform))
+ for index, (platform, torch, package) in enumerate(
+ (platform, torch, package)
+ for platform in platforms
+ for torch in torches
+ for package in prebuilt_wheels.SPECS
+ )
+ ]
+
+
+class TestNotes:
+ def test_both_platforms(self):
+ notes = prebuilt_wheels.render_notes(
+ _entries(["linux_x86_64", "win_amd64"]), tag = "t", repo = "o/r"
+ )
+ assert notes == (
+ "Prebuilt CUDA 13 wheels. Linux x86_64: flash-attn 2.8.4, causal-conv1d 1.7.0 and "
+ "mamba-ssm 2.3.2.post1, built for PyTorch 2.13 and 2.14 on Python 3.13. "
+ "Windows x86_64: flash-attn 2.8.4, causal-conv1d 1.7.0 and mamba-ssm 2.3.2.post1, "
+ "built for PyTorch 2.13 and 2.14 on Python 3.13.\n"
+ )
+
+ def test_platforms_with_different_coverage_are_not_merged(self):
+ entries = _entries(["linux_x86_64"]) + _entries(["win_amd64"], torches = ("2.14.0",))
+ notes = prebuilt_wheels.render_notes(entries, tag = "t", repo = "o/r")
+ assert "built for PyTorch 2.13 and 2.14 on Python 3.13. Windows" in notes
+ assert notes.endswith("built for PyTorch 2.14 on Python 3.13.\n")
+
+ def test_packages_are_listed_per_platform(self):
+ entries = _entries(["linux_x86_64"]) + [
+ row for row in _entries(["win_amd64"]) if row[1].startswith("mamba_ssm-")
+ ]
+ notes = prebuilt_wheels.render_notes(entries, tag = "t", repo = "o/r")
+ windows = notes.split("Windows x86_64: ", 1)[1]
+ assert windows == "mamba-ssm 2.3.2.post1, built for PyTorch 2.13 and 2.14 on Python 3.13.\n"
+
+ def test_windows_only(self):
+ notes = prebuilt_wheels.render_notes(_entries(["win_amd64"]), tag = "t", repo = "o/r")
+ assert notes.startswith("Prebuilt Windows x86_64 CUDA 13 wheels for flash-attn 2.8.4")
+
+ def test_linux_only_reads_as_before(self):
+ notes = prebuilt_wheels.render_notes(_entries(["linux_x86_64"]), tag = "t", repo = "o/r")
+ assert notes == (
+ "Prebuilt Linux x86_64 CUDA 13 wheels for flash-attn 2.8.4, causal-conv1d 1.7.0 and "
+ "mamba-ssm 2.3.2.post1, built for PyTorch 2.13 and 2.14 on Python 3.13.\n"
+ )
+
+
+class TestSums:
+ def test_every_wheel_sorted_by_name(self):
+ entries = _entries(["win_amd64", "linux_x86_64"])
+ entries.append(("sha256:" + "f" * 64, "SHA256SUMS"))
+ entries.append(("a" * 64, "x.whl.sigstore.json"))
+ text = prebuilt_wheels.render_sums(entries)
+ lines = text.splitlines()
+ assert len(lines) == 12
+ names = [line.split(" ", 1)[1] for line in lines]
+ assert names == sorted(names)
+ assert all(name.endswith(".whl") for name in names)
+ assert {n.rsplit("-", 1)[1] for n in names} == {"linux_x86_64.whl", "win_amd64.whl"}
+
+ def test_the_sha256_prefix_is_dropped(self):
+ text = prebuilt_wheels.render_sums([("sha256:" + "AB" * 32, "a.whl")])
+ assert text == "ab" * 32 + " a.whl\n"
+
+ @pytest.mark.parametrize("digest", ["unknown", "sha256:unknown", "-", "", "z" * 64, "a" * 63])
+ def test_a_missing_digest_is_refused(self, digest):
+ with pytest.raises(SystemExit):
+ prebuilt_wheels.render_sums([(digest, "a.whl")])
+
+ def test_nothing_to_list_is_refused(self):
+ with pytest.raises(SystemExit):
+ prebuilt_wheels.render_sums([("a" * 64, "SHA256SUMS")])
+
+ def test_cli(self):
+ stdin = f"{'b' * 64} z.whl\n{'a' * 64} a.whl\n"
+ out = subprocess.run(
+ [sys.executable, str(SCRIPTS / "prebuilt_wheels.py"), "sums"],
+ input = stdin,
+ check = True,
+ capture_output = True,
+ text = True,
+ ).stdout
+ assert out == f"{'a' * 64} a.whl\n{'b' * 64} z.whl\n"
+
+
+WINDOWS_LISTING = "\n".join(
+ [
+ rf"D:\a\unsloth\unsloth\src\build\temp.win-amd64-cpython-313\Release\csrc\flash_attn\src\k_{i:03d}.obj: cuda_compile"
+ for i in range(20)
+ ]
+ + [
+ r"D:\a\unsloth\unsloth\src\build\temp.win-amd64-cpython-313\Release\csrc\flash_attn\flash_api.obj: compile",
+ r"D:\a\unsloth\unsloth\src\build\lib.win-amd64-cpython-313\flash_attn_2_cuda.cp313-win_amd64.pyd: link",
+ "all: phony",
+ ]
+)
+
+
+class TestShards:
+ def test_windows_targets_are_whole_paths(self):
+ objects = shard.list_objects(WINDOWS_LISTING)
+ assert len(objects) == 21
+ assert all(o.startswith("D:\\a\\") and o.endswith(".obj") for o in objects)
+
+ def test_windows_slices_cover_everything_once(self):
+ slices = [shard.slice_objects(WINDOWS_LISTING, k, 8) for k in range(8)]
+ assert sorted(o for s in slices for o in s) == shard.list_objects(WINDOWS_LISTING)
+
+ def test_linux_targets_are_unchanged(self):
+ listing = "/src/build/temp/a.o: cuda_compile\n/src/build/temp/b.o: compile\nall: phony"
+ assert shard.list_objects(listing) == ["/src/build/temp/a.o", "/src/build/temp/b.o"]
+
+ def test_no_objects_is_an_error_not_a_full_build(self):
+ with pytest.raises(SystemExit) as raised:
+ shard.slice_objects("all: phony\nfoo.pyd: link", 0, 8)
+ assert raised.value.code != 0
+
+ def test_an_empty_slice_never_runs_ninja(self, tmp_path, monkeypatch):
+ """Two objects across eight shards leaves six empty slices. `ninja -v -j 1` with no
+ target would build the default target, the whole extension, in every one of them."""
+ calls = []
+
+ def fake_run(command, **kwargs):
+ calls.append(command)
+ return types.SimpleNamespace(stdout = "a.obj: cuda_compile\nb.obj: cuda_compile\n")
+
+ cpp_extension = types.ModuleType("torch.utils.cpp_extension")
+ cpp_extension._run_ninja_build = None
+ torch = types.ModuleType("torch")
+ utils = types.ModuleType("torch.utils")
+ torch.utils = utils
+ utils.cpp_extension = cpp_extension
+ monkeypatch.setitem(sys.modules, "torch", torch)
+ monkeypatch.setitem(sys.modules, "torch.utils", utils)
+ monkeypatch.setitem(sys.modules, "torch.utils.cpp_extension", cpp_extension)
+ monkeypatch.setattr(shard.subprocess, "run", fake_run)
+ (tmp_path / "setup.py").write_text(
+ "import torch.utils.cpp_extension as c\nc._run_ninja_build('.', False, '')\n"
+ )
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setattr(sys, "argv", ["prebuilt_wheels_shard.py", "5", "8"])
+ with pytest.raises(SystemExit) as raised:
+ shard.main()
+ assert raised.value.code == 0
+ assert calls == [["ninja", "-t", "targets", "all"]]
+
+ def test_a_non_empty_slice_names_its_targets(self, tmp_path, monkeypatch):
+ calls = []
+
+ def fake_run(command, **kwargs):
+ calls.append(command)
+ return types.SimpleNamespace(stdout = "a.obj: cuda_compile\nb.obj: cuda_compile\n")
+
+ cpp_extension = types.ModuleType("torch.utils.cpp_extension")
+ torch = types.ModuleType("torch")
+ utils = types.ModuleType("torch.utils")
+ torch.utils = utils
+ utils.cpp_extension = cpp_extension
+ monkeypatch.setitem(sys.modules, "torch", torch)
+ monkeypatch.setitem(sys.modules, "torch.utils", utils)
+ monkeypatch.setitem(sys.modules, "torch.utils.cpp_extension", cpp_extension)
+ monkeypatch.setattr(shard.subprocess, "run", fake_run)
+ monkeypatch.setenv("MAX_JOBS", "1")
+ (tmp_path / "setup.py").write_text(
+ "import torch.utils.cpp_extension as c\nc._run_ninja_build('.', False, '')\n"
+ )
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setattr(sys, "argv", ["prebuilt_wheels_shard.py", "1", "8"])
+ with pytest.raises(SystemExit):
+ shard.main()
+ assert calls[-1] == ["ninja", "-v", "-j", "1", "b.obj"]
+
+
+class TestDeadline:
+ @pytest.mark.parametrize(
+ "text, seconds", [("300m", 18000), ("90s", 90), ("2h", 7200), ("45", 45)]
+ )
+ def test_durations(self, text, seconds):
+ assert deadline.parse_duration(text) == seconds
+
+ @pytest.mark.parametrize("text", ["", "m", "-5m", "0", "five"])
+ def test_bad_durations(self, text):
+ with pytest.raises(SystemExit):
+ deadline.parse_duration(text)
+
+ def test_the_exit_status_passes_through(self):
+ assert deadline.run(30, [sys.executable, "-c", "raise SystemExit(3)"]) == 0 + 3
+
+ @pytest.mark.skipif(sys.platform == "win32", reason = "POSIX process groups")
+ def test_the_whole_tree_ends_at_the_deadline(self, tmp_path):
+ marker = tmp_path / "grandchild.pid"
+ script = (
+ "import subprocess, sys, time\n"
+ "child = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(120)'])\n"
+ f"open({str(marker)!r}, 'w').write(str(child.pid))\n"
+ "time.sleep(120)\n"
+ )
+ started = time.monotonic()
+ assert deadline.run(2, [sys.executable, "-c", script]) == 124
+ assert time.monotonic() - started < 60
+ grandchild = int(marker.read_text())
+ for _ in range(50):
+ try:
+ import os
+ os.kill(grandchild, 0)
+ except ProcessLookupError:
+ break
+ time.sleep(0.1)
+ else:
+ pytest.fail("the grandchild outlived the deadline")
+
+ def test_usage(self):
+ assert deadline.main(["x", "5m"]) == 2
+ assert deadline.main(["x", "5m", "echo"]) == 2
+
+
+MSVC_STEP = "Activate the MSVC ${{ matrix.platform == 'win_arm64' && 'arm64' || 'x64' }} environment (Windows)"
+# Step name -> its `if`: the two toolkits are per Windows platform, the rest per OS.
+WINDOWS_SETUP = {
+ "Enable long paths (Windows)": "runner.os == 'Windows'",
+ "Install the CUDA ${{ matrix.cuda_tag }} toolkit (Windows)": "matrix.platform == 'win_amd64'",
+ "Install the CUDA 13.4 toolkit (Windows ARM64)": "matrix.platform == 'win_arm64'",
+ MSVC_STEP: "runner.os == 'Windows'",
+ "Install ccache (Windows)": "runner.os == 'Windows'",
+ "Report memory, page file and disk (Windows)": "runner.os == 'Windows'",
+}
+LINUX_ONLY = (
+ "Harden runner (audit)",
+ "Free up disk space",
+ "Install the CUDA ${{ matrix.cuda_tag }} toolkit",
+ "Install ccache",
+ "Set up swap space",
+)
+
+
+class TestWorkflow:
+ def test_platforms_input_defaults_to_both(self, workflow):
+ inputs = triggers(workflow)["workflow_dispatch"]["inputs"]
+ assert inputs["platforms"]["default"] == "linux_x86_64,win_amd64"
+ plan = _step(workflow, "plan", "Expand the dispatch inputs into a matrix")
+ assert plan["env"]["UW_PLATFORMS"] == "${{ inputs.platforms }}"
+
+ @pytest.mark.parametrize("job", ["warm", "build"])
+ def test_windows_setup_exists_in_both_jobs_and_is_gated(self, workflow, job):
+ names = [s.get("name") for s in _steps(workflow, job)]
+ for name, gate in WINDOWS_SETUP.items():
+ step = _step(workflow, job, name)
+ assert step["if"] == gate, (job, name)
+ assert step["shell"] == "pwsh", (job, name)
+ for name in LINUX_ONLY:
+ assert _step(workflow, job, name)["if"] == "runner.os == 'Linux'", (job, name)
+ first_checkout = next(i for i, n in enumerate(names) if n and n.startswith("Checkout"))
+ assert names.index("Enable long paths (Windows)") < first_checkout
+ assert workflow["jobs"][job]["defaults"]["run"]["shell"] == "bash"
+
+ def test_warm_and_build_run_the_same_windows_setup(self, workflow):
+ for name in WINDOWS_SETUP:
+ assert _step(workflow, "warm", name) == _step(workflow, "build", name), name
+
+ def test_the_cuda_installer_is_nvidias_and_signature_checked(self, workflow):
+ run = _step(workflow, "build", "Install the CUDA ${{ matrix.cuda_tag }} toolkit (Windows)")[
+ "run"
+ ]
+ assert "https://developer.download.nvidia.com/compute/cuda/" in run
+ assert "Get-AuthenticodeSignature" in run and "O=NVIDIA Corporation" in run
+ assert "'-s'" in run and "'-n'" in run
+ assert "New-Item -ItemType Junction -Path 'C:\\cuda'" in run
+ for package in ("nvcc", "cudart", "cublas_dev", "cusparse_dev", "cusolver_dev"):
+ assert f"'{package}'" in run, package
+ assert "visual_studio_integration" not in run and "Display.Driver" not in run
+
+ def test_ccache_is_pinned_by_sha256(self, workflow):
+ run = _step(workflow, "build", "Install ccache (Windows)")["run"]
+ assert "ccache-4.14.1-windows-x86_64" in run
+ assert "6219f3865ca59aec41ee4b678df171d5d35855ecb2b6dbbbd20690b3a68af7b4" in run
+ assert "CCACHE_COMPILERCHECK=content" in run
+
+ def test_msvc_environment_is_exported_with_distutils_use_sdk(self, workflow):
+ step = _step(workflow, "build", MSVC_STEP)
+ run = step["run"]
+ assert "vswhere.exe" in run and "$env:VCVARS" in run
+ assert (
+ "'vcvars64.bat'" in step["env"]["VCVARS"]
+ and "'vcvarsarm64.bat'" in step["env"]["VCVARS"]
+ )
+ assert "DISTUTILS_USE_SDK=1" in run
+ assert "GITHUB_ENV" in run and "GITHUB_PATH" in run
+
+ def test_artifacts_and_caches_are_per_platform(self, workflow):
+ body = json.dumps(workflow["jobs"])
+ for prefix in ("ccache-", "wheel-", "prebuilt-ccache-"):
+ for value in (
+ v
+ for v in body.split('"')
+ if v.startswith(prefix) and "${{ matrix" in v and "*" not in v
+ ):
+ assert value.startswith(prefix + "${{ matrix.platform }}"), value
+
+ def test_the_windows_build_ends_the_whole_tree_at_the_deadline(self, workflow):
+ for job, name in (
+ ("warm", "Compile slice ${{ matrix.shard }} of ${{ matrix.shards }}"),
+ ("build", "Build the wheel"),
+ ):
+ run = _step(workflow, job, name)["run"]
+ assert "prebuilt_wheels_timeout.py" in run
+ assert 'timeout --signal=INT --kill-after=120 "$BUILD_TIMEOUT"' in run
+
+ def test_the_rename_expects_the_cells_platform(self, workflow):
+ step = _step(workflow, "build", "Name the wheel the way upstream names it")
+ assert step["env"]["WHEEL_PLATFORM"] == "${{ matrix.platform }}"
+ assert (
+ "PLATFORM" not in step["env"]
+ ) # vcvars exports Platform=x64; Windows env is case-insensitive
+ assert "-${WHEEL_PLATFORM}.whl" in step["run"]
+
+ def test_the_torch_abi_check_follows_the_cell(self, workflow):
+ for job in ("warm", "build"):
+ step = next(
+ s
+ for s in _steps(workflow, job)
+ if s.get("name", "") == "Install ${{ matrix.torch_spec }}"
+ )
+ assert step["env"]["MATRIX_ABI"] == "${{ matrix.abi }}"
+ assert 'os.environ["MATRIX_ABI"]' in step["run"]
+
+ def test_the_windows_smoke_hides_the_toolkit(self, workflow):
+ """A user has no CUDA toolkit; the import must resolve against torch's own DLLs."""
+ smoke = _step(workflow, "build", "Smoke test the wheel in a fresh venv")
+ run = smoke["run"]
+ assert "Scripts/python.exe" in run
+ assert "unset CUDA_HOME CUDA_PATH" in run
+ assert smoke["env"]["SMOKE_EXCLUDE"] == "${{ matrix.smoke_exclude }}"
+ assert smoke["env"]["SMOKE_EXTRA"] == "${{ matrix.smoke_extra }}"
+ assert 'os.environ.get("SMOKE_EXCLUDE"' in run and 'os.environ.get("SMOKE_EXTRA"' in run
+
+ def test_the_gpu_job_only_gets_linux_cells(self, workflow):
+ job = workflow["jobs"]["gpu-smoke"]
+ assert job["strategy"]["matrix"] == "${{ fromJSON(needs.plan.outputs.gpu_matrix) }}"
+ assert "needs.plan.outputs.gpu_count != '0'" in job["if"]
+ assert workflow["jobs"]["plan"]["outputs"]["gpu_count"]
+
+ def test_publish_rebuilds_sha256sums_over_the_whole_release(self, workflow):
+ steps = workflow["jobs"]["publish"]["steps"]
+ create = next(s for s in steps if s.get("name") == "Create or update the release")
+ # This run's SHA256SUMS lists this run only; it must not become the release's.
+ assert "wheels/SHA256SUMS" not in create["run"]
+ regen = next(s for s in steps if "SHA256SUMS" in s.get("name", ""))
+ run = regen["run"]
+ assert "prebuilt_wheels.py sums" in run
+ assert "--clobber SHA256SUMS" in run
+ assert "releases/assets/$id" in run
+ assert "sha256:unknown" not in run
+ assert run.index("prebuilt_wheels.py sums") < run.index("prebuilt_wheels.py notes")
+
+
+# The text the patch keys on, from the pinned upstream files.
+_MAMBA_KERNEL = """#include <c10/cuda/CUDAException.h>
+
+#ifndef USE_ROCM
+ #include <cub/block/block_load.cuh>
+#else
+ #include <hipcub/hipcub.hpp>
+#endif
+
+constexpr float kLog2e = M_LOG2E;
+
+void launch() {
+ BOOL_SWITCH(x, kX, [&] {
+ BOOL_SWITCH(y, kY, [&] {
+ if (kSmemSize >= 48 * 1024) {
+ #ifndef USE_ROCM
+ C10_CUDA_CHECK(cudaFuncSetAttribute(
+ kernel, cuda_branch));
+ #else
+ C10_CUDA_CHECK(cudaFuncSetAttribute(
+ (void *) kernel, rocm_branch));
+ #endif
+ }
+ });
+ });
+}
+
+void dispatch() {
+ #ifndef USE_ROCM
+ cuda_dispatch();
+ #else
+ rocm_dispatch();
+ #endif
+}
+"""
+
+
+class TestMambaMsvcPatch:
+ def _tree(
+ self,
+ tmp_path,
+ text = _MAMBA_KERNEL,
+ ):
+ kernels = tmp_path / "csrc" / "selective_scan"
+ kernels.mkdir(parents = True)
+ for name in ("selective_scan_fwd_kernel.cuh", "selective_scan_bwd_kernel.cuh"):
+ (kernels / name).write_text(text, encoding = "utf-8")
+ return kernels
+
+ def _run(self, root):
+ return subprocess.run(
+ [sys.executable, str(SCRIPTS / "patch_mamba_msvc.py"), str(root)],
+ capture_output = True,
+ text = True,
+ )
+
+ def test_keeps_the_cuda_branch_and_is_idempotent(self, tmp_path):
+ kernels = self._tree(tmp_path)
+ assert self._run(tmp_path).returncode == 0
+ first = (kernels / "selective_scan_fwd_kernel.cuh").read_text(encoding = "utf-8")
+ assert "cuda_branch" in first and "rocm_branch" not in first
+ # File-scope USE_ROCM blocks are legal under MSVC and stay.
+ assert "hipcub" in first
+ # Function-scope USE_ROCM blocks are legal under MSVC and stay.
+ assert "rocm_dispatch()" in first
+ again = self._run(tmp_path)
+ assert again.returncode == 0 and "already patched" in again.stdout
+ assert (kernels / "selective_scan_fwd_kernel.cuh").read_text(encoding = "utf-8") == first
+
+ def test_a_second_in_lambda_block_fails_loudly(self, tmp_path):
+ body = _MAMBA_KERNEL.split("void launch()")[1].split("void dispatch()")[0]
+ self._tree(tmp_path, _MAMBA_KERNEL + "void launch2()" + body)
+ result = self._run(tmp_path)
+ assert result.returncode == 1
+ assert "found 2" in result.stderr
+
+ def test_runs_on_windows_mamba_cells_only(self, workflow):
+ for job in ("warm", "build"):
+ step = next(
+ s
+ for s in workflow["jobs"][job]["steps"]
+ if "patch_mamba_msvc.py" in s.get("run", "")
+ )
+ assert step["if"] == "runner.os == 'Windows' && matrix.package == 'mamba-ssm'"