Skip to content

[Engine] Inkling 2: prompt scratch lent to the expert cache between prompts - #243

Draft
okada-shogo wants to merge 4 commits into
simplexai-labs:mainfrom
okada-shogo:port/scratch-layouts
Draft

okada-shogo wants to merge 4 commits into
simplexai-labs:mainfrom
okada-shogo:port/scratch-layouts

Conversation

@okada-shogo

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

Copy link
Copy Markdown

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 --serve request per run, 256 tokens written (1 after the
2K 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:

Card Model Measure main this PR alone Change Experts in VRAM at load (main → PR) Experts in VRAM after the prompt (main → PR)
RTX 2070 8 GB AD-IQ3_S prompt of 302 tokens 214.1 211.1 −1.4% 1277 → 1273 1369 → 1466
RTX 2070 8 GB IQ4_NL prompt of 302 tokens 208.0 205.8 −1.0% 937 → 935 1014 → 1098
RTX 2070 8 GB AD-IQ3_S prompt of 2K tokens 466.3 464.3 −0.4% 1277 → 1273 1172 → 1154
RTX 2070 8 GB IQ4_NL prompt of 2K tokens 410.1 416.6 +1.6% 937 → 935 819 → 811
RTX 2070 8 GB AD-IQ3_S prompt of 8K tokens 507.0 506.6 −0.1% 1277 → 1273 1263 → 1364
RTX 2070 8 GB IQ4_NL prompt of 8K tokens 516.4 515.4 −0.2% 937 → 935 1057 → 1137

Decode speed (tok/s, higher is better), RTX 2070 8 GB, mean of 3 runs:

Card Model Measure main this PR alone Change Experts in VRAM at load (main → PR) Experts in VRAM after the prompt (main → PR)
RTX 2070 8 GB AD-IQ3_S decode after a 302-token prompt 42.2 42.9 +1.7% 1277 → 1273 1369 → 1466
RTX 2070 8 GB IQ4_NL decode after a 302-token prompt 40.0 41.2 +3.0% 937 → 935 1014 → 1098
RTX 2070 8 GB AD-IQ3_S decode after an 8K-token prompt 36.3 36.9 +1.7% 1277 → 1273 1263 → 1364
RTX 2070 8 GB IQ4_NL decode after an 8K-token prompt 30.7 31.9 +3.9% 937 → 935 1057 → 1137
Each run (tok/s), head d39af0b

Prompt speed (tok/s, higher is better), RTX 2070 8 GB, each run:

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 8 GB AD-IQ3_S prompt of 302 tokens main 215.7 214.5 212.0
RTX 2070 8 GB AD-IQ3_S prompt of 302 tokens this PR alone 211.8 207.7 213.8
RTX 2070 8 GB IQ4_NL prompt of 302 tokens main 209.7 210.3 203.9
RTX 2070 8 GB IQ4_NL prompt of 302 tokens this PR alone 205.6 205.8 206.0
RTX 2070 8 GB AD-IQ3_S prompt of 2K tokens main 467.1 467.4 464.3
RTX 2070 8 GB AD-IQ3_S prompt of 2K tokens this PR alone 467.2 461.0 464.7
RTX 2070 8 GB IQ4_NL prompt of 2K tokens main 412.4 393.9 423.9
RTX 2070 8 GB IQ4_NL prompt of 2K tokens this PR alone 427.8 423.9 398.2
RTX 2070 8 GB AD-IQ3_S prompt of 8K tokens main 507.0 506.5 507.5
RTX 2070 8 GB AD-IQ3_S prompt of 8K tokens this PR alone 506.8 507.2 505.7
RTX 2070 8 GB IQ4_NL prompt of 8K tokens main 516.6 517.5 515.2
RTX 2070 8 GB IQ4_NL prompt of 8K tokens this PR alone 516.4 515.9 513.8

Decode speed (tok/s, higher is better), RTX 2070 8 GB, each run:

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 8 GB AD-IQ3_S decode after a 302-token prompt main 41.9 42.2 42.4
RTX 2070 8 GB AD-IQ3_S decode after a 302-token prompt this PR alone 43.0 42.6 43.1
RTX 2070 8 GB IQ4_NL decode after a 302-token prompt main 39.8 40.2 40.1
RTX 2070 8 GB IQ4_NL decode after a 302-token prompt this PR alone 40.8 41.4 41.5
RTX 2070 8 GB AD-IQ3_S decode after an 8K-token prompt main 36.3 36.3 36.3
RTX 2070 8 GB AD-IQ3_S decode after an 8K-token prompt this PR alone 36.9 36.9 36.9
RTX 2070 8 GB IQ4_NL decode after an 8K-token prompt main 30.8 30.5 30.7
RTX 2070 8 GB IQ4_NL decode after an 8K-token prompt this PR alone 32.0 31.7 31.9

Related discussion

Approach

  • Inkling2::scratch_layouts() turns on the engine's two layouts. assign_scratch() now sizes the buffers by buf_rows_ instead of rows_.
  • A call of fewer than 32 tokens uses the small layout. The prompt batch's layout is part of the buffers that lend_prompt() gives the expert cache between prompts, and take_prompt() takes it back.
  • The attention's scores (64 MiB, sized for the context) are one buffer outside the scratch, shared by both layouts, as in Qwen3.6 (qwen35.cpp). Their size is unchanged, so the attention's chunks are too.
  • The layouts are off where nothing would be lent: with the DSpark drafter (its features use a whole call's rows as their stride), with the experts frozen (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.
  • Class: placement only. Every call does the same arithmetic. Only which experts are in VRAM changes.

Validation

Passed:

  • Speed (head d39af0b), the first table: RTX 2070 8 GB (Bazzite box, Ryzen 7 5800X), e2e-ab.sh (main and this PR
    interleaved, 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 each
    request a bare VRAM line reports the experts held; both builds CUDA 12.9 75;86).
  • Identity (head d39af0b), main vs this PR, RTX 2070 8 GB (Bazzite box, sm_75, CUDA 12.9), expert cache capped
    (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-token
    prompts, AD-IQ3_S at 302 / 2,048).
  • Experts in VRAM at load (head d39af0b vs main, both with the same two test patches: a cache cap that is off
    unless STRATA_X_CAP is set, and a stderr line with the cache size after each request). RTX 2070 Super 8 GB (it
    also 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.
    • Not frozen, the same in both runs: AD-IQ3_S 1,255 → 1,251 at load and 1,347 → 1,444 after the prompt; IQ4_NL
      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).
    • Frozen (STRATA_FREEZE_EXPERTS=1, the layouts off): IQ4_NL 922 and 922 in both runs; AD-IQ3_S 1,255 and 1,255 in
      run 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.
  • serve (head dc9cc2c), 6 requests each (51 to 8,192 tokens), AD-IQ3_S and IQ4_NL, in four modes: plain, --draft-model (layouts off), --window 4, --max-context 131072. All 6 answered in every run, 0 errors, at least 27 MiB of VRAM free.
  • Builds of d39af0b: CUDA sm_75 strata-poly, poly_draft_race_test, poly_gdn_mma_state_test, and CUDA 75;86 strata-poly. HIP strata-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 at inkling2.cu without it.
  • python3 tools/settings_register.py --check, tools/test_settings_register.py (8 tests), python3 tools/check --only docs on d39af0b: passed. This PR adds no new STRATA_* env var.

Failed:

  • none observed

Not run:

  • compute-sanitizer --tool memcheck over two long prompts and a short answer between them, not frozen (the lend and
    return cycle): the run was invalid for the same reason (--kv q8_0 refused before the first CUDA call).
  • serve with two overlapping requests (parallel: 2, --batch 2), not frozen: needs [Engine] Inkling 2 Mini: several requests at once (batch slots, parallel) #226. Without it Inkling 2
    runs one sequence and refuses --batch, so the engine did not start.
  • Identity at 8,192 tokens with IQ4_NL. With the cache capped at 800 experts there was no VRAM left for the attention cache, both before and after.
  • Any run on AMD (only the compile check above), on Windows HIP, on Metal (not built; there vmm_ is false, so the layouts are off), or on several cards (the layouts are off with several cards).

Limits

  • The decode gain is small (+1.7% to +3.9%) and only on cards where the expert cache is the limit.
  • On by default, so when the cache is not frozen the greedy output can differ from main: which experts are in VRAM decides a layer's sums to the last bit (issue 55). That is the same for any change of placement.
  • With pieced prefill (--batch), the larger lent region is taken back and lent again at every piece. That costs time only, and it is not measured yet.
  • Off with --draft-model, frozen experts, every expert in VRAM, --window or batch steps of 32 rows or more, several cards, or no VMM.
  • AMD / ROCm on Linux: on HIP-Linux, main's prompt lending crashes when some experts are in RAM: vmm_unmap() frees and reserves the addresses again (a SIGSEGV inside hipLaunchKernel, RX 9070 XT, IQ4_NL). Main already lends Inkling 2's other prompt buffers (prompt_lends_ does not depend on scratch_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

okada-shogo and others added 4 commits November 23, 2026 07:48
…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>
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