Repository navigation
[Engine] Inkling 2: prompt flash attention picks its block size by the last wave, same bits - #262
Merged
Merged
Conversation
okada-shogo
force-pushed
the
port/flash-fill
branch
from
November 23, 2026 13:48
5da532f to
a547e4a
Compare
…last wave ink_flash_kernel without LEAN took 64 query rows a block, so a 1,024-row call of Inkling 2 Mini is 128 blocks (16 x 8 key heads) at one block a multiprocessor. On an RTX 5080 Laptop (60 multiprocessors) that is 2.13 waves, the third with 8 blocks: the tail the grid-fill audit found. ink_flash_kernel<G, LEAN, TOKENS>: without LEAN the rows a block are now 64, 48 or 32, and ink_attend_batch() picks them per call: the size whose launch ends soonest by its waves (blocks over the resident slots, rounded up, times a block's time against one of 64: 0.9 for 48, 0.65 for 32, measured on the 5080 Laptop). Another size than 64 is taken only when that is at least 5% less, so cards whose last wave is full or nearly so keep 64. For 1,024-row calls that moves to 48 on 59 to 63, 88 to 127 and 176 to 199 multiprocessors. On the 5080 Laptop one layer's attention is 4% to 16% faster (full 32K keys 12.85 -> 11.64 ms, window 0.84 -> 0.73 ms), and the flash kernel's time in an 8K prompt 10.7% less. LEAN (sm_80), STRATA_FAST_ATTN's kernel, Turing's ink_flash75_kernel and HIP's kernel are unchanged. Class: same bits. A row's scores, weights and sums do not depend on which block it is in: its key tiles still start at multiples of 64 and go in the same order, and the tiles a wider block adds for the row's neighbours give it weights of 0 and a scale of 1 (or keep its zeros before its first key). poly_ink_flash_test now runs every case with each size and fails unless all give the same bits (fp16, FP8 and Q8_0 caches; 1,024-row prompt chunks added), and its --bench times each size (--rows N for other call sizes). STRATA_INK_FLASH_TOKENS=64/48/32 fixes the size (64 = main's shape); ink_attend_batch_rows() takes one for the test, and ink_attend_batch() keeps its signature (Metal defines its own). ink_flash_tokens() says which size a call gets (0 where the call goes to another kernel). The finding is the grid-fill audit's (candidate 6); the per-card rule follows simplexai-labs#233's launch-shape pattern. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo
force-pushed
the
port/flash-fill
branch
from
November 23, 2026 20:40
a547e4a to
b2e2a95
Compare
okada-shogo
marked this pull request as ready for review
November 23, 2026 20:41
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.
Same bits as main. No regression was measured on Turing, Ampere (RTX A5000) or Blackwell (RTX 5070). On Turing (RTX 2070) nothing changes by construction: the call goes to
ink_mma75_kernel/ink_flash75_kernel, whose SASS matches main's, and identity is SAME.What it speeds up: cards whose SM count leaves a ragged last wave for a 1,024-row chunk (about 60 SMs). The gain was measured on an RTX 5080 Laptop (60 SMs), against an older main (labelled below).
On every card: a prompt's last, shorter chunk can get a smaller block. On the A5000 a 256-row last chunk takes 32-row blocks and is 45–47% faster per call.
Problem and result
ink_flash_kernel(Inkling 2's prompt attention, sm_80+; without LEAN, so every card but sm_80) takes 64 query rows a block, so a 1,024-row prompt call is 128 blocks (16 × 8 key heads) at one block per multiprocessor. On an RTX 5080 Laptop (60 SMs) that is 2.13 waves, and the third wave has only 8 blocks. The grid-fill audit flagged this as the largest single fill loss on RTX 50 cards.The block size is now a template argument.
ink_flash_tokens()picks the size whose launch ends soonest, counted in waves. On the 5080 Laptop it picks 48 rows (176 blocks, 2.93 waves): one layer's attention is 4% to 16% faster. End to end, the 32K prompt is 7% faster. At 8K the measured +7% is mostly noise (see below). The 2K prompt is mostly expert copies over the bus and varies by ±12% from run to run.Head on an RTX 5070 (48 SMs), prompt tok/s, higher is better. main = parent f0ab63a. IQ4_NL, fp16 cache, 1,771 of 3,072 experts in VRAM in every run; mean of 3 interleaved runs. On 48 SMs the rule keeps 64 rows for full 1,024-row calls, and only a prompt's last, shorter chunk changes, so no change is expected:
Head on an RTX 5070, decode tok/s, higher is better (128 tokens written after each prompt, same runs; decode does not use this kernel):
Head on an RTX A5000 (sm_86, 64 SMs), prompt tok/s, higher is better. main = parent f0ab63a. IQ4_NL, fp16 cache, all 3,072 experts in VRAM; mean of 3 interleaved runs. On 64 SMs a 1,024-row chunk is exactly 2 full waves of 64-row blocks, so the rule keeps 64 there. Only the 32K prompt's last 256-row chunk changes (to 32 rows):
Head on an RTX A5000, decode tok/s, higher is better (128 tokens written after each prompt, same runs):
Head on an RTX A5000, one call (µs, lower is better):
poly_ink_flash_test --bench --rows 256, mean of 3 runs. "main" is 64 rows fixed (STRATA_INK_FLASH_TOKENS=64):First version on an RTX 5080 Laptop (60 SMs), prompt tok/s, higher is better. main = d1f711e. IQ4_NL, fp16 cache, 2,445 of 3,072 experts in VRAM in every run; mean of 5 interleaved runs:
First version on an RTX 5080 Laptop, decode tok/s, higher is better (128 tokens written after each prompt, same runs; decode does not use this kernel):
What this PR itself saves is the kernel time. Inside the engine (nsys, mean of 2 runs), the flash kernel's total drops by 5.3% at a 2K prompt (4 ms) and by 10.7% at 8K (44 ms of a 6.2 s prompt). So most of the +7% at 8K is this box's run-to-run spread, not this change. At 32K, attention is a larger share of the prompt, and all 5 runs of this PR are faster than all 5 of main. The runs are below.
Each run (tok/s)
Head on the RTX A5000, prompt tok/s, binaries of a547e4a and f0ab63a (sm_86, portable CPU code); round order alternates:
Head on the RTX A5000, decode tok/s, the same runs:
Head on the RTX 5070, prompt tok/s, binaries of a547e4a and f0ab63a (sm_120); round order alternates (main first in rounds 1 and 3):
Head on the RTX 5070, decode tok/s, the same runs:
Prompt tok/s, RTX 5080 Laptop 16 GB, IQ4_NL, binary of 37670fc; round order alternates (main first in rounds 1, 3, 5):
Decode tok/s, the same runs:
An earlier 3-round run of the same comparison (binary of an earlier revision whose rule made the same choices on this card: 48 rows for every call of these prompts), prompt tok/s:
Decode tok/s, that run:
Kernel times (µs, lower is better)
Head on the RTX 5070, one call:
poly_ink_flash_test --bench [--rows 256], best of 5 calls a run, mean of 3 runs. "main" is the same binary with 64 rows fixed (STRATA_INK_FLASH_TOKENS=64), whose SASS is main's. µs, lower is better:On the 5070 the rule's choices hold. At 1,024 rows, forcing 48 or 32 would be slower: full layer at 32K keys, 14945 µs with 48 rows and 15504 µs with 32, against 12816 µs with 64. At 256 rows, 48 is fastest.
Head on the RTX 5070, in the engine: total
ink_flash_kerneltime per prompt under nsys 2026.5.2, 2 runs each. ms, lower is better:The tables below are the first version on the RTX 5080 Laptop.
In the engine: total time of
ink_flash_kernelin one prompt undernsys profile --trace=cuda(2026.5.2). Values are ms, lower is better. RTX 5080 Laptop, IQ4_NL, fp16 cache,--tokens N --max-new 1, 2 runs:The flash kernel is 10% of the prompt's kernel time at 2K and 16% at 8K on this card (doorbell kernels left out).
One call:
poly_ink_flash_test --bench [--rows 256], one call of Inkling 2 Mini's shape (24 query heads on 8 key heads, window 1,024), best of 5 calls a run, mean of 3 runs. "main" is the same binary with the block size fixed at 64 rows (STRATA_INK_FLASH_TOKENS=64): main's kernel, the same code. µs, lower is better:Each run of the 1,024-row full-layer calls (µs):
Each size forced, mean of 3 runs (µs, lower is better). Rows of 32 are slower than 64 at 1,024 rows on this card, so the rule does not take them there:
All per-run logs are listed under Validation.
Related discussion
poly_ink_flash_test).Approach
ink_flash_kernel<G, LEAN, TOKENS>: the rows a block are now a template argument. It defaults to main's value (32 with LEAN, 64 without), and without LEAN it can also be 48 or 32. The kernel body is [Engine] Inkling 2: flash attention: 32-token blocks on A100-class cards (sm_80), same bits #230's.<G, false, 64>against main's<G, false>and<G, true, 32>against<G, true>, for G = 1 to 4 on sm_86, sm_89 and sm_120. The 4 Turingink_flash75_kernels match too.ink_flash_tokens()picks a size per call:cudaOccupancyMaxActiveBlocksPerMultiprocessor), rounded up.STRATA_INK_FLASH_TOKENS=64/48/32fixes the size. 64 is main's shape, so it is the off switch. It is registered in the settings register asrollback.ink_attend_batch_rows(a, stream, tokens)takes a forced size, for the test.ink_attend_batch()keeps its signature, because Metal defines its own.poly_ink_flash_test([Engine] Inkling 2: flash attention: 32-token blocks on A100-class cards (sm_80), same bits #230's) now checks each case at every size, with fp16 caches and, with hashes only, FP8 and Q8_0 caches. It also has 1,024-row prompt-chunk cases and--bench [--rows N]per size.STRATA_FAST_ATTN'sink_fast_kernel, Turing'sink_flash75_kernel, HIP's RDNA4 kernel, Metal (its ownink_attend_batch(), same signature), and every other model.ink_flash_tokens()returns 0 when a call goes to one of those other kernels. The test then skips its size loop.Validation
Passed (head b2e2a95, on main 4664424):
75;86;89;120,strata-polyandpoly_ink_flash_test.python3 tools/settings_register.py --check: exit 0.python3 tools/check --only docs: passed.ink_mma75_kernel,ink_flash75_kernel) match main 4664424 instruction for instruction.ink_mma75_kernel/ink_flash75_kernel, whose SASS matches main's.poly_ink_flash_test: rc=0 for both. The output hashes are the same in 14 of 14 shared cases, including [Engine] Inkling 2: prompt attention on Turing in attn_mma_kernel's shape, 2.8 -> 1.0 ms a layer #278's split-call cases.ink_flash_tokens()returns 0 there, so no size loop runs.--tokens2,048 and 8,192 + 64 greedy,--top-probs 20, fp16 cache,STRATA_GPU_EXPERTS=850 STRATA_FREEZE_EXPERTS=1,--expert-swaps 0: ids and every top-20 line are the same, against main and against each forced size: 8 / 8 SAME. Both builds refuse--kv fp8on Turing.Passed (a547e4a, on main f0ab63a; the same change before the last rebase):
RB_MARCH=v3, both builds):poly_ink_flash_test: rc=0, 117 / 117 calls give the same bits at every size (fp16, FP8 and Q8_0 caches).--tokens2,048 and 8,192 + 64 greedy,--top-probs 20, frozen experts, fp16 and FP8 at 2,048: 11 / 11 SAME.poly_ink_flash_test: rc=0, 39 ok and 0 FAIL. Every call with each of 64, 48 and 32 rows gives the bits of the chosen size: 117 / 117 calls (13 cases × 3 sizes × fp16, FP8 and Q8_0 caches).--tokens2,048 and 8,192 + 64 greedy,--top-probs 20,STRATA_GPU_EXPERTS=1300 STRATA_FREEZE_EXPERTS=1,--expert-swaps 0, fp16 cache, plus FP8 at 2,048. Result: ids and every top-20 line are the same, against main and against each forced size: 11 / 11 SAME.--servewith one greedy request per run, 32K context, default flags, no expert profile. The 32K prompt is the 8,192-token test prompt repeated to 32,000 tokens.CMAKE_CUDA_ARCHITECTURES=75;86;89;120,strata-polyandpoly_ink_flash_test(local). main f0ab63a was built the same way.ink_flash_kernel<G, false, 64>against main's<G, false>and<G, true, 32>against<G, true>, for G = 1 to 4 on sm_86, sm_89 and sm_120: 24 of 24 the same. The 4 sm_75ink_flash75_kernels are the same too.python3 tools/settings_register.py --check: exit 0.python3 tools/check --only docs: 6 of 6 passed.ink_flash75_kernel, which this PR leaves alone.poly_ink_flash_test: rc=0 for both main f0ab63a and this head. Theink_flash75_kerneloutput hashes are the same in 11 of 11 shared cases.ink_flash_tokens()returns 0, so no size loop runs.--tokens2,048 and 8,192 + 64 greedy,--top-probs 20, fp16 cache,STRATA_GPU_EXPERTS=850 STRATA_FREEZE_EXPERTS=1,--expert-swaps 0: ids and every top-20 line are the same, main against this PR and against each forced size: 8 / 8 SAME. Turing has no FP8 cache, and both builds refuse--kv fp8there.Passed (first version 37670fc, on main d1f711e, the same rule and the same per-size kernel code then):
poly_ink_flash_testran 13 cases: groups of 1 to 4 query heads, full and window layers, ring caches, the gate, calls past position 0, and 1,024-row prompt chunks. The fp16 cases are checked against float64, and the same cases also run with FP8 caches. Every call with each of 64, 48 and 32 rows gives the bits of the chosen size: 78 / 78 calls, 0 failed.--tokens2,048 and 8,192 + 64 greedy,--top-probs 20, experts frozen (STRATA_FREEZE_EXPERTS=1,--expert-swaps 0; the same 2,445 experts in VRAM in every run), fp16 cache, plus FP8 cache at 2,048. Result: ids and every top-20 line are the same, main against this PR and against each forced size: 11 / 11 comparisons SAME.--servewith one greedy request per run, 32K context, default flags, no expert profile. The 32K prompt is the 8,192-token test prompt repeated to 32,000 tokens. Every run rc=0.ink_attend_batch()a third argument that Metal's definition lacks. The head keepsink_attend_batch()'s signature and addsink_attend_batch_rows().Failed:
Not run:
ink_attend_batch_rows(), which hands a size-free call toink_attend_batch()and returns false for a fixed size, andink_flash_tokens(), which returns 0.ink_attend_batch()'s signature, which Metal defines. The new functions are declared but never called on Metal.Limits
🤖 Generated with Claude Code