[PR #5467] [Engine] q36: flash attention on mma.m16n8k16 for the prompt (fp16 K/V) #5437

Open
opened 2026-11-07 16:35:12 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5467
Author: @bahooo22
Created: 11/7/2026
Status: 🔄 Open

Base: main ← Head: pr-fa-mma


📝 Commits (1)

  • 04d13e7 q36: FA-пилот — prompt attention на mma.m16n8k16 (fp16 K/V)

📊 Changes

4 files changed (+591 additions, -0 deletions)

View changed files

➕ docs/research/fa-attention.md (+46 -0)
➕ docs/research/fa-attention.ru.md (+45 -0)
📝 src/qwen35/q36_engine.cu (+75 -0)
➕ src/qwen35/q36_fa.cuh (+425 -0)

📄 Description

Problem and result

The q36 prompt attention went through cuBLAS: about 45,000 launches on a 96k prompt and an attention-score buffer for the whole prompt; that branch cost 61.5 s of the 103.2 s prompt (super-chunk N=4). After the change the 10 attention layers of the KAT geometry run one mma kernel per layer per chunk: no score buffer, online softmax in exp2, causality as a parameter (j <= p0+i), K/V in fp16. At 96k, N=4, KAT IQ4_XS, q4_0 KV: prompt 1341.1 tok/s against 1019.1 without FA (+31.6 %), attention 61,543 -> 40,946 ms (-33 %), GDN and MoE untouched. Opt-in (STRATA_Q36_FA_LAYER, default -1 = cuBLAS) because fp16 K/V changes the summation order.

CUDA prompt flash attention for q36 had no dedicated issue; the nearest is the HIP analogue #3482 (Q4_0-KV prompt attention on RDNA4 matrix cores). Tracking issue: https://github.com/Niko1221/Strata/issues/5465. Design and pilot gates are in docs/research/fa-attention.md (EN/RU), included in this PR. Scope agreement requested before merge (feature, new kernel).

Approach

  • src/qwen35/q36_fa.cuh (+425): k_fa_prefill on mma.m16n8k16, tile Cfg<Qs,Slice> (4x32/8x32/4x64/8x64); K and V share the staging buffer (41,216/65,536/64,256/94,720 B); 4x32 gives 2 blocks/SM on GA104.
  • src/qwen35/q36_engine.cu (+75): the hook in the prompt attention branch; STRATA_Q36_FA_LAYER (-1 off, a layer index, all), STRATA_Q36_FA_TILE, STRATA_Q36_FA_LOG, STRATA_Q36_FA_CHECK (rerun cuBLAS on the FA layer and compare per head).
  • docs/research/fa-attention.md (+46) and .ru.md (+45).
  • The cuBLAS branch stays the fallback for another geometry, KV type or a failed launch; nothing removed.

Validation

Passed (revision 04d13e7a, Windows 11, CUDA 13.3, sm_86, RTX 3060 Ti 8 GiB):

  • G1 (layer 3, T=2048 and two T=9): FA against cuBLAS, cos 1.000000, max_abs 8.1e-4...1.2e-3 against a 2e-3 tolerance.
  • G2 (4k, FA all, production path without FA_CHECK): greedy generation 8/8 equal to base, argmax 8/8; fc_rc=1 (summation order changed, expected).
  • G3 (96k, N=4, FA all): 1341.1 tok/s against 1019.1, tg 15.2 against 15.4; profile attention 61,543 -> 40,946 ms.
  • G4 (tile sweep): 4x32 best; 8x32/8x64 slower (one block per SM).

Failed: none observed. The 8x16/Q-ldg/qdouble/qregs variants were rejected in the pilot and are not part of this PR.

Not run: HIP, SYCL, Metal; other geometries (kernel runs only for H=16, HKV=2, HD=256, sm_80+); a bit-exact comparison (impossible by design with fp16 K/V). Draft until backends are covered.

Limits

The pilot covers the KAT geometry only; another geometry keeps the cuBLAS branch. fp16 K/V means the output differs from cuBLAS in the last bits (cos 1.0, max_abs within 2e-3).


🔄 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/Niko1221/Strata/pull/5467 **Author:** [@bahooo22](https://github.com/bahooo22) **Created:** 11/7/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr-fa-mma` --- ### 📝 Commits (1) - [`04d13e7`](https://github.com/Niko1221/Strata/commit/04d13e7aa660ef6af3e28ddd6ddb334211632495) q36: FA-пилот — prompt attention на mma.m16n8k16 (fp16 K/V) ### 📊 Changes **4 files changed** (+591 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `docs/research/fa-attention.md` (+46 -0) ➕ `docs/research/fa-attention.ru.md` (+45 -0) 📝 `src/qwen35/q36_engine.cu` (+75 -0) ➕ `src/qwen35/q36_fa.cuh` (+425 -0) </details> ### 📄 Description ## Problem and result The q36 prompt attention went through cuBLAS: about 45,000 launches on a 96k prompt and an attention-score buffer for the whole prompt; that branch cost 61.5 s of the 103.2 s prompt (super-chunk N=4). After the change the 10 attention layers of the KAT geometry run one mma kernel per layer per chunk: no score buffer, online softmax in exp2, causality as a parameter (j <= p0+i), K/V in fp16. At 96k, N=4, KAT IQ4_XS, q4_0 KV: prompt 1341.1 tok/s against 1019.1 without FA (+31.6 %), attention 61,543 -> 40,946 ms (-33 %), GDN and MoE untouched. Opt-in (STRATA_Q36_FA_LAYER, default -1 = cuBLAS) because fp16 K/V changes the summation order. ## Related discussion CUDA prompt flash attention for q36 had no dedicated issue; the nearest is the HIP analogue #3482 (Q4_0-KV prompt attention on RDNA4 matrix cores). Tracking issue: https://github.com/Niko1221/Strata/issues/5465. Design and pilot gates are in docs/research/fa-attention.md (EN/RU), included in this PR. Scope agreement requested before merge (feature, new kernel). ## Approach - src/qwen35/q36_fa.cuh (+425): k_fa_prefill on mma.m16n8k16, tile Cfg<Qs,Slice> (4x32/8x32/4x64/8x64); K and V share the staging buffer (41,216/65,536/64,256/94,720 B); 4x32 gives 2 blocks/SM on GA104. - src/qwen35/q36_engine.cu (+75): the hook in the prompt attention branch; STRATA_Q36_FA_LAYER (-1 off, a layer index, all), STRATA_Q36_FA_TILE, STRATA_Q36_FA_LOG, STRATA_Q36_FA_CHECK (rerun cuBLAS on the FA layer and compare per head). - docs/research/fa-attention.md (+46) and .ru.md (+45). - The cuBLAS branch stays the fallback for another geometry, KV type or a failed launch; nothing removed. ## Validation **Passed** (revision 04d13e7a, Windows 11, CUDA 13.3, sm_86, RTX 3060 Ti 8 GiB): - G1 (layer 3, T=2048 and two T=9): FA against cuBLAS, cos 1.000000, max_abs 8.1e-4...1.2e-3 against a 2e-3 tolerance. - G2 (4k, FA all, production path without FA_CHECK): greedy generation 8/8 equal to base, argmax 8/8; fc_rc=1 (summation order changed, expected). - G3 (96k, N=4, FA all): 1341.1 tok/s against 1019.1, tg 15.2 against 15.4; profile attention 61,543 -> 40,946 ms. - G4 (tile sweep): 4x32 best; 8x32/8x64 slower (one block per SM). **Failed:** none observed. The 8x16/Q-ldg/qdouble/qregs variants were rejected in the pilot and are not part of this PR. **Not run:** HIP, SYCL, Metal; other geometries (kernel runs only for H=16, HKV=2, HD=256, sm_80+); a bit-exact comparison (impossible by design with fp16 K/V). Draft until backends are covered. ## Limits The pilot covers the KAT geometry only; another geometry keeps the cuBLAS branch. fp16 K/V means the output differs from cuBLAS in the last bits (cos 1.0, max_abs within 2e-3). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
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
Niko1221/Strata#5437
No description provided.