Skip to content

[Engine] Inkling 2: simpler decode kernel for cached experts - #284

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

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

Conversation

@okada-shogo

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

Copy link
Copy Markdown
Contributor

Problem and result

During decode, Inkling 2 multiplies the token by each cached expert with native_mmvq_slots_kernel. That kernel uses 4 warps for each output row, adds their partial sums through shared memory, and leaves threads idle when a row's blocks do not divide evenly over the block's 64 thread pairs (an expert row of 2304 or 1152 IQ4_NL values is 72 or 36 blocks). Inkling 2 already asks for a lighter kernel (MmvqSlots::lanes), but only IQ3_S and IQ3_XXS had one.

This PR adds that lighter kernel for IQ4_NL, Q8_0, Q2_0, MXFP4, Q4_0, Q2_K, Q4_K and IQ4_XS: a single warp computes each row, with no shared memory and no barrier. It adds the same terms in the same order, so the output is the same bits. It is CUDA only, there is no new flag, and HIP is unchanged. Decode on an RTX 2070 Super is +2.4% on IQ4_NL and +0.7% on UD-Q2_K_XL.

Decode tok/s, RTX 2070 Super 8 GB (Ryzen 5 3600XT), 302-token prompt + 256 tokens, mean of 3 interleaved runs:

Card Model Measure main this PR alone Change
RTX 2070 Super 8 GB IQ4_NL decode after 302 (tok/s) 43.0 44.0 +2.4%
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 302 (tok/s) 46.2 46.5 +0.7%

Both sides ran with --prompt-int8 and STRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1 STRATA_INK_PRESTAGE=1. The prompt does not use these kernels; its tok/s is in the details below as a control.

Each run (tok/s)

Decode tok/s. Rounds in the order main/PR, PR/main, main/PR; each process serves one warm-up request first.

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL decode after 302 main 43.1 43.0 42.9
RTX 2070 Super 8 GB IQ4_NL decode after 302 this PR alone 44.4 44.1 43.6
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 302 main 46.3 46.0 46.2
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 302 this PR alone 46.5 46.6 46.4

Prompt tok/s, the same runs (control):

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL prompt 302 main 286.2 288.4 286.9
RTX 2070 Super 8 GB IQ4_NL prompt 302 this PR alone 285.0 283.5 284.2
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 302 main 386.3 392.7 396.5
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 302 this PR alone 389.3 389.6 388.3

An earlier run with the same setup, except that main ran first in every round:

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL decode after 302 main 42.8 41.8 41.4
RTX 2070 Super 8 GB IQ4_NL decode after 302 this PR alone 43.8 43.6 43.2
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 302 main 46.0 46.1 45.8
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 302 this PR alone 46.5 46.4 46.5
Kernel times: 8 experts in one launch (µs, lower is better)

RTX 2070 8 GB (sm_75, not Super), poly_slots_lanes_test --bench: 8 experts per launch, n_out 2304, best of 5 replays of a graph of 200 launches, four sets of weights in turn, measured before the per-format gate was added. "main" is the same launch without lanes. The formats marked "kept off" are slower with the new kernel, so this PR keeps the block kernel for them.

Noise check: after the gate, the "kept off" formats run the same kernel in both columns, and the second column still reads 0.2–9.6% slower. So this bench's "lanes kernel" column, which is measured second, leans slow by up to about that much.

Card Format n_in main lanes kernel Change In this PR
RTX 2070 8 GB IQ4_NL 2304 96.1 84.9 −11.7% lanes
RTX 2070 8 GB IQ4_NL 1152 54.0 51.1 −5.4% lanes
RTX 2070 8 GB Q8_0 2304 125.9 114.7 −8.9% lanes
RTX 2070 8 GB Q8_0 1152 85.1 62.8 −26.2% lanes
RTX 2070 8 GB Q2_0 2304 67.5 64.7 −4.1% lanes
RTX 2070 8 GB Q2_0 1152 47.4 38.8 −18.1% lanes
RTX 2070 8 GB MXFP4 2304 125.5 120.1 −4.3% lanes
RTX 2070 8 GB MXFP4 1152 71.3 68.6 −3.8% lanes
RTX 2070 8 GB Q4_0 2304 92.9 73.4 −21.0% lanes
RTX 2070 8 GB Q4_0 1152 49.3 43.4 −12.0% lanes
RTX 2070 8 GB Q5_0 2304 97.6 95.8 −1.8% kept off
RTX 2070 8 GB Q5_0 1152 52.9 59.4 +12.3% kept off
RTX 2070 8 GB Q2_K 2304 99.5 88.6 −11.0% lanes
RTX 2070 8 GB Q2_K 1280 56.1 54.2 −3.4% lanes
RTX 2070 8 GB Q3_K 2304 131.2 181.3 +38.2% kept off
RTX 2070 8 GB Q3_K 1280 107.6 121.5 +12.9% kept off
RTX 2070 8 GB Q4_K 2304 89.3 78.8 −11.8% lanes
RTX 2070 8 GB Q4_K 1280 52.9 50.4 −4.7% lanes
RTX 2070 8 GB Q5_K 2304 94.4 111.5 +18.1% kept off
RTX 2070 8 GB Q5_K 1280 55.9 68.7 +22.9% kept off
RTX 2070 8 GB Q6_K 2304 118.5 152.3 +28.5% kept off
RTX 2070 8 GB Q6_K 1280 81.9 89.8 +9.6% kept off
RTX 2070 8 GB IQ4_XS 2304 87.3 83.1 −4.8% lanes
RTX 2070 8 GB IQ4_XS 1280 61.0 53.4 −12.5% lanes

