Skip to content

server: --vision-image-share fixed gives every image the same share of the visual-token budget (#2510) - #2717

Merged
ashhart merged 3 commits into
ashhart:mainfrom
devopsfortunato:task/Tensorfold-Contribut+t-mw9v3f8h-i34ly3
Nov 21, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
devopsfortunato:task/Tensorfold-Contribut+t-mw9v3f8h-i34ly3

Conversation

@devopsfortunato

@devopsfortunato devopsfortunato commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

A request's visual-token budget (--vision-image-tokens, 4,096 by default) is divided by the number of images in the request, so a turn that appends an image re-sizes every earlier image, gives it a new cache identity, and the prompt cache cannot resume from the first image (#2510).

This adds an opt-in, --vision-image-share fixed, where every image takes --vision-image-tokens / --vision-max-images tokens whatever the request's image count. The default is --vision-image-share request, main's per-request split, so with the flag unset the budget, the refusal text and the replies are unchanged. A fixed share that divides into nothing is refused naming the image and both flags. The flag needs --vision when it is not request.

What the opt-in trades, as documented in docs/vision.md: a lone image gets image_tokens / max_images (1,024 under the defaults) instead of the whole budget, and in return an earlier image keeps its rows from turn to turn, so the cache resumes from the first image.

This is the shape asked for in the review of the previous head: default stays main's split, fixed share opt-in, pinning test for one and four images, docs and changelog. The previous head made the fixed share the default; it is replaced.

Checks on this head

  • Head commit: ccac04ed2
  • Lean checker (tools/pr_check.py --base upstream/main --pr 2717): 168 on base, 168 on head, 0 new findings, 0 errors, 0 warnings. zig fmt --check and git diff --check clean.
  • Tests run (Zig 0.17.0, Linux aarch64, 4 cores, no GPU):
    • zig build test-vision-cli-host — 25/25 pass.
    • zig build test-media-request-ownership-host — 890 pass, 2 skipped, 0 fail (reaches image_share_test.zig and image_reuse_test.zig).
    • zig build test-server-cpu — 1671 pass, 15 skipped, 0 fail.
  • Tests shown to fail without the change: with the fixed arm ignoring the opt-in, four tests fail (fixed shares of 2 and 1,024 rows read 8 and 4,096); with the default arm ignoring the request count, three fail (expected 40, found 16, expected 8, found 2, the 3-token request split). Changing the default's refusal text fails the exact-string assertion.
  • CI: see the checks on this head.
  • Not run, and why: zig build test-golden and the full zig build test (host has 8 GB; CI runs them), and no device gate: no kernel, precision or cache format changes, and the host has no GPU or checkpoint.

Receipt

This reaches prompt preparation (how many visual tokens an image may take), not inference arithmetic.

  • Main against branch: with the flag unset the request split is main's expression verbatim (image_tokens / n_images) and main's refusal string. image_share_test.zig pins it for one image (cap equal to the whole budget, ids and cache words) and for four images (a quarter each).
  • Boxes it reaches: images.expand on requests with image parts, for every family that opens vision. Each family's api.Vision receives the share from its VisionOpen; vision_inputs.zig has its own pricing but nothing references it.
  • New switch? --vision-image-share request|fixed, default request, other values refused by name by the existing flag parser, needs --vision when not request; no profile row (it changes tokens when fixed, so it is not a --profile switch).
  • Environment: base d796b267e, Zig 0.17.0, Linux aarch64, 4 cores, 8 GB RAM, no GPU, no checkpoint.
  • Platforms: host tests only. Metal, CUDA, HIP and XPU device paths not run; their diffs are one-line field additions.
  • Exactness / speed / prompt speed: n/a for the default path (tokens unchanged, pinned by the host test above). The opt-in changes tokens by design.
  • Check the opt-in is held to, fixed before any run: on a two-turn conversation where turn 2 appends a second image, with --vision-image-share fixed turn 2 must resume from the first image's rows (cached tokens cover the first image's prefix), where the default re-prepares the first image (cached tokens stop before it). Its answers are then compared with the default split's on the same image questions, and the opt-in passes only if no question that the default answers correctly is answered wrongly. This needs a vision checkpoint, which this host does not have; I have not run it. I can post the result with the default path's token hashes beside it once a run is possible, and the opt-in stays documented as unqualified until then.
  • Licence of any new weights or drafter: n/a.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency (the video case skips where libtfvideo loads).
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

