Skip to content

Studio runtime keeps the GPU when nvidia-smi cannot answer, and prefers a GPU-capable llama-server beside a CPU build - #11005

Merged
danielhanchen merged 21 commits into
mainfrom
embed-nvml-fallback
Sep 15, 2026
Merged

danielhanchen merged 21 commits into
mainfrom
embed-nvml-fallback

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

The runtime half of #10985, plus the binary-precedence defect behind #5941. Both leave a working CUDA llama.cpp running on the CPU, which is what the "my GPU is not used" reports describe once the installer side is right.

What was wrong

  • Embeddings pinned themselves to the CPU whenever nvidia-smi could not answer. LlamaCppBackend._get_gpu_memory asks nvidia-smi, then amd-smi, then torch. On a host where nvidia-smi is absent, stale or hung and torch is not installed (a GGUF-only install), it returns [], and LlamaServerBackend._gpu_available reads that as "no GPU": the embedding server is launched with -ngl 0 and CUDA_VISIBLE_DEVICES="" around a CUDA build. The installers stopped trusting that misread in Studio: read the NVIDIA inventory from NVML or the CUDA driver when nvidia-smi cannot answer #10985; the runtime still did.
  • A CPU build beside a CUDA build always won (Unsloth keeps using my CPU for inference. and I found the reason and solution! #5941). llama_server_candidates listed only <root>/, build/bin and build/bin/Release, and the finder took the first executable hit, so a tree with a CPU build/ next to a CUDA build-cuda/ ran on the CPU forever, with the user's log reading "llama.cpp was compiled without GPU support" while training used the GPU normally.
  • The Windows embedding launch had no CUDA DLL search path. Chat builds one through _build_windows_path_dirs (venv nvidia wheels, CUDA_PATH); the embedding server built its own environment and skipped it, so a CUDA build whose cudart lives in the venv loaded no GPU backend.
  • A venv path with brackets broke the Linux library search. sys.prefix went into glob.glob unescaped, so Studio[CUDA] matched nothing and the child lost its CUDA library path. torch/lib was also missing from the chat and embedding lists although the shared helper includes it.

The change

  • studio/nvidia_probe.py reports memory_total_mib and memory_free_mib per NVML device (nvmlDeviceGetMemoryInfo); the CUDA driver rows carry 0, since memory needs a context that probe never creates.
  • _get_gpu_memory gains an NVML step between nvidia-smi and amd-smi: _get_gpu_memory_nvml runs the probe module in a child with a 10 s deadline, masks the physical rows by CUDA_VISIBLE_DEVICES as the CUDA runtime reads it (indices, GPU- and MIG- uuid prefixes; an entry naming no single device ends the mask there), reports a GPU in MIG mode as the slice CUDA exposes and pins the child to it by uuid, and ignores rows without a memory reading so the torch probe still runs. UNSLOTH_NVIDIA_LIBRARY_PROBE=0 turns it off, as it does the installers' probe.
  • llama_server_candidates also lists build-cuda, build-hip, build-rocm and build-vulkan; prefer_gpu_capable moves a usable candidate that ships a GPU backend library ahead of a first hit proven CPU-only (a split-library build with ggml-cpu and no GPU backend beside it). Unknown layouts, the installer's wrapper included, keep their place, so the managed install resolves as before. Both resolve_llama_server_binary and the runtime finder use it.
  • The Windows embedding launch prepends the same DLL directories as chat.
  • Both Linux library builders escape the prefix and add torch/lib.

Measured

On this host (8x B200, driver 590.48.01), with a stub nvidia-smi that exits 1 on PATH and no torch importable:

main this branch
_get_gpu_memory() [] [(3, 182630, 183359)] (the one GPU CUDA_VISIBLE_DEVICES allows)
embedding launch -ngl 0, CUDA_VISIBLE_DEVICES="" -ngl -1

Tests

studio/backend/tests/test_gpu_memory_nvml_fallback.py: the NVML step (a failing nvidia-smi now reads the inventory, the mask applies in its index, uuid and MIG forms, rows without memory are not evidence, a working nvidia-smi is not second-guessed, the switch and a missing script turn it off, the real script is found beside the installers), the embedding decision (offloads on NVML alone, still CPU when nothing answers), the Windows embedding DLL path, the bracketed prefix and torch/lib on both Linux builders, and the #5941 tree on Linux and Windows layouts for both the settings resolver and the runtime finder. The probe module tests cover the new payload fields, including on a real NVIDIA host.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 15, 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-09-15T18:10:01.934236Z f2895e5 Manual request
ℹ️ 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.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 15, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 15, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6fa7fd574e

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

Comment on lines +11673 to +11674
if token.isdigit():
hits = [r for r in rows if str(r.get("index")) == token]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Defer numeric masks until GPU index spaces agree

When nvidia-smi fails on a heterogeneous multi-GPU host and CUDA_DEVICE_ORDER is not PCI_BUS_ID, numeric CUDA_VISIBLE_DEVICES entries are CUDA ordinals, while these NVML row indices identify physical devices. Matching them directly can therefore feed auto-placement the free/total memory of a different GPU and cause an invalid layer fit or OOM; the existing _cuda_compute_caps path explicitly declines this same join unless PCI ordering is enabled, so this fallback should likewise defer to the CUDA/torch probe or map the devices by UUID.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The NVML index is the index nvidia-smi prints (nvidia-smi is an NVML client, same enumeration), so this fallback joins a numeric mask exactly as the nvidia-smi rows above it do and sets _GPU_IDS_ARE_PCI_INDICES for the same reason. A different join here would make the two probes disagree on the same host. UUID entries now match the rows' own uuid, which is the unambiguous form.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 15, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…fer only backends this process can reach, for PR #11005
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 15, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 15, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: bf8e793b88

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

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 15, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 461a4476ee

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

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 15, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 15, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 15, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 15, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: f2895e52b5

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

@danielhanchen
danielhanchen merged commit c829b33 into main Sep 15, 2026
56 of 57 checks passed
@danielhanchen
danielhanchen deleted the embed-nvml-fallback branch September 15, 2026 18:33
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…rs a GPU-capable llama-server beside a CPU build (unslothai#11005)

* Runtime NVML memory fallback, GPU-capable llama-server preference, Windows embedding DLL path

* Honour uuid CUDA_VISIBLE_DEVICES masks in the NVML fallback and prefer the GPU build this host's vendor runs

* Keep a denied pinned candidate ahead of the GPU-capable reorder for PR unslothai#11005

* An ambiguous uuid abbreviation hides every GPU; the real-host probe test allows a card with no free memory

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* List MIG slices in the probe so a MIG assignment is honoured, and prefer only backends this process can reach, for PR unslothai#11005

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Hand a uuid mask back to the child as uuids, keep a known-empty vendor set, void the probe on a missing handle, for PR unslothai#11005

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Keep the devices before an invalid mask entry, prefer the GPU build in the legacy in-tree fallback, read the CUDA mask for HIP, for PR unslothai#11005

* A MIG parent is its first slice to CUDA: report the slice memory and pin the child to it, for PR unslothai#11005

* A MIG slice the mask names stays that slice, for PR unslothai#11005

* One MIG slice per card, and the uuid map is reset for every memory query, for PR unslothai#11005

* Apply the repository kwarg spacing, for PR unslothai#11005

* Read the WSL2 runtimes behind /dev/dxg as the GPU vendor, for PR unslothai#11005

* Derive the child's uuid pin from the NVML rows on each call, let a GPU build for another vendor yield, skip the Linux path test on Windows, for PR unslothai#11005

* Make the embedding and sibling-build tests independent of the host, for PR unslothai#11005

* Tell the probe child to emit utf-8, and pin the embedding test off Apple Silicon, for PR unslothai#11005

* A visible NVML row without a memory reading voids the answer, for PR unslothai#11005

* The two ROCm visibility masks stack when deciding whether AMD is reachable, for PR unslothai#11005

* A full card is a reading; only a missing total voids the NVML answer, for PR unslothai#11005

---------

Co-authored-by: danielhanchen <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

2 participants