Skip to content

llama.cpp: warm the Qwen vision encoder at the image token cap (from #75) - #111

Open
Adon1s wants to merge 1 commit into
cobanov:mainfrom
Adon1s:pr/vision-warmup
Open

Adon1s wants to merge 1 commit into
cobanov:mainfrom
Adon1s:pr/vision-warmup

Conversation

@Adon1s

@Adon1s Adon1s commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What happens

With --image-max-tokens N, llama.cpp warms a Qwen vision encoder with the largest square under N: 35×35 = 1,225
tokens for 1,280 (clip-model.h, set_warmup_n_tokens). Photos get more than that. smart_resize rounds each side
to 32 px and scales down only when the result is over N, so every count up to N is reached: a 1280×1261 photo gets
36×35 = 1,260 tokens, a 1280×1024 one 40×32 = 1,280.

Each photo with more tokens than any before rebuilds the encoder's GPU buffers. The new buffer is cut from the
largest free block and the old one is left as a hole. That is the GPU part of the loss in #75.

Why exactly N, in any shape

The Qwen graphs (Qwen2-VL, 2.5-VL with its window attention, 3-VL, and Ling3-VL, which shares their setup) size
every tensor by the patch count, width × height. None uses either side alone: the learned position table is
interpolated to the grid in one op. So the most the buffers ever need is N tokens, whatever the shape. ggml grows a
buffer for any increase, so the warm-up has to be exactly N, not close to it.

llama.cpp's master still warms with the square (the code moved to mtmd-model.h on 2026-11-08).

Change

  • patches/0021: for those projectors, when --image-max-tokens N is below their warm-up size (46×46), the
    warm-up image is a×b = N tokens, b the largest divisor of N not over its square root. 1280 gives 40×32
    (1280×1024 px); a prime N gives 1×N.
  • Other projectors keep their square (for some of them the shape matters). Without --image-max-tokens nothing
    changes.
  • The encoder logs a warning when an image grows a compute buffer after the warm-up
    (compute buffer grew from A to B MiB for a W x H image). ggml's own line for that is a debug line, compiled out
    of release builds.
  • tests/server_vision_warmup_test.py (run by scripts/test-fleet.sh --rpc): untrained Qwen3-VL text and projector
    fixtures on a CPU llama-server with --image-max-tokens 1280, and images of 1,225, 1,240, 1,260 and 1,280 tokens.
    It checks each image's token count, the 1280×1024 warm-up, and that no buffer grows. With the warm-up change
    switched off it fails: the buffer grows three times.

Tested

PC, CI container, CPU llama-server, Unsloth's Qwen3.8-27B mmproj-F16.gguf (the test with --projector):

Warm-up Compute buffer after the warm-up Images that grew it
Square (before) 1120×1120 143.6 MiB 3 (to 145.4, 147.7, 150.1)
At the cap 1280×1024 150.1 MiB none

So the warm-up reserves 6.5 MiB more, once, and that is memory the photos took anyway.

PS5 (stock PS5LM title, Qwen3.8-27B UD-IQ2_S with mmproj-F16.gguf, -c 2048 -b 1280 -ub 256 --image-max-tokens 1280, screen on, the same 25 listing photos as in #75 at full size, memory logged after each
photo while the server was idle). MiB, largest free block:

Photo Prompt tokens main With this PR
ready 1,669 1,669
14 1,362 1,362
15 1,487 (most so far) 1,208 (−154) 1,331 (−31)
22 1,208 1,331
23 1,507 (most so far, 1280×1261) 1,055 (−153) 1,270 (−61)
25 1,055 1,270

0 errors in both runs, about 10–13 s a photo. The two drops of ~150 MiB, one whole encoder buffer each, are gone;
the run ends 215 MiB higher. What is left is smaller steps; at photo 23 the title heap's peak rose 52 MiB, which
#110 handles.

With --image-max-tokens N, llama.cpp warmed a Qwen-VL encoder with the
largest square under N (35x35 = 1,225 tokens for 1,280). smart_resize
lets photos reach N (a 1280x1261 photo gets 36x35 = 1,260), and each
photo bigger than any before rebuilt the encoder's compute buffers from
the largest free block, leaving the old ones as a hole.

patches/0021 warms Qwen2-VL, 2.5-VL, 3-VL and Ling3-VL projectors with
exactly N tokens (40x32 for 1,280): their buffers depend only on the
token count, never the shape. It also logs when an image grows a compute
buffer after the warm-up. Other projectors and runs without a cap are
unchanged.

tests/server_vision_warmup_test.py runs untrained Qwen3-VL fixtures on a
host llama-server with images of 1,225 to 1,280 tokens and fails when a
buffer grows; it fails with the warm-up change switched off.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T06:17:50.998898Z 175a1cc PR opened
ℹ️ 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.

@cobanov cobanov left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks @Adon1s, this is the llama.cpp-side fix I hoped for, and the reasoning holds up. The Qwen-VL graphs size by patch count only, so warming at exactly the cap, in any a×b shape, is the real worst case. The PS5 table (no more ~150 MiB steps at photos 15 and 23) and the CPU test that fails without the change make it convincing, and 0021 is a free number.

The catch is timing. #103 has just landed and moved llama.cpp to d2bceda3d, where clip.cpp and clip-model.h no longer exist, so 0021 no longer applies on main (the green CI here is from the old base). Could you:

  1. Port the patch to tools/mtmd/mtmd-model.h (set_warmup_n_tokens), mtmd-model.cpp (the QWEN2VL/25VL/3VL/LING3VL case) and mtmd-subcontext.cpp (get_dummy_batch for the w×h image).
  2. Vision and audio now share one sched, so keep the "compute buffer grew" baseline on the shared mtmd_backends sched. Take it after all the sub-contexts have warmed up, next to the compute-buffer log in mtmd.cpp.
  3. Update the warmup_buffers match in tests/server_vision_warmup_test.py, which looks for the line under reserve_compute_meta, and rerun it with --projector on the real mmproj. A quick photo run on the console after the port would be great too.

Optional: this looks upstreamable to ggml-org/llama.cpp, which would let us drop the patch later.

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.

2 participants