Skip to content

Stop the remote GGUF guard reserving an output buffer for the first slot - #8635

Closed
danielhanchen wants to merge 2 commits into
mainfrom
fix/remote-gguf-first-slot-output-buffer
Closed

danielhanchen wants to merge 2 commits into
mainfrom
fix/remote-gguf-first-slot-output-buffer

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Aug 13, 2026 •

Copy link
Copy Markdown
Member

Rebased onto current main, which now carries #8641's extraction of the reserve into _remote_gguf_compute_reserve_gb. This PR keeps that extraction and changes only what it charges.

Which PR introduced what

Failure Introduced by Fixed by
ImportError: cannot import name 'openai_codex_auth_router' #8551, which added the router to main.py without the test stub #8590, merged
ValueError: Responses event type is missing (2 tests) #8551, which added response_event_type and wired only openai_codex_client to pass the SSE event name #8608, merged
TestEstimateGgufRequiredGb off by 0.575732421875 GB (8 tests) #8524 #8641 made the tests green; this PR settles the charge

Why the charge is the part still open

#8524 made two changes to the remote branch. The default micro-batch is right and stays: a request that emits no batch flags still launches at llama.cpp's 512, and the local branch already charges against that default.

The slot count is the other one. max(1, n_parallel) reserves an output buffer for the first slot, and the local branch, which reads the header and is fitted to measured allocations, does not:

output_slots = par if self.is_embedding_gguf else max(0, par - 1)
# Matches measured chat {1:36, 2:492, 4:1388, 8:3220} MiB.

One slot reserves the activation scratch and no output buffer. The remote branch stands in for that same computation on a model whose header cannot be read yet, and the two are meant to agree; a comment already in this function names the failure mode, "the same model 409s while remote and loads once cached". Charging the first slot made every uncached chat load read 0.58 GB heavier than the identical cached one, on an estimator whose only consumer answers 409.

So the slot count returns to the local branch's form, and the activation scratch it was standing in for is charged explicitly under a ceiling, the way the vocab already is. Without that a one-slot remote load would carry the KQ mask alone, which is the same remote-vs-cached divergence pointing the other way.

Measured

Against _estimate_compute_buffer_bytes for a 152k-vocab, 5120-wide chat model at the default micro-batch:

1 slot    cached 0.0449 GiB    remote 0.0726 GiB    +0.028
2 slots   cached 0.3782 GiB    remote 0.6476 GiB    +0.269

Remote now sits just above cached instead of half a gigabyte above it. test_the_remote_reserve_tracks_what_the_cached_path_charges pins both directions: never under the cached figure, and at one slot not a whole ceiling output buffer over it.

Suites that touch the estimator or the buffers it models: 818 passed (test_batch_sizes_per_load, test_chat_load_during_training, test_mtp_drafter_companion, test_compute_buffer, test_embedding_gguf_launch, test_kv_cache_estimation, test_diffusion_memory).

The residual, stated plainly

A remote embedding GGUF now reads low, by 0.31 GB at one slot for a 152k vocab, because the cached branch charges an output buffer at every slot and this one cannot tell an embedding repo from a chat repo before the header is readable. #8524's own comment records that there is no reliable pre-load probe for it. The under-charge is bounded by the model's real vocab and those repos are small; the alternative is half a gigabyte of headroom taken from every uncached chat load. That trade is the reviewable decision here, and it reverses part of a merged PR, so it deserves a second opinion rather than my word for it.

Not measured

No llama-server was launched. The single-slot 36 MiB figure is the one already recorded in the estimator, not a fresh reading, and the embedding residual is derived from the vocab ceiling rather than observed.

@danielhanchen

Copy link
Copy Markdown
Member Author

Re-verified this independently before recommending it, since it partially reverses a merged PR.

The load-bearing claim is that the local branch never charges an output buffer for the first slot. core/inference/llama_cpp.py:6751:

output_slots = par if self.is_embedding_gguf else max(0, par - 1)
# Matches measured chat {1:36,2:492,4:1388,8:3220} MiB.

So one slot reserves activation scratch and no output buffer, and that is a fitted measurement rather than a guess. Charging one anyway bills 262144 * 512 * 4 * 1.15 = 0.575 GiB, roughly 16x the measured single-slot buffer, on every uncached load.

