Skip to content

Say why Studio could not enumerate a GPU, instead of just logging [] - #10056

Merged
danielhanchen merged 10 commits into
mainfrom
studio-explain-empty-gpu-probe
Aug 31, 2026
Merged

danielhanchen merged 10 commits into
mainfrom
studio-explain-empty-gpu-probe

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

When Studio cannot enumerate a GPU it plans the load blind and hands placement to llama.cpp --fit, and the log records that it happened but never why.

Before

GGUF size: 16.4 GB, mmproj: 0.9 GB, est. KV cache: 12.4 GB, MTP reserve: 1.29 GB,
context: 262144, GPUs free: [], selected: None, --fit: on

_get_gpu_memory() returns [] for several unrelated reasons, and they need different fixes:

Reading [] you cannot tell which. _get_gpu_memory_amd_smi's own docstring lists several more ways to reach it.

After

Studio could not enumerate any GPU, so it planned this load blind and left placement to
llama.cpp --fit: torch reports no usable CUDA or HIP device (CUDA_VISIBLE_DEVICES is empty)

What this deliberately does not claim

It does not say the model will run on the CPU. llama.cpp enumerates devices itself and still receives --fit on, so it can place on a GPU this probe never saw. Earlier revisions of this PR did assert that, and it was wrong.

Three states are excluded, because an empty list there does not mean the probe failed:

state why excluded
both Manual modes they empty gpus on purpose to stand the planner down; _detected_gpus holds the real probe result
explicit gpu_ids pick restored into gpu_indices below this block and pinned for the child
Apple Silicon the probe reads CUDA and HIP only, so Metal offload is unaffected. An Intel Mac is not exempt, since auto_detect_backend falls back to CPU there

The arch gate already warns with a better, more actionable message when it is the cause, so that case is left to it.

Scope, honestly

This is a diagnostic. It changes no placement, adds no flag, no config and no state, so old installs are unaffected in both directions. It does not fix a reported failure: the log that prompted it turned out to be Manual mode standing the planner down, which this now correctly stays quiet about. The value is the visibility-mask and amd-smi-declined cases, which are currently unanswerable from a log.

The explainer never calls mem_get_info, so it does not create the HIP primary context the amd-smi branch exists to avoid; a test fails if it ever does.

Testing

Reproduced unmocked on a real CUDA host: an empty CUDA_VISIBLE_DEVICES and a mask pointing at an absent ordinal both drive the probe to [] and produce the right reason, while the normal case stays non-empty and silent.

Simulated in an isolated uv venv across [Linux, WSL, Windows, macOS] x [NVIDIA, AMD/ROCm, CPU only], plus 6 edge cases and 13 load-site cases. The guard is executed, not just read: it is cut out of llama_cpp.py by indentation and exec'd with controlled bindings, so the shipped text runs.

24 tests, mutation-checked; 776 passed across nine GPU suites.

A user reported a model running from system RAM. Their entire log said:

  GGUF size: 16.4 GB, ... GPUs free: [], selected: None, --fit: on
  Load mode: the whole load (34.3 GB) fits without paging, using
  --load-mode none.

That records the verdict and none of the reason, and the second line
reads like reassurance when it is only about disk paging. There was no
way to answer "why did my model go to RAM" from it.

_get_gpu_memory() returns [] for at least six unrelated reasons: no
torch, a torch with no usable CUDA/HIP device, zero devices enumerated,
an empty or stale visibility mask, the #7624 arch gate dropping every
card, or amd-smi and the torch fallback both declining. Every one of
them puts the model on the CPU, and they need completely different
fixes.

Adds _explain_empty_gpu_probe(), and warns with it at the placement
decision when the list is empty. Facts only, gathered without calling
mem_get_info, so it never creates the HIP primary context the amd-smi
branch exists to avoid. It never raises and never returns "", since an
unexplained empty list is the thing being fixed. The arch gate already
warns with a better message when it is the cause, so that case is left
to it rather than warned about twice.

Tests: 15 cases, one per cause plus the mask reporting and the safety
properties. Mutation-checked: dropping the mask note fails 1, inverting
the coverage test fails 2, returning "" on error fails 1.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-08-31T11:04:41.419340Z a1348ed New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

Found by simulating the change across [Linux, WSL, Windows, macOS] x
[NVIDIA, AMD/ROCm, CPU only] rather than only on this host.

This probe reads CUDA and HIP, so it returns [] on every Mac. load_model
has a whole "No GPU is enumerated on Metal" branch for that state, and
an Apple Silicon machine in it still offloads the entire model to the
Metal GPU. The warning as first written told every one of those users
their model was running on the CPU out of system RAM, which is the
opposite of what happens. Three of the twelve matrix cells were wrong.

The warning now excludes darwin, and the explainer answers for macOS by
naming its own blind spot rather than blaming torch for a device it was
never going to see.

Tests: the three macOS cells, plus a source-level check that the load
site keeps both guards. Mutation-checked: warning on macOS fails 1,
dropping the explainer's macOS branch fails 3.
@danielhanchen

