Repository navigation
Studio GGUF: charge images in the fit and tool-result sizing counts (#14968) - #15014
danielhanchen merged 10 commits into
Conversation
…nslothai#14968) The counting copy used by fit_rolling_context and the tool-loop room probes dropped image parts, so a vision chat with many screenshots looked ~20k tokens smaller than it was and was never compacted before llama-server refused it. Images now become a text stand-in of what they will cost: the admission ceiling, or on Qwen-VL projectors a size-based merged-patch count capped by --image-max-tokens. Audio and video stay unpriced, and the request still sends the real images.
… charged (unslothai#14968) The fake backend has a 4096-token window and no projector, so each MCP screenshot was charged the ~4.2k admission ceiling and the loop reported no room before the repeat note. Give it a Qwen3-VL projector so the 6 px image costs what it would.
|
The After this change the import is no longer referenced in The codespell and shellcheck output in that job is informational and passes on |
…ion cache keyed off the payload, drop the unused import [skip ci]
[skip ci]
|
@codex review |
1 similar comment
|
@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. |
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ 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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ 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". |
…ng, honour --image-min-tokens, price the prompt offset with images [skip ci]
…ng when --image-min-tokens upscales
…kens is set [skip ci]
Fixes #14968
Before: on a local GGUF vision model with Auto-compact on, the token counts behind compaction and tool-result sizing dropped images entirely. A chat with ten screenshots (~20k image tokens) looked ~20k tokens smaller than it was, so nothing was evicted and no tool result was trimmed until llama-server refused the prompt with "Message too long: 66651 tokens exceeds the 65536-token context window".
After: each image is charged what it will actually cost the prompt. Older tool exchanges are evicted, and tool results are sized to the room that is really left, the same way text-only chats have behaved since #5562. The request itself still sends the real images; only the counting copy changes.
How:
context_window.py:messages_with_priced_imagesbuilds the counting copy with eachimage_url/input_imagepart replaced by a text stand-in of N single-token repeats, so/apply-template+/tokenizecount it as N tokens. Audio and video stay unpriced and are dropped as before.estimate_message_tokens_with_priced_imagesadds the image prices to the cheap estimate instead of serialising the stand-in.image_dimensionsreads an inline data-URL image's size with PIL (cached per URL, since the same image is priced on every count in a tool loop).llama_cpp.py:_image_token_pricecharges the same per-image admission ceiling the OpenAI admission gate uses (_mcp_image_tokens). On Qwen2/2.5/3-VL projectors, where an image's size gives a close count, it uses(w / s) * (h / s)merged patches (s = 28 or 32 px) plus mtmd's wrapper tokens, capped by--image-max-tokensand never above the ceiling. A text-only model is charged nothing, and an image whose size can't be read cheaply (remote URL, unknown projector) is charged the ceiling.fit_rolling_contextcounts and estimates, the tool-result room probes, the final-answer fit) now goes throughLlamaCppBackend._countable_messages/_estimate_countable_messageinstead ofmessages_without_unpriced_media/estimate_message_tokens_without_unpriced_media.Tests:
studio/backend/tests/test_image_token_pricing_14968.py(13 tests) covers the stand-in, audio/video staying unpriced, the no-media fast path, the estimate, reading dimensions, Qwen3-VL pricing from size, the--image-max-tokenscap, the ceiling bound, the ceiling fallback, text-only models, and the report's shape: ten 1920x1080 screenshots plus a long tool loop, which the old count kept whole (~46k text under the 49k budget) and which now evicts older tool rounds while keeping the screenshots.test_llama_cpp_tool_loop.py::test_repeated_note_on_an_mcp_image_keeps_the_base64_out_of_the_tool_textnow gives its fake backend a Qwen3-VL projector: with a 4096-token window and no projector, each screenshot is charged the ~4.2k admission ceiling (the same one admission charges), so the loop correctly reported no room before reaching the repeat note the test checks.Ran the 43 backend suites that touch these counts on Windows 11 (Python 3.13, no GPU needed). The new tests pass, and the remaining failures are environmental and also fail on unchanged main: missing optional packages (
transformers,ddgs, sqlite-vec), symlinks needing admin rights on Windows, a case-insensitive filesystem check, a console encoding error, and a/proc/self/fdcheck. The GGUF over-the-per-turn-cap tests (test_tool_call_cap_notice_extra.py::test_cap_notice_does_not_invite_a_spent_one_shot_retry[...-gguf]and thetest_gguf_*over_cap*tests intest_llama_cpp_tool_loop.py) stall on this machine with and without this change, at the same test. Not tested against a live llama-server with a vision GGUF.Follow-up changes
--image-min-tokensset, images are charged the ceiling, since mtmd resizes against it on its own rounded grid.estimate_message_tokens_without_unpriced_mediaimport that tripped the import hoist check.Verification against a live llama-server
Reporter's build (b11798-mix-f8a7950, CPU), Qwen3-VL-2B with its mmproj, 65536 context,
max_tokens32768 (prompt budget 49152). One user message with ten 1920x1080 screenshots plus 13 tool rounds, fitted with each tree's counting code, then sent with the real images:request (69099 tokens) exceeds the available context size (65536 tokens)Per-image price vs what llama-server charged (
prompt_tokenswith the image minus without):--image-max-tokens 2048The text stand-in tokenizes to exactly N tokens on both the Qwen3 and Gemma 3 tokenizers.