Skip to content

common : --fit loads a model one GPU holds on that GPU alone - #514

Open
danielhanchen wants to merge 2 commits into
base/upstream-9a811bab3from
pin-fit-one-gpu
Open

danielhanchen wants to merge 2 commits into
base/upstream-9a811bab3from
pin-fit-one-gpu

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Summary

The unslothai/llama.cpp prebuilt is an aged upstream b#### tag plus the pins listed in scripts/unsloth/pr-set.json, merged in order. This pin is one commit on top of upstream b12966 (9a811bab3c4), so it needs no other pin. Once upstream has an equivalent change, the pin is dropped.

The problem

With 2 or more GPUs visible, --fit keeps the default layer split whenever the model fits across the cards, even when one card alone could hold all of it. A layer split runs the cards one after the other and copies the activations at every hop, so a model that fits one card runs slower on two. On 2x R9700, Qwen4-35B-A3B UD-Q4_K_XL at -c 32768 --fit on decodes at 97.7 tok/s (MTP 2) over both cards and at about 127 tok/s on one.

The fix

  • common/fit.cpp: new common_fit_one_gpu(), run by common_fit_apply() after a successful fit. It applies only when:
    • 2 or more GPUs are visible to the model;
    • the fit kept all layers on the GPUs, with no tensor overrides (from the user or the fit) and no MoE cache;
    • the user did not set -sm, -ts, -ngl or a draft device list.
  • Only GPU / IGPU devices count and can be picked (a host accelerator such as BLAS in -dev reports host memory).
  • It then measures (no_alloc, as --fit does) the model, context and compute buffers as -sm none on the GPU with the most free memory, plus the drafter on the same GPU (in-file MTP context or -md model). If free memory minus that need still leaves the fit margin (--fit-target) of that GPU, the model loads there with -sm none and --main-gpu set to that GPU. The params are updated too, so a -md drafter and a reload follow the same placement.
  • The host row of the measure is not used. It holds the input layers in every placement (Qwen4's per-layer token embeddings are several GB), so a check on it would reject every such model.
  • One log line per launch where the measure runs:
    • common_fit_one_gpu: --fit: one GPU (0) holds the model: needs X + Y MiB, Z MiB free, margin M MiB: -sm none on it
    • or ... does not hold the model ..., after which the split stays as the fit chose it.
  • common_measure_memory() (the server's measure worker) measures its minimum-context report on the same single GPU.
  • Kill switch: LLAMA_FIT_ONE_GPU=0 keeps the split.
  • With one GPU, or when any of the conditions above fails, nothing is measured and nothing changes. common_fit_params() itself is unchanged, so llama-fit-params and llama-bench --fit behave as before.

Evidence

Check Result
Pin set (llama_pinset.py sim --tags window: b13023 aged, b13024 newest), master pr-set + open candidates #485-#498, #501, #502 (including #489, which also touches common/fit.cpp) + one more candidate branch + this pin, 67 pins this pin merges clean on both tags; pin_contract for this pin: ok, 0 files with new symbols, 78 lines accounted for; merge_checks: clean
Same sim without this pin (control) the same refusals on the same tags: #495 at b13023 and b13024 (src/llama-model-loader.cpp), #251 and #268 at b13024. They exist without this pin and touch no file it changes
CPU build of the merged b13023 tree (llama test-llama-archs test-backend-ops test-mtmd-impl, as the preflight) ok; feature_matrix CPU: all 36 features demonstrated
Full CUDA build of the merged b13023 tree (sm_100, tests and server on) ok
git merge-tree with upstream master (2ed8d0971bd) clean
B200, 2 cards visible, pin-set tree b13023 with and without this pin, llama-completion -c 4096 -fit on, greedy 48 tokens. Models: Qwen3-0.6B Q4_K_M (dense), gemma-3-1b-it Q4_K_M, granite-4.0-h-tiny Q4_K_M (hybrid), Qwen3-30B-A3B UD-Q2_K_XL (MoE), Qwen3.8-27B UD-IQ2_XXS, Inkling-2-Mini UD-IQ1_S for all 6 models the output md5 is identical across base on 2 cards, pin on 2 cards, pin with LLAMA_FIT_ONE_GPU=0, base on 1 card and pin on 1 card. The pin on 2 cards logs one GPU (0) holds the model for every model (e.g. needs 11831 + 0 MiB, 182008 MiB free, margin 1024 MiB for Qwen3-30B-A3B). The kill switch arm, both 1-card arms and the base log nothing
2x Radeon AI PRO R9700 (gfx1201, 32 GB each), HIP, pin-set tree b13023 (+ open candidates) with and without this pin, -lv 4, greedy. Arms: base 2 cards, pin 2 cards, pin with LLAMA_FIT_ONE_GPU=0, base and pin with 1 card visible, and pin 2 cards with --fit-target set so one card is just too small greedy output identical in every arm for all 6 models. Qwen3-0.6B Q4_K_M, Qwen3-30B-A3B UD-Q2_K_XL, Qwen3.8-27B UD-IQ2_XXS, Inkling-2-Mini UD-IQ1_S and Qwen4-35B-A3B UD-Q4_K_XL: the pin loads on 1 device ("one GPU (0) holds the model", e.g. Qwen4 needs 23400 MiB of 32404 free) and, with the tight --fit-target, keeps 2 devices ("does not hold"). Qwen3.8-Flash-Next UD-IQ4_XS, which does not fit one card: 2 devices, split kept. LLAMA_FIT_ONE_GPU=0 and the 1-card arms log nothing

Speed on the pin-set tree (b13023 + open candidates, HIP builds of base and pin on the box), 2x R9700, Qwen4-35B-A3B UD-Q4_K_XL, both cards visible, -c 32768 --fit on, chat decode, 3 rotated rounds x 3 (n = 9 per arm), median [min - max] tok/s:

Arm MTP 0 MTP 2 (acceptance 0.460 in every arm)
base 55.15 [54.70 - 55.32] 66.38 [65.79 - 66.77]
pin, LLAMA_FIT_ONE_GPU=0 55.32 [54.98 - 55.38] 66.49 [65.84 - 66.85]
pin 67.90 [67.54 - 68.34], +23.1% 89.78 [88.43 - 90.46], +35.3%

The ranges do not overlap and the kill switch matches base. Every pin launch logs --fit: one GPU (0) holds the model: needs 23751 + 0 MiB (MTP 0) or needs 24306 + 196 MiB (MTP 2), then -sm none on it; base and the kill switch log nothing. The output text is identical across base, pin and kill switch at MTP 0 and at MTP 2, and the greedy md5 is identical for Qwen4 (5ce5fb1edbf8) and Qwen3.8-Flash-Next (c07036e86a89, no fit line, split kept).

Earlier speed on our private fork (same rule and measure). 2x R9700 (ROCm), Qwen4-35B-A3B UD-Q4_K_XL, both cards visible, -c 32768 --fit on, 3 rounds:

  • every launch logs one GPU (0) holds the model: needs 23751 + 0 MiB, 32404 MiB free, margin 1024 MiB: -sm none on it. The ROCm0 model buffer is 22724 MiB and the other card stays idle;
  • against the layer split: chat decode +30% / +32%, agent decode +30% / +29%, agent prefill +44% / +28% (MTP 0 / MTP 2);
  • MTP 2 chat decode 129.3 tok/s against 97.7.

A model that does not fit one card keeps today's split. On 2x RTX 3090 with the same Qwen4 model at 96K context, the measure logs one GPU (0) does not hold the model: needs 25505 + 0 MiB, 23859 MiB free, margin 1024 MiB. The model stays on both cards, and the greedy text matches the split arms 30 / 30.

Not proven

  • The B200 runs prove correctness only. The B200 run did not exercise the "does not hold" branch; the 2x R9700 run above does (5 models with a tight --fit-target, plus Qwen3.8-Flash-Next, which does not fit one card).
  • B200 cards have about 180 GB free, so every test model fits one card there. Qwen4-35B-A3B and Qwen3.8-Flash-Next were not run on B200 (local per-card limit).
  • Launches with a drafter (-md or in-file MTP) are measured through the same extra-model path as --fit. Only Qwen4 MTP 2 on the fork exercised it.
  • The merged b13023 tree needs its GGML_OP_COUNT static asserts raised from 102 to 103 to compile: Host-weight prefetch in the scheduler, faster MoE cache prompt and decode, cache seeding under llama-server #268 and model : Inkling 1 (ggml-org#25731) on b12533 for the prebuilt #378 each add an op and each set the count to 102. That is a pin-set issue unrelated to this pin. Both test arms carry the same local fix.
  • Windows, macOS, Vulkan and multi-GPU setups with RPC devices were not run. The change is host-side code in common/.

…IT_ONE_GPU=0 keeps the split)

When the fit keeps the whole model on 2+ GPUs with the layer split and the GPU with the most free memory holds model, context, compute and the drafter (measured with no_alloc as -sm none) within its fit margin, the model loads there with -sm none. The host row of the measure is not used: it holds the input layers in every placement.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T08:53:17.349939Z 660df7f 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.

@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: 6e193f0aa9

ℹ️ 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 thread common/fit.cpp
Comment on lines +1103 to +1107
mparams.split_mode = LLAMA_SPLIT_MODE_NONE;
mparams.main_gpu = best;
mparams.n_gpu_layers = -1;
params.split_mode = LLAMA_SPLIT_MODE_NONE;
params.main_gpu = best;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep minimum-context measurements on the selected single GPU

When this optimization succeeds during common_measure_memory() and the minimum context is smaller than the maximum, the two memory reports use different placements. That function passes a copy named mparams_fit to common_fit_apply(), but later measures info_min with its original mparams, which still has layer splitting enabled. Updating params here does not update that original struct. Consequently, the server's measure worker reports the minimum-context model spread across multiple GPUs even though loading it now selects one GPU, understating the memory required on the selected card. Update the measurement path to use the selected placement for both reports.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in 660df7f: the min-context measure now takes split_mode and main_gpu from params, so it measures the same single GPU the fit chose.

Comment thread common/fit.cpp
Comment on lines +1055 to +1058
for (size_t i = 0; i < devs.size(); i++) {
if (best < 0 || dmds[i].free > best_free) {
best = int(i);
best_free = dmds[i].free;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Exclude host accelerators from single-GPU selection

With an explicit device list containing a host accelerator, such as --device CUDA0,BLAS, this loop can select BLAS instead of a GPU. parse_device_list() accepts non-CPU accelerator devices, and common_get_device_memory_data_impl() substitutes free host memory for BLAS's reported 0/0 memory budget. When host memory exceeds free VRAM, BLAS therefore wins; its host-backed allocations are then excluded from need, so the check succeeds and switches the entire model to CPU-backed execution instead of retaining GPU offload. Filter the eligible devices to GPU/IGPU types for both the two-GPU prerequisite and candidate selection, preserving their original indices for main_gpu.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in 660df7f: only GPU / IGPU devices count toward the 2-GPU check and can be picked; no GPU picked means the split stays.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 660df7f588

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

This branch has not been deployed

No deployments
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