Related discussion

Split out of #200 at the maintainer's request (one optimization per PR). The kernel is #200's native_mmvq_slots_lanes_kernel (commit a4fa510), adapted to main's launch_slots().

Approach

  • When slots.lanes is set, launch_slots() uses native_mmvq_slots_lanes_kernel: 4 rows per block, one warp per row. This covers both the host's list of cached experts (native_mmvq_slots) and the card's own expert picks (native_mmvq_ids, used when all of a layer's routed experts are in VRAM).
  • Lane l does the work of the block kernel's threads l, 32 + l, 64 + l and 96 + l, each into its own sum. It adds those sums in the order the block kernel adds its warps' partials, then runs the same warp_sum. Each term is the same F::apply(F::load(...)), in the same order, so the bits are the same.
  • Q5_0, Q3_K, Q5_K and Q6_K were slower with this kernel (kernel table above), so kSlotsLanesFaster keeps the block kernel for them. Inkling 2's files do not use these formats for their experts.
  • Only Inkling 2 sets lanes (Engine::lane_slots_), so other models are unchanged. Under HIP, #if !defined(__HIPCC__) keeps the old launches.
  • New test: poly_slots_lanes_test.

Validation

Passed:

  • Rebased onto main d2ecae2 (head e7de73c), RTX 2070 Super 8 GB (sm_75):
    • poly_slots_lanes_test: 0 failures of 660 checks, both by default and with STRATA_SLOTS_WARP=1. poly_iq3_lanes_test: ok.
    • Frozen identity, main d2ecae2 vs this PR, with the expert cache capped (STRATA_GPU_EXPERTS=1900 for UD-Q2_K_XL, 850 for IQ4_NL): SAME in 8 of 8 comparisons.
    • Without the cap, 1 of 8 comparisons differed (UD-Q2_K_XL, no graphs, default settings), because the two builds loaded 1962 and 1974 experts. With the cap, it is SAME.
  • The checks below were run on the earlier base, main 1a41ff6:
    • Identity, RTX 2070 8 GB (sm_75, driver 610.43): strata-poly --tokens @ids302 --max-new 64 --top-probs 20, greedy, STRATA_FREEZE_EXPERTS=1. Token ids and top-20 lines are SAME for IQ4_NL and UD-Q2_K_XL, with default settings and with the settings above, with and without CUDA graphs (8 of 8 comparisons). This engine binary was built before the format gate. The gate only changes Q5_0 / Q3_K / Q5_K / Q6_K launches, which these files do not use: their experts are IQ4_NL + Q8_0, and Q2_0 / IQ1_S / IQ2_* / NVFP4 / MXFP4.
    • poly_slots_lanes_test (new): ok: 0 failures of 660 checks on an RTX 2070 8 GB (sm_75) and an RTX 3060 (sm_86), both by default and with STRATA_SLOTS_WARP=1 (the warp kernel as the old path). Each slot's outputs are followed by canaries: every row must be written and no pad value touched. compute-sanitizer memcheck of --quick: 0 errors on both cards. After the format gate (commit b428c59), on the RTX 2070: 0 failures of 660 checks again.
    • The test covers IQ4_NL, Q8_0, Q2_0, MXFP4, Q4_0, Q5_0, Q2_K, Q3_K, Q4_K, Q5_K, Q6_K and IQ4_XS; 1 to 32 experts per launch; n_in from one block to 4096; n_out 1 to 2304. It compares lanes against no lanes to the bit, through both the host list and the card's picks.
    • poly_iq3_lanes_test: ok, 0 failures (RTX 2070).
  • Build: CUDA 12.9, CMAKE_CUDA_ARCHITECTURES="75;86;120" (on d2ecae2: "75;86"), targets strata-poly, poly_slots_lanes_test, poly_iq3_lanes_test.

Failed:

  • none observed

Not run:

  • HIP build: no ROCm toolchain was used for this PR. The change is inside #if !defined(__HIPCC__), so HIP code is unchanged.
  • sm_120 kernel test: the binary is built for it, but no Blackwell card was run.
  • Speed on Ampere / Ada / Blackwell: only the RTX 2070 Super (end-to-end) and the RTX 2070 (kernel) were measured.
  • Decode after longer prompts (8K, 32K).

Limits

🤖 Generated with Claude Code

…s a warp a row

With MmvqSlots::lanes / MmvqIds::lanes set (Inkling 2 only, already set
by Inkling2::load_geometry()), launch_slots() now takes
native_mmvq_slots_lanes_kernel for native_mmvq.cu's formats IQ4_NL,
Q8_0, Q2_0, MXFP4, Q4_0, Q2_K, Q4_K and IQ4_XS.  Q5_0, Q3_K, Q5_K and
Q6_K were slower with it (RTX 2070, 8 experts: Q3_K 2304 -> 2304 131 ->
181 us, Q5_K 94 -> 112, Q6_K 119 -> 152, Q5_0 1152 -> 2304 53 -> 59)
and keep the block kernel (kSlotsLanesFaster).  That is both
native_mmvq_slots() (the host's list of cached experts) and
native_mmvq_ids() (the card's picks, the path taken when a layer's
routed experts are all in VRAM).  Before, only IQ3_S
and IQ3_XXS had a lanes kernel; these formats ran
native_mmvq_slots_kernel, a block of 4 warps a row.

A warp takes a row.  Lane l does the work of the block's threads l,
32 + l, 64 + l and 96 + l, each into its own sum, and adds them in the
order the block adds its warps' partials, then the same warp_sum: the
same terms in the same order, so the same bits.  No shared memory, no
barrier, and no thread idle where the block's threads do not divide a
row (an expert's 2304 and 1152 IQ4_NL values are 72 and 36 blocks for
the block's 64 thread pairs).  CUDA only; HIP launches are unchanged.

Ported from our earlier engine in PR simplexai-labs#200 (commit a4fa510, "kernels:
the cached experts' slots launches a warp a row, opt-in"), adapted to
main's launch_slots().

poly_slots_lanes_test checks the lanes launch (host list and card picks)
against the launch without lanes, to the bit, for 12 formats, 1 to 32
experts, n_in from one block to 4096 and row counts that leave a
block's last warps empty; canaries after each slot's outputs catch rows
left unwritten and writes past a slot.

0 of 660 checks fail on an RTX 2070 (sm_75) and an RTX 3060 (sm_86),
with and without STRATA_SLOTS_WARP=1; compute-sanitizer memcheck clean.  8 experts per launch, RTX 2070: IQ4_NL 2304 -> 2304
118.2 -> 106.1 us, 1152 -> 2304 54.0 -> 51.2; Q8_0 1152 -> 2304 85.3 ->
62.7; Q2_0 1152 -> 2304 47.1 -> 38.5.  RTX 2070 Super, decode after a
302-token prompt (--prompt-int8 setting), mean of 3 runs: IQ4_NL 43.0 ->
44.0 tok/s, UD-Q2_K_XL 46.2 -> 46.5.  Greedy tokens and top-20 SAME as
main with frozen experts (IQ4_NL, UD-Q2_K_XL; graphs and no graphs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: IQ4_NL / Q8_0 / Q2_0 / MXFP4 expert products a warp a row (same bits) [Engine] Inkling 2: one warp per row in decode expert products for IQ4_NL, Q8_0, Q2_0 and MXFP4 Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: one warp per row in decode expert products for IQ4_NL, Q8_0, Q2_0 and MXFP4 [Engine] Inkling 2: lighter decode kernel for cached experts in IQ4_NL, Q8_0, Q2_0 and MXFP4 (one warp per output row, +2.4% IQ4_NL decode, same bits) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: lighter decode kernel for cached experts in IQ4_NL, Q8_0, Q2_0 and MXFP4 (one warp per output row, +2.4% IQ4_NL decode, same bits) [Engine] Inkling 2: simpler decode kernel for cached IQ4_NL, Q8_0, Q2_0 and MXFP4 experts (+2.4% IQ4_NL decode, same bits) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: simpler decode kernel for cached IQ4_NL, Q8_0, Q2_0 and MXFP4 experts (+2.4% IQ4_NL decode, same bits) [Engine] Inkling 2: simpler decode kernel for cached experts (+2.4% IQ4_NL decode) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: simpler decode kernel for cached experts (+2.4% IQ4_NL decode) [Engine] Inkling 2: simpler decode kernel for cached 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