Skip to content

studio/setup.sh: cope with fresh CUDA toolkits like 13.3 - #5826

Merged
danielhanchen merged 5 commits into
mainfrom
pr-cuda133-fix
May 29, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
pr-cuda133-fix

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

CUDA 13.3 shipped today and the reddit thread at https://www.reddit.com/r/unsloth/ shows the typical fallout: unsloth studio update produces a wall of llama.cpp errors before silently falling back to a CPU-only build. Three small loose ends in studio/setup.sh cover most of what users hit on the first day after a new CUDA toolkit ships.

  1. setup.ps1 already aborts cleanly when the CUDA toolkit is below llama.cpp's 12.4 minimum via PR: Fix/cuda minimum check and abort #4517 (issue [Bug] no minimum CUDA version check for llama.cpp #4437), but setup.sh still hit the generic cmake failure. New _nvcc_meets_llama_minimum helper parses nvcc -V, downgrades to CPU on < 12.4, and points to the toolkit archive.
  2. The day a new CUDA toolkit ships, its host-compiler whitelist lags whatever gcc/clang the distro is on, so nvcc rejects the host compiler with a wall of #error -- unsupported GNU version before any real compile runs. NVCC_PREPEND_FLAGS now carries -allow-unsupported-compiler so the build moves on. The env-var route keeps the flag out of $CMAKE_ARGS so bash word-splitting on the cmake call stays safe.
  3. The Linux CUDA/ROCm configure failure path had no symmetry with the macOS Metal fallback: a single nvcc failure left BUILD_OK=false and no llama.cpp at all. Generalised the existing Metal to CPU fallback to cover any GPU_BACKEND, so a CUDA configure or build failure now transparently retries with the CPU args and the user still ends up with a working llama-server. GPU_BACKEND is cleared after a successful CPU fallback so a later build-step failure does not re-enter the fallback against the already-CPU config.

The version probe is a standalone function so it can be unit-tested. Added tests/sh/test_nvcc_meets_llama_minimum.sh (12 cases covering 11.8 through 14.0, garbage output, and the empty-path defensive branch) and two cases in tests/sh/test_get_torch_index_url.sh covering the legacy CUDA Version: 13.3 header (driver-reported) and a future-proof 13.7. Wired into tests/run_all.sh and studio-backend-ci.yml.

Staging validation

Cross-OS validation on danielhanchen#158: ubuntu-latest, macos-14, windows-latest all green (the new _nvcc_meets_llama_minimum helper parses cleanly under macOS bash 3.2, GNU bash 5 and git-bash on Windows).

Test plan

  • bash tests/run_all.sh locally
  • bash -n studio/setup.sh && bash -n install.sh clean
  • Cross-OS shell test green on staging fork
  • studio-backend-ci.yml green on this PR

CUDA 13.3 shipped today. Three loose ends in studio/setup.sh surfaced
during the llama.cpp build path:

1. setup.ps1 already aborts cleanly when the CUDA toolkit is below
   llama.cpp's minimum (12.4) via #4517, but setup.sh still hit the
   generic cmake failure described in #4437. Added a min-version check
   that downgrades to a CPU build for nvcc < 12.4 with a clear message
   pointing to the toolkit archive.
2. The first day a new CUDA toolkit ships, its host-compiler whitelist
   lags whatever gcc/clang the distro is on, so nvcc rejects the host
   compiler with a wall of "#error -- unsupported GNU version" before
   any real compile runs. NVCC_PREPEND_FLAGS now carries
   -allow-unsupported-compiler so the build moves on instead.
3. The Linux CUDA/ROCm configure failure path had no symmetry with the
   macOS Metal fallback: a single nvcc failure left BUILD_OK=false and
   no llama.cpp at all. Generalised the existing Metal -> CPU fallback
   to cover any GPU_BACKEND, so a CUDA configure or build failure now
   transparently retries with the CPU args and the user still ends up
   with a working llama-server.

