Repository navigation
[Engine] Inkling 2: merge small decode kernels for CPU experts - #288
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
… experts' sum and convolution With experts outside VRAM (the doorbell), each Inkling 2 layer of a token ran route, count_picks and bell_ring (three one-block kernels), and after the down product a memset, expert_weights, moe_sum, bell_add and ink_conv_add. route_ring() does the first three in one launch (Engine::route_card_ring(), Inkling 2 only), and moe_sum_later() now holds with the doorbell too, so layer_tail() sums the experts, waits for the host's answer, adds it and runs the convolution in one launch (ink_moe_bell_conv_add(), ink_moe_conv_add()'s kernel with the bell's wait). Six launches fewer a layer, 288 a token of Inkling 2 Mini. Every result is the same bits: the same arithmetic in the same order. poly_inkling2_test's route_ring and moe_bell_conv_add cases compare them byte for byte: misses and none, --tail-skip's place, an exact tie of two picks, a width that is not a whole number of blocks, a bell answered before the launch, one the host answers while the launch waits, and an abort. Other families and the path without the doorbell launch what they did. The doorbell is VecSzn's design (after Strata v0.2.19's Inkling hybrid mode). 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
When some of a token's routed experts are not in VRAM, main decodes Inkling 2 with the "doorbell": the GPU asks the CPU workers for the missing experts through a flag in pinned host memory (
STRATA_POLY_BELL), with no host round trip. On that path, each layer runs 3 one-block kernels after the router (route,count_picks,bell_ring), and 5 more launches after the down product (a memset,expert_weights,moe_sum,bell_add,ink_conv_add). On an 8 GB card these tiny launches are a measurable part of each token.This PR merges them into 2 launches per layer, 6 fewer, which is 192 to 236 fewer launches per token. Every output is the same bits. Decode on an RTX 2070 Super is 1.9% to 4.3% faster. The path without the doorbell and other model families are unchanged.
Decode tok/s, RTX 2070 Super 8 GB, mean of 6 runs (2 jobs × 3 rounds, alternating order). Both sides ran with
--prompt-int8andSTRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1 STRATA_INK_PRESTAGE=1:Prompt speed is unchanged (within ±1% in every case; tables in the logs). GPU power and energy per token are the same within measurement (table in Validation).
Each run (decode tok/s)
Runs 1-3 and 4-6 are two separate jobs. In each round, main and this PR ran one after the other in alternating order. This PR was faster in 22 of the 24 pairs.
Kernel microbenchmark (µs per token, lower is better)
48 layers in one CUDA graph, median of 15 × 200 replays, RTX 2070 Super. The doorbell's
abortis set, so every wait ends at once: this measures launches and kernels, not the CPU experts.The last row is slower in isolation. In the merged kernel, one thread in each of its 9 blocks reads the doorbell flag in host memory, while
bell_adduses one block (15 µs against 8 µs per layer when the answer is already there). In a real token the card waits there for the CPU experts anyway.End to end, this version beat three alternatives in the same 5-way job (3 rounds; the three steady cases, because IQ4_NL after 2,048 was too noisy). This PR's shape gave +2.7 to +3.0% there. The alternatives were:
bell_addand convolution as 3 launches: +2.0 to +2.1%;Related discussion
#200, comment #200 (comment) (one optimization per PR).
The doorbell is VecSzn's design (after Strata v0.2.19's Inkling hybrid mode). The idea to merge the one-block kernels came from our own trace study.
Approach
route_ring()(engine/src/poly/kernels.cu) does routing, pick counts and the doorbell ring in one launch.route_kernel's body becomes a device function,route_pick(), whichroute_kerneluses unchanged. In the new kernel, the first warp routes the token, its lanes count their picks, and then the block runsbell_ring_kernel's body (bell_ring_block()).Engine::route_card_ring()is a new virtual. It defaults to false, so other families keep the three launches.moe_card()calls it only when the token rings the doorbell.Inkling2::moe_sum_later()now also applies with the doorbell, solayer_tail()does the sum.moe_card()then skipsbell_add(),moe_shared()skips the memset, and the down scales stay on the weights inside the kernel, asexpert_weights()applied them.ink_moe_bell_conv_add()(engine/src/poly/inkling2.cu) does the experts' sum, the wait for the CPU's answer, the CPU's sum and the convolution in one launch. It isink_moe_conv_add()'s kernel withbell_add_kernel's wait and add between the sum and the convolution:fo = (0 + Σ w·x) + hy, then the same convolution.Validation
Passed:
poly_inkling2_test, revision cf1f6eb on main 7af9bbd, RTX 2070 Super (sm_75, driver 580.178.04):521 checks, 0 failed. The same test on main:425 checks, 0 failed. The new cases are all compared byte for byte with the launches they replace:route_ring: picks, weights, counts, the doorbell's bytes and the host input, with and without experts missing from VRAM,--tail-skip's place 6 and 8, and an exact tie of two picks (equal score and bias: the lower expert goes first).moe_bell_conv_add: experts' sum, output, stream and state, plain and scaled, at width 2,304 and at 2,200 (the last block is partial and its extra threads still reach the barrier). Doorbell cases: no ring; answered before the launch; answered by the host after the launch while the kernel waits (hyis NaN until then, so an early read would show); and an abort (the launch must end).Engine identity, revision 5ba48fc (cf1f6eb changes only the test) against main 7af9bbd, RTX 2070 8 GB (sm_75):
strata-poly --tokens @ids302 --max-new 64 --top-probs 20,STRATA_FREEZE_EXPERTS=1 STRATA_GPU_EXPERTS=600(doorbell on). IQ4_NL (no down scales) and UD-Q2_K_XL loop (NVFP4 down scales); default flags and the speed-run flags; CUDA graphs and--no-graphs; without and with--tail-skip 4. Token ids and top-20 lines are SAME in all 16 settings. Graphs vs--no-graphsare also SAME, so graph capture works with the new launches.Decode end to end, revision 5ba48fc against main 7af9bbd, RTX 2070 Super 8 GB, Ryzen 5 3600XT:
--serve, one warm-up request, then 302 or 2,048 prompt tokens + 256 written, greedy,--kv fp16, 32K context, no expert profile, with--prompt-int8andSTRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1 STRATA_INK_PRESTAGE=1on both sides. Tables above.GPU power during decode (the merged kernel polls the doorbell from one thread in each of 9 blocks,
bell_addfrom one):nvidia-smipower.draw every 100 ms over the last 15 s of a 768-token decode, same flags, 3 runs per side, alternating order. No measurable difference:J/token = W / tok/s, mean of 3 runs.
nsys kernel trace (decode, same flags, 64 tokens):
route_ring_kernelandink_moe_bell_conv_add_kernelrun 48 times per token from the recorded graphs.bell_ring_kernel,count_picks_kernel,bell_add_kernelandexpert_weights_kernelno longer run in recorded tokens.Builds: CUDA (sm_75;86) and HIP (gfx1201;gfx1100, ROCm 7.2.4) of
strata-polyandpoly_inkling2_test.Failed:
Not run:
Limits
route_pick()returns expertn_expertfor a lane when a token has fewer than n_used finite scores (a NaN router score), and the doorbell then readsslots[n_expert], asbell_ring_kerneland the expert products do in main.🤖 Generated with Claude Code