Repository navigation
[Engine] Inkling 2: prompt attention on Turing's tensor cores - #209
Open
okada-shogo wants to merge 1 commit into
Open
okada-shogo wants to merge 1 commit into
okada-shogo wants to merge 1 commit into
Conversation
ink_attend_batch() took no call below Ampere, so on Turing a prompt batch's attention went through attend_batch(), which writes every score out and reads it back. ink_flash_kernel needs cp.async, the m16n8k16 step and ~82 KB of shared memory, none of which Turing has. ink_flash75_kernel is the same attention in a shape that fits: 32 tokens of a key head a block, a warp 16 tokens of one query head, 32-key tiles in one buffer filled with plain copies, the blocks with the most keys first. Its scores, weights and sums are ink_flash_kernel's, in the same order (query and weights rounded to halves, the bias in floats, the softmax's extra term of score 0, the gate); only the tile bounds differ. The tensor-core helpers now build from sm_75, where the step is two m16n8k8 steps; on sm_80 and later they and ink_flash_kernel compile as before, and its dispatch is unchanged. FP8 caches stay Ampere+. HIP: no change. Class: same bits on Ampere and later; on Turing the attention's last bits change (same rounding points as attend_batch(), another summation order). poly_ink_flash_test checks ink_attend_batch(), ink_attend() and attend_batch() against float64 (groups of 1-4, full and window layers, ring caches, calls past position 0, short extents, a gate). RTX 2070 Super: flash max error 0.85-1.7e-3, attend_batch() 0.85-1.7e-3, ink_attend() <2e-6. One 1,024-row call (24 heads on 8): full layer 2K/8K/32K keys 2.98/9.95/38.9 ms vs attend_batch() 9.31/20.2/63.0 ms; window layer 2.83 vs 9.17 ms. The arithmetic is VecSzn's ink_flash_kernel; the Turing tile shape follows attn_mma_kernel from our earlier Inkling engine (simplexai-labs#200). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo
marked this pull request as ready for review
November 23, 2026 00:07
okada-shogo
added a commit
to okada-shogo/PolyStrata
that referenced
this pull request
Nov 23, 2026
…ing's tensor cores Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo
added a commit
to okada-shogo/PolyStrata
that referenced
this pull request
Nov 23, 2026
simplexai-labs#209 sends a Turing prompt batch to ink_flash75_kernel unless the caches are FP8; simplexai-labs#214 adds Q8_0 caches, which that kernel would read as halves (poly_inkling2_kvq8_test on an RTX 2070: ink_attend_batch rel rms nan). With --kv q8_0 on Turing the batch goes to attend_batch() again, simplexai-labs#214's checked path there. Only the combination of the two changes. 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. It adds a prompt-attention kernel that
runs on Turing's tensor cores.
Prompt +7% at 2K, +17% at 8K and +22% at 32K tokens on an 8 GB Turing card; decode unchanged.
On by default on Turing;
STRATA_INK_FLASH=0still selects the old path,attend_batch(). Against that path theoutput differs in the last bits (a rounding change, see Limits).
What it changes, in plain words: on Ampere and newer, a prompt's attention runs in VecSzn's
ink_flash_kernel,which uses the scores as it computes them. On Turing (sm_75),
ink_attend_batch()returns false, so prompt attentionfalls back to
attend_batch(), which writes every score to memory and reads it back. This PR addsink_flash75_kernel, a version ofink_flash_kernelthat fits Turing. Ampere and newer keepink_flash_kernel(sameSASS, dispatch unchanged). HIP is unchanged.
tok/s, mean of 3 interleaved runs. RTX 2070 8 GB + Ryzen 7 5800X, IQ4_NL, fp16 KV, default settings (no freeze, no profile,
937 of 3072 experts in VRAM at load in every run), one
--serverequest per run. main d1f711e vs this PR 105a9e1:Prompt speed, tok/s, higher is better, mean of 3 runs:
Decode speed, tok/s, higher is better, mean of 3 runs:
Each run (tok/s)
Prompt speed, tok/s, each run:
Decode speed, tok/s, each run:
Kernel time, in milliseconds (ms), lower is better. What it times: one prompt-attention call of 1,024 prompt rows for one layer (
poly_ink_flash_test --bench), RTX 2070 Super, 2 runs that agree within 1%. This is one piece of the prompt; the prompt tok/s table above is the end-to-end effect.attend_batch()(ms)Related discussion
Split out of #200, as the maintainer asked there:
#200 (comment)
Merge order with #229 (
STRATA_FAST_ATTN) and #230 (sm80 flash-kernel work): both carry a longerengine/src/poly/ink_flash_test.cu(it adds a bit hash of the output and--bench [keys]) and the same CMake lines,and touch the same
INK_FLASHblock andink_attend_batch()(inkling2.cu). Whichever lands second gets an add/addconflict in the test and a text conflict in inkling2.cu. If #229 / #230 land first, this PR is rebased onto their test
and keeps only its Turing lines (the header comment and the doc line in inkling2_kernels.hpp).
Approach
ink_flash_kernelusescp.asyncand the m16n8k16 step (sm80+) and needs ~82 KB of shared memory. Turing has nocp.async, runs m16n8k16 as two m16n8k8 steps, and has 64 KB per block.How the Turing kernel works
ink_flash_kernel), 32-key tiles in onebuffer filled with plain 16-byte copies. The blocks of the last tokens (the most keys) start first. ~28 KB shared
memory for groups of 3, 206-213 registers, no spills.
ink_flash_kerneldoes it, in the same order: query rounded to half, q.k infloats times the scale, bias row times table column in floats, minus the sink, weights rounded to half, an extra
softmax term of score 0, then the gate. Only the tile bounds differ, so the running max (and the weights rounded
against it) can differ in the last bits.
ink_mma/ink_ldsm4/ink_h2now build from sm_75 (INK_MMA); on sm_75ink_mmais two m16n8k8 steps. Onsm80+ they compile to the same code as before. The
cp.asynchelpers stay sm80+.ink_attend_batch(): PTX >= 80 ->ink_flash_kernel(unchanged); PTX 75..79 with fp16 caches ->ink_flash75_kernel; otherwise false. FP8 caches stay Ampere+.poly_ink_flash_test(run by hand, needs a card):ink_attend_batch(),ink_attend()andattend_batch()with an
AttnBiasagainst a float64 reference.--benchtimes one 1,024-row call at 2K/8K/32K keys.On Turing the result is lossy in the last bits against
attend_batch(): the same rounding points, in anothersummation order. Ampere and newer: same SASS, same dispatch. HIP: no change.
Validation
Passed:
CUDA 12.9 build, sm_75, GCC 11.4, at this commit:
strata-polyandpoly_ink_flash_testbuild.poly_ink_flash_test, RTX 2070 Super (sm_75): 11 cases (groups of 1-4, full and window layers, ring caches, callsthat start past position 0, short extent, few rows),
0 failed. Max error vs float64: flash 0.85e-3 to 1.73e-3,attend_batch()0.85e-3 to 1.73e-3 (the same size),ink_attend()< 2e-6, flash vsattend_batch()at most 4.0e-4.poly_ink_flash_test --bench: the kernel times above.SASS identity, CUDA 12.9:
nvcc -O3 -cubinofinkling2.cufrom main d1f711e and from this branch for sm_80, sm_86,sm_89 and sm_120,
cuobjdump -sassper function: all 81 of main'sink_*kernels are identical on every arch. Theonly new functions are the four
ink_flash75_kernelinstances.End-to-end speed, the table above: RTX 2070 8 GB (Bazzite box, Ryzen 7 5800X),
e2e-ab.sh(main and this PRinterleaved, order reversed every other round, one warm-up;
strata-poly --serve --prompt-cache 0 --conversation-cache-mib 0 --kv fp16, 302 + 256, 2,048 + 1, 8,192 + 256, 32,768 + 1 tokens; both builds CUDA 12.9,75;86). An earlier noisy two-round run on the same card pointed the same way.Output agreement, flash vs
STRATA_INK_FLASH=0(same binary), IQ4_NL, fp16 KV, 32 greedy tokens,--top-probs 20,then flash fed the
attend_batch()tokens (--follow). RTX 2080 Ti 11 GB (every expert in VRAM) and RTX 2070 8 GB.attend_batch()run twice gives the same bits on both cards.Flash (this PR alone) against
attend_batch()(STRATA_INK_FLASH=0) on the same card, IQ4_NL, fp16 KV, 32 greedytokens, one run each. KL in nats over the top 20, lower is better; "fed" means flash was given
attend_batch()'s tokens:KL against a reference card: main d1f711e on an RTX 3090 (sm_86, its own
ink_flash_kernel, every expert in VRAM),fed the same tokens as each Turing run.
Accuracy against the RTX 3090 reference, IQ4_NL, fp16 KV, 32 steps, one run each. KL(3090 || run) in nats over the
3090's top 20, lower is better; top-1 the same, of 32 steps, higher is better:
attend_batch()(main's Turing path)HIP build (compile only, no AMD card), built on top of [Engine] build Inkling 2's DSpark screen on HIP #206 (this head merged with b681c69), ROCm 7.2.4 container,
gfx1100;gfx1201:cmake -S engine -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DSTRATA_ENABLE_HIP=ON -DSTRATA_ENABLE_CUDA=OFF -DCMAKE_HIP_ARCHITECTURES="gfx1100;gfx1201" -DSTRATA_BUILD_TESTS=ON -DSTRATA_NATIVE_EXPERTS=ON -DCMAKE_HIP_COMPILER=/opt/rocm/llvm/bin/clang++ -DCMAKE_PREFIX_PATH=/opt/rocm && ninja -C build -k 0 strata-poly poly_ink_flash_test: both build, 0 errors.Build for sm_80+: an independent review built this head (105a9e1) with CUDA 12.9 for
75;80;86;89;120:strata-polyandpoly_ink_flash_testbuild.Failed: none observed.
Not run:
Limits
attend_batch()from the 3rd written token on. That step is sensitive. The3090 reference itself gives 0.81 / 0.19 to the two tokens there, and main's own Turing path picks the first token on
the 2080 Ti but the second on the 2070. Over the 32 steps, this PR is a little further from the 3090 than
attend_batch(): mean KL 0.21 vs 0.11 on the 2080 Ti and 0.33 vs 0.28 on the 2070, with medians equal or lower. At8K the two paths are equally close to the 3090. This is the same class of rounding change as main's sm80
ink_flash_kernelvsattend_batch(): queries and weights rounded to halves, in another summation order.this kernel there (valid, but not the Ampere kernel). The shipped arch lists never do this.
A variant capped at 2 blocks per SM may be faster; we have not tried it.
--kv q8_0): whichever of the two merges second must add!a.kvq8to this PR's Turinggate in
ink_attend_batch()(ptx >= 75 && !a.kv8 && !a.kvq8 && flash75(...)). Without it, Q8_0 caches go toink_flash75_kernel, which reads fp16 rows: wrong prompt attention with--kv q8_0on Turing(
poly_inkling2_kvq8_teston an RTX 2070:ink_attend_batchrel rms nan). The resolution is commit43fa074dc on fork branch
combined/ready.Credits: the arithmetic is VecSzn's
ink_flash_kernel; the Turing tile shape followsattn_mma_kernelfrom our earlierInkling engine (#200).
🤖 Generated with Claude Code