Skip to content

Inkling 2: window-bounded KV for the sliding-window visits (1/4, SparkInkling 0.5.1 engine) - #430

Merged
AzeezIsh merged 4 commits into
Atlas-Inf:mainfrom
Enntity:inkling2/up051-1-swa-kv
Nov 28, 2026
Merged

AzeezIsh merged 4 commits into
Atlas-Inf:mainfrom
Enntity:inkling2/up051-1-swa-kv

Conversation

@data-angel

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

Copy link
Copy Markdown
Contributor

Change 1 of 4 porting the SparkInkling 0.5.1 engine to main (9f4fd773f), one change per PR, stacked linearly.
This PR is three commits on main; the next PR is based on this one's branch.

What

Inkling 2 Mini's 40 sliding-window visits (1,024-token window) held a KV row for every token of context: 192 KiB a
token over the 48 visits. They now keep KV only for their window, so context costs the 8 global visits' 32 KiB a
token.

  • spark-runtime (790c09b07): PagedKvCache::new_windowed keeps a plan's sliding-window layers in pools of their
    own, a few blocks per live sequence, at a local block the primary block maps to (a device map the kernels read).
    A local block follows its primary block, so a shared prefix shares its window rows and a freed primary block drops
    them. Live sequences hold what their next forward reads (window_hold / window_release), snapshots pin the
    window they resume from, pins go first under pressure, and an exhausted pool is a KV cache exhausted refusal.
    A released local block is lent on another stream only after an event recorded on the releasing stream.
    poison_block moves to zero_impl.rs beside the zeroing it mirrors.
  • Inkling 2 (c4b3d64ee): the factory builds the windowed cache for Inkling (high-speed swap, the NVMe disk tier,
    multi-rank and GLM plans keep full pools). Visit 0 maps the rows' slots and block tables through the window map
    (inkling2_window_meta, captured in decode graphs); window visits write and read through them. The attention
    kernels are unchanged and read the same rows at the same positions in the same order. A snapshot whose window is
    gone is declined and the prefill recomputes; swap-resume is off with a window.
  • Snapshot pins (b83832a7e): pins take up to an eighth of the KV budget (at least 8), and a resume that adopts a
    pinned window moves it to the back of the eviction order, so a second session's long prompt no longer evicts the
    first one's resume points.

Port conflict: main moved route / kv_write from inkling2/glue.rs to glue/route.rs; window_meta stays in
glue.rs (479 lines).

Observed (the fork's engine, not this branch; see "Where the numbers come from" below): SparkInkling 0.5.1's
default profile serves 8 sequences of up to 1,048,576 tokens at 0.80 of GPU memory on one DGX Spark, with KV pools
of 1,909,536 tokens (Strict weights) and 1,790,944 (Spark weights); its saver profile (0.50) holds 882,992 and
741,520.

The series

The engine of SparkInkling 0.5.1
(Enntity/atlas inkling2/rc-0.5.1-20261126 at a4dbba378), ported onto main (9f4fd773f) as one change
per PR, stacked linearly:

  1. Window-bounded KV for the sliding-window visits — Inkling 2: window-bounded KV for the sliding-window visits (1/4, SparkInkling 0.5.1 engine) #430
  2. Spark projections on the row-invariant tensor-core kernels — Inkling 2: Spark projections on the row-invariant tensor-core kernels (2/4, stacked on 1/4) #431
  3. Inkling 2 prefill kernels — Inkling 2: prefill kernels — tensor-core bias, per-head flash attention, fused norms, gates and gate_up epilogue (3/4, stacked on 2/4) #432
  4. scheduler: a busy prompt runs one prefill chunk a tick — scheduler: a prompt arriving while others decode runs one prefill chunk a tick (4/4, stacked on 3/4) #433

The DFlash2 drafter commits that 0.5.0 carried are already on main. Two things are not in this series:
the fork's ATLAS_PREFILL_BUSY_TOKENS chunk cap (#4 here makes it unnecessary, and the fork removed it, so it is
not proposed and no PR adds an ATLAS_* name) and the opt-in LoopCD (--loopcd), which is not proposed for now.

After #4, the engine files these commits touch equal a4dbba378's except for main's own changes underneath
them (glue.rs's route / kv_write now in glue/route.rs; weights.rs, serve_load.rs), rustfmt, code moved
as is to keep files under the 500-line cap (#2, #3), and the AMD flash prefill kept beside the new gb10 kernel (#3).

Where the numbers come from. Every GPU number below was measured on the fork's engine on its older base
(before #413 and #426), not on these branches: SparkModelBench 293a44f (RigMark protocol 1.3.0, RigMark Quality
v0.0.2, reasoning_effort: low) on one DGX Spark, SparkInkling 0.5.1's default profile, published with receipts in
results/2026-11-27-rc-0.5.1.
No GPU has run these rebased branches.

Test plan

  • On this branch (Rust 1.93.1, CUDA stubs, no GPU): cargo fmt --all -- --check; cargo clippy -p spark-runtime -p spark-model -p spark-server --tests -- -D warnings; cargo test --release -p spark-runtime -p spark-model -p spark-server (spark-model 2,524, spark-runtime 608 and spark-server 3,258 passed; the same five spark-server tests fail on main in this container (three need a .git checkout, two read the disk's free space)); the 500-line cap; check_spdx.py, check_kernel_shadows.py
  • No new ATLAS_* name (check_env_budget.py reads 1,141 on main and on this branch)
  • On the fork's engine, one DGX Spark: SparkModelBench full run in both modes, the task probe (30/30 arithmetic, 10/10 tool calls), and greedy text with drafts equal to text without at one, two and four requests, 8/8 prompts in both modes
  • spark benchmark run <gate> --pull-request-gate gates and the strix-hip build: left to the maintainers (this branch touches no strix-hip file; inkling2_misc.cu, which it extends with inkling2_window_meta, is symlinked there)

Authorship

AI-authored: Claude (Opus 5.5) in Claude Code, directed and reviewed by a human; no hand-written code.

CLA

  • I have read and agree to the Contributor License Agreement.

🤖 Generated with Claude Code

data-angel and others added 3 commits November 27, 2026 10:31
A sliding-window layer reads only its last `window` keys, yet the paged
cache gave every layer a row per token of context. `new_windowed` keeps
the plan's layers in pools of their own, a few blocks per live sequence,
at a local block the primary block maps to (a device map the kernels
read). A local block follows its primary block, so a shared prefix
shares its window rows; a freed primary block drops them.

Holders keep a mapping alive: live sequences hold what their next
forward reads (`window_hold` / `window_release`), snapshots pin the
window they resume from (with a primary ref), pins go first under
pressure, and an exhausted pool is a `KV cache exhausted` refusal. A
released local block is lent on another stream only after an event
recorded on the releasing stream. Window layers keep unpooled entries in
the primary list, so primary-indexed loops skip them; `poison_block`
moves to zero_impl.rs beside the zeroing it mirrors.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 790c09b)
Inkling 2 Mini's 40 sliding-window visits (1,024 tokens) held a KV row
for every token: 192 KiB a token over 48 visits. They now take the
window pool, so context costs the 8 global visits' 32 KiB a token.

- factory: Inkling builds a windowed cache sized for every live
  sequence's window and step, one forward's new rows and up to 8-32
  snapshot pins; the rest of the budget buys primary blocks. High-speed
  swap, the NVMe disk tier, multi-rank and GLM plans keep full pools.
- model: a sequence holds the window its next forward reads, from the
  32-key tile holding its first row's first key (where the prefill
  kernels start), through its last row. Holds are taken last in both
  reservations, trimmed after each prefill chunk and released before
  the table shrinks. A rejected draft never moves the floor. Rows below
  a resume point must still be mapped, so no zeroed block stands in for
  rows that exist; a snapshot whose window is gone is declined and the
  prefill recomputes. Snapshot aux collection queues a pin, applied
  under the cache lock. Swap-resume is off with a window.
- layer: visit 0 maps the rows' slots and block tables through the
  window map (`inkling2_window_meta`, captured in decode graphs); window
  visits write and read through them. Attention kernels are unchanged
  and read the same rows at the same positions in the same order.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit c4b3d64)
A long prompt snapshots every 4,096 tokens, and a resume needs its
snapshot's window rows. One pin per live sequence kept only the newest
eight, so a second session's long prompt evicted the first one's resume
points (seen on one GB10: a 607K prompt declined the 180K prompt's shared
snapshot at 69,640 and recomputed it). Pins now take up to an eighth of
the KV budget (at least 8), and a resume that adopts a pinned window
moves it to the back of the eviction order.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit b83832a)
@data-angel

Copy link
Copy Markdown
Contributor Author

