Repository navigation
[w2] Fold the Q attention rotation (FWHT) into the rms_norm_mul_rope kernel - #171
Merged
Merged
Conversation
…ernel RMS_NORM, MUL, ROPE, RESHAPE, MUL_MAT (Hadamard hint) of the Q of a rotated KV cache run as one kernel: the rope result of the row stays in shared memory and is transformed by the butterfly of fwht_cuda (same stage order, scale applied first). Heads of 128, 256 and 512 values. One launch less per attention layer (Gemma 4, Qwen 3.x, Qwen 4 q8_0 KV). The K chain (rotation stored into a quantized cache) is left to the K store fusions. GGML_CUDA_NO_ROPE_FWHT_FUSION=1 turns the fusion off. Adds ROPE_FWHT eval and perf cases. Assisted-by: Claude Sonnet 5.5
Owner
Author
|
Reverted from master on 2026-12-04 (w2 build crashed Qwen4-single at >20k ctx; no measured gain). Commit preserved on branch archive/w2-25f0def; will be re-landed individually after GPU validation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The Q attention rotation (Hadamard / FWHT) after rope is a separate
fwht_cudalaunch per attention layer (PR #152 already fused the K and V rotations into the q8_0/q4_0 store). This folds it into the existing fused norm+rope kernel:RMS_NORM, MUL, ROPE, RESHAPE, MUL_MAT(Hadamard hint)-> onerms_norm_mul_rope_f32launch (FWHT_N= 128 / 256 / 512 template epilogue).fwht_cuda(same stage order h = 1, 2, 4, ..., the scale1/sqrtf(n)applied first,x+y/x-ypairs), so the values are meant to be bit-identical to the separate kernels. One block ofD/2threads per row (64 / 128 / 256 threads), 2 values per thread.ggml_cuda_op_mul_mat_use_fwhtrequires F32), which the FA reads as before. The "f16 as the consumer expects" option in the plan does not apply: the MUL_MAT result is F32.ggml_cuda_try_fuse_rms_norm_mul_rope_fwht, self-contained function plus one dispatch call inggml_cuda_try_fuse) needs rotation size == head size (128/256/512), contiguous rope output and MUL_MAT result, wave32,ggml_can_fuse_subgraph+ggml_check_edges+ memory-range check. It leaves the K chain alone (MUL_MAT -> RESHAPE -> VIEW ... -> SET_ROWS), which keeps its quantizing store fusion.ROPE -> MUL_MAT(hadamard)without a norm (step 3, the plain rope kernel is a different, per-element kernel, so it is not cheap with this epilogue) and the output inverse rotation fold (step 5 stretch: the V rotation is 64-value rows spanning two q8_1 blocks, so it needs a new quantize kernel or an FA epilogue).Rotation sites and remaining fwht launches per token (after #152)
Same chain shape (
RMS_NORM, MUL, ROPE, RESHAPE, MUL_MAT) in all three attention builders:build_attn(Gemma 4 / Qwen 3.x, llama-graph.cpp ~l.3489), the iswa variants (~l.3779, l.3902) andbuild_attn_qsain qwen4exp.cpp (l.962, so Qwen 4 gets the same fold). K and V rotation are fused into the cache store (fusion.cufwht_set_rows_q,rms_norm_fwht_set_rows_q). What remains is one Q rotation and one output inverse rotation (V rot, 64-wide) per attention layer. Counts below are estimates from the earlier kern/prof traces (pre-#152 totals minus the K/V launches), not new measurements:Expected gain (estimate, not measured)
Scout bound with Q and output folds: Qwen3.8 +1.0-1.4%, Gemma +2-3%. The Q fold alone is about half of that. Fewer launches (about 2 us idle per dependent launch).
Env switch
GGML_CUDA_NO_ROPE_FWHT_FUSION=1turns the fusion off (bit-identical, default on). No other switch renamed.Files
ggml/src/ggml-cuda/rope.cu,rope.cuh:FWHT_Nepilogue inrms_norm_mul_rope_f32, launcher,fwht_mmargument (default nullptr).ggml/src/ggml-cuda/fusion.cu: matcher + kill switch.tests/test-backend-ops.cpp:test_rope_fwht(op_descROPE_FWHT), own commented block; eval: D 128/256/512 x nb 1/3/512 x neox/normal/imrope, freq factors, partial rope, K chain with set_rows (must not be folded); perf: D 128/256/512 x nb 1/3/512, neox + normal.CPU checks done
fusion.cu.o,rope.cu.o,fwht.cu.oOK. No new warnings (one existingfwht.cu:420loop-not-unrolled warning).llvm-readelf --notesof the FWHT kernel instances (D 128/256/512, with/without freq factors):private_segment_fixed_size0, no spills, 55-61 VGPRs, LDS 1/2/4 KB. The non-FWHT instances are unchanged (0 LDS, 43-48 VGPRs).v_mul_f32(no FMA contraction into the first stage), stages areds_bpermute+ sign-select +v_add_f32, same asfwht_cuda<128>in the baseline object.test-backend-ops test -b CPU -o ROPE_FWHT33/33 OK,perf -b CPU -o ROPE_FWHTruns;test-llama-archs -a gemma4 -b CPUand-a qwen4exp -b CPUpass. Build tree deleted.The CUDA fusion path itself was NOT run (no GPU).
Risks
fusion.cuconflicts with the other w2 fusion branches: kept to one new function plus one 3-line dispatch addition in theRMS_NORMblock ofggml_cuda_try_fuse.GPU validation tonight (NOT YET RUN)
Use preamble (P) of the wave plan.
Eval:
Launch count (trace):
tg (each on and off, off =
GPURUN_OPTS='-e GGML_CUDA_NO_ROPE_FWHT_FUSION=1'):KLD (exactly 0):
Qwen4 QSA only becomes sparse above 2048 KV: for Qwen4 KLD add
-c 16384 --chunks 3.Hard rules followed
No GPU used or locked, no production/homelab/router/docker-compose touched, nothing sent to ggml-org/llama.cpp, build under the build lock at nice 19 / -j6 and deleted afterwards. Do not merge before the GPU gates.
Assisted-by: Claude Sonnet 5.5