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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Prebuilt wheels: drop -lineinfo, unreachable SASS and flash-attn PTX; add extension-free mamba-ssm (#14142)
Daniel Hana9819dd6eb93
diff --git a/.github/scripts/patch_cuda_targets.py b/.github/scripts/patch_cuda_targets.pynew file mode 100644index 00000000000..59ef90001f7--- /dev/null+++ b/.github/scripts/patch_cuda_targets.py@@ -0,0 +1,134 @@+#!/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++"""Trim the device code each package's setup.py compiles to what a GPU can actually run.++Device code is almost all of every wheel, and part of it is dead weight:++- causal-conv1d and mamba-ssm pass -lineinfo, which only feeds profilers and sanitizers a source+ line table. The generated SASS is identical without it (checked with cuobjdump -sass), and it is+ more than half of every cubin.+- They also hardcode SASS for sm_87 (Jetson Orin), sm_110 (Jetson Thor) and sm_121 (GB10 / DGX+ Spark, N1X). Those are integrated Arm GPUs, so the x86 wheels drop them; the Arm wheels keep the+ full list. Whatever a platform can reach keeps SASS compiled for its own arch.+- flash-attn's setup.py (at our pin) embeds compute_<newest> PTX next to the SASS so an unlisted+ future GPU can JIT. Upstream's 2.8.1 wheels ship SASS only, every GPU flash-attn targets has+ SASS here, and compute_120 PTX cannot JIT for anything older than sm_120, so it is a second copy+ of every kernel for hardware that does not exist yet.++Like patch_mamba_cxx20.py this asserts on text rather than applying a diff: every line it touches+must be in one of two known states, an already-trimmed file is a success, and anything else fails+loudly so a pin bump is re-read by a human instead of building something nobody chose.++Usage: patch_cuda_targets.py --package NAME --archs "75;80;90;100;103;120" <setup.py>+"""++from __future__ import annotations++import argparse+import re+import sys+from pathlib import Path++# The SASS targets causal-conv1d's and mamba-ssm's setup.py emit on CUDA 13. A pin that adds or+# removes one is a different build and fails here.+SSM_KNOWN_ARCHS = frozenset({"75", "80", "87", "90", "100", "103", "110", "120", "121"})+SSM_PACKAGES = ("causal-conv1d", "mamba-ssm")+# One in the CUDA branch of each; the HIP branch does not pass it.+LINEINFO = '"-lineinfo",'+LINEINFO_OCCURRENCES = 1++FLASH_PTX = 'cc_flag += ["-gencode", f"arch=compute_{newest},code=compute_{newest}"]'+FLASH_PTX_TRIMMED = "pass # Unsloth: SASS only, no compute_<newest> PTX"++_GENCODE = re.compile(+ r'^(?P<indent>[ \t]*)cc_flag\.append\("-gencode"\)\r?\n'+ r'(?P=indent)cc_flag\.append\("arch=compute_(?P<arch>\d+),code=sm_(?P=arch)"\)\r?\n',+ re.MULTILINE,+)+++def _fail(message: str) -> int:+ print(f"::error::{message}", file = sys.stderr)+ return 1+++def trim_ssm(source: str, archs: set[str]) -> tuple[str | None, str]:+ """(new source or None on failure, message) for causal-conv1d / mamba-ssm."""+ found = [m.group("arch") for m in _GENCODE.finditer(source)]+ present = set(found)+ if len(found) != len(present):+ return None, f"a gencode is listed twice: {found}"+ unknown = present - SSM_KNOWN_ARCHS+ if unknown:+ return None, f"setup.py emits SASS this script does not know: {sorted(unknown)}"+ missing = archs - present+ if missing:+ return None, f"requested SASS {sorted(missing)} is not in setup.py ({sorted(present)})"+ # Already trimmed: only requested archs left. Untrimmed: exactly the known CUDA 13 set.+ if present != archs and present != SSM_KNOWN_ARCHS:+ return None, (+ f"setup.py emits {sorted(present, key = int)}, neither the known CUDA 13 set nor the "+ f"requested one; re-read it before bumping the pin"+ )+ # A dropped pair becomes `pass`, so an `if` whose every target went keeps a body.+ trimmed = _GENCODE.sub(+ lambda m: m.group(0)+ if m.group("arch") in archs+ else f"{m.group('indent')}pass # Unsloth: no sm_{m.group('arch')} SASS (patch_cuda_targets.py)\n",+ source,+ )+ lineinfo = trimmed.count(LINEINFO)+ if lineinfo not in (0, LINEINFO_OCCURRENCES):+ return None, f"expected {LINEINFO_OCCURRENCES} {LINEINFO} line, found {lineinfo}"+ trimmed = re.sub(+ r"^[ \t]*" + re.escape(LINEINFO) + r"[ \t]*\r?\n", "", trimmed, flags = re.MULTILINE+ )+ dropped = sorted(present - archs, key = int)+ return trimmed, (+ f"SASS {';'.join(sorted(archs, key = int))} (dropped {dropped or 'none'})"+ + (", -lineinfo removed" if lineinfo else "")+ )+++def trim_flash(source: str) -> tuple[str | None, str]:+ if source.count(FLASH_PTX_TRIMMED) == 1 and FLASH_PTX not in source:+ return source, "already SASS only"+ if source.count(FLASH_PTX) != 1:+ return None, f"expected one PTX gencode line ({FLASH_PTX}), found {source.count(FLASH_PTX)}"+ return source.replace(FLASH_PTX, FLASH_PTX_TRIMMED), "compute_<newest> PTX removed, SASS kept"+++def main(argv: list[str] | None = None) -> int:+ parser = argparse.ArgumentParser(description = __doc__.splitlines()[0])+ parser.add_argument("--package", required = True)+ parser.add_argument("--archs", default = "", help = "SASS targets to keep, ';'-separated")+ parser.add_argument("setup_py", type = Path)+ args = parser.parse_args(argv)+ if not args.setup_py.is_file():+ return _fail(f"{args.setup_py} does not exist")+ source = args.setup_py.read_text(encoding = "utf-8")+ if args.package == "flash-attn":+ new, message = trim_flash(source)+ elif args.package in SSM_PACKAGES:+ archs = {a for a in args.archs.split(";") if a}+ if not archs or not archs <= SSM_KNOWN_ARCHS:+ return _fail(f"--archs must be a subset of {sorted(SSM_KNOWN_ARCHS, key = int)}")+ new, message = trim_ssm(source, archs)+ else:+ return _fail(f"unknown package {args.package!r}")+ if new is None:+ return _fail(f"{args.setup_py}: {message}")+ try:+ compile(new, str(args.setup_py), "exec")+ except SyntaxError as exc:+ return _fail(f"{args.setup_py}: the trimmed setup.py does not parse: {exc}")+ if new != source:+ args.setup_py.write_text(new, encoding = "utf-8")+ print(f"{args.setup_py}: {message}")+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/.github/scripts/prebuilt_wheels.py b/.github/scripts/prebuilt_wheels.pyindex 9b4445665ea..2387a8da18b 100644--- a/.github/scripts/prebuilt_wheels.py+++ b/.github/scripts/prebuilt_wheels.py@@ -73,7 +73,26 @@ NVCC_APPEND_FLAGS = "-Xfatbin -compress-all --compress-mode=size"# ccache hashes the nvcc command line, not NVCC_APPEND_FLAGS, and the Linux runners' ccache (4.5)# has no namespaces, so the flags' identity goes into the actions/cache key instead: a cache# saved before them would otherwise hand back uncompressed objects.-CCACHE_TAG = "fatbin-size"+CCACHE_TAG = "fatbin-size-sass"++# SASS targets per package (patch_cuda_targets.py trims each setup.py to these). causal-conv1d's+# and mamba-ssm's own list also builds sm_87 (Jetson Orin), sm_110 (Jetson Thor) and sm_121 (GB10 /+# DGX Spark, N1X), which are integrated Arm GPUs: no x86 host can have one, so the x86 wheels drop+# them and the Arm wheels keep the full list. Every GPU a platform can reach keeps SASS compiled+# for its own arch (ptxas schedules 87 / 103 / 121 differently from 80 / 100 / 120), so nothing+# falls back to a sibling's cubin. flash-attn keeps its list in its SPECS env+# (FLASH_ATTN_CUDA_ARCHS); the patch only drops the PTX its setup.py adds for the newest arch.+SSM_SASS = "75;80;90;100;103;120"+SSM_SASS_ARM = "75;80;87;90;100;103;110;120;121"+SSM_SASS_BY_PLATFORM = {"linux_aarch64": SSM_SASS_ARM, "win_arm64": SSM_SASS_ARM}+++def cuda_archs(package: str, platform: str) -> str:+ """The SASS targets one cell must contain, ';'-separated, as the gate checks them."""+ if package == "flash-attn":+ return SPECS[package]["env"]["FLASH_ATTN_CUDA_ARCHS"]+ return SSM_SASS_BY_PLATFORM.get(platform, SSM_SASS)+# 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@@ -198,9 +217,9 @@ SPECS = {# Leave time to upload partial caches after a build timeout."build_timeout": "300m","import_names": ["flash_attn", "flash_attn_2_cuda"],- # Largest CUDA extension the size gate lets through, in MB. Compressed it is ~280; the- # uncompressed build that prompted the gate was 1,488.- "max_extension_mb": 450,+ # Largest CUDA extension the size gate lets through, in MB. SASS only and compressed it is+ # ~176 (PTX was 23% of each measured TU); with the PTX ~229, uncompressed 1,488.+ "max_extension_mb": 230,},"causal-conv1d": {"dist": "causal_conv1d",@@ -215,8 +234,8 @@ SPECS = {},"build_timeout": "120m","import_names": ["causal_conv1d", "causal_conv1d_cuda"],- # ~105 with these flags, 263 in default mode.- "max_extension_mb": 160,+ # x86 ~31 trimmed without -lineinfo (Arm ~53), 117 before, 263 in default mode.+ "max_extension_mb": 80,},"mamba-ssm": {"dist": "mamba_ssm",@@ -238,9 +257,9 @@ SPECS = {"patch": "cxx20","build_timeout": "180m","import_names": ["mamba_ssm", "selective_scan_cuda"],- # ~230 with these flags, 358 in default mode: the gate only catches flags that never- # reached nvcc.- "max_extension_mb": 300,+ # x86 ~62 trimmed without -lineinfo (Arm ~88), 241 before, 358 in default mode: the gate+ # catches flags or a trim that never reached nvcc.+ "max_extension_mb": 130,},}@@ -412,6 +431,7 @@ def build_matrix("build_env": _build_env(spec, platform),"nvcc_append_flags": NVCC_APPEND_FLAGS,"ccache_tag": CCACHE_TAG,+ "cuda_archs": cuda_archs(package, platform),"max_extension_mb": spec["max_extension_mb"],"import_names": " ".join(spec["import_names"]),"wheel_name": wheel_name(@@ -446,6 +466,78 @@ def gpu_matrix(include: list[dict]) -> list[dict]:return [cell for cell in include if cell["platform"] == "linux_x86_64"]+# mamba-ssm without its CUDA extension. Since 2.3.2.post1 its setup.py builds selective_scan_cuda+# only with MAMBA_KEEP_CUDA_BUILD=TRUE, and the Mamba-2 hybrids (Nemotron-H, Falcon-H1, Granite-4.0-H,+# Bamba, Zamba2, Mamba2) never call it on transformers 5: they need mamba_ssm's Triton ops plus a+# causal conv, which unsloth-zoo supplies in Triton. One py3-none-any wheel (about 224 KB) then serves+# every torch, Python and OS, instead of a 230 to 594 MB CUDA wheel per cell. Built from the same pin+# as the CUDA wheel and published beside it whenever a run builds mamba-ssm.+PURE_PACKAGE = "mamba-ssm"+PURE_SMOKE_TORCH = "2.13.0"+# Import smoke only (no GPU on these runners). triton / triton-windows match that torch's Triton.+PURE_SMOKE_TRITON = {"linux": "triton==3.7.1", "windows": "triton-windows==3.7.1.post27"}+# What `import mamba_ssm` pulls in besides torch and Triton: mixer_seq_simple imports+# huggingface_hub and transformers.generation. tilelang / quack-kernels (Mamba-3) are optional.+PURE_SMOKE_DEPS = ("einops", "packaging", "huggingface_hub", "transformers")+PURE_IMPORTS = (+ "import mamba_ssm",+ "from mamba_ssm.ops.triton.ssd_combined import mamba_chunk_scan_combined",+ "from mamba_ssm.ops.triton.selective_state_update import selective_state_update",+)+++def pure_wheel_name() -> str:+ return f"{SPECS[PURE_PACKAGE]['dist']}-{SPECS[PURE_PACKAGE]['version']}-py3-none-any.whl"+++def pure_matrix(packages: str = "") -> list[dict]:+ """The extension-free mamba-ssm build: one cell when the run builds mamba-ssm, else none."""+ chosen = _resolve(packages, DEFAULT_PACKAGES, DEFAULT_PACKAGES, "package")+ if PURE_PACKAGE not in chosen:+ return []+ spec = SPECS[PURE_PACKAGE]+ return [+ {+ "package": PURE_PACKAGE,+ "dist": spec["dist"],+ "version": spec["version"],+ "repo": spec["repo"],+ "ref": spec["ref"],+ "wheel_name": pure_wheel_name(),+ "smoke_torch": PURE_SMOKE_TORCH,+ "smoke_triton_linux": PURE_SMOKE_TRITON["linux"],+ "smoke_triton_windows": PURE_SMOKE_TRITON["windows"],+ "smoke_deps": " ".join(PURE_SMOKE_DEPS),+ "smoke_imports": "; ".join(PURE_IMPORTS),+ }+ ]+++def check_pure_wheel(wheel: str) -> list[str]:+ """Problems with the extension-free wheel: its exact name, no compiled code, a pure tag."""+ problems = []+ name = os.path.basename(wheel)+ if name != pure_wheel_name():+ problems.append(f"{name}: expected {pure_wheel_name()}")+ with zipfile.ZipFile(wheel) as archive:+ names = archive.namelist()+ compiled = [n for n in names if n.endswith((".so", ".pyd", ".dll", ".dylib"))]+ if compiled:+ problems.append(f"{name}: carries compiled code: {', '.join(compiled)}")+ dist = SPECS[PURE_PACKAGE]["dist"]+ for required in (f"{dist}/__init__.py", f"{dist}/ops/triton/ssd_combined.py"):+ if required not in names:+ problems.append(f"{name}: {required} is missing")+ wheel_meta = [n for n in names if n.endswith(".dist-info/WHEEL")]+ if len(wheel_meta) != 1:+ problems.append(f"{name}: expected one .dist-info/WHEEL, found {len(wheel_meta)}")+ else:+ meta = archive.read(wheel_meta[0]).decode("utf-8", "replace").splitlines()+ if "Root-Is-Purelib: true" not in meta or "Tag: py3-none-any" not in meta:+ problems.append(f"{name}: WHEEL metadata is not pure py3-none-any")+ return problems++def warm_matrix(include: list[dict]) -> list[dict]:"""One warm job per (cell, shard), for the cells whose package is sharded."""return [@@ -550,13 +642,20 @@ def render_notes(entries: list[tuple[str, str]], tag: str, repo: str) -> str: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]+ pure = any(name == pure_wheel_name() for _, name in entries)+ tail = (+ f" Also {PURE_PACKAGE} {SPECS[PURE_PACKAGE]['version']} without its CUDA extension "+ f"({pure_wheel_name()}), for Mamba-2 models whose causal conv runs on Triton.\n"+ if pure+ else "\n"+ )if not parsed:- return "No wheels are attached to this release yet.\n"+ return tail.lstrip() if pure else "No wheels are attached to this release yet.\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"+ f"{_packages(parsed)}, built for {_coverage(parsed)}.{tail}")# Per platform: separate runs need not cover the same packages or torch minors.per_platform = " ".join(@@ -564,7 +663,7 @@ def render_notes(entries: list[tuple[str, str]], tag: str, repo: str) -> str:for platform in platformsfor rows in [[row for row in parsed if row["platform"] == platform]])- return f"Prebuilt CUDA {CUDA_TAG} wheels. {per_platform}\n"+ return f"Prebuilt CUDA {CUDA_TAG} wheels. {per_platform}{tail}"def render_sums(entries: list[tuple[str, str]]) -> str:@@ -599,6 +698,7 @@ def _cmd_matrix(args: argparse.Namespace) -> int:matrix = json.dumps({"include": include}, separators = (",", ":"))warm = warm_matrix(include)gpu = gpu_matrix(include)+ pure = pure_matrix(os.environ.get("UW_PACKAGES", ""))print(matrix)# Writing the step output here rather than echoing it in YAML keeps the JSON -- which is@@ -615,11 +715,14 @@ def _cmd_matrix(args: argparse.Namespace) -> int:gpu_json = json.dumps({"include": gpu}, separators = (",", ":"))handle.write(f"gpu_matrix={gpu_json}\n")handle.write(f"gpu_count={len(gpu)}\n")+ pure_json = json.dumps({"include": pure}, separators = (",", ":"))+ handle.write(f"pure_matrix={pure_json}\n")+ handle.write(f"pure_count={len(pure)}\n")summary = os.environ.get("GITHUB_STEP_SUMMARY")if summary:- listing = "\n".join(f"- `{cell['wheel_name']}`" for cell in include)+ listing = "\n".join(f"- `{cell['wheel_name']}`" for cell in include + pure)with open(summary, "a", encoding = "utf-8") as handle:- handle.write(f"### Build plan\n\n{len(include)} cells:\n\n{listing}\n")+ handle.write(f"### Build plan\n\n{len(include) + len(pure)} cells:\n\n{listing}\n")return 0@@ -684,6 +787,98 @@ def _cmd_size_gate(args: argparse.Namespace) -> int:return 0+# A fatbin container: magic, version 1, a 16-byte header and the size of the entries after it.+# Each entry's header starts with kind (1 PTX, 2 ELF), its own header size, the payload size,+# and the target arch at byte 28. Read straight from the file rather than through cuobjdump,+# which only reads its host's object format (a Linux cuobjdump says a Windows .pyd "does not+# contain device code") and names PTX entries sm_XX.ptx, easy to miss with a pattern.+_FATBIN_MAGIC = (0xBA55ED50).to_bytes(4, "little")+_FATBIN_PTX, _FATBIN_ELF = 1, 2+++def device_targets(data: bytes) -> tuple[set[str], set[str]]:+ """(SASS archs, PTX archs) of every fatbin embedded in an ELF .so or PE .pyd."""+ sass, ptx = set(), set()+ pos = data.find(_FATBIN_MAGIC)+ while pos >= 0:+ version = int.from_bytes(data[pos + 4 : pos + 6], "little")+ header = int.from_bytes(data[pos + 6 : pos + 8], "little")+ size = int.from_bytes(data[pos + 8 : pos + 16], "little")+ end = pos + 16 + size+ if version != 1 or header != 16 or size <= 0 or end > len(data):+ pos = data.find(_FATBIN_MAGIC, pos + 4)+ continue+ entry = pos + 16+ while entry + 32 <= end:+ kind = int.from_bytes(data[entry : entry + 2], "little")+ entry_header = int.from_bytes(data[entry + 4 : entry + 8], "little")+ payload = int.from_bytes(data[entry + 8 : entry + 16], "little")+ arch = str(int.from_bytes(data[entry + 28 : entry + 32], "little"))+ if kind not in (_FATBIN_PTX, _FATBIN_ELF) or entry_header < 32:+ break+ if entry + entry_header + payload > end:+ break+ (ptx if kind == _FATBIN_PTX else sass).add(arch)+ entry += entry_header + payload+ pos = data.find(_FATBIN_MAGIC, end)+ return sass, ptx+++def _archs(prefix: str, archs: set[str]) -> str:+ return ", ".join(f"{prefix}_{arch}" for arch in sorted(archs, key = int))+++def check_targets(name: str, sass: set[str], ptx: set[str], want: set[str]) -> list[str]:+ """Problems with one extension's device code: every wanted SASS present, nothing else."""+ problems = []+ if missing := want - sass:+ problems.append(f"{name}: no SASS for {_archs('sm', missing)}")+ if extra := sass - want:+ problems.append(f"{name}: SASS for {_archs('sm', extra)} was not asked for")+ if ptx:+ problems.append(f"{name}: carries PTX for {_archs('compute', ptx)}")+ return problems+++def _cmd_targets_gate(args: argparse.Namespace) -> int:+ want = {arch for arch in args.archs.split(";") if arch}+ if not want:+ raise SystemExit("--archs is empty")+ problems, checked = [], 0+ with zipfile.ZipFile(args.wheel) as archive:+ members = [n for n in archive.namelist() if n.endswith((".so", ".pyd"))]+ if not members:+ raise SystemExit(f"{args.wheel}: no compiled extension in the wheel")+ for member in members:+ sass, ptx = device_targets(archive.read(member))+ # A host-only helper module has no device code at all; only a CUDA one is checked.+ if not sass and not ptx:+ print(f"{member}: no device code")+ continue+ checked += 1+ print(+ f"{member}: SASS sm_{';'.join(sorted(sass, key = int))}"+ + (f", PTX compute_{';'.join(sorted(ptx, key = int))}" if ptx else ", no PTX")+ )+ problems += check_targets(member, sass, ptx, want)+ if not checked:+ # Every package here is a CUDA extension; none with device code means the scan found+ # nothing, not that there was nothing to check.+ problems.append(f"{args.wheel}: no extension with device code")+ for problem in problems:+ print(f"::error::{problem}")+ return 1 if problems else 0+++def _cmd_pure_gate(args: argparse.Namespace) -> int:+ problems = check_pure_wheel(args.wheel)+ for problem in problems:+ print(f"::error::{problem}")+ if not problems:+ print(f"{os.path.basename(args.wheel)}: pure py3-none-any, no compiled code")+ return 1 if problems else 0++def _cmd_notes(args: argparse.Namespace) -> int:sys.stdout.write(render_notes(_read_digest_lines(), tag = args.tag, repo = args.repo))return 0@@ -702,8 +897,8 @@ def main(argv: list[str] | None = None) -> int:matrix_parser.add_argument("--github",action = "store_true",- help = "also append matrix/count, warm_matrix/warm_count and gpu_matrix/gpu_count to "- "$GITHUB_OUTPUT and a listing to $GITHUB_STEP_SUMMARY",+ help = "also append matrix/count, warm_matrix/warm_count, gpu_matrix/gpu_count and "+ "pure_matrix/pure_count to $GITHUB_OUTPUT and a listing to $GITHUB_STEP_SUMMARY",)matrix_parser.set_defaults(func = _cmd_matrix)@@ -727,6 +922,19 @@ def main(argv: list[str] | None = None) -> int:gate_parser.add_argument("wheel")gate_parser.set_defaults(func = _cmd_size_gate)+ targets_parser = sub.add_parser(+ "targets-gate", help = "fail unless the wheel's device code is exactly --archs SASS"+ )+ targets_parser.add_argument("--archs", required = True)+ targets_parser.add_argument("wheel")+ targets_parser.set_defaults(func = _cmd_targets_gate)++ pure_parser = sub.add_parser(+ "pure-gate", help = "fail unless the wheel is the extension-free mamba-ssm build"+ )+ pure_parser.add_argument("wheel")+ pure_parser.set_defaults(func = _cmd_pure_gate)+args = parser.parse_args(argv)return args.func(args)diff --git a/.github/workflows/prebuilt-cuda-wheels.yml b/.github/workflows/prebuilt-cuda-wheels.ymlindex 7c1954a2ba7..9e4199fbe47 100644--- a/.github/workflows/prebuilt-cuda-wheels.yml+++ b/.github/workflows/prebuilt-cuda-wheels.yml@@ -37,7 +37,7 @@ on:type: stringdefault: '3.13'packages:- description: 'packages, comma separated (flash-attn, causal-conv1d, mamba-ssm)'+ description: 'packages, comma separated (flash-attn, causal-conv1d, mamba-ssm; mamba-ssm also builds its extension-free py3-none-any wheel)'type: stringdefault: 'flash-attn,causal-conv1d,mamba-ssm'platforms:@@ -83,6 +83,8 @@ jobs:warm_count: ${{ steps.plan.outputs.warm_count }}gpu_matrix: ${{ steps.plan.outputs.gpu_matrix }}gpu_count: ${{ steps.plan.outputs.gpu_count }}+ pure_matrix: ${{ steps.plan.outputs.pure_matrix }}+ pure_count: ${{ steps.plan.outputs.pure_count }}steps:- name: Harden runner (egress block)uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1@@ -455,6 +457,15 @@ jobs:if: runner.os == 'Windows' && matrix.package == 'mamba-ssm'run: python unsloth/.github/scripts/patch_mamba_msvc.py src+ # Drop causal-conv1d's and mamba-ssm's -lineinfo (same SASS, half the size), their SASS for+ # the integrated Arm GPUs on x86 wheels (sm_87 / 110 / 121), and flash-attn's PTX copy of its+ # newest arch. Asserts on the text it edits, like the mamba patches above.+ - name: Trim the CUDA targets+ env:+ PACKAGE: ${{ matrix.package }}+ CUDA_ARCHS: ${{ matrix.cuda_archs }}+ run: python unsloth/.github/scripts/patch_cuda_targets.py --package "$PACKAGE" --archs "$CUDA_ARCHS" src/setup.py+- name: Restore this slice's ccache from earlier runsuses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0with:@@ -838,6 +849,15 @@ jobs:if: runner.os == 'Windows' && matrix.package == 'mamba-ssm'run: python unsloth/.github/scripts/patch_mamba_msvc.py src+ # Drop causal-conv1d's and mamba-ssm's -lineinfo (same SASS, half the size), their SASS for+ # the integrated Arm GPUs on x86 wheels (sm_87 / 110 / 121), and flash-attn's PTX copy of its+ # newest arch. Asserts on the text it edits, like the mamba patches above.+ - name: Trim the CUDA targets+ env:+ PACKAGE: ${{ matrix.package }}+ CUDA_ARCHS: ${{ matrix.cuda_archs }}+ run: python unsloth/.github/scripts/patch_cuda_targets.py --package "$PACKAGE" --archs "$CUDA_ARCHS" src/setup.py+- name: Download the warm slicesif: matrix.shards > 0uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1@@ -938,6 +958,16 @@ jobs:WHEEL_NAME: ${{ matrix.wheel_name }}run: python ../unsloth/.github/scripts/prebuilt_wheels.py size-gate --package "$PACKAGE" "dist/${WHEEL_NAME}"+ # The trim above only edits text; this proves the binary. Every target sm_XX has SASS, none+ # other does, and there is no PTX: a missing cubin would fail at the first kernel launch on+ # that GPU, which neither a CPU runner nor the single-GPU gpu-smoke job would notice.+ - name: Check the device code targets+ working-directory: src+ env:+ CUDA_ARCHS: ${{ matrix.cuda_archs }}+ WHEEL_NAME: ${{ matrix.wheel_name }}+ run: python ../unsloth/.github/scripts/prebuilt_wheels.py targets-gate --archs "$CUDA_ARCHS" "dist/${WHEEL_NAME}"+# The whole reason this release exists, so it is a gate and not a report. A fresh venv with# the matching torch, the wheel installed from the file, and the compiled extension# imported out of process. An upstream wheel on torch 2.13 dies exactly here, with@@ -1040,6 +1070,140 @@ jobs:# the ABI failure this release exists to avoid; this catches the rarer one where the wheel# imports but carries no cubin the card can run. It is a separate job because the build runners# have no GPU and the self-hosted one is shared with the Docker smoke tests.+ # mamba-ssm without its CUDA extension: one py3-none-any wheel for every torch, Python and OS,+ # for the Mamba-2 hybrids, whose causal conv unsloth-zoo runs in Triton (see pure_matrix in+ # prebuilt_wheels.py). No toolkit, no GPU: the build is setuptools copying Python files, and the+ # gates prove that is all it did. Built whenever the run builds mamba-ssm.+ pure:+ name: ${{ matrix.package }} without its CUDA extension+ needs: plan+ if: ${{ !cancelled() && needs.plan.outputs.pure_count != '0' }}+ runs-on: ubuntu-latest+ timeout-minutes: 30+ permissions:+ contents: read+ strategy:+ matrix: ${{ fromJSON(needs.plan.outputs.pure_matrix) }}+ steps:+ - name: Harden runner (audit)+ uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1+ with:+ egress-policy: audit++ - name: Checkout Unsloth (build scripts only)+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1+ with:+ path: unsloth+ persist-credentials: false+ sparse-checkout: |+ .github/scripts+ sparse-checkout-cone-mode: false++ - name: Checkout ${{ matrix.repo }}+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1+ with:+ repository: ${{ matrix.repo }}+ ref: ${{ matrix.ref }}+ path: src+ persist-credentials: false++ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0+ with:+ python-version: '3.13'++ # setup.py imports torch at the top, so the build environment needs one; the CPU build is+ # enough because nothing is compiled. MAMBA_KEEP_CUDA_BUILD unset is what keeps the+ # extension out (FORCE_BUILD off too, so it never fetches upstream's CUDA wheel instead).+ # SOURCE_DATE_EPOCH = the pinned commit's time, so a rebuild gives the same bytes.+ - name: Build the wheel+ env:+ TORCH_SPEC: torch==${{ matrix.smoke_torch }}+ MAMBA_SKIP_CUDA_BUILD: 'TRUE'+ MAMBA_FORCE_BUILD: 'FALSE'+ MAMBA_KEEP_CUDA_BUILD: 'FALSE'+ run: |+ set -euo pipefail+ python -m pip install --quiet --upgrade pip+ python -m pip install --quiet "$TORCH_SPEC" --index-url https://download.pytorch.org/whl/cpu+ python -m pip install --quiet setuptools wheel packaging ninja+ export SOURCE_DATE_EPOCH="$(git -C src log -1 --format=%ct)"+ python -m pip wheel ./src --no-deps --no-build-isolation -w dist+ ls -l dist++ - name: Check the wheel is pure+ env:+ WHEEL_NAME: ${{ matrix.wheel_name }}+ run: python unsloth/.github/scripts/prebuilt_wheels.py pure-gate "dist/${WHEEL_NAME}"++ # The Triton ops Mamba-2 uses, imported from the installed wheel in a fresh venv with+ # --no-deps (tilelang / quack-kernels are Mamba-3 only). Import only: no GPU here.+ - name: Import it (Linux)+ env:+ WHEEL_NAME: ${{ matrix.wheel_name }}+ TORCH_SPEC: torch==${{ matrix.smoke_torch }}+ TRITON_SPEC: ${{ matrix.smoke_triton_linux }}+ SMOKE_DEPS: ${{ matrix.smoke_deps }}+ SMOKE_IMPORTS: ${{ matrix.smoke_imports }}+ run: |+ set -euo pipefail+ python -m venv smoke+ ./smoke/bin/python -m pip install --quiet --upgrade pip+ ./smoke/bin/python -m pip install --quiet "$TORCH_SPEC" --index-url https://download.pytorch.org/whl/cpu+ # shellcheck disable=SC2086+ ./smoke/bin/python -m pip install --quiet "$TRITON_SPEC" $SMOKE_DEPS+ ./smoke/bin/python -m pip install --quiet --no-deps "dist/${WHEEL_NAME}"+ cd /tmp && "$GITHUB_WORKSPACE/smoke/bin/python" -c "$SMOKE_IMPORTS; print(mamba_ssm.__file__, mamba_ssm.__version__)"++ - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1+ with:+ # wheel-* so the sign job's download picks it up with the CUDA wheels; no platform in+ # the name because the wheel has none.+ name: wheel-py3-none-any-mamba_ssm+ path: dist/${{ matrix.wheel_name }}+ if-no-files-found: error+ retention-days: 7++ pure-windows:+ name: ${{ matrix.package }} without its CUDA extension / import on Windows+ needs: [plan, pure]+ if: ${{ !cancelled() && needs.pure.result == 'success' }}+ runs-on: windows-latest+ timeout-minutes: 30+ permissions:+ contents: read+ defaults:+ run:+ shell: bash+ strategy:+ matrix: ${{ fromJSON(needs.plan.outputs.pure_matrix) }}+ steps:+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0+ with:+ python-version: '3.13'++ - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1+ with:+ name: wheel-py3-none-any-mamba_ssm+ path: dist++ - name: Import it (Windows, triton-windows)+ env:+ WHEEL_NAME: ${{ matrix.wheel_name }}+ TORCH_SPEC: torch==${{ matrix.smoke_torch }}+ TRITON_SPEC: ${{ matrix.smoke_triton_windows }}+ SMOKE_DEPS: ${{ matrix.smoke_deps }}+ SMOKE_IMPORTS: ${{ matrix.smoke_imports }}+ run: |+ set -euo pipefail+ python -m venv smoke+ ./smoke/Scripts/python.exe -m pip install --quiet --upgrade pip+ ./smoke/Scripts/python.exe -m pip install --quiet "$TORCH_SPEC" --index-url https://download.pytorch.org/whl/cpu+ # shellcheck disable=SC2086+ ./smoke/Scripts/python.exe -m pip install --quiet "$TRITON_SPEC" $SMOKE_DEPS+ ./smoke/Scripts/python.exe -m pip install --quiet --no-deps "dist/${WHEEL_NAME}"+ py="$(pwd)/smoke/Scripts/python.exe"+ cd "$RUNNER_TEMP" && "$py" -c "$SMOKE_IMPORTS; print(mamba_ssm.__file__, mamba_ssm.__version__)"+gpu-smoke:name: GPU forward passneeds: [plan, build]@@ -1084,8 +1248,13 @@ jobs:sign:name: Sign and attest- needs: [plan, build]- if: ${{ !cancelled() && needs.build.result == 'success' }}+ needs: [plan, build, pure, pure-windows]+ # The extension-free wheel is skipped when the run does not build mamba-ssm; when it is+ # built, both of its jobs must pass, like any other planned wheel.+ if: >-+ ${{ !cancelled() && needs.build.result == 'success'+ && (needs.plan.outputs.pure_count == '0'+ || (needs.pure.result == 'success' && needs.pure-windows.result == 'success')) }}runs-on: ubuntu-latesttimeout-minutes: 45# Named for the same reason release-desktop.yml and woa-wheelhouse.yml name it: it is what@@ -1125,9 +1294,11 @@ jobs:# plan job already counted the cells; this asserts the same number arrived.- name: Every planned wheel arrivedenv:- EXPECTED: ${{ needs.plan.outputs.count }}+ CUDA_COUNT: ${{ needs.plan.outputs.count }}+ PURE_COUNT: ${{ needs.plan.outputs.pure_count }}run: |set -euo pipefail+ EXPECTED=$(( CUDA_COUNT + ${PURE_COUNT:-0} ))ls -l wheels/actual="$(find wheels -name '*.whl' -type f | wc -l)"if [ "$actual" -ne "$EXPECTED" ]; thendiff --git a/tests/python/test_prebuilt_cuda_wheels_pure.py b/tests/python/test_prebuilt_cuda_wheels_pure.pynew file mode 100644index 00000000000..d60c9688366--- /dev/null+++ b/tests/python/test_prebuilt_cuda_wheels_pure.py@@ -0,0 +1,167 @@+# 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 extension-free mamba-ssm wheel: pure_matrix, pure-gate, notes and the workflow jobs."""++from __future__ import annotations++import importlib.util+import json+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"++_spec = importlib.util.spec_from_file_location("prebuilt_wheels", SCRIPTS / "prebuilt_wheels.py")+pw = importlib.util.module_from_spec(_spec)+_spec.loader.exec_module(pw)++NAME = "mamba_ssm-2.3.2.post1-py3-none-any.whl"+++def _wheel(+ tmp_path,+ name = NAME,+ extra = (),+ tag = "py3-none-any",+ purelib = "true",+):+ path = tmp_path / name+ with zipfile.ZipFile(path, "w") as archive:+ archive.writestr("mamba_ssm/__init__.py", '__version__ = "2.3.2.post1"\n')+ archive.writestr("mamba_ssm/ops/triton/ssd_combined.py", "")+ archive.writestr(+ "mamba_ssm-2.3.2.post1.dist-info/WHEEL",+ f"Wheel-Version: 1.0\nRoot-Is-Purelib: {purelib}\nTag: {tag}\n",+ )+ for member in extra:+ archive.writestr(member, b"\0")+ return path+++def test_name_follows_the_cuda_wheel_pin():+ assert pw.pure_wheel_name() == NAME+ assert pw.SPECS["mamba-ssm"]["version"] == "2.3.2.post1"+++ "packages, count",+ [+ ("", 1),+ ("mamba-ssm", 1),+ ("flash-attn,mamba-ssm", 1),+ ("flash-attn", 0),+ ("causal-conv1d", 0),+ ],+)+def test_one_cell_only_when_the_run_builds_mamba_ssm(packages, count):+ cells = pw.pure_matrix(packages)+ assert len(cells) == count+ if cells:+ cell = cells[0]+ assert cell["wheel_name"] == NAME+ # Same source as the CUDA wheel.+ assert (cell["repo"], cell["ref"]) == (+ pw.SPECS["mamba-ssm"]["repo"],+ pw.SPECS["mamba-ssm"]["ref"],+ )+ assert "ssd_combined import mamba_chunk_scan_combined" in cell["smoke_imports"]+++def test_matrix_writes_pure_outputs(tmp_path, monkeypatch):+ out = tmp_path / "out"+ monkeypatch.setenv("GITHUB_OUTPUT", str(out))+ monkeypatch.delenv("GITHUB_STEP_SUMMARY", raising = False)+ for packages, count in (("mamba-ssm", 1), ("flash-attn", 0)):+ out.write_text("")+ monkeypatch.setenv("UW_PACKAGES", packages)+ monkeypatch.setenv("UW_PLATFORMS", "linux_x86_64")+ assert pw.main(["matrix", "--github"]) == 0+ lines = dict(line.split("=", 1) for line in out.read_text().splitlines())+ assert lines["pure_count"] == str(count)+ assert len(json.loads(lines["pure_matrix"])["include"]) == count+ # The CUDA cells are unchanged by it.+ assert "py3-none-any" not in lines["matrix"]+++def test_gate_accepts_the_pure_wheel(tmp_path):+ assert pw.main(["pure-gate", str(_wheel(tmp_path))]) == 0+++ "kwargs, problem",+ [+ ({"extra": ["selective_scan_cuda.cpython-313-x86_64-linux-gnu.so"]}, "compiled code"),+ ({"extra": ["selective_scan_cuda.cp313-win_amd64.pyd"]}, "compiled code"),+ ({"name": "mamba_ssm-2.3.2.post1-cp313-cp313-linux_x86_64.whl"}, "expected"),+ ({"tag": "cp313-cp313-linux_x86_64"}, "not pure"),+ ({"purelib": "false"}, "not pure"),+ ],+)+def test_gate_refuses_anything_else(tmp_path, kwargs, problem):+ wheel = _wheel(tmp_path, **kwargs)+ assert any(problem in p for p in pw.check_pure_wheel(str(wheel)))+ assert pw.main(["pure-gate", str(wheel)]) == 1+++def test_notes_mention_it_and_sums_list_it():+ cuda = "mamba_ssm-2.3.2.post1+cu13torch2.13cxx11abiTRUE-cp313-cp313-linux_x86_64.whl"+ entries = [("a" * 64, cuda), ("b" * 64, NAME)]+ notes = pw.render_notes(entries, tag = "t", repo = "o/r")+ assert "without its CUDA extension" in notes and NAME in notes+ assert "mamba-ssm 2.3.2.post1" in notes+ assert NAME in pw.render_sums(entries)+ only = pw.render_notes([("b" * 64, NAME)], tag = "t", repo = "o/r")+ assert NAME in only and "No wheels" not in only+ assert "without its CUDA extension" not in pw.render_notes(+ [("a" * 64, cuda)], tag = "t", repo = "o/r"+ )++[email protected](scope = "module")+def workflow():+ return yaml.safe_load(WORKFLOW.read_text(encoding = "utf-8"))+++def test_pure_jobs_are_planned_and_gated(workflow):+ jobs = workflow["jobs"]+ outputs = jobs["plan"]["outputs"]+ assert "pure_matrix" in outputs and "pure_count" in outputs+ pure = jobs["pure"]+ assert "pure_count != '0'" in pure["if"]+ assert pure["runs-on"] == "ubuntu-latest"+ steps = {step.get("name", ""): step for step in pure["steps"]}+ build = steps["Build the wheel"]+ assert build["env"]["MAMBA_KEEP_CUDA_BUILD"] == "FALSE"+ assert build["env"]["MAMBA_FORCE_BUILD"] == "FALSE"+ assert "pure-gate" in steps["Check the wheel is pure"]["run"]+ assert "--no-deps" in steps["Import it (Linux)"]["run"]+ upload = next(step for step in pure["steps"] if "upload-artifact" in step.get("uses", ""))+ # The sign job downloads wheel-*; the Windows import job downloads the same name.+ assert upload["with"]["name"] == "wheel-py3-none-any-mamba_ssm"+ windows = jobs["pure-windows"]+ assert windows["runs-on"] == "windows-latest"+ assert any("triton_windows" in str(step.get("env", {})) for step in windows["steps"])+ download = next(+ step for step in windows["steps"] if "download-artifact" in step.get("uses", "")+ )+ assert download["with"]["name"] == upload["with"]["name"]+++def test_sign_waits_for_the_pure_wheel_only_when_planned(workflow):+ sign = workflow["jobs"]["sign"]+ assert {"pure", "pure-windows"} <= set(sign["needs"])+ condition = sign["if"]+ assert "pure_count == '0'" in condition+ assert "needs.pure.result == 'success'" in condition+ assert "needs.pure-windows.result == 'success'" in condition+ arrived = next(+ step for step in sign["steps"] if step.get("name") == "Every planned wheel arrived"+ )+ assert "PURE_COUNT" in arrived["env"] and "CUDA_COUNT" in arrived["env"]+ assert "CUDA_COUNT + ${PURE_COUNT:-0}" in arrived["run"]diff --git a/tests/python/test_prebuilt_cuda_wheels_targets.py b/tests/python/test_prebuilt_cuda_wheels_targets.pynew file mode 100644index 00000000000..8f532fd2e07--- /dev/null+++ b/tests/python/test_prebuilt_cuda_wheels_targets.py@@ -0,0 +1,381 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""SASS trimming for the prebuilt CUDA wheels: patch_cuda_targets.py, cuda_archs, targets-gate."""++from __future__ import annotations++import importlib.util+import os+import stat+import subprocess+import sys+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, 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", SCRIPTS / "prebuilt_wheels.py")+sys.modules["patch_cuda_targets"] = _load("patch_cuda_targets", SCRIPTS / "patch_cuda_targets.py")++# The gencode block and nvcc list of causal-conv1d's setup.py at the pinned commit (mamba-ssm's is+# the same shape).+SSM_SETUP = """+def build():+ cc_flag = []+ if True:+ cc_flag.append("-gencode")+ cc_flag.append("arch=compute_75,code=sm_75")+ cc_flag.append("-gencode")+ cc_flag.append("arch=compute_80,code=sm_80")+ cc_flag.append("-gencode")+ cc_flag.append("arch=compute_87,code=sm_87")+ if bare_metal_version >= Version("11.8"):+ cc_flag.append("-gencode")+ cc_flag.append("arch=compute_90,code=sm_90")+ if bare_metal_version >= Version("12.8"):+ cc_flag.append("-gencode")+ cc_flag.append("arch=compute_100,code=sm_100")+ cc_flag.append("-gencode")+ cc_flag.append("arch=compute_120,code=sm_120")+ if bare_metal_version >= Version("13.0"):+ cc_flag.append("-gencode")+ cc_flag.append("arch=compute_103,code=sm_103")+ cc_flag.append("-gencode")+ cc_flag.append("arch=compute_110,code=sm_110")+ cc_flag.append("-gencode")+ cc_flag.append("arch=compute_121,code=sm_121")+ nvcc = [+ "-O3",+ "--use_fast_math",+ "--ptxas-options=-v",+ "-lineinfo",+ ] + cc_flag+ return nvcc+"""++FLASH_SETUP = """+def add_cuda_gencodes(cc_flag, archs, bare_metal_version):+ if "80" in archs:+ cc_flag += ["-gencode", "arch=compute_80,code=sm_80"]+ numeric = [a for a in archs if a.isdigit()]+ if numeric:+ newest = max(numeric, key=int)+ cc_flag += ["-gencode", f"arch=compute_{newest},code=compute_{newest}"]+ return cc_flag+"""+++def _patch(tmp_path, source, *args):+ setup = tmp_path / "setup.py"+ setup.write_text(source, encoding = "utf-8")+ result = subprocess.run(+ [sys.executable, str(SCRIPTS / "patch_cuda_targets.py"), *args, str(setup)],+ capture_output = True,+ text = True,+ )+ return result, setup+++def _gencodes(source: str) -> set[str]:+ return {+ line.split("code=sm_")[1].split('"')[0]+ for line in source.splitlines()+ if "code=sm_" in line and "cc_flag.append" in line+ }+++class TestSsmTrim:+ @pytest.mark.parametrize("package", ["causal-conv1d", "mamba-ssm"])+ def test_keeps_only_the_requested_sass_and_drops_lineinfo(self, tmp_path, package):+ result, setup = _patch(+ tmp_path, SSM_SETUP, "--package", package, "--archs", "75;80;90;100;120"+ )+ assert result.returncode == 0, result.stderr+ patched = setup.read_text(encoding = "utf-8")+ assert _gencodes(patched) == {"75", "80", "90", "100", "120"}+ assert '"-lineinfo"' not in patched+ # The CUDA 13 block lost every target; it must still be valid Python.+ compile(patched, "setup.py", "exec")+ assert "dropped ['87', '103', '110', '121']" in result.stdout++ def test_the_arm_list_only_drops_lineinfo(self, tmp_path):+ result, setup = _patch(+ tmp_path,+ SSM_SETUP,+ "--package",+ "causal-conv1d",+ "--archs",+ prebuilt_wheels.SSM_SASS_ARM,+ )+ assert result.returncode == 0, result.stderr+ patched = setup.read_text(encoding = "utf-8")+ assert patched == SSM_SETUP.replace(' "-lineinfo",\n', "")+ assert "dropped none" in result.stdout++ def test_the_x86_list(self, tmp_path):+ result, setup = _patch(+ tmp_path, SSM_SETUP, "--package", "mamba-ssm", "--archs", prebuilt_wheels.SSM_SASS+ )+ assert result.returncode == 0, result.stderr+ assert _gencodes(setup.read_text(encoding = "utf-8")) == {+ "75",+ "80",+ "90",+ "100",+ "103",+ "120",+ }++ def test_is_idempotent(self, tmp_path):+ result, setup = _patch(+ tmp_path, SSM_SETUP, "--package", "causal-conv1d", "--archs", "75;80;90;100;120"+ )+ assert result.returncode == 0+ once = setup.read_text(encoding = "utf-8")+ again = subprocess.run(+ [+ sys.executable,+ str(SCRIPTS / "patch_cuda_targets.py"),+ "--package",+ "causal-conv1d",+ "--archs",+ "75;80;90;100;120",+ str(setup),+ ],+ capture_output = True,+ text = True,+ )+ assert again.returncode == 0, again.stderr+ assert setup.read_text(encoding = "utf-8") == once++ def test_an_unknown_upstream_target_fails_loudly(self, tmp_path):+ source = SSM_SETUP.replace("compute_121,code=sm_121", "compute_130,code=sm_130")+ result, setup = _patch(+ tmp_path, source, "--package", "causal-conv1d", "--archs", "75;80;90;100;120"+ )+ assert result.returncode == 1+ assert "does not know" in result.stderr+ assert setup.read_text(encoding = "utf-8") == source++ def test_a_requested_target_upstream_dropped_fails(self, tmp_path):+ source = SSM_SETUP.replace(+ ' cc_flag.append("-gencode")\n cc_flag.append("arch=compute_75,code=sm_75")\n',+ "",+ )+ result, _ = _patch(+ tmp_path, source, "--package", "causal-conv1d", "--archs", "75;80;90;100;120"+ )+ assert result.returncode == 1+ assert "not in setup.py" in result.stderr++ def test_a_partial_list_is_neither_known_state(self, tmp_path):+ source = SSM_SETUP.replace(+ ' cc_flag.append("-gencode")\n cc_flag.append("arch=compute_121,code=sm_121")\n',+ "",+ )+ result, _ = _patch(+ tmp_path, source, "--package", "causal-conv1d", "--archs", "75;80;90;100;120"+ )+ assert result.returncode == 1+ assert "re-read it" in result.stderr++ def test_rejects_an_arch_outside_the_known_set(self, tmp_path):+ result, _ = _patch(tmp_path, SSM_SETUP, "--package", "causal-conv1d", "--archs", "75;86")+ assert result.returncode == 1+++class TestFlashTrim:+ def test_drops_the_ptx_and_keeps_the_sass(self, tmp_path):+ result, setup = _patch(tmp_path, FLASH_SETUP, "--package", "flash-attn")+ assert result.returncode == 0, result.stderr+ patched = setup.read_text(encoding = "utf-8")+ assert "code=compute_" not in patched+ assert 'cc_flag += ["-gencode", "arch=compute_80,code=sm_80"]' in patched+ compile(patched, "setup.py", "exec")+ again, _ = _patch(tmp_path, patched, "--package", "flash-attn")+ assert again.returncode == 0+ assert "already" in again.stdout++ def test_a_moved_ptx_line_fails(self, tmp_path):+ source = FLASH_SETUP.replace("code=compute_{newest}", "code=[compute_{newest}]")+ result, _ = _patch(tmp_path, source, "--package", "flash-attn")+ assert result.returncode == 1+++class TestCudaArchs:+ def _cells(self):+ cells = prebuilt_wheels.build_matrix(+ torches = "2.13.0,2.14.0", platforms = ",".join(prebuilt_wheels.PLATFORMS)+ )+ cells += prebuilt_wheels.build_matrix(+ torches = "2.10.0,2.11.0,2.12.0", pythons = "3.12", platforms = "win_amd64"+ )+ return cells++ def test_every_cell_names_its_targets(self):+ cells = self._cells()+ assert {cell["platform"] for cell in cells} == set(prebuilt_wheels.PLATFORMS)+ integrated_arm = {"87", "110", "121"}+ for cell in cells:+ archs = cell["cuda_archs"].split(";")+ if cell["package"] == "flash-attn":+ assert archs == ["80", "90", "100", "120"]+ elif cell["platform"] in ("linux_aarch64", "win_arm64"):+ # The full upstream list: only -lineinfo goes.+ assert set(archs) == set(+ importlib.import_module("patch_cuda_targets").SSM_KNOWN_ARCHS+ ), cell["label"]+ else:+ # Every GPU an x86 host can have keeps its own SASS, B300's sm_103 included.+ assert archs == ["75", "80", "90", "100", "103", "120"], cell["label"]+ assert not integrated_arm & set(archs)++ def test_flash_attn_targets_come_from_its_own_env(self):+ env = prebuilt_wheels.SPECS["flash-attn"]["env"]["FLASH_ATTN_CUDA_ARCHS"]+ assert prebuilt_wheels.cuda_archs("flash-attn", "linux_x86_64") == env++ def test_the_ccache_tag_changed_with_the_targets(self):+ """ccache would miss on the changed command lines anyway; a new tag keeps the cache key+ from restoring objects built for the old target list at all."""+ assert prebuilt_wheels.CCACHE_TAG != "fatbin-size"+++def _fatbin(*entries: tuple[int, int]) -> bytes:+ """A fatbin container holding (kind, arch) entries, kind 1 PTX / 2 ELF, laid out like nvcc's."""+ body = b""+ for kind, arch in entries:+ payload = bytes(16)+ header = bytearray(64)+ header[0:2] = kind.to_bytes(2, "little")+ header[4:8] = (64).to_bytes(4, "little")+ header[8:16] = len(payload).to_bytes(8, "little")+ header[28:32] = arch.to_bytes(4, "little")+ body += bytes(header) + payload+ return (+ (0xBA55ED50).to_bytes(4, "little")+ + (1).to_bytes(2, "little")+ + (16).to_bytes(2, "little")+ + len(body).to_bytes(8, "little")+ + body+ )+++ELF, PTX = 2, 1+X86 = (75, 80, 90, 100, 120)+++def _so(*entries: tuple[int, int], containers: int = 2) -> bytes:+ """Host bytes around the fatbin, split over several containers as a real extension is."""+ pieces = [b"\x7fELF" + bytes(100)]+ for i in range(containers):+ pieces.append(_fatbin(*entries[i::containers]) + bytes(37))+ return b"".join(pieces)+++class TestTargetsGate:+ def test_reads_every_container(self):+ data = _so(*((ELF, a) for a in X86), (PTX, 120))+ sass, ptx = prebuilt_wheels.device_targets(data)+ assert sass == {"75", "80", "90", "100", "120"}+ assert ptx == {"120"}++ def test_ignores_a_stray_magic(self):+ stray = (0xBA55ED50).to_bytes(4, "little") + bytes(4) + (2**40).to_bytes(8, "little")+ data = stray + _so((ELF, 80), (ELF, 90))+ assert prebuilt_wheels.device_targets(data) == ({"80", "90"}, set())++ def test_no_device_code(self):+ assert prebuilt_wheels.device_targets(b"\x7fELF" + bytes(4096)) == (set(), set())++ def test_check_targets(self):+ want = {"75", "80", "90", "100", "120"}+ assert prebuilt_wheels.check_targets("x", want, set(), want) == []+ problems = prebuilt_wheels.check_targets("x", {"75", "80", "87"}, {"120"}, want)+ assert any("no SASS for sm_90, sm_100, sm_120" in p for p in problems)+ assert any("sm_87 was not asked for" in p for p in problems)+ assert any("PTX for compute_120" in p for p in problems)++ def _wheel(+ self,+ tmp_path,+ data,+ suffix = "cpython-313-x86_64-linux-gnu.so",+ ):+ wheel = tmp_path / "causal_conv1d-1.7.0-cp313-cp313-linux_x86_64.whl"+ with zipfile.ZipFile(wheel, "w") as archive:+ archive.writestr(f"causal_conv1d_cuda.{suffix}", data)+ archive.writestr("causal_conv1d/__init__.py", b"")+ return wheel++ @pytest.mark.parametrize(+ "archs, extra, status",+ [+ ("75;80;90;100;120", (), 0),+ ("75;80;90;100;110;120", (), 1),+ ("75;80;90;100;120", ((ELF, 121),), 1),+ ("75;80;90;100;120", ((PTX, 120),), 1),+ ],+ )+ def test_gate(self, tmp_path, archs, extra, status):+ wheel = self._wheel(tmp_path, _so(*((ELF, a) for a in X86), *extra))+ assert prebuilt_wheels.main(["targets-gate", "--archs", archs, str(wheel)]) == status++ def test_reads_a_windows_pyd(self, tmp_path):+ data = b"MZ" + bytes(200) + _fatbin(*((ELF, a) for a in X86)) + bytes(64)+ wheel = self._wheel(tmp_path, data, suffix = "cp313-win_amd64.pyd")+ assert (+ prebuilt_wheels.main(["targets-gate", "--archs", "75;80;90;100;120", str(wheel)]) == 0+ )++ def test_an_extension_with_no_device_code_fails(self, tmp_path):+ wheel = self._wheel(tmp_path, b"\x7fELF" + bytes(4096))+ assert prebuilt_wheels.main(["targets-gate", "--archs", "75;80", str(wheel)]) == 1++[email protected](scope = "module")+def workflow():+ return yaml.safe_load(WORKFLOW.read_text(encoding = "utf-8"))+++class TestWorkflowWiring:+ @pytest.mark.parametrize("job", ["warm", "build"])+ def test_trim_runs_after_the_source_patches_and_before_compiling(self, workflow, job):+ steps = workflow["jobs"][job]["steps"]+ names = [s.get("name") for s in steps]+ trim = names.index("Trim the CUDA targets")+ assert names.index("Patch mamba-ssm for the MSVC host compiler (Windows)") < trim+ compile_step = (+ "Build the wheel"+ if job == "build"+ else ("Compile slice ${{ matrix.shard }} of ${{ matrix.shards }}")+ )+ assert trim < names.index(compile_step)+ step = steps[trim]+ # Unconditional: every package and platform builds trimmed, or the gate below fails.+ assert "if" not in step+ assert step["env"]["CUDA_ARCHS"] == "${{ matrix.cuda_archs }}"+ assert "patch_cuda_targets.py" in step["run"]++ def test_the_targets_gate_runs_before_the_wheel_leaves_the_build(self, workflow):+ steps = workflow["jobs"]["build"]["steps"]+ names = [s.get("name") for s in steps]+ gate = names.index("Check the device code targets")+ assert names.index("Check the extension size") < gate+ assert gate < names.index("Smoke test the wheel in a fresh venv")+ assert "if" not in steps[gate]+ assert "targets-gate" in steps[gate]["run"]+ assert steps[gate]["env"]["CUDA_ARCHS"] == "${{ matrix.cuda_archs }}"