Repository navigation
fix(studio): restore parallel vision chat slots - #9842
Conversation
|
Thanks for digging into this one. I reproduced the bug, confirmed your diagnosis is correct, and measured the constant against real projectors. Summary first: the direction is right and the core of the fix works, but the flat 4096 is 2 tokens under the real Qwen ceiling, and the passthrough route forwards an image it is not billed for. Details and evidence below. The diagnosis holdsI ran the estimator directly on
On I also confirmed the layer. A bare llama-server on the build we ship (b10639),
llama.cpp overlaps vision requests fine. The serialisation was ours, and this is the right place to fix it. For the record, this is a regression I introduced in #9392. The docstring there claimed Studio "sends every attachment as 1. The 4096 constant is 2 tokens under the real ceilingI measured actual mtmd cost as
Two things worth keeping:
Gemma 3 is a flat 258 at every resolution, so 4096 over-reserves it ~16x. At a 40k context that is harmless (still 4 concurrent). At an 8k context it is not: this PR still admits only 1 Gemma vision chat, where projector-aware accounting would admit 4. Worth a follow-up rather than a blocker. I only tested these two projector families. Tiled or multi-crop projectors are not covered by either of us, and Studio never passes 2. Blocking: passthrough forwards an image it is not billed forThe new docstring says the generation path injects the legacy image only when there is no message-level image. That is true of Measured on this branch with real Before this PR that route was over-charged (whole cache) so it was accidentally safe. After, it under-charges by a full image. I would fix it by giving both builders one shared normalisation helper rather than patching the estimator, since a second copy of "which images does this request really carry" is exactly how #9392's docstring drifted from reality. 3. The same bug survives on other shapes that reach the same estimatorThe strip predicate is
Neither is a defect you introduced, and Studio's own frontend emits neither, so I am happy for them to be follow-ups. But the PR title says "restore parallel vision chat slots" and one of the two vision routes is untouched. 4. Audio and videoYou call this out as deliberate, and I agree it is out of scope, but for the record it is not a small residue: a 200 KiB 5. Smaller things, all in the lines this PR rewrote
6. Test notesBoth media equality assertions are currently vacuous, because
The fixtures are also What passes
One thing this PR cannot fixThe reporter also notes that setting AskBlocking: fix the passthrough double-forward (item 2). Nice to have in this PR: raise the constant above 4098 with a comment explaining where the number comes from (item 1). Happy to take the Anthropic route and audio/video as follow-ups. Everything else looks good, and thank you for tracking this down. |
… builders Two things kept the image allowance from being the bound admission needs. The passthrough builder spliced the legacy top-level image unconditionally, while the GGUF chat builder skipped it when the messages already carried one. Studio echoes the current image into both spellings, so a request with client tools or a response_format went to llama-server with two copies of one image against a reservation for one. Measured on llama-server b10639 with a 2048x2048 image: 4417 tokens reserved, 8466 actually charged. Across capacity x context shapes that is a real overcommit in 12 of 30 combinations, including 4 slots at 32768 and 8 slots at 40000, which is the collision unslothai#9392 exists to stop. Both builders now ask one shared predicate, since two copies of "does this request already carry an image" is how they drifted apart. The 4096 constant was also below the real ceiling. 4096 is llama.cpp's default cap on Qwen-VL embeddings per image, but mtmd wraps them in delimiters, so a max-resolution image measures 4098 on Qwen3-VL-4B and 258 on Gemma 3 4B. The constant is now the embedding cap plus an explicit wrapper allowance, with the measurements and the projector families it cannot promise anything for written down next to it. Also drop the unset optional fields before the dense estimate, as every other dump of these models in this file already does. Without exclude_none a bare {"role": "user", "content": "hi"} is priced at 34 tokens rather than 8, all of it null padding that is never sent. The two media equality assertions ran at a budget both sides clamped to, so they held on the unfixed estimator too. They now run clear of the clamp, and the new cases assert what the fix is for: every builder forwards exactly the number of images admission charged for, and the allowance bounds the measured cost of a real projector.
|
I pushed a commit to this branch rather than leaving you a list: What changedBoth builders now ask one shared predicate. The constant is the embedding cap plus an explicit wrapper allowance (
Was it a real bug, and does the fix actually holdReal, and the mechanism is deterministic rather than marginal. On The passthrough gap was not theoretical either. Driving the real builders into a live llama-server with a 2048x2048 image:
Sweeping that shortfall over slot counts and context sizes, it is a genuine overcommit in 12 of 30 capacity-by-budget combinations, including 4 slots at 32768 and 8 slots at 40000. That is the collision #9392 exists to stop, so it was worth fixing before merge rather than after. Simulation coverageA 3000-cell matrix over Before the commit: 1 failure, the allowance being under the measured Qwen ceiling. After: all 3000 cells hold. CompatibilityPython. The backend floor is 3.10 and OS and browsers. Not applicable, and I checked rather than assumed: the diff is two backend Python files with no GPU and CPU. The thing that could have been hardware-dependent is the mtmd token count the constant has to bound. It is not: a CPU-only llama.cpp build of the same revision produces identical counts to the CUDA build at every resolution. The axis that does vary with hardware is the estimator's inputs, since VRAM downshifts Old installs. Every client shape still admits and stays bounded: a pre-content-parts Studio sending only Regressions. Full One thing the de-vacuuming exposedOnce Still open, deliberately not in this commitThe Anthropic What I did not testReal Windows, macOS or WSL hosts, and AMD hardware. I am comfortable asserting the code is indifferent to all of them given there is no platform-specific construct in it and the interpreter matrix is identical, but that is an argument from the diff, not a measurement. Projector families beyond Qwen-VL and Gemma 3 are also untested, and tiling or multi-crop ones could exceed the cap; the comment on the constant says so. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 68bb28bd87
ℹ️ 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".
| if not payload.image_base64 or _messages_carry_an_image(messages): | ||
| return messages |
There was a problem hiding this comment.
Preserve distinct legacy images in passthrough requests
When a tools or response_format request contains any message-level image plus a different image_base64 attachment—for example, an older image in history and a new legacy-only image on the current turn—this new early return silently drops the top-level image, so llama-server answers without the image the client just supplied. The existing _legacy_image_is_distinct helper already distinguishes Studio's byte-identical echo from a genuinely separate attachment; the passthrough builder should use that distinction rather than treating the presence of any message image as proof that the legacy field is redundant.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Correct, and thanks for catching it. Skipping the legacy field whenever the messages carried any image deduplicated Studio's echo but also dropped a genuinely different attachment, so llama-server answered without the image the client just sent. Fixed in b4160be: both builders and the admission estimate now key on _legacy_image_is_distinct, so the echo is charged and sent once while a real second image is charged and sent. _latest_user_image_payload now reads plain dicts as well as models, since the estimate sees whatever the caller passed and the two predicates have to agree or the reservation stops matching the prompt. Regression added: every builder must forward exactly the number of images admission charged for, with a history image plus a distinct legacy attachment among the shapes.
| _OPENAI_LLAMA_ADMISSION_IMAGE_EMBEDDING_CAP = 4096 | ||
| _OPENAI_LLAMA_ADMISSION_IMAGE_WRAPPER_TOKENS = 128 | ||
| _OPENAI_LLAMA_ADMISSION_IMAGE_TOKENS = ( | ||
| _OPENAI_LLAMA_ADMISSION_IMAGE_EMBEDDING_CAP + _OPENAI_LLAMA_ADMISSION_IMAGE_WRAPPER_TOKENS | ||
| ) |
There was a problem hiding this comment.
Derive the reservation from the configured image token cap
When a load uses llama_extra_args=["--image-max-tokens", "8192"], an image may consume more than the hard-coded 4,224-token reservation, allowing the admission queue to grant concurrent requests whose real embeddings do not fit in the shared KV cache. Studio does permit this configuration: LoadRequest.llama_extra_args is forwarded verbatim (studio/backend/models/inference.py:335-341), and the pass-through validator documents that every non-managed flag is appended after Studio's flags (studio/backend/core/inference/llama_server_args.py:4-9); --image-max-tokens is not managed. The reservation therefore needs to reflect the effective launch argument or loaded projector rather than assuming Studio can never raise the cap.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Confirmed, and my comment claiming Studio never passes one was wrong. I checked validate_extra_args directly: it rejects --parallel but forwards --image-max-tokens 8192 verbatim, so the cap is reachable through llama_extra_args. I then measured it on llama-server b10639: with --image-max-tokens 8192 a 4096x4096 Qwen3-VL image costs 8102 KV positions against 4098 at the default, so the hard-coded allowance really would have admitted concurrent requests the cache cannot hold. Fixed in b4160be: _openai_llama_admission_image_tokens reads the effective cap off the running backend and falls back to the default ceiling, so a lowered cap reserves less too. Regression covers a raised, lowered, absent and malformed cap.
… cap Two holes in the per-image reservation, both found in review. Skipping the legacy top-level image whenever the messages carried any image was too coarse. It deduplicates Studio's echo, which is byte-equal to a part already in the thread, but it also silently dropped a genuinely different image attached to the current turn through the legacy field, so llama-server answered without the image the client just sent. Both builders and the admission estimate now key on _legacy_image_is_distinct, which compares against the one value the composer could have echoed. The echo is still charged and sent once; a real second image is now charged and sent, rather than dropped. _latest_user_image_payload reads models and plain dicts alike so that predicate can be shared: the generation paths only ever hold models, but the estimate sees whatever the caller passed, and the two have to agree or the reservation stops matching the prompt. The per-image allowance was also a hard-coded default in a place that can be configured. --image-max-tokens is not an Unsloth-managed flag, so llama_extra_args forwards it verbatim; measured on llama-server b10639 with --image-max-tokens 8192, a 4096x4096 Qwen3-VL image costs 8102 against 4098 at the default, so the default allowance would have admitted concurrent requests the cache cannot hold. The reservation now reads the effective cap off the running backend and falls back to the default ceiling, so lowering the cap reserves less too. Regressions cover both: every builder forwards exactly the number of images admission charged for, now including a history image plus a distinct legacy attachment, and the allowance tracks a raised, lowered, absent or malformed cap.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! 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". |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0f9a62fe49
ℹ️ 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".
| cap = _extra_args_image_max_tokens(getattr(llama_backend, "_extra_args", None)) | ||
| if cap is None: | ||
| return _OPENAI_LLAMA_ADMISSION_IMAGE_TOKENS |
There was a problem hiding this comment.
Read the effective cap from the loaded projector
When no explicit argv cap exists—or when a low cap is supplied for a fixed-resolution projector—this value is not an upper bound. I checked the bundled llama-server's common/arg.cpp:2634-2639: its help says the default is “read from model” and the option applies only to dynamic-resolution models; tools/mtmd/clip.cpp:1766,1802,1965 defines supported projector maxima of 62,500, 8,000, and 16,384 tokens. Large images for those projectors can therefore occupy far more than the 4,224 positions reserved here, allowing concurrent requests into a KV cache that cannot hold them. Read the loaded projector's effective limit rather than treating 4,096 or every _extra_args value as authoritative.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Right on both counts, and I checked the sources you named. clip.cpp's set_limit_image_tokens really does differ per family: 256 lfm2, 280 gemma4v, 576 minimax_m3, 1024 pixtral/kimivl/lightonocr, 4096 the qwen mergers and glm4v, 16384 hunyuanvl, 62500 youtuvl. And the flag's own help says default: read from model and only used by vision models with dynamic resolution, so a low cap against a fixed-resolution projector is not authoritative either. A flat 4096 was a bound for most families and a quarter of what one hunyuanvl image costs. Fixed in cc311a9. /props carries no image-token limit (I started a server and dumped it: only modalities.vision), so this reads the projector file instead. The mmproj is already opened at load for its capabilities, so it now also yields clip.projector_type, and admission looks the ceiling up from a table keyed on the clip-impl.h strings, verified against the two mmproj files I have here (qwen3vl_merger, gemma3). An unrecognised family keeps the previous default rather than inventing a number. It takes the LARGER of the projector ceiling and any --image-max-tokens rather than trusting the flag, which is what covers your fixed-resolution case. Regression asserts every family in the table is bounded, that youtuvl gets at least 62500, that a small-ceiling family reserves near it, and that a low cap cannot pull a qwen reservation under 4096.
There is no universal per-image ceiling to hard-code. llama-server documents --image-max-tokens as "read from model" and applies it only to dynamic-resolution projectors, and clip.cpp sets a different limit per family: 256 for lfm2, 280 for gemma4v, 576 for minimax_m3, 1024 for pixtral and kimivl, 4096 for the qwen and glm4v mergers, 16384 for hunyuanvl and 62500 for youtuvl. A flat 4096 bound holds for most of those and reserves a quarter of what one hunyuanvl image costs, which admits concurrent requests the cache cannot hold. The mmproj already gets read at load for its capabilities, so it now also yields clip.projector_type, and admission looks the ceiling up from a table keyed on the strings in clip-impl.h. An unrecognised family keeps the previous default rather than inventing a number. Takes the LARGER of the projector ceiling and any --image-max-tokens rather than trusting the flag. It is honoured only by dynamic-resolution projectors, so a low cap against a fixed-resolution one is ignored by llama.cpp and believing it would reserve less than the image costs. Over-reserving is the safe direction here. /props carries no image-token limit, so this reads the projector file rather than the running server.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cc311a958f
ℹ️ 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".
| # model" -- and the spread is three orders of magnitude, so assuming any one number | ||
| # under-reserves somebody. A family absent here falls back to the default ceiling | ||
| # below, which is the pre-existing behaviour rather than a new guess. | ||
| _MMPROJ_IMAGE_TOKEN_MAX = { |
There was a problem hiding this comment.
Include the GLM5Next projector ceiling
When the loaded projector reports clip.projector_type="glm5next", this table misses it and the lookup falls back to 4,096 tokens. Fresh evidence in the current revision is the bundled llama-server source at /workspace/unsloth_studio/llama.cpp/tools/mtmd/clip.cpp:1788-1802, where GLM-5.3-Flash calls set_limit_image_tokens(16, 8000). A maximum-resolution image can therefore consume about 8,000 embeddings while admission reserves only 4,224 positions, allowing concurrent GLM-5.3 vision requests into a KV cache that cannot hold their actual commitments.
Useful? React with 👍 / 👎.
| known = _MMPROJ_IMAGE_TOKEN_MAX.get(str(projector).strip().lower()) if projector else None | ||
| cap = max( | ||
| known or _OPENAI_LLAMA_ADMISSION_IMAGE_EMBEDDING_CAP, | ||
| _extra_args_image_max_tokens(getattr(llama_backend, "_extra_args", None)) or 0, |
There was a problem hiding this comment.
Honor the environment-provided image token cap
When the server is launched with LLAMA_ARG_IMAGE_MAX_TOKENS and no equivalent argv flag, this reads only _extra_args, so admission continues using the projector/default ceiling. Fresh evidence in the current revision is the bundled llama-server registration at /workspace/unsloth_studio/llama.cpp/common/arg.cpp:2634-2639, which exposes --image-max-tokens through .set_env("LLAMA_ARG_IMAGE_MAX_TOKENS"). For example, an environment cap of 8192 lets a dynamic-resolution image approach that size while Studio reserves only 4,224 positions, recreating the concurrent-request KV overcommit this change is intended to prevent.
Useful? React with 👍 / 👎.
pre-commit.ci has been failing on main since #9842 and #9791, which landed asserts written in the parenthesize-the-message style that a newer ruff produces. The kwarg-spacing hook pins ruff==0.6.9, which writes the message unparenthesized and the condition parenthesized instead, so it rewrote both files on every run and the job reported 'files were modified by this hook'. Formatting only: the AST of each file is unchanged and both still pass.
Summary
Root cause
image_urland the legacy top-levelimage_base64field.Fix
Testing
python -m compileall -q routes/inference.py tests/test_llama_admission_kv_budget_media_and_tools.py— passed.pytestorfastapi.Issue
Fixes #9840