Repository navigation
stop the planner withholding a split reserve from one card, and price physical cores - #9863
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3c21d78ef6
ℹ️ 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".
Ran this branch on the five cells from #9861 that were most likely to move: the two headroom cells, two representative losses, and the one cell where the planner won. Same host, same ballast figures, same
The reserve fix does more than reduce the spillI expected this to shrink each spill by the returned GiB. It does not: in all five cells the planner now produces no plan at all. The phantom GiB was not inflating the deficit, it was manufacturing it. On the two headroom cells the card now ends at the same occupancy in both arms, 47224 MiB against 47224 for Qwen3-32B and 47234 against 47234 for the Q2 MoE, where before the planner sat 1884 MiB and 2458 MiB below the baseline. Four of the five regressions are gone outright, including the worst one in the report at 0.24x. That is a bigger effect than the description claims for it. It also removes the only winThe 70B cell is the one I would not want lost, and this branch loses it: Arm B is unchanged across the two runs, 7.68 against 7.69 on separate code, so the comparison is sound. What moved is arm A: it stops planning, hands the load to So the phantom GiB was the only reason the planner engaged in the one case where engaging was right. Correcting it is still correct, and I am not arguing for keeping a wrong reserve because it accidentally fired in a good place. What that exposesThis is the missing cost gate from the other direction. Engagement is decided on necessity alone: the planner asks whether it must spill to fit, never whether spilling would beat what llama.cpp is about to do. When the model fits, it abstains, even where A gate that only fires when a spill is unavoidable cannot reach the 70B case, because there the model does fit and a spill is a choice. Method noteI first simulated this offline by adding 1 GiB to the budget and re-running Both fixes look right to me. On the second one, the thread count only reaches |
|
Correction to my previous comment: that run is invalid and its conclusions do not follow. Please disregard the table in it. This branch contains #9862, which merged while I was setting the run up, so That is what the 0.96x to 1.02x readings actually are: two identical commands, which is a reasonable noise check and nothing else. Specifically, neither of these claims is supported:
Re-running the same five cells with One thing from that comment does stand, because it does not depend on the run: the thread-count change reaches only |
|
Re-ran the five cells on this branch with
Every cell spills 0.8 to 1.1 GB less and improves on both metrics. Generation moves +0.02 to +0.30. The largest gain is the Q2 MoE, which was the worst cell in #9861 at 0.24x and is now 0.54x. The 70B win is preserved, 1.99x against 1.89x, which is within the run-to-run spread I see on that cell. My previous comment claimed this branch removed it. That was the disabled-planner artifact and it was wrong. There is no tension here between fixing the reserve and keeping the case where spilling pays. No regression is eliminated. Four of the five are still below parity after the fix. This matches the offline prediction I made before either run: adding the returned GiB to the budget and re-running Residual conservatism. On the two cells where
So the split reserve was roughly half of that gap and something else accounts for the rest. Worth knowing before this is treated as closing the headroom question. Both changes look right to me and I would take them. The remaining distance to parity in these four cells is not something a budget correction can reach, since the planner still elects to spill in cases where |
|
Confirmed this against main, where llama_cpp.py:25088 still folds the pipeline reserve into the flat per-device term and _usable_vram takes it off device 0, and the thread count at :25106 is still logical rather than physical. The re-run with the flag actually set backs both changes, so I will get this reviewed. |
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ 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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 58804d667b
ℹ️ 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".
| args = [str(arg) for arg in extra_args] if extra_args else [] | ||
| if any(arg.partition("=")[0] in _GENERATION_CPU_AFFINITY_FLAGS for arg in args): | ||
| return None |
There was a problem hiding this comment.
Account for inherited CPU affinity
When the child inherits LLAMA_ARG_CPU_MASK or LLAMA_ARG_CPU_RANGE (especially with strict placement), llama-server applies the same generation affinity represented by these argv flags, but this check examines only extra_args; the inherited variables are neither passed into this helper nor scrubbed before spawn. The planner therefore prices the host's full physical-core count and can report a substantially understated spill penalty for a server restricted to a few CPUs. Inspect the launch environment here as well, or decline pricing when an inherited generation-affinity setting is present.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Rejected: neither ggml-org nor unslothai llama.cpp registers LLAMA_ARG_CPU_MASK or LLAMA_ARG_CPU_RANGE. common/arg.cpp registers -C/--cpu-mask and -Cr/--cpu-range without set_env, while --threads explicitly uses set_env("LLAMA_ARG_THREADS"): https://github.com/ggml-org/llama.cpp/blob/master/common/arg.cpp#L1513-L1552. These environment names do not affect the child, so declining a plan for them would reject inert host variables.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9b47e1048c
ℹ️ 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".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e194aac694
ℹ️ 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".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e194aac694
ℹ️ 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".
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
Line-count breakdown
This is not 1,393 lines of runtime behavior. The production diff is 474 additions and 70 deletions. The size comes mainly from regression coverage for single and multi-GPU boundaries, heterogeneous splits, full residency, partial and full spill, CPU-only planning, SMT and non-SMT topology, restricted affinity, offline CPUs, invalid topology fallbacks, Windows, Linux, WSL, macOS, NVIDIA, AMD, Vulkan, Metal, tensor-split rounding, cache distribution, context seams, and load cleanup. |
mahiatlinux
left a comment
There was a problem hiding this comment.
Planner and offload behavior verified across the covered hardware and boundary cases.
|
All reviewed and fixed, LGTM. full fork CI results |
Main landed #9863 in the meantime, which covers the same two seams this branch had an earlier version of, so the overlap resolves to main: llama_cpp.py main's _linux_math_core_count plus its richer _spilled_decode_threads, which returns Optional[int] and declines on SMT oversubscription or CPU affinity that a physical-core model cannot represent. This branch's simpler psutil-only helper is superseded, and the call site now reads main's precomputed decode_threads with its None guard. offload_planner.py comment reflow only; pipeline_overhead_bytes and host_ram_headroom_bytes are unchanged either side. In the seam tests the physical-core cases (monkeypatched affinity, the llama.cpp fallback ladder, the thread-override suite) come from main, because they test the implementation main owns. Kept this branch's lm_head-ordering sweep over #9863's single 4608 fixture: the sweep spans 4096 to 14336, already includes 4608, and asserts the ORDER of every plan the seam produces, so it does not retire itself silently when the cost gate declines a particular card size. Also dropped two comment blocks auto-merge duplicated where main's reflow rewrote a line this branch had also touched. 311 of 312 offload tests pass. The one failure, test_oversubscribed_decode_threads_decline_spill_planning, reproduces on main at 368e1cb and is not from this merge.
…lanner Brings the branch's main merge and the fitter-fallback scoring commit (6735632) together with the ladder work: sub-FFN rungs, the projector, parallel-slot and draft rungs, the MoE long-prompt gate, context-last reduction and the BACK_FIRST default. Resolutions worth knowing about: - offload_planner.py keeps the ladder's unit-based selection and takes the remote's _fit_fallback_placement verbatim. The per-device selection main gained in #9863 is ported onto units: _device_slots does its arithmetic in float32 like llama.cpp, _per_device_usage returns per-device bytes and rows, every device after the first is charged its pipeline reserve against its raw size, and a partial multi-device spill is now re-selected from each device's own rows and re-checked as a whole instead of abstaining outright. When the output device cannot close its shortfall with FFN rows, the lm_head rung is tried per device before abstaining. - The 1536 MiB reserve floor is priced in load_model's snapshot from what the usable budget already withheld (_reserve_floor_bytes) and reaches the seam as one explicit term applied after the projector surcharge leaves the fused terms, so fixtures that pass their own overhead are not silently raised. - Two ported multi-device tests are re-anchored from the largest-first tie break to BACK_FIRST, and the multi-GPU drafter control gives the second card the pipeline reserve it now has to hold.
…-threads spill test (#11120) * Studio backend tests: pin the logical CPU count in the oversubscribed-threads spill test test_oversubscribed_decode_threads_decline_spill_planning stubs os.sched_getaffinity to 16 CPUs to say "affinity is the whole machine", but _spilled_decode_threads takes the logical count from psutil.cpu_count(logical = True), which the test leaves real. On a host wider than 16 threads that 16-CPU affinity reads as a taskset, the function declines on the affinity arm, and the oversubscription the test is about is never reached: both halves come back None and the second assertion fails. Green iff the host has 16 or fewer logical CPUs, which is why it has survived since #9863 on 2026-08-29 -- GitHub-hosted runners are 2 to 4 vCPU and never show it. It reproduces on any wide self-hosted runner; measured on a 192-thread host. taskset does not reproduce it, because psutil.cpu_count ignores affinity and still reports the whole machine. Stub psutil as well. _SmtHost is already defined at module scope as the 16 logical / 8 physical host this test assumed, and the two affinity tests directly above it pin psutil the same way for the same reason, so this is the established pattern in the file rather than a new one. The assertions are unchanged and both still bite. Dropping the `if actual > physical: return None` arm turns the first one red; making the in-budget arm return None turns the second one red. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
…-threads spill test (unslothai#11120) * Studio backend tests: pin the logical CPU count in the oversubscribed-threads spill test test_oversubscribed_decode_threads_decline_spill_planning stubs os.sched_getaffinity to 16 CPUs to say "affinity is the whole machine", but _spilled_decode_threads takes the logical count from psutil.cpu_count(logical = True), which the test leaves real. On a host wider than 16 threads that 16-CPU affinity reads as a taskset, the function declines on the affinity arm, and the oversubscription the test is about is never reached: both halves come back None and the second assertion fails. Green iff the host has 16 or fewer logical CPUs, which is why it has survived since unslothai#9863 on 2026-08-29 -- GitHub-hosted runners are 2 to 4 vCPU and never show it. It reproduces on any wide self-hosted runner; measured on a 192-thread host. taskset does not reproduce it, because psutil.cpu_count ignores affinity and still reports the whole machine. Stub psutil as well. _SmtHost is already defined at module scope as the 16 logical / 8 physical host this test assumed, and the two affinity tests directly above it pin psutil the same way for the same reason, so this is the established pattern in the file rather than a new one. The assertions are unchanged and both still bite. Dropping the `if actual > physical: return None` arm turns the first one red; making the in-budget arm return None turns the second one red. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Two wrong inputs to the offload planner, both found while checking #9861. Independent of the default, which #9862 already moved.
1. A single card was charged a layer-split reserve
_PIPELINE_PER_DEVICE_OVERHEAD_MIBis the fixed cost of splitting layers across devices. Every other use of it inllama_cpp.pycharges it for devices AFTER the first --max(0, n_gpus - 1) * _pipeline_overhead_bytesat :18664 and :18922, andn > 1guards at :18761 and :19186 -- because the first device's share is already folded into the compute buffer. The comment at :18277 says so outright: "k=1 adds nothing".The planner path folded it into
PlanOptions.overhead_bytes_per_deviceinstead, and_usable_vramsubtracts that from every device. So a single-GPU load had a GiB withheld that nothing was ever going to allocate, and the planner then spilled real blocks to cover a deficit it had invented.That is the "spilled into headroom that was there" symptom in #9861, where llama.cpp kept 1.4 to 2.5 GB more resident than the planner treated as available. It reproduces in our own fixtures: six seam tests were sized against the inflated budget, and with the GiB returned three of them now report "the whole load fits in VRAM (15.00 of 15.00 GiB usable), so nothing is spilled".
PlanOptions.pipeline_overhead_bytesis now its own term, chargedmax(0, n_devices - 1)times.2. The cost model was told hyperthreads, not cores
HostProfile(threads = os.cpu_count() or 1)counts every logical CPU. Studio deliberately leaves--threadsunset, and this file already documents what that means at :20452: an unset--threadsmakes llama.cpp size its pool fromcommon_cpu_get_num_math, which counts physical cores.So on a 6-core / 12-thread desktop the model was told 12. Through
generation_slowdown's Amdahl fit that is a 3.9x penalty multiplier where the truth is about 7.5x -- roughly half the real cost of a spill, on exactly the class of host #9861 measured._spilled_decode_threads()asks psutil for the physical count and falls back toos.cpu_count()when it cannot answer, rather than assuming a 2:1 SMT ratio that would be wrong on a machine without SMT.Tests
Two new ones. The split-reserve test asserts the user-visible effect (a load sized to fit in the disputed GiB comes back resident) and then the arithmetic directly through
_usable_vram, because the two-card plan-level answer is dominated by the partial-spill abstain and would read the same either way. The thread test pins the physical count and the fallback.Six existing seam fixtures were re-sized, not re-baselined: each had been tuned against the inflated budget, so their deficits came partly from the phantom GiB. Each keeps the claim it was written for and says in a comment why the number moved. The two-card drafter case needed 9 GiB rather than 10 for a subtler reason -- with the reserve corrected, the old pair only needed a PARTIAL spill, which abstains before reaching the per-device check that test is actually about.
510 offload tests pass.