Skip to content

agx: load a 16x16 A operand row as one word load - #1358

Merged
RyanTheTide merged 2 commits into
aurora-wipfrom
satya/cmat-a-row-word-load
Nov 30, 2026
Merged

RyanTheTide merged 2 commits into
aurora-wipfrom
satya/cmat-a-row-word-load

Conversation

@satchlj

@satchlj satchlj commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

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's mul_mm cooperative 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_mm coopmat shader, eight matmad16 per iteration):

instrs local_load mov icmpsel GPRs
before 239 32 68 6 175
after 232 24 37 35 207

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 was 2 * (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 with col >> 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_MAT for q4_0, q4_K, q5_K, q6_K, q8_0 and f16, compiled for M5 through the drm shim with AGX_MESA_DEBUG=shaderdb; 145 shaders, 53 differ from the parent 082b9e9c14c):

vs parent first commit only both commits
shaders with more / fewer instructions 47 / 6 11 / 41
total instructions +1025 −308
total ALU +1408 +20
shaders with more GPRs 34 28
shaders with fewer threads 26 19
shader parent first commit only both commits
q4_0 coopmat, largest 8193 instrs, 5494 alu, 239 GPRs 8033, 5393, 239 8033, 5360, 239
f16 coopmat, largest 7980, 5298, 239 7875, 5251, 239 7871, 5214, 239
q4_K, 191 GPRs 2339, 1750, 191 (512 thr) 2380, 1799, 207 (512) 2336, 1755, 207 (512)
q8_0, 203 GPRs 2341, 1750, 203 (512 thr) 2370, 1787, 223 (448) 2359, 1776, 207 (512)
q6_K, 127 GPRs 1169, 969, 127 (832 thr) 1191, 995, 143 (704) 1164, 968, 143 (704)
q4_0, small tile 681, 520, 95 (1024 thr) 699, 542, 127 (832) 680, 523, 111 (896)

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 current matmad16. 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; base 724e94ecfd5):

test base this PR change
pp512, run 1 (A B B A) 3109.4 / 3074.7 3157.2 / 3126.1 +1.6%
pp512, run 2 (B A A B) 3073.6 / 3058.7 3125.8 / 3120.3 +1.9%
pp2048 2946.2 / 2933.5 2973.9 / 2990.6 +1.4%
tg128 109.9 / 106.4, 104.5 / 104.6 107.8 / 107.6, 104.9 / 104.6 none
Qwen3.8-2B Q4_K_M pp512 7550.7 / 7665.0 7589.1 / 7609.6 none (noise)
Qwen3.8-2B tg128 214.4 / 213.8 215.3 / 215.7 +0.6%

Conformance (J775c, base 724e94ecfd5):

  • test-backend-ops -o MUL_MAT,MUL_MAT_ID: 3535/3535 pass.
  • dEQP-VK cooperative_matrix: identical to base on every case (12,786 Pass, 22,680 NotSupported, 0 Fail).
  • meson asahi suite: 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, on aurora-wip 082b9e9c (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) at b8c3e8d491: dEQP-VK cooperative_matrix and test-backend-ops MUL_MAT/MUL_MAT_ID identical to base. The second commit (head 40acc58f345) is not yet run on hardware. Offline only: the shader stats above and the meson asahi suite, 270/271 OK (hk_query_copy fails 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).

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>
@satchlj

satchlj commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Exact-head re-run on the current aurora-wip 082b9e9c14c (J775c, M5 Max; tree-identical to head b8c3e8d4). Each arm's library in its own ICD, ABBA order, -fa 1, nothing else on the GPU.

test base (082b9e9c) this PR change
Llama-2-7B Q4_0 pp512 3102.1 / 3097.2 3156.2 / 3088.7 +0.7% (one run +1.7%, one −0.3%)
Llama-2-7B Q4_0 pp2048 2935.7 / 2938.7 2988.4 / 2979.9 +1.6%
Llama-2-7B Q4_0 tg128 108.24 / 108.25 108.09 / 108.10 none
Qwen3.8-2B Q4_K_M pp512 7683.8 / 7690.4 7566.0 / 7572.5 −1.5%
Qwen3.8-2B tg128 217.4 / 217.1 219.0 / 216.1 none

Conformance at this head: test-backend-ops -o MUL_MAT,MUL_MAT_ID passes. dEQP-VK cooperative_matrix gives 12,786 Pass, 22,680 NotSupported, 0 Fail, the same counts as base.

Regression: Qwen Q4_K pp512 is 1.5% slower in both runs (run-to-run spread under 1%). On the older base 724e94ec the same comparison was −0.1%. The change also reaches the Q4_K mul_mm coopmat shader, so the next step is shaderdb for that shader on both arms. This stays a draft until that is explained or fixed, and until the AGX2/AGX3B conformance rows are in.

