Repository navigation
[Engine] Inkling 2: faster expert-cache refill after long prompts - #299
Open
okada-shogo wants to merge 2 commits into
Open
okada-shogo wants to merge 2 commits into
okada-shogo wants to merge 2 commits into
Conversation
…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>
|
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:
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 Details: CONTRIBUTING.md |
|
Thanks, this follows the format now. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
This PR makes two Inkling-only changes, one commit each. Both are on by default and were measured together:
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:Decode over the 512 tokens, RTX 2070 8 GB, tok/s (higher is better; mean of 3 runs):
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=Noverrides it.Engine::default_swaps()gives the value of--expert-swapswhen it is not set: 64 by default, 128 for Inkling 2.--expert-swapsnow accepts up to 256 (it was 64).kSwapTrades), as before, because each trade needs a spare slot in the RAM mirror andbuild_mirror()keeps about 64. More uploads only fill the empty slots left after a prompt sooner.STRATA_FREEZE_EXPERTS,follow()andgrow_cache()move nothing.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.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)
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.
--serve,--kv fp16,--prompt-int8, plusSTRATA_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.~/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/.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/.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 768on every side). Warm-up request first, 512 tokens, 3 Latin-square rounds, decode tok/s. Log:~/inkling-work/e2e/ct-mr3-draft/.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/.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:
On main 1a41ff6 (4 Latin-square rounds), the same two changes gave:
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/.--vram-reserve-mib1280--vram-reserve-mib768Almost 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:
Not run:
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.Limits
🤖 Generated with Claude Code