Skip to content

[Engine] Inkling 2: autotuned kernel for prompt F32 products - #224

Open
okada-shogo wants to merge 3 commits into
simplexai-labs:mainfrom
okada-shogo:port/gemm-f32
Open

okada-shogo wants to merge 3 commits into
simplexai-labs:mainfrom
okada-shogo:port/gemm-f32

Conversation

@okada-shogo

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

Copy link
Copy Markdown
Contributor

Problem and result

In a prompt batch, Inkling 2 Mini runs two F32 matrix products through gemm_f32(): the loop controllers' down
products (4,608 inputs -> 64 outputs) and the routers (2,304 -> 128). This PR adds a faster kernel for them, and the
engine picks its tile size by timing every option on the card once at load (the "autotune"; the picks are cached per
card). The output bits are the same as main's whatever the pick. CUDA and HIP, Inkling 2 only.

With the autotune, the two products are 14% to 45% faster than main on an RTX 2070 Super and 24% to 43% faster on an
AMD RX 9070 XT, in every shape. End to end the effect is small: prompt tok/s moves +0.1% to +1.1% on the RTX 2070
Super, +1.2% to +1.6% on an RTX 3090 and +0.7% to +1.0% on the RX 9070 XT, because these products are only 2-3% of
a prompt's time. The first version of this PR used a fixed tile rule instead; that rule was up to 2x slower than main
on the RX 9070 XT at 1,024 rows, which is why the autotune replaced it.

Prompt tok/s with the autotune (higher is better, mean of 3 runs; --serve, one GEN 1 request, fp16 KV; same
experts in VRAM both ways):

Card Model Measure main (tok/s) this PR alone (tok/s) Change
RTX 2070 Super 8 GB IQ4_NL prompt of 2K tokens 483.4 483.8 +0.1%
RTX 2070 Super 8 GB IQ4_NL prompt of 8K tokens 576.4 580.2 +0.7%
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) prompt of 2K tokens 569.9 576.2 +1.1%
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) prompt of 8K tokens 576.7 580.7 +0.7%
RTX 3090 24 GB IQ4_NL prompt of 2K tokens 2,830.3 2,875.6 +1.6%
RTX 3090 24 GB IQ4_NL prompt of 8K tokens 2,732.7 2,773.1 +1.5%
RTX 3090 24 GB UD-Q2_K_XL (loop) prompt of 2K tokens 2,476.2 2,511.1 +1.4%
RTX 3090 24 GB UD-Q2_K_XL (loop) prompt of 8K tokens 2,409.4 2,439.4 +1.2%
RX 9070 XT 16 GB UD-Q2_K_XL (loop) prompt of 2K tokens 1,087.1 1,097.9 +1.0%
RX 9070 XT 16 GB UD-Q2_K_XL (loop) prompt of 8K tokens 1,224.7 1,233.7 +0.7%

The same measure with the first fixed rule (215e21d), for reference (RTX 2070 Super, IQ4_NL 929 and UD-Q2_K_XL
1,988 of 3,072 experts in VRAM, measured without the warm-up request):

Card Model Measure main (tok/s) this PR alone (tok/s) Change
RTX 2070 Super 8 GB IQ4_NL prompt of 2K tokens 417.9 418.6 +0.2%
RTX 2070 Super 8 GB IQ4_NL prompt of 8K tokens 494.1 497.2 +0.6%
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) prompt of 2K tokens 514.9 519.0 +0.8%
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) prompt of 8K tokens 504.4 508.7 +0.9%

Why the end-to-end change is small: an nsys profile of one prompt shows that these two products take only 2.1% to
2.8% of the prompt's time on an RTX 2070 Super and an RTX 2080 Ti. A 20% to 30% faster kernel then predicts +0.6% to
+0.7% prompt tok/s, which matches the measured +0.3% to +0.7%, so the benchmark is not hitting another bottleneck.
With a 2% to 3% share, the end-to-end gain stays near +1%. The RTX 3090 (+1.2% to +1.4%) was not profiled; the share
there may be a little larger.

How the end-to-end runs were set up
  • RTX 2070 Super (autotune): main f0ab63a against head f675573, local sm_75 builds, one warm-up request (302
    tokens) in the same engine before each measured one, 32K context, default placement (IQ4_NL 917 to 921 and
    UD-Q2_K_XL 1,968 to 1,976 of 3,072 experts in VRAM at load, the same both ways within 8). The fixed-rule rows are
    215e21d, measured earlier without the warm-up request.
  • RTX 3090 (autotune): main f0ab63a and head 576d251, 75;86 builds, the same method (e2e-nw.sh: one warm-up
    request, 32K context, 3 rounds, order swapped each round), on a Salad cloud container (Ryzen 7 9700X for UD-Q2_K_XL;
    Ryzen 9 9950X with 62 GB RAM for IQ4_NL). All 3,072 experts in VRAM both ways (IQ4_NL 14.7 GiB), and no page faults
    during the runs (IQ4_NL: the file was fully in the page cache, pgmajfault unchanged).
  • RX 9070 XT: the user's desktop PC (Windows 11, WSL2 capped to 12 threads, ROCm 7.2.4, Ryzen 7 7800X3D), UD-Q2_K_XL
    with all 3,072 experts in VRAM, 16K context, a warm-up round first. IQ4_NL results there are not usable: the Windows
    desktop shares the card, so the number of experts in VRAM changes from run to run.