The trade-off is genuine and worth stating in the open rather than burying. The remote branch has no header, so it cannot ask is_embedding_gguf. #8524 resolved that unknown toward "assume embedding"; this resolves it toward "assume chat". Neither is free:

assume embedding (#8524) assume chat (this PR)
ordinary model, 1 slot over-charged 0.58 GiB correct
remote embedding GGUF, 1 slot correct under-charged ~62 MiB for a bge-class vocab
2+ slots correct covered by the 262144 vocab ceiling

Assuming chat is the better default: it is the overwhelmingly common case, the error is an order of magnitude smaller, and it removes a divergence where an uncached model cost 0.58 GiB more than the identical model once on disk. Since this guard 409s a load that will not fit beside a running training job, the over-charge was refusing loads for bytes that never become resident.

Confirmed locally: studio/backend/tests/test_chat_load_during_training.py 118 passed, 2 subtests passed.

This is currently the only thing keeping Backend CI red across the repo, reproduced on a clean origin/main checkout with no PR applied.

@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: 23251121c3

ℹ️ 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 studio/backend/routes/inference.py Outdated
# embedding GGUF does output its whole first micro-batch, but its real
# vocab sits far under the 262144 ceiling assumed here, so from two slots
# on the ceiling already covers the slot this drops.
_out_slots = n_parallel if tensor_parallel else max(0, n_parallel - 1)

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 Retain the first output buffer for remote embedding GGUFs

For an uncached remote embedding GGUF using non-tensor placement with n_parallel = 1, this sets _out_slots to zero even though the launched embedding server allocates an output buffer for every slot (_estimate_compute_buffer_bytes uses output_slots = par for embedding models). With an explicit large micro-batch such as 32768, even a 32K vocabulary leaves roughly 4.6 GiB unaccounted for, so the training guard can approve a load that exhausts VRAM and terminates the active training run. Preserve the first-slot reserve when the remote model is known or may be an embedding model.

Useful? React with 👍 / 👎.

Comment thread studio/backend/routes/inference.py Outdated
# embedding GGUF does output its whole first micro-batch, but its real
# vocab sits far under the 262144 ceiling assumed here, so from two slots
# on the ceiling already covers the slot this drops.
_out_slots = n_parallel if tensor_parallel else max(0, n_parallel - 1)

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 Account for single-slot activation scratch remotely

For a remote chat GGUF using non-tensor placement with n_parallel = 1, this also zeros the only flat-buffer allowance, although the local estimator always charges act_scratch = 4 * n_embd * ubatch * 4 even when chat needs no full output buffer. The remote path consequently retains only the KQ mask: at the default micro-batch it misses the measured single-slot scratch, while an explicit 32768-token micro-batch on an 8192-wide model leaves about 4.6 GiB unaccounted for. The removed ceiling-sized output term previously covered this allocation, so the guard can now admit a load that OOMs the active training run; add a separate conservative activation-scratch reserve before dropping the chat output slot.

Useful? React with 👍 / 👎.

@danielhanchen

Copy link
Copy Markdown
Member Author

CI status, since the red checks here are not this PR's.

Measured on main's own Backend CI at 75d983dcf (#8590's merge commit, no PR applied), main has 10 failures in two families: 8 in TestEstimateGgufRequiredGb, all off by the same 0.575732421875 GiB, and 2 ValueError: Responses event type is missing.

This PR clears all 8 GGUF failures. The Backend job reports 2 failed, 22750 passed, and those 2 are the Responses family, which #8608 fixes. #8608 shows the mirror image: 0 Responses failures, and the same 8 GGUF failures this PR removes.

So the two are complementary and mutually blocking. Merging both should take Backend CI to zero.

The Chat UI Tests red is a Playwright failure, Voice model picker did not wheel-scroll: TimeoutError. This PR changes three files, all backend: studio/backend/routes/inference.py plus two files under studio/backend/tests/. It touches no frontend code and no voice picker, so it cannot cause a wheel-scroll timeout.

@danielhanchen

Copy link
Copy Markdown
Member Author

Both items are genuine. The first is fixed in 1b85738.

_estimate_compute_buffer_bytes is act_scratch + out_buffer * output_slots, and act_scratch = 4 * n_embd * ub * 4 is charged unconditionally: it is the whole of the measured 36 MiB single-slot chat buffer. Since this PR makes the ceiling output buffer multiply by slots - 1, a one-slot remote load was left carrying the KQ mask and nothing else, running 4.6 GiB short at ub=32768 on an 8192-wide model. Your figure is exact.

The fix adds an act_scratch_bytes term mirroring the local branch, with _ASSUMED_MAX_EMBD = 8192 (Llama-3-70B and Qwen2.5-72B are 8192, DeepSeek-V3 7168, Gemma-3-27B 5376), doubled per device in tensor mode. 8192 rather than 16384 is deliberate: 16384 is exactly _ASSUMED_MAX_VOCAB / 16, so a ceiling there re-charges the dropped slot under another name and re-breaks the eight estimate tests by the same 0.575 GiB.

Named test: test_the_remote_guard_charges_the_single_slot_activation_scratch. Reverting the term gives assert 4.0 == 8.599999999627471.

The second item I am leaving open, because the remedy as stated cannot be written. There is no remote embedding signal: is_embedding_gguf reads _pooling_type from the GGUF header, which is unreadable before download, and --embedding is in the banned user-flag set, so llama_extra_args cannot carry it either. The "or may be" branch is a straight revert to max(1, n_parallel), which reinstates the 0.575 GiB on every uncached load. Closing it needs new plumbing, a ranged header read or a catalog-side pooling-type hint, and belongs in its own PR.

One correction to this PR's own description while I am here. It says 2+ slots are "covered by the 262144 vocab ceiling". That is false for embedding GGUFs above a 131072 vocab: Qwen3-Embedding-8B at 2 slots reserves 44.88 GiB at launch against 36.80 GiB charged, 8.08 GiB short, where pre-PR it was covered. After the fix, chat is never under-reserved at any slot count or micro-batch; embedding is still under-reserved at 1 and 2 slots.

…them

#8524 changed the remote output-buffer slot count from n_parallel - 1 to
max(1, n_parallel), so every single-slot uncached estimate gained about 0.58 GB.
#8641 then extracted the reserve and held it at zero in the drafter tests, which
turned Backend CI green without settling whether the charge is right.

It is not. The local branch, which reads the header and is fitted to measured
allocations, charges act_scratch + output_buffer * (slots - 1) for a chat model:
{1:36, 2:492, 4:1388, 8:3220} MiB, so one slot reserves the scratch and no output
buffer. The remote branch stands in for that same computation on a model whose
header is not readable yet, and the two are supposed to agree: an existing comment
in the same function calls out the case where "the same model 409s while remote and
loads once cached" as the thing to avoid. Charging the first slot made every
uncached chat load read 0.58 GB heavier than the identical cached one, on an
estimator whose only consumer refuses the load when it does not fit.

So the slot count goes back to matching the local branch, and the activation
scratch it was standing in for is charged explicitly, at a ceiling the way the
vocab already is. Without that a one-slot remote load would carry the KQ mask
alone, which is the same divergence pointing the other way.

Measured against _estimate_compute_buffer_bytes for a 152k-vocab, 5120-wide chat
model at the default micro-batch, remote now sits just above cached rather than
half a gigabyte above it:

  1 slot   cached 0.0449 GiB   remote 0.0726 GiB   (+0.028)
  2 slots  cached 0.3782 GiB   remote 0.6476 GiB   (+0.269)

Known residual: a remote EMBEDDING GGUF now reads low, by 0.31 GB at one slot for
a 152k vocab, because that branch charges an output buffer at every slot and this
one cannot tell an embedding repo from a chat repo before the header is readable.
#8524's own comment records that there is no reliable pre-load probe for it. The
under-charge is bounded by the model's real vocab and those repos are small, which
is the same small-half trade the activation scratch used to be, against half a
gigabyte of headroom taken from every chat load.
@danielhanchen
danielhanchen force-pushed the fix/remote-gguf-first-slot-output-buffer branch from 16a9f50 to c4ef549 Compare August 13, 2026 06:52

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

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

# motivated charging it, but its real vocab sits far below the 262144 ceiling
# assumed here, so from two slots on the ceiling already covers the slot this
# drops.
_out_slots = n_parallel if tensor_parallel else max(0, n_parallel - 1)

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 Preserve the quantized-cache context scratch

When an uncached non-tensor chat GGUF uses a quantized KV cache, this drops the first-slot ceiling buffer even though the remote branch does not receive cache_type_kv and therefore budgets only the f16 KQ mask. Once cached, _compute_buffer_ctx_bytes additionally charges the context-linear dequantization scratch; for a regular-attention model with n_embd=5120, 32K context, and a 32768-token micro-batch, the new mask-plus-activation reserve is about 7.6 GiB while the cached compute estimate is about 25.4 GiB. The removed output reserve previously covered this case, so this change can let the coexistence guard approve a load that exhausts VRAM; pass the planned cache type into the remote estimator and conservatively reserve its context scratch before zeroing this slot.

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.

Confirmed, and this one decides the PR. Measured on this branch for a 5120-wide model at a 32768 context and micro-batch:

cached context buffer   f16 3.00 GiB    q8_0 22.50 GiB
remote reserve, 1 slot                   7.60 GiB
one ceiling output buffer at that ubatch 36.80 GiB

The remote branch never receives cache_type_kv, so it budgets the f16 mask and nothing else, while the cached path charges 22.5 GiB of dequant scratch for the same launch. The first-slot ceiling buffer was covering that, and dropping it takes the estimate far below what the model costs once cached, which is the direction that OOMs the training run rather than the direction that 409s a load.

Same for the other two you raised: _estimate_gguf_kv_gb charges (devices - 1) * _PIPELINE_PER_DEVICE_OVERHEAD_MIB and the architecture-specific context term through _compute_buffer_ctx_bytes, and the remote branch charges neither. All three are terms it cannot compute without the header, so the ceiling output buffer is the only slack standing in for them.

You are also right about the arithmetic in the ceiling comment, and it was wrong in the direction that mattered: out_buffer = vocab * ubatch * 4 against scratch = 16 * n_embd * ubatch puts equality at n_embd = vocab / 4 = 65536, not /16. A 16384-wide ceiling would have been a quarter of one output buffer, so the stated reason for not covering that class does not hold.

So the premise of this PR is wrong. The first-slot charge is not a principled output buffer, but it is load-bearing slack, and removing it is only safe once the terms it stands in for are charged, which the remote path cannot do while the header is unreadable. Closing rather than patching around it; the 0.58 GB over-charge at the default micro-batch is real but small next to a 20 GB shortfall at large ones.

* (2 * devices if tensor_parallel else 1)
* LlamaCppBackend._COMPUTE_BUFFER_SAFETY
)
return (mask_bytes + out_buffer_bytes + act_scratch_bytes) / (1024**3)

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 Add the layer-split device overhead explicitly

For an uncached one-slot chat GGUF explicitly split across multiple GPUs, this return omits the fixed per-extra-device allocation that the cached path adds as (devices - 1) * _PIPELINE_PER_DEVICE_OVERHEAD_MIB (currently 1 GiB per device). With two pinned GPUs and a 1024-token micro-batch, the removed first-slot ceiling contributed about 1.15 GiB and covered that allocation, whereas the replacement activation term contributes only the ordinary flat scratch, leaving the remote estimate roughly 1 GiB below the process that launches. Add the same per-extra-device overhead used by _estimate_gguf_kv_gb, or the guard can approve a layer-split load that OOMs beside training.

Useful? React with 👍 / 👎.

@danielhanchen

Copy link
Copy Markdown
Member Author

This is currently the fix for a red main, which is worth stating plainly since the PR title reads like a narrow optimisation.

main at 22cb12ec6 fails 8 tests with no PR applied:

$ cd studio/backend && python -m pytest tests/test_chat_load_during_training.py -q -k EstimateGgufRequiredGb
8 failed, 45 passed, 65 deselected
AssertionError: 10.575732421875 != 10.0 within 6 places

All 8 are TestEstimateGgufRequiredGb, and the 0.5757 GB is exactly one output buffer.

