Skip to content

[Engine] Inkling 2: opt-in prefetch of the next layer's experts - #271

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

okada-shogo wants to merge 2 commits into
simplexai-labs:mainfrom
okada-shogo:port/prestage-mirror

Conversation

@okada-shogo

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

Copy link
Copy Markdown
Contributor

Problem and result

With STRATA_INK_PRESTAGE=1, an Inkling 2 prompt that runs in several calls now copies the next layer's missing
experts 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). It
copies 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 with STRATA_INK_PRESTAGE=1. Mean of 3 interleaved runs:

Card Model Measure main (tok/s) this PR alone (tok/s) Change
RTX 2070 Super 8 GB IQ4_NL prompt 302 207.9 207.1 −0.4%
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 302 238.1 238.2 +0.1%
RTX 2070 Super 8 GB IQ4_NL prompt 2K 480.3 540.7 +12.6%
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 2K 595.2 587.4 −1.3%
RTX 2070 Super 8 GB IQ4_NL prompt 8K 592.6 596.6 +0.7%
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 8K 600.0 597.7 −0.4%
RTX 2070 Super 8 GB IQ4_NL prompt 32K 592.6 581.4 −1.9%
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 32K 568.4 568.5 0%

Decode tok/s, higher is better (256 tokens written after the prompt, same runs). Decode does not run the changed code.

Card Model Measure main (tok/s) this PR alone (tok/s) Change
RTX 2070 Super 8 GB IQ4_NL decode after 302 41.9 41.8 −0.4%
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 302 44.6 44.5 −0.2%
RTX 2070 Super 8 GB IQ4_NL decode after 8K 34.1 34.1 −0.1%
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 8K 39.4 40.1 +1.6%

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:

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL prompt 302 main 208.5 208.3 206.9
RTX 2070 Super 8 GB IQ4_NL prompt 302 this PR alone 206.7 207.2 207.3
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 302 main 238.8 238.3 237.2
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 302 this PR alone 238.7 239.0 237.0
RTX 2070 Super 8 GB IQ4_NL prompt 2K main 480.9 480.2 479.7
RTX 2070 Super 8 GB IQ4_NL prompt 2K this PR alone 540.5 541.3 540.4
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 2K main 595.2 595.6 594.7
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 2K this PR alone 588.1 587.5 586.7
RTX 2070 Super 8 GB IQ4_NL prompt 8K main 592.8 592.7 592.3
RTX 2070 Super 8 GB IQ4_NL prompt 8K this PR alone 597.2 596.3 596.4
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 8K main 600.8 601.0 598.2
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 8K this PR alone 598.2 598.5 596.5
RTX 2070 Super 8 GB IQ4_NL prompt 32K main 591.5 592.9 593.5
RTX 2070 Super 8 GB IQ4_NL prompt 32K this PR alone 581.2 580.9 582.1
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 32K main 567.8 569.7 567.6
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 32K this PR alone 568.4 568.5 568.5

Decode tok/s:

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL decode after 302 main 41.9 41.9 42.0
RTX 2070 Super 8 GB IQ4_NL decode after 302 this PR alone 42.2 42.0 41.1
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 302 main 44.8 44.7 44.3
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 302 this PR alone 44.3 44.4 44.8
RTX 2070 Super 8 GB IQ4_NL decode after 8K main 34.1 34.1 34.1
RTX 2070 Super 8 GB IQ4_NL decode after 8K this PR alone 34.1 34.0 34.1
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 8K main 39.4 39.4 39.5
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 8K this PR alone 40.2 40.1 39.9

Decode after 32K, from the same 32K runs (tok/s; not one of the measures asked for, shown for completeness):

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL decode after 32K main 28.7 28.7 28.6
RTX 2070 Super 8 GB IQ4_NL decode after 32K this PR alone 28.5 28.6 28.5
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 32K main 33.4 33.5 33.6
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 32K this PR alone 32.2 32.3 32.5

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(): with STRATA_INK_PRESTAGE=1 and the pinned mirror, the prompt maps pad(one) + one bytes of the staging buffer instead of one (one = one layer's experts). The cache gives up the difference through the same shrink_cache() path, and narrow_stage() gives it back with grow_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)), after ev_free_ (the last call that read that part). The next layer run's calls wait for that copy's event and read at stage_ + stage_at_. If the next run's uncached experts are not the ones that were copied, it copies them as main does.
  • Prestage only happens when every uncached expert of both layers fits in one round, so no later round writes to the buffer in the meantime. Any other stage() waits for a pending prestage copy and drops it.
  • Change class: same bits (same experts, same layout inside a part, same launches). Placement during the prompt changes (a smaller cache); the cache after the prompt is the same.
  • Inkling 2 on CUDA only, opt-in. Other model families, HIP and Metal never set prestage_.
  • Load prints one line when STRATA_INK_PRESTAGE=1 cannot 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:

  • Rebased head 6a57938 on main 36e654b: identity on an RTX 2070 8 GB (sm_75), same command as below (--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 vs STRATA_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/.
  • Rebased head: CUDA 12.9 sm_75 and HIP (ROCm, gfx1201;gfx1100) strata-poly build; python tools/settings_register.py --check and tools/test_settings_register.py: OK.
  • Identity, main e89d56b vs this PR with STRATA_INK_PRESTAGE=1 vs 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 3072 rows 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/.
  • End-to-end A/B above: 49 of 49 runs rc=0. Prestage engaged in every opt-in run of 2K, 8K and 32K (47 of 48 layer runs, 95 of 96 at IQ4_NL 32K).
  • nsys, IQ4_NL 2K, one run each (CLI, 32K context): the time the GPU sat idle while experts were copied fell from 1,048 ms (main) to 574 ms. Host-to-device copies rose from 22.2 to 24.3 GB, because of the smaller cache. Kernel time 3,013 vs 3,017 ms. Log ~/inkling-work/fill-trace/ps2-trace3/.
  • Builds: CUDA 12.9 -DCMAKE_CUDA_ARCHITECTURES="75;86" (strata-poly). HIP (ROCm container, gfx1201;gfx1100) strata-poly compiles.
  • python tools/settings_register.py --check and python -m unittest tools.test_settings_register: OK.
Identity: every case (token ids and every top-20 line, byte for byte)

SAME = identical.

Card Model Prompt (tokens + written) VRAM setting Mode ids + top-20: main vs STRATA_INK_PRESTAGE=1 ids + top-20: main vs this PR, default Layer runs prestaged (opt-in run)
RTX 2070 8 GB IQ4_NL 302 + 64 --max-context 16384 graphs SAME SAME — (single call)
RTX 2070 8 GB IQ4_NL 302 + 64 --max-context 16384 --no-graphs SAME SAME — (single call)
RTX 2070 8 GB IQ4_NL 2,048 + 64 --max-context 16384 graphs SAME SAME 0 of 48
RTX 2070 8 GB IQ4_NL 2,048 + 64 --max-context 16384 --no-graphs SAME SAME 0 of 48
RTX 2070 8 GB IQ4_NL 8,192 + 64 --max-context 9216 --vram-reserve-mib 1536 graphs SAME SAME 0 of 96
RTX 2070 8 GB IQ4_NL 8,192 + 64 --max-context 9216 --vram-reserve-mib 1536 --no-graphs SAME SAME 0 of 96
RTX 2070 8 GB IQ4_NL 2,048 + 64 --max-context 9216 --vram-reserve-mib 3072 graphs SAME SAME 47 of 48
RTX 2070 8 GB IQ4_NL 2,048 + 64 --max-context 9216 --vram-reserve-mib 3072 --no-graphs SAME SAME 47 of 48
RTX 2070 8 GB IQ4_NL 8,192 + 64 --max-context 9216 --vram-reserve-mib 3072 graphs SAME SAME 47 of 48
RTX 2070 8 GB IQ4_NL 8,192 + 64 --max-context 9216 --vram-reserve-mib 3072 --no-graphs SAME SAME 47 of 48
RTX 2070 8 GB UD-Q2_K_XL 302 + 64 --max-context 16384 graphs SAME SAME — (single call)
RTX 2070 8 GB UD-Q2_K_XL 302 + 64 --max-context 16384 --no-graphs SAME SAME — (single call)
RTX 2070 8 GB UD-Q2_K_XL 2,048 + 64 --max-context 16384 graphs SAME SAME 0 of 48
RTX 2070 8 GB UD-Q2_K_XL 2,048 + 64 --max-context 16384 --no-graphs SAME SAME 0 of 48
RTX 2070 8 GB UD-Q2_K_XL 8,192 + 64 --max-context 9216 --vram-reserve-mib 1536 graphs SAME SAME 47 of 48
RTX 2070 8 GB UD-Q2_K_XL 8,192 + 64 --max-context 9216 --vram-reserve-mib 1536 --no-graphs SAME SAME 47 of 48
RTX 2070 8 GB UD-Q2_K_XL 2,048 + 64 --max-context 9216 --vram-reserve-mib 3072 graphs SAME SAME 47 of 48
RTX 2070 8 GB UD-Q2_K_XL 2,048 + 64 --max-context 9216 --vram-reserve-mib 3072 --no-graphs SAME SAME 47 of 48
RTX 2070 8 GB UD-Q2_K_XL 8,192 + 64 --max-context 9216 --vram-reserve-mib 3072 graphs SAME SAME 47 of 48
RTX 2070 8 GB UD-Q2_K_XL 8,192 + 64 --max-context 9216 --vram-reserve-mib 3072 --no-graphs SAME SAME 47 of 48

Failed:

  • none observed.

Not run:

  • Windows (there is no pinned mirror there, so prestage stays off), HIP and Metal: the switch is off on these.
  • Cards other than Turing 8 GB. A card with more VRAM keeps more experts per layer and loses relatively less to the second part; this was not measured.

Limits

  • The second part is one layer's size (about 1 GB on IQ4_NL, sized by the largest layer). On UD-Q2_K_XL at 2K and on IQ4_NL at 32K the experts the cache gives up for it cost more than the copies saved, so the switch stays opt-in.
  • About 0.57 s of waiting for copies is left on an IQ4_NL 2K prompt (nsys): one layer's copy still takes longer than one layer's compute.
  • It does not work with the plain pinned buffers used when there is no mirror: their CPU copy would hold up the current call's launches.

🤖 Generated with Claude Code

…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
okada-shogo marked this pull request as ready for review November 24, 2026 01:51
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: copy the next layer's experts during the current one, opt-in (STRATA_INK_PRESTAGE=1, same bits) [Engine] Inkling 2: optionally upload the next layer's experts while the current layer runs (STRATA_INK_PRESTAGE=1) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: optionally upload the next layer's experts while the current layer runs (STRATA_INK_PRESTAGE=1) [Engine] Inkling 2: optionally upload the next layer's experts while the current layer runs (STRATA_INK_PRESTAGE=1, +12.6% on 2K IQ4_NL prompts) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: optionally upload the next layer's experts while the current layer runs (STRATA_INK_PRESTAGE=1, +12.6% on 2K IQ4_NL prompts) [Engine] Inkling 2: opt-in upload of the next layer's experts during the current one Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: opt-in upload of the next layer's experts during the current one [Engine] Inkling 2: opt-in prefetch of the next layer's experts Nov 24, 2026
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>
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