Repository navigation
[Engine] Inkling 2: prompt scratch lent to the expert cache between prompts - #243
Draft
okada-shogo wants to merge 4 commits into
Draft
okada-shogo wants to merge 4 commits into
okada-shogo wants to merge 4 commits into
Conversation
…mpts Inkling 2 sized every per-token buffer of its scratch for the prompt batch (rows_) and kept it in VRAM while an answer was written, though a decode call uses a few rows of it: 430 MiB at the default prompt batch of 1,024 on an RTX 2070 Super. It now has the engine's two scratch layouts (scratch_layouts(), use_scratch()), as Qwen3.6 does: a call of fewer than kStageRows tokens has a small layout of its own, and a prompt batch's is part of the prompt's buffers that lend_prompt() gives the expert cache between prompts. assign_scratch() sizes by buf_rows_. Not with the DSpark drafter, whose features are strided by a whole call's rows. Placement only: the calls and their arithmetic are the same, so a capped and frozen expert cache gives the same greedy ids and --top-probs 8 lines before and after (IQ4_NL 302, 1,000 and 2,048 tokens; AD-IQ3_S 302 and 2,048; RTX 2070 Super). What changes is the VRAM the expert cache has while an answer is written. The two layouts and the lending are VecSzn's; a small resident scratch on 8 GB cards was also merbanan's idea in his Strata inkling2-turing branch (a 512-row work chunk). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On ROCm on Linux, VRAM goes back only with its addresses (vmm_unmap() frees them and reserves them again). Lending a prompt's buffers to the expert cache crashed strata-poly there: Inkling 2 Mini IQ4_NL on an RX 9070 XT with some experts in RAM, SIGSEGV inside hipLaunchKernel. Where STRATA_VMM_RELEASE_WITH_ADDRESSES is defined (HIP on Linux), Inkling2::scratch_layouts() is now false, so the prompt batch's scratch is not put into those lent buffers and stays in one layout, as before the previous commit. The override moves to inkling2.cpp, which includes <cuda.h> (on HIP, hip_compat's), where the macro is defined. CUDA and Windows HIP are unchanged. Class: placement only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The 64 MiB of attention scores (score_, sized for the context) were in each of the two scratch layouts, so the few tokens' layout cost about 70 MiB beside the prompt batch's: 15-22 fewer experts in VRAM at load on an 8 GB card. They are now one buffer apart from the scratch that both layouts share, as VecSzn's Qwen3.6 already does (qwen35.cpp, alloc_caches()). Their size is the same, so the attention's chunks and the output bits are too. The layouts are also off where nothing is ever lent (lend_prompt() returns early): with the experts frozen (STRATA_FREEZE_EXPERTS, --check-batch), and where the free VRAM at load holds every expert, the batch's scratch and 2 GiB more. There the VRAM is main's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In the few tokens' layout awork_ kept the prompt batch layout's address, which is unmapped while that layout is lent. Nothing reads it there (only calls of 32 rows or more, which take the batch layout first), but a null pointer makes any later misuse fault plainly. 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
Inkling 2 sized all of its scratch for the prompt batch (1,024 rows, about 430 MiB on an RTX 2070 Super). It kept all of it in VRAM while it wrote an answer, but a decode call uses only a few rows.
Now Inkling 2 uses the engine's two scratch layouts, as Qwen3.6 already does. Between prompts, the prompt batch's scratch goes to the expert cache. RTX 2070 Super, 8 GB, after a prompt: about 100 more AD-IQ3_S experts in VRAM and about 80 more IQ4_NL. With the expert cache frozen, the output is the same bits as main. Not frozen, the output follows the cache's contents, as with any change of placement (see Limits).
Decode +1.7% to +3.9% on an 8 GB card, from about 80 to 100 more experts in VRAM after a prompt; prompt speed unchanged.
Speed in tokens per second (tok/s), higher is better, mean of 3 interleaved runs, head d39af0b vs main d1f711e. RTX 2070 8 GB + Ryzen 7 5800X (a quiet box), fp16 KV,
32K context, default settings (no freeze, no profile), one
--serverequest per run, 256 tokens written (1 after the2K prompt). In every decode row each of this PR's three runs is faster than each of main's.
Prompt speed (tok/s, higher is better), RTX 2070 8 GB, mean of 3 runs:
Decode speed (tok/s, higher is better), RTX 2070 8 GB, mean of 3 runs:
Each run (tok/s), head d39af0b
Prompt speed (tok/s, higher is better), RTX 2070 8 GB, each run:
Decode speed (tok/s, higher is better), RTX 2070 8 GB, each run:
Related discussion
inkling2-turingbranch (a 512-row work chunk).vmm_unmap()frees and reserves the addresses again. [Engine] Inkling 2 on AMD (RDNA3/RDNA4): HIP support #217 (cfcb0b9) turnsprompt_lends_off on HIP-Linux. This PR adds nothing to the lent buffers on HIP-Linux (second commit), so it does not depend on [Engine] Inkling 2 on AMD (RDNA3/RDNA4): HIP support #217. See Limits.Approach
Inkling2::scratch_layouts()turns on the engine's two layouts.assign_scratch()now sizes the buffers bybuf_rows_instead ofrows_.lend_prompt()gives the expert cache between prompts, andtake_prompt()takes it back.qwen35.cpp). Their size is unchanged, so the attention's chunks are too.STRATA_FREEZE_EXPERTS,--check-batch), where the free VRAM at load holds every expert, and on HIP-Linux (STRATA_VMM_RELEASE_WITH_ADDRESSES). There the scratch is main's, in one layout.Validation
Passed:
e2e-ab.sh(main and this PRinterleaved, order reversed every other round, one warm-up;
strata-poly --serve --prompt-cache 0 --conversation-cache-mib 0 --kv fp16 --max-context 32768, 302 + 256, 2,048 + 1 and 8,192 + 256 tokens; after eachrequest a bare
VRAMline reports the experts held; both builds CUDA 12.975;86).(IQ4_NL 800, AD-IQ3_S 1,000 experts) and frozen, the same cache set FNV for both builds. 32 greedy tokens plus
--top-probs 8, md5 of the ids and top lines: the same in 5 of 5 cases (IQ4_NL at 302 / 1,000 / 2,048-tokenprompts, AD-IQ3_S at 302 / 2,048).
unless
STRATA_X_CAPis set, and a stderr line with the cache size after each request). RTX 2070 Super 8 GB (italso drives the desktop),
strata-poly --serve --prompt-cache 0 --conversation-cache-mib 0 --kv fp16 --max-context 32768, one 302-token prompt and 32 tokens, 2 runs each, all 16 runs rc=0.922 → 918 at load and 999 → 1,081 after the prompt. The cost at load is now 4 experts (was 15-22 before the
scores were shared).
STRATA_FREEZE_EXPERTS=1, the layouts off): IQ4_NL 922 and 922 in both runs; AD-IQ3_S 1,255 and 1,255 inrun 2, 1,255 and 1,251 in run 1. Frozen, this PR's scratch is main's code path. That one run ended with 32 MiB
more VRAM free than main's, which points at the desktop's VRAM use at load, but it is not confirmed.
--draft-model(layouts off),--window 4,--max-context 131072. All 6 answered in every run, 0 errors, at least 27 MiB of VRAM free.strata-poly,poly_draft_race_test,poly_gdn_mma_state_test, and CUDA 75;86strata-poly. HIPstrata-poly(gfx1201;gfx1100, ROCm 7.2.4) with b681c69 from [Engine] Inkling 2 on AMD (RDNA3/RDNA4): HIP support #217 cherry-picked, because main's HIP build stops atinkling2.cuwithout it.python3 tools/settings_register.py --check,tools/test_settings_register.py(8 tests),python3 tools/check --only docson d39af0b: passed. This PR adds no newSTRATA_*env var.Failed:
Not run:
compute-sanitizer --tool memcheckover two long prompts and a short answer between them, not frozen (the lend andreturn cycle): the run was invalid for the same reason (
--kv q8_0refused before the first CUDA call).parallel: 2,--batch 2), not frozen: needs [Engine] Inkling 2 Mini: several requests at once (batch slots, parallel) #226. Without it Inkling 2runs one sequence and refuses
--batch, so the engine did not start.vmm_is false, so the layouts are off), or on several cards (the layouts are off with several cards).Limits
--batch), the larger lent region is taken back and lent again at every piece. That costs time only, and it is not measured yet.--draft-model, frozen experts, every expert in VRAM,--windowor batch steps of 32 rows or more, several cards, or no VMM.vmm_unmap()frees and reserves the addresses again (a SIGSEGV insidehipLaunchKernel, RX 9070 XT, IQ4_NL). Main already lends Inkling 2's other prompt buffers (prompt_lends_does not depend onscratch_layouts()). So the crash on AMD is fixed by [Engine] Inkling 2 on AMD (RDNA3/RDNA4): HIP support #217 (cfcb0b9), not by this PR. This PR keeps the scratch out of the lent buffers on HIP-Linux, so with or without [Engine] Inkling 2 on AMD (RDNA3/RDNA4): HIP support #217 it changes nothing there.🤖 Generated with Claude Code