Skip to content

[Engine] Inkling 2: prompt attention on Turing in attn_mma_kernel's shape, 2.8 -> 1.0 ms a layer - #278

Merged
VecSzn merged 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/flash75-mma
Nov 23, 2026
Merged

VecSzn merged 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/flash75-mma

Conversation

@okada-shogo

Copy link
Copy Markdown
Contributor

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 adds ink_mma75_kernel, which uses the tile shape of attn_mma_kernel from #200 and computes the position bias on the tensor cores. It is the new default on Turing. STRATA_INK_MMA75=0 goes 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 --serve request per run.

Prompt speed, tok/s, higher is better, mean of 3 interleaved runs:

Card Model Measure main (tok/s) this PR alone (tok/s) Change
RTX 2070 Super 8 GB IQ4_NL prompt 302 206.3 206.8 +0.3%
RTX 2070 Super 8 GB IQ4_NL prompt 2K 478.7 489.0 +2.2%
RTX 2070 Super 8 GB IQ4_NL prompt 8K 591.1 624.3 +5.6%
RTX 2070 Super 8 GB IQ4_NL prompt 32K 586.2 643.0 +9.7%
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 302 238.0 239.5 +0.6%
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 2K 597.9 620.6 +3.8%
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 8K 604.8 641.5 +6.1%
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 32K 567.9 622.3 +9.6%

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:

Card Model Measure main (tok/s) this PR alone (tok/s) Change
RTX 2070 Super 8 GB IQ4_NL decode after 302 40.4 39.9 −1.3%
RTX 2070 Super 8 GB IQ4_NL decode after 2K 35.6 35.8 +0.5%
RTX 2070 Super 8 GB IQ4_NL decode after 8K 33.1 33.0 −0.3%
RTX 2070 Super 8 GB IQ4_NL decode after 32K 22.4 22.6 +1.0%
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 302 43.8 43.7 −0.1%
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 2K 42.5 41.9 −1.4%
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 8K 38.8 38.6 −0.4%
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 32K 27.8 28.0 +0.5%

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:

Card Reference (greedy answers) Measure main (#209 kernel) this PR alone
RTX 2070 8 GB main's default (#209 kernel) top-1 same, % of 576 steps 100.0% (it is the reference) 98.3%
RTX 2070 8 GB main's default (#209 kernel) KL mean (nats) 0 (it is the reference) 0.024
RTX 2070 8 GB attend_batch() (STRATA_INK_FLASH=0) top-1 same, % of 576 steps 98.8% 98.4%
RTX 2070 8 GB attend_batch() (STRATA_INK_FLASH=0) KL mean (nats) 0.034 0.050
RTX 2070 8 GB noise floor: main run again on the same tokens top-1 same, % of 576 steps 100.0% -
Each run (tok/s)

Prompt speed, tok/s, each run (order swapped every round):

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL prompt 302 main 202.9 208.3 207.7
RTX 2070 Super 8 GB IQ4_NL prompt 302 this PR alone 204.2 208.6 207.7
RTX 2070 Super 8 GB IQ4_NL prompt 2K main 477.7 478.8 479.7
RTX 2070 Super 8 GB IQ4_NL prompt 2K this PR alone 487.8 490.1 489.2
RTX 2070 Super 8 GB IQ4_NL prompt 8K main 590.5 591.8 590.9
RTX 2070 Super 8 GB IQ4_NL prompt 8K this PR alone 624.3 624.5 624.1
RTX 2070 Super 8 GB IQ4_NL prompt 32K main 586.8 586.1 585.6
RTX 2070 Super 8 GB IQ4_NL prompt 32K this PR alone 642.9 643.9 642.3
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 302 main 236.0 237.8 240.1
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 302 this PR alone 238.7 241.7 238.1
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 2K main 596.8 598.3 598.6
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 2K this PR alone 618.3 622.6 620.8
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 8K main 602.7 604.8 606.8
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 8K this PR alone 640.2 642.0 642.3
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 32K main 566.8 568.3 568.7
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 32K this PR alone 623.4 621.9 621.7

Decode speed, tok/s, each run:

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL decode after 302 main 40.2 40.4 40.7
RTX 2070 Super 8 GB IQ4_NL decode after 302 this PR alone 39.3 40.5 39.9
RTX 2070 Super 8 GB IQ4_NL decode after 2K main 34.8 36.1 35.9
RTX 2070 Super 8 GB IQ4_NL decode after 2K this PR alone 35.7 36.0 35.6
RTX 2070 Super 8 GB IQ4_NL decode after 8K main 33.0 33.2 33.1
RTX 2070 Super 8 GB IQ4_NL decode after 8K this PR alone 32.9 33.0 33.1
RTX 2070 Super 8 GB IQ4_NL decode after 32K main 22.5 22.4 22.3
RTX 2070 Super 8 GB IQ4_NL decode after 32K this PR alone 22.8 22.5 22.6
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 302 main 43.5 43.9 43.9
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 302 this PR alone 43.7 43.8 43.7
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 2K main 42.4 42.5 42.6
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 2K this PR alone 41.9 41.8 42.0
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 8K main 38.7 38.8 38.9
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 8K this PR alone 38.4 38.7 38.8
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 32K main 27.7 27.7 28.1
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 32K this PR alone 27.8 27.8 28.3
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 and attend_batch()'s at 567. main's own #209 kernel matches attend_batch() at 569. The whole job was run twice and gave the same bits.

Card Model Prompt (tokens) Reference Measure main (#209 kernel) this PR alone
RTX 2070 8 GB IQ4_NL 302 main (#209) top-1 same, % of 64 100.0% 100.0%
RTX 2070 8 GB IQ4_NL 302 main (#209) KL mean (nats) 0 0.0020
RTX 2070 8 GB IQ4_NL 302 attend_batch() top-1 same, % of 64 100.0% 100.0%
RTX 2070 8 GB IQ4_NL 302 attend_batch() KL mean (nats) 0.0011 0.0022
RTX 2070 8 GB IQ4_NL 2,048 main (#209) top-1 same, % of 64 100.0% 96.9%
RTX 2070 8 GB IQ4_NL 2,048 main (#209) KL mean (nats) 0 0.0745
RTX 2070 8 GB IQ4_NL 2,048 attend_batch() top-1 same, % of 64 96.9% 95.3%
RTX 2070 8 GB IQ4_NL 2,048 attend_batch() KL mean (nats) 0.0887 0.1907
RTX 2070 8 GB IQ4_NL 8,192 main (#209) top-1 same, % of 64 100.0% 100.0%
RTX 2070 8 GB IQ4_NL 8,192 main (#209) KL mean (nats) 0 0.0000
RTX 2070 8 GB IQ4_NL 8,192 attend_batch() top-1 same, % of 64 100.0% 100.0%
RTX 2070 8 GB IQ4_NL 8,192 attend_batch() KL mean (nats) 0.0000 0.0000
RTX 2070 8 GB UD-Q2_K_XL 302 main (#209) top-1 same, % of 64 100.0% 100.0%
RTX 2070 8 GB UD-Q2_K_XL 302 main (#209) KL mean (nats) 0 0.0010
RTX 2070 8 GB UD-Q2_K_XL 302 attend_batch() top-1 same, % of 64 100.0% 100.0%
RTX 2070 8 GB UD-Q2_K_XL 302 attend_batch() KL mean (nats) 0.0009 0.0011
RTX 2070 8 GB UD-Q2_K_XL 2,048 main (#209) top-1 same, % of 64 100.0% 96.9%
RTX 2070 8 GB UD-Q2_K_XL 2,048 main (#209) KL mean (nats) 0 0.0509
RTX 2070 8 GB UD-Q2_K_XL 2,048 attend_batch() top-1 same, % of 64 100.0% 96.9%
RTX 2070 8 GB UD-Q2_K_XL 2,048 attend_batch() KL mean (nats) 0.0134 0.0633
RTX 2070 8 GB UD-Q2_K_XL 8,192 main (#209) top-1 same, % of 64 100.0% 100.0%
RTX 2070 8 GB UD-Q2_K_XL 8,192 main (#209) KL mean (nats) 0 0.0000
RTX 2070 8 GB UD-Q2_K_XL 8,192 attend_batch() top-1 same, % of 64 100.0% 100.0%
RTX 2070 8 GB UD-Q2_K_XL 8,192 attend_batch() KL mean (nats) 0.0001 0.0000
RTX 2070 8 GB IQ3_S 302 main (#209) top-1 same, % of 64 100.0% 93.8%
RTX 2070 8 GB IQ3_S 302 main (#209) KL mean (nats) 0 0.0025
RTX 2070 8 GB IQ3_S 302 attend_batch() top-1 same, % of 64 98.4% 100.0%
RTX 2070 8 GB IQ3_S 302 attend_batch() KL mean (nats) 0.0034 0.0033
RTX 2070 8 GB IQ3_S 2,048 main (#209) top-1 same, % of 64 100.0% 96.9%
RTX 2070 8 GB IQ3_S 2,048 main (#209) KL mean (nats) 0 0.0868
RTX 2070 8 GB IQ3_S 2,048 attend_batch() top-1 same, % of 64 93.8% 93.8%
RTX 2070 8 GB IQ3_S 2,048 attend_batch() KL mean (nats) 0.2016 0.1903
RTX 2070 8 GB IQ3_S 8,192 main (#209) top-1 same, % of 64 100.0% 100.0%
RTX 2070 8 GB IQ3_S 8,192 main (#209) KL mean (nats) 0 0.0000
RTX 2070 8 GB IQ3_S 8,192 attend_batch() top-1 same, % of 64 100.0% 100.0%
RTX 2070 8 GB IQ3_S 8,192 attend_batch() KL mean (nats) 0.0000 0.0000

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.

Card Layer and keys #209 kernel (µs) this PR alone (µs) Change
RTX 2070 Super 8 GB sliding-window layer, 2,048 keys 2,837 1,013 −64.3%
RTX 2070 Super 8 GB sliding-window layer, 8,192 keys 2,830 1,012 −64.3%
RTX 2070 Super 8 GB sliding-window layer, 32,768 keys 2,844 1,011 −64.5%
RTX 2070 Super 8 GB full-attention layer, 2,048 keys 2,954 1,195 −59.6%
RTX 2070 Super 8 GB full-attention layer, 8,192 keys 9,947 4,618 −53.6%
RTX 2070 Super 8 GB full-attention layer, 32,768 keys 38,870 18,071 −53.5%

Each run:

Card Layer and keys Kernel Run 1 (µs) Run 2 (µs) Run 3 (µs)
RTX 2070 Super 8 GB sliding-window layer, 2,048 keys #209 (ink_flash75_kernel) 2,832 2,830 2,848
RTX 2070 Super 8 GB sliding-window layer, 2,048 keys this PR alone (ink_mma75_kernel) 1,013 1,009 1,017
RTX 2070 Super 8 GB sliding-window layer, 8,192 keys #209 (ink_flash75_kernel) 2,834 2,833 2,824
RTX 2070 Super 8 GB sliding-window layer, 8,192 keys this PR alone (ink_mma75_kernel) 1,017 1,008 1,010
RTX 2070 Super 8 GB sliding-window layer, 32,768 keys #209 (ink_flash75_kernel) 2,847 2,858 2,827
RTX 2070 Super 8 GB sliding-window layer, 32,768 keys this PR alone (ink_mma75_kernel) 1,006 1,009 1,018
RTX 2070 Super 8 GB full-attention layer, 2,048 keys #209 (ink_flash75_kernel) 2,969 2,941 2,953
RTX 2070 Super 8 GB full-attention layer, 2,048 keys this PR alone (ink_mma75_kernel) 1,196 1,194 1,194
RTX 2070 Super 8 GB full-attention layer, 8,192 keys #209 (ink_flash75_kernel) 9,939 9,979 9,924
RTX 2070 Super 8 GB full-attention layer, 8,192 keys this PR alone (ink_mma75_kernel) 4,615 4,622 4,616
RTX 2070 Super 8 GB full-attention layer, 32,768 keys #209 (ink_flash75_kernel) 38,843 38,910 38,858
RTX 2070 Super 8 GB full-attention layer, 32,768 keys this PR alone (ink_mma75_kernel) 18,179 18,023 18,012

For comparison, #200's attn_mma_kernel took 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_kernel to 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:

What ink_mma75_kernel does:

Class: rounding (fp16 MMA, fp32 accumulate), the same class as #209. Ampere and newer: same SASS. HIP: no change.

Validation

Passed:

  • Builds, CUDA 12.9, sm_75, GCC 11.4, at this commit: strata-poly and poly_ink_flash_test. No spills: STACK 0, LOCAL 0 for all four ink_mma75_kernel instances (cuobjdump --dump-resource-usage).
  • poly_ink_flash_test, RTX 2070 (sm_75), at this commit. Run with the default (ink_mma75_kernel) and with STRATA_INK_MMA75=0 (ink_flash75_kernel): 0 failed both ways.
    • 14 cases against float64: groups of 1-4, full and window layers, ring caches, windows of 20 to 1,024, calls past position 0, short extent, few rows. Max error 1.73e-3 for both kernels, the same as attend_batch().
    • 11 split-call cases: the same rows in one call, then in 2-4 calls run last first, which moves rows to other block, warp and row slots for G = 1-4. Every output float has the same bits, with equal output hashes. This is the cross-call test.
    • The STRATA_INK_MMA75=0 output 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).
  • End-to-end speed, the tables above: 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.
  • Accuracy, the table above: m75-acc.sh on an RTX 2070 8 GB, main 5b080f3 and this kernel, run twice with the same bits. Also checked on f0ab63a90 (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.
  • Off switch: STRATA_INK_MMA75=0 gives 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.
  • SASS identity, CUDA 12.9: nvcc -O3 -cubin of inkling2.cu from main and from this branch for sm_80, sm_86, sm_89 and sm_120, cuobjdump -sass per function. All 126 of main's functions are identical on every arch. The only new functions are the 4 ink_mma75_kernel instances, which are empty there.
  • HIP compile (ROCm 7.2.4 container, gfx1100;gfx1201): inkling2.cu and ink_flash_test.cu build, 0 errors.
  • python tools/settings_register.py --check: passes. STRATA_INK_MMA75 is registered as a rollback switch, and docs/SETTINGS.md was regenerated.
  • An independent review of the kernel and test found no correctness bugs. Its two test findings are fixed in this commit: G = 2 and G = 4 splits that did not move rows, and missing window cases for G = 2 and 4.

Failed: none observed.

Not run:

  • No end-to-end run on another Turing card (RTX 2080 Ti, Quadro). The accuracy check ran on an RTX 2070 8 GB, the speed runs on an RTX 2070 Super.
  • No KL against an RTX 3090 / A100 all-experts-in-VRAM reference for this revision. The references here are main's two Turing paths on the same card.
  • No run on an Ampere-or-newer or AMD card. Those paths are unchanged (same SASS; HIP compile only).

Limits

Credits: the tile shape and the bias on the tensor cores are attn_mma_kernel from our earlier Inkling engine (#200: 7f3f457, 16f3585, dbd4227, c69df6a). The arithmetic is VecSzn's ink_flash_kernel, and the kernel this replaces is #209's ink_flash75_kernel.

🤖 Generated with Claude Code

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>
@okada-shogo
okada-shogo marked this pull request as ready for review November 23, 2026 18:11
@VecSzn
VecSzn merged commit 311c11b into simplexai-labs:main Nov 23, 2026
2 checks passed
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.

2 participants