Bisected to 2c1d09c13 (#8524, launch embedding ggufs with --embedding):

commit result
a7e073e6e (#8435) 53 passed
dac7b51a0 53 passed
2c1d09c13 (#8524) 8 failed, 45 passed
every commit after, through 22cb12ec6 8 failed, 45 passed

The line is in the remote branch of _estimate_gguf_required_gb:

-  _out_slots = n_parallel if tensor_parallel else max(0, n_parallel - 1)
+  # The remote header is unknown, so reserve as if it enables embeddings.
+  _out_slots = max(1, n_parallel)

The intent is defensible, since a remote GGUF's pooling type is genuinely unknown before download. The cost is that every remote GGUF, chat models included, is now charged one output buffer it will not use, so admission refuses loads that do fit. #8524 updated test_batch_sizes_per_load.py and test_compute_buffer.py but not test_chat_load_during_training.py, which is why this landed red rather than being caught.

This branch at ca1ab192a restores the suite:

53 passed, 65 deselected

Worth noting it fixes the source rather than re-baselining the 8 assertions. That distinction matters: those tests exist to check drafter classification, and the extra buffer was incidental to them, so re-baselining would have quietly locked in the over-reservation instead of removing it.

No failing checks here, and the branch is level with main.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex 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: ca1ab192a3

ℹ️ 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 +5932 to +5935
# The 405B class reaches 16384 and is deliberately not covered: 16384 is exactly
# _ASSUMED_MAX_VOCAB / 16, so a ceiling there would make this term one whole ceiling
# output buffer and charge the dropped first slot straight back under another name.
_ASSUMED_MAX_EMBD = 8192

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 Raise the activation ceiling above 8192

For an uncached one-slot remote GGUF whose n_embd exceeds 8192—the adjacent comment itself identifies the supported 16384-wide class—this new ceiling still underprices the activation scratch. With n_ubatch=32768, a 16384-wide model reserves about 9.2 GiB for this term, while the guard charges only 4.6 GiB; the removed first-slot output reserve previously covered that gap, so the guard can now approve a load that OOMs an active training run. The stated equivalence is also off: because the output formula is 4 * vocab * ubatch and the scratch formula is 16 * n_embd * ubatch, a whole ceiling output buffer corresponds to n_embd = _ASSUMED_MAX_VOCAB / 4, not / 16, so covering 16384-wide models would not undo the intended reduction.

Useful? React with 👍 / 👎.

* (2 * devices if tensor_parallel else 1)
* LlamaCppBackend._COMPUTE_BUFFER_SAFETY
)
return (mask_bytes + out_buffer_bytes + act_scratch_bytes) / (1024**3)

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 Preserve Inkling's context-linear reserve

For an uncached one-slot Inkling GGUF using the default f16 cache, this replacement no longer covers the architecture-specific context buffer that the cached path charges in _compute_buffer_ctx_bytes. At a 32768-token context and micro-batch, Inkling's 8192 * n_ctx * (ubatch / 512) term alone is 16 GiB, whereas this remote path now reserves only the 3 GiB mask plus 4.6 GiB activation ceiling; the removed first-slot output term contributed 36.8 GiB and previously covered the allocation. The resulting deficit can let the coexistence guard approve a remote Inkling load that exhausts VRAM, so retain a conservative architecture-specific context allowance while the header is unreadable.

Useful? React with 👍 / 👎.

@danielhanchen

Copy link
Copy Markdown
Member Author

Correction to my comment above: by the time I posted it, main was already green. #8641 merged at 06:37 UTC and I posted at 06:57, having bisected against a checkout fetched at 05:57 without re-fetching. So "this is currently the fix for a red main" was wrong, and the bisect table describes a window that had already closed.

What still holds, and why this PR is not redundant:

#8641 fixed the 8 failing tests by extracting _remote_gguf_compute_reserve_gb and pricing the reserve separately. It did not change the charge. main today still has

_out_slots = max(1, n_parallel)

at routes/inference.py:6181, so every remote GGUF is still charged one output buffer for its first slot, chat models included, and admission still refuses loads that fit.

This branch already contains #8641 (and #8608, #8638), keeps the new helper, and changes only the charge:

_out_slots = n_parallel if tensor_parallel else max(0, n_parallel - 1)

tests/test_chat_load_during_training.py -k EstimateGgufRequiredGb: 53 passed at ca1ab192a, same as main. The value here is the over-reservation, not the red suite.

@danielhanchen

Copy link
Copy Markdown
Member Author

Settling the charge with the measured data, since #8524 and this PR assert opposite things about it and #8641 landed on a third position.

The three claims:

llama_cpp.py carries the measurement outright: # Matches measured chat {1:36,2:492,4:1388,8:3220} MiB. That is enough to decide it. Fitting each model on the 1- and 2-slot rows only, then predicting the other two:

slots measured A: scratch + buffer x (n-1) B: buffer x n
1 36 36 (+0.0%) 246 (+583%)
2 492 492 (+0.0%) 492 (+0.0%)
4 1388 1404 (+1.2%) 984 (-29%)
8 3220 3228 (+0.2%) 1968 (-39%)

Model A is this PR's, with act_scratch = 36 MiB and out_buffer = 456 MiB per additional slot. It lands within 1.2% at every point on two fitted parameters. Model B is #8524's: out by a factor of seven at one slot, and under-reserving by 39% at eight, which is the direction that fails at launch rather than merely wasting headroom.

So #8524's slot count was wrong and this change is right.

The fit also shows the two halves of this PR are inseparable. Without charging activation scratch unconditionally, model A predicts 0 MiB at one slot against a measured 36. Dropping the first output buffer without adding the scratch back is not a smaller version of this fix, it is a different and wrong model. That is what the follow-up commit here corrects.

On #8641, to be fair to it: its stated premise does not survive this fit, but its action was the right one and I would not undo it. Rather than baking either value into the assertions it extracted the reserve into _remote_gguf_compute_reserve_gb and had the tests hold it at zero, the way they already hold _estimate_gguf_kv_gb. That is precisely why the arithmetic can be corrected here without touching a single test expectation.

One thing this does not settle: embedding GGUFs still charge every slot, and above a 131072 vocab that is under-reserved at 1 and 2 slots, quantified in my earlier comment. It needs a remote pooling-type signal that does not exist yet, so it stays out of this PR.

@danielhanchen

Copy link
Copy Markdown
Member Author

Closing. The review above overturned the premise, and I checked each point against the code rather than taking it on faith.

The argument for this change was that the remote estimate should match the cached one, and that charging an output buffer for the first slot made every uncached chat load read 0.58 GB heavier than the same model once cached. That part is true. What I had not checked is what else the remote branch fails to charge, where the same first-slot buffer was quietly covering the difference in the opposite direction.

Measured on this branch, 5120-wide model, 32768 context and micro-batch:

cached context buffer    f16 3.00 GiB    q8_0 22.50 GiB
remote reserve, 1 slot                    7.60 GiB
one ceiling output buffer at that ubatch  36.80 GiB

The remote branch is never passed cache_type_kv, so it budgets the f16 KQ mask alone while the cached path charges 22.5 GiB of dequantization scratch for the identical launch. _estimate_gguf_kv_gb also charges (devices - 1) * _PIPELINE_PER_DEVICE_OVERHEAD_MIB for a layer split and the architecture-specific context term from _compute_buffer_ctx_bytes; the remote branch charges neither. None of the three can be computed without the header, so the ceiling output buffer is the only slack standing in for all of them.

That makes the first-slot charge unprincipled as an output buffer but load-bearing as slack, and the asymmetry decides it: over-reserving costs a 409 the user can see and retry, under-reserving costs a training run. The 0.58 GB over-charge at the default micro-batch is real, and small next to a 20 GB shortfall at large ones.

The comment justifying the 8192 activation ceiling was also wrong, as the review points out: with out_buffer = vocab * ubatch * 4 and scratch = 16 * n_embd * ubatch, equality sits at n_embd = vocab / 4 = 65536, not /16, so a 16384-wide ceiling would have been a quarter of one output buffer rather than a whole one.

What is worth keeping from this, for whoever picks it up: the remote estimator is missing the quantized-cache scratch, the per-device layer-split overhead and the architecture-specific context term, and it compensates with a slot count that does not mean what its name says. Charging those explicitly, from the ceilings the branch already uses, would let the slot count go back to matching the local branch. That is the real fix and it is bigger than this PR.

@danielhanchen
danielhanchen deleted the fix/remote-gguf-first-slot-output-buffer branch August 13, 2026 07:56
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