Skip to content

[Engine] Inkling 2: faster expert-cache refill after long prompts - #299

Open
okada-shogo wants to merge 2 commits into
simplexai-labs:mainfrom
okada-shogo:port/cache-tune
Open

okada-shogo wants to merge 2 commits into
simplexai-labs:mainfrom
okada-shogo:port/cache-tune

Conversation

@okada-shogo

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

Copy link
Copy Markdown
Contributor

Problem and result

On an 8 GB card, Inkling 2 Mini IQ4_NL keeps about 900-1,000 of its 3,072 experts in VRAM. Two things make the first answer tokens slow:

  • After a long prompt, most of the expert cache is empty: only 119 of ~860 slots are filled after 32K tokens, and the cache refills at 64 experts a token. The first answer tokens therefore run mostly on the CPU.
  • During an answer, the cache follows the routing with a 128-token half-life. That is slower than an answer changes topic.

This PR makes two Inkling-only changes, one commit each. Both are on by default and were measured together:

  1. The cache's usage counts decay with a half-life of 32 tokens instead of 128.
  2. The cache does 128 uploads at a time instead of 64, which refills it faster after a prompt.

Other model families are unchanged. On an RTX 2070 8 GB, the first 64 tokens after a 32K prompt take 6% less time a token, and the first 16 are 15-16% faster, on IQ4_NL and UD-Q2_K_XL. Almost all of this comes from the faster refill. Decode over 512 tokens after 32K is +1.8% (IQ4_NL) and +1.3% (UD-Q2); other cases are within noise.

First tokens after the prompt, RTX 2070 8 GB, ms/token (lower is better; mean of 3 runs). "Refill only" is this branch with STRATA_SWAP_HALF=128, i.e. main's half-life:

Model Prompt Tokens main refill only this PR
IQ4_NL 32K 1-16 55.6 47.1 47.2 (-15%)
IQ4_NL 32K 1-64 39.1 36.8 36.9 (-5.6%)
UD-Q2_K_XL 32K 1-16 42.7 36.0 36.0 (-16%)
UD-Q2_K_XL 32K 1-64 28.7 27.0 26.9 (-6.3%)
IQ4_NL 8K 1-64 33.6 32.5 32.8 (-2.4%)
UD-Q2_K_XL 8K 1-64 22.0 21.8 21.7 (-1.4%)

Decode over the 512 tokens, RTX 2070 8 GB, tok/s (higher is better; mean of 3 runs):

Model After main refill only this PR
IQ4_NL 8K 34.4 34.6 34.2 (-0.5%)
IQ4_NL 32K 30.3 30.6 30.8 (+1.8%)
UD-Q2_K_XL 8K 48.0 48.1 48.1 (+0.3%)
UD-Q2_K_XL 32K 40.9 41.2 41.4 (+1.3%)

The refill does almost all of it. The shorter half-life adds +0.5% after 32K, and -0.5% / +0% after 8K, which is within the run-to-run spread on these prompts.

Accuracy: perplexity is unchanged (+0.06%, the same as main against itself). Top-1 agreement with main's greedy answers is 98.1%, above the 97.8% bar for a default change. For comparison, refill only scores 97.8%, main with the cache frozen 98.4%, and main against itself 99.3%. An earlier run on an older main gave 97.4% for the same pair (see Validation).

Related discussion

This follows the expert-cache measurements in #200. Credit: merbanan (Strata inkling2-turing) for the decayed-count cache idea.

Approach

  • Engine::swap_half() is a new family hook, kSwapHalf (128) by default. Inkling 2 returns 32. STRATA_SWAP_HALF=N overrides it.
  • Engine::default_swaps() gives the value of --expert-swaps when it is not set: 64 by default, 128 for Inkling 2.
  • --expert-swaps now accepts up to 256 (it was 64).
  • Trades (an expert in VRAM replaced by a busier one from RAM) stay at most 64 a token (kSwapTrades), as before, because each trade needs a spare slot in the RAM mirror and build_mirror() keeps about 64. More uploads only fill the empty slots left after a prompt sooner.
  • Metal keeps 64 uploads, because its memory arena holds back the bytes of every upload in flight.
  • Frozen identity holds by construction: under STRATA_FREEZE_EXPERTS, follow() and grow_cache() move nothing.
  • After a prompt, follow() fills the empty slots first, as many a token as there are free uploads, so 128 uploads double the refill rate. During an answer, trades stay at 1-2 a token.
  • The values were chosen offline. I replayed routing traces through a copy of follow() whose hit rate matches the engine's own counter within 0.2 points. The traces cover 18 answers of 512 tokens each: code, chat and prose after 2K-32K prompts.

Change class: placement. It changes which experts sit in VRAM; routing is unchanged.

Offline cache simulation (IQ4_NL, RTX 2070 8 GB traces)
Policy experts found in VRAM swaps / token
main (half-life 128, in >= 2 x out + 2) 80.7% 0.6
half-life 32 (this PR) 82.3% 1.95
LRU / ARC (64 a token) 77.6% / 78.5% 53 / 46
Belady, demand fetch (offline ceiling) 89.4% 15

After a 32K prompt, the share of experts found in VRAM over the first 16 tokens goes from 40% to 50% (IQ4_NL) and from 47% to 69% (UD-Q2). Full write-up: cache-policy-sim.md.

Validation

Passed:

  • Speed on a Bazzite PC: RTX 2070 8 GB (not Super, PCIe 3.0 x16), Ryzen 7 5800X. The tables in "Problem and result" come from this run.

    • Revisions: main 36e654b against this branch (7030c07 + 31b77bc), built as pushed.
    • Setup: --serve, --kv fp16, --prompt-int8, plus STRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1 STRATA_INK_PRESTAGE=1. Each run first sends a 302-token warm-up request, waits 18 s, and then sends the measured request: 512 tokens after an 8K or 32K prompt.
    • 3 rounds in a Latin-square order. Log: ~/inkling-work/e2e/ct-e2e3/.
  • Top-1 against main's greedy answers, same PC. Method from 1636925: 3 files (IQ4_NL, UD-Q2_K_XL, an IQ3_S) × prompts of 302 / 2,048 / 8,192 tokens × 64 steps = 576 steps, cache not frozen. Revisions: main 36e654b against this branch as pushed (31b77bc). Per-case rows are in rows.tsv. Log: ~/inkling-work/h2h/ctr-r1/.

    Engine same top-1 KL mean
    main fed its own answers again 99.3% 0.0008
    main frozen (no swaps) 98.4% 0.013
    refill only 97.8% 0.027
    this PR (refill + half-life 32) 98.1% 0.025

    Without the 2,048-token prompt, where every placement change shows a KL spike (main's own included), the scores are: main again 99.0%, refill only 98.2%, this PR 98.7%.

  • Frozen identity (STRATA_FREEZE_EXPERTS=1): the top-20 lines match main's in all 9 cases, in both top-1 runs (logs ~/inkling-work/h2h/ctr-r1/ and ~/inkling-work/h2h/ct-acc1/).

  • Perplexity, wikitext-2-raw test, IQ4_NL, 16 chunks of 2,048 tokens (16,384 scored), same setup as the speed run, cache not frozen. Results in table.md. Log: ~/inkling-work/acc/ppl/ppl-ct/.

    • main 5.7625; main again 5.7659 (+0.06% ±0.09%); this PR 5.7657 (+0.06% ±0.13%).
    • KL vs main: main again 0.0023, this PR 0.0024. Same top-1: 98.11% / 98.14%.
    • This run is on main 1a41ff6, before the review fixes. Those fixes cap trades at 64 a token as main does, keep Metal at 64, and add checks on the setting. None of them changes the default path's behaviour on CUDA.
  • Worst cases for the shorter half-life (draft model, 4 parallel chats): no loss against refill only. The decay counts evaluated rows, not accepted tokens, so a step that evaluates several rows decays faster. Measured on a local RTX 2070 Super with the same flags as the speed run (--prompt-int8, STRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1 STRATA_INK_PRESTAGE=1). Sides: main 36e654b; refill only (this branch, STRATA_SWAP_HALF=128); this PR.

    • DSpark drafter (--draft-model dspark-Inkling-2-Mini-Q8_0.gguf, --vram-reserve-mib 768 on every side). Warm-up request first, 512 tokens, 3 Latin-square rounds, decode tok/s. Log: ~/inkling-work/e2e/ct-mr3-draft/.

      Model After main refill only this PR
      IQ4_NL 8K 44.4 44.9 44.1
      IQ4_NL 2K 49.4 42.5 49.9
      UD-Q2_K_XL 8K 63.0 62.8 63.4
      UD-Q2_K_XL 2K 62.8 63.3 63.8
      • After 8K, every side accepts 97-98% of the drafts, and the three agree within about ±1%.
      • After 2K, the greedy text changes with placement and the acceptance changes with it (52-98%). Tok/s follows the acceptance, not the cache. Refill only's 42.5 includes one run at 52% acceptance.
      • Engine counters, UD-Q2_K_XL, 302-token prompt + 512 tokens, identical acceptance on every side: experts from VRAM 94.0 / 94.1 / 94.2%, about 2.3 swaps a token on all three.
      • At the default 256 MiB reserve, the drafter with these settings ran out of VRAM (cudaGraphInstantiate) after a 2K prompt, on main too. That is a separate issue.
    • 4 chats at once ("parallel": 4, 300 tokens each). 9 fresh servers in a Latin-square order, 2 rounds each, aggregate decode tok/s. Log: ~/inkling-work/e2e/ct-mr5-par/.

      Model main refill only this PR
      IQ4_NL 54.9 55.1 55.1
      UD-Q2_K_XL 68.0 68.2 68.2
Speed: each run (decode tok/s), and earlier runs on main 1a41ff6 and with 96 uploads

Decode tok/s for each run of the speed test above:

Model After Build Run 1 Run 2 Run 3
IQ4_NL 8K main 34.6 34.3 34.2
IQ4_NL 8K refill only 34.4 34.6 34.7
IQ4_NL 8K this PR 34.5 34.2 33.9
IQ4_NL 32K main 30.2 30.3 30.3
IQ4_NL 32K refill only 30.7 30.6 30.6
IQ4_NL 32K this PR 31.0 30.5 30.9
UD-Q2_K_XL 8K main 48.0 48.0 48.0
UD-Q2_K_XL 8K refill only 48.2 48.1 48.1
UD-Q2_K_XL 8K this PR 48.2 48.0 48.2
UD-Q2_K_XL 32K main 41.0 40.8 40.8
UD-Q2_K_XL 32K refill only 41.2 41.2 41.2
UD-Q2_K_XL 32K this PR 41.5 41.3 41.4

On main 1a41ff6 (4 Latin-square rounds), the same two changes gave:

  • after 2K: IQ4_NL 38.0 -> 38.5 tok/s, UD-Q2 47.7 -> 47.6;
  • after 32K, first 64 tokens: -5.4% on both files.

With 96 uploads instead of 128 (first 64 tokens after 32K, ms/token, main / 96 / 128): IQ4_NL 38.7 / 37.6 / 36.9, UD-Q2 29.8 / 28.5 / 28.1. 128 is better than 96 in every case.

Accuracy: the earlier top-1 run on main 1a41ff6 (97.4%)

Same method, main 1a41ff6 against the two changes before the review fixes, with main at other VRAM reserves as a placement floor. Logs: ~/inkling-work/h2h/ct-acc1/, ~/inkling-work/h2h/ctf-f1/.

Engine same top-1 KL mean
main fed its own answers again 99.5-99.8% 0.0006-0.0008
main frozen (no swaps) 98.6% 0.033
main at --vram-reserve-mib 1280 98.4% 0.033
main at --vram-reserve-mib 768 97.9% 0.051
this PR 97.4% 0.054

Almost all of the difference was at the 2,048-token prompt, where every placement change (main's own included) showed a ~7 nat KL spike. A case has 64 steps, so a few steps either way between runs is noise: the same pair scores 97.4% here and 98.1% in the same-base run above.

Failed:

  • none observed

Not run:

  • The main speed and accuracy runs on an RTX 2070 Super: they are from an RTX 2070, also on PCIe 3.0 x16. Only the worst-case runs above are from the 2070 Super.
  • Narrower PCIe (x8, x4, eGPU): the first token after a prompt waits for every upload it asked for (settle_swaps()). That is 128 IQ4_NL experts (~0.6 GB), about 50 ms at PCIe 3.0 x16 and longer on a narrower link.
  • Metal: it keeps 64 uploads, so only the half-life applies there, and that was not run.
  • 12-16 GB cards and HIP.
  • Prompts other than the e2e set's raw-text token ids for the speed runs.

Limits

  • The half-life part measures +0.5% at most here, against +1.6-3.1% in the simulator. The simulator used varied code, chat and prose prompts; these speed runs use repeated raw-text prompts.
  • Top-1 against main's greedy path has 64 steps a case, so it moves by a few steps between runs: the same pair scored 98.1% on main 36e654b and 97.4% on 1a41ff6. Main at another VRAM reserve scored 97.9-98.4% in the older run. Perplexity (16,384 tokens) shows no loss.
  • The two parts can be merged separately. The refill gives almost all of the speed (97.8% top-1); the half-life adds about +0.5% after 32K (98.1% with both).

🤖 Generated with Claude Code

okada-shogo and others added 2 commits November 24, 2026 09:34
…32 tokens

follow() halves an expert's recent uses every kSwapHalf (128) tokens. A family hook, swap_half(), gives Inkling 2
32; every other family keeps kSwapHalf, so their paths do not change. STRATA_SWAP_HALF=N (1 to 65536) sets
Inkling 2's, 128 being main's; a set value is logged at start and an invalid one is refused with a line. The 2x+2
rule for a trade is unchanged. The setting is in the settings register (opt-in) and docs/SETTINGS.md.

Offline replay of routing traces (STRATA_ROUTE_TRACE, 18 answers: code, chat and prose after 2K-32K
prompts, 512 tokens each, RTX 2070 8 GB), with a replica of follow() that matches the engine's hit counter
within 0.2 points: IQ4_NL experts found in VRAM 80.7% -> 82.3% at 1.95 swaps a token (main 0.63); UD-Q2_K_XL
96.8% -> 97.2%.

As before, the half-life counts evaluated rows (follow(written)), so with a drafter or batch slots a step of
several rows decays several tokens' worth.

Class: placement (which experts are in VRAM; routing unchanged). Same bits under STRATA_FREEZE_EXPERTS by
construction: follow() returns before the decay and asks no trade. The decayed-count idea is merbanan's
(Strata inkling2-turing, 0.95 a token).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…oken

After a read, the cache has the slots the read's buffers gave back, empty, and follow() fills them first, as many
a token as uploads are free (Options::expert_swaps, 64 by default). A family hook, default_swaps(), gives Inkling 2
128 when --expert-swaps is not given (64 on Metal, whose arena holds back the bytes of every upload at a time);
every other family keeps 64. --expert-swaps N still sets it, now up to 256 (was 64).

Trades stay at most kSwapTrades (64) a token whatever the number of uploads, as before: each takes one of the
mirror's spares (build_mirror(), sized by the same constant) for the expert that goes out. So more uploads only
fill a read's empty slots sooner. The next token waits for every upload asked (settle_swaps()): 128 experts of
IQ4_NL are ~0.6 GB, ~50 ms over PCIe 3.0 x16, which only the tokens right after a read pay.

Offline replay of routing traces (STRATA_ROUTE_TRACE, RTX 2070 8 GB; the engine's own cache at the first
answer token): after 32K tokens, IQ4_NL found 40% -> 50% of its experts in VRAM over the first 16 tokens
(about 67 ms less CPU-expert time over the first 64); UD-Q2_K_XL 47% -> 69% (about 91 ms). At 64 a token the
refill used about half the bus. Measured on an RTX 2070: the first 64 tokens after a 32K prompt 5% faster
(15% over the first 16), on IQ4_NL and UD-Q2_K_XL.

Class: placement. Same bits under STRATA_FREEZE_EXPERTS by construction: grow_cache() and follow() move
nothing there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Thanks for the pull request. To keep the tracker readable, every item needs an area prefix in the title and the parts of the pull request template filled in. This one is missing:

  • Fill in "Validation": the exact commands, the revision and environment tested, what you saw. Keep Passed, Failed and Not run apart.

Edit the pull request (title and description) and this note goes away on its own. If it is still missing parts in 5 days it will be closed, which is not a rejection: fix it and comment /reopen.

Details: CONTRIBUTING.md

@github-actions github-actions Bot added the needs-format Title or description does not follow CONTRIBUTING.md yet label Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: the expert cache follows an answer faster, and refills faster after a long prompt [Engine] Inkling 2: faster expert-cache refill after long prompts, shorter usage memory Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: faster expert-cache refill after long prompts, shorter usage memory [Engine] Inkling 2: refill the expert cache faster after long prompts and shorten its usage half-life (first tokens after 32K up to 16% faster) Nov 24, 2026
@github-actions github-actions Bot removed the needs-format Title or description does not follow CONTRIBUTING.md yet label Nov 24, 2026
@github-actions

Copy link
Copy Markdown

Thanks, this follows the format now.

@okada-shogo okada-shogo changed the title [Engine] Inkling 2: refill the expert cache faster after long prompts and shorten its usage half-life (first tokens after 32K up to 16% faster) [Engine] Inkling 2: faster expert-cache refill after long prompts (first tokens up to 16% faster) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: faster expert-cache refill after long prompts (first tokens up to 16% faster) [Engine] Inkling 2: faster expert-cache refill after long prompts Nov 24, 2026
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