Skip to content

[Engine] Inkling 2: merge small decode kernels for CPU experts - #288

Open
okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/launch-merge
Open

okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/launch-merge

Conversation

@okada-shogo

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

Copy link
Copy Markdown
Contributor

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-int8 and STRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1 STRATA_INK_PRESTAGE=1:

Card Model Measure main this PR alone Change
RTX 2070 Super 8 GB IQ4_NL decode after 302 (tok/s) 41.4 43.1 +4.0%
RTX 2070 Super 8 GB IQ4_NL decode after 2,048 (tok/s) 37.0 37.7 +1.9%
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) decode after 302 (tok/s) 48.9 50.2 +2.7%
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) decode after 2,048 (tok/s) 46.0 48.0 +4.3%

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)
Card Model Measure Build Run 1 Run 2 Run 3 Run 4 Run 5 Run 6
RTX 2070 Super 8 GB IQ4_NL decode after 302 main 42.1 37.1 41.9 42.9 42.6 42.0
RTX 2070 Super 8 GB IQ4_NL decode after 302 this PR alone 43.6 40.7 43.3 43.5 43.7 43.7
RTX 2070 Super 8 GB IQ4_NL decode after 2,048 main 37.7 38.5 37.9 34.6 38.8 34.7
RTX 2070 Super 8 GB IQ4_NL decode after 2,048 this PR alone 38.8 38.6 39.2 38.9 38.8 32.1
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) decode after 302 main 48.6 49.2 49.4 48.5 48.8 49.1
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) decode after 302 this PR alone 50.1 49.8 50.8 49.9 50.3 50.6
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) decode after 2,048 main 46.9 46.5 43.0 46.8 46.3 46.4
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) decode after 2,048 this PR alone 48.1 47.9 48.2 47.6 47.9 48.1

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 abort is set, so every wait ends at once: this measures launches and kernels, not the CPU experts.

Card Part Case main (separate launches) this PR alone Change
RTX 2070 Super 8 GB route + count + ring experts outside VRAM 665.3 576.2 −13.4%
RTX 2070 Super 8 GB route + count + ring all picks in VRAM (no ring) 383.4 296.2 −22.7%
RTX 2070 Super 8 GB sum + bell + conv no ring 381.1 156.9 −58.8%
RTX 2070 Super 8 GB sum + bell + conv a ring, answered at once 755.3 885.0 +17.2%

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_add uses 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:

  • a single block polling the host for the whole end of the layer: +0.7 to +1.3%;
  • sum, bell_add and convolution as 3 launches: +2.0 to +2.1%;
  • merging only the routing kernels: −2.0 to +1.6%.

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(), which route_kernel uses unchanged. In the new kernel, the first warp routes the token, its lanes count their picks, and then the block runs bell_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, so layer_tail() does the sum. moe_card() then skips bell_add(), moe_shared() skips the memset, and the down scales stay on the weights inside the kernel, as expert_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 is ink_moe_conv_add()'s kernel with bell_add_kernel's wait and add between the sum and the convolution: fo = (0 + Σ w·x) + hy, then the same convolution.
  • Same bits by construction: every float operation is the same and in the same order. The new tests check it byte for byte.
  • CUDA only, like the doorbell. HIP builds (no doorbell there) and Metal (guarded out) are unchanged.
  • nsys kernel count per token: IQ4_NL 1,120 → 928, UD-Q2_K_XL 1,302 → 1,066.

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 (hy is 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-graphs are 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-int8 and STRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1 STRATA_INK_PRESTAGE=1 on both sides. Tables above.

  • GPU power during decode (the merged kernel polls the doorbell from one thread in each of 9 blocks, bell_add from one): nvidia-smi power.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:

    Card Model Measure main this PR alone Change
    RTX 2070 Super 8 GB IQ4_NL GPU power (W) 120.8 120.5 −0.2%
    RTX 2070 Super 8 GB IQ4_NL energy (J/token, lower is better) 2.790 2.788 −0.1%
    RTX 2070 Super 8 GB UD-Q2_K_XL (loop) GPU power (W) 123.2 123.8 +0.5%
    RTX 2070 Super 8 GB UD-Q2_K_XL (loop) energy (J/token, lower is better) 2.533 2.475 −2.3%

    J/token = W / tok/s, mean of 3 runs.

  • nsys kernel trace (decode, same flags, 64 tokens): route_ring_kernel and ink_moe_bell_conv_add_kernel run 48 times per token from the recorded graphs. bell_ring_kernel, count_picks_kernel, bell_add_kernel and expert_weights_kernel no longer run in recorded tokens.

  • Builds: CUDA (sm_75;86) and HIP (gfx1201;gfx1100, ROCm 7.2.4) of strata-poly and poly_inkling2_test.

Failed:

  • none observed

Not run:

  • AMD and Metal runs: the doorbell is CUDA only, so this code does not run there.
  • Cards with every expert in VRAM: they do not use the doorbell, so nothing changes.
  • Other NVIDIA cards (only sm_75 was timed). Laptop GPUs: the power check ran on a desktop card only.

Limits

  • Not changed here (also in main): route_pick() returns expert n_expert for a lane when a token has fewer than n_used finite scores (a NaN router score), and the doorbell then reads slots[n_expert], as bell_ring_kernel and the expert products do in main.
  • It only helps decode tokens that use the doorbell (8 to 16 GB cards with experts on the CPU).
  • The merged kernel polls the doorbell from 9 blocks. It is faster end to end on the 2070 Super, but slower when the answer is already waiting (see the microbenchmark). GPU power was the same on the 2070 Super; other CPU/GPU pairs (laptops) may differ.

🤖 Generated with Claude Code

… 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>
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: six fewer launches per layer in a doorbell decode token [Engine] Inkling 2: merge small decode kernels on the CPU-expert path (6 fewer launches per layer) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: merge small decode kernels on the CPU-expert path (6 fewer launches per layer) [Engine] Inkling 2: merge 8 small decode kernels into 2 when experts run on the CPU (6 fewer launches per layer, +1.9-4.3% decode, same bits) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: merge 8 small decode kernels into 2 when experts run on the CPU (6 fewer launches per layer, +1.9-4.3% decode, same bits) [Engine] Inkling 2: merge small decode kernels on the CPU-expert path (+2-4% decode) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: merge small decode kernels on the CPU-expert path (+2-4% decode) [Engine] Inkling 2: merge small decode kernels for CPU experts Nov 24, 2026
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