Skip to content

Studio: raise the micro-batch for the projectors that abort llama-server on an image - #10683

Merged
oobabooga merged 30 commits into
unslothai:mainfrom
chakshu-dhannawat:fix/llama-server-mmproj-ubatch-10559
Sep 14, 2026
Merged

oobabooga merged 30 commits into
unslothai:mainfrom
chakshu-dhannawat:fix/llama-server-mmproj-ubatch-10559

Conversation

@chakshu-dhannawat

@chakshu-dhannawat chakshu-dhannawat commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #10559

Loading unsloth/gemma-4-12B-it-qat-GGUF and 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:

GGML_ASSERT((cparams.causal_attn || cparams.n_ubatch >= n_tokens_all)
  && "non-causal attention requires n_ubatch >= n_tokens") failed   llama-context.cpp:1749

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:99 carries 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-size at that projector's own per-image ceiling. For Gemma 4 that is 1120, which clip.cpp enforces with set_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_ubatch answers that as one question, asked with the same arguments by load_model and 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 inherited LLAMA_ARG_MMPROJ(_URL), --mmproj-auto discovery). 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_causal is true for exactly gemma4v, gemma4uv, gemma3 and deepseek4v. gemma4v decodes causally on E2B and E4B, identified by the text-side n_embd.
  • Of those, clip.cpp caps gemma4v and gemma4uv at 1120 tokens per image, gemma3 at 256 and deepseek4v at 384. Only the first two can exceed 512.
  • Everything else (qwen3vl_merger at 4096, youtuvl at 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 writes gemma4uv there and no bare clip.projector_type at all.

Why the exact ceiling and not a round 2048

The micro-batch scales _estimate_compute_buffer_bytes, which feeds model_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-tokens raises a family's ceiling in clip.cpp, so it raises this target with it.

What is left alone

  • A micro-batch set through the field, LLAMA_ARG_UBATCH, -ub or --ubatch-size. The user owns it.
  • A named batch caps the raise rather than cancelling it, since it also caps the chunk mtmd cuts: at -b 256 the chunk is 256 and the stock 512 holds it. Signed values get the same uint32 round-trip llama.cpp applies, so -b -1 reads as 4294967295 and not as a batch below the ubatch.
  • Text-only models, audio-only encoders, and vision loads whose projector is suppressed.
  • A projector that cannot be classified, which gets headroom instead of a ceiling.

Verification

Reproduced and fixed on an RTX 6000 Ada with the bundled llama.cpp build 10840:

  • Gemma 4 12B qat at llama.cpp's stock defaults aborts on a 1400x1400 image with the assert above; at the emitted --ubatch-size 1120 the same request returns 200.
  • Running this branch and current main side by side and diffing the launched command line: byte-identical for a text-only GGUF, for Qwen3.5-4B (causal vision) and for Gemma 3 4B (non-causal but 256 tokens). Gemma 4 differs by exactly one flag pair, --ubatch-size 1120, with every routing flag unchanged.
  • llama-server's own CUDA0 compute buffer size for 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.

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
@danielhanchen

Copy link
Copy Markdown
Member

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?

@oobabooga

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-11T15:53:43.583745Z 3f8aa9e New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 10, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 10, 2026
@oobabooga

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@oobabooga

Copy link
Copy Markdown
Member

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 10, 2026

@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: 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".

Comment on lines +19959 to +19965
_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,
)

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 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 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 10, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 11, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 3c116114b8

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 12, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 12, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: f127688700

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member

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?

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: bf58358958

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member

Thanks. This one is an upstream gap rather than ours: mtmd-helper.cpp:99 still carries the TODO that n_ubatch has to hold the image, and the reports of the resulting assert (ggml-org/llama.cpp#21427, ggml-org/llama.cpp#21461, ggml-org/llama.cpp#21550, ggml-org/llama.cpp#21816, ggml-org/llama.cpp#23409) were closed as stale telling people to pass -ub themselves. Studio builds the command line, so there was nowhere for a user to do that.

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:

Model Projector Text n_embd Emitted Image result
gemma-4-12B-it-qat gemma4uv 3840 --ubatch-size 1120 200, 1111 prompt tokens
gemma-4-31B-it / -qat gemma4v 5376 --ubatch-size 1120 not loaded
gemma-4-26B-A4B-it / -qat gemma4v 2816 --ubatch-size 1120 not loaded
gemma-4-E2B-it gemma4v 1536 none 200 at the stock 512
gemma-4-E4B-it gemma4v 2560 none not exercised
gemma-3-4b-it gemma3 2560 none 200, 268 prompt tokens
Qwen3.5-4B qwen3vl_merger 2560 none not exercised
Qwen3.6-35B-A3B qwen3vl_merger 2048 none estimate unchanged
Qwen3.8-27B qwen3vl_merger 5120 none estimate unchanged

The crash itself, same model, image and request, on the same GPU:

before after
Launched flags none --ubatch-size 1120
Child GPU residency 13692 MiB 14774 MiB
3000x3000 image HTTP 500 HTTP 200, 1111 prompt tokens
llama-server exited (code -6), respawned alive, no assert

One thing to watch, not from this PR: the raise measured about 1.1 GB on an RTX 6000 Ada, while _estimate_compute_buffer_bytes charges 2.77 GB for it, so the memory panel will over-report Gemma 4 vision loads until that formula is looked at separately. --image-min-tokens can also push the chunk over the ceiling and is not handled here.

I ran CI at oobabooga#226: 52 pass, 4 skipped. Its three Core zoo failures are one unsloth-zoo test at 055c0497, tests/test_vllm_flashinfer_block_reversible.py. That test fails the same way on unrelated branches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Unsloth Bug] llama-server crashes with GGML_ASSERT on image input for Gemma 4 (default ubatch too small)

3 participants