Repository navigation
[Engine] Inkling 2: prompt attention on Turing in attn_mma_kernel's shape, 2.8 -> 1.0 ms a layer - #278
Merged
Conversation
ink_flash75_kernel (PR 209) takes 2.8 ms for one 1,024-row call of a sliding-window layer on an RTX 2070 Super, where attn_mma_kernel of our earlier Inkling engine (PR 200) takes about 1 ms over the same keys. The tensor-core steps are the same in both; the time goes to the work around them: - the relative-position bias: ink_flash75_kernel makes it a 16-term float dot product per score, read from shared memory as 4 float4s (16 FMAs and 4 LDS.128 for every score, every tile of a window layer, since its extent is the window); - the softmax in natural units (expf) and int64 masks on every score; - 213 registers and 6 warps a block: 1 block, 6 warps an SM, so the plain tile copies and the two barriers a tile are not hidden. ink_mma75_kernel is attn_mma_kernel's shape (7f3f457, 16f3585): 4 warps of 16 rows a block, rows interleaved token-major over the key head's G query heads, 32-key tiles counted from position 0. The bias is a product on the tensor cores: the warp's R (log2 units) times the table rows of the tile's distances, both split into a half and the half of what is left (hi*hi + lo*hi + hi*lo, about float rounding), read once a score from a per-warp float tile. The softmax runs in log2 units, and a tile that every row of a warp attends whole skips its masks. 219 registers, 27 KB shared memory: 2 blocks, 8 warps an SM. The query is rounded to halves unscaled, the sink is subtracted from each score, the weights are rounded to halves and summed as halves, the softmax has the term of score 0 and the gate comes last, as in ink_flash75_kernel. poly_ink_flash_test --bench, RTX 2070 Super, one 1,024-row call: window layer 2.84 -> 1.01 ms, full layer at 2K keys 2.95 -> 1.20 ms, 8K 9.95 -> 4.62 ms, 32K 38.9 -> 18.1 ms. Prompt tok/s (3 interleaved runs, IQ4_NL): +2.3% at 2K, +5.6% at 8K, +9.9% at 32K. Class: rounding (fp16 MMA, fp32 accumulate), the same class as 209. Upstream's accuracy check (1636925: 3 prompts of 302 to 8,192 tokens, 64 tokens, IQ4_NL, UD-Q2_K_XL loop and an IQ3_S; RTX 2070 8 GB, experts frozen) on main 5b080f3: the most likely token is 209's greedy answer at 566 of 576 steps (98.3%) and attend_batch()'s (STRATA_INK_FLASH=0) at 567 (98.4%), where 209's own kernel gives 569 (98.8%); two runs gave the same bits. It is the default on Turing; STRATA_INK_MMA75=0 goes back to ink_flash75_kernel (main's bits, checked on all 9 cases). sm_80+ and HIP are unchanged. poly_ink_flash_test gains a split-call test: the same rows in one call and in several (run last call first), every output bit equal; the splits move rows to other block, warp and row slots for G = 1 to 4, full and window layers, a window below the tile (11 cases). Window cases for G = 2 and 4 and a 20-position window are added to the float64 checks. Run it with and without STRATA_INK_MMA75=1 on Turing. Credits: the kernel shape and the bias on the tensor cores are attn_mma_kernel from our PR 200 (7f3f457, 16f3585, dbd4227, c69df6a); the arithmetic and the Turing kernel it replaces are VecSzn's ink_flash_kernel and PR 209's ink_flash75_kernel. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
For Turing cards (RTX 20 series, sm_75), while Inkling 2 reads a prompt. #209's Turing attention kernel (
ink_flash75_kernel) spends most of its time outside the tensor cores. This PR addsink_mma75_kernel, which uses the tile shape ofattn_mma_kernelfrom #200 and computes the position bias on the tensor cores. It is the new default on Turing.STRATA_INK_MMA75=0goes back to #209's kernel, with main's bits.Prompt +2% at 2K, +6% at 8K and +10% at 32K tokens on an RTX 2070 Super; decode unchanged. One layer's attention call is 2.8 ms → 1.0 ms (sliding-window layer) and 39 ms → 18 ms (full layer at 32K keys).
RTX 2070 Super 8 GB + Ryzen 5 3600XT, main f0ab63a vs this PR alone, main's default flags (prompt products as halves; Turing's int8 prompt products are opt-in upstream with
--prompt-int8, so they are off in both builds), fp16 KV, 32K context, no profile, one--serverequest per run.Prompt speed, tok/s, higher is better, mean of 3 interleaved runs:
Decode speed, tok/s, higher is better, mean of 3 interleaved runs (128 tokens written; 32 after the 32K prompt). Decode does not use this kernel:
Accuracy, separately (upstream's check from 1636925: 3 prompts of 302, 2,048 and 8,192 tokens, 64 tokens each, 3 files: IQ4_NL, UD-Q2_K_XL, an IQ3_S). Each engine is fed the reference's greedy tokens, and "top-1 same" counts the steps where its most likely token is the reference's. KL is KL(reference ‖ engine) over the reference's top 20, in nats (lower is better). The bar for a default change is 97.8% top-1:
attend_batch()(STRATA_INK_FLASH=0)attend_batch()(STRATA_INK_FLASH=0)Each run (tok/s)
Prompt speed, tok/s, each run (order swapped every round):
Decode speed, tok/s, each run:
Accuracy per file and prompt
RTX 2070 8 GB (Bazzite box), experts frozen (
STRATA_FREEZE_EXPERTS=1,--vram-reserve-mib 1024; 785 / 1,701 / 1,068 of 3,072 experts in VRAM for IQ4_NL / UD-Q2_K_XL / IQ3_S in every run of both builds), fp16 KV,--top-probs 20, 64 steps per case. Totals: this PR matches main's greedy answers at 566 of 576 steps andattend_batch()'s at 567. main's own #209 kernel matchesattend_batch()at 569. The whole job was run twice and gave the same bits.attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()attend_batch()Almost all of the misses are in the 2,048-token prompt, which has near-ties. main's own #209 kernel misses at the same places against
attend_batch().Kernel time (µs)
One prompt-attention call of 1,024 rows for one layer (
poly_ink_flash_test --bench), in µs, lower is better, mean of 3 interleaved runs. This is one piece of the prompt; the prompt tok/s table above is the end-to-end effect.Each run:
ink_flash75_kernel)ink_mma75_kernel)ink_flash75_kernel)ink_mma75_kernel)ink_flash75_kernel)ink_mma75_kernel)ink_flash75_kernel)ink_mma75_kernel)ink_flash75_kernel)ink_mma75_kernel)ink_flash75_kernel)ink_mma75_kernel)For comparison, #200's
attn_mma_kerneltook 0.96 ms per window-layer call inside the engine under nsys on the same card, against 2.85 ms for #209's kernel (one 2K prompt trace each).Related discussion
Split out of #200, as the maintainer asked there: #200 (comment). This is the follow-up to #209 (merged), which ported only the arithmetic of
ink_flash_kernelto Turing.Approach
Why #209's kernel is about 3× slower than #200's on the same keys (both skip keys outside the window, so the extra time is per tile). Kernel facts come from the nsys traces; per-score counts come from the code:
float4loads. That is 16 FMAs and 4 shared-memory loads per score. In a window layer the bias extent equals the window, so every tile pays it. poly: Inkling 2 Mini (inkling2 / inkling2_loop) #200 computes the bias as a tensor-core product (R × table rows of the tile's distances), so each score does one shared-memory load.expfin natural units, rounds each weight through a half, and checks the mask with 64-bit compares on every score.What
ink_mma75_kerneldoes:attn_mma_kernel). 4 warps of 16 query rows per block; rows interleaved token-major over the key head's query heads (21 tokens per block for Inkling 2 Mini's 3 heads per key head); 32-key tiles counted from position 0.exp2f), and no mask work on tiles that every row of a warp attends in full.Class: rounding (fp16 MMA, fp32 accumulate), the same class as #209. Ampere and newer: same SASS. HIP: no change.
Validation
Passed:
strata-polyandpoly_ink_flash_test. No spills: STACK 0, LOCAL 0 for all fourink_mma75_kernelinstances (cuobjdump --dump-resource-usage).poly_ink_flash_test, RTX 2070 (sm_75), at this commit. Run with the default (ink_mma75_kernel) and withSTRATA_INK_MMA75=0(ink_flash75_kernel):0 failedboth ways.attend_batch().STRATA_INK_MMA75=0output hashes equal [Engine] Inkling 2: prompt attention on Turing's tensor cores #209's.poly_ink_flash_test --bench, RTX 2070 Super: the kernel table above (3 rounds under the build lock).m75-e2e.sh, main and this PR interleaved, order swapped each round, one warm-up per build and model, all runs rc=0, live expert profiles unchanged.strata-poly --serve --prompt-cache 0 --conversation-cache-mib 0 --max-context 32768 --kv fp16, 302 + 128, 2,048 + 128, 8,192 + 128, 32,000 + 32 tokens. Experts in VRAM were the same in both builds.m75-acc.shon an RTX 2070 8 GB, main 5b080f3 and this kernel, run twice with the same bits. Also checked onf0ab63a90(this PR's base): main's 9 reference runs are byte-identical to 5b080f3's, and this PR's default reproduces the measured runs byte for byte.STRATA_INK_MMA75=0gives byte-identical ids and top-20 lines to main in all 9 cases (3 files × 3 prompts), at 5b080f3 and at f0ab63a. One f0ab63a run first loaded 3 fewer experts (1,065 of 3,072) and differed; rerun with the same placement, it was identical.nvcc -O3 -cubinofinkling2.cufrom main and from this branch for sm_80, sm_86, sm_89 and sm_120,cuobjdump -sassper function. All 126 of main's functions are identical on every arch. The only new functions are the 4ink_mma75_kernelinstances, which are empty there.inkling2.cuandink_flash_test.cubuild, 0 errors.python tools/settings_register.py --check: passes.STRATA_INK_MMA75is registered as a rollback switch, and docs/SETTINGS.md was regenerated.Failed: none observed.
Not run:
Limits
attend_batch()'s at 98.4%. [Engine] Inkling 2: prompt attention on Turing's tensor cores #209's kernel matchesattend_batch()at 98.8%. On an older main (2afe585, about 40 commits ago), the same check gave this kernel 97.1% against [Engine] Inkling 2: prompt attention on Turing's tensor cores #209's answers, 98.3% againstattend_batch()'s, and 97.6% for [Engine] Inkling 2: prompt attention on Turing's tensor cores #209 itself. The misses are near-ties in the 2K prompt, which any rounding change of the attention moves. If the maintainer wants a wider margin, keeping it opt-in is a one-line change inflash75()(STRATA_INK_MMA75=1to take it).Credits: the tile shape and the bias on the tensor cores are
attn_mma_kernelfrom our earlier Inkling engine (#200: 7f3f457, 16f3585, dbd4227, c69df6a). The arithmetic is VecSzn'sink_flash_kernel, and the kernel this replaces is #209'sink_flash75_kernel.🤖 Generated with Claude Code