Repository navigation
agx: load a 16x16 A operand row as one word load - #1358
Conversation
A lane holds four columns, every other one from its first, of each of two rows of a 16x16 A operand. Each row took two four-element 16-bit loads, and every element was then moved into its half of an operand register. Where the rows start 4-byte aligned, the eight-element half row that holds a lane's columns is one load of four 32-bit words, and the lane's elements are the low halves of those words, or the high halves where its first column is odd: a select of a half each, straight into the operand registers. A shared variable this loads from is aligned to 4 bytes for it. In the inner loop of the llama.cpp mul_mm cooperative matrix shader, whose A tile is in shared memory (Q4_0, M5 Max), local loads per eight matmad16 go from 32 to 24 and instructions from 239 to 232. On Llama 2 7B Q4_0, pp512 is 1.6-1.9% faster and pp2048 1.4%; token generation and Qwen3.8 2B Q4_K_M are unchanged. Signed-off-by: Satya Benson <satya@benson.earth>
|
Exact-head re-run on the current
Conformance at this head: Regression: Qwen Q4_K pp512 is 1.5% slower in both runs (run-to-run spread under 1%). On the older base |
|
J713 (MacBook Air M4, G16G / AGX3B) coopmat row: no change. Exact head The full
Per case: 731 Pass -> Pass, 17,201 NotSupported -> NotSupported, 0 regressions, 0 Fail on either arm. 0 new GPU fault, timeout or recovery lines in dmesg. Restored: Enforcing, 0 failed units. Still to come: the J615 (M3, G15G) row of the same tree, queued behind a lock, and llama.cpp b12211 |
|
J615 (M3, G15G) and J713 (M4, G16G) rows complete: no change on either. Exact head
The Not run: the optional pp512 Llama-2-7B Q4_0 A/B. There's no model on the lab store. Traces: |
|
J775c (AGX4) row at this head Qwen3.8-2B Q4_K_M pp512, order base/final/final/base twice:
−1.0%, and consistent: every run with this PR is below every parent run. The Qwen regression reported earlier (−1.5% with SLC off) is still there. llama-2-7B Q4_0, order base/final/final/base:
With the AGX2/AGX3B rows clean, the open item is the AGX4 Q4_K path. Keeping this as a draft until that is understood. The next step is comparing the Q4_K matmul shader between the two arms. |
|
Where the AGX4 Qwen3.8-2B (Q4_K_M) loss comes from:
In shader B the word load raises register demand past the 512-thread limit, so occupancy drops by 12.5%. Both shaders also gain about 2% instructions and about 3% ALU, even though 24 Next: keep the word load where it does not raise GPRs, or extract lanes from the word without keeping the whole word live. Still a draft. |
The 16x16 A operand word load indexed its row in 16-bit elements from the first column rounded down to even, so its byte offset was 2 * (col & ~1) on top of the row's. That does not divide into a word index without a shift, and the shift hid each row's and each k step's constant offset from the shared offset fold: every one of them became its own address register, live across the loop, and a few more instructions ahead of it. Index the row in words with col >> 1 instead, so the offset divides exactly and those constants fold into the load's immediate as they do for the 16-bit loads. Take each element as a select between the low and the high half of its word rather than as the low half of the word shifted by 16 or 0, which was sometimes emitted as a bit-field extract and a move. Offline, on the llama.cpp MUL_MAT shaders compiled for M5 through the drm shim, this brings instructions and ALU back to about the parent's, or below it in the large Q4_0 and F16 cooperative matrix shaders. Signed-off-by: Satya Benson <satya@benson.earth>
|
Pushed |
|
J775c (AGX4, M5 Max) row at the new head Qwen3.8-2B Q4_K_M pp512, order parent/head/head/parent twice:
+2.2%: every run with this PR is above every parent run, so the earlier −1.0% is gone. llama-2-7B Q4_0, parent/head/head/parent:
Cooperative-matrix CTS and |
|
Correctness at |
There was a problem hiding this comment.
Verdict: MERGE at 40acc58f
Blockers: none
Checked:
- Audited
src/asahi/compiler/agx_nir_lower_cmat.c(+75/-0acrossb8c3e8d4and40acc58f). - Verified the 16x16 row-major 16-bit
GLSL_CMAT_USE_Alane-to-column mapping inmatrix_col(): forelementa multiple of 4,coltakes values{0, 1, 8, 9}, so word indexcol >> 1(0or4) loads four 32-bit words covering columns0..7or8..15with no out-of-range reads, andodd = (col & 1) != 0selects the high/low 16-bit halves (nir_unpack_32_2x16_split_y/_x) matchingbase + 2*everbatim. - Verified
matrix_rows_aligned(b->shader, intr, 4)checks scalar/vector pointer element size divisibility by 4 bytes, walksarray/ptr_as_arraystrides for divisibility by 4, and accepts only a cast withalign_mul >= 4 && align_offset % 4 == 0or an implicitly laid-out non-structnir_var_mem_sharedvariable (raisingdata.alignmentto at least 4), safely preserving the legacy load path otherwise. - Verified exact-head J775c (M5 Max, AGX4) results at
40acc58f345:Qwen3.8-2B Q4_K_M pp512 +2.2%,llama-2-7B Q4_0 pp512 +3.9%/pp2048 +3.5%,test-backend-ops -b Vulkan0 -o MUL_MAT,MUL_MAT_IDPASS, and47,031Vulkan CTScooperative_matrixcases identical to parent082b9e9c14c; AGX3B (J615 M3 + J713 M4) conformance rows and host Asahi tests clean;MERGEABLEagainstaurora-wip.
Evidence
- Head SHA:
40acc58f345a8fc449dbe3662e451b0bac656fa5(base=aurora-wip,MERGEABLE).
RyanTheTide
left a comment
There was a problem hiding this comment.
Non-author review at 40acc58f: approve.
- Alignment guard holds. The fast path is taken only for 16x16 row-major 16-bit A loads where
matrix_rows_aligned()proves every row starts on 4 bytes. That means the pointer element type and every array stride in the deref chain are multiples of 4, plus either an aligned cast or a non-struct shared variable without explicit layout (which it then aligns). A plain f16 pointer (2 bytes) fails the first check, so odd element strides can't reach the word load. - Half selection is right. A lane reads columns c, c+2, c+4, c+6. Loading four words from word c>>1 covers elements floor_even(c) .. +7, so the lane's elements are all low halves when c is even and all high halves when it's odd. The
bcselonoddpicks them in operand order. The extra element read (c+7 or c-1) stays inside the lane's 8-column half of the row, so the load never touches memory outside the matrix row. - Indexing the row in words keeps the shared offset divisible and the constant row/step offsets foldable, as the comment says. The uvec4 load carries align 4, so the backend splits it where a 16-byte access isn't allowed.
- Stores, B/C/R, column-major and other sizes all keep the existing code.
Hardware: our J615 (M3, AGX2 path) and J713 (M4, AGX3B) rows show the full cooperative_matrix tree identical to base and test-backend-ops MUL_MAT/MUL_MAT_ID all passing, plus your M5 rows and pp512 gains. The assertion we hit in assertion-enabled builds is pre-existing on base (#1365), not from this change.
DjDeveloperr
left a comment
There was a problem hiding this comment.
Verdict: FINISH, then merge at 40acc58f. The code is right; one row is missing.
Missing: one AGX2 or AGX3B row at 40acc58f. The J615 and J713 rows ran at b8c3e8d4. Your second commit 40acc58f rewrites the same lowering: the word index and a select per half instead of the shift. That code runs on every GPU that exposes cooperative matrices, so the earlier rows don't cover it. Re-running the same set on either Mac at this head closes it: the full dEQP-VK.compute.pipeline.cooperative_matrix.* tree plus test-backend-ops -o MUL_MAT,MUL_MAT_ID, both arms.
Checked: the word load runs only for 16-bit row-major 16x16 A loads whose rows provably start 4-byte aligned. The pointer type and every array stride must be multiples of 4, plus either a cast that says so or a plain shared variable outside an explicit layout, which then gets aligned. Anything else keeps the old path. The lane's first columns are 0, 1, 8 and 9, so the four words cover columns 0 to 7 or 8 to 15 and never read past the row. Even columns take the low halves and odd columns the high halves, which matches the old element order. There's no device or environment gate. The J775c row at this head shows Qwen3.8-2B pp512 +2.2% and llama-2-7B pp512 +3.9%, and cooperative-matrix CTS and test-backend-ops are identical to the parent.
Note (not blocking)
The description's Tested line still says the second commit hasn't run on hardware and that the PR stays a draft until the M5 A/B is in. Your 15:47Z and 16:00Z comments are that A/B and the conformance run, so the body can say so.
|
Post-merge row, PASS — J773g (M4 mini, T8132 G16G) at
|
Cuts the register moves around 16x16 A operand loads from shared memory in
agx_nir_lower_cmat. This is the hot loop of llama.cpp'smul_mmcooperative matrix shader for Q4_0 prompt processing.Change: a lane holds four columns of each of two A rows, every other one starting from its first column. Each row was two overlapping four-element 16-bit loads, then a move per element into its half of an operand register. Where the rows start 4-byte aligned, the eight-element half row holding a lane's columns is now one load of four 32-bit words. The lane takes the low halves, or the high halves when its first column is odd, with one select per element straight into the operand registers.
Row alignment is proven from the deref chain: cast alignment, or an implicitly laid-out shared variable, which then gets
data.alignment = 4. Any other case keeps the old path. No device, model or environment gates. About 65 added lines.Hot block (the
mul_mmcoopmat shader, eightmatmad16per iteration):In the Llama-2 Q4_0 run no other shader changed and the token-generation shaders are identical; in a Qwen3.8 Q4_K_M run two other cooperative matrix shaders changed (comment 7281741782), which the second commit addresses.
Second commit (address fold and half select): the first commit indexed the row in 16-bit elements from
col & ~1, so the byte offset was2 * (col & ~1). That only becomes a word index through a shift, and the shift hid every row's and k step's constant offset from the shared offset fold. Each one turned into its own address register, live across the loop. The row is now indexed in words withcol >> 1, so the offset divides exactly and the constants fold into the load immediate, as they do for the 16-bit loads. Each element is now a select between the low and high half of its word instead of a shift by 16 or 0, which was sometimes emitted as a bit-field extract plus a move.Offline shader stats (llama.cpp
test-backend-ops -o MUL_MATfor q4_0, q4_K, q5_K, q6_K, q8_0 and f16, compiled for M5 through the drm shim withAGX_MESA_DEBUG=shaderdb; 145 shaders, 53 differ from the parent082b9e9c14c):Open: GPRs. With scheduling off (
AGX_MESA_DEBUG=nosched), both commits use no more GPRs than the parent, for example 95→87 on the small q4_0 tile above. The remaining rise comes from the M5 compute ILP schedule. Its load-in-flight window counts loads, not registers, so with two word loads per A tile instead of four 16-bit loads it reaches the next k step's A operand and holds it across the currentmatmad16. Changing that window is a scheduler policy change for every shader, so it is not in this PR.Results (J775c, M5 Max; Llama-2-7B Q4_0,
-fa 1; each arm's library in its own ICD, run ABBA with nothing else on the GPU; base724e94ecfd5):Conformance (J775c, base
724e94ecfd5):test-backend-ops -o MUL_MAT,MUL_MAT_ID: 3535/3535 pass.cooperative_matrix: identical to base on every case (12,786 Pass, 22,680 NotSupported, 0 Fail).asahisuite: 351 OK. 3 failures (asahi-egl-priority-{allow,deny,once}) fail the same way without this change.Tested: J775c (M5 Max) at the first commit
b8c3e8d491, onaurora-wip082b9e9c(comments 7262674191 and 7281741782): llama-2-7B Q4_0 pp512 +1.9%, pp2048 +1.5%; Qwen3.8-2B Q4_K_M pp512 −1.0% (ABBA ×2); conformance identical. J615 (M3) and J713 (M4) atb8c3e8d491: dEQP-VK cooperative_matrix andtest-backend-opsMUL_MAT/MUL_MAT_ID identical to base. The second commit (head40acc58f345) is not yet run on hardware. Offline only: the shader stats above and the mesonasahisuite, 270/271 OK (hk_query_copyfails the same way on the parent in the build VM, an mmap failure). Draft until the M5 A/B at this head is in.Risk: two-way door; compiler-only, falls back to the old load path when row alignment is not proven. Affects every GPU that exposes cooperative matrices (AGX2, AGX3B, M5).