Repository navigation
[Engine] Inkling 2: keep all experts in pinned RAM when it fits - #290
Open
okada-shogo wants to merge 1 commit into
Open
okada-shogo wants to merge 1 commit into
okada-shogo wants to merge 1 commit into
Conversation
…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>
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
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:
cudaEventSynchronizewhile 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), ormirror: the experts outside VRAM only, RAM short ….--no-full-mirrororSTRATA_POLY_MIRROR=partialkeeps 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).
--prompt-int8,STRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1, fp16 KV,--serve. AlsoSTRATA_INK_PRESTAGE=1, which main ignores.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):
With #271's
STRATA_INK_PRESTAGE=1on the stack of our open Inkling PRs, the same change was worth more on an RTX2070 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 whenEngine::full_mirror_default()is true (Inkling 2 only), neither--no-full-mirrornorSTRATA_POLY_MIRROR=partialis set, and the RAM rule allows it. Otherwise it builds the old mirror.ram_available()(MemAvailable, or what the cgroup leaves) minus the copy must stay at leastmax(8 GiB, 20% of
ram_total()).ram_total()is MemTotal, or the tightest cgroup v2memory.maxon the way to the root. Under WSL2 MemTotal isthe VM's, so the rule uses the RAM the engine really has.
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).STRATA_POLY_MIRROR=1or--lock-experts. There it is full underthe same rule, and
--no-full-mirrorkeeps it partial.STRATA_POLY_MIRRORis updated.Validation
Passed:
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-20lines equal the partial mirror's (
STRATA_POLY_MIRROR=partial, same binary) in 18 of 18 comparisons. The startlines show "mirror: all experts" in the full runs. RTX 2070 8 GB.
NAME=em-warm WARM=…/idsb2048.txt WARM_GAP=18 SIDES="M P" … bash e2e-nw.sh, 49 of 49 runs rc=0.NAME=em-chat SIDES="M P" … bash chat-ub.sh, 12 of 12 runs rc=0.poly_stream_stage_testPASS.equal in 6 of 6 comparisons (RTX 2070 Super 8 GB, 62.7 GiB RAM: "mirror: all experts").
tools/settings_register.py --checkandtools/test_settings_register.py: pass.Each run (tok/s): second request and chat-like run
Second request in the engine, prompt:
Decode:
Each chat run, prompt tok/s of turns T1-T5:
Chat decode (128 written a turn): −1.2% to +1.3% across all turns and both files.
Failed:
Not run:
STRATA_POLY_MIRROR=1.Limits
--no-full-mirrorturns it off.STRATA_INK_PRESTAGE=1([Engine] Inkling 2: opt-in prefetch of the next layer's experts #271).🤖 Generated with Claude Code