Repository navigation
Studio: raise the micro-batch for the projectors that abort llama-server on an image - #10683
Conversation
Vision encoders like Gemma 4 attend over all image tokens at once, so llama-server aborts with GGML_ASSERT(n_ubatch >= n_tokens_all) when the default 512-token micro-batch is smaller than a single image. Default to 2048 for both --batch-size and --ubatch-size when launching a vision model with an mmproj and the user left the batch sizes unset. Changes: - Add _batch_ubatch_for_mmproj helper that only overrides defaults when mmproj_path is set, is_vision is True, both n_batch and n_ubatch are None, and no env/extra_arg already sets them. - Apply the override in GgufBackend._load right after unpacking the intent. - Add unit tests covering the default, non-vision, explicit overrides, and extra_arg overrides. Closes unslothai#10559
|
Confirmed the default still comes from _DEFAULT_LLAMA_N_UBATCH = 512 in studio/backend/core/inference/llama_cpp.py, so a Gemma 4 image chunk aborts on the normal path. Raising both to 2048 also grows the compute buffer for every vision GGUF load, so could you confirm what this does to VRAM headroom and layer offload on a smaller card before I review? |
|
@codex security 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 |
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: 57c9d7f4fa
ℹ️ 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".
| _fit_mmproj_path = ( | ||
| None | ||
| if extra_args_disable_mmproj(extra_args) | ||
| else self._resolve_launch_mmproj_path( | ||
| model_path = model_path, | ||
| mmproj_path = mmproj_path, | ||
| ) |
There was a problem hiding this comment.
Decide the default after paravirtual projector suppression
On a virtualized Metal host whose capability probe confirms that --no-mmproj-offload is unavailable, this early resolution succeeds and raises both sizes to 2048, but the launch block subsequently sets launch_mmproj_path = None at llama_cpp.py:20306-20320 to avoid corrupt projector output. The resulting text-only CPU server still emits the enlarged batch flags and pays their compute-buffer cost. Fresh evidence beyond the previously fixed stale-path case is this later paravirtual suppression, so the decision needs to use the final launch path or be reverted when that path is cleared.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The divergence is real but I am leaving it, deliberately.
_pv_mmproj_unpinnable is gated on _paravirtual_cpu_forced, and paravirtual_normalized_request returns gpu_memory_mode='manual', gpu_layers=0 on that host. So the launch it describes is CPU-only: nothing is offloaded, no VRAM fit is distorted, and the residual is host RAM for one micro-batch on a virtualised Mac whose llama-server predates --no-mmproj-offload.
Against that, the two ways to close it both cost more than they buy. Using the final launch path means moving the decision past server_caps, which is probed inside Phase 3 after _ubatch_for_slots has already priced the fit, so the estimate would stop describing the child. Reverting the pair at the suppression site leaves the fit reserving for a projector that was dropped, which is the same inconsistency pointed the other way. Both also duplicate a predicate that already exists in three shapes in this file.
Everywhere except that host the two conditions agree, which is what e26adb4 and 57c9d7f were for.
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
|
Confirmed the default is still 512 in studio/backend/core/inference/llama_cpp.py, so a Gemma 4 image chunk aborts on the normal path, and thanks for narrowing the raise to the 1120 per-image ceiling. The emitted ubatch feeds _estimate_compute_buffer_bytes and so model_size_fit, so could you confirm a Gemma 4 12B load that fits today on a 12 GB card still fits at 1120 before I review? |
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
|
Thanks. This one is an upstream gap rather than ours: I checked the projector metadata of every vision GGUF in the unsloth Hub org, 127 repos: it raises for 6 and misses none, with E2B and E4B correctly left on 512 since they decode causally. Reading the flag off the launched command line for the ones I loaded:
The crash itself, same model, image and request, on the same GPU:
One thing to watch, not from this PR: the raise measured about 1.1 GB on an RTX 6000 Ada, while I ran CI at oobabooga#226: 52 pass, 4 skipped. Its three |
Fixes #10559
Loading
unsloth/gemma-4-12B-it-qat-GGUFand sending any reasonably sized image killed llama-server. On an RTX 6000 Ada with the bundled build 10840, a 1400x1400 image is 862 prompt tokens and the child dies with:mtmd cuts an image into chunks of
min(n_batch, its tokens)and asserts that the micro-batch holds the chunk while attention is non-causal. llama.cpp's defaults are-b 2048 -ub 512, so the 862-token chunk has nowhere to go. Upstream knows:tools/mtmd/mtmd-helper.cpp:99carries a TODO saying "n_ubatch must be enough to hold the image".The fix
When a load will open a projector whose images can exceed the stock micro-batch, and nobody has set one, Studio emits
--ubatch-sizeat that projector's own per-image ceiling. For Gemma 4 that is 1120, whichclip.cppenforces withset_limit_image_tokens(70, 1120). The batch is left alone: llama.cpp's own 2048 default already exceeds the ceiling, and the chunk is capped by it._launch_required_ubatchanswers that as one question, asked with the same arguments byload_modeland by the estimators that price the launch. It takes the maximum over every projector that can reach the child (the model's own, a pass-through--mmproj, an inheritedLLAMA_ARG_MMPROJ(_URL),--mmproj-autodiscovery). It deliberately does not model which projector llama.cpp would keep if several are named: for every projector this process cannot open the answer is the unknown-family headroom anyway, so the precedence would be a second copy of llama.cpp's argument handling to keep correct without changing an outcome that matters.Which models get it
Only the Gemma 4 image towers, which is where the ceiling sits:
mtmd_decode_use_non_causalis true for exactlygemma4v,gemma4uv,gemma3anddeepseek4v.gemma4vdecodes causally on E2B and E4B, identified by the text-siden_embd.clip.cppcapsgemma4vandgemma4uvat 1120 tokens per image,gemma3at 256 anddeepseek4vat 384. Only the first two can exceed 512.qwen3vl_mergerat 4096,youtuvlat 62500, and so on) decodes causally and never reaches the assert.A sweep over all 64 projector families llama.cpp knows, at four text embedding sizes, confirms 62 of them are untouched.
The family comes from
clip.vision.projector_type, which is the spelling a projector carrying both an image and an audio tower uses; Gemma 4 writesgemma4uvthere and no bareclip.projector_typeat all.Why the exact ceiling and not a round 2048
The micro-batch scales
_estimate_compute_buffer_bytes, which feedsmodel_size_fit, so the number chosen here is spent against the VRAM budget. Gemma 4 12B is priced at 10.27 GiB at 1120 and 13.45 GiB at 2048. Studio's Apple path budgets about 12 GiB on a 16 GB Mac and refuses an explicitly pinned context above what fits, so 2048 would have taken a model that loads today and stopped it loading, on the baseline Mac and on a 12 GB discrete card. 1120 is exactly sufficient for the assert and leaves both fitting.--image-max-tokensraises a family's ceiling in clip.cpp, so it raises this target with it.What is left alone
LLAMA_ARG_UBATCH,-ubor--ubatch-size. The user owns it.-b 256the chunk is 256 and the stock 512 holds it. Signed values get the same uint32 round-trip llama.cpp applies, so-b -1reads as 4294967295 and not as a batch below the ubatch.Verification
Reproduced and fixed on an RTX 6000 Ada with the bundled llama.cpp build 10840:
--ubatch-size 1120the same request returns 200.--ubatch-size 1120, with every routing flag unchanged.CUDA0 compute buffer sizefor Gemma 4 at four slots: 104.02 MiB at-ub 512, 420.07 MiB at-ub 2048.42 tests in
tests/studio/test_llama_cpp_mmproj_batch.py, plus 10211 backend tests with the same 16 pre-existing failures as the merge base.