Skip to content

Fix main: price the serving-slot reduction at the fit floor, not the Auto offload context - #9594

Merged
danielhanchen merged 3 commits into
mainfrom
fix-slot-reduction-probe
Aug 23, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
fix-slot-reduction-probe

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Backend tests (the (Python 3.13) job) has been red on main since #9492
(1f475d7): 26 failures across test_slot_reduction_context_refit.py and
test_slot_refit_platform_matrix.py, both added by #9345 (1c06bc4). All 59
pass on #9492's merge base 15b9102. The two files were not run on that
branch. Run 32659798911. Every open PR inherits this.

The tests are reporting a real regression, not a stale baseline.

What went wrong

#9492 introduced _AUTO_OFFLOAD_CTX = 8192 and used it in three places that
had a literal 4096. Its reasoning:

Auto only reaches this path when no discrete-GPU subset can hold the model.
llama.cpp must offload layers to host RAM either way, so prefer a context
that remains useful for chat.

That is true of two of the three sites, which are display values: the UI
context ceiling and the file-size-only arm. The third, in load_model's auto
arm, is the probe context of a placement search that runs after it - the
serving-slot reduction, whose entire purpose is to overturn the premise "no
subset can hold the model" by re-asking at fewer slots. Offload is not
unavoidable at that point. It is avoidable roughly in the band where the
constant now breaks it.

Raising the probe can only make that search fail, and its failure is
all-or-nothing: the block returns early, so the load keeps the full slot count
it could not afford and offloads anyway.

Measured on a 12 GiB card at 4 requested slots:

weights before #9492 on main
hybrid 10,900 MiB 1 slot, ctx 7,680, GPU 4 slots, ctx 8,192, offload
hybrid 11,000 MiB 1 slot, ctx 6,144, GPU 4 slots, ctx 8,192, offload
hybrid 11,100 MiB 1 slot, ctx 4,608, GPU 4 slots, ctx 8,192, offload
dense 9,800 MiB 2 slots, ctx 4,608, GPU 4 slots, ctx 8,192, offload
dense 10,000 MiB 1 slot, ctx 6,144, GPU 4 slots, ctx 8,192, offload
dense 10,500 MiB 1 slot, ctx 4,096, GPU 4 slots, ctx 8,192, offload

