Repository navigation
Studio: a masked NVIDIA GPU must not install the CPU bundle over CUDA - #10909
Conversation
has_usable_nvidia is visible_device_tokens != [] on both probe paths, so a host with has_physical_nvidia and not has_usable_nvidia is exactly one that has an NVIDIA GPU and an empty CUDA_VISIBLE_DEVICES. That host reached the CPU branch of _linux_published_attempts and was handed linux-cpu. The mask is scoped to one process. The install is not: activate_install_tree replaces the tree in place with no backup, so a single masked run left a CUDA machine on the CPU bundle permanently. Return an empty attempt list and let the caller source-build, which is what the ROCm branch already does, and name the reason in the log. Measured on a 5x B200 host: CUDA_VISIBLE_DEVICES='' resolved app-b10909-mix-bea84f7-linux-x64-cpu.tar.gz with prebuilt_available true; it now resolves prebuilt_available false with the mask named. Unmasked resolution is unchanged.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
Security findingsBlocking findings (1)
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
nvidia-smi -L, --query-gpu=compute_cap and the driver CUDA version are driver queries and keep answering under an empty CUDA_VISIBLE_DEVICES, so compute_caps and driver_cuda_version are fully populated and the normal coverage match works. Installing the CUDA bundle is therefore strictly better than returning an empty attempt list: no source build, no nvcc requirement, and the mask still does its job at run time because the CUDA build simply sees no devices and runs on CPU. ROCm keeps precedence and an explicit CPU request still bypasses this entirely.
There was a problem hiding this comment.
🛡️ Codex Security Review · Automatically triggered
Here are some automated security review suggestions for this pull request.
Reviewed commit: 5399903419
ℹ️ About Codex security reviews in GitHub
This is an experimental Codex feature. Security reviews are triggered when:
- You comment "@codex security review"
- A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review
Once complete, Codex will leave suggestions, or a comment if no findings are found.
| "NVIDIA GPU present but hidden by CUDA_VISIBLE_DEVICES=" | ||
| f"{host.visible_cuda_devices!r}; not installing the CPU bundle over it" | ||
| ) | ||
| return attempts |
There was a problem hiding this comment.
🛡️ Codex Security Review · Automatically triggered
Security: Verify masked-GPU source fallbacks
When Studio is installed on Linux with a physically present but masked NVIDIA GPU, no reusable source build, and Git/CMake available, this return forces exit 2. setup.sh then clones ggml-org/llama.cpp at a mutable release tag and immediately runs its CMake configuration without verifying a commit, signature, or content hash. An upstream compromise or retag can therefore execute code as the installer user, whereas the parent selected the checksum-approved CPU archive. Resolve and verify the manifest's pinned source commit/archive before entering this fallback.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The description of setup.sh is accurate but the attribution is not, so I am not changing this PR for it.
setup.sh only ever clones by ref. _RESOLVED_SOURCE_REF_KIND is tag or branch on every user-reachable path (studio/setup.sh:2613, :3098, :3119, :3122); the commit and pull arms are gated behind _LLAMA_PR, a developer flag. So an unverified git clone --depth 1 --branch <tag> https://github.com/ggml-org/llama.cpp.git is the shape of the from-scratch source build for every host that reaches it, and has been since long before this branch.
What this PR changes is which hosts count as NVIDIA, not what an NVIDIA host gets. The empty-attempts-then-source-build outcome is the existing deliberate policy for a Linux NVIDIA host whose CUDA selection produced nothing (a3ca5d2a5, #6310, "don't silently fall back to a CPU prebuilt on NVIDIA Linux GPU hosts") and for a ROCm host with no matching gfx bundle, and it is also where any host lands when a prebuilt download or preflight fails. A masked NVIDIA host is the same hardware as a visible one; giving it the CPU archive instead is the bug this PR fixes, because activate_install_tree replaces the tree in place and the mask is scoped to one process.
The residual class is narrow: physically present NVIDIA, hidden by the mask, and a release shipping no CUDA bundle covering its compute cap. nvidia-smi -L, --query-gpu=compute_cap and the driver CUDA version all keep answering under an empty mask, so compute_caps is populated and the normal coverage match applies; every fork release since b9585 ships CUDA bundles for x86_64 and arm64. Making that one sub-case take the CPU archive would leave masked hosts behaving differently from visible ones for no security gain, since the visible host already takes the same clone.
Verifying the pinned source is worth doing, but it belongs in setup.sh and covers every source build, not this branch. The machinery is already there on the Python side: source_build_plan_for_release and preferred_source_archive resolve repo + commit + sha256 from the approved checksum asset, and validate fails closed via require_approved_source_hash and the "exact source archive without a source repo" guard. Tracking that separately rather than widening this PR.
Revised after simulating this, and the first version was wrongThe original commit returned an empty attempt list for a masked host, forcing a source build. That is worse than it needed to be, and I only found out by measuring rather than reasoning.
So This is safe because the CUDA build degrades to CPU by itself when no device is visible. Verified with the real Correct output either way. The mask still does its job at run time, and the install is right for the next unmasked run, which a CPU bundle would not have been. Blast radiusI built a base-vs-head matrix over the cartesian product of [Linux, WSL, Windows, macOS] x [NVIDIA, AMD, Intel, CPU-only] with their sub-conditions (masked, Blackwell sm_120, ancient sm_61, NVIDIA + AMD iGPU, ROCm covered and uncovered, arm64, Apple Silicon, Intel Mac), 44 scenarios, running the real 4 of 44 scenarios change, and all four are the masked NVIDIA host:
Everything else is byte-identical, including every ROCm, Vulkan, Intel, CPU-only, arm64 and macOS row. Kept deliberately, and asserted:
Parser edge cases
Every value lands on a CUDA bundle. None regress to CPU. Existing installsI wrote real
A wrongly-downgraded install records
|
Comment-only pass over what this branch adds. Measured numbers and the mechanism write-ups move to the PR description, where they belong; what stays is the invariants the code does not state on its own.
|
@codex security review |
…source Two holes in the masked-NVIDIA path, both of which left the machine on CPU by a different route. An emptied CUDA_VISIBLE_DEVICES makes select_visible_gpu_rows drop every nvidia-smi row, so host.compute_caps comes back empty and the selector takes its unknown-SM path, which accepts a portable artifact without checking min_sm or max_sm. A masked sm_61 host would install a bundle whose floor is sm_70 and still have no offload once the mask came off. HostInfo now carries physical_compute_caps, recorded before the mask filter, and the masked branch selects against those. Measured on a 5x B200 box: the masked resolve went from cuda13-portable to cuda13-newer, which is exactly what the same host resolves unmasked. When no CUDA bundle matches, the empty attempt list is meant to hand the caller a CUDA source build. setup.sh gated its nvcc search on _setup_nvidia_usable, which is false under the mask by construction, so it compiled a CPU-only binary and activated it over the tree: slower than the CPU prebuilt and just as permanent. The gate is now _setup_nvidia_physical. A toolkit with no GPU at all is still refused, which is what that guard was written for; a card hidden by a mask is still a card. _setup_has_usable_nvidia_gpu is written in terms of the new physical probe so the two cannot drift. Tests: a masked sm_61 host is not handed an sm_70 floor (fails without the change), the masked hosts in the existing tests now look like detect_host really leaves them, and the physical probe is exercised across unset, "", -1 and 0. The setup.sh source-level tests follow the gate and the delegation. tests/studio/install/ + test_install_resolve_prebuilt.py: 4344 passed, 7 skipped.
…D one
Regression from the previous commit. Gating the nvcc search on physical NVIDIA
alone meant a mixed AMD+NVIDIA host that hid NVIDIA via CUDA_VISIBLE_DEVICES
built CUDA for the card the user hid: the AMD probes run whenever NVIDIA is not
usable, so _setup_amd_detected is set on that host, and the ROCm branch below
only fires while GPU_BACKEND is still empty.
The masked arm now also requires that no AMD GPU was detected. A USABLE NVIDIA
card still wins outright, which is the existing NVIDIA-priority rule, and a CUDA
toolkit with no GPU at all is still refused.
TestSetupShSourceBuildBackendChoice runs the gate expression as a truth table
over the six host classes that reach it:
visible NVIDIA, no AMD -> cuda
visible NVIDIA + AMD -> cuda
masked NVIDIA, no AMD -> cuda
masked NVIDIA + usable AMD -> rocm
AMD only -> rocm
no GPU at all -> cpu
The source-level assertion in TestSetupShHardening followed: it forbade any
mention of _setup_nvidia_usable in the window, which the correct gate needs.
tests/studio/install/ + test_install_resolve_prebuilt.py: 4350 passed, 7 skipped.
The AMD carve-out from the previous commit checked DETECTION only, while hipcc
availability is checked later. So a mixed host with NVIDIA hidden, an AMD GPU
detected by rocminfo, and no hipcc anywhere skipped the nvcc search, left
GPU_BACKEND empty in the ROCm block, and took a CPU source build with nvcc
sitting right there, activated over the tree for good.
The carve-out was the wrong shape, so it is gone rather than patched. The nvcc
search is one _select_nvcc function called twice, in the order the decision
wants: usable NVIDIA first (the original rule, restored verbatim), then the ROCm
block unchanged, then a retry for a physically present card while GPU_BACKEND is
still empty. A masked card yields to a visible AMD GPU only when ROCm can really
be selected, and reclaims the build when it cannot. A CUDA toolkit with no GPU at
all is still refused.
TestSetupShSourceBuildBackendChoice now extracts the whole block and runs it as a
real bash sequence with the two toolchain lookups stubbed, rather than testing
one gate expression in isolation:
usable NVIDIA, no AMD -> cuda
usable NVIDIA + AMD + hipcc -> cuda
masked NVIDIA, no AMD -> cuda
masked NVIDIA + AMD + hipcc -> rocm
masked NVIDIA + AMD, no hipcc -> cuda
AMD only + hipcc -> rocm
nvcc present, no GPU at all -> cpu
Exactly one row, the fifth, fails on the previous revision.
Merged origin/main: the branch was ~40 commits behind and CI was failing four
staged-root shell checks that pass on main and on the merge.
tests/studio/install/ + test_install_resolve_prebuilt.py: 4701 passed, 19
skipped. tests/sh/test_setup_staged_root.sh: 36 passed.
Both gates in detect_torch_cuda_runtime_preference answer "no GPU" under an
emptied CUDA_VISIBLE_DEVICES: has_usable_nvidia is false by definition, and
torch.cuda.is_available() sees no devices. So the masked branch got no
preference and linux_cuda_choice_from_release fell back to newest-first,
selecting a CUDA 13 bundle for a cu12 venv. That is the stray-runtime mismatch
the unmasked path exists to avoid, reintroduced by the very branch that is
supposed to make a masked host behave like an unmasked one.
detect_torch_cuda_runtime_preference takes a gpu_hidden_by_mask opt-in for a
caller that has already established a physically present card behind the mask.
It bypasses those two gates and nothing else: torch.version.cuda is a build-time
constant no mask touches, and it is exactly the question being asked. Every
other caller is unchanged by default.
Measured on the 5x B200 host, torch 2.11.0+cu128:
torch_cuda_preference: GPU hidden by CUDA_VISIBLE_DEVICES; reading
torch.version.cuda without the availability check
torch_cuda_preference: selected runtime_line=cuda12 from torch.version.cuda=12.8
and the masked resolve is byte-for-byte the unmasked one.
Two tests: the preference is skipped without the opt-in and returns cuda12 with
it on a masked host whose torch.cuda.is_available() is False, and the masked
branch is pinned to actually pass it.
tests/studio/install/ + test_install_resolve_prebuilt.py: 4703 passed, 19
skipped. tests/sh/test_setup_staged_root.sh: 36 passed.
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Problem
On a host with NVIDIA GPUs, an empty
CUDA_VISIBLE_DEVICESmakes the installer choose the CPU llama.cpp bundle and install it over the CUDA one.Measured on a 5x B200 box with a healthy driver and a working
nvidia-smi, changing nothing but the mask:Note
prebuilt_available: trueon the second one. This is not a plan that fails and falls back; it is a CPU bundle that gets downloaded and installed, andactivate_install_treereplaces the tree in place with no backup of the CUDA build.An empty mask is routine: schedulers, containers, CI steps and
unslothinvocations that want to stay off the GPU all set it. The mask is scoped to one process. The install it triggers is permanent and shared.Cause
has_usable_nvidiaisvisible_device_tokens != []on both probe paths, thenvidia-smi -Lone and the/proc/driver/nvidia/gpusrescue. Sohas_physical_nvidia and not has_usable_nvidiameans exactly one thing: the GPU is present andCUDA_VISIBLE_DEVICESis empty._linux_published_attemptsbranches onhas_usable_nvidia, so that host lands in the finalelseand picks up the CPU bundle. The comment there already states the intended rule, it just keys on the wrong flag:A masked host is not a CPU-only host, but it reaches there all the same.
Change
Select CUDA as if unmasked, rather than the CPU bundle.
nvidia-smiis an NVML tool, not a CUDA application, so it keeps answering under an empty mask and the driver CUDA version is still readable. The compute caps are not, though:select_visible_gpu_rowsfilters the rows by the mask, sohost.compute_capscomes back empty, which is the selector's unknown-SM path and accepts a portable artifact without checkingmin_sm/max_sm. Measured on the masked B200 host:So
HostInfonow carriesphysical_compute_caps, recorded before the mask filter, and the masked branch selects against those.compute_capskeeps its meaning as the visible set, so nothing else changes. Without this, a masked sm_61 host would accept a bundle whose floor is sm_70 and still have no offload once the mask came off.The mask still does its job at run time, since the CUDA build simply sees no devices and runs on CPU exactly as the CPU bundle would. The difference is that the install stays correct for the next unmasked run.
The source-build leg
When no CUDA bundle matches, the empty attempt list is meant to hand the caller a CUDA source build.
studio/setup.shgated its nvcc search on_setup_nvidia_usable, which is false under the mask by construction, so it compiled a CPU-only binary and activated it over the tree: slower than the CPU prebuilt and just as permanent.setup.shnow has_setup_has_physical_nvidia_gpu, which is the old body minus the_setup_cvd_hides_nvidiaearly return, and_setup_has_usable_nvidia_gpuis written as "not hidden, and physical" so the two cannot drift. The nvcc search gates on_setup_nvidia_physical. A CUDA toolkit with no GPU at all is still refused, which is what that guard was written for; a card hidden by a mask is still a card.usableis unchanged from before the refactor.A masked card only claims the build when there is no AMD GPU to serve. The AMD probes run whenever NVIDIA is not usable, so a mixed host reaches the gate with
_setup_amd_detectedset, and the ROCm branch below only fires whileGPU_BACKENDis empty; without the carve-out, hiding the NVIDIA card would build CUDA for the card the user hid instead of HIP for the one they left visible. A usable NVIDIA card still wins outright, which is the existing NVIDIA-priority rule. The whole gate expression is extracted fromsetup.shand run as a truth table:The nvcc search is one
_select_nvccfunction called twice, in the order the decision wants: a usable card takes it first (the original rule, unchanged), then the ROCm block runs, then a physically present card retries whileGPU_BACKENDis still empty. So a masked card yields to a visible AMD GPU only when ROCm can really be selected, and reclaims the build when it cannot. Checking AMD detection alone was not enough, becausehipccavailability is settled later: a host with an AMD GPU and nohipccanywhere would otherwise have taken a CPU source build with nvcc sitting right there.The whole block is extracted and run as a real bash sequence with the two toolchain lookups stubbed:
The last row is the guard this was written for: a CUDA toolkit with no GPU at all is still refused.
Placement matters in two directions. The gate sits after the ROCm branch, so a masked NVIDIA host that also has usable ROCm still takes ROCm. It sits after the Vulkan branch, which is already gated on
not has_physical_nvidia, so an iGPU next to a masked NVIDIA card does not become a second route to a non-CUDA install. And an explicit CPU request never reaches it at all, because_apply_host_overridesclearshas_physical_nvidiafirst.There is precedent for this reading elsewhere in the tree:
install_python_stackalready refuses to conclude a torch flavor from an emptied mask, on the grounds that a mask is a reason not to conclude something from a probe rather than a fact about the hardware.Verification
On the same B200 host, after the change:
linux-cuda, cuda13-newerCUDA_VISIBLE_DEVICES=""linux-cpulinux-cuda, cuda13-newerThe masked pick is now byte-for-byte the unmasked pick. Before the physical-caps commit it resolved
cuda13-portablethrough the unknown-SM path, which is correct on sm_100 and wrong on an sm_61 host.I also ran the masked CUDA binary for real: it prints
ggml_cuda_init: failed to initialize CUDA: no CUDA-capable device is detectedand then serves on CPU at 1031 tok/s, against 3839 tok/s on CUDA0 with the mask lifted. So the masked run degrades exactly as the CPU bundle would, and the install is still right afterwards.Four tests in
TestLinuxPublishedAttemptsNvidiaCpuGateintests/studio/install/test_selection_logic.py: a masked NVIDIA host takes CUDA and not the CPU bundle, a masked NVIDIA host with no CUDA match falls through to the source build rather than the CPU bundle, a masked NVIDIA host with an Intel iGPU alongside it is not rescued onto Vulkan either, and a masked sm_61 host is not handed a bundle whose floor is sm_70. PlusTestSetupShPhysicalNvidiaSurvivesTheMask, which exercises the new shell probe across unset,"",-1and0. The existingtest_cpu_host_gets_cpu_attemptcovers the case that must not change, and still passes.A 44-scenario base-vs-head selection matrix over
[Windows, Linux, WSL, macOS] x [NVIDIA, AMD, Intel, CPU-only], built from the real b10909 asset list, moves exactly 4 rows: the masked Linux and WSL NVIDIA ones. Everything else is byte-identical.Existing installs are unaffected either way: a host that was already wrongly downgraded recorded
backend_request: auto, so the next update re-detects and heals itself, while a deliberatecpuorvulkanchoice stays honoured.tests/studio/,studio/backend/tests/test_install_resolve_prebuilt.pyandunsloth_cli/tests, after mergingmain: 11220 passed, 106 skipped.tests/sh/test_setup_staged_root.sh: 36 passed.Scope
Linux published-bundle selection. This is one of three separate paths that can end with a GPU machine on CPU; the other two are handled in #10906 and #10908.
Before / after in the UI
Two full Studio installs from this PR's merge base and its head, both run with
CUDA_VISIBLE_DEVICES=exported so the installer sees a host whose NVIDIA GPU is present and hidden. Settings > Resources > GGUF inference engine:llama.cpp currently runs on CPU.with the picker onAutomatic (CPU)becomesllama.cpp currently runs on CUDA.onAutomatic (CUDA). Read from the same two servers:backendcpu -> cuda, the auto option'sresolved_backendcpu -> cuda,available_backendsgainscuda, andbackend_requestisautowithenv_backendnull on both sides, so this is the automatic path and not a stored preference.The wider shot is the control. Both sides report
No visible GPU detected. CPU-only resources are shown above.under GPU devices andNo visible GPUfor VRAM, which is the mask doing its job on both halves; the head install selected CUDA anyway. The markers the two installs wrote agree:app-b10909-mix-bea84f7-linux-x64-cpu.tar.gzwithbackend: cpuagainstapp-b10909-mix-bea84f7-linux-x64-cuda13-newer.tar.gzwithbackend: cuda,force_cpu: falseon both.Both halves were built at
9f182473aand996974d46. The commits after that shot are the Torch runtime-preference fix, amainmerge and a comment pass; the masked resolve at the current head still selectsapp-b10909-mix-bea84f7-linux-x64-cuda13-newer.tar.gz, so what these screenshots show is unchanged.