@ashhart ashhart 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 for this. Pricing images in prompt order fixes the resume problem in #2510: an earlier image keeps its rows when a turn adds one. One thing blocks it as written. With the default 4,096-token budget, one full-size image can take the whole budget, so a request with two large images is now refused at the second one. Before this change, eight images shared the budget at about 512 tokens each. That breaks multi-image requests that work today.
Could the share stay stable without being first-come? For example, size each image at image_tokens / --vision-max-images (fixed for the server's life, so no turn re-sizes an earlier one), or keep the even split within a turn but price images from earlier messages as they were first prepared. Please keep your new refusal test, and add one for two full-size images in one request at the default budget that must pass.

@joshuaswarren

Copy link
Copy Markdown
Contributor

GREEN at ff8e7ac — opencode-go/glm-5.3-flash as routed by Main (model id unverifiable); implementer model unverified. Red-first reproduced on my host: base tree + this head's tests = 78 pass / 2 fail / 80, the failures being 'expected 20, found 52' and 'expected error.Refused' — the old even split both shrinks the earlier image and refuses nothing, matching your receipt verbatim. At the head the media gate's image/video and core binaries pass (the step's api-module binary carries my container's 12 pre-existing originals_test env failures, identical at base, so the step as a whole is red here for reasons predating this PR). Mutation: replacing the running-left decrement with a per-image reset fails the past-budget refusal test; full removal is compile-rejected. Diff read: caps in prompt order, low-detail still capped, refusal names the image and flag before its fetch, single-image and all-fit identical to main; the appended image no longer re-sizes earlier ones so the cache identity holds. No GPU/image checkpoint here — the end-to-end resume rests on the host tests carrying the row identities, which they do.

@ashhart

ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Thanks @devopsfortunato. The head hasn't changed since our review, so the blocking point still stands. With the default 4,096-token budget, the first full-size image takes the whole budget, so a request with two large images is refused at the second ("the images before it use the request's visual-token budget"), or the second gets almost no tokens. Today both get about 2,048 each.

Could the per-image share be stable without being first-come? For example, size each image at image_tokens / --vision-max-images, or keep the even split within a turn and price images from earlier messages as they were first prepared. Please keep your refusal test, and add one with two full-size images in one request at the default budget that must be admitted. The CHANGELOG line will need to follow whichever design you pick.

I opened no other engine's source.

  • Synthena (Ash's Agent)

devopsfortunato added a commit to devopsfortunato/TensorFold that referenced this pull request Nov 10, 2026
…shhart#2717)

Each image is priced at --vision-image-tokens / --vision-max-images, a share that depends on neither the request's image count nor its position, so a turn that adds an image never re-sizes the ones already in the prompt and the cache resumes from the first image. The request's images stay within --vision-image-tokens, which is the row capacity each engine reserves, and a share that divides to 0 is refused by name.
devopsfortunato added a commit to devopsfortunato/TensorFold that referenced this pull request Nov 10, 2026
…shhart#2717)

Each image is priced at --vision-image-tokens / --vision-max-images, a share that depends on neither the request's image count nor its position, so a turn that adds an image never re-sizes the ones already in the prompt and the cache resumes from the first image. The request's images stay within --vision-image-tokens, which is the row capacity each engine reserves, and a share that divides to 0 is refused by name.
@devopsfortunato
devopsfortunato force-pushed the task/Tensorfold-Contribut+t-mw9v3f8h-i34ly3 branch from 90bca17 to 74e6aa5 Compare November 10, 2026 17:04
@devopsfortunato

Copy link
Copy Markdown
Contributor Author

Rewritten as 74e6aa5e9 (rebased onto today's main), and the answer to your blocking point is your first option, because I checked your second against what the engines actually reserve.

Why not the turn split. Every engine reserves exactly --vision-image-tokens rows for a request: native/flashnext_images.zig sets capacity = image_tokens + video_tokens, native/qwen27_images.zig allocates image_tokens rows and returns ImageTooLarge past them, vision/qwen_cuda_media.zig refuses rows > image_tokens, and flashnext/cuda_lanes_load.zig reserves the same for its stream. So an even split within a turn that keeps earlier turns at their first price either overshoots that capacity (the request fails later, at the device, instead of at expand) or has to clamp — and clamping makes a second single-image message refused, which is worse than main. Under a fixed capacity those two things cannot both hold, so the share has to be independent of the request's image count and of the image's position, which is image_tokens / --vision-max-images.

What it does. Every image takes that fixed share — 1,024 under the defaults — so a turn that adds an image never re-sizes the ones already in the prompt, the prompt cache resumes from the first image, and the request's images stay within --vision-image-tokens (verified: 2 × 1,024 = 2,048 and 4 × 1,024 = 4,096). A share that divides to 0 is refused by name, naming both flags.

The cost, said plainly. A lone image now gets a quarter of the budget, not all of it. That is the price of a share that can never move, and it is in the description, docs/vision.md and the CHANGELOG sentence.

Your two tests. Two full-size images in one request at the default budget are admitted (1,024 each, turn.rows = 2,048), and the refusal for a share of 0 stays. The suite's control: pricing at image_tokens / n_images again fails four tests — the lone picture reads 4,096 instead of 1,024, two pictures read 2,048 each, the resuming picture reads 40 instead of 16, and the share-of-0 case is admitted.

Checks on 74e6aa5e9: zig fmt --check and git diff --check clean; zig build test-media-request-ownership-host 84/84; lean checker 0 new. The description's pricing paragraph, the overlap note and the head line are updated to match. Still not run here: a Metal or CUDA pass, since this host has no device.

@ashhart

ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Thanks @devopsfortunato, and thank you for checking the turn-split idea against what each engine actually reserves; that settles it. The share-of-0 refusal names both flags before anything is fetched, and the tests pin it.

One change before this lands, and it's on us, since the fixed share was our suggestion. Dividing by --vision-max-images shrinks a lone image on every vision family: 4,096 tokens become 1,024 at the defaults, and a dots.mocr page gets 3,600 of its 14,400. That's a quality loss on the most common request, a single image or one OCR page, and we don't want to ship that. Could the share divide the budget by the number of images in this request instead (image_tokens / n, still capped at each family's per-image maximum)? Then:

The tests would pin both ends: one image equal to main, and four images with equal shares whatever their order.

Then the smaller things:

  1. Docs: docs/recipes/dots-mocr-hip.md still says the budget is "taken in prompt order, each image taking what the earlier images left", which is the first commit's design. docs/vision.md's CUDA Qwen sentence should describe the per-request share.
  2. Lean: the two new tests have two-line comments (images.zig:365 and :413), which the lean checker counts as 2 new problems against today's main. One line each clears it.
  3. The description's Receipt section still describes the earlier design. Please update it for the per-request share.
  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

@devopsfortunato I owe you a correction, and a decision instead of another round of suggestions.

My 10 Nov ask (divide the budget by the number of images in the request) is what main already does (zig/src/server/images.zig:169), so it would not fix #2510. Your own head's tests say as much. Please don't build it, and sorry for the back and forth.

Looking at all three designs together, we've been asking for three things that can't all hold while every engine reserves exactly --vision-image-tokens rows per request:

  1. a lone image keeps the whole budget (its replies match main's);
  2. two full-size images in one request are both admitted;
  3. adding an image in a later turn never re-sizes the earlier ones (the prompt cache resumes).

Any two of these rule out the third. Prompt order gives up 2, the fixed share gives up 1, and main gives up 3.

The decision: the default stays main's per-request split, so single-image and OCR replies don't change. Your current head's fixed share (image_tokens / --vision-max-images) becomes an opt-in for multi-turn image chats that want cache reuse, for example --vision-image-share fixed (the default is request; pick a better name if you have one). That means:

  • with the flag unset, replies are byte-identical to main, and one test pins that for one image and for four;
  • with it set, your existing tests apply as they are (shares stable across turns, the share-of-0 refusal naming both flags first);
  • docs/vision.md and the CHANGELOG line say what the opt-in trades: each image gets image_tokens / max_images tokens, and in return earlier images keep their size and the cache resumes from the first image.

The three smaller points from my last comment (the dots.mocr recipe text, the two-line test comments, the Receipt section) still apply, adjusted to this.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @devopsfortunato for the rework and the clear tests.

The current head still makes the fixed share the default: every image takes --vision-image-tokens / --vision-max-images. With that, a lone image gets a quarter of the budget, and single-image and OCR replies change. That is a token change on the default path, so it can't land this way.

As we wrote on 12 Nov, the shape we can take is:

  1. The default stays main's per-request split, for example --vision-image-share request. With the flag unset, replies must be byte-identical to main. Please add a test that pins this for one image and for four.
  2. The fixed share becomes an opt-in, for example --vision-image-share fixed. Your existing tests apply to it as they are: stable shares across turns, and the share-of-0 refusal naming both flags.
  3. docs/vision.md and the CHANGELOG line say what the opt-in trades: each image gets image_tokens / max_images tokens, and in return earlier images keep their size and the cache resumes from the first image.

Since the opt-in changes tokens, please also state in the description the check it is held to, fixed before you run it: for example, the image questions and the pass mark against the default split. Post its result with the default path's token hashes beside it.

  • Synthena

@devopsfortunato
devopsfortunato force-pushed the task/Tensorfold-Contribut+t-mw9v3f8h-i34ly3 branch from 74e6aa5 to 6a856b6 Compare November 20, 2026 00:24
@devopsfortunato devopsfortunato changed the title server: images take the visual-token budget in prompt order (#2510) server: --vision-image-share fixed gives every image the same share of the visual-token budget (#2510) Nov 20, 2026
@devopsfortunato
devopsfortunato force-pushed the task/Tensorfold-Contribut+t-mw9v3f8h-i34ly3 branch from 6a856b6 to ccac04e Compare November 20, 2026 00:26
@devopsfortunato

Copy link
Copy Markdown
Contributor Author

Reworked as asked: the default stays main's per-request split, byte-identical with the flag unset (pinned for one and four images in image_share_test.zig); the fixed share is now the opt-in --vision-image-share fixed, with its tests, docs/vision.md and the changelog line stating the trade. The old head's fixed-share default is gone. The description lists the check the opt-in should be held to; I could not run it here (no vision checkpoint), so it is not claimed as passed. Head ccac04ed2 is rebased on current main (the CHANGELOG conflict was the only one).

@ashhart

ashhart commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thanks @devopsfortunato, and thanks for the careful rework.

The asks from our last comment are resolved: the default is main's per-request split, byte-identical with the flag unset and pinned for one and four images in image_share_test.zig; the fixed share is the opt-in --vision-image-share fixed, with stable shares across turns and a share-of-0 refusal that names both flags; and docs/vision.md and the CHANGELOG line say what the opt-in trades. Your rebased head keeps both sides of the CHANGELOG entry, and the rest of the diff applies to current main.

One item remains. The check you wrote in the description has not been run: you were unable to run it on your host without a vision checkpoint, and we were unable to run it on this head before this review. The opt-in stays documented as unqualified until then. It needs a vision checkpoint on a GB10 (CUDA sm_121) with Qwen3.8-27B: on a two-turn chat where turn 2 adds a second image, turn 2 must resume from the first image's rows under fixed while the default re-prepares it; on the same image questions, no question the default answers correctly may be answered wrongly; and the default path's token hashes must match main's. We will time turn 2's first token in both arms and hold the opt-in to that result before it is described as qualified.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I ran a preliminary Metal check on an M5 Max with Qwen3.8-27B-MLX-4bit at 22d8d538. I built main 9b9218f72 and its trial merge with ccac04ed2 using zig build native -Dcpu=apple_m1. The trial merge needed only the changelog resolved. Its test-vision-cli-host and test-media-request-ownership-host targets passed 923 tests with two skips.

For each arm I started a fresh server with --vision --vision-image-tokens 4096 --vision-max-images 4 --context 8192 --parallel 1 --prompt-cache-gib 2 --no-thinking --no-drafts. I ran two conversations, red→blue and green→yellow. A turn contained one generated 1536×1536 solid PNG and asked for that image's color. Turn two retained turn one's messages, including the first image, and appended the second image. Each request used temperature 0 and a 12-token limit.

Arm Turn-two cached tokens Turn-two TTFT observations
main 0, 0 8.576 s, 9.477 s
PR, share flag omitted 0, 0 8.315 s, 9.105 s
PR, --vision-image-share fixed 1,071, 1,062 1.965 s, 2.034 s

All arms answered all four color questions correctly. The four logged token hashes matched across all three arms (d527abcfa8c5, c76ac8a572fa, 5bb6d63d6b3c, 81d8ba7d36cf). The fixed arm used 1,069 and 2,121 prompt tokens on turns one and two. The other arms used 2,349 and 4,123. These are two observations per arm on Metal. The requested GB10 qualification, broader image questions and CUDA timing remain open.

Igor Lessio - AIFLOW LABS

@ashhart
ashhart force-pushed the task/Tensorfold-Contribut+t-mw9v3f8h-i34ly3 branch from ccac04e to ed93845 Compare November 21, 2026 12:00
ashhart and others added 3 commits November 21, 2026 15:39
…f the visual-token budget (ashhart#2510)

The default stays the per-request split, so replies without the flag are unchanged. With fixed, each image takes --vision-image-tokens / --vision-max-images whatever the request's image count, so a turn that adds an image leaves earlier images' rows where they were and the prompt cache resumes from the first image. A share of 0 is refused naming both flags.
@ashhart
ashhart force-pushed the task/Tensorfold-Contribut+t-mw9v3f8h-i34ly3 branch from ed93845 to cb6ab0b Compare November 21, 2026 15:40
@ashhart
ashhart merged commit 72ebe5f into ashhart:main Nov 21, 2026
1 check failed
@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thank you @devopsfortunato. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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

Labels

None yet

Projects

None yet

4 participants