8 of 14 sizes swept flip from fully GPU-resident to host offload: the ~3x
decode collapse (#6718) named in _slots_that_fit_on_gpu's own docstring as
the thing it exists to prevent. A user gets 8,192 tokens at a third of the
speed where they previously had 6,144 at full speed. That is not a Pareto move.

The arithmetic, at 10,200 MiB against a 12288 x 0.97 = 11,919.36 MiB budget.
Candidates are 707.375 MiB apart (557.75 MiB of per-slot compute buffer plus
149.625 MiB of per-slot Mamba state, both context-independent), while probing
higher costs a uniform 64 KiB/token. The 2-slot candidate is 11,685.0 MiB at
4096 and 11,947.0 MiB at 8192
. A display constant moved it across the budget
by 27.6 MiB.

Both sides of the argument

For "#9492 is right and the tests are stale": the tests hardcode 26 literals,
every one downstream of a 4096 anchor, and one has the number in its name.
Both resulting plans sit exactly on the VRAM frontier (11,911.0 and 11,914.2
MiB against 11,919.36), so neither over-commits, and "1 slot with 18,688
tokens" instead of "2 slots with 7,680" is arguably the better chat experience,
which is what the PR set out to deliver. All 116 tests #9492 added pass.

Against, which wins: the change moves placement, not presentation. #9492's
own comment at llama_cpp.py:2831-2841 makes placement-neutrality the stated
correctness criterion for moving this constant. Three ways it is violated:

  1. --fit off becomes --fit on for the whole band above.
  2. Device pinning changes. At 10,200 MiB on a 12 GiB plus 1.25 GiB pair, the
    2-slot candidate no longer fits card 0 alone, the selector adds card 1, and
    the re-fit runs across both: CUDA_VISIBLE_DEVICES goes "0" to "0,1" -
    the layer split the block's comment says this path avoids.
  3. The final slot count stops being monotone in the ask. Asking for 2 returns
    2; asking for 3 returns 1.

#9492's coverage was structurally blind to all of it:
test_auto_offload_ctx_platform_matrix.py never passes n_parallel, so all
108 cells ran at the GgufLoadIntent default of 1, and the block is gated on
n_parallel > 1. The commit's "identical in all 108" is true of the cells it
ran and not of the feature.

The fix

Three changes, all inside the reduction block:

  1. The search is priced at a new local _reduce_ctx, which is
    min(effective_ctx, _FIT_MIN_CTX) and never _AUTO_OFFLOAD_CTX. An
    explicit context is the exception and keeps its own value, since it launches
    verbatim and a slot count picked at a lower context would not hold.
    _FIT_MIN_CTX rather than a literal because _largest_ctx starts its
    binary search at the same floor and the two must agree; that search's
    min(4096, _native) now reads the constant too.
  2. The re-fit is applied whenever it exists, not only when it exceeds
    effective_ctx. That gate was safe only while the search and
    effective_ctx shared a value. The re-fit is the largest context the final
    slot count holds on the cards the reduction chose, so anything else launches
    a context that was never priced.
  3. The published ceiling replaces the offload anchor instead of max()-ing
    with it. Reaching there means the anchor described a plan this block just
    superseded, and max() would advertise a context the launched plan cannot
    serve - the same ceiling/selection inversion Studio: improve Auto context selection and share remembered model settings #9492 removed, pointing the
    other way.

Placement is now invariant to _AUTO_OFFLOAD_CTX, which is what that constant
being a display value means. Everything #9492 intended is kept: the UI ceiling
anchor, the file-size-only arm, and the 8,192 context on loads that do
genuinely offload.

Tests

Two of the 26 were genuinely stale. Both assert the offload fallback itself,
never reach the reduction, and hardcoded 4096. They now read
min(_AUTO_OFFLOAD_CTX, NATIVE_CTX) so the constant can move again, each with
a guard that the case still lands in the branch its name claims. The
explicit-context one keeps its 32768 literal, because honouring the request
verbatim is the contract and a derived value there would assert nothing.

TestTheReductionIsPricedAtTheFitFloor is new and pins the root cause:

  • slots, residency, context and devices unchanged across _AUTO_OFFLOAD_CTX in
    {4096, 8192, 16384, 32768}, so this cannot be satisfied by re-baselining
    the constant into the expectation;
  • every size in the rescued band launches --fit off, with slots < 4 proving
    it was rescued by the reduction rather than fitting outright;
  • a counterweight at 11,400 MiB that must still offload, so the band above is
    not green merely because everything is;
  • the slot count is monotone in the ask, guarded against being all one number.

test_compute_buffer.py's test_wired_into_both_call_sites counted source
substrings and reddened on the variable rename here. It now reads the call
sites out of load_model's parse tree and requires every call passing
split_extra_bytes to pass _cc_split_extra(<name>). That turned up a
fourth call site the counting version could not see: it counted the two
spelled effective_ctx plus one spelled ctx, and the projector-floor site
spells its context _mm_floor_ctx, so the check that claimed to cover every
call site had never covered it. It has been wired correctly all along.

Anti-vacuity evidence

One mutation at a time against the repaired suites (77 tests):

Mutation Result
this branch 77 passed
probe put back at the offload fallback 35 failed
ceiling put back to max() 7 failed
re-fit gated on being an increase again 16 failed
explicit context probed at the floor 2 failed
_AUTO_OFFLOAD_CTX moved to 4096 77 passed
_AUTO_OFFLOAD_CTX moved to 16384 3 failed

Every reintroduction of the defect reddens. Moving the constant to 4096 is
fully green, which is the invariance the fix is for. At 16384 the three
failures are in test_slot_refit_ctx_checkpoints.py, where the charged case
stops reaching the re-fit at all; placement there is still identical at both
anchors (4 slots, --fit on, same search) and only the launched context tracks
the constant, by design. Left alone rather than re-scoped, since that file is
green before and after this change.

Local runs

  • the three re-fit suites: 77 passed, was 33 passed and 26 failed.
  • 81 placement, VRAM, GPU, context and llama.cpp backend test files, the blast
    radius of this change: 4033 passed, 2 skipped, 0 failed. Was 1 failed.

The other failure on the same run, Repo tests (CPU), is a different root
cause and is fixed in #9591.

…ad context

`Backend tests` has been red on main since #9492 (1f475d7), 26 failures across
test_slot_reduction_context_refit.py and test_slot_refit_platform_matrix.py,
both added by #9345 (1c06bc4). All 59 pass on #9492's merge base (15b9102).
The two files were not run on that branch.

The tests are reporting a real regression, not a stale baseline.

#9492 introduced _AUTO_OFFLOAD_CTX = 8192 and put it in three places that had a
literal 4096. Its reasoning: "Auto only reaches this path when no discrete-GPU
subset can hold the model. llama.cpp must offload layers to host RAM either
way, so prefer a context that remains useful for chat." That is true of two of
the three sites, which are display values. The third, in load_model's auto arm,
is the probe context of a placement search that runs AFTER it, in a block whose
entire purpose is to overturn the premise "no subset can hold the model" by
re-asking at fewer serving slots. Offload is not unavoidable at that point; it
is avoidable in roughly the band where the constant now breaks it.

What that costs, measured on a 12 GiB card at 4 requested slots:

| weights            | before #9492            | on main                |
|--------------------|-------------------------|------------------------|
| hybrid 10,900 MiB  | 1 slot, ctx 7,680, GPU  | 4 slots, ctx 8,192, offload |
| hybrid 11,000 MiB  | 1 slot, ctx 6,144, GPU  | 4 slots, ctx 8,192, offload |
| hybrid 11,100 MiB  | 1 slot, ctx 4,608, GPU  | 4 slots, ctx 8,192, offload |
| dense  9,800 MiB   | 2 slots, ctx 4,608, GPU | 4 slots, ctx 8,192, offload |
| dense 10,000 MiB   | 1 slot, ctx 6,144, GPU  | 4 slots, ctx 8,192, offload |
| dense 10,500 MiB   | 1 slot, ctx 4,096, GPU  | 4 slots, ctx 8,192, offload |

8 of 14 sizes swept flip from fully GPU-resident to host offload, the ~3x
decode collapse (#6718) named in _slots_that_fit_on_gpu's own docstring as the
thing it exists to prevent. The failure is all-or-nothing: when no slot count
fits at the probe, the block returns early, so the load keeps the full slot
count it could not afford AND offloads. It gets 8,192 tokens at a third of the
speed where it previously had 6,144 at full speed. Not a Pareto move.

The arithmetic, at 10,200 MiB against a 12288 x 0.97 = 11,919.36 MiB budget.
Candidates are 707.375 MiB apart (557.75 MiB of per-slot compute buffer plus
149.625 MiB of per-slot Mamba state, both context-independent), while probing
higher costs a uniform 64 KiB/token. The 2-slot candidate is 11,685.0 MiB at
4096 and 11,947.0 MiB at 8192. A display constant moved it across the budget by
27.6 MiB.

Two further consequences, both of which #9492 explicitly claimed it would not
have. Its own comment at llama_cpp.py:2831-2841 makes placement-neutrality the
correctness criterion for moving this constant. Placement moved anyway, through
a call site the analysis did not enumerate:

- Device pinning. At 10,200 MiB on a 12 GiB plus 1.25 GiB pair, the 2-slot
  candidate no longer fits card 0 alone, so the selector adds card 1 and the
  re-fit runs across both: CUDA_VISIBLE_DEVICES goes from "0" to "0,1", the
  layer split the block's comment says this path avoids.
- Monotonicity. Asking for 2 slots returns 2; asking for 3 returns 1.

#9492's coverage was structurally blind to all of this:
test_auto_offload_ctx_platform_matrix.py never passes n_parallel, so all 108
cells ran at the GgufLoadIntent default of 1, and the block is gated on
n_parallel > 1.

## The fix

Three changes in llama_cpp.py, all in the reduction block:

1. The search is priced at a new local `_reduce_ctx`, which is
   `min(effective_ctx, _FIT_MIN_CTX)` and never _AUTO_OFFLOAD_CTX. An explicit
   context is the exception and keeps its own value, since it launches verbatim
   and a slot count picked at a lower context would not hold. `_FIT_MIN_CTX`
   rather than a literal because _largest_ctx starts its binary search at the
   same floor and the two have to agree; that search's `min(4096, _native)` now
   reads the constant too.
2. The re-fit is applied whenever it exists, not only when it exceeds
   effective_ctx. That gate was safe only while the search and effective_ctx
   shared a value. The re-fit is the largest context the FINAL slot count holds
   on the cards the reduction chose, so anything else launches a context that
   was never priced.
3. The published ceiling replaces the offload anchor rather than max()-ing with
   it. Reaching there means the anchor described a plan this block superseded,
   and max() would advertise a context the launched plan cannot serve, the same
   ceiling/selection inversion #9492 removed, pointing the other way.

Placement is now invariant to _AUTO_OFFLOAD_CTX, which is what the constant
being a display value means. #9492's intended improvements are all kept: the UI
ceiling anchor, the file-size-only arm, and the 8,192 context on loads that do
genuinely offload.

## Tests

Two of the 26 were genuinely stale: both assert the offload fallback itself,
never reach the reduction, and hardcoded 4096. They now read
`min(_AUTO_OFFLOAD_CTX, NATIVE_CTX)` so the constant can move again, each with
a guard asserting the case still lands in the branch its name claims. The
explicit-context one keeps its 32768 literal, since honouring the request
verbatim is the contract and a derived value there would assert nothing.

TestTheReductionIsPricedAtTheFitFloor is new and pins the root cause:

- the plan's slots, residency, context and devices are unchanged across
  _AUTO_OFFLOAD_CTX in {4096, 8192, 16384, 32768}, so this cannot be satisfied
  by re-baselining the constant into the expectation;
- every size in the rescued band launches --fit off, with `slots < 4` proving
  it was rescued by the reduction rather than fitting outright;
- a counterweight at 11,400 MiB that must still offload, so the band above is
  not green merely because everything is;
- the final slot count is monotone in the requested count, with a guard that
  the row is not all the same number.

test_compute_buffer.py's test_wired_into_both_call_sites counted source
substrings and reddened on the variable rename here. It now reads the call
sites out of load_model's parse tree and requires every one that passes
split_extra_bytes to pass `_cc_split_extra(<name>)`. That turned up a fourth
call site the counting version could not see: it counted the two spelled
`effective_ctx` plus one spelled `ctx`, and the projector-floor site spells its
context `_mm_floor_ctx`, so the check that claimed to cover every call site had
never covered it. It has been wired correctly all along.

## Anti-vacuity

One mutation at a time against the repaired suites (77 tests):

| Mutation                                          | Result           |
|---------------------------------------------------|------------------|
| this branch                                        | 77 passed        |
| probe put back at the offload fallback             | 35 failed        |
| ceiling put back to max()                          | 7 failed         |
| re-fit gated on being an increase again            | 16 failed        |
| explicit context probed at the floor               | 2 failed         |
| _AUTO_OFFLOAD_CTX moved to 4096                    | 77 passed        |
| _AUTO_OFFLOAD_CTX moved to 16384                   | 3 failed         |

Every reintroduction of the defect reddens. Moving the constant to 4096 is
fully green, which is the invariance the fix is for. At 16384 the three
remaining failures are in test_slot_refit_ctx_checkpoints.py, where the
charged case stops reaching the re-fit at all; placement there is still
identical at both anchors (4 slots, --fit on, same search) and only the
launched context tracks the constant, by design. Left alone rather than
re-scoped: that file is green before and after this change.

## Local runs

- the three re-fit suites: 77 passed, was 33 passed 26 failed.
- 81 placement, VRAM, GPU, context and llama.cpp backend test files, the blast
  radius of this change: 4033 passed, 2 skipped, 0 failed. Was 1 failed.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 297ae17702

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

@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: e57fcd32da

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

_refit[0],
n_parallel,
)
effective_ctx, gpu_indices = _refit

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 Preserve a fitting non-aligned native context

When an Auto load has a native context below 4096 that is not divisible by 256 and reducing the slot count succeeds, the initial reduction probe has already proved that exact native context fits, but _largest_ctx only tests 256-token multiples and returns a smaller value. Applying _refit unconditionally here—where the old code only applied increases—therefore silently lowers the launched -c by up to 255 tokens (for example, 300 becomes 256); preserve the proven exact context or explicitly test it before accepting the rounded-down result.

Useful? React with 👍 / 👎.

@danielhanchen
danielhanchen merged commit 7208e2b into main Aug 23, 2026
48 checks passed
@danielhanchen
danielhanchen deleted the fix-slot-reduction-probe branch August 23, 2026 22:04
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