Profile of one prompt: share, predicted and measured gain

What it measures: one IQ4_NL prompt of 2,048 or 8,192 tokens + 1 token under nsys profile --trace=cuda, main d1f711e and this PR, default placement, fp16 KV. "Products" = the kernels this PR replaces (main: gemm_tile_kernel, this PR: gemm_ahead_kernel; the trace shows only those launches, 288 and 1,152 a prompt). Share = their time / the prompt's time under nsys, on main. Predicted = the products' saved ms / main's prompt ms without nsys. Measured = the mean of 3 --serve runs (GEN 1) without nsys, run in the same job.

Card Prompt Products on main (ms) Products, this PR (ms) Kernel change Share of prompt time on main Predicted prompt tok/s change Measured prompt tok/s change
RTX 2070 Super 8 GB 2K tokens 105.1 74.0 −29.6% 2.1% +0.64% +0.29%
RTX 2070 Super 8 GB 8K tokens 410.9 294.4 −28.4% 2.4% +0.71% +0.68%
RTX 2080 Ti 22 GB 2K tokens 85.1 67.7 −20.5% 2.6% +0.56% +0.70%
RTX 2080 Ti 22 GB 8K tokens 351.3 271.5 −22.7% 2.8% +0.65% +0.51%

Where the prompt's time goes on main (% of the prompt's time under nsys; the three add up to 100%):

Card Prompt Experts in VRAM GPU running kernels Only copies (expert uploads over PCIe) GPU idle
RTX 2070 Super 8 GB 2K tokens 919 of 3,072 70.8% 23.7% 5.6%
RTX 2070 Super 8 GB 8K tokens 919 of 3,072 88.5% 7.9% 3.6%
RTX 2080 Ti 22 GB 2K tokens 3,072 of 3,072 88.0% 0.6% 11.4%
RTX 2080 Ti 22 GB 8K tokens 3,072 of 3,072 95.2% 0.6% 4.2%

The largest kernels on every card and prompt are dequant_flat_kernel (18% of kernel time), gemm_iq_f16_grouped_kernel (15% to 16%), attn_softmax_block_kernel (14% to 18%) and cuBLAS FP16 GEMMs; none of them changes in this PR. The prompt is not bound by the CPU: main on half the physical cores reads it at the same speed (RTX 2070 Super: −0.7% at 2K and −0.4% at 8K, 3 of 6 cores; RTX 2080 Ti: 0% at 2K and 0% at 8K, 24 of 48 cores).

Each run without nsys (tok/s):

Card Prompt Build Run 1 (tok/s) Run 2 (tok/s) Run 3 (tok/s)
RTX 2070 Super 8 GB 2K tokens main 417.7 417.3 418.6
RTX 2070 Super 8 GB 2K tokens this PR alone 419.3 419.2 418.7
RTX 2070 Super 8 GB 8K tokens main 495.6 497.0 495.3
RTX 2070 Super 8 GB 8K tokens this PR alone 498.7 499.6 499.7
RTX 2080 Ti 22 GB 2K tokens main 658.7 660.2 660.3
RTX 2080 Ti 22 GB 2K tokens this PR alone 664.9 665.2 662.9
RTX 2080 Ti 22 GB 8K tokens main 659.1 659.0 659.2
RTX 2080 Ti 22 GB 8K tokens this PR alone 661.5 662.8 663.0

RTX 2080 Ti: rented box, the same sm_75 binaries (md5 db354870 main, 47d55511 this PR). On it this PR picks smaller tiles (68 SMs): gemm_ahead_kernel<32, 32, ...> and <32, 64, ...>.

Each run (tok/s)

Prompt tok/s, higher is better, each run:

Card Model Measure Build Run 1 (tok/s) Run 2 (tok/s) Run 3 (tok/s)
RTX 2070 Super 8 GB IQ4_NL prompt of 2K tokens main 417.6 419.1 416.9
RTX 2070 Super 8 GB IQ4_NL prompt of 2K tokens this PR alone 418.1 416.5 421.3
RTX 2070 Super 8 GB IQ4_NL prompt of 8K tokens main 492.8 494.2 495.4
RTX 2070 Super 8 GB IQ4_NL prompt of 8K tokens this PR alone 496.1 497.5 498.1
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) prompt of 2K tokens main 513.6 515.3 515.8
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) prompt of 2K tokens this PR alone 519.2 519.4 518.4
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) prompt of 8K tokens main 504.7 504.4 504.2
RTX 2070 Super 8 GB UD-Q2_K_XL (loop) prompt of 8K tokens this PR alone 509.0 508.4 508.8
RTX 3090 24 GB (autotune) IQ4_NL prompt of 2K tokens main 2,843.2 2,831.2 2,816.4
RTX 3090 24 GB (autotune) IQ4_NL prompt of 2K tokens this PR alone 2,882.5 2,885.7 2,858.6
RTX 3090 24 GB (autotune) IQ4_NL prompt of 8K tokens main 2,739.5 2,736.3 2,722.3
RTX 3090 24 GB (autotune) IQ4_NL prompt of 8K tokens this PR alone 2,773.0 2,775.8 2,770.5
RTX 3090 24 GB (autotune) UD-Q2_K_XL (loop) prompt of 2K tokens main 2,474.8 2,482.9 2,471.0
RTX 3090 24 GB (autotune) UD-Q2_K_XL (loop) prompt of 2K tokens this PR alone 2,502.0 2,525.1 2,506.3
RTX 3090 24 GB (autotune) UD-Q2_K_XL (loop) prompt of 8K tokens main 2,416.3 2,408.3 2,403.7
RTX 3090 24 GB (autotune) UD-Q2_K_XL (loop) prompt of 8K tokens this PR alone 2,446.7 2,442.4 2,429.0
RX 9070 XT 16 GB (autotune) UD-Q2_K_XL (loop) prompt of 2K tokens main 1,086.5 1,087.1 1,087.7
RX 9070 XT 16 GB (autotune) UD-Q2_K_XL (loop) prompt of 2K tokens this PR alone 1,099.4 1,098.3 1,096.0
RX 9070 XT 16 GB (autotune) UD-Q2_K_XL (loop) prompt of 8K tokens main 1,222.3 1,224.7 1,227.2
RX 9070 XT 16 GB (autotune) UD-Q2_K_XL (loop) prompt of 8K tokens this PR alone 1,236.9 1,234.9 1,229.2
RTX 2070 Super 8 GB (autotune) IQ4_NL prompt of 2K tokens main 483.3 483.2 483.8
RTX 2070 Super 8 GB (autotune) IQ4_NL prompt of 2K tokens this PR alone 484.7 483.3 483.4
RTX 2070 Super 8 GB (autotune) IQ4_NL prompt of 8K tokens main 577.3 575.3 576.7
RTX 2070 Super 8 GB (autotune) IQ4_NL prompt of 8K tokens this PR alone 581.3 578.6 580.7
RTX 2070 Super 8 GB (autotune) UD-Q2_K_XL (loop) prompt of 2K tokens main 569.8 571.7 568.3
RTX 2070 Super 8 GB (autotune) UD-Q2_K_XL (loop) prompt of 2K tokens this PR alone 576.7 575.7 576.2
RTX 2070 Super 8 GB (autotune) UD-Q2_K_XL (loop) prompt of 8K tokens main 576.1 575.9 578.1
RTX 2070 Super 8 GB (autotune) UD-Q2_K_XL (loop) prompt of 8K tokens this PR alone 580.1 580.7 581.2

strata-poly --serve --prompt-cache 0 --conversation-cache-mib 0 --max-context 32768 --kv fp16; main d1f711e and
this branch alternated, order swapped each round. RTX 2070 Super: local card, sm_75 build. The "(autotune)" rows
are main f0ab63a against head 576d251 with one warm-up request first (see above).

Kernel time in µs: poly_gemm_small_test --bench

With the autotune (this head), kernel time in microseconds (µs), lower is better, the same shapes, mean of 3 runs
of 5 rounds each; a negative Change is faster:

Card Shape main, gemm_f32 (µs) this PR alone, gemm_f32_tiles (µs) Change
RTX 2070 Super 8 GB 4608 -> 64, 333 rows 252.0 192.7 −23.5%
RTX 2070 Super 8 GB 2304 -> 128, 333 rows 166.2 124.8 −24.9%
RTX 2070 Super 8 GB 4608 -> 64, 1024 rows 415.1 277.4 −33.2%
RTX 2070 Super 8 GB 2304 -> 128, 1024 rows 250.4 214.5 −14.3%
RTX 2070 Super 8 GB 4608 -> 64, 4096 rows 1382.4 758.0 −45.2%
RTX 2070 Super 8 GB 2304 -> 128, 4096 rows 848.3 617.3 −27.2%
RX 9070 XT 16 GB 4608 -> 64, 333 rows 214.2 122.2 −43.0%
RX 9070 XT 16 GB 2304 -> 128, 333 rows 168.0 104.7 −37.7%
RX 9070 XT 16 GB 4608 -> 64, 1024 rows 200.1 134.5 −32.8%
RX 9070 XT 16 GB 2304 -> 128, 1024 rows 122.2 93.0 −23.9%
RX 9070 XT 16 GB 4608 -> 64, 4096 rows 432.2 277.4 −35.8%
RX 9070 XT 16 GB 2304 -> 128, 4096 rows 271.3 206.3 −24.0%

