Skip to content

vision: restore the prefix cache and per-chunk encode for every tower - #303

Merged
AzeezIsh merged 4 commits into
Atlas-Inf:mainfrom
Enntity:fix/vision-every-tower
Nov 1, 2026
Merged

AzeezIsh merged 4 commits into
Atlas-Inf:mainfrom
Enntity:fix/vision-every-tower

Conversation

@data-angel

Copy link
Copy Markdown
Contributor

Summary

On main, no vision prompt reaches the prefix cache on any tower: vision_prefix_cache_ok is is_some_and(|_| false), so a repeated image or video re-prefills in full. SparkQwen 0.4.0 and SparkGLM 0.2.0 both cache them. Turning the gate back on alone is not safe, because the prefill and MRoPE paths also need to handle a chunk that starts after a cached image. This PR ports those fixes and then restores the gate for every tower.

  • MRoPE: every walker positions a chunk from the whole prompt. The generic walker restarted at item 0 and the linear position, so a prefill after a cached image would have positioned the rest from its own first token. build_glm5 has no caller on main; its arm is back.
  • Per-chunk encode: a chunk splices only its own rows on a warm cache. Selection could start one item early while the splice skipped that item's rows, so every later image read the previous image's rows. A computed placeholder with no row is now an error, never a raw token embedding. Rows are checked against the window on every rank before any collective.
  • Gate: back on for every tower. ATLAS_VISION_PREFIX_CACHE=0 turns it off. qwen4_exp in-pass checkpoints register under the same content key, or skip a prompt the key does not cover.
  • Release: a finished vision sequence releases its prompt only. Releasing over the reply dropped references it never took, from a node another request's prompt had put on the same key.

The four commits port Enntity/atlas 4b30eb8, bd3eca1, ca929ec, 8746056, da94c7a, d85c88d and a946481. Each commit message says which.

Part of #220.

Test plan

  • cargo fmt --all -- --check
  • ATLAS_SKIP_BUILD=1 cargo clippy --locked --workspace --tests -- -Dwarnings, CI's env. Not --all-features, which stops on objc2 on Linux.
  • python3 scripts/check_spdx.py, which is what CI runs. scripts/check-license-headers.sh needs Docker and did not run in my container. All four new files carry the header.
  • typos: not run, not installed here.
  • spark-model with the filters vision mrope pc_policy finish_leaf: 158 passed, 3 ignored. The workspace suite was not run at this tip.
  • Added tests, including embed_chunk_sim_tests, which drives the real selection, window and plan over random prompts, caches and chunkings, and vision_cache_stream_tests.
  • Real model and hardware: two DGX Sparks, TP2, 2026-11-01, results below.

SparkQwen, this tip (cecf6539e) vs SparkQwen 0.4.0. Qwen3.8-Flash-Next, Spark and Strict modes, 8x32k profile. Four image and video prompts plus one image and video pair sent together, 14 responses at temperature 0 with reasoning_effort: low.

  • Content, reasoning, finish reason, prompt tokens and completion tokens are identical on 14 of 14 in both modes, and cached token counts are equal.
  • Text is unchanged against 0.4.0: greedy 8/8, prompt logprobs zero delta over 26,912 tokens, tools 7/7, agentic 20/20, in both modes. Greedy, logprobs and tools also match on a 4x1m profile.

Spark mode, preliminary, one run, three requests each:

Prompt Cached tokens, both engines 0.4.0 TTFT (ms) This branch TTFT (ms)
image-digits 32, 144, 144 233, 235, 237 240, 243, 238
video-colors 32, 1472, 1472 1032, 205, 196 1047, 207, 206

Cached repeats run a few ms slower than 0.4.0 here, and the same shows in Strict. I have not found why.

SparkGLM, not this tip. Main has no GLM tower path yet, so these commits were tested merged into our SparkGLM tree (Enntity/atlas a76faa010), against SparkGLM 0.2.0 in Spark mode.

  • Vision identical on 14 of 14 with equal cached counts. Video TTFT 790 ms on the first request, 171 and 173 ms on repeats.
  • The 109-case suite and greedy output are identical in both modes, and logprobs show zero delta over 20,705 tokens.
  • The GPU unit tests ran on this GLM tree, not on this branch: 24 passed. glm5_vision_native_encoder_oracle_dump and mxfp8_real_tensors_match_bf16_per_tp2_rank did not run, because they need a checkpoint env var we had not set.

These comparisons are against the reference products, not against main in the same run, so they do not isolate this PR's speed effect. Main was not measured with this probe, so "main re-prefills in full" comes from reading the gate, not from a before number.

Notes for reviewers

  • The cache is on by default. That is the point of the PR, and it applies to every tower. If you would rather land it default-off, flipping switch_on() in vision_cache.rs is a one-line change.
  • The gate was closed in 11361ab (vision: SparkQwen 0.4.0 vision/video path (content-keyed vision cache, per-seq state, whole-request admission, hour video, #239 fix; ported from Enntity) #286), which changed |v| v.is_glm5_next to |_| false. I do not know why. If it was covering a problem these commits do not address, please say.
  • Omni tower: now keyed like the others, and untested on hardware.
  • ATLAS_VISION_CHECK_ROWS=1 is a new slow diagnostic that re-encodes each item of the window alone and compares its rows.
  • Departures from 0.4.0 (9b25f57596d1): out_row() and out_hidden_size() accessors, and the Qwen3-VL arm. The tower dequantizing VitWeight at run time, output_rows() as p_max and VisionCapacityError are main's and stay.
  • Left out: the ep_min_u32 gather fast path, and the verify-split and native MTP trait additions. The GLM multi-rank cap in vision_row_bounds belongs with the GLM stack.
  • spark-model: restore GLM reference video preprocessing (Enntity f3f3876b) #256: merging its head adds no textual conflicts beyond those it already has against main. It targets stack/dflash-warm-ctx.

Authorship

AI-authored (Claude Code). No human-written code.

🤖 Generated with Claude Code

data-angel and others added 4 commits October 31, 2026 21:09
The generic walker restarted each chunk at the linear position and at item
0, so a prefill starting after a cached image would have positioned the rest
from its own first token. Walk the prompt from position 0 and slice the
processed range, as the Qwen3-VL arm does.

GLM-5 had lost its walker: `build_glm5` has no caller on main, so a GLM video
took the generic walk, which does not know its per-frame markers and
timestamp text. Its arm is back. A run a walker pushes past the range is
sliced off; a walk shorter than the range is request-local.

Ported from Enntity/atlas 4b30eb8 and bd3eca1 (AGPL-3.0-only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- `items_in_range` selects an item only for a placeholder in
  [max(from, computed_from), to). It selected an item the cache covers wholly
  but whose pads end inside a later chunk: the window started one item early
  while the splice skipped that item's rows, and every later image read the
  previous image's rows. A chunk wholly in the cache encodes nothing.
- `vision_splice_plan` places only the placeholders the prefill computes. A
  cached head of a straddling item still advances the row index, and a
  computed placeholder with no row is an error, never a raw token embedding.
- A chunk's rows are checked against the window on every rank before the
  drain, the encode and any collective. A single rank used to overrun
  `buf_out`.
- The head is told by rank. A head without pixels fails through the agreement
  instead of splicing whatever `buf_out` held.
- A worker takes the agreement's rank-0 word as an idle receive, so a long
  encode does not mark the communicator unhealthy.
- `prepare_vision_embed_for_chunked` moves the pixels onto the sequence: the
  sequence holds the request's only copy and the permits it carries.
- `ATLAS_VISION_CHECK_ROWS=1` re-encodes each item of the window alone and
  compares its rows (diagnostic, slow).

`embed_chunk_sim_tests` drives the real selection, window and plan over random
prompts, caches and chunkings.

Ported from Enntity/atlas ca929ec, 8746056, da94c7a and d85c88d
(AGPL-3.0-only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`vision_prefix_cache_ok` was `is_some_and(|_| false)`, so no vision prompt
was ever looked up or inserted: a repeated image re-prefilled in full. The key
is content hashes over the placeholder runs, and every walker now positions a
prefill that starts after a cached image from the whole prompt, so nothing
about it is tower-specific.

qwen4_exp's in-pass checkpoints register under the same key, or skip a prompt
the key does not cover.

Ported from Enntity/atlas a946481 (AGPL-3.0-only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A vision sequence is never cached when it finishes, so the radix references
it holds are its prompt's. Releasing over the reply would drop references it
never took from a node another request's prompt put on the same key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@AzeezIsh

AzeezIsh commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

I also measured vision TTFT on main before and after this PR (8c2b67218 vs 3bd5d7b1e). Setup: 0.4.0's 8x32k spark config, 2 fresh boots per arm, interleaved; each case sent 3 times. Medians, with cold n=2 and warm n=4 per arm. Receipts are in reiner's ~/code/night1027/out/vttft-303/.

case cold before → after (ms) warm before → after (ms) warm cached tokens, after
1-colour PNG 242 → 247 228 → 224 112
640×480 JPEG 413 → 414 412 → 282 336
1280×720 PNG 1351 → 1355 1357 → 1037 928
MP4 clip 3616 → 3587 3598 → 785 4592

Cold requests are unchanged, and repeats of the same image or video are faster, the video about 4.6×. Before this PR, nothing in either phase was cached.

@AzeezIsh
AzeezIsh merged commit 3bd5d7b into Atlas-Inf:main Nov 1, 2026
1 check passed
@AzeezIsh

AzeezIsh commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Merged after the KL logit-drift and bench gate on reiner + spark2, against main 8c2b67218 with both arms built fresh. Receipts are in reiner's ~/code/night1027/out/dap-303-cecf6539e/.

KL logit drift. For each position I compute KL(main ‖ this PR) from the two engines' top-20 logprob distributions:

  • Text: teacher-forced on 6 cases (prose, code, JSON, multilingual, 6K and 20K registry), plus 4 generated chat prompts, in 2 fresh boots per arm on 0.4.0's spark config.
    • Main against itself (the determinism floor): 0 over 34,245 positions.
    • This PR against main: exactly 0, 34,245 of 34,245 positions identical, in both boots.
  • Vision: on rc16 with --video-allow-ffmpeg, 3 images and 1 video, each sent 3 times. KL is 0 over 36,765 positions, vision cases included.
    • Warm repeats on this PR hit the cache: cached tokens 112, 336, 928 and 4592, against 0 on main.
    • Within this PR, the cached repeats (r2, r3) have KL 0 against the cold first request.

Bench (sq_bench, spark config, interleaved boots, n=4–6 per cell; main, then this PR):

cell main this PR
C1 60.04 60.20
C2 92.47 92.31
C4 142.71 143.25
decode code / json / prose 93.44 / 71.63 / 61.33 93.16 / 71.79 / 61.69

TTFT at 2K, 8K, 16K and 28K is equal. Every cell is inside main's run-to-run range.

rc16: greedy C1 and C8 8/8 identical to main, tools 21/21.

On your question about the gate: I closed it in #286, not deliberately. That port dropped the GLM vision tower, so |v| v.is_glm5_next had nothing to call and became |_| false. Nothing else depended on it being closed.

@AzeezIsh

AzeezIsh commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Post-merge GB10 confirmation is queued. #305 carries this PR's commits unchanged on main 8c2b672, and its sparky gate runs an FP4-Local vision prefix-cache probe:

  • arms: main vs this change vs ATLAS_VISION_PREFIX_CACHE=0;
  • checks: cold vs warm, a new image after a cached one, cached-token counts.
    It also runs the controls, agentic-50 and BFCL. Results will be posted here.

@AzeezIsh

AzeezIsh commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Post-merge GB10 vision probe (sparky, Flash-Next FP4-Local, single GB10).

Setup:

  • Arms: main 8c2b672 without this PR; train integ: GB10 train 2026-11-01 (#159, #303, ports of #255 and #256) #305 containing this PR's commits unchanged (cache on, the default); the same train binary with ATLAS_VISION_PREFIX_CACHE=0.
  • r1short profile, --enable-prefix-caching on every arm, temp 0, reasoning_effort:"none", max_tokens 128, one fresh serve per arm.
  • Images are from crates/atlas-plugin/assets/vision/.

Requests:

  • R1: one image, sent twice.
  • R2: two images, sent twice.
  • R3: R2's turn plus a new image in turn 2.
main cache on cache off (=0)
cached tokens: R1 repeat / R2 repeat / R3 turn 2 0 / 0 / 0 80/85, 432/436, 432/1146 0 / 0 / 0
R1 warm == cold yes yes yes
R2 (two images) warm == cold yes no (diverges ~100 chars in) yes
texts vs main — differ on every request, including the first, cold one identical on every request

Observed under this fingerprint, one run per arm:

  1. The cache engages. The repeats and R3's prefix restore 432 of 436 tokens.
  2. Cold != warm for the two-image prompt. That's the same equality class as prefix caching changes temperature-0 answers on Flash-Next; one tool round-trip goes wrong (warm replay from a Marconi anchor) #158, now reachable for vision prompts.
  3. Outputs change before anything is cached. With the gate on, even a fresh server's first R1/R2 request differs from main and from =0. My guess is the gate changes the prefill geometry (chunk / checkpoint placement) of vision prompts; that's a hypothesis, I haven't traced it.

No quality judgement either way: I haven't scored these texts. Before relying on the default-on cache for qwen4_exp, I'd suggest:

Artifacts on sparky: ~/code/perf/train1101-ctl/vis-{a0,train,train-off}.json and job 68f.

@data-angel

Copy link
Copy Markdown
Contributor Author

@AzeezIsh thanks for the probe, and sorry this sat. Your three findings, in order:

  1. Outputs change before anything is cached. My first suspect is the tail-checkpoint split, from reading the code; I haven't confirmed it on hardware. prefill_chunk_with (prefill_b/dispatch_with.rs) splits a prompt's last chunk at the last block boundary when the model has SSM layers, the cache is active and pc_key(seq, tokens) is Some. Before this PR a vision prompt had no key, so its last chunk ran as one pass. Now it's keyed, so it takes the two-pass shape a text prompt has always taken under --enable-prefix-caching. That changes the cold layout, and near-ties can flip. If this is the whole story, a cache-on arm with ATLAS_NO_TAIL_SPLIT=1 gives the same cold texts as ATLAS_VISION_PREFIX_CACHE=0.

  2. Cold != warm on the two-image prompt. This is the prefix caching changes temperature-0 answers on Flash-Next; one tool round-trip goes wrong (warm replay from a Marconi anchor) #158 class, which main fixes for text only under --exact-prefix-cache. I don't know yet whether vision prompts honour the grid under that flag. The grid cuts on token positions, but the per-chunk encode and the restore point (432 of 436 tokens in your R2) may not land on a cut.

  3. Plan. One GB10, Flash-Next FP4-Local, r1short, your R1/R2/R3 requests, five arms: main before this PR, cache on, =0, cache on with ATLAS_NO_TAIL_SPLIT=1, and cache on with --exact-prefix-cache. I'll count two checks as passed if:

    • the NO_TAIL_SPLIT arm's cold texts equal =0's;
    • under --exact-prefix-cache, warm == cold on R1 to R3.

    If the second check fails, I'll send a PR that puts vision prompts on the grid, plus a GPU test.

On the default: I'd keep vision caching on. Text prompts already behave this way by default, and caching an image differently from the text around it would be its own inconsistency. What I owe is cold == warm for vision under --exact-prefix-cache. If you'd rather have qwen4_exp default-off until that holds, it's the one-line switch_on() change and I'll send it.

Our Sparks are all busy right now; I'll post the run here once I get one.

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