Pulled the version probe out into _nvcc_meets_llama_minimum so it can
be unit-tested. Added tests/sh/test_nvcc_meets_llama_minimum.sh and two
extra cases in tests/sh/test_get_torch_index_url.sh covering the legacy
"CUDA Version: 13.3" header (driver-reported) and the future 13.7
case. Wired the new test into tests/run_all.sh and the studio-backend
CI workflow.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a minimum version check for the CUDA toolkit (requiring version 12.4 or newer) on Linux to align with llama.cpp requirements and prevent silent build failures. It also includes fallback mechanisms to CPU builds when the CUDA toolkit is too old or when GPU configurations fail, along with corresponding unit tests. The review feedback highlights two main improvement opportunities: robustly handling potential leading zeros in version numbers to prevent Bash octal arithmetic errors, and optimizing the extraction of status and version lines from the helper function output by using a standard Bash read block instead of spawning multiple subshells.

Comment thread studio/setup.sh
Comment on lines +145 to +154
_maj=${_raw%%.*}
_min_raw=${_raw#*.}
_min=${_min_raw%%.*}
if [ "$_maj" -lt 12 ] 2>/dev/null; then
echo "too_old"
elif [ "$_maj" -eq 12 ] && [ "$_min" -lt 4 ] 2>/dev/null; then
echo "too_old"
else
echo "ok"
fi

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

In Bash, integer comparisons using [ with operators like -lt or -eq treat numbers with leading zeros (e.g., 08, 09) as octal values. If a version component contains a leading zero (such as 08 or 09), this causes a syntax error (value too great for base), which makes the comparison fail and can lead to incorrect logic (e.g., treating a version as ok when it should be rejected, or vice versa). Stripping leading zeros using parameter expansion ensures robust decimal arithmetic.

Suggested change
_maj=${_raw%%.*}
_min_raw=${_raw#*.}
_min=${_min_raw%%.*}
if [ "$_maj" -lt 12 ] 2>/dev/null; then
echo "too_old"
elif [ "$_maj" -eq 12 ] && [ "$_min" -lt 4 ] 2>/dev/null; then
echo "too_old"
else
echo "ok"
fi
_maj=${_raw%%.*}
_maj=${_maj#0}; _maj=${_maj:-0}
_min_raw=${_raw#*.}
_min=${_min_raw%%.*}
_min=${_min#0}; _min=${_min:-0}
if [ "$_maj" -lt 12 ]; then
echo "too_old"
elif [ "$_maj" -eq 12 ] && [ "$_min" -lt 4 ]; then
echo "too_old"
else
echo "ok"
fi

Comment thread studio/setup.sh
Comment on lines +1035 to +1036
_NVCC_STATUS="$(printf '%s\n' "$_NVCC_CHECK" | sed -n '1p')"
_NVCC_VER="$(printf '%s\n' "$_NVCC_CHECK" | sed -n '2p')"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Instead of spawning two subshells with printf and sed to extract the first and second lines of _NVCC_CHECK, you can use a standard Bash here-string (<<<) with a { read; read; } block. This is significantly more efficient because it runs entirely in-process without spawning any external commands, which is especially beneficial on platforms with high process-creation overhead like Windows (Git Bash).

Suggested change
_NVCC_STATUS="$(printf '%s\n' "$_NVCC_CHECK" | sed -n '1p')"
_NVCC_VER="$(printf '%s\n' "$_NVCC_CHECK" | sed -n '2p')"
{ read -r _NVCC_STATUS; read -r _NVCC_VER; } <<< "$_NVCC_CHECK"

…ported-compiler

The -allow-unsupported-compiler flag is the core of the fresh-CUDA-toolkit fix
(it lets nvcc accept a host gcc/clang newer than its release-time whitelist, so
CUDA 13.3 day-one builds do not abort on '#error -- unsupported GNU version'),
but it had no automated coverage. Add a source-pattern test asserting the flag
is present, delivered via NVCC_PREPEND_FLAGS so it also covers cmake's CUDA
compiler-id probe, and kept out of CMAKE_ARGS for bash word-splitting safety.
@danielhanchen

Copy link
Copy Markdown
Member Author

Validation notes. Hardware: RTX PRO 6000 Blackwell (sm_120 / compute 12.0), driver 580.82.07, real CUDA 13.3 toolkit (V13.3.33), gcc 16.

Build wall and the fix

  • Reproduced the exact failure on real CUDA 13.3 with a host gcc above 13.3's whitelist (gcc 16, ceiling is 15): nvcc/cmake abort with #error -- unsupported GNU version. With NVCC_PREPEND_FLAGS=-allow-unsupported-compiler the configure passes (CUDA compiler identification is NVIDIA 13.3.33 with host compiler GNU 16.0.1) and the full sm_120 build completes.
  • End to end through setup.sh (forced a real prebuilt miss so it falls through to the source build): on main the source build dies at the wall and finishes with "llama.cpp unavailable"; on this branch it builds a working native sm_120 llama-server.

CPU fallback (the generalized Metal to CPU change)

  • Forced a CUDA build the flag cannot rescue (broken host compiler): main ends with no binary; this branch logs "CUDA configure failed; retrying CPU build..." and produces a working CPU llama-server.

Runtime correctness on CUDA 13.3 (re: #4849)

  • Built llama.cpp for sm_120 with CUDA 13.3 and ran a quant sweep on GPU with greedy decoding: IQ4_XS, IQ4_NL, IQ3_XXS, IQ2_M, IQ2_XXS, Q2_K, Q3_K_M, Q4_K_M, Q5_K_M, Q6_K all coherent with flash attention on and off. GPU output is bit-identical to CPU for the low-bit IQ/K quants and there is no GPU-only gibberish, so the 13.2 dequant miscompile is resolved in 13.3. (Only 1-bit IQ1_M is weak, equally on CPU, which is quantization rather than a kernel bug.)

Prebuilt path

  • A normal update is unaffected: it resolves to the latest unsloth prebuilt (cuda13 fat binary with native sm_120), validates via the GPU smoke test, and runs. The source build is reached only via a pinned tag with no matching prebuilt (confirmed EXIT_FALLBACK), force-compile, or a prebuilt validation failure, which is exactly where this change helps.

Added in this branch

  • A source-pattern test asserting setup.sh exports NVCC_PREPEND_FLAGS=-allow-unsupported-compiler and keeps it out of CMAKE_ARGS. The headline fix previously had no automated coverage; the test fails on main and passes here.

Two related hardening items for the source-build path (empty arch detection producing a PTX-only binary, and the lack of a post-build GPU smoke test) are out of scope here and tracked in #5854.

…dows parity)

Mirror the Linux setup.sh headline fix from this PR on Windows. A freshly
released CUDA toolkit ships with a host-compiler whitelist that lags the
installed toolchain, so nvcc can reject the host with
"#error -- unsupported Microsoft Visual Studio version!" before any real
compile runs (the MSVC analogue of the gcc wall the Linux side hit on
CUDA 13.3). Set NVCC_PREPEND_FLAGS=-allow-unsupported-compiler in the CUDA
build branch so both cmake's configure-time CUDA compiler-id probe and the
cmake --build step proceed. The flag disables the host version check only and
is a no-op when the compiler is already supported.

Set via the process environment (not the $CmakeArgs array), after the
Refresh-Environment calls that re-sanitize CUDA env vars, and appended
idempotently to any value the user already set.

Validated with PowerShell 7.6.2: full setup.ps1 AST parse is clean and the
snippet is idempotent (empty -> set, existing -> append once, no duplicate).
Needs real Windows + CUDA CI to exercise the actual nvcc/MSVC build.

Adds test_setup_ps1_exports_allow_unsupported_compiler asserting the flag is
present, env-delivered, kept out of $CmakeArgs, and scoped to the CUDA-on branch.
@danielhanchen

Copy link
Copy Markdown
Member Author

Follow-up: extended the headline -allow-unsupported-compiler fix to the Windows installer (studio/setup.ps1) for parity.

Why: the same wall exists on Windows. A freshly released CUDA toolkit ships a host-compiler whitelist that lags the installed toolchain, so on Windows nvcc rejects the host with #error -- unsupported Microsoft Visual Studio version! (the MSVC analogue of the gcc wall the Linux side hit on CUDA 13.3). -allow-unsupported-compiler disables the gcc and MSVC host-version checks alike. Before this, a CUDA configure failure on Windows had no recovery: setup.ps1 set $BuildOk = $false and reported build failed ... continuing with LlamaCppDegraded = $true, leaving no llama.cpp at all.

Change: in the CUDA build branch of setup.ps1 (guarded by if ($HasNvidiaSmi -and $NvccPath)), set $env:NVCC_PREPEND_FLAGS = "-allow-unsupported-compiler" before the cmake configure + build. Set via the process environment (not the $CmakeArgs array) so it also covers cmake's configure-time CUDA compiler-id probe, placed after the Refresh-Environment re-sanitize block so it cannot be clobbered, and appended idempotently to any value the user already set. No-op when the toolchain is already supported, same safety profile as the Linux change.

Validation (PowerShell 7.6.2 on Linux):

  • Full setup.ps1 AST parse is clean (no syntax errors).
  • Snippet logic unit-tested: empty -> set, existing value -> append once, re-run -> no duplicate, already-present -> unchanged.
  • Added test_setup_ps1_exports_allow_unsupported_compiler (asserts the flag is present, env-delivered, kept out of $CmakeArgs, and scoped to the CUDA-on branch). Full test_pr4562_bugfixes.py suite green (53 passed).

Caveat: this is statically + logically validated only. I cannot exercise the real nvcc/MSVC build here (no Windows + CUDA). It needs a Windows + CUDA CI run, or a maintainer with that setup, to confirm end to end.

Out of scope (separate follow-up): Windows still lacks the generalized CUDA -> CPU build fallback that the Linux side gained here via _gpu_fallback_label. When a CUDA build fails for a reason the flag does not address (e.g. "No CUDA toolset found"), setup.ps1 reports degraded rather than retrying CPU-only. That is a larger structural change and is best done and validated on real Windows; tracking it alongside #5854.

Shorten the verbose multi-line comments and test docstrings introduced by
this PR (setup.sh, setup.ps1, and the shell/python tests) to be succinct
while preserving the rationale. No code or test-assertion changes.
@danielhanchen
danielhanchen merged commit a3a0cb1 into main May 29, 2026
35 checks passed
@danielhanchen
danielhanchen deleted the pr-cuda133-fix branch May 29, 2026 12:09
danielhanchen added a commit to oobabooga/unsloth that referenced this pull request May 31, 2026
…ows + Linux) (unslothai#5879)

Make the NVIDIA prebuilt selection version-dynamic and driver-gated on both the
Windows (ggml-org) and Linux (unslothai/llama.cpp) paths, so CUDA version bumps
are handled with no code change while staying safe across driver versions.

- Derive candidate CUDA runtime lines from the driver major; pick the highest
  upstream-published minor the driver can actually run, so a sub-13.3 driver is
  never handed an unguaranteed 13.3 build.
- Pin b9360's cuda-13.1 build (immutable, hash-verified) as a GPU fallback for a
  Blackwell host on a 13.1/13.2 driver that the in-release 13.3 build gates off,
  on both the simple and published install paths. Dormant for every other host
  and self-disabling once upstream ships a driver-runnable build again.
- Seed the published-path ordering from the release's real published minors so a
  future CUDA major is selectable with no code change.

Refs unslothai#5861, unslothai#5817, unslothai#5807, unslothai#5858, unslothai#5854, unslothai#5826, unslothai#5887.
danielhanchen added a commit that referenced this pull request Jun 23, 2026
…5854) (#6481)

* studio/setup.sh: guard empty CUDA arch detection in the source build

PR #5826 hardened setup.sh for fresh CUDA toolkits, but the source build
still set -DCMAKE_CUDA_ARCHITECTURES only when nvidia-smi reported a
compute capability. When that query returns nothing the build proceeded
with no explicit arch list, so llama.cpp built PTX only. On a driver older
than the toolkit that binary fails at runtime with "the provided PTX was
compiled with an unsupported toolchain" - the build succeeds, so neither
the build-time check nor the CPU fallback caught it (issue #5854).

Resolve the arch list before committing to a CUDA build. A new pure helper
_resolve_cuda_archs parses and de-duplicates the nvidia-smi compute_cap
output and honors an explicit UNSLOTH_LLAMA_CUDA_ARCHS override. When the
result is empty, build CPU llama.cpp instead of a PTX-only binary, with a
clear message pointing at the override - so the user still ends up with a
working llama-server. The override also lets advanced users force a native
build on hosts where nvidia-smi cannot report compute_cap.

No behavior change when an arch is detected: -DGGML_CUDA=ON plus the arch,
CUDA flags and NVCC_PREPEND_FLAGS are assembled exactly as before.

Adds tests/sh/test_resolve_cuda_archs.sh (single/multi/dedup/empty/garbage/
whitespace/override cases), wired into tests/run_all.sh and the
studio-backend-ci.yml shell-test loop.

* studio/setup.sh: resolve nvidia-smi via /usr/bin fallback for arch detection

Addresses review feedback on the empty-CUDA-arch guard: _setup_has_usable_nvidia_gpu
classifies a host as NVIDIA-usable using nvidia-smi on PATH OR /usr/bin/nvidia-smi,
but the new arch detection probed only `command -v nvidia-smi`. On a GPU host where
nvidia-smi is off PATH (reachable only at /usr/bin), arch detection returned empty
and the new empty-arch branch dropped the build to CPU, losing CUDA. Mirror the same
PATH-then-/usr/bin resolution so those hosts still get a native CUDA build.

Also scope _resolve_cuda_archs locals with `local` (no behavior change; it already
runs under command substitution).

* tests: update compute_cap-probe assertion for $_smi_bin resolution

The nvidia-smi /usr/bin fallback parameterized the binary in the compute_cap
probe (_setup_run_smi "$_smi_bin" ...), so the literal-string assertion in
test_compute_cap_probe_timeout_wrapped no longer matched. Assert the probe is
preceded by _setup_run_smi (timeout-wrapped) instead, scanning all occurrences
so the comment mention is ignored. Same intent, binary-agnostic.

* tests: ruff-format the compute_cap probe assertion (pre-commit)

Collapse the backslash-continued assert onto one line and normalize slice
spacing so the ruff-format pre-commit hook (0.6.9) is satisfied. Formatting
only; no behavior change.

* Tighten code comments (no logic change)

* studio(windows): build CPU when CUDA arch is undetectable (#5854)

The Windows source build added -DGGML_CUDA=ON unconditionally but only set
-DCMAKE_CUDA_ARCHITECTURES when $CudaArch was detected. With no detectable
compute capability that produced a PTX-only binary, the same hole the Linux
fix closed. Build CPU llama.cpp in that case, and honor UNSLOTH_LLAMA_CUDA_ARCHS
to force a CUDA build, matching setup.sh. Detected-arch builds are unchanged.

* test: anchor NVCC_PREPEND_FLAGS scope check on the final CPU branch

The undetectable-arch CPU fallback adds an earlier -DGGML_CUDA=OFF, so the
ordering check now anchors on -DGGML_CUDA=ON and the last -DGGML_CUDA=OFF
instead of the first.

---------

Co-authored-by: Daniel Han <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant