Repository navigation
Conversation
…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
|
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:
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 Details: CONTRIBUTING.md |
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
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(orSTRATA_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-batch--stage-ring 1, each run (tokens/s)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-batchand 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-ringsplitsstage_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 byplan_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-aheadtokens (default 1,024), decode, the CPU share path and--layer-splitdo not take the ring; with it off,moe_rows()takes the paths it took before (ringis 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) anddocs/models/inkling.md(a section with the numbers above).Validation
Revision
5bddf3729(branchfeat/inkling2-prefill-overlap) on42e489d14. 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 thecuda-compilepreset), in a container. ModelInkling-2-Mini-UD-Q4_K_XL.gguf. Prompts are synthetic (a repeated paragraph, tokenized with the model'stokenizer.jsonto 512, 2,048 and 7,580 tokens), read cold.Passed:
cmake --preset cuda-compile -DSTRATA_BUILD_TESTS=OFF -S engine -B build -G Ninja && cmake --build build --target strata-poly, exit 0.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 linesprompt N tokens X tok/sare 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.--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.feat/inkling2-dflash2), at the same speed (534.6 and 537.5 tokens/s there, 533.9 to 539.1 here).--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:
expert staging: invalid argument) from a self-referencing pointer I had written (sb = sb + at); fixed before the commit.Not run:
cuda*functions as the code around them, through the compat headers, but that is not a build. Everything about AMD stays unverified.--layer-split, the CPU share path (cpu_share:spareis false when the ring runs).Engine::moe_rows(), shared by the other MoE models; none was run.--expert-swaps 0runs on this card already differ from one another after 22 to 67 tokens, so only the frozen runs say anything.--max-new.tools/andengine/tests; no new test is added (a regression check for the ring would need a card with partial experts).Limits
--stage-aheadtokens (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.--prompt-batch 2048against 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.🤖 Generated with Claude Code
https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW