mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3903] [Engine] qwen35: decode attention that holds up at long context (--kv int8 / fp16 / k8v4, --gqa-attn, --attn-mma) #4821
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#4821
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/3903
Author: @xyehya
Created: 10/24/2026
Status: 🔄 Open
Base:
main← Head:pr/q36-02-decode-attn📝 Commits (6)
1883224[Engine] qwen35: accept Q4_K down-projection experts9903b66[Engine] qwen35: a parity case for the Q4_K down experts at FF 512aab82b7[Engine] qwen35: --kv int8 for the qwen35moe engine, and --tokens-file0a2b727[Engine] qwen35: grouped-query decode attention (#3641's kernel), --kv fp16 and k8v473ba60e[Engine] qwen35: int8 decode attention reads the cache through shared memory, one pass per verify windowefb3066[Engine] qwen35: --attn-mma, decode attention on tensor cores for the int8 cache (not bit-identical)📊 Changes
8 files changed (+1741 additions, -49 deletions)
View changed files
📝
CMakeLists.txt(+15 -0)📝
docs/QWEN36.md(+39 -1)📝
setup.py(+2 -2)📝
src/kernels/cuda/iq_kernels.cu(+3 -3)📝
src/kernels/native_expert_parity.cpp(+7 -2)➕
src/qwen35/q36_attn.cuh(+890 -0)📝
src/qwen35/q36_engine.cu(+285 -41)➕
tools/qwen35/q36_attn_parity.cu(+500 -0)📄 Description
Series (each stacked on the one before; review in order): #3699 → #3903 → #3904 → #3905 → #3906 → #3907 → #3908 → #3909 → #3910. Scope discussion: #3700. This is 2 of 9; it builds on #3699.
Branch
pr/q36-02-decode-attn, stacked onpr/q36-01-q4k-down.Problem and result
On the Qwen3.6 engine path the K/V cache is fp32 only (45 KB per token), and the decode attention reads each cache cell once per query head. On a 16 GB card that means two things:
--max-context 262144runs out of memory (#3303, #3700), and decode falls steeply as the context fills (#2464, wherek_attn_partis the profiled hot spot).This PR adds opt-ins; with none of them the engine runs as before:
--kv fp16 | int8 | k8v4: 22.5 / 12.7 / 9.5 KB per token and session instead of 45 KB. fp16 is the cache of #3641; int8 is q8_0 rows (the format proposed in #3700).--gqa-attn: the grouped-query decode attention of #3641 for the fp32 and int8 caches (a block per KV head reads each cell once for all its query heads), plus an int8 kernel that streams the cache through shared memory and serves a whole MTP verify window in one pass over the cache. Its partials have the same bits as the first int8 kernel's.--attn-mma(with--gqa-attn --kv int8, sm_80+; says so and is ignored otherwise): the same attention on the tensor cores. Not bit-identical to the default kernel (q, the softmax weights and the products are rounded to fp16).--tokens-file F:--tokensfrom a file, for prompts too long for an argument (documented indocs/QWEN36.md).Result on an RTX 5080 16 GB (same day, forced 256-token decode,
--mtp 0; every run is in Validation):--max-context 262144loads with--kv int8(82 of 256 experts per layer in VRAM, 7.74 GiB free after load). Level 01 at 262144 stops withcudaMalloc ... out of memory.--max-context 65536, level 01 against--kv int8 --gqa-attn: 4K 155.1 -> 190.8 tok/s (+23%), 16K 104.9 -> 167.6 (+60%). At 40K level 01 cannot be run from the CLI (the ids exceed the 128 KiB argument limit); on level 02 the fp32 path gives 57.0,--kv int8132.6, with--gqa-attn176.6.--attn-mmaon top: +1.6% / +2.5% / +2.4% at 4K / 16K / 40K (65K context).--attn-mmaagainst the default kernel; decode at 200K.Related discussion
--kv f16and the grouped-query kernel): the kernel here is theirs, ported to the int8 and k8v4 caches;--kv fp16is the same cache. If #3641 lands first this PR drops its copy of that kernel.--kv int8 / fp8 / q4): overlaps in the cache format and the--kvparsing. Whichever lands first, the other rebases; the int8 layouts differ (#3678: f16 scale per 64 values; here: q8_0, fp32 scale per 32).Approach
One cache layout per format, chosen by a template argument of the attention-prep and attention kernels, so the fp32 path runs the same kernels as before.
--kvtakes the main path's names. A value this path does not have (q4_0) is said and skipped in serve mode, as an unknown option is, and refused elsewhere.Compatibility effect: until now this engine skipped a config's
--kv; a hand-written config that carries--kv int8orfp16now gets that cache. Setup and the Terminal do not write--kvfor this family, so a default install is unaffected;setup.py's warning for--kvnow says setup does not write it for this family (it used to say the family does not use it).--attn-mmaand the shared-memory int8 kernel usecp.async/mma.sync, guarded for sm_80+ (the int8 shared-memory kernel falls back to the first int8 kernel on older cards) and compiled out for HIP.Documented in
docs/QWEN36.md("Opt-ins of this engine path"), with the test settings (STRATA_Q36_ATTN_OLD,STRATA_Q36_ATTN_ROWS,STRATA_Q36_GQA_FROM,STRATA_Q36_CACHE_WINDOW,STRATA_Q36_ATTN_MMA).Validation
Revision: branch
pr/q36-02-decode-attn(efb3066) onorigin/main5c8dd32(level 01 below it). Environment: Fedora 44 (Linux 7.2.11), CUDA 13.4.92 with g++-15 as host compiler, RTX 5080 16 GB (driver 615.78.08), Core Ultra 9 285K, 61 GB DDR5 (6400 MT/s configured), Sage-A1.5 Q4_K_M with the BF16ssm_alpha/ssm_betagraft (the file of #3699).Passed:
Build, CUDA sm_120 and "75;120" (commands as in the level-01 PR), targets
strata q36_attn_parity.q36_attn_parity(fp32, fp16, int8 and k8v4 cells against a double-precision reference; part_q8x against part_q8; part_q8m against the oracle): pass.Default path against level 01's binary, every flag off, fp32 cache:
STRATA_Q36_FIXED_SLOTS=1 strata --native $MODEL --max-context 65536 --prefill 2048 --pool-workers 16 --expert-slots 56 --mtp 0 --tokens <ids> --force <ids> --dump-full-logits OUT,cmpof the two dumps at a 4,096- and a 14,336-token prompt: IDENTICAL / IDENTICAL (64,563,200 bytes each, non-empty).Slot count, free-running (no
--expert-slots, no FIXED_SLOTS,--max-context 65536 --prefill 2048 --pool-workers 16 --mtp 0): 90 of 256 experts per layer in VRAM, as level 01. With--max-context 262144 --kv int8 --gqa-attn: 82 (fp32 does not fit at 262144, so there is nothing to compare it with).The new int8 kernel against the first one on the same binary (
STRATA_Q36_ATTN_OLD=1against unset,--kv int8 --gqa-attn, fixed 56 slots, 4K / 14K): logitscmp(--mtp 0) IDENTICAL / IDENTICAL; with--mtp 2and--mtp 3free greedy generation (160 tokens, 4K and 14K prompts;--dump-full-logitsand--forcedo not run the speculative path, so the verify windows need free generation;--mtp 3accepted 95 of 192 drafts) the generated ids are identical in all four runs. Commands:scratch-stack1/ab.shandab_gen.sh(kept with the report).--attn-mmaon a card below sm_80, or without--gqa-attn --kv int8, says so and is ignored (code read; the sm_75 build compiles it).Timing (passed as measured; not a gate). Same box and day (2026-10-23), the branch's binary built from
efb3066(level 01's from9903b66), one coldstrataprocess per run under the GPU lock, arms alternated A-B-A-B, two runs each. Command, with$P= prompt ids of the given depth and$G= the forced text:(level 01 has no
--tokens-file: its ids go inline, so it reaches 16K but not 40K.) tg = decode tok/s, individual runs, then the mean:--kv int8--kv int8 --gqa-attn--kv int8 --gqa-attn --attn-mma--kv int8 --gqa-attn--kv int8 --gqa-attn --attn-mmaLevel 02 with no flag decodes like level 01 (155.2 against 155.1 tok/s at 4K, the same 90 slots), and its prompt read is unchanged by the flags (4K: 5,360 (level 01), 5,358 (level 02
--kv int8 --gqa-attn) tok/s; 40K: 4,552). Raw result lines and stderr are kept with the bench notes.Failed: none observed.
Not run:
--attn-mmaagainst the default kernel, and against the--prefill 1024floor: not measured on this branch; the unit measured speed only.--attn-mmais not bit-identical.q36_attn.cuhis included byq36_engine.cu, which the HIP build compiles; the sm_80+ code is under#ifguards but never compiled for HIP.--batch) and vision with the int8 / fp16 cache.Limits
--attn-mmais ignored with a message (sm_75 compiled, not run).part_q8xandpart_q8mare written for 8 query heads per KV head (Qwen3.6-35B-A3B: 16 / 2); other shapes use the first int8 kernel.--kv fp16andk8v4refuse more than 8 query heads per KV head.Not specific to Sage-A1.5
Sage-A1.5 is a fine-tune of Qwen3.6-35B-A3B with the same architecture and tensor layout, so this path runs the base model
unchanged. The before/after numbers above are Sage-A1.5's. The base model was measured once, on my fork's build of the whole series
(
xyehya/Stratabranchq36-r2: these PRs' flags on, plus a few fork-only defaults), same card and server sweep, withSTRATA_Q8_PACKED=0(that file keeps its attention and DeltaNet matrices in Q8_0, and the packed copy would take 1.9 GiBfrom the expert slots): Unsloth's
Qwen3.6-35B-A3B-UD-Q4_K_XL(fromunsloth/Qwen3.6-35B-A3B-MTP-GGUF),262K int8 cache, decode 221 / 208 / 199 / 187 / 164 / 159 tok/s and prompt read 6,473 / 7,010 / 6,277 / 4,656 / 3,411 /
2,506 tok/s at 4K / 16K / 40K / 90K / 160K / 240K (Sage-A1.5 on the same build: decode 231 / 210 / 213 / 187 / 179 / 162).
Not measured: this PR's own before/after on the base model.
🤖 Generated with Claude Code
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.