Skip to content

Widen the pinned child GPU mask when extra args name a companion device (#11810) - #11823

Merged
danielhanchen merged 11 commits into
unslothai:mainfrom
deepspace28:fix/11810-companion-device-mask
Sep 30, 2026
Merged

danielhanchen merged 11 commits into
unslothai:mainfrom
deepspace28:fix/11810-companion-device-mask

Conversation

@deepspace28

Copy link
Copy Markdown
Contributor

Fixes #11810.

The pin is a placement constraint on the MAIN model only, but it surfaced as an absolute mask: when _select_gpus decides the main model + KV fit on one card, _emit_child_gpu_visibility sets CUDA_VISIBLE_DEVICES to that subset, and any --mmproj-device / -mmdev / --spec-draft-device the user's extra args name outside that set makes llama.cpp reject the flag at argument parsing as "invalid device". Studio's retry chain then misreads the reject as a fit and as a speculative-drafter failure and burns all four attempts on the same masked environment.

One change in the single-GPU pinning arm of load_model, applied after the inherited visibility order has resolved and rewritten the split and just before the mask becomes the child's environment: _widen_pin_ids_for_companion_devices parses the CUDA GPUs the extra args point commas-included at (reusing _extra_args_device / _extra_args_draft_device, now in the same table as the other device readers), unions any GPU the pin hides, and -- since the widened mask would otherwise spread the main model across it under -ngl -1 -- emits a --device naming the main model's original cards so they keep the placement the fit picked. llama.cpp numbers CUDA-n by position in the visible set, so the remap goes through the final widened mask; a user's own --device is never touched. Tokens that are not CUDA-n (cpu, none, HIP names) are ignored, so the widening is a no-op everywhere the flags already resolve.

Validated upstream by the reporter on a V100 + 3060 Ti host: the exact config that rejected all four attempts now starts (llama-server ready on port 10584, VRAM consistent with mmproj moving to GPU 1, main model staying on GPU 0).

tests/test_companion_device_widening.py pins the helper: hidden-companion widening, the CUDA-n remap under a non-ascending main order, no --device emitted when the user supplied one, an in-mask flag changing nothing, non-CUDA tokens ignored, the inline --flag=value spelling, comma lists, and last-wins among repeated flags. One unrelated pre-existing failure in tests/test_gpu_selection.py (Could not determine home directory under {'CUDA_VISIBLE_DEVICES': 'GPU-aaa,GPU-bbb'} env-clearing) reproduces identically with the change stashed; everything nearby (204 tests + 94 subtests) passes.

@danielhanchen

Copy link
Copy Markdown
Member

Thanks for getting the #11810 reporter to confirm this on the V100 + 3060 Ti host. Widening the pinned mask only when --mmproj-device or --spec-draft-device names a hidden card, while pinning the main model with --device, looks like the right scope. Will get this reviewed.

@danielhanchen danielhanchen self-assigned this Sep 28, 2026
The widening compared the user's CUDA<n> tokens with physical ids and left
the tokens as written, so:
- main pinned to GPU 1 with --mmproj-device CUDA0 widened the mask to 1,0,
  where the child's CUDA0 is the main card, and the projector silently
  landed on GPU 1;
- a companion on the pinned card itself (pin 1, CUDA1) was still an
  invalid device, since the one-card child only has CUDA0;
- under an inherited mask such as CUDA_VISIBLE_DEVICES=2,3, CUDA0 was
  read as physical 0 and exposed a card the parent had hidden.

Read the tokens through the inherited mask (skipping a uuid or MIG mask),
from the launch argv so stripped flags change nothing, only for the flag
that wins in each group, and rewrite them to the card's position in the
child's mask. ROCm<n> tokens get the same treatment.
…ion widening

- An explicit gpu_ids pin is the pool the training guard budgeted, so a companion flag
  is only renumbered within it and never adds another card to the child's mask.
- With LLAMA_ARG_DEVICE inherited and no argv --device, widening would append a
  --device overriding the user's main-device choice, so that launch is left unchanged.
The widening numbers the companion flags and the generated main --device for the
launch's mask. When a kernel-image crash respawns on other cards, the generated
--device is dropped and the same fit runs again against the retry's mask, reading each
token as a position in the crashed launch's mask, so the projector or drafter keeps its
card and the main model gets the new ones.
@danielhanchen

Copy link
Copy Markdown
Member

Review record for #11823, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#177.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror a40213acc (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

🛡️ Codex Security Review

Here are some automated security review suggestions for this pull request.

Reviewed commit: a40213acc2

No triage reaction was recorded on the mirror

Round 2: reviewed mirror 6aa55ef20 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on studio/backend/core/inference/llama_cpp.py (on the mirror)

@@ -29985,10 +30122,38 @@ def _try_auto_vulkan_cpu_fallback(
                                 "weights outgrow; set "
                                 "GGML_CUDA_ENABLE_UNIFIED_MEMORY=1 for the respawn."
                             )
+                        _retry_mask = list(_remaining)
+                        if _companion_fit_mask is not None:
+                            # The companion flags and the generated main --device were
+                            # numbered for the crashed launch's mask: fit them to this one,
+                            # reading each token as a position in that previous mask.
+                            if _companion_added_device is not None:
+                                for _k in range(len(cmd) - 1, 0, -1):
+                                    if cmd[_k - 1 : _k + 1] == _companion_added_device:
+                                        del cmd[_k - 1 : _k + 1]
+                                        break
+                            _had_main_device = _extra_args_main_device(cmd) is not None
+                            _retry_mask, _refit = _widen_pin_ids_for_companion_devices(
+                                cmd,
+                                list(_remaining),
+                                list(_companion_fit_mask),
+                                may_widen = not gpu_ids,

P2 Badge Exclude companion-caused failures from architecture retries

When the kernel-image error comes from a widened companion rather than the automatically selected main GPU, _crashed is still derived only from gpu_indices, and this refit preserves the failing companion in the retry mask. For example, main GPU 0 plus a projector on physical GPU 1 produces [0, 1]; if GPU 1 lacks kernels, the retry treats only GPU 0 as crashed, selects GPU 1 as _remaining, and refits the projector onto GPU 1 again, repeating the deterministic failure. Base the exclusion on the fitted child mask, or avoid this retry when a widened companion makes the failing device ambiguous.

Useful? React with 👍 / 👎.

Rejected on the mirror

Reply from danielhanchen:

Not changing this: it needs a build without kernels for a card the user explicitly named as the companion. Before this PR that launch already failed (invalid device), and after it the load still ends in an error, so nothing regresses, and the retry only runs for auto-selected GPUs.

Round 3: reviewed mirror 451607c64 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

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

Reviewed commit: 451607c64c

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 451607c64 (no equivalent commit found on this PR).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] --mmproj-device / --spec-draft-device rejected as "invalid device" when Studio pins llama-server to a single GPU

2 participants