Skip to content

glm vision: borrow the tower's scratch from the prefill arena (~+15% KV pool at TP=2, image/video unchanged) - #338

Merged
AzeezIsh merged 3 commits into
Atlas-Inf:mainfrom
Enntity:glm/vision-lent-scratch
Nov 15, 2026
Merged

AzeezIsh merged 3 commits into
Atlas-Inf:mainfrom
Enntity:glm/vision-lent-scratch

Conversation

@data-angel

@data-angel data-angel commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On the rank that encodes images and video, the GLM vision tower holds 1.25 GiB of scratch for the life of the process. That's because, in multi-rank worlds, GlmCapacity::budgets sizes it for the processor's whole budget: 120,000 patches, one video temporal group. The KV pool is the minimum across ranks, so that one allocation costs every rank the same number of KV blocks.

The tower only runs between prefill passes. So this PR has it borrow its scratch from the BufferArena's per-pass members instead of owning it:

  • One span. The arena's 15 per-pass members are carved from a single allocation (crates/spark-runtime/src/buffers/lend.rs). The tower binds its 10 scratch buffers inside that span (GlmVisionEncoder::new_lent, attach_lent_scratch).
  • Lend. BufferArena::lend does a device-wide sync, so no pass is still using the members, then hands out the span.
  • Return. BufferArena::return_lent zeroes only the borrowed bytes that overlap members decode expects zeroed (zero_all/zero_dirty), then waits on the encode stream. The four members the arena never zeroes are placed first, so that part of the borrow needs no memset.
  • One path. All four encode call sites (prefill, and the vision cache head) go through vision_forward_items(). The span is returned even if the encode fails.
  • Fallback. If the span is smaller than the tower's scratch, the factory allocates privately, which is today's behaviour. Single-rank worlds keep the bounded tower and never lend.
  • Accounting. BufferSizes::total_bytes() counts the aligned span instead of the 15 members.
  • Failure paths. If the zeroing on return fails, return_lent still waits on the encode stream and marks the arena's dirty extent unknown, so the next zero_dirty zeroes every member whole. A lent tower that was never attached refuses to encode, so it can't launch on null scratch.

Results

GLM-5.3-Flash FP4-Local, TP=2 on two GB10s, 4x1M profile, KV shard on, --gpu-memory-utilization 0.91. The A/B uses the same product image with only this commit changed (on top of our Spark branch, 5f79b46b), freshly booted.

Before After
KV pool (blocks of 16 tokens) 116,958–123,298 136,362–144,780 (+12–20%, about +15% paired)
C1 decode at 32K, cold, 3 reps per boot (tok/s) 41.87 (6 boots) 42.06 (4 boots)
C4 decode at 16K, cold, 3 reps per boot (tok/s) 44.12 (6 boots) 44.15 (4 boots)
Image TTFT, cache-busted, adjacent rounds (s) 3.27 3.26
Video TTFT, 48 frames / 19,424 tokens, adjacent rounds (s) 23.14 23.11

The pool's range covers 8 boots of this change: 4 of the test build and 4 of the product release image built from this branch's commits. Rank 0's measured pre-KV footprint varies by about ±0.3 GB from boot to boot. Against the adjacent boots of the build without this commit, the gain was +12% to +18%.

Exactness against the build without this commit:

  • Vision: 10/10 identical: text, reasoning and every token logprob. Cases cover a small image, a 1792×1344 image, a short video and a 19K-token video, each alone and mixed with concurrent text decodes.
  • Greedy text: 8/8 identical. Prompt logprobs (about 20K tokens) show zero delta.
  • Live suite: 109/109.
  • New GPU test. lent_scratch_is_bit_exact_with_owned_scratch fills the span with 0xff bytes, which read as NaN, and checks that two passes produce rows identical to a tower with its own scratch.

Cost: the sync before lending and the memset on return. On the test build, image TTFT was about 20 ms (0.7%) higher than adjacent base rounds. On the release image, image and video TTFT were level with adjacent base rounds (two pairs).

Checks

  • cargo fmt --check

  • cargo clippy -D warnings on spark-runtime and spark-model

  • spark-runtime unit tests pass, including updated arena allocation counts and total_bytes accounting

  • the vision encoder's GPU tests, on a GB10

  • new unit test a_failed_return_of_the_lent_span_leaves_everything_to_zero (mock memset failure injection); it fails without the fix

We didn't run the PR-gate benchmark suites.

Notes for review

  • The 15 members now sit back to back at 4096-byte alignment instead of in separate allocations. A kernel that writes past the end of one member now lands in the next one, not in allocator slack. We found no such kernel, and the output identity above covers the GLM paths. Other models share the arena layout.
  • On TP=2, ncclCommRegister of norm_output/moe_output now registers sub-ranges of the span. Registration failure is non-fatal, and decode speed is level in our A/B.
  • VisionTower::forward_batched and VisionEncoder::forward/forward_batched stay public and skip the lend. Nothing in production calls them, but a future caller on a lent tower would have to go through vision_forward_items.

🤖 Generated with Claude Code

data-angel and others added 3 commits November 14, 2026 15:58
In multi-rank worlds the GLM vision tower sizes its scratch for the
processor's whole budget (GlmCapacity::budgets: 120,000 patches, one video
temporal group). That is 1.25 GiB held for the life of the process on the
encoding rank, and since the KV pool is the minimum across ranks it costs
every rank the same number of blocks.

The tower only runs between prefill passes, so the scratch now comes from
the BufferArena instead. The arena's 15 per-pass members (FP8 activations,
router and gate logits, QKV/attention/norm/MoE outputs, SSM and expert
intermediates) are carved from one allocation (buffers/lend.rs), and the
encoding rank lends that span to the tower:

- lend(): device-wide sync, then hand out the span's base.
- return_lent(): zero only the borrowed bytes that overlap members decode
  expects zeroed, then wait on the encode stream. The four members the
  arena never zeroes sit first in the span, so the borrow's first bytes
  need no memset.

Every encode call site (prefill and the vision cache head) goes through
vision_forward_items(). When the span is smaller than the tower's scratch,
the factory falls back to a private allocation, which is the old behaviour.
total_bytes() accounts for the aligned span.

GLM-5.3-Flash FP4-Local, TP=2 on two GB10s, 4x1M profile, util 0.91: the
KV pool goes from 116,958-122,639 to 142,988-144,780 blocks (+19%). Vision
output is identical to the owned-scratch build (images and a 19K-token
video, alone and mixed with text decodes, every token logprob), greedy text
and prompt logprobs are identical, and text decode is level. Image TTFT is
about 20 ms (0.7%) higher from the sync and memset around each encode.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
norm_output is now carved from the arena's per-pass span, which is freed
once through its base, so the teardown-order check looks for the span.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@AzeezIsh
AzeezIsh force-pushed the glm/vision-lent-scratch branch from b31250e to 1897b47 Compare November 14, 2026 21:15
@AzeezIsh

Copy link
Copy Markdown
Contributor

Restacked: the four GLM PRs are now one linear stack on main cb59c7663, in order: #338 → #346 → #347 → #348. This PR sits on main. Its own commits: a1bd3f5, b575fbf, 1897b47.

git range-diff against the previous heads shows every original commit unchanged (=). All cherry-picks applied without conflicts.

Pre-push checks on the stack top: fmt, clippy --workspace --tests, cargo test --workspace --locked, the ≤500-line cap, typos, SPDX, mdBook and cargo doc all pass. While the PRs below are open, GitHub shows their commits in this PR's diff as well.

Pair gate on 2× GB10 is running now: GLM-5.3-Flash FP4-Local, the SparkGLM 0.3.2 4x1m spark launch with KV shard at 0.91, each PR's binary against its parent's. Results will follow here.

Generated with Devin

@AzeezIsh

Copy link
Copy Markdown
Contributor

Pair gate: passed (2× GB10, reiner + spark2)

Each arm is a binary built from a fresh checkout at an exact commit. Only the binary differs: base = main cb59c7663 (767710ed…), PR = 1897b4786 (f648b082…). Boots alternate base, PR, base, PR, 2 per arm.

GLM-5.3-Flash FP4-Local. Checkpoint 76348ddb, drafter 7d74cdd8. Launch is SparkGLM 0.3.2's own serve.py with the 4x1m profile in spark mode: SPARKGLM_KV_SHARD=1, SPARKGLM_GPU_MEMORY_UTILIZATION=0.91, carveout off.

Check Result
Teacher-forced top-20 logprobs, 6 text cases up to ~20K tokens + 4 greedy chats bit-identical: 27,169/27,169 positions, KL 0, on both boot pairs. The base-vs-base floor is also exact
Vision: 3 images + 1 video, generated with top-20 bit-identical: 29,488 positions, KL 0
Vision encodes while 2 text sequences decode (big2048.png, 640×480 jpg, clip.mp4) 5/5 identical tokens and logprobs; within-arm repeat 5/5
Greedy, 8 prompts at C1 and C8 8/8 and 8/8 identical
Tool probe 21/21 on both arms
KV pool (rank 0 binds; per boot) base 36,729 / 22,187 → PR 44,214 / 45,969 blocks. Rank 0 pre-KV 104.7–105.4 → 104.2 GB
Decode C1 prose / code / json (tok/s, medians of 6) 39.83 / 70.28 / 54.56 → 39.72 / 70.22 / 54.37 (ranges overlap)
Aggregate C1 / C2 / C4 40.17 / 53.44 / 63.91 → 40.17 / 53.05 / 64.73 (ranges overlap)
Cold TTFT 2K / 8K / 16K / 28K (s) 1.20 / 2.66 / 5.08 / 8.49 → 1.21 / 2.66 / 5.08 / 8.48

About the KV numbers: on this pair the pool is set by reiner's rank 0, which has 119.6 GB visible against spark2's 121.6 GB and carries the vision tower. Only 2–3 GB is left for KV at 0.91, so absolute pools are far below your 136K–145K. Rank 0's pre-KV also moves about ±0.35 GB between boots. Read the gain as observed on this pair with n=2 boots per arm, in the direction your change predicts (rank 0 frees the 1.25 GiB scratch). It isn't a re-measurement of your +15%.

Qwen3.8-Flash-Next FP4-Local regression check. The arena layout is shared by every model, so this ran on the pair's default rc16 TP2 serve (Spark config), same interleave, base = cb59c7663:

  • KL 0 over 34,245 text positions (floor exact) and 36,765 vision positions.
  • Greedy C1/C8 8/8 identical; tools 21/21.
  • Decode, C1–C4 and cold TTFT 2K–28K medians all level, e.g. prose 61.41 → 61.80 and C4 142.56 → 142.39 tok/s.

CI: the workspace jobs pass. The remaining reds (env-budget ratchet, PR benchmark gate, cargo deny RUSTSEC-2026-0441) fail on main as well.

Generated with Devin

@AzeezIsh
AzeezIsh merged commit e50e619 into Atlas-Inf:main Nov 15, 2026
24 of 27 checks passed
@AzeezIsh

Copy link
Copy Markdown
Contributor

@data-angel this is merged as e50e619 after the pair gate above: GLM and Qwen are both bit-exact, and rank 0's KV pool grew on our pair. #346, #347 and #348 are restacked on it and gating now. I'll tag you on each with its receipts. Note that I force-pushed the rebased stack to your four glm/* branches; every original commit is patch-identical (range-diff =). Please pull before pushing anything new.

Generated with Devin

AzeezIsh added a commit that referenced this pull request Nov 15, 2026
… they hold (KV pool +11% at TP=2, stacked on #338)

buffer arena: three GLM-5 per-pass buffers sized for what they hold (KV pool +11% at TP=2, stacked on #338)
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