Skip to content

[Engine] Inkling 2 drafter: grow its attention cache with the context - #317

Open
okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/ink-draft-kv-grow
Open

okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/ink-draft-kv-grow

Conversation

@okada-shogo

@okada-shogo okada-shogo commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem and result

With the DSpark drafter (--draft-model), Inkling 2 allocated the drafter's full-attention layer cache for the whole
--max-context at load: 128 MiB at 32K and 512 MiB at 128K. A short chat uses a few MiB of it. That VRAM is missing
from the expert cache for the whole run.

With this PR that cache grows with the conversation, like the target's own full-attention caches already do.

Experts in VRAM, Inkling 2 Mini IQ4_NL, drafter Q8_0, --prompt-batch 1024, default reserve (one engine, one run):

Card Context Measure main this PR alone Change
RTX 2070 Super 8 GB 32K experts after load 736 754 +18
RTX 2070 Super 8 GB 128K experts after load 657 754 +97
RX 9070 XT 16 GB (HIP) 32K experts after load 2,244 2,267 +23
RX 9070 XT 16 GB (HIP) 128K experts after load 2,163 2,266 +103

Decode tok/s, mean of 3 runs (RTX 2070 Super 8 GB, Inkling 2 Mini IQ4_NL, drafter Q8_0, --prompt-batch 1024 --vram-reserve-mib 768, 256 tokens written; three texts in place of the memorized benchmark text: code (C++ source of this engine), a multi-turn chat in the model's template, and novel prose (Pride and Prejudice, from chapter 20; a well-known book, so its drafts are kept more often)):

Card Model Measure main this PR alone Change
RTX 2070 Super 8 GB IQ4_NL decode after chat 302, ctx 32K 35.8 35.8 +0.0%
RTX 2070 Super 8 GB IQ4_NL decode after code 302, ctx 32K 32.0 33.4 +4.4%
RTX 2070 Super 8 GB IQ4_NL decode after prose 302, ctx 32K 51.1 51.4 +0.6%
RTX 2070 Super 8 GB IQ4_NL decode after chat 2,048, ctx 32K 34.4 34.4 -0.1%
RTX 2070 Super 8 GB IQ4_NL decode after code 2,048, ctx 32K 30.6 31.2 +1.8%
RTX 2070 Super 8 GB IQ4_NL decode after prose 2,048, ctx 32K 41.5 46.7 +12.5%
RTX 2070 Super 8 GB IQ4_NL decode after chat 8,192, ctx 32K 30.5 29.5 -3.3%
RTX 2070 Super 8 GB IQ4_NL decode after code 8,192, ctx 32K 32.7 33.4 +2.0%
RTX 2070 Super 8 GB IQ4_NL decode after prose 8,192, ctx 32K 37.5 39.0 +3.9%
RTX 2070 Super 8 GB IQ4_NL decode after chat 2,048, ctx 128K 33.0 35.0 +6.2%
RTX 2070 Super 8 GB IQ4_NL decode after code 2,048, ctx 128K 29.2 31.3 +7.2%

Experts in VRAM after the request, and the share of drafts kept (same runs, mean):

Card Model Request Experts after, main Experts after, this PR Drafts kept, main Drafts kept, this PR
RTX 2070 Super 8 GB IQ4_NL chat 302, ctx 32K 778 803 49% 39%
RTX 2070 Super 8 GB IQ4_NL code 302, ctx 32K 778 803 38% 72%
RTX 2070 Super 8 GB IQ4_NL prose 302, ctx 32K 778 803 84% 83%
RTX 2070 Super 8 GB IQ4_NL chat 2,048, ctx 32K 778 803 28% 24%
RTX 2070 Super 8 GB IQ4_NL code 2,048, ctx 32K 778 803 29% 54%
RTX 2070 Super 8 GB IQ4_NL prose 2,048, ctx 32K 779 803 74% 80%
RTX 2070 Super 8 GB IQ4_NL chat 8,192, ctx 32K 665 688 33% 53%
RTX 2070 Super 8 GB IQ4_NL code 8,192, ctx 32K 662 688 72% 72%
RTX 2070 Super 8 GB IQ4_NL prose 8,192, ctx 32K 662 688 74% 77%
RTX 2070 Super 8 GB IQ4_NL chat 2,048, ctx 128K 702 803 38% 38%
RTX 2070 Super 8 GB IQ4_NL code 2,048, ctx 128K 702 802 49% 51%
Prompt tok/s (same runs)
Card Model Measure main this PR alone Change
RTX 2070 Super 8 GB IQ4_NL prompt chat 302, ctx 32K 186.1 186.0 -0.0%
RTX 2070 Super 8 GB IQ4_NL prompt code 302, ctx 32K 193.3 196.2 +1.5%
RTX 2070 Super 8 GB IQ4_NL prompt prose 302, ctx 32K 230.1 229.5 -0.3%
RTX 2070 Super 8 GB IQ4_NL prompt chat 2,048, ctx 32K 369.7 371.2 +0.4%
RTX 2070 Super 8 GB IQ4_NL prompt code 2,048, ctx 32K 381.1 383.0 +0.5%
RTX 2070 Super 8 GB IQ4_NL prompt prose 2,048, ctx 32K 434.0 435.0 +0.2%
RTX 2070 Super 8 GB IQ4_NL prompt chat 8,192, ctx 32K 353.4 354.3 +0.3%
RTX 2070 Super 8 GB IQ4_NL prompt code 8,192, ctx 32K 363.7 365.6 +0.5%
RTX 2070 Super 8 GB IQ4_NL prompt prose 8,192, ctx 32K 395.6 397.2 +0.4%
RTX 2070 Super 8 GB IQ4_NL prompt chat 2,048, ctx 128K 362.2 370.6 +2.3%
RTX 2070 Super 8 GB IQ4_NL prompt code 2,048, ctx 128K 375.3 381.8 +1.7%
Each run (tok/s)
Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL decode after chat 302, ctx 32K main 36.0 35.3 36.2
RTX 2070 Super 8 GB IQ4_NL decode after chat 302, ctx 32K this PR 35.7 35.2 36.6
RTX 2070 Super 8 GB IQ4_NL prompt chat 302, ctx 32K main 185.5 185.5 187.2
RTX 2070 Super 8 GB IQ4_NL prompt chat 302, ctx 32K this PR 183.5 187.1 187.4
RTX 2070 Super 8 GB IQ4_NL decode after code 302, ctx 32K main 30.8 32.6 32.6
RTX 2070 Super 8 GB IQ4_NL decode after code 302, ctx 32K this PR 32.3 31.2 36.7
RTX 2070 Super 8 GB IQ4_NL prompt code 302, ctx 32K main 192.8 194.8 192.3
RTX 2070 Super 8 GB IQ4_NL prompt code 302, ctx 32K this PR 197.1 192.9 198.7
RTX 2070 Super 8 GB IQ4_NL decode after prose 302, ctx 32K main 50.3 51.6 51.5
RTX 2070 Super 8 GB IQ4_NL decode after prose 302, ctx 32K this PR 51.2 51.6 51.5
RTX 2070 Super 8 GB IQ4_NL prompt prose 302, ctx 32K main 226.3 231.2 232.7
RTX 2070 Super 8 GB IQ4_NL prompt prose 302, ctx 32K this PR 228.4 231.0 229.0
RTX 2070 Super 8 GB IQ4_NL decode after chat 2,048, ctx 32K main 34.8 33.7 34.7
RTX 2070 Super 8 GB IQ4_NL decode after chat 2,048, ctx 32K this PR 34.5 33.7 34.9
RTX 2070 Super 8 GB IQ4_NL prompt chat 2,048, ctx 32K main 369.5 369.2 370.3
RTX 2070 Super 8 GB IQ4_NL prompt chat 2,048, ctx 32K this PR 372.8 368.2 372.7
RTX 2070 Super 8 GB IQ4_NL decode after code 2,048, ctx 32K main 30.1 31.1 30.7
RTX 2070 Super 8 GB IQ4_NL decode after code 2,048, ctx 32K this PR 33.1 29.8 30.7
RTX 2070 Super 8 GB IQ4_NL prompt code 2,048, ctx 32K main 380.4 381.6 381.4
RTX 2070 Super 8 GB IQ4_NL prompt code 2,048, ctx 32K this PR 382.8 384.4 381.8
RTX 2070 Super 8 GB IQ4_NL decode after prose 2,048, ctx 32K main 39.1 44.4 41.1
RTX 2070 Super 8 GB IQ4_NL decode after prose 2,048, ctx 32K this PR 43.2 54.5 42.5
RTX 2070 Super 8 GB IQ4_NL prompt prose 2,048, ctx 32K main 433.3 433.7 434.9
RTX 2070 Super 8 GB IQ4_NL prompt prose 2,048, ctx 32K this PR 436.0 434.9 434.1
RTX 2070 Super 8 GB IQ4_NL decode after chat 8,192, ctx 32K main 28.3 31.7 31.6
RTX 2070 Super 8 GB IQ4_NL decode after chat 8,192, ctx 32K this PR 29.1 29.3 30.2
RTX 2070 Super 8 GB IQ4_NL prompt chat 8,192, ctx 32K main 352.9 354.5 352.7
RTX 2070 Super 8 GB IQ4_NL prompt chat 8,192, ctx 32K this PR 354.0 354.0 354.8
RTX 2070 Super 8 GB IQ4_NL decode after code 8,192, ctx 32K main 33.6 32.3 32.2
RTX 2070 Super 8 GB IQ4_NL decode after code 8,192, ctx 32K this PR 33.2 32.7 34.2
RTX 2070 Super 8 GB IQ4_NL prompt code 8,192, ctx 32K main 363.7 363.8 363.6
RTX 2070 Super 8 GB IQ4_NL prompt code 8,192, ctx 32K this PR 365.8 365.2 365.7
RTX 2070 Super 8 GB IQ4_NL decode after prose 8,192, ctx 32K main 34.6 41.2 36.8
RTX 2070 Super 8 GB IQ4_NL decode after prose 8,192, ctx 32K this PR 42.1 35.5 39.4
RTX 2070 Super 8 GB IQ4_NL prompt prose 8,192, ctx 32K main 395.7 395.6 395.6
RTX 2070 Super 8 GB IQ4_NL prompt prose 8,192, ctx 32K this PR 397.0 397.3 397.4
RTX 2070 Super 8 GB IQ4_NL decode after chat 2,048, ctx 128K main 33.3 33.2 32.4
RTX 2070 Super 8 GB IQ4_NL decode after chat 2,048, ctx 128K this PR 34.3 35.3 35.4
RTX 2070 Super 8 GB IQ4_NL prompt chat 2,048, ctx 128K main 363.3 361.5 361.7
RTX 2070 Super 8 GB IQ4_NL prompt chat 2,048, ctx 128K this PR 370.0 370.3 371.5
RTX 2070 Super 8 GB IQ4_NL decode after code 2,048, ctx 128K main 28.7 28.7 30.3
RTX 2070 Super 8 GB IQ4_NL decode after code 2,048, ctx 128K this PR 31.7 31.0 31.3
RTX 2070 Super 8 GB IQ4_NL prompt code 2,048, ctx 128K main 375.1 373.9 376.9
RTX 2070 Super 8 GB IQ4_NL prompt code 2,048, ctx 128K this PR 379.9 381.9 383.6
Earlier runs on a memorized benchmark text (not comparable: drafts kept ~99% after 2K+)

After the request in the speed runs below (--vram-reserve-mib 768): 778 -> 803 (302 tokens, 32K), 662 -> 688 (8,192
tokens, 32K), 703 -> 802 (2,048 tokens, 128K).

Prompt tok/s, mean of 3 runs (RTX 2070 Super 8 GB, IQ4_NL, drafter Q8_0, --prompt-batch 1024 --vram-reserve-mib 768):

Card Model Measure main this PR alone Change
RTX 2070 Super 8 GB IQ4_NL prompt 302 tokens, ctx 32K 223.3 224.5 +0.5%
RTX 2070 Super 8 GB IQ4_NL prompt 8,192 tokens, ctx 32K 364.9 366.7 +0.5%
RTX 2070 Super 8 GB IQ4_NL prompt 2,048 tokens, ctx 128K 369.4 377.5 +2.2%

Decode tok/s, mean of 3 runs (RTX 2070 Super 8 GB, IQ4_NL, drafter Q8_0, --prompt-batch 1024 --vram-reserve-mib 768):

Card Model Measure main this PR alone Change
RTX 2070 Super 8 GB IQ4_NL decode after 302 tokens, ctx 32K 37.5 38.2 +1.9%
RTX 2070 Super 8 GB IQ4_NL decode after 8,192 tokens, ctx 32K 43.7 44.0 +0.6%
RTX 2070 Super 8 GB IQ4_NL decode after 2,048 tokens, ctx 128K 42.2 50.1 +18.8%

Why decode is faster after the longer prompts on both sides: the benchmark prompt is a text the model knows by heart (a software licence), so after 2K–8K tokens of it the drafter's guesses are almost all accepted, while after 302 tokens far fewer are. Compare each row's two columns, not the rows with each other. The 128K gain comes from the 99 extra experts in VRAM (703 → 802); main's three runs there spread from 38.9 to 46.1 tok/s, this PR's from 49.2 to 50.7.

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL prompt 302, ctx 32K main 224.1 220.1 225.7
RTX 2070 Super 8 GB IQ4_NL prompt 302, ctx 32K this PR 226.1 221.5 225.8
RTX 2070 Super 8 GB IQ4_NL prompt 8,192, ctx 32K main 365.0 364.5 365.1
RTX 2070 Super 8 GB IQ4_NL prompt 8,192, ctx 32K this PR 366.3 367.1 366.7
RTX 2070 Super 8 GB IQ4_NL prompt 2,048, ctx 128K main 366.4 372.0 369.7
RTX 2070 Super 8 GB IQ4_NL prompt 2,048, ctx 128K this PR 375.4 377.2 379.8
RTX 2070 Super 8 GB IQ4_NL decode 302, ctx 32K main 37.1 37.7 37.7
RTX 2070 Super 8 GB IQ4_NL decode 302, ctx 32K this PR 38.2 38.2 38.2
RTX 2070 Super 8 GB IQ4_NL decode 8,192, ctx 32K main 43.8 43.3 44.1
RTX 2070 Super 8 GB IQ4_NL decode 8,192, ctx 32K this PR 43.3 44.2 44.5
RTX 2070 Super 8 GB IQ4_NL decode 2,048, ctx 128K main 38.9 46.1 41.5
RTX 2070 Super 8 GB IQ4_NL decode 2,048, ctx 128K this PR 50.7 50.4 49.2

Output bits are unchanged (frozen identity below).

Related discussion

none

Approach

  • The drafter's full layer gets its keys and values from kv_alloc() in Inkling2::alloc_caches(). cover() maps
    rows as the target's rows grow, and trim_kv() gives them back. The window layers' small rings stay as before.
  • The drafter only writes and reads positions inside the context (guess_block() limits a block to the room left),
    so max_context rows are enough.
  • A conversation brought back from the conversation cache does not carry the drafter's rows. The rows mapped for it
    may be VRAM the expert cache used before, so load_card_rows() zeroes them (before, fresh VRAM was zero).
  • Metal keeps the old single allocation.

Validation

Passed:

  • Identity, RTX 2070 Super, CUDA 12.9 sm_75, main 36e654b vs this PR. Experts capped and frozen
    (STRATA_GPU_EXPERTS=600 STRATA_FREEZE_EXPERTS=1), drafter draft lengths fixed (STRATA_SPEC_FIXED=1),
    --serve --kv fp16 --prompt-batch 1024 --draft-model dspark-Inkling-2-Mini-Q8_0.gguf, greedy token ids:
    • 2,048-token prompt + 128 tokens, context 32K: SAME (main run twice, this PR).
    • 8,192-token prompt + 128 tokens, context 32K: SAME.
    • 2,048-token prompt + 128 tokens, context 128K: SAME.
    • (These ran before the restore zeroing was added; that path is not reached by them.)
  • VRAM: the table above, from a trace build that reports every allocation (RTX 2070 Super and RX 9070 XT).
  • Speed: the table above (e2e-nw.sh, 3 rounds).
  • Builds: CUDA sm_75 (this branch). HIP gfx1201: the same change, in a test tree with VRAM tracing, built and ran on an RX 9070 XT (ROCm on Linux).
  • Independent code review of the diff.

Failed:

  • none observed

Not run:

  • Metal: no Mac. The change is #if-guarded out there.
  • A conversation-cache restore with the drafter (the zeroing path) was not run on a card; it is covered by review.

Limits

🤖 Generated with Claude Code

The DSpark drafter's full-attention layer kept keys and values for the whole
--max-context from the start (fp16, 4 KiB a position): 128 MiB at 32K and
512 MiB at 128K that the expert cache never had. They are now two more of
the engine's growing attention caches (kv_alloc(), mapped by cover() with
the target's rows and given back by trim_kv()), so a short conversation maps
only the rows it reaches. The window layers' rings stay as they were. Metal
keeps one allocation.

A conversation brought back (load_card_rows()) does not carry the drafter's
rows; the ones mapped again for it can be VRAM the expert cache had, so they
are zeroed there, as fresh VRAM was before.

Placement only: the drafter writes and reads the same cells; only the VRAM
behind the rows it does not reach yet is not mapped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant