mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3943] [CLOSED] [Engine] sm_6x prompt path: QSA attention without partials or the merge kernel, bitwise, prompt reads 2-3% faster on GP100 (opt-in, STRATA_PATTN_SM60=1) #4844
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4844
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/3943
Author: @ruibeikaa
Created: 10/24/2026
Status: ❌ Closed
Base:
main← Head:pattn-sm60📝 Commits (2)
66df26eqsa_pattn: the prompt path's QSA attention on sm_6x without partials or the merge kernel (STRATA_PATTN_SM60=1, opt-in)61b5185docs: OLDER_GPUS.md - STRATA_PATTN_SM60=1, the prompt path's QSA attention on sm_6x (opt-in)📊 Changes
6 files changed (+2226 additions, -0 deletions)
View changed files
📝
CMakeLists.txt(+8 -0)📝
docs/OLDER_GPUS.md(+7 -0)➕
include/strata/kernels/qsa_pattn.hpp(+45 -0)➕
src/kernels/cuda/qsa_pattn.cu(+1676 -0)➕
src/kernels/pattn_bench.cu(+479 -0)📝
src/prefill/prefill.cpp(+11 -0)📄 Description
Problem and result
Measured on PH402 SKU 200 boards: four GP100 dies, 32 GB HBM2 each (128 GB in all), every expert resident in VRAM, no CPU share, no PCIe share.
Below sm_70
qsa_prompt_attn_batchreturns false, soprefill.cppreads every QSA layer's attention through the decode kernels:qsa_decode_attn_batch, 32 queries a call, one CTA per (64-cell chunk, KV head, query) ofattn_chunk_kernel_pre75<1, 2>(#540's kernel), and every query's partials (33 chunks x 24 heads x 256 floats = 817 KB) written to the scratch and read back byattn_merge_kernel. For a 1,536-query prompt chunk that is 1.25 GB written and 1.25 GB read per layer. On a GP100 die both phases are bound by instruction issue, not by memory: the score phase spends ~150 instructions per cell (96 of them FFMA), mostly onreduce12's shuffles and the selects between them.With
STRATA_PATTN_SM60=1that loop is replaced byqsa_pattn_batch: a score kernel and a value kernel per batch of ~100 queries, in the same attention scratch, with no partials and no merge kernel. Every output byte isqsa_decode_attn_batch's. One GP100 die, a 1,536-query chunk, 12 QSA layers, recorded block selections, fixed-seed int8 K/V, ms per layer (this branch, 3 repetitions each):qsa_decode_attn_batch, merge v1 (default)STRATA_ATTN_MERGE_V2=1)STRATA_PATTN_SM60=1End to end on four GP100 dies, on a different revision (engine 0.1.47 plus my GP100 stack, details below), prompt reads took 1.8-3.2% less time from 7K to 150K tokens (8K -2.2%, 26K -3.0%, 33K -2.7%, an 88K append -3.1%, 150K -1.9% / -3.2% in two runs), decode was unchanged, and every reply hash was identical. Unset (the default) nothing changes.
Attention is only ~9-10% of a prompt read on this card (the experts and the QSA block selection, which this PR does not touch, are most of the rest), which is why a 30% faster attention gives 2-3% end to end.
Related discussion
No scope issue: an opt-in switch for one card family that leaves the default path alone. The bits are tied to #540's pre-Turing chunk kernel, which
qsa_decode_attn_batchruns on sm_6x in the experimental build. Same card and setup as #3455, #3752 and #3867; this change is independent of them and applies to plain main.Approach
src/kernels/cuda/qsa_pattn.cu,include/strata/kernels/qsa_pattn.hpp:pattn_mode()(readsSTRATA_PATTN_SM60once and says so on stderr) andqsa_pattn_batch().ps_score_x_kernel, one CTA per (chunk, KV head, query), 256 threads: q in registers, loaded once in a per-lane slot order; the chunk's 64 K rows (codes and scales) staged in shared memory with 16-byte loads; two cells at a time through one reduce-scatter. Inreduce12the two lanes of an exchange send different slots, which costs ~30 selects per cell; here the lane-dependent part is moved into which head a register slot holds (fixed when q is loaded) and which cell counts as the first, so every lane keeps and sends the same slots: 25 shuffles, 25 adds and 4 selects per pair of cells instead of 2 x (15 + 15 + ~30). Each level's value on lane L is still "its partial on L + its partial on L xor o", the pairing ofwarp_sum, and the per-lane 8-dimension products, the dequantization(float) code * scand* scaleare the decode kernel's expressions. The chunk softmax is the decode kernel's (warp_max(fmaxf(a, b)),__expf(s - m),warp_sum(ea + eb)). It writes the chunk's probabilities, cell rows, m and l to the scratch.ps_value_d4_kernel, one CTA per (KV head, query), 64 threads: 4 dims x 12 heads a thread, a cell's 4 V codes in one 32-bit load, the 12 weights in 3 broadcast float4 loads, the cells of a chunk folded in ascending order withfmafas the decode kernel does. The merge is done while folding, inattn_merge_kernel's operations and order: M = fmaxf over the chunks' m, then per chunk in order w =__expf(m - M), L = fmaf(l, w, L), acc = fmaf(acc_c, w, acc), skipping chunks with m == -FLT_MAX, and finally L > 0 ? acc / L : 0. Merge v1 and v2 give the same bits, so the output equals either.attn_batch x qsa_decode_attn_scratch_floats), so no VRAM is added.src/prefill/prefill.cpp: four lines in front of the existing 32-query loop; whenqsa_pattn_batchreturns false the loop runs as before.STRATA_EXPERIMENTAL_SM60(not HIP, not Metal; the call site is outside the Metal build). At run time only withSTRATA_PATTN_SM60=1, on a compute capability 6.x device, with int8 K and V (no q4 pools), and cap <= 2,112 (33 chunks). Anything else returns false and nothing is launched.ps_score_x_kernel128 registers, 20,992 B shared, no spill, 2 CTAs per SM;ps_value_d4_kernel143 registers, 8,388 B shared, no spill, 7 CTAs per SM.src/kernels/pattn_bench.cu(-DSTRATA_PATTN_BENCH=ON, one GPU, no model): replays aSTRATA_QSA_DUMPselection file with fixed-seed int8 K/V and queries, timesqsa_decode_attn_batchexactly asprefill.cppcalls it, and memcmps every candidate's output against it, all 12 layers.qsa_pattn.cualso keeps the other variants I measured (a per-chunk kernel, a fused persistent kernel, register and shared-memory score phases, staged V) as bench-only variants; I can drop them from the PR if you prefer only the default.docs/OLDER_GPUS.md: one bullet in the GP100 section.Validation
Environment note: measured only on PH402 SKU 200 boards - four GP100 dies of 48 SMs and 32 GB HBM2 each (128 GB in all), application clocks 715 / 1050 MHz - with every expert resident in VRAM (no CPU share, no PCIe share, no adaptive tier). The single-die numbers use one of those dies.
Rig: Windows 11, driver 581.80 (TCC), CUDA 12.9, VS 2022; build
-DSTRATA_EXPERIMENTAL_SM60=ON -DCMAKE_CUDA_ARCHITECTURES=60, ggml at the pinned 3cf03257f. Model: Swift 1.5 IQ3_S GGUF,--kv int8.Passed:
8183c6b6+ this PR), one GP100 die,STRATA_SCORES_THREAD=1 PATTN_SPLIT=1463:pattn_bench <dump> 3 attn 26k,88k,115k,s0,s2k s1463, once with merge v1 (the default) and once withSTRATA_ATTN_MERGE_V2=1. The dump holds the selections of 12 QSA layers of real prompt reads at 26K, 109K and 112K tokens (88K is the 109K record's blocks scaled; s0 / s2k are synthetic 0-1.5K and 1.5K-3K contexts, so a selection narrower than the cap, a partial last chunk and absent chunks are covered). Every case, both runs:parity split 1463 IDENTICAL (memcmp, 12 layers) (0 of 12 layers differ, 0 words), i.e. all 12 x 1,536 x 6,144 output floats equal toqsa_decode_attn_batch's, with merge v1 and with merge v2. The table above is from these two runs (the "with the switch" column from the merge v1 run; the merge v2 run gave the same times within 0.03 ms).PATTN_MASK_PAGES=3,100,2000,5000,6000,6400(those pages unresident, page -1), cases 26k and s0: IDENTICAL on both (26K: 44.60 -> 30.89 ms per layer; 0-1.5K: 18.50 -> 12.88).STRATA_EXPERIMENTAL_SM60(strata,pattn_bench), clean.011c9e60) plus about 35 opt-in GP100 commits of mine (#3455, #3752, a variant of #3867,--pipeline-windows 3and others not posted), Swift 1.5 IQ3_S, 1M context,--layer-split 12,24,36, MTP,--kv int8, conversation cache on, the launcher'sSTRATA_SCORES_THREAD=1 STRATA_ATTN_MERGE_V2=1, one fresh engine per arm, 13 requests per arm (five short prompts, 8K / 26K / 88K-token contexts with 300-400 tokens of decode, 1K / 2K / 6.9K / 30K reads, a 151K read with 800 tokens of decode). Two runs. Every reply hash with the switch equal to the same stack's without this change (13/13 in both runs; the same build with the switch off matched too) and the draft acceptance identical. Prompt read time, without -> with (ms; without = the mean of its two clean runs, with = run 2): 1K 2,370 -> 2,359, 2K 3,175 -> 3,161, 6.9K 9,371 -> 9,206, 8K 11,490 -> 11,241, 26K 27,026 -> 26,219, 33K 33,881 -> 32,956, 88K append 84,045 -> 81,478, 150K 142,379 -> 139,680 (run 1: 137,752). Decode within 0.1 ms per window. VRAM per die unchanged. In an arm with this switch and two other opt-in items on, the state hashes after every request (GDN, KV, index, PLE, draft-layer K/V; with forced wrong guesses), after 8K / 26K / 88K reads among others, were equal to the arm without them on 8 of 8 requests.Failed: none observed.
Not run:
STRATA_EXPERIMENTAL_SM60CUDA-only guard and the call site is outside the Metal build). An sm_86 build withoutSTRATA_EXPERIMENTAL_SM60(the new file then compiles only the two stub functions).qsa_decode_attn_batchon GP100 only.--kvmodes other than int8 (the switch declines them), contexts whose selection cap exceeds 2,112 (declined).Limits
STRATA_EXPERIMENTAL_SM60CUDA builds, int8 K/V, cap <= 2,112 only.pattn_benchneeds aSTRATA_QSA_DUMPfile with records at the contexts its cases name.CI note: the red
metal-linkcheck is not caused by this PR. The job fails while compiling upstream'smetal/src/mtl_rt.mm(lines 69 and 92:MTLGPUFamilyMetal4,MTLCompileOptions.mathMode,MTLMathModeFast/MTLMathModeSafe), which themacos-14runner's SDK does not provide. The workflow runs on any pull request that touchessrc/orinclude/; this PR changes no file undermetal/.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.