[PR #10] [MERGED] Prompt attention on the tensor cores for Turing: the context in the accumulator registers (58368 bytes) #277

Closed
opened 2026-10-29 21:23:42 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/10
Author: @dummerjindabin
Created: 10/8/2026
Status: ✅ Merged
Merged: 10/8/2026
Merged by: @mw00

Base: main ← Head: turing-tensor-core-prefill


📝 Commits (1)

  • 1b97b71 Prompt attention on the tensor cores for Turing: the context stays in the accumulator registers

📊 Changes

1 file changed (+34 additions, -19 deletions)

View changed files

📝 src/kernels/cuda/glm_batch.cu (+34 -19)

📄 Description

The second half of #8, as you asked. The crash fix (the FP16 latents) is already in v1.0.7, so this is one commit, not two - the reason for the split was "ship the crash fix first if the tensor-core change needs more checking", and that is what happened.

The problem

mla_attn_tc_kernel asked for 91,136 bytes of dynamic shared memory a block (sO 32 KB + sQ 16 KB + sL 32 KB + sPart 8 KB + sP 1 KB) against Turing's 65,536 ceiling. The opt-in failed, tc_ok went -1, and sm_75 silently ran the F32 kernel, so the faster prefill never reached RTX 20-series.

The change

The context O moves out of shared memory into the wmma accumulator fragments:

  • a warp owns four 16-wide column tiles for the whole cell walk (TC_OT = KV / 16 / 8; eight warps x 4 tiles x 16 = 512 = KV, so the tiles cover the context exactly once);
  • the per-chunk online-softmax rescale multiplies the fragments in place. In the m16n16k16 f32 accumulator layout, lane l holds rows l>>2 in x[0],x[1],x[4],x[5] and rows (l>>2)+8 in x[2],x[3],x[6],x[7] - that is where the per-row factors go;
  • the tiles are stored into ctx at the end, and a __syncthreads() before the row normalization makes sure every warp's tiles are there first (the F32 kernel normalizes by row).

Shared memory a block: 91,136 -> 58,368 bytes (16x512x2 + 32x512x2 + 4x16x32x4 + 16x32x2). That still needs the opt-in, but 58,368 <= 65,536, so Turing grants it - and on Volta (96 KB) the kernel is unchanged, with more room left over.

Measured here

2x Titan RTX (sm_75, driver 610.57.04, CUDA 13.0 build), Maya-S IQ2_XXS, 96K context, one 18,476-token prompt, greedy. The engine logs which prompt kernel it picked - glm_batch: CUDA0 prompt attention on the F32 cores vs ... on the tensor cores - so the A/B below is not a guess about what ran:

prefill engine's own prompt figure decode
STRATA_GLM_PREFILL_ATTN=f32 (what Turing ran before this patch) 62 s 3.4 ms/tok (~294 tok/s) 12.6 tok/s
this kernel 58 s 3.1 ms/tok (~323 tok/s) 11.3 tok/s
  • +7..10% prefill, which is inside this box's run-to-run spread (±15%): on Turing the long-prompt prefill is bound by fetching experts, not by attention. Your V100 A/B is the one that will show the real difference.
  • STRATA_GLM_PREFILL_ATTN_CHECK=1 (run on top of v1.0.8), over all 110 chunk checks: rel L2 5.3e-5 .. 1.6e-4 (median 1.1e-4), worst head 2.5e-4 .. 1.3e-3 (median 6.4e-4). Your V100 run quoted rel L2 ~7e-5 / worst head ~4e-4: the medians sit in the same band, the tail here is a little wider.
  • The prompt's three codes, buried at ~2k / 9k / 17k tokens, came back 3/3, and the whole run has no invalid argument / illegal memory access / planned 0 of ... routed rows lines. Before v1.0.7 the same prompt never computed attention at all.

To reproduce: run the same prompt twice, once with STRATA_GLM_PREFILL_ATTN=f32 and once without it (or use --bench), add STRATA_GLM_PREFILL_ATTN_CHECK=1 for the numeric comparison, and grep the engine log for prompt attention on the, glm stat: and attention check.

What I could not test

There is no sm_70 card here, so the V100 run you described is still the gate. The register rescale uses the same m16n16k16 f32 accumulator layout on sm_70 and sm_75, but I have only verified sm_75. I did not add a test: STRATA_GLM_PREFILL_ATTN_CHECK=1 is the comparison this kernel needs, and the repo's parity tests are CPU/HIP-side. I also left CHANGELOG.md alone, since releases look like your call.

Rebase against #7

I merged #7 into this branch to check the rebase you mentioned: the conflicts are CMakeLists.txt, README.md and src/kernels/cuda/glm_batch.cu - and merging #7 into plain main (83f0fbb) gives exactly the same three, so this patch adds no new conflict surface. The only place they meet is the TC kernel, which #7 wraps in #if !defined(STRATA_USE_HIP) (it is CUDA-only there anyway); if #7 lands first, this is a two-line rebase.

Raw evidence (engine log and journal, 2x Titan RTX)
# which prompt kernel each run picked (one line per GPU)
f32 run      : glm_batch: CUDA0 prompt attention on the F32 cores
f32 run      : glm_batch: CUDA1 prompt attention on the F32 cores
CHECK run    : glm_batch: CUDA0 prompt attention on the tensor cores
default run  : glm_batch: CUDA0 prompt attention on the tensor cores
default run  : glm_batch: CUDA1 prompt attention on the tensor cores

# prefill progress, journalctl --user -u maya, same 18,476-token prompt
f32 run      : reading the prompt: 6,272 of 18,476 tokens, 27 s so far
f32 run      : reading the prompt: 9,408 of 18,476 tokens, 37 s so far
f32 run      : reading the prompt: 9,408 of 18,476 tokens, 47 s so far
f32 run      : reading the prompt: 15,680 of 18,476 tokens, 55 s so far
f32 run      : reading the prompt: 18,475 of 18,476 tokens, 62 s so far
default run  : reading the prompt: 6,272 of 18,476 tokens, 25 s so far
default run  : reading the prompt: 9,408 of 18,476 tokens, 34 s so far
default run  : reading the prompt: 12,544 of 18,476 tokens, 44 s so far
default run  : reading the prompt: 15,680 of 18,476 tokens, 51 s so far
default run  : reading the prompt: 18,475 of 18,476 tokens, 58 s so far

# the two runs' stat lines (prompt = ms per prompt token)
f32 run      : glm stat: decode 79.14 ms/tok (12.64 tok/s) | ... | prompt 3.4 ms/tok | vram 5070/5179 ram 5241/5326
default run  : glm stat: decode 88.75 ms/tok (11.27 tok/s) | ... | prompt 3.1 ms/tok | vram 5071/5179 ram 5308/5392

# numeric check, 110 lines: the first two, the widest rel L2 and the widest worst head
glm_batch attention check: 256 tokens, rel L2 8.173e-05, worst head 2.806e-04
glm_batch attention check: 256 tokens, rel L2 6.957e-05, worst head 3.661e-04
glm_batch attention check: 206 tokens, rel L2 1.610e-04, worst head 9.349e-04
glm_batch attention check: 256 tokens, rel L2 1.577e-04, worst head 1.272e-03

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/mw00/project-maya/pull/10 **Author:** [@dummerjindabin](https://github.com/dummerjindabin) **Created:** 10/8/2026 **Status:** ✅ Merged **Merged:** 10/8/2026 **Merged by:** [@mw00](https://github.com/mw00) **Base:** `main` ← **Head:** `turing-tensor-core-prefill` --- ### 📝 Commits (1) - [`1b97b71`](https://github.com/mw00/project-maya/commit/1b97b71ec856c3db33d5c1cd38627f5e50786bf7) Prompt attention on the tensor cores for Turing: the context stays in the accumulator registers ### 📊 Changes **1 file changed** (+34 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `src/kernels/cuda/glm_batch.cu` (+34 -19) </details> ### 📄 Description The second half of #8, as you asked. The crash fix (the FP16 latents) is already in v1.0.7, so this is **one commit, not two** - the reason for the split was "ship the crash fix first if the tensor-core change needs more checking", and that is what happened. ## The problem `mla_attn_tc_kernel` asked for **91,136 bytes** of dynamic shared memory a block (sO 32 KB + sQ 16 KB + sL 32 KB + sPart 8 KB + sP 1 KB) against Turing's **65,536** ceiling. The opt-in failed, `tc_ok` went -1, and sm_75 silently ran the F32 kernel, so the faster prefill never reached RTX 20-series. ## The change The context O moves out of shared memory into the **wmma accumulator fragments**: - a warp owns four 16-wide column tiles for the whole cell walk (`TC_OT = KV / 16 / 8`; eight warps x 4 tiles x 16 = 512 = KV, so the tiles cover the context exactly once); - the per-chunk online-softmax rescale multiplies the fragments in place. In the m16n16k16 f32 accumulator layout, lane `l` holds rows `l>>2` in `x[0],x[1],x[4],x[5]` and rows `(l>>2)+8` in `x[2],x[3],x[6],x[7]` - that is where the per-row factors go; - the tiles are stored into `ctx` at the end, and a `__syncthreads()` before the row normalization makes sure every warp's tiles are there first (the F32 kernel normalizes by row). Shared memory a block: **91,136 -> 58,368 bytes** (16x512x2 + 32x512x2 + 4x16x32x4 + 16x32x2). That still needs the opt-in, but 58,368 <= 65,536, so Turing grants it - and on Volta (96 KB) the kernel is unchanged, with more room left over. ## Measured here 2x Titan RTX (sm_75, driver 610.57.04, CUDA 13.0 build), Maya-S IQ2_XXS, 96K context, one **18,476-token** prompt, greedy. The engine logs which prompt kernel it picked - `glm_batch: CUDA0 prompt attention on the F32 cores` vs `... on the tensor cores` - so the A/B below is not a guess about what ran: | | prefill | engine's own prompt figure | decode | |---|---|---|---| | `STRATA_GLM_PREFILL_ATTN=f32` (what Turing ran before this patch) | 62 s | 3.4 ms/tok (~294 tok/s) | 12.6 tok/s | | this kernel | 58 s | 3.1 ms/tok (~323 tok/s) | 11.3 tok/s | - **+7..10% prefill**, which is inside this box's run-to-run spread (±15%): on Turing the long-prompt prefill is bound by fetching experts, not by attention. Your V100 A/B is the one that will show the real difference. - `STRATA_GLM_PREFILL_ATTN_CHECK=1` (run on top of v1.0.8), over all 110 chunk checks: `rel L2 5.3e-5 .. 1.6e-4` (median 1.1e-4), `worst head 2.5e-4 .. 1.3e-3` (median 6.4e-4). Your V100 run quoted rel L2 ~7e-5 / worst head ~4e-4: the medians sit in the same band, the tail here is a little wider. - The prompt's three codes, buried at ~2k / 9k / 17k tokens, came back **3/3**, and the whole run has no `invalid argument` / `illegal memory access` / `planned 0 of ... routed rows` lines. Before v1.0.7 the same prompt never computed attention at all. To reproduce: run the same prompt twice, once with `STRATA_GLM_PREFILL_ATTN=f32` and once without it (or use `--bench`), add `STRATA_GLM_PREFILL_ATTN_CHECK=1` for the numeric comparison, and grep the engine log for `prompt attention on the`, `glm stat:` and `attention check`. ## What I could not test There is no sm_70 card here, so the V100 run you described is still the gate. The register rescale uses the same m16n16k16 f32 accumulator layout on sm_70 and sm_75, but I have only verified sm_75. I did not add a test: `STRATA_GLM_PREFILL_ATTN_CHECK=1` is the comparison this kernel needs, and the repo's parity tests are CPU/HIP-side. I also left `CHANGELOG.md` alone, since releases look like your call. ## Rebase against #7 I merged #7 into this branch to check the rebase you mentioned: the conflicts are `CMakeLists.txt`, `README.md` and `src/kernels/cuda/glm_batch.cu` - and merging #7 into plain `main` (83f0fbb) gives exactly the same three, so **this patch adds no new conflict surface**. The only place they meet is the TC kernel, which #7 wraps in `#if !defined(STRATA_USE_HIP)` (it is CUDA-only there anyway); if #7 lands first, this is a two-line rebase. <details> <summary>Raw evidence (engine log and journal, 2x Titan RTX)</summary> ``` # which prompt kernel each run picked (one line per GPU) f32 run : glm_batch: CUDA0 prompt attention on the F32 cores f32 run : glm_batch: CUDA1 prompt attention on the F32 cores CHECK run : glm_batch: CUDA0 prompt attention on the tensor cores default run : glm_batch: CUDA0 prompt attention on the tensor cores default run : glm_batch: CUDA1 prompt attention on the tensor cores # prefill progress, journalctl --user -u maya, same 18,476-token prompt f32 run : reading the prompt: 6,272 of 18,476 tokens, 27 s so far f32 run : reading the prompt: 9,408 of 18,476 tokens, 37 s so far f32 run : reading the prompt: 9,408 of 18,476 tokens, 47 s so far f32 run : reading the prompt: 15,680 of 18,476 tokens, 55 s so far f32 run : reading the prompt: 18,475 of 18,476 tokens, 62 s so far default run : reading the prompt: 6,272 of 18,476 tokens, 25 s so far default run : reading the prompt: 9,408 of 18,476 tokens, 34 s so far default run : reading the prompt: 12,544 of 18,476 tokens, 44 s so far default run : reading the prompt: 15,680 of 18,476 tokens, 51 s so far default run : reading the prompt: 18,475 of 18,476 tokens, 58 s so far # the two runs' stat lines (prompt = ms per prompt token) f32 run : glm stat: decode 79.14 ms/tok (12.64 tok/s) | ... | prompt 3.4 ms/tok | vram 5070/5179 ram 5241/5326 default run : glm stat: decode 88.75 ms/tok (11.27 tok/s) | ... | prompt 3.1 ms/tok | vram 5071/5179 ram 5308/5392 # numeric check, 110 lines: the first two, the widest rel L2 and the widest worst head glm_batch attention check: 256 tokens, rel L2 8.173e-05, worst head 2.806e-04 glm_batch attention check: 256 tokens, rel L2 6.957e-05, worst head 3.661e-04 glm_batch attention check: 206 tokens, rel L2 1.610e-04, worst head 9.349e-04 glm_batch attention check: 256 tokens, rel L2 1.577e-04, worst head 1.272e-03 ``` </details> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-29 21:23:42 +07:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mw00/project-maya#277
No description provided.