Each run (µs), with the autotune:

Card Shape Build Run 1 (µs) Run 2 (µs) Run 3 (µs)
RTX 2070 Super 8 GB 4608 -> 64, 333 rows main, gemm_f32 260.1 248.0 247.8
RTX 2070 Super 8 GB 4608 -> 64, 333 rows this PR alone, gemm_f32_tiles 192.8 192.7 192.7
RTX 2070 Super 8 GB 2304 -> 128, 333 rows main, gemm_f32 166.3 166.2 166.2
RTX 2070 Super 8 GB 2304 -> 128, 333 rows this PR alone, gemm_f32_tiles 124.7 125.0 124.7
RTX 2070 Super 8 GB 4608 -> 64, 1024 rows main, gemm_f32 411.7 411.7 422.0
RTX 2070 Super 8 GB 4608 -> 64, 1024 rows this PR alone, gemm_f32_tiles 275.4 280.8 276.1
RTX 2070 Super 8 GB 2304 -> 128, 1024 rows main, gemm_f32 252.1 249.6 249.6
RTX 2070 Super 8 GB 2304 -> 128, 1024 rows this PR alone, gemm_f32_tiles 214.8 214.3 214.6
RTX 2070 Super 8 GB 4608 -> 64, 4096 rows main, gemm_f32 1374.0 1392.0 1381.1
RTX 2070 Super 8 GB 4608 -> 64, 4096 rows this PR alone, gemm_f32_tiles 752.3 755.2 766.6
RTX 2070 Super 8 GB 2304 -> 128, 4096 rows main, gemm_f32 849.3 852.8 842.7
RTX 2070 Super 8 GB 2304 -> 128, 4096 rows this PR alone, gemm_f32_tiles 606.4 623.5 622.2
RX 9070 XT 16 GB 4608 -> 64, 333 rows main, gemm_f32 229.1 205.7 207.7
RX 9070 XT 16 GB 4608 -> 64, 333 rows this PR alone, gemm_f32_tiles 130.2 117.4 118.9
RX 9070 XT 16 GB 2304 -> 128, 333 rows main, gemm_f32 195.9 151.9 156.2
RX 9070 XT 16 GB 2304 -> 128, 333 rows this PR alone, gemm_f32_tiles 121.3 95.0 97.8
RX 9070 XT 16 GB 4608 -> 64, 1024 rows main, gemm_f32 201.0 199.6 199.6
RX 9070 XT 16 GB 4608 -> 64, 1024 rows this PR alone, gemm_f32_tiles 134.8 134.4 134.4
RX 9070 XT 16 GB 2304 -> 128, 1024 rows main, gemm_f32 124.3 120.9 121.3
RX 9070 XT 16 GB 2304 -> 128, 1024 rows this PR alone, gemm_f32_tiles 94.3 92.4 92.3
RX 9070 XT 16 GB 4608 -> 64, 4096 rows main, gemm_f32 433.0 424.9 438.7
RX 9070 XT 16 GB 4608 -> 64, 4096 rows this PR alone, gemm_f32_tiles 277.0 273.8 281.5
RX 9070 XT 16 GB 2304 -> 128, 4096 rows main, gemm_f32 270.8 272.4 270.8
RX 9070 XT 16 GB 2304 -> 128, 4096 rows this PR alone, gemm_f32_tiles 206.0 207.8 205.2

#224's first rule (215e21d), for reference:

Kernel time, in microseconds (µs), lower is better. What it times: one prompt-batch F32 product of Inkling 2 Mini's shapes (routers 2304 inputs -> 128 outputs, loop controllers 4608 -> 64) for a batch of 333, 1024 or 4096 prompt rows. Mean of 3 runs; a negative Change is faster.

Card Shape main, gemm_f32 (µs) this PR alone, gemm_f32_tiles (µs) Change
RTX 2070 Super 8 GB 2304 -> 128, 333 rows 167.4 147.3 −12.0%
RTX 2070 Super 8 GB 4608 -> 64, 333 rows 305.3 194.0 −36.5%
RTX 2070 Super 8 GB 2304 -> 128, 1024 rows 250.3 214.5 −14.3%
RTX 2070 Super 8 GB 4608 -> 64, 1024 rows 415.5 276.2 −33.5%
RTX 2070 Super 8 GB 2304 -> 128, 4096 rows 862.8 614.6 −28.8%
RTX 2070 Super 8 GB 4608 -> 64, 4096 rows 1406.6 748.6 −46.8%
RTX 3060 12 GB 2304 -> 128, 333 rows 127.5 89.0 −30.2%
RTX 3060 12 GB 4608 -> 64, 333 rows 174.0 129.7 −25.5%
RTX 3060 12 GB 2304 -> 128, 1024 rows 201.1 245.6 +22.1% (slower)
RTX 3060 12 GB 4608 -> 64, 1024 rows 322.1 296.2 −8.0%
RTX 3060 12 GB 2304 -> 128, 4096 rows 733.9 712.0 −3.0%
RTX 3060 12 GB 4608 -> 64, 4096 rows 1042.7 707.5 −32.1%
RX 9070 XT 16 GB 2304 -> 128, 333 rows 244.2 169.3 −30.7%
RX 9070 XT 16 GB 4608 -> 64, 333 rows 440.3 234.9 −46.6%
RX 9070 XT 16 GB 2304 -> 128, 1024 rows 266.0 518.2 +94.8% (slower)
RX 9070 XT 16 GB 4608 -> 64, 1024 rows 463.6 512.8 +10.6% (slower)
RX 9070 XT 16 GB 2304 -> 128, 4096 rows 611.4 559.5 −8.5%
RX 9070 XT 16 GB 4608 -> 64, 4096 rows 942.1 716.9 −23.9%