@RyanTheTide

Copy link
Copy Markdown

J713 (MacBook Air M4, G16G / AGX3B) coopmat row: no change. Exact head b8c3e8d4 vs base 082b9e9c, each in its own ICD via VK_DRIVER_FILES on the saved 7.3.0-aurora-fleet85 kernel. Userspace only, no reboot, silent.

The full dEQP-VK.compute.pipeline.cooperative_matrix.* tree plus api.smoke, both arms:

group base head
khr_a 173 P / 3,461 NS 173 P / 3,461 NS
khr_b 173 P / 3,461 NS 173 P / 3,461 NS
khr_c 0 P / 36 NS same
khr_r 373 P / 9,562 NS 373 P / 9,562 NS
nv, 64b_indexing, maintenance1 all NS same
op_constant_null 4 P 4 P
api.smoke 8 P 8 P

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 test-backend-ops -o MUL_MAT,MUL_MAT_ID on both Macs. Traces: traces/gpu/20261130-gpu-ungate-cm.

@RyanTheTide

Copy link
Copy Markdown

J615 (M3, G15G) and J713 (M4, G16G) rows complete: no change on either. Exact head b8c3e8d4 vs base 082b9e9c, each in its own ICD via VK_DRIVER_FILES, saved kernels (J615 7.3.0-aurora-edge, J713 7.3.0-aurora-fleet85). Userspace only, no reboot, silent.

J615 G15G J713 G16G
dEQP-VK.compute.pipeline.cooperative_matrix.* (full tree) + api.smoke 731 Pass -> Pass, 17,201 NS -> NS, 0 regressions 731 Pass -> Pass, 17,201 NS -> NS, 0 regressions
llama.cpp b12211 test-backend-ops -b Vulkan0 -o MUL_MAT 2,521/2,521 both arms 2,521/2,521 both arms
-o MUL_MAT_ID 1,109/1,109 both arms 1,109/1,109 both arms
new GPU fault/timeout/recovery lines 0 0
restored Enforcing, 0 failed units Enforcing, 0 failed units

The test-backend-ops rows use release ICDs (-Db_ndebug=true, as distro packages). Assertion-enabled builds of both arms abort at startup in nir_scalar_chase_alu_src, so that's pre-existing and not from this PR: filed as #1365.

Not run: the optional pp512 Llama-2-7B Q4_0 A/B. There's no model on the lab store.

Traces: traces/gpu/20261130-gpu-ungate-cm. Kit: kits/test/20261130-gpu-ungate-cm.

@satchlj

satchlj commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

J775c (AGX4) row at this head b8c3e8d491 vs its parent 082b9e9c14c, on a boot with SLC enabled (the earlier J775c rows ran with SLC off). Arms built from the same tree, MESA_SHADER_CACHE_DISABLE=true, llama.cpp llama-master-20261119, fa=1, 5 repetitions per arm.

Qwen3.8-2B Q4_K_M pp512, order base/final/final/base twice:

arm runs (t/s) mean
parent 7748.8, 7704.4, 7715.4, 7705.8 7718.6
this PR 7608.0, 7663.1, 7656.0, 7636.6 7640.9

−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:

test parent this PR change
pp512 3099.7, 3116.9 (mean 3108.3) 3167.7, 3167.8 (mean 3167.7) +1.9%
pp2048 2936.1, 2933.4 (mean 2934.7) 2978.0, 2979.3 (mean 2978.7) +1.5%

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.

@satchlj

satchlj commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

Where the AGX4 Qwen3.8-2B (Q4_K_M) loss comes from: AGX_MESA_DEBUG=shaderdb for both arms during one pp512 run. Of 62 compiled binaries, only two compute shaders differ between the parent and this head:

shader instrs alu ic GPRs threads code size
A, parent → this PR 1664 → 1697 1304 → 1345 708 → 772 207 → 207 512 → 512 16582 → 17030
B, parent → this PR 1849 → 1877 1461 → 1497 896 → 952 207 → 223 512 → 448 18726 → 19190

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 local_loads go away (the extracts from the loaded word cost more than the loads they replace here). The llama-2 Q4_0 shaders are unchanged, which fits llama gaining while Qwen loses.

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>
@satchlj

satchlj commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

Pushed 40acc58f345 on top of b8c3e8d491 (fast-forward). It folds the word-load addresses again and selects halves directly; details and offline shader stats are in the description. M5 A/B to run: parent 082b9e9c14c vs head 40acc58f345, Qwen3.8-2B Q4_K_M pp512 and llama-2-7B Q4_0 pp512/pp2048. Offline, about half the cooperative matrix shaders still use more GPRs than the parent (the ILP load window, see "Open: GPRs"), so the Qwen row is what settles it.

@satchlj

