Repository navigation
[Engine] qwen4/metal: rewrite q4_qsa_attend_batch with simdgroup_matrix - #476
Closed
NightRain233 wants to merge 1 commit into
Closed
NightRain233 wants to merge 1 commit into
NightRain233 wants to merge 1 commit into
Conversation
…ix (10x faster 4K prefill QSA)
Collaborator
|
Integrated on |
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.
Problem and result
In
STRATA_METAL_PROF=2profiling of 4,096-token Qwen4-35B-A3B prefill on Apple Silicon Metal (df2c3ff, M5 Ultra),q4_qsa_attend_batch_q8_8was the single largest GPU hotspot: 515.1 ms (29.3%of total 4K prefill GPU time) at only1.34 TFLOP/s(1.48 GB/s), because it performed scalar per-element K/V reads (load_v1inside the chunk loop) and scalarfor (h = 0..G-1)FMAs instead of using Apple Siliconsimdgroup_matrixhardware MMA.This PR adds
q4_qsa_attend_batch_mma_{q8,f16}_8fordim == 256 && G <= 8(strictly undergpu::metal::optimize(), keeping the original scalar kernel intact fordim != 256,G > 8, and--no-metal-optimize).Measured on Apple M5 Ultra 256 GB (
Qwen4-35B-A3B-UD-IQ2_XS,--kv q8_0 --no-draft --lookup 0):r512_G8_n2048inpoly_qwen4_parity):FP16:12.872 ms/call (1.33 TFLOP/s)->1.325 ms/call (12.97 TFLOP/s)(9.71xfaster)Q8_0:13.043 ms/call (1.32 TFLOP/s)->1.348 ms/call (12.75 TFLOP/s)(9.68xfaster)STRATA_METAL_PROF=2):q4_qsa_attend_batch:492.0 ms(1.40 TFLOP/s) ->48.2 ms(14.27 TFLOP/s) (10.20xfaster;10.67xvs515.1 mson the Chinese/code 4K prompt)2,488.2 tok/s->3,425.1 tok/s(+37.7%)Related discussion
engine/metal/, zero overlap with CUDA/HIP).Approach
engine/metal/src/poly/qwen4.metal,engine/metal/src/poly/qwen4.cpp,engine/metal/poly_tests/qwen4_parity.cpp).(n_kv, rows, 1), total TG memory16,224 B(<= 16 KBfor 2 concurrent TGs/core with bank-conflict-free padded stridessq_h[8][264],skv[16][264],sp_h[8][72],sp_f[8][64];sp_tmpaliasessp_hand warp-privateo_tmpaliasesskv).uint2256 B burst + 16 B L1 scale line for Q8_0;uint4512 B burst for FP16) with software-pipelined register prefetch overlapping the next 16 rows' device loads with 16 back-to-back 8×8×8simdgroup_multiply_accumulateops.o_tmpaccumulator rescaling wheneverall(s_alpha[0..G-1] == 1.0f); explicitn_ids <= 0zeroing fast-path; pads unused headsh >= Gwith0.0hso any1 <= G <= 8works.Validation
bec6133ondf2c3ff, macOS Apple M5 Ultra 256 GB, AppleClang 21.0.0 Release):cmake --build engine/build-metal -j --target strata-poly poly_qwen4_parity poly_quantized_parity poly_advanced_parity poly_memory_growth-> 0 errors.ctest --test-dir engine/build-metal -L poly --output-on-failure-> 51/51 passed (100%).poly_qwen4_parityQSA batch MMA matrix (rows = 1, 64, 512,G = 4, 8,dim = 256,n_ids = 0, 17, 64, 512, 2048,ring = 0&257,Q8_0&FP16, stridedkv_rs/v_rs,dim = 128fallback, poisonedn_ids = 0): 0 mismatches (max err0to1.39e-4).python3 -m unittest tools.test_settings_register && python3 tools/settings_register.py --check-> 8/8 tests OK.Limits
dim == 256 && G <= 8(covers Qwen4-35B-A3B and Qwen3.8-Flash-Next); other shapes use the existing scalar kernel.simdgroup_matrixinputs with FP32 accumulation and FP32 online softmax (max abs err<= 1.39e-4vs FP32 reference, well withinpoly_qwen4_paritytolerances;--no-metal-optimizeselects the original FP32 scalar kernel).