RTX 2070 Super, 2304 -> 128, 333 rows, this PR alone: run 2 (191.1 µs) was an outlier against 125.2 and 125.5.
Without it the mean is 125.35 µs, −25.1% against main; the smallest other gain on that card is then −14.3%.

Kernel time in µs, lower is better, each run:

Card Shape Build Run 1 (µs) Run 2 (µs) Run 3 (µs)
RTX 2070 Super 8 GB 2304 -> 128, 333 rows main, gemm_f32 167.3 167.4 167.4
RTX 2070 Super 8 GB 2304 -> 128, 333 rows this PR alone, gemm_f32_tiles 125.2 191.1 125.5
RTX 2070 Super 8 GB 4608 -> 64, 333 rows main, gemm_f32 366.0 249.4 300.6
RTX 2070 Super 8 GB 4608 -> 64, 333 rows this PR alone, gemm_f32_tiles 194.0 194.0 194.1
RTX 2070 Super 8 GB 2304 -> 128, 1024 rows main, gemm_f32 249.8 250.6 250.6
RTX 2070 Super 8 GB 2304 -> 128, 1024 rows this PR alone, gemm_f32_tiles 214.8 214.6 214.2
RTX 2070 Super 8 GB 4608 -> 64, 1024 rows main, gemm_f32 416.9 414.8 414.9
RTX 2070 Super 8 GB 4608 -> 64, 1024 rows this PR alone, gemm_f32_tiles 275.6 276.4 276.7
RTX 2070 Super 8 GB 2304 -> 128, 4096 rows main, gemm_f32 845.2 898.2 844.9
RTX 2070 Super 8 GB 2304 -> 128, 4096 rows this PR alone, gemm_f32_tiles 611.9 618.0 613.9
RTX 2070 Super 8 GB 4608 -> 64, 4096 rows main, gemm_f32 1425.6 1368.8 1425.4
RTX 2070 Super 8 GB 4608 -> 64, 4096 rows this PR alone, gemm_f32_tiles 749.2 748.7 747.9
RTX 3060 12 GB 2304 -> 128, 333 rows main, gemm_f32 127.5 127.7 127.4
RTX 3060 12 GB 2304 -> 128, 333 rows this PR alone, gemm_f32_tiles 88.9 89.3 88.9
RTX 3060 12 GB 4608 -> 64, 333 rows main, gemm_f32 174.0 174.0 174.0
RTX 3060 12 GB 4608 -> 64, 333 rows this PR alone, gemm_f32_tiles 129.7 129.8 129.5
RTX 3060 12 GB 2304 -> 128, 1024 rows main, gemm_f32 201.0 201.2 201.2
RTX 3060 12 GB 2304 -> 128, 1024 rows this PR alone, gemm_f32_tiles 245.8 245.5 245.5
RTX 3060 12 GB 4608 -> 64, 1024 rows main, gemm_f32 322.2 322.0 322.0
RTX 3060 12 GB 4608 -> 64, 1024 rows this PR alone, gemm_f32_tiles 296.3 296.2 296.0
RTX 3060 12 GB 2304 -> 128, 4096 rows main, gemm_f32 738.6 726.4 736.6
RTX 3060 12 GB 2304 -> 128, 4096 rows this PR alone, gemm_f32_tiles 710.0 715.7 710.2
RTX 3060 12 GB 4608 -> 64, 4096 rows main, gemm_f32 1037.1 1037.4 1053.6
RTX 3060 12 GB 4608 -> 64, 4096 rows this PR alone, gemm_f32_tiles 708.1 709.3 705.2
RX 9070 XT 16 GB 2304 -> 128, 333 rows main, gemm_f32 243.1 243.2 246.3
RX 9070 XT 16 GB 2304 -> 128, 333 rows this PR alone, gemm_f32_tiles 168.8 169.0 170.1
RX 9070 XT 16 GB 4608 -> 64, 333 rows main, gemm_f32 410.6 481.5 428.8
RX 9070 XT 16 GB 4608 -> 64, 333 rows this PR alone, gemm_f32_tiles 234.8 234.6 235.4
RX 9070 XT 16 GB 2304 -> 128, 1024 rows main, gemm_f32 266.0 266.3 265.8
RX 9070 XT 16 GB 2304 -> 128, 1024 rows this PR alone, gemm_f32_tiles 532.2 532.1 490.2
RX 9070 XT 16 GB 4608 -> 64, 1024 rows main, gemm_f32 497.0 446.2 447.5
RX 9070 XT 16 GB 4608 -> 64, 1024 rows this PR alone, gemm_f32_tiles 529.4 525.8 483.3
RX 9070 XT 16 GB 2304 -> 128, 4096 rows main, gemm_f32 625.3 627.3 581.6
RX 9070 XT 16 GB 2304 -> 128, 4096 rows this PR alone, gemm_f32_tiles 572.2 569.8 536.4
RX 9070 XT 16 GB 4608 -> 64, 4096 rows main, gemm_f32 971.9 925.7 928.6
RX 9070 XT 16 GB 4608 -> 64, 4096 rows this PR alone, gemm_f32_tiles 714.1 717.5 719.1