Copy link
Copy Markdown
Member Author

Verification pass across the platform and GPU matrix, plus what it changed.

It found a real bug in this PR

The probe reads CUDA and HIP only, so it returns [] on every Mac. load_model already has a "No GPU is enumerated on Metal" branch for exactly that state, and an Apple Silicon machine sitting in it still offloads the whole model to the Metal GPU. The warning as first written told every one of those users their model was running on the CPU out of system RAM, which is the opposite of what happens. Three of the twelve matrix cells were wrong.

Fixed in e4a8d70ec: the warning excludes darwin, and the explainer answers for macOS by naming its own blind spot instead of blaming torch for a device it was never going to see.

The matrix

Driven in an isolated uv venv with no Studio dependencies installed, loading the function as a bare AST fragment to prove it needs none of the backend:

platform  gpu        warns  reason
Linux     NVIDIA     True   torch reports no usable CUDA or HIP device
Linux     AMD/ROCm   True   torch sees 3 ROCm device(s) but the probe returned none ...
Linux     CPU only   True   torch is not importable, so no GPU could be enumerated
WSL       (same three, WSL2 is sys.platform == "linux")
Windows   (same three)
macOS     NVIDIA     False  this probe reads CUDA and HIP only; macOS offloads through Metal
macOS     AMD/ROCm   False  this probe reads CUDA and HIP only; macOS offloads through Metal
macOS     CPU only   False  this probe reads CUDA and HIP only; macOS offloads through Metal

Plus six edge cases, each the same empty list for a different reason: a container hiding every device with an empty HIP_VISIBLE_DEVICES, a stale ordinal mask pointing at a device that left, the arch gate dropping every card, a Vulkan build with no device, an unreadable install marker making a helper raise, and a torch with no cuda attribute at all. All 12 cells and 6 edge cases pass.

Before and after

Before, a model that ran from system RAM produced only GPUs free: [], selected: None, with the reason absent, and the line below it ("fits without paging", which is about disk paging) reading like reassurance. After, the cause is named at warning level.

Does it break anything

No behaviour changes: placement is untouched and this only explains a decision already made. Nothing is added to the launch command, no new flag, no state or config, so old installs are unaffected in both directions. The cost is paid only on the path that already gave up on the GPU, and the explainer never calls mem_get_info, so it does not create the HIP primary context the amd-smi branch exists to avoid; a test fails if it ever does. Where the arch gate is the cause it already warns with a better message, so that case is still left to it.

Not applicable here: no HTTP route, template or asset changes, so there is nothing for a browser to exercise. I would rather say that than run a browser matrix that proves nothing.

771 passed across test_gpu_arch_gate_7624, test_gpu_arch_gate_consumers_7624, test_gpu_arch_gate_os_matrix_7624, test_amd_apu_unified_memory, test_gpu_memory_mode, test_amd_smi_hip_index_and_coverage, test_cuda_sm_gate and test_llama_cpp_placement. Mutation-checked throughout: warning on macOS fails 1, dropping the explainer's macOS branch fails 3, dropping the mask note fails 1, inverting the coverage test fails 2, returning "" on error fails 1.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: e4a8d70ecb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

Both Manual modes set gpus = [] deliberately, to stand the Python
planner down and let --fit or an explicit --gpu-layers place the model.
The GPU is fully used there. Reading gpus alone, this warned "running on
the CPU, model in system RAM" at every Manual load.

That is the case this whole change came from. The reporting user's log
has "GPUs free: []" directly under "Manual mode with Auto layers hands
memory management to llama.cpp --fit", so their empty list was the mode
standing the planner down, not a probe that failed.

Guard on _detected_gpus, which holds the probe result and is captured
before either Manual branch empties the pool. load_model already draws
this distinction for _placement_verdict_partial and the load mode.

Also replaces the three source-inspection assertions with a structural
extraction of the guard. They took a fixed window of characters before
the message, and the window stopped covering the condition as soon as
the comment grew: two of them passed against a mutant for that reason,
which made the check worthless exactly when it mattered.

Tests: 21, mutation-checked. Dropping the _detected_gpus guard fails 1,
dropping the macOS guard fails 1, capturing _detected_gpus empty fails 1.
869 passed across the GPU suites.
@danielhanchen

Copy link
Copy Markdown
Member Author

Re-checked this properly, and reproduced the state on real hardware rather than only in mocks. Two things came out of it: a second false-positive bug, and a correction to the diagnosis this PR was built on.

Reproduced, unmocked

Real host, real torch, real CUDA device, fresh subprocess per case:

### empty mask, the container / stale-mask case
  probe -> []
  reason -> torch reports no usable CUDA or HIP device (CUDA_VISIBLE_DEVICES is empty)
### mask points at a device that is not there
  probe -> []
  reason -> torch reports no usable CUDA or HIP device (CUDA_VISIBLE_DEVICES='9')
### normal, GPU visible
  probe -> [(3, 181727, 183359)]
  reason -> (not empty, no warning)

So the GPUs free: [] state reproduces, the reason is named and correct, and the healthy case is untouched.

The bug that found: Manual mode is not an absent GPU

Both Manual modes set gpus = [] on purpose, to stand the Python planner down and let --fit or an explicit --gpu-layers do the placement. The GPU is fully used there. Reading gpus alone, this PR warned "running on the CPU and the model will live in system RAM" at every Manual-mode load.

Worse, that is exactly the case this change came from. The reporting user's log reads:

Manual mode with Auto layers hands memory management to llama.cpp --fit ...
GGUF size: 16.4 GB, ..., GPUs free: [], selected: None, --fit: on

Their empty list was the mode standing the planner down, not a probe that failed. I had read it as a probe failure. It very likely was not one, and their model was probably on the GPU throughout.

Fixed in 729c32a5b by guarding on _detected_gpus, which holds the probe result and is captured before either Manual branch empties the pool. load_model already draws this distinction for _placement_verdict_partial and for the load mode.

The load site is now executed, not just read

The guard is cut out of llama_cpp.py by indentation and exec'd with controlled bindings, so what runs is the shipped text:

case                         warned  expected
probe empty, Linux           True    True
probe empty, Windows         True    True
probe empty, macOS/Metal     False   False
probe empty, arch gate       False   False
Manual+Auto layers (gpus=[]) False   False
Manual offload (gpus=[])     False   False
Manual, 3 GPUs, the report   False   False
GPU present, Linux           False   False
GPU present, macOS           False   False
GPU present, arch gate       False   False

main has no such guard at all, so every one of these is silent before the change.

A test-quality problem worth naming

Three assertions took a fixed window of characters before the message. Once the guard's comment grew, the window stopped reaching the condition, and two of them passed against a mutant that deleted the guard entirely. They are now a structural extraction of the if statement. The mutation results before that fix were worthless, which is the failure mode mutation testing is supposed to catch, so I would rather flag it than quietly re-run.

21 tests, mutation-checked: dropping the _detected_gpus guard fails 1, dropping the macOS guard fails 1, capturing _detected_gpus empty fails 1. 869 passed across nine GPU suites.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

pre-commit-ci Bot and others added 2 commits August 31, 2026 08:30
Two review findings, both reproduced against the code at head.

An empty probe does not mean a CPU load. llama.cpp enumerates devices
itself and still receives --fit on, so it can place on a GPU this probe
never saw, and an explicit gpu_ids pick is restored into gpu_indices
below the warning (llama_cpp.py, "if gpu_ids and gpu_indices is None")
and pinned for the child, so those loads reach the GPU the user chose.
Asserting "will run on the CPU and the model will live in system RAM"
stated an outcome this code does not know. The message now reports what
this process observed -- it could not enumerate a GPU, so it planned
blind and left placement to --fit -- and stops there. Loads with an
explicit pick are excluded outright.

sys.platform == "darwin" was too broad an exemption. auto_detect_backend
selects Metal only on Apple Silicon and falls back to CPU otherwise, so
an Intel Mac with an empty probe really is CPU-only and wants this line.
Gated on is_apple_silicon via a helper that falls back to the old
platform test if the import fails.

Tests: 24, and the simulation now covers an explicit GPU pick, Apple
Silicon and an Intel Mac. Mutation-checked: dropping the gpu_ids
exclusion fails 1, reverting to the blanket darwin exemption fails 1,
going back to asserting the launch outcome fails 5.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 31, 2026
@danielhanchen danielhanchen changed the title Say why the GPU probe came back empty instead of just logging [] Say why Studio could not enumerate a GPU, instead of just logging [] Aug 31, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen and others added 2 commits August 31, 2026 10:31
Two review findings, both introduced by the previous commit.

The explainer still answered "macOS offloads through Metal" for any
darwin host, while the load site had just been narrowed to Apple
Silicon. So an Intel Mac, which auto_detect_backend resolves to CPU, got
the warning and then an explanation naming an offload path it does not
have. Both now use _metal_capable_host(), and a test asserts they use
the same check, since two notions of "is this Mac fine" is how they
drifted apart in the first place.

The message also said placement was left to llama.cpp --fit. use_fit is
the internal default, read before extra_args are appended to the
command, and a last-wins --fit off there wins; the later code calls
fit_is_effectively_on for exactly that distinction, and cmd does not
exist yet at this point. The message now reports only that the load was
planned without device information, and claims nothing about what runs
it.

Also fixes the test helper that extracts the guard. It stopped at the
line holding the marker, so the message's continuation literal was never
inspected and a mutant rewriting it passed. It now walks to the end of
the block by indentation, and that mutant fails 2.

Tests: 29. Mutation-checked: reverting the explainer to blanket darwin
fails 2, re-claiming the fitter fails 2. 781 passed across the GPU suites.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 902e9b278c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

Comments only, no code change. 27 comment lines to 7.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 31, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 31, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 31, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 31, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 31, 2026
@danielhanchen
danielhanchen merged commit 489e85a into main Aug 31, 2026
5 of 46 checks passed
@danielhanchen
danielhanchen deleted the studio-explain-empty-gpu-probe branch August 31, 2026 11:07
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