Repository navigation
[Engine] Inkling 2: opt-in prefetch of the next layer's experts - #271
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
…run, opt-in (STRATA_INK_PRESTAGE=1, same bits) In a prompt read of several calls (read_groups()), each layer run's experts that are not cached go through the staging buffer, and main copies them only when the run starts, so the card waits for that copy. With STRATA_INK_PRESTAGE=1 the read's staging buffer gets a second part as large as the first (widen_stage(); the expert cache lends that VRAM for the read and takes it back in narrow_stage(), as it does for the first part), and once a run's first call has queued its own copy, the next run's uncached experts are copied from the pinned mirror into the other part on the upload stream (prestage()). The copy waits for the last call that read that part (the run before's, ev_free_); the next run's calls wait for its event and read their experts there. If the next run's experts are not the ones copied, it copies its own as before. Only with the pinned mirror (build_mirror(), the Linux default): its copies are queued at once, where the pinned buffers' CPU copy would hold the call's launches up. The experts, their layout within a part and the launches are the same, so the results are too: identity of main, this change with STRATA_INK_PRESTAGE=1, and without it, on an RTX 2070 8 GB (302 / 2,048 / 8,192 tokens, IQ4_NL and UD-Q2_K_XL, graphs and --no-graphs, experts frozen), 40 of 40 comparisons byte-identical, prestage engaged in 12 of the 20 opt-in runs. RTX 2070 Super 8 GB, mean of 3 runs: IQ4_NL 2,048-token prompt +12.6% (480.3 -> 540.7 tok/s), UD-Q2_K_XL 2,048 -1.3%, IQ4_NL 32,768 -1.9%; decode unchanged. Off by default. Inkling 2 on CUDA only; HIP compiles the code, never takes it. From our reference PR simplexai-labs#200 (commit 802b20d, "Inkling 2 Mini's prompt reads do not wait for the copies to the card"), whose row-table half is simplexai-labs#265; adapted to main's read_groups() and the pinned mirror. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo
force-pushed
the
port/prestage-mirror
branch
from
November 24, 2026 01:50
36166a1 to
6a57938
Compare
okada-shogo
marked this pull request as ready for review
November 24, 2026 01:51
Prestage runs in read_groups() on CUDA. On HIP and Metal it is not built, and with --draft-model Inkling 2's prompt reads go call by call (carry_rows() is 0), so the switch did nothing without a word. Load now prints a line in both cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
With
STRATA_INK_PRESTAGE=1, an Inkling 2 prompt that runs in several calls now copies the next layer's missingexperts to the card while the card computes the current layer. On an RTX 2070 Super 8 GB, a 2,048-token IQ4_NL prompt
is processed 12.6% faster, with the same bits as main. It is off by default: the staging buffer then takes about 1 GB
more from the expert cache during the prompt, and that costs more than it saves on UD-Q2_K_XL at 2K (−1.3%) and on
IQ4_NL at 32K (−1.9%). This is a follow-up to #265: the other half of #200's commit 802b20d.
Background: on an 8 GB card, processing a prompt copies about 22 GB of experts to the card. #265 stopped the row tables
from waiting behind those copies, but the card still waited about 1.0 s of a 2K IQ4_NL prompt for each layer's own
copy, which main starts only when the layer's first call starts. #200's prestage would copy the next layer's experts
earlier, but on main it never engaged on 8 GB. Linux stages the copies from the pinned RAM copy of the experts (the
pinned mirror), which always wrote to the start of the staging buffer, and the prompt's staging buffer holds one
layer, so two layers never fit.
This PR gives the prompt's staging buffer a second part as large as the first (only with
STRATA_INK_PRESTAGE=1). Itcopies the next layer's experts that are not in the cache from the pinned mirror into the free part while the current
layer is processed.
The branch head is 6a57938, rebased onto main 36e654b. The patch did not change in the rebase (
git range-diff),so the speed and nsys numbers below are from the pair before it: main e89d56b vs 36166a1. Identity was run again at
the new head (see Validation).
Prompt tok/s, higher is better. RTX 2070 Super 8 GB (PCIe 3.0 x16), Ryzen 5 3600XT, fp16 KV,
--serve, one request per run, no expert profile, experts in VRAM as placed by default. main = e89d56b, this PR = 36166a1 withSTRATA_INK_PRESTAGE=1. Mean of 3 interleaved runs:Decode tok/s, higher is better (256 tokens written after the prompt, same runs). Decode does not run the changed code.
The expert cache shrinks during the prompt and grows back after it to the same size in both builds (IQ4_NL 2K: 808 → 1,155 experts on main, 599 → 1,155 with this PR; UD-Q2_K_XL 2K: 1,849 → 2,269 and 1,696 → 2,269).
Each run (tok/s)
main e89d56b (md5 8985edc0) vs this PR 36166a1 (md5 c1146903) with
STRATA_INK_PRESTAGE=1, both CUDA 12.9 sm_75;86. Order A B in rounds 1 and 3, B A in round 2. Experts at load: 927 (IQ4_NL) and 1,986 (UD-Q2_K_XL) in every run. 49 of 49 runs rc=0. Log~/inkling-work/e2e/ps2-e2e/progress.log.Prompt tok/s:
Decode tok/s:
Decode after 32K, from the same 32K runs (tok/s; not one of the measures asked for, shown for completeness):
Related discussion
Split out of #200 as asked (one optimization per PR). From #200's 802b20d ("Inkling 2 Mini's prompt reads do not wait for the copies to the card"). Its row-table half is #265 (merged as f0ab63a); #265's Limits named this follow-up.
Approach
widen_stage(): withSTRATA_INK_PRESTAGE=1and the pinned mirror, the prompt mapspad(one) + onebytes of the staging buffer instead ofone(one= one layer's experts). The cache gives up the difference through the sameshrink_cache()path, andnarrow_stage()gives it back withgrow_cache(). If the second part cannot be mapped, the prompt uses one part, as main does.moe_rows(): a "layer run" is the group of calls that process one layer of the prompt. Once a layer run's first call has queued its own copy,prestage()plans the next layer run (plan_ahead()) and queues its uncached experts on the upload stream into the other part (stage(..., to)), afterev_free_(the last call that read that part). The next layer run's calls wait for that copy's event and read atstage_ + stage_at_. If the next run's uncached experts are not the ones that were copied, it copies them as main does.stage()waits for a pending prestage copy and drops it.prestage_.STRATA_INK_PRESTAGE=1cannot engage: on HIP and Metal builds ("is CUDA only: off"), and on CUDA with--draft-model, whose prompt reads are not grouped ("does nothing with --draft-model: off").Validation
Passed:
--vram-reserve-mib 3072 --max-context 9216, graphs, frozen experts), IQ4_NL and UD-Q2_K_XL at 302, 2,048 and 8,192 tokens + 64. Token ids and all 64 top-20 lines were byte for byte the same in all 12 comparisons: main vs this PR default (6 SAME), this PR default vsSTRATA_INK_PRESTAGE=1(6 SAME). Prestage engaged in every 2K and 8K run (47 of 48 layer runs). Log:~/inkling-work/h2h/psid-ps3-id/.strata-polybuild;python tools/settings_register.py --checkandtools/test_settings_register.py: OK.STRATA_INK_PRESTAGE=1vs this PR default, on an RTX 2070 8 GB (sm_75).strata-poly --tokens @idsN --max-new 64 --top-probs 20 --kv fp16 --expert-swaps 0,STRATA_FREEZE_EXPERTS=1 STRATA_POLY_AHEAD=0, with and without--no-graphs. Token ids and all 64 top-20 lines were byte for byte the same in all 40 comparisons. A frozen run cannot shrink the cache, so the second part is mapped only where the card has room for it. The--vram-reserve-mib 3072rows were added so that prestage runs in every case (47 of 48 layer runs). Every case is in the table below. Logs:~/inkling-work/h2h/psid-ps2-id-75/,psid-ps2-id8k-75/,psid-ps2-idroom-75/.~/inkling-work/fill-trace/ps2-trace3/.-DCMAKE_CUDA_ARCHITECTURES="75;86"(strata-poly). HIP (ROCm container, gfx1201;gfx1100) strata-poly compiles.python tools/settings_register.py --checkandpython -m unittest tools.test_settings_register: OK.Identity: every case (token ids and every top-20 line, byte for byte)
SAME = identical.
STRATA_INK_PRESTAGE=1--max-context 16384--max-context 16384--no-graphs--max-context 16384--max-context 16384--no-graphs--max-context 9216 --vram-reserve-mib 1536--max-context 9216 --vram-reserve-mib 1536--no-graphs--max-context 9216 --vram-reserve-mib 3072--max-context 9216 --vram-reserve-mib 3072--no-graphs--max-context 9216 --vram-reserve-mib 3072--max-context 9216 --vram-reserve-mib 3072--no-graphs--max-context 16384--max-context 16384--no-graphs--max-context 16384--max-context 16384--no-graphs--max-context 9216 --vram-reserve-mib 1536--max-context 9216 --vram-reserve-mib 1536--no-graphs--max-context 9216 --vram-reserve-mib 3072--max-context 9216 --vram-reserve-mib 3072--no-graphs--max-context 9216 --vram-reserve-mib 3072--max-context 9216 --vram-reserve-mib 3072--no-graphsFailed:
Not run:
Limits
🤖 Generated with Claude Code