Skip to content

[Engine] Inkling 2: prompt attention on Turing's tensor cores - #209

Open
okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/attn-sm75
Open

okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/attn-sm75

Conversation

@okada-shogo

@okada-shogo okada-shogo commented Nov 22, 2026 •

Copy link
Copy Markdown

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=0 still selects the old path, attend_batch(). Against that path the
output 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 attention
falls back to attend_batch(), which writes every score to memory and reads it back. This PR adds
ink_flash75_kernel, a version of ink_flash_kernel that fits Turing. Ampere and newer keep ink_flash_kernel (same
SASS, 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 --serve request per run. main d1f711e vs this PR 105a9e1:

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

Card Model Measure main this PR alone Change
RTX 2070 8 GB IQ4_NL prompt 302 tokens 215.5 219.0 +1.7%
RTX 2070 8 GB IQ4_NL prompt 2K tokens 431.1 461.5 +7.0%
RTX 2070 8 GB IQ4_NL prompt 8K tokens 517.0 603.1 +16.7%
RTX 2070 8 GB IQ4_NL prompt 32K tokens 502.5 611.3 +21.7%

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

Card Model Measure main this PR alone Change
RTX 2070 8 GB IQ4_NL decode after a 302-token prompt 40.2 40.3 +0.2%
RTX 2070 8 GB IQ4_NL decode after an 8K-token prompt 30.7 30.8 +0.4%
Each run (tok/s)

Prompt speed, tok/s, each run:

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 8 GB IQ4_NL prompt 302 tokens main 215.5 215.7 215.2
RTX 2070 8 GB IQ4_NL prompt 302 tokens this PR alone 220.1 217.3 219.7
RTX 2070 8 GB IQ4_NL prompt 2K tokens main 432.6 430.9 429.9
RTX 2070 8 GB IQ4_NL prompt 2K tokens this PR alone 461.8 460.8 461.9
RTX 2070 8 GB IQ4_NL prompt 8K tokens main 516.1 518.1 516.8
RTX 2070 8 GB IQ4_NL prompt 8K tokens this PR alone 603.8 603.9 601.6
RTX 2070 8 GB IQ4_NL prompt 32K tokens main 503.5 501.2 502.8
RTX 2070 8 GB IQ4_NL prompt 32K tokens this PR alone 611.9 611.9 610.1

Decode speed, tok/s, each run:

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 8 GB IQ4_NL decode after a 302-token prompt main 40.1 40.2 40.3
RTX 2070 8 GB IQ4_NL decode after a 302-token prompt this PR alone 40.7 40.2 39.9
RTX 2070 8 GB IQ4_NL decode after an 8K-token prompt main 30.7 30.6 30.8
RTX 2070 8 GB IQ4_NL decode after an 8K-token prompt this PR alone 30.9 30.8 30.8

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.

Card Layer and keys main, attend_batch() (ms) this PR alone (ms) Change
RTX 2070 Super full-attention layer, 2,048 keys 9.39 ms 2.98 ms −68.3%
RTX 2070 Super full-attention layer, 8,192 keys 20.2 ms 10.0 ms −50.5%
RTX 2070 Super full-attention layer, 32,768 keys 63.1 ms 38.9 ms −38.4%
RTX 2070 Super sliding-window layer, any number of keys 9.2 ms 2.83 ms −69.2%

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 longer
engine/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_FLASH block and ink_attend_batch() (inkling2.cu). Whichever lands second gets an add/add
conflict 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_kernel uses cp.async and the m16n8k16 step (sm80+) and needs ~82 KB of shared memory. Turing has no
cp.async, runs m16n8k16 as two m16n8k8 steps, and has 64 KB per block.

How the Turing kernel works
  • 32 tokens of a key head per block, a warp 16 tokens of one query head (as in ink_flash_kernel), 32-key tiles in one
    buffer 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.
  • Each score, weight and sum is computed as ink_flash_kernel does it, in the same order: query rounded to half, q.k in
    floats 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_h2 now build from sm_75 (INK_MMA); on sm_75 ink_mma is two m16n8k8 steps. On
    sm80+ they compile to the same code as before. The cp.async helpers stay sm80+.
  • Dispatch in ink_attend_batch(): PTX >= 80 -> ink_flash_kernel (unchanged); PTX 75..79 with fp16 caches ->
    ink_flash75_kernel; otherwise false. FP8 caches stay Ampere+.
  • New test poly_ink_flash_test (run by hand, needs a card): ink_attend_batch(), ink_attend() and attend_batch()
    with an AttnBias against a float64 reference. --bench times 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 another
summation 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-poly and poly_ink_flash_test build.

  • poly_ink_flash_test, RTX 2070 Super (sm_75): 11 cases (groups of 1-4, full and window layers, ring caches, calls
    that 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 vs attend_batch() at most 4.0e-4.

  • poly_ink_flash_test --bench: the kernel times above.

  • SASS identity, CUDA 12.9: nvcc -O3 -cubin of inkling2.cu from main d1f711e and from this branch for sm_80, sm_86,
    sm_89 and sm_120, cuobjdump -sass per function: all 81 of main's ink_* kernels are identical on every arch. The
    only new functions are the four ink_flash75_kernel instances.

  • End-to-end speed, the table above: RTX 2070 8 GB (Bazzite box, Ryzen 7 5800X), e2e-ab.sh (main and this PR
    interleaved, 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 greedy
    tokens, one run each. KL in nats over the top 20, lower is better; "fed" means flash was given attend_batch()'s tokens:

    Prompt (tokens) Card Greedy tokens the same KL right after the prompt (nats) Top-1 the same when fed (of 32) KL max when fed (nats)
    8,192 RTX 2080 Ti 11 GB yes 1e-7 32 8e-6
    8,192 RTX 2070 8 GB yes 1e-7 32 3e-5
    2,048 RTX 2080 Ti 11 GB no, they split at token 3 1.2e-5 28 2.6
    2,048 RTX 2070 8 GB no, they split at token 3 1.5e-4 31 1.1
  • 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:

    Prompt (tokens) Card Measure main, attend_batch() (main's Turing path) this PR alone
    8,192 RTX 2080 Ti 11 GB KL mean (nats) 1.6e-6 1.7e-6
    8,192 RTX 2080 Ti 11 GB top-1 the same (of 32) 32 32
    8,192 RTX 2070 8 GB KL mean (nats) 2.0e-6 1.6e-6
    8,192 RTX 2070 8 GB top-1 the same (of 32) 32 32
    2,048 RTX 2080 Ti 11 GB KL mean (nats) 0.11 0.21
    2,048 RTX 2080 Ti 11 GB KL median (nats) 1.5e-3 1.6e-3
    2,048 RTX 2080 Ti 11 GB KL max (nats) 2.0 3.6
    2,048 RTX 2080 Ti 11 GB top-1 the same (of 32) 30 28
    2,048 RTX 2070 8 GB KL mean (nats) 0.28 0.33
    2,048 RTX 2070 8 GB KL median (nats) 6.2e-4 2.9e-4
    2,048 RTX 2070 8 GB KL max (nats) 5.2 5.3
    2,048 RTX 2070 8 GB top-1 the same (of 32) 29 28
  • 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-poly and poly_ink_flash_test build.

Failed: none observed.

Not run:

  • No end-to-end run on an Ampere or newer card with this branch. The sm_80+ path is unchanged (same SASS, see above).
  • No run on an AMD card (HIP is compile only; nothing in this PR runs there).

Limits

  • The 2K prompt's greedy text differs from attend_batch() from the 3rd written token on. That step is sensitive. The
    3090 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. At
    8K the two paths are equally close to the 3090. This is the same class of rounding change as main's sm80
    ink_flash_kernel vs attend_batch(): queries and weights rounded to halves, in another summation order.
  • Turing only, fp16 caches only. FP8 caches still need Ampere+.
  • The gate is the PTX version, not the card: a build with only compute_75 PTX, JIT-compiled on an Ampere card, would run
    this kernel there (valid, but not the Ampere kernel). The shipped arch lists never do this.
  • Long full layers gain less (1.6x at 32K keys). The kernel uses 206-213 registers, so one block of 6 warps fits per SM.
    A variant capped at 2 blocks per SM may be faster; we have not tried it.
  • Merge hazard with [Engine] Inkling 2: --kv q8_0, an 8-bit attention cache without FP8 hardware #214 (--kv q8_0): whichever of the two merges second must add !a.kvq8 to this PR's Turing
    gate in ink_attend_batch() (ptx >= 75 && !a.kv8 && !a.kvq8 && flash75(...)). Without it, Q8_0 caches go to
    ink_flash75_kernel, which reads fp16 rows: wrong prompt attention with --kv q8_0 on Turing
    (poly_inkling2_kvq8_test on an RTX 2070: ink_attend_batch rel rms nan). The resolution is commit
    43fa074dc on fork branch combined/ready.

Credits: the arithmetic is VecSzn's ink_flash_kernel; the Turing tile shape follows attn_mma_kernel from our earlier
Inkling engine (#200).

🤖 Generated with Claude Code

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
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>
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.

1 participant