satchlj commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

J775c (AGX4, M5 Max) row at the new head 40acc58f345 vs the parent 082b9e9c14c, same build tree, MESA_SHADER_CACHE_DISABLE=true, llama.cpp llama-master-20261119, fa=1, 5 repetitions per arm, SLC on.

Qwen3.8-2B Q4_K_M pp512, order parent/head/head/parent twice:

arm runs (t/s) mean
parent 7736.7, 7715.5, 7740.4, 7740.4 7733.3
this PR 7925.2, 7884.5, 7897.3, 7898.7 7901.4

+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:

test parent this PR change
pp512 3113.0, 3117.2 (3115.1) 3234.6, 3238.8 (3236.7) +3.9%
pp2048 2934.8, 2936.8 (2935.8) 3040.3, 3037.5 (3038.9) +3.5%

Cooperative-matrix CTS and test-backend-ops -o MUL_MAT,MUL_MAT_ID at this head are running now; results follow.

@satchlj

satchlj commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

Correctness at 40acc58f345 on J775c (AGX4): test-backend-ops -b Vulkan0 -o MUL_MAT,MUL_MAT_ID passes (2/2 backends OK). Vulkan CTS cooperative_matrix tree: 47,031 cases, identical to the parent 082b9e9c14c case for case (0 differing: 12,786 Pass, 22,680 NotSupported, 11,565 Crash on both arms; the crashes are the pre-existing assert abort, mesa#1365). With the AGX2/AGX3B rows clean and the AGX4 A/B above (Qwen3.8-2B pp512 +2.2%, llama-2-7B pp512 +3.9%, pp2048 +3.5%), marking ready.

@satchlj
satchlj marked this pull request as ready for review November 30, 2026 16:00
@satchlj
satchlj requested review from a team and DjDeveloperr as code owners November 30, 2026 16:00

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 40acc58f

Blockers: none

Checked:

  • Audited src/asahi/compiler/agx_nir_lower_cmat.c (+75/-0 across b8c3e8d4 and 40acc58f).
  • Verified the 16x16 row-major 16-bit GLSL_CMAT_USE_A lane-to-column mapping in matrix_col(): for element a multiple of 4, col takes values {0, 1, 8, 9}, so word index col >> 1 (0 or 4) loads four 32-bit words covering columns 0..7 or 8..15 with no out-of-range reads, and odd = (col & 1) != 0 selects the high/low 16-bit halves (nir_unpack_32_2x16_split_y / _x) matching base + 2*e verbatim.
  • Verified matrix_rows_aligned(b->shader, intr, 4) checks scalar/vector pointer element size divisibility by 4 bytes, walks array/ptr_as_array strides for divisibility by 4, and accepts only a cast with align_mul >= 4 && align_offset % 4 == 0 or an implicitly laid-out non-struct nir_var_mem_shared variable (raising data.alignment to 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_ID PASS, and 47,031 Vulkan CTS cooperative_matrix cases identical to parent 082b9e9c14c; AGX3B (J615 M3 + J713 M4) conformance rows and host Asahi tests clean; MERGEABLE against aurora-wip.
Evidence
  • Head SHA: 40acc58f345a8fc449dbe3662e451b0bac656fa5 (base=aurora-wip, MERGEABLE).

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 bcsel on odd picks 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.

@RyanTheTide
RyanTheTide merged commit 6e153e5 into aurora-wip Nov 30, 2026
@RyanTheTide
RyanTheTide deleted the satya/cmat-a-row-word-load branch November 30, 2026 17:40

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@DjDeveloperr

Copy link
Copy Markdown

Post-merge row, PASS — J773g (M4 mini, T8132 G16G) at 40acc58f345a8fc449dbe3662e451b0bac656fa5. The full cooperative_matrix CTS list has no status changes against the base, and the fixed-output CRCs are unchanged.

  • Arms: aurora-wip base 082b9e9c14c3 and this head. Each used its own Borealis ICD with the shader cache disabled. dEQP ran directly with 8 jobs.

  • Every cooperative_matrix case in the Vulkan CTS default list, 54,276 cases per arm:

    Pass Fail NotSupported
    base 2,265 0 52,011
    head 2,265 0 52,011

    0 cases changed status. QPA counts agree with the stdout totals.

  • gpubench 868b0644 CRCs, identical on both arms: compute.uber uniform d4 b022fbaf; render.postfx 1080p MSAA4 d1/d2 668d6f0a; render.bindless_ubo 512 draws ubo_vec4 d2 1af53d5c; render.deferred tile 1080p MSAA4 d2 6dc74b48.

  • Health: 0 GPU faults, recoveries or hang kills. The Mac was on its saved stock kernel (7.3.0-ARCH-m4mainefed1ae3, same boot) with system Mesa hashes unchanged before and after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants