Repository navigation
studio/setup.sh: cope with fresh CUDA toolkits like 13.3 - #5826
Conversation
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.
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
There was a problem hiding this comment.
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.
| _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 |
There was a problem hiding this comment.
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.
| _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 |
| _NVCC_STATUS="$(printf '%s\n' "$_NVCC_CHECK" | sed -n '1p')" | ||
| _NVCC_VER="$(printf '%s\n' "$_NVCC_CHECK" | sed -n '2p')" |
There was a problem hiding this comment.
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).
| _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.
|
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
CPU fallback (the generalized Metal to CPU change)
Runtime correctness on CUDA 13.3 (re: #4849)
Prebuilt path
Added in this branch
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.
|
Follow-up: extended the headline 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 Change: in the CUDA build branch of Validation (PowerShell 7.6.2 on Linux):
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 |
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.
…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.
…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]>
Summary
CUDA 13.3 shipped today and the reddit thread at https://www.reddit.com/r/unsloth/ shows the typical fallout:
unsloth studio updateproduces a wall of llama.cpp errors before silently falling back to a CPU-only build. Three small loose ends instudio/setup.shcover most of what users hit on the first day after a new CUDA toolkit ships.setup.ps1already 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), butsetup.shstill hit the generic cmake failure. New_nvcc_meets_llama_minimumhelper parsesnvcc -V, downgrades to CPU on< 12.4, and points to the toolkit archive.#error -- unsupported GNU versionbefore any real compile runs.NVCC_PREPEND_FLAGSnow carries-allow-unsupported-compilerso the build moves on. The env-var route keeps the flag out of$CMAKE_ARGSso bash word-splitting on the cmake call stays safe.BUILD_OK=falseand no llama.cpp at all. Generalised the existing Metal to CPU fallback to cover anyGPU_BACKEND, so a CUDA configure or build failure now transparently retries with the CPU args and the user still ends up with a workingllama-server.GPU_BACKENDis 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 intests/sh/test_get_torch_index_url.shcovering the legacyCUDA Version: 13.3header (driver-reported) and a future-proof 13.7. Wired intotests/run_all.shandstudio-backend-ci.yml.Staging validation
Cross-OS validation on danielhanchen#158: ubuntu-latest, macos-14, windows-latest all green (the new
_nvcc_meets_llama_minimumhelper parses cleanly under macOS bash 3.2, GNU bash 5 and git-bash on Windows).Test plan
bash tests/run_all.shlocallybash -n studio/setup.sh && bash -n install.shclean