Repository navigation
[Engine] Inkling 2: autotuned kernel for prompt F32 products - #224
Open
okada-shogo wants to merge 3 commits into
Open
okada-shogo wants to merge 3 commits into
okada-shogo wants to merge 3 commits into
Conversation
okada-shogo
force-pushed
the
port/gemm-f32
branch
from
November 23, 2026 10:48
2cc8c18 to
82349dc
Compare
…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>
okada-shogo
force-pushed
the
port/gemm-f32
branch
from
November 23, 2026 14:14
82349dc to
215e21d
Compare
…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
marked this pull request as ready for review
November 23, 2026 22:20
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
In a prompt batch, Inkling 2 Mini runs two F32 matrix products through
gemm_f32(): the loop controllers' downproducts (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, oneGEN 1request, fp16 KV; sameexperts in VRAM both ways):
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):
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
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.
75;86builds, the same method (e2e-nw.sh: one warm-uprequest, 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).
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--serveruns (GEN 1) without nsys, run in the same job.Where the prompt's time goes on main (% of the prompt's time under nsys; the three add up to 100%):
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):
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:
strata-poly --serve --prompt-cache 0 --conversation-cache-mib 0 --max-context 32768 --kv fp16; main d1f711e andthis 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 --benchWith 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:
Each run (µs), with the autotune:
#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.
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:
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
gemm_f32_tiles()takes one group of 33 to 256 outputs in a new kernel,gemm_ahead_kernel. Thekernel 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.fmafafter the other in input order, likegemm_small_kernelandgemm_tile_kernel. Every option (each tile andgemm_f32()) sums this way, so the pick cannot change the output.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.
(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.
STRATA_POLY_F32_AUTOTUNE=0), a cost model picks on Turing and Blackwell, andgemm_f32()itself is used on other cards, so the fallback is never slower than main.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).
Engine::f32_tiles_, set inInkling2::load_geometry()). Other families do not change. CUDA andHIP; Metal keeps its own
gemm_f32.poly_gemm_small_testchecks both functions against the host, bit for bit, and--benchtimes 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_testall passed (every option bit for bitagainst 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);
--bench3 runs on both (the autotune tables above); UD-Q2_K_XL prompt speed on the RX 9070 XT, main f0ab63a andthis 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-polyandpoly_gemm_small_testbuild at this commit.poly_gemm_small_test(both functions vs the hostfmachain, 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_f32andgemm_f32_tilesprintthe 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_kernelruns in place ofgemm_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, andpoly_gemm_small_testin this PR's tree), 0 errors;poly_gemm_small_testall passed (bit for bit, both functions,the same product hash in all 6 shapes);
--bench3 runs and the prompt speed above (--serve, oneGEN 1request,fp16 KV, 16K context, the two builds' runs interleaved).
Head 576d251 (the review fixes, listed below):
poly_gemm_small_testall passed on the RTX 2070 Super (sm_75,220 ok lines, 0 FAIL;
--bench3 runs, 0 FAIL), including the new checks: input counts that are not a multiple of16 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-polyandpoly_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
(
temp_name()), created withO_CREAT|O_EXCL|O_NOFOLLOW(CREATE_NEWon Windows), then renamed. The helpersmoved from
conversation_file.cpptostrata/core/temp_file.hpp, so both use one copy. No sharedpath.tmp.gemm_f32_tiles_use()merges by shape and bucket under a per-card lock;gemm_f32_tiles_pick()takes it too.kept only where it beats
gemm_f32()by 2% or more.STRATA_POLY_F32_AUTOTUNE:0,off,noorfalseturn it off; other values leave it on. Register lines fixed.Failed:
Not run:
from f675573's where a tile was within 2% of
gemm_f32()).Limits
of a prompt's time.
else the user's cache folder;
STRATA_POLY_F32_TUNE_CACHEnames another file).Credit: ported from our Inkling engine in #200.
🤖 Generated with Claude Code