Both functions print the same hash of the product in every case. These two products are a small part of a prompt
batch, which is why the end-to-end change is small.

Related discussion

Split out of #200, as the maintainer asked there:
#200 (comment)

Independent of the other split PRs (based on main, not stacked).

Approach

  • New function gemm_f32_tiles() takes one group of 33 to 256 outputs in a new kernel, gemm_ahead_kernel. The
    kernel uses bigger tiles, loads values into registers two steps ahead, and stores to shared memory without bank
    conflicts. Other shapes go to gemm_f32() as before.
  • Same bits: each output is still one fmaf after the other in input order, like gemm_small_kernel and
    gemm_tile_kernel. Every option (each tile and gemm_f32()) sums this way, so the pick cannot change the output.
  • Tile choice (autotune): at load, Inkling 2 times its two shapes in five ways (the four tiles and gemm_f32()
    itself) at each row bucket up to the prompt batch, and keeps the fastest. The picks go to a cache file keyed by the
    card (name, SMs/CUs, clock, driver, runtime) and the build, so later loads use them without timing.
    • The one-time timing took 565 ms on the RTX 2070 Super (30 picks, up to 4,096 rows) and 351 ms on the RX 9070 XT
      (22 picks, a prompt batch of 1,024) at f675573. With head 576d251's steadier timing it is 885 ms on the RTX
      2070 Super. Loads from the cache take 0 to 1 ms.
    • Without a cache (or with STRATA_POLY_F32_AUTOTUNE=0), a cost model picks on Turing and Blackwell, and
      gemm_f32() itself is used on other cards, so the fallback is never slower than main.
  • Why not a fixed rule: the first version picked the largest of four tiles that still gave half the SMs a block,
    tuned on the 2070 Super. It did not carry over to other cards: the RTX 3060's router at 1,024 rows was 22% slower,
    and on the RX 9070 XT both 1,024-row shapes were slower than main (up to 2x).
  • Inkling 2 only (Engine::f32_tiles_, set in Inkling2::load_geometry()). Other families do not change. CUDA and
    HIP; Metal keeps its own gemm_f32.
  • poly_gemm_small_test checks both functions against the host, bit for bit, and --bench times Inkling 2's shapes.

Revisions: f675573 added the autotune, and head 576d251 adds the review fixes on top (listed under Validation).
Kernel tables marked "autotune" were measured at f675573; the others at 215e21d with its fixed rule.

Validation

Tested tree: 17a95fe9b (the engine diff to the PR head 2cc8c18 is empty); main d1f711e.

Passed:

  • Head f675573 (this PR's tile kernel + the autotune): poly_gemm_small_test all passed (every option bit for bit
    against the host, the autotune's picks valid, a cache round trip, the cost model where the cache cannot be written)
    on an RTX 2070 Super (CUDA 12.9, sm_75, local) and an RX 9070 XT (HIP, ROCm 7.2.4, gfx1201, WSL2; 0 build errors);
    --bench 3 runs on both (the autotune tables above); UD-Q2_K_XL prompt speed on the RX 9070 XT, main f0ab63a and
    this head alternated, a warm-up round first. IQ4_NL and UD-Q2_K_XL prompt speed on the RTX 2070 Super, main
    f0ab63a and this head (e2e-nw.sh: one warm-up request in the engine before each measured one, 3 rounds).

  • CUDA 12.9 build, sm_75: strata-poly and poly_gemm_small_test build at this commit.

  • poly_gemm_small_test (both functions vs the host fma chain, bit for bit) and --bench, 3 runs each: all passed,
    0 FAIL lines, on the RTX 2070 Super (sm_75) and an RTX 3060 12 GB (sm_86). gemm_f32 and gemm_f32_tiles print
    the same product hash in all 6 shapes.

  • Identity (prompts of 302, 2,048 and 8,192 tokens + 4 greedy tokens, --top-probs 20, fp16 KV, --expert-swaps 0,
    IQ4_NL and UD-Q2_K_XL, main twice and this PR twice): token ids and top-20 lines the same as main at 2,048 and
    8,192 tokens on both cards, and UD-Q2_K_XL 302 on the RTX 3060. At 302 tokens (IQ4_NL on both cards, UD-Q2_K_XL on
    the 2070 Super) the top-20 lines differ, but main vs main differs there as well, so those cases are not conclusive.

  • The prompt speed tables above.

  • nsys profile of one 2K and one 8K IQ4_NL prompt, main and this PR, on the RTX 2070 Super and an RTX 2080 Ti 22 GB
    (all experts in VRAM): this PR's gemm_ahead_kernel runs in place of gemm_tile_kernel; results in the profile table above.

  • HIP (ROCm 7.2.4, gfx1201, WSL2) on an RX 9070 XT 16 GB: main f0ab63a and this PR built (strata-poly, and
    poly_gemm_small_test in this PR's tree), 0 errors; poly_gemm_small_test all passed (bit for bit, both functions,
    the same product hash in all 6 shapes); --bench 3 runs and the prompt speed above (--serve, one GEN 1 request,
    fp16 KV, 16K context, the two builds' runs interleaved).

  • Head 576d251 (the review fixes, listed below): poly_gemm_small_test all passed on the RTX 2070 Super (sm_75,
    220 ok lines, 0 FAIL; --bench 3 runs, 0 FAIL), including the new checks: input counts that are not a multiple of
    16 for the 16-input tiles (2,008 -> 100 and 7 -> 64
    inputs -> outputs), a second engine's merged write (31 lines, the first's picks kept), no temporary file left,
    and the unwritable cache under a regular file. HIP build (ROCm, strata-poly and poly_gemm_small_test): 0 errors.

  • Head 576d251 end to end on an RTX 3090 24 GB (sm_86, autotune): IQ4_NL and UD-Q2_K_XL prompt speed above, main f0ab63a
    and this head, 3 rounds, all experts in VRAM both ways.

Review fixes in head 576d251
  • Cache writes and the writability probe use a temporary file of their own: the session files' hidden unique name
    (temp_name()), created with O_CREAT|O_EXCL|O_NOFOLLOW (CREATE_NEW on Windows), then renamed. The helpers
    moved from conversation_file.cpp to strata/core/temp_file.hpp, so both use one copy. No shared path.tmp.
  • A write keeps the card's lines for shapes and buckets it did not time.
  • gemm_f32_tiles_use() merges by shape and bucket under a per-card lock; gemm_f32_tiles_pick() takes it too.
  • Timing: 3 untimed calls per option, each sample at least 0.5 ms of calls, a launch error drops that option, and a tile is
    kept only where it beats gemm_f32() by 2% or more.
  • A failure while timing gives the cost model, frees the buffers, and writes nothing.
  • STRATA_POLY_F32_AUTOTUNE: 0, off, no or false turn it off; other values leave it on. Register lines fixed.

Failed:

  • none observed

Not run:

  • RDNA3 and other AMD cards; NVIDIA cards other than the RTX 2070 Super, RTX 3060, RTX 3090 and RTX 2080 Ti.
  • IQ4_NL prompt speed on the RX 9070 XT on a quiet card (the desktop PC's runs are too noisy to use).
  • The autotune on the RTX 3060 and RTX 2080 Ti (their kernel and profile rows are the fixed rule's).
  • Head 576d251 on the RX 9070 XT (built for HIP, not run there) and its end-to-end speed there (its picks can differ
    from f675573's where a tile was within 2% of gemm_f32()).

Limits

  • Close to speed-neutral end to end: at most +1.6% prompt tok/s in the runs above, because these products are 2-3%
    of a prompt's time.
  • The first load on a card spends about 0.35 to 0.9 s timing the tiles (once; cached in PolyStrata-data/cache/f32-tiles.txt,
    else the user's cache folder; STRATA_POLY_F32_TUNE_CACHE names another file).
  • Inkling 2 only, although the bits would be the same for any model.

Credit: ported from our Inkling engine in #200.

🤖 Generated with Claude Code

…tiles, loaded two steps ahead

Inkling 2 Mini's prompt batches send its controllers' down products (4,608 inputs to 64 outputs) and its routers
(2,304 to 128) through gemm_f32(). gemm_f32_tiles() takes one group of 33 to 256 outputs in gemm_ahead_kernel: a
template on the block's and a thread's tokens and outputs that reads a step's inputs and weights into registers two
steps before it works on them and stores them so that a warp's writes fill the 32 banks, in the largest of four tiles
that still gives half the SMs a block. Other shapes go to gemm_f32().

Each output is still one fmaf after the other in the inputs' order, so the bits are gemm_f32()'s: poly_gemm_small_test
checks both against the host on Inkling 2 Mini's shapes too, and its --bench times both and prints a hash of each
product. Only Inkling 2 takes it (Engine::f32_tiles_); the other families' products are unchanged. CUDA and HIP;
Metal keeps its own gemm_f32.

Ported from our Inkling engine (simplexai-labs#200, a34789a), adapted to main's gemm_f32().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…per card

gemm_f32_tiles() took the largest of four tiles that still gave half the SMs a block. That was tuned on an RTX 2070
Super (40 SMs) and does not carry over: on an RTX 3060 (28 SMs) the router at 1,024 rows got 16 blocks of 64 x 128 and
was 22% slower than gemm_f32(), and on RDNA (where one block alone waits on its loads) the rule's tiles were up to 2.5x
slower than the best one, and slower than gemm_f32() on several shapes.  No count the runtime reports predicts it:
filling every SM to its occupancy limit made Turing 20-59% slower.

So at load Inkling 2 times its two shapes (routers, controllers' down products) in each way: the four tiles and
gemm_f32() itself, at row buckets of 32, 48, 64 ... up to the prompt batch (4,096 at most), one warm-up round and 5
rounds of about 0.5 ms of back-to-back calls, the median.  The picks go to a cache file keyed by the card (name, SMs,
clock, driver, runtime) and this build, PolyStrata-data/cache/f32-tiles.txt beside the model (else the user's cache
folder; STRATA_POLY_F32_TUNE_CACHE names another), so later loads take them without timing and every run of a setup
picks the same.  STRATA_POLY_F32_AUTOTUNE=0, a cache that cannot be written or no VRAM to time in: a cost model
(n_in x the larger of a block's time alone and the busiest SM's blocks one after the other) on Turing and Blackwell,
where it picked the fastest way on every shape, and gemm_f32() on every other card, so the fallback is never slower
than without the tiles.

Every way sums each output with one fmaf after the other in the inputs' order: the same bits whatever the pick.
poly_gemm_small_test checks every way against the host, that the autotuned picks are valid, a cache round trip, the
cost model behind an unwritable cache, and the fallback's choice for the card's family; --bench times every way, simplexai-labs#224's first rule, the pick and the model.
Inkling 2 only (Engine::f32_tiles_); the other families' products are unchanged.  CUDA and HIP.

Builds on VecSzn's gemm_f32() on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cks, a lock, steadier timing

simplexai-labs#224's review:
- The cache is written, and probed for writability, through a temporary file of the call's own: the session files'
  hidden unique name beside the destination, created with O_CREAT|O_EXCL|O_NOFOLLOW (CreateFileW CREATE_NEW on
  Windows), then renamed over it. temp_name(), random64() and wide() move from conversation_file.cpp to
  strata/core/temp_file.hpp so both use one copy; the probe makes and removes such a file and never truncates a
  shared name, so two processes no longer race on path.tmp.
- A write keeps the card's lines for shapes and buckets it did not time (an engine with other shapes or a smaller
  prompt batch wrote them), and gemm_f32_tiles_use() merges by shape and bucket under a per-card lock that
  gemm_f32_tiles_pick() takes too: two engines on one card in one process no longer replace each other's picks or
  race on them. The SM count and the cost model's family are per card and atomic.
- Timing: 3 untimed calls of every way first (module load, a cold card), then 4 calls to size each sample to at least
  0.5 ms (up to 1,024 calls), then the 5 rounds. A launch error (cudaGetLastError after each batch) drops that way. A
  tile is kept only where its median beats gemm_f32()'s by 2% or more; of equal tiles the larger.
- A failure while timing (an error the runtime returns) gives the cost model for every shape not from the cache, the
  buffers and events freed, and nothing written.
- STRATA_POLY_F32_AUTOTUNE: 0, off, no or false (any case) turn it off; any other value leaves it on (before, any
  non-number turned it off). The register's lines and parse types fixed.
- poly_gemm_small_test: K tails for the 16-input tiles (2,008 inputs -> 100, 7 -> 64), a merged write from a second
  engine, no temporary file left, and the unwritable cache under a regular file (works on Windows too), in the
  system's temp folder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@okada-shogo
okada-shogo marked this pull request as ready for review November 23, 2026 22:20
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: larger tiles for prompt F32 products (controllers, routers) [Engine] Inkling 2: faster kernel for the prompt's F32 products (loop controllers, routers), picked by autotune Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: faster kernel for the prompt's F32 products (loop controllers, routers), picked by autotune [Engine] Inkling 2: faster kernel for the prompt's F32 products (loop controllers, routers), tile picked by autotune at load (same bits) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: faster kernel for the prompt's F32 products (loop controllers, routers), tile picked by autotune at load (same bits) [Engine] Inkling 2: autotuned kernel for prompt F32 products (controllers, routers) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: autotuned kernel for prompt F32 products (controllers, routers) [Engine] Inkling 2: autotuned kernel for prompt F32 products 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