Skip to content

[Engine] opt-in staging ring: copy a prompt batch's next experts while the previous round is multiplied - #366

Open
drakarah wants to merge 1 commit into
simplexai-labs:mainfrom
drakarah:feat/inkling2-prefill-overlap
Open

drakarah wants to merge 1 commit into
simplexai-labs:mainfrom
drakarah:feat/inkling2-prefill-overlap

Conversation

@drakarah

@drakarah drakarah commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Problem and result

On a card that holds only part of an MoE model's experts, reading a prompt copies each layer's missing experts through one staging buffer. The copy of the next layer's experts cannot start until the layer before has finished reading that buffer, so the card's multiplications wait on the copies. On Inkling 2 Mini (UD-Q4_K_XL, RTX 4060 8 GB, 1,001 of 3,072 experts on the card) a cold 2,048-token prompt took 4.03 s in the dense part of the trace: kernels and copies ran together for 15% of it, kernels alone for 48%, copies alone for 28%, neither for 9%. The kernels summed 2.53 s and the 21.8 GB of copies 1.66 s (12.7 GB/s, on a stream of their own), so the copies were not too slow, they were not hidden.

With --stage-ring 1 (or STRATA_POLY_STAGE_RING=1) the same buffer is used as two halves and each round is copied while the one before it is multiplied. The dense window of the same prompt goes from 3.77 s to 3.27 s (kernels and copies run together for 33% of it), and the prompt reads 13% faster at the default batch size:

Prompt --prompt-batch Default, each run (tokens/s) --stage-ring 1, each run (tokens/s)
512 tokens 1,024 253.8, 257.8 256.4, 252.1 (not engaged: under 1,024 a batch)
2,048 tokens 1,024 536.8, 537.4, 539.1, 533.9 606.9, 612.3, 608.9
7,580 tokens 1,024 695.5, 689.1 722.3, 722.2
2,048 tokens 2,048 615.7 745.2
7,580 tokens 2,048 840.2 885.4

It is off by default and uses no VRAM beyond the buffer that was already there. Context, not a claim: the TensorRT Edge-LLM build of the same model on the same card read 550 tokens at 235 and 241 tokens/s, 2,002 at 843 and 841, and 7,282 at 689 and 716, so this closes part of the gap at 2,048 tokens (537 to 609), not all of it; the rest is compute (the kernels alone sum 2.53 s).

Related discussion

none. The change is opt-in and changes no default (--prompt-batch and the expert cache are untouched: a larger batch also reads a long prompt faster, 695 to 840 tokens/s at 2,048 here, but costs 1.4 MiB of VRAM a token that the expert cache gives up, 1,001 experts at 1,024, 842 at 2,048, 527 at 4,096, so it is left as it is).

Approach

--stage-ring splits stage_ into two halves of the largest layer's expert size (ring_plan(), one stride for every layer so a mixed-type file such as Unsloth's UD cannot overlap its halves). A staged round is copied on the upload stream after an event the previous multiplications in that half record, multiplied after an event the copy records, and the copy of the round two on is queued after a round's multiplications. A layer's first two rounds (the experts copied ahead of the routing, Layer::ahead, now capped at two halves by plan_ahead()) are queued when the layer is reached, under the layer before's last round.

A prompt read of several batches runs a layer's batches one after the other, and the single buffer already reuses the staged experts for the second. The ring does the same where the call before copied them all ahead and nothing else (ring_il_, ring_ahead_); any other write to the buffer or its remapping drops that. The first version of this change left the reuse out and copied 39 GB where 21.8 GB are needed, with no speed-up.

Calls under --stage-ahead tokens (default 1,024), decode, the CPU share path and --layer-split do not take the ring; with it off, moe_rows() takes the paths it took before (ring is false, stage() gets offset 0, no event or copy is added). Changed files: engine/src/poly/engine.cpp, engine.hpp, main.cpp (the option and its help line) and docs/models/inkling.md (a section with the numbers above).

Validation

Revision 5bddf3729 (branch feat/inkling2-prefill-overlap) on 42e489d14. Environment: Linux, RTX 4060 8 GB (PCIe 4.0 x8), Core i9-12900H, 30 GB RAM, CUDA 13.4 (sm_86 and sm_89 from the cuda-compile preset), in a container. Model Inkling-2-Mini-UD-Q4_K_XL.gguf. Prompts are synthetic (a repeated paragraph, tokenized with the model's tokenizer.json to 512, 2,048 and 7,580 tokens), read cold.

Passed:

  • Build: cmake --preset cuda-compile -DSTRATA_BUILD_TESTS=OFF -S engine -B build -G Ninja && cmake --build build --target strata-poly, exit 0.
  • Speed table above: strata-poly --model Inkling-2-Mini-UD-Q4_K_XL.gguf --tokens <ids> --max-new 2 --no-draft --max-context 16384 [--prompt-batch N] [--stage-ring 1], the run lines prompt N tokens X tok/s are the numbers listed per run. Variability: 533.9 to 539.1 across four default runs of the 2,048-token prompt; the other cells are one or two runs.
  • Tokens written: the two tokens written after each of the three prompts are the same with and without --stage-ring 1 (also with --prompt-batch 2048). With --expert-swaps 0, 40 tokens written after the 2,048-token prompt are identical across two runs without the ring, two with it, and between the two groups.
  • Default path: with the ring off, the two tokens after the 2,048-token prompt equal those of an earlier build of the same engine code (the fork's feat/inkling2-dflash2), at the same speed (534.6 and 537.5 tokens/s there, 533.9 to 539.1 here).
  • Trace (nsys with --cuda-graph-trace=node, 2,048 tokens, one run each): dense window 3.77 s default, 3.27 s with the ring; 21.8 GB copied in both.

Failed:

  • none observed on the submitted revision. An earlier revision of this change (without the reuse across a layer's batches) ran at 524 to 532 tokens/s against 534 to 539 and copied 39 GB; it is what led to the reuse above and is not what is submitted. A first build of the submitted code also failed at the first prompt (expert staging: invalid argument) from a self-referencing pointer I had written (sb = sb + at); fixed before the commit.

Not run:

  • HIP, SYCL and Metal builds. The new lines call the same cuda* functions as the code around them, through the compat headers, but that is not a build. Everything about AMD stays unverified.
  • Any card other than this RTX 4060, a card that holds every expert (nothing is staged there; not run), --layer-split, the CPU share path (cpu_share: spare is false when the ring runs).
  • Any model other than Inkling 2 Mini. The code is Engine::moe_rows(), shared by the other MoE models; none was run.
  • Output equality beyond the cases above: no logits compared bit for bit, and without --expert-swaps 0 runs on this card already differ from one another after 22 to 67 tokens, so only the frozen runs say anything.
  • Decode speed after a prompt read with the ring, prompts over 7,580 tokens, a longer --max-new.
  • The CPU-only tests under tools/ and engine/tests; no new test is added (a regression check for the ring would need a card with partial experts).
  • Reproduction by anyone else.

Limits

  • Needs a card that holds part of the experts and a batch of --stage-ahead tokens (1,024) or more. At 7,580 tokens the gain is 4.5% at the default batch (722 against 689 to 695 tokens/s); I did not trace that prompt, so I cannot say why it is smaller.
  • Not at the edge engine's speed at 2,048 tokens (745 with --prompt-batch 2048 against its 842): its multiplications are also faster. An FP16-accumulate prompt GEMM on GeForce cards, which its notes credit with up to +44% prefill on dense models, is a possible next step here, lossy and not done.
  • Two halves is the smallest ring; a ring of smaller chunks (the edge engine copies 32 experts at a time through three buffers) could hide the copy of a layer's first round too, and is not tried.
  • Draft until the HIP and SYCL builds are done by someone with those environments; I would be glad of targeted help on them in this thread.

🤖 Generated with Claude Code

https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW

…erts while the one before it is multiplied

A prompt batch on a card that holds only part of the experts copies the experts it lacks through the staging buffer. The
buffer was one: the copy of a layer's staged experts could not start until the layer before had read it, so the card's
multiplications waited on the copies for a third of a 2,048-token prompt (nsys, RTX 4060: 4.03 s of which kernels and
copies ran together for 15%, the kernels alone for 48%, the copies alone for 28%, neither for 9%; kernels summed 2.53 s,
copies 1.66 s at 12.7 GB/s).

STRATA_POLY_STAGE_RING=1 (--stage-ring 1; off by default, so the default path is as before) splits the same buffer into
two halves. Each staged round is copied on the upload stream once its half is free (an event the multiplications of the
round before in that half record) and multiplied once it has arrived (an event the copy records); the copy of the round
two on follows the multiplications of a round, and a layer's first two rounds (the experts copied ahead of the routing)
start when the layer is reached, under the layer before's last. Where a layer's next batch finds its experts in the halves
(a prompt of several batches), it copies nothing, as the single buffer's `mine` does. Any other write to the buffer, or
its remapping, drops that.

Measured on an RTX 4060 (8 GB, PCIe 4.0 x8), Core i9-12900H, 30 GB RAM, CUDA 13.4, Inkling-2-Mini-UD-Q4_K_XL, the prompt
read cold, tokens/s, default against --stage-ring 1: 2,048 tokens 534-539 against 607-612 (+13%); 7,580 tokens 689-695
against 722 (+4.5%); 512 tokens unchanged (not engaged under 1,024 a batch). With --prompt-batch 2048: 2,048 tokens 616
against 745, 7,580 tokens 840 against 885. The tokens written after the prompts are the same, and 40 tokens written
after the 2,048-token prompt with --expert-swaps 0 are identical with and without it.

Not measured: a card that holds every expert, a CPU share of the experts, AMD (HIP), SYCL and Metal builds, --layer-split.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW
@github-actions github-actions Bot added the needs-format Title or description does not follow CONTRIBUTING.md yet label Nov 27, 2026
@github-actions

Copy link
Copy Markdown

Thanks for the pull request. To keep the tracker readable, every item needs an area prefix in the title and the parts of the pull request template filled in. This one is missing:

  • Start the title with its area: [Engine], [GUI], [TUI], [Benchmark], [Docs] or [Other], then a short description. Example: "[Engine] fix decode stop on long prompts".

Edit the pull request (title and description) and this note goes away on its own. If it is still missing parts in 5 days it will be closed, which is not a rejection: fix it and comment /reopen.

Details: CONTRIBUTING.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-format Title or description does not follow CONTRIBUTING.md yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant