Repository navigation
agx: share shifted array indices on M5 (q4_0 mat-vec) - #1428
Conversation
llama.cpp reads block (row * ncols + col) / 32 of a quantized matrix and vec4 ((col & ~31) + ((col & 31) >> 1)) / 4 of its vector, with col = i * 256 + 8 * tid + 256 * k in the k-th unrolled iteration. The constant 256 * k sits under the shift, so every iteration computed its indices from scratch and nothing folded the constant into the access. Where c and c0 differ by a multiple of 2^s, (y + c) >> s is ((y + c0) >> s) + ((c - c0) >> s) unless y + c and y + c0 lie on opposite sides of a 2^32 wrap, and then the two indices are 2^(32 - s) - |c - c0| / 2^s elements apart. M5 buffer bindings span at most INT32_MAX bytes, so when that many elements of the array stride cover 2^31 bytes, at most one of the two accesses is in bounds. When the program performs both, in one block with nothing between them that ends the invocation, the later index can be computed from the earlier one with one add, and its constant then folds into the access offset. The new pass runs before set_speculate(), which marks every access speculatable under soft faults, and skips accesses that are already speculatable or bounds-checked. Unrolled iterations write (x + 256k) & 31 for x & 31, so agx_nir_fold_aligned_masks and CSE now also run just before it, to give every iteration the same terms. Offline, on the llama.cpp MUL_MAT shaders compiled for M5 through the drm shim, the unrolled loop of the Q4_0 matrix-vector shader goes from 160 to 119 integer instructions per four iterations (360 to 319 ALU in all); the four iterations' vector reads share one index with byte offsets 1024, 2048 and 3072. The cooperative matrix shaders do not change. Signed-off-by: Satya Benson <satya@benson.earth>
agx_nir_fold_m5_offsets gave accesses that differ by constants one shared index and byte offsets only for power-of-two array strides and byte buffers. The 18-byte blocks of a Q4_0 matrix, read 16 bits at a time, are neither, so each unrolled iteration computed its own offset (an add, a multiply by 18 and two more adds per row) even once their indices differ by constants. The argument for byte buffers does not depend on the access width: zext(X + c) = zext(X + c0) + (c - c0) unless X + c wraps between them, and then one of the offsets is past 2^32 - 2^16, outside every M5 buffer. Apply it to accesses of any width, indexed in bytes. Offline, on the llama.cpp MUL_MAT shaders compiled for M5 through the drm shim, the unrolled loop of the Q4_0 matrix-vector shader goes from 119 to 87 integer instructions per four iterations (319 to 287 ALU in all; 360 before the previous commit), with the same 103 registers. The Q6_K matrix-vector shader drops from 135 to 127 registers (768 to 832 threads), but its loop now rematerializes five byte masks and is 13 instructions longer. Across 110 MUL_MAT shaders the two commits change 40: 38 have fewer instructions and 2 more, 13 use fewer registers and 12 run more threads; none use more registers or fewer threads. The cooperative matrix shaders do not change. Signed-off-by: Satya Benson <satya@benson.earth>
Sharing one index between the unrolled iterations of a loop saves its instructions, but the shared index stays live across the whole unrolled body. In a long body that holds several of them, such as the NVFP4 MUL_MAT_ID matrix-vector shader (a 1500-instruction loop over four rows), that raised the register count from 127 to 234 halves, 832 to 384 threads, while removing 227 instructions; the MXFP4 one went from 1024 to 896 threads. When agx_nir_opt_shifted_index changed an M5 shader and the result is below the occupancy of a register-light program, compile it again without the pass and keep whichever runs more threads, as is already done for the unroll factor. Also run the pass after the word merge and the read-only loop load hoist instead of before them, still ahead of set_speculate(). Offline, on the llama.cpp MUL_MAT (Q4_0, Q4_K, Q5_K, Q6_K, Q8_0, F16) and MUL_MAT_ID shaders compiled for M5 through the drm shim, counting the kept compile only, the three commits change 57 of 206 shaders: 55 have fewer instructions and 2 more (4719 fewer in all, 4619 fewer ALU), 20 use fewer registers and 2 use two halves more, 17 run more threads and none fewer. The Q4_0 matrix-vector shader for one column has 1580 instructions (1653 before), 287 ALU in its unrolled loop of four iterations (360 before), at 103 registers and 1024 threads. The cooperative matrix shaders do not change. Signed-off-by: Satya Benson <satya@benson.earth>
|
Broader Vulkan CTS at
78,442 cases, 0 differing. The one Fail, |
There was a problem hiding this comment.
Verdict: MERGE at a024158d
Blockers: none
Checked:
- Full 5-file diff (
agx_compile.c,agx_nir.h,agx_nir_fold_m5_offsets.c,agx_nir_opt_shifted_index.c,meson.build,+415/-21),MERGEABLEagainstaurora-wip. - Strict M5-only gating:
agx_nir_opt_shifted_index,agx_nir_fold_m5_offsets, and the occupancy fallback inagx_compile_shader_nir_unrolled()are all gated onm5 && !g18_scans(AGX_ISA_ENCODING_AGX3C && !emit_agx3d), leaving non-M5 GPUs untouched. - Soundness of
agx_nir_opt_shifted_index: runs only when!checked_offsets(non-robust) and beforeset_speculate(), requires both shifts and their non-speculatable global accesses in the same straight-line segment (split on calls/jumps/terminate/demote), verifiesagx_lin_binding_base()(<= INT32_MAXbytes), checksapart = ((1 << (32 - s)) - (|d| >> s)) * stride >= 2^31(ruling out both indices being in[0, INT32_MAX / stride)across a 32-bit wrap), and dominates all rewritten uses viaa->order < q->order. - Soundness of
agx_nir_fold_m5_offsets:mem_access_size_align_cbalready restricts global loads/stores to 8/16/32-bit (<= 4components) beforeagx_optimize_nir(), and bothagx_emit_load()andagx_emit_store()supportAGX_NIR_MEMORY_INDEX_BYTESwith 16-bit signed byte offsets. - Occupancy retry in
agx_compile_shader_nir_unrolled(): properly saves/restores*key, rewindspre_preamble_blob, and frees unusedplain.binary/plain_post_opt/clone. - Exact-head
a024158hardware verification on J775c (M5 Max, AGX4):test-backend-ops(MUL_MAT,MUL_MAT_ID,GET_ROWS) 2/2 pass, q4_0-21%to-28%,llama-2-7B Q4_0tg128+2.4%paired (pp512unchanged), and78,442Vulkan CTS cases across compute/ssbo/ubo/spirv_assembly/memory_model/glsl.indexing (0differing vs parent40acc58f345).
Evidence
- Checked
agx_compile.c:13120-13158(!checked_offsets, beforeset_speculate),agx_compile.c:13297(agx_nir_fold_m5_offsets),agx_compile.c:13843-13866(mem_access_size_align_cb), andagx_compile.c:2257/2478(AGX_NIR_MEMORY_INDEX_BYTES). - Nit (non-blocking): in
src/asahi/compiler/agx_nir_opt_shifted_index.c:531,(d & ((1 << a->s) - 1))uses a signedint1whilea->scan be1..31, which is signed-shift UB ifa->s == 31under UBSan;(UINT64_C(1) << a->s) - 1avoids it.
|
@DjDeveloperr, code-owner heads-up: App approval at |
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
|
Merging at |
Problem
On AGX4 (M5 Max), llama.cpp token generation is limited by the q4_0 matrix-vector shader (
mul_mat_vec_q4_0_f32_f32, used for n=1 because this driver reports no accelerated int8 dot product). The shader is issue-bound, not memory-bound: its unrolled loop spends 360 ALU per 4 iterations on 32 loads, and about 96 of those recompute array indices that differ only by a constant before a shift.Change
Three commits (on top of #1358, now merged):
agx: share shifted array indices between unrolled iterations on M5. A new pass rewritesa + ((y + c) >> s)from an earlier sibling access in the same block as one add. On this hardware a binding is at most INT32_MAX bytes and the index feeds an access-chain multiply, so a wrap that changed the result would make one of the two accesses out of bounds. It runs before speculation and only on non-speculatable, non-robust accesses.agx: index M5 accesses with non-power-of-two strides in bytes. The existing byte-offset folding now also applies to 16- and 32-bit accesses, so q4_0's 18-byte blocks get their +144/+288/+432 byte offsets as load immediates.agx: keep the higher-occupancy compile when sharing shifted indices. Where the pass fires and the result runs fewer threads, compile again without it and keep the higher-occupancy result, like the existing unroll-factor choice.Results (J775c, M5 Max)
Same session, mesa#1358 head
40acc58f345against this heada024158,MESA_SHADER_CACHE_DISABLE=true.test-backend-ops -b Vulkan0 -o MUL_MAT,MUL_MAT_ID,GET_ROWS: passes (2/2 backends OK, 0 failures).test-backend-ops perf -o MUL_MAT, n=1:llama-2-7B Q4_0, fa=1, 5 repetitions, order before/after/after/before:
Offline (shader-db through the drm shim, M5 Pro identity, same ISA): across 206 MUL_MAT / MUL_MAT_ID shaders, 57 change, 55 with fewer instructions and 2 with more (−4719 instructions, −4619 ALU in total); 20 use fewer GPRs and 2 more; 17 run more threads and none fewer. Cooperative-matrix matmul shaders do not change.
Not tested yet