Skip to content

[Engine] Inkling 2: keep all experts in pinned RAM when it fits - #290

Open
okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/expert-mirror
Open

okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/expert-mirror

Conversation

@okada-shogo

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

Copy link
Copy Markdown
Contributor

Problem and result

While processing a prompt, the engine gives up hundreds of the experts in VRAM to make room for the prompt's intermediate rows and its wider staging buffer. The next request loads them back. The pinned RAM copy of the experts (the mirror) held only the experts outside VRAM plus 65 spare slots, so most experts the cache gave up had no pinned copy. As a result:

  • they went back to the card from the memory-mapped model file through the two pinned 64 MB buffers;
  • each use of those buffers waited behind the experts already queued on the single upload copy engine;
  • the engine thread waited in cudaEventSynchronize while the card sat idle.

With this PR, Inkling 2's mirror also holds the experts that are in VRAM, from model load on, when enough RAM is free: at least max(8 GiB, 20% of RAM) must stay free after the copy. An expert that comes onto the card keeps its copy, so when the cache gives it up, it needs no copy back and no trip through the pinned buffers. On this RTX 2070, IQ4_NL takes 15.0 GiB of pinned RAM instead of 10.6, and UD-Q2_K_XL 7.5 instead of 2.8. The startup log says which mirror was made:

  • mirror: all experts (15.0 GiB pinned; 25.9 of 31.3 GiB of RAM free before it), or
  • mirror: the experts outside VRAM only, RAM short ….

--no-full-mirror or STRATA_POLY_MIRROR=partial keeps the old mirror. The output is bit-identical to main. Other models are unchanged.

The second prompt in an engine is 3.0-10.4% faster at 302 and 2K tokens, and 0.2-2.2% faster at 8K and 32K.

Prompt tok/s, higher is better. Second request in the same engine (the case of every chat turn after the first).

  • Setup: RTX 2070 8 GB (not Super), Ryzen 7 5800X, 31 GB RAM, Linux. main = 6d59254; this PR alone = ee9f73d82 on 1a41ff6, with no engine change between the two for a single card. The PR is that commit rebased onto main 36e654b (eaa8df8, no conflict); see Validation.
  • Both builds: --prompt-int8, STRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1, fp16 KV, --serve. Also
    STRATA_INK_PRESTAGE=1, which main ignores.
  • Each run: a warm-up request on another 2,048-token prompt, an 18 s pause, then the measured request.
  • Rounds alternate the order of the two builds. Mean of 3 runs.
Card Model Measure main (tok/s) this PR alone (tok/s) Change
RTX 2070 8 GB IQ4_NL prompt 302 287.5 317.4 +10.4%
RTX 2070 8 GB IQ4_NL prompt 2K 627.6 651.3 +3.8%
RTX 2070 8 GB IQ4_NL prompt 8K 906.6 926.6 +2.2%
RTX 2070 8 GB IQ4_NL prompt 32K 911.3 916.0 +0.5%
RTX 2070 8 GB UD-Q2_K_XL prompt 302 375.1 407.4 +8.6%
RTX 2070 8 GB UD-Q2_K_XL prompt 2K 841.5 866.9 +3.0%
RTX 2070 8 GB UD-Q2_K_XL prompt 8K 876.2 885.8 +1.1%
RTX 2070 8 GB UD-Q2_K_XL prompt 32K 808.8 810.4 +0.2%

Decode (256 tokens written after the prompt): +0.1% to +1.3%.

A chat-like run: one engine, five requests in a row with 8 s between them (2,048, 302, 8,192, 553 and 2,048 tokens, 128 written each), same builds and flags, 3 rounds. From the second request on, prompts are processed +0.8% to +6.9% faster (table below). Decode stays within ±1.2%.

Chat-like run, prompt tok/s (mean of 3 runs; T1 is the engine's first request):

Card Model Turn main (tok/s) this PR alone (tok/s) Change
RTX 2070 8 GB IQ4_NL T1, 2,048 tokens 608.3 596.0 −2.0%
RTX 2070 8 GB IQ4_NL T2, 302 321.1 343.1 +6.9%
RTX 2070 8 GB IQ4_NL T3, 8,192 906.3 932.1 +2.8%
RTX 2070 8 GB IQ4_NL T4, 553 389.7 394.7 +1.3%
RTX 2070 8 GB IQ4_NL T5, 2,048 653.5 679.9 +4.1%
RTX 2070 8 GB UD-Q2_K_XL T1, 2,048 tokens 839.9 869.5 +3.5%
RTX 2070 8 GB UD-Q2_K_XL T2, 302 391.1 411.8 +5.3%
RTX 2070 8 GB UD-Q2_K_XL T3, 8,192 876.2 883.0 +0.8%
RTX 2070 8 GB UD-Q2_K_XL T4, 553 487.3 508.7 +4.4%
RTX 2070 8 GB UD-Q2_K_XL T5, 2,048 854.0 865.2 +1.3%

With #271's STRATA_INK_PRESTAGE=1 on the stack of our open Inkling PRs, the same change was worth more on an RTX
2070 Super, because prestaging makes the wait longer: warm IQ4_NL 2K +14.2%, 8K +9.3%.

Related discussion

An nsys comparison with #200 on the RTX 2070 Super showed gaps of 27-81 ms at the start of layer runs. Each gap lined up with the engine thread blocked in stage() / stage_rest() on a pinned buffer's event, with 370-980 MB of copies ahead of it. A second request's prompt ran ~8-10% slower than the first. The experts it lacked were the ones the first prompt had evicted, and they had no mirror copy.

Approach

  • build_mirror() first tries to copy every expert. It does so only when Engine::full_mirror_default() is true (Inkling 2 only), neither --no-full-mirror nor STRATA_POLY_MIRROR=partial is set, and the RAM rule allows it. Otherwise it builds the old mirror.
    • RAM rule: ram_available() (MemAvailable, or what the cgroup leaves) minus the copy must stay at least
      max(8 GiB, 20% of ram_total()).
    • ram_total() is MemTotal, or the tightest cgroup v2 memory.max on the way to the root. Under WSL2 MemTotal is
      the VM's, so the rule uses the RAM the engine really has.
    • If the pinned memory for the whole copy cannot be allocated, it falls back once to the old mirror.
  • Experts in VRAM are copied from the file exactly as the others are (same layout as a card slot).
  • refills_mirror() is also true with the full mirror. An expert that arrives on the card (a refill, or a trade that swaps it in for a less-used one) keeps its copy, and one that leaves finds its copy (mirror_keep() returns at once).
  • Windows: unchanged. A mirror is made only with STRATA_POLY_MIRROR=1 or --lock-experts. There it is full under
    the same rule, and --no-full-mirror keeps it partial.
  • Docs: models/inkling.md and BACKENDS.md. The register note for STRATA_POLY_MIRROR is updated.

Validation

Passed:

  • Same bits, capped and frozen: STRATA_GPU_EXPERTS=600 STRATA_FREEZE_EXPERTS=1, the flags above, prompts of 302,
    2,048 and 8,192 tokens, 64 greedy tokens, --top-probs 20, IQ4_NL and UD-Q2_K_XL. The full mirror's ids and top-20
    lines equal the partial mirror's (STRATA_POLY_MIRROR=partial, same binary) in 18 of 18 comparisons. The start
    lines show "mirror: all experts" in the full runs. RTX 2070 8 GB.
  • Speed: the tables above and below.
    • Second-request runs: NAME=em-warm WARM=…/idsb2048.txt WARM_GAP=18 SIDES="M P" … bash e2e-nw.sh, 49 of 49 runs rc=0.
    • Chat-like runs: NAME=em-chat SIDES="M P" … bash chat-ub.sh, 12 of 12 runs rc=0.
  • Builds of eaa8df8 (on main 36e654b): CUDA sm_75 (CUDA 12.9) and HIP (gfx1201;gfx1100, ROCm 7.2.4), both rc=0;
    poly_stream_stage_test PASS.
  • After the rebase: frozen identity full vs partial, 2,048 tokens, both files: ids and top-20
    equal in 6 of 6 comparisons (RTX 2070 Super 8 GB, 62.7 GiB RAM: "mirror: all experts").
  • tools/settings_register.py --check and tools/test_settings_register.py: pass.
  • An independent review found no output-changing issue. Its points are fixed in this commit: a trade kept giving the incoming copy back; the Windows wording; the cgroup walk; one message.
Each run (tok/s): second request and chat-like run

Second request in the engine, prompt:

Model Measure Build Run 1 Run 2 Run 3
IQ4_NL prompt 302 main 272.0 296.8 293.8
IQ4_NL prompt 302 this PR 317.3 316.7 318.2
IQ4_NL prompt 2K main 626.9 627.9 628.0
IQ4_NL prompt 2K this PR 650.3 654.2 649.5
IQ4_NL prompt 8K main 902.3 911.8 905.6
IQ4_NL prompt 8K this PR 928.8 923.4 927.6
IQ4_NL prompt 32K main 911.3 910.9 911.6
IQ4_NL prompt 32K this PR 916.4 915.1 916.5
UD-Q2_K_XL prompt 302 main 387.8 355.9 381.6
UD-Q2_K_XL prompt 302 this PR 398.8 416.3 407.2
UD-Q2_K_XL prompt 2K main 835.4 844.4 844.6
UD-Q2_K_XL prompt 2K this PR 871.3 864.4 865.0
UD-Q2_K_XL prompt 8K main 875.5 878.1 875.1
UD-Q2_K_XL prompt 8K this PR 884.8 886.4 886.1
UD-Q2_K_XL prompt 32K main 808.7 808.7 809.1
UD-Q2_K_XL prompt 32K this PR 810.8 810.6 809.9

Decode:

Model Measure Build Run 1 Run 2 Run 3
IQ4_NL decode after 302 main 42.4 40.8 42.1
IQ4_NL decode after 302 this PR 42.1 42.1 42.7
IQ4_NL decode after 8K main 34.9 34.9 34.8
IQ4_NL decode after 8K this PR 35.3 35.3 35.0
UD-Q2_K_XL decode after 302 main 49.2 49.2 49.3
UD-Q2_K_XL decode after 302 this PR 49.3 49.3 49.4
UD-Q2_K_XL decode after 8K main 45.3 45.3 45.3
UD-Q2_K_XL decode after 8K this PR 45.4 45.3 45.3

Each chat run, prompt tok/s of turns T1-T5:

Model Build Round T1 T2 T3 T4 T5
IQ4_NL main 1 578.7 314.9 893.4 386.1 651.3
IQ4_NL this PR 1 571.3 344.8 931.6 371.8 677.9
IQ4_NL this PR 2 599.3 340.3 932.2 409.7 679.7
IQ4_NL main 2 622.1 325.0 917.6 392.4 654.5
IQ4_NL main 3 624.0 323.4 908.0 390.6 654.6
IQ4_NL this PR 3 617.4 344.2 932.6 402.5 682.2
UD-Q2_K_XL main 1 821.0 391.9 876.2 486.3 852.5
UD-Q2_K_XL this PR 1 870.0 410.3 882.4 507.5 865.2
UD-Q2_K_XL this PR 2 862.9 414.3 882.6 508.4 867.2
UD-Q2_K_XL main 2 876.6 392.9 877.4 485.2 854.3
UD-Q2_K_XL main 3 822.1 388.6 875.1 490.4 855.1
UD-Q2_K_XL this PR 3 875.6 410.7 884.0 510.1 863.2

Chat decode (128 written a turn): −1.2% to +1.3% across all turns and both files.

Failed:

  • none observed

Not run:

  • Windows with STRATA_POLY_MIRROR=1.
  • A machine where the RAM rule refuses the full mirror: the message path was read, not run.
  • Cards other than the RTX 2070 and RTX 2070 Super.

Limits

  • It costs RAM: IQ4_NL takes 4.4 GiB more pinned RAM and UD-Q2_K_XL 4.7 GiB more (RTX 2070 8 GB). It is used only where max(8 GiB, 20%) of the RAM stays free; --no-full-mirror turns it off.
  • The first request after the load gains little; the gain is in the requests after it. It is larger with
    STRATA_INK_PRESTAGE=1 ([Engine] Inkling 2: opt-in prefetch of the next layer's experts #271).
  • Same bits: see the frozen identity check above.

🤖 Generated with Claude Code

…room

A prompt read gives up hundreds of the cached experts (the carried rows, the wider staging buffer) and the next
request takes them back.  The mirror held only the experts outside VRAM and 65 spares, so most of those had no copy:
they went back to the card from the mapped file through the two pinned 64 MB buffers, whose turns wait behind the
experts already queued on the one upload copy engine, and the engine thread waited in cudaEventSynchronize with the
card idle.  For Inkling 2 (Engine::full_mirror_default()) the mirror now holds the cached experts too, from the
load on, where the RAM free to the process keeps max(8 GiB, 20% of its RAM) beside it (under WSL or in a container,
theirs: ram_total() takes the cgroup's limit).  An expert that comes onto the card keeps its copy
(refills_mirror()), so one the cache gives up needs no copy back.  The start line says which mirror was made.
--no-full-mirror or STRATA_POLY_MIRROR=partial keeps the old one.  Same bits: the same experts reach the same
launches.  Other models unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: the pinned copy of the experts holds every expert where the RAM has room (same bits) [Engine] Inkling 2: keep every expert in pinned RAM when RAM allows, so evicted experts re-upload faster Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: keep every expert in pinned RAM when RAM allows, so evicted experts re-upload faster [Engine] Inkling 2: keep every expert in pinned RAM when RAM allows, so evicted experts re-upload faster (second prompt up to +10.4%) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: keep every expert in pinned RAM when RAM allows, so evicted experts re-upload faster (second prompt up to +10.4%) [Engine] Inkling 2: keep all experts in pinned RAM when it fits (faster re-uploads) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: keep all experts in pinned RAM when it fits (faster re-uploads) [Engine] Inkling 2: keep all experts in pinned RAM when it fits 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