Holding this as a draft: I found a bug in the window-bounded KV it carries.

Two concurrent prompts of about 20K tokens each, on one DGX Spark (Inkling 2 Mini, Spark mode, 8 sequences of up to 1,048,576 tokens, 8,192-token prefill chunks), both fail. Typical error: KV window: window rows of logical blocks [...] below position 24576 are gone (from model/kv_window.rs sync), as a prefill chunk or as a decode or verify step. Four concurrent 20K prompts all fail the same way. Two concurrent 8K prompts, one chunk each, succeed. So a sequence's window rows are freed while its next prefill chunk still needs them, when two multi-chunk prefills interleave. Our 0.5.0 build, which predates the busy-prefill change in #433, fails identically, so the cause is in this PR's code.

I'll push the fix to this branch with a CPU test that interleaves two chunked prefills, and mark it ready again.

🤖 Generated with Claude Code

@data-angel
data-angel marked this pull request as draft November 27, 2026 18:42
Two or more long prompts prefilling together failed with "KV window:
window rows of logical blocks [...] are gone" (HTTP 500, or one token
then an error). Their later chunks ran through the batched prefill
step, which reserved blocks with ensure_blocks_through_prefill and
never took the window holds, so those chunks wrote no sliding-window
KV and the next step found it missing. The mixed decode+prefill step,
the whole-prompt prefill (a preempted request's resume) and the
two-phase prefill reserved the same way. One-chunk prompts take the
single path, which holds its rows, so they passed.

- ensure_prefill_blocks reserves a prefill forward's blocks and window
  rows together; every path that does not vote uses it, and a source
  test keeps direct ensure_blocks_through_prefill calls out of model
  code.
- The batched, kernel-batched and mixed steps trim each sequence's
  window after its chunk, so the pool holds one forward's rows at a
  time as it is sized to.
- With window-bounded KV a whole-prompt prefill runs as chunks, the
  two-phase prefill falls back to chunks, and scoring is refused (its
  streams would hold whole prompts' windows).

No effect without a window plan: the hold and trim return at once.

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

Copy link
Copy Markdown
Contributor Author

Fixed in dca1509, and this PR is ready again.

Cause. When two or more prompts are mid-prefill, the scheduler runs their later chunks through the batched prefill step. That step reserved blocks with ensure_blocks_through_prefill and never took the window holds, so those chunks wrote no sliding-window KV, and the next step reported the rows as gone. The mixed decode+prefill step, the whole-prompt prefill (used to resume a preempted request) and the two-phase prefill reserved the same way. A one-chunk prompt takes the single path, which does hold its rows, so 2×8K passed and 2×20K failed.

Fix.

  • ensure_prefill_blocks reserves a prefill forward's blocks and its window rows together, and every prefill path that does not vote now uses it.
  • The batched, kernel-batched and mixed steps trim each sequence's window after its chunk, so the pool holds one forward's rows at a time, as it is sized to.
  • With window-bounded KV, a whole-prompt prefill runs as chunks, the two-phase prefill falls back to chunks, and scoring is refused.
  • New tests: interleaved_chunked_prefills_keep_both_windows, and a source test that keeps direct ensure_blocks_through_prefill calls out of model code.
  • Without a window plan nothing changes: the hold and trim return at once.

Checks.

  • On this branch: fmt, clippy (spark-model and spark-server, with tests), and spark-model's 2,526 lib tests.
  • On the 0.5.1 engine with the same commit, in a SparkInkling image on one DGX Spark, the engine's whole CPU suite passed (8,863 tests).
  • Through that image's ./start.sh (default profile), concurrent prompts of 2×8K, 2×20K, 4×20K, 8×20K and 2×60K all finished with HTTP 200 and full-length answers in both Spark and Strict mode, with no errors in the server log. Before the fix, 2×20K failed.

🤖 Generated with Claude Code

@data-angel
data-angel marked this pull request as ready for review November 27, 2026 22:15

@AzeezIsh AzeezIsh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@data-angel Gated dca1509e0 against its base main 9f4fd773f: one GB10 (sparky jobs 208, 209) and one Strix Halo (DevLab A/B). Both binaries were built from fresh worktrees at the exact commits. It holds up, so I'm merging it. One observation for later is at the end.

Exact where it should be (GB10, base vs PR)

check result
Inkling Strict, plain: greedy C1 / C8 8/8 · 8/8 identical
Inkling Strict + incoai DFlash2 (0.5.1 strict draft env): greedy C1 / C8, vs plain base 8/8 · 8/8 identical, base and PR
Inkling Spark + DFlash2: greedy C1 / C8 8/8 · 8/8 identical
SparkInkling 0.5.1 default profile (8 × 1,048,576, 8,192-token chunks) + DFlash2, PR: greedy C1 vs plain base 8/8 identical
4 × ~25K-token prompts at once, prefix cache off: cold serial answers 4/4 identical, twice (job 209)
4 × ~25K at once, with DFlash2 (Strict and Spark): concurrent answers base vs PR 4/4 · 4/4
Qwen3.8-27B QAD exact profile: lp_dump (26,912 tokens), greedy C1 / C8 max|Δ| 0.0000 · 8/8 · 8/8

Concurrent answers without the drafter differ from run to run on main itself: base vs base was 2/4 identical in job 209, PR vs PR 4/4. Batched prefill timing on main explains it, not this PR.

Capacity (KV pool, max tokens, same budget)

config base PR
Strict, 0.80, 131K max seq 404,704 2,069,536
0.5.1 default profile (Strict + DFlash2) 378,000 1,906,640
Spark + DFlash2, 0.80 364,096 1,854,192
Strix Halo Spark, 0.80 (A/B harness) 298,336 1,501,008

Your repro list and long context (PR, 0.5.1 default profile): 2×20K, 4×20K, 8×20K and 2×60K at once (prompts of ~25K and ~75K tokens) all returned HTTP 200 with full-length answers. One 624,810-token prompt was answered correctly, which base can't hold at this profile. Under pressure I ran 8 × ~123K at once into a 335,808-token pool (0.35): all 8 were served (admission queued them, nothing preempted). 0 ERROR/panic lines in every arm.

Also clean (PR): tools 14/14 in every arm; quality probe arithmetic 40/40; image 3/3; cachehit warm == cold 6/6 in every arm; cross-contamination 8/8 identical, 0 contaminated. Decode is unchanged (sq_bench Strict C1 36.1 / 36.1 tok/s, C8 110.9 / 110.7). Cold 41K-token prefill was 3,437 / 3,458 tok/s on base and 3,334 / 3,390 on the PR, about 2.5% slower on 2 reps each. That's within reach of noise, so I'm noting it, not claiming it.

Strix Halo (DevLab run 46134953448, harness inkling_ab_gate.sh, base built on the same runner): the strix-hip build passes, and the window is on there too. lp_dump is identical (max|Δ| 0 over 19,952 tokens), NLL ties 32/32, Strict greedy 8/8, decode unchanged (Spark C1 42.5 → 42.8, C8 64.7 → 66.0), 0 hipError lines. Spark greedy at C4 was 3/8, but a main-vs-main control on the same harness (run 46140800913) gives 2/8 Spark and 6/8 Strict. So C4 text on that harness isn't stable run to run, and the 3/8 says nothing about this PR.

CI: every workspace job passes. The env budget, cargo deny and the PR benchmark gate are red on main too.

Observation, not blocking: one prefix-cache resume that base served was declined on the PR (job 208 Strict arm, after a 2 × ~25K concurrent phase):

prefix snapshot at 24992 declined: KV window: window rows of logical blocks [1536..1561] below position 24992 are gone
Prefix cache hit: 25008 tokens (1563 blocks) but no SSM snapshot — recomputing all KV

The missing rows start exactly at token 24,576, a chunk start, and the rows below it were still mapped. So the pin for the 24,992 snapshot looks evicted (or never taken) while an older pin survived. The answer is still right because it recomputes, but it costs a re-prefill (8.6 s vs 2.0 s here). Hit/recompute counts otherwise match base in every arm. Worth a look when you next touch the pins.

The stack: #431–#433 carry this PR's first three commits but not dca1509e0. Their branches merge cleanly onto main with this PR, and none adds a direct ensure_blocks_through_prefill call, so no rebase is needed. I'm gating each one as the merge commit main will get, and those runs are in progress.

sparky nightq 208 / 209 · base 9f4fd773f ink 3eddad86 q27 ed47238d · PR dca1509e0 ink 27eaee9a q27 a592dd85 · DevLab 46134953448 / 46140800913 · 2026-11-28

🤖 Generated with Claude Code

@AzeezIsh
AzeezIsh merged commit ffb94a5 into Atlas-Inf:main Nov 28, 2026
24 of 28 checks passed
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