Skip to content

agx: relabel K in 16x16 16-bit cooperative matrix operands - #1430

Merged
DjDeveloperr merged 3 commits into
aurora-wipfrom
feat/agx-cmat-k-relabel
Dec 1, 2026
Merged

DjDeveloperr merged 3 commits into
aurora-wipfrom
feat/agx-cmat-k-relabel

Conversation

@satchlj

@satchlj satchlj commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

What: load 16x16 16-bit cooperative matrix A and B operands with two four-element loads each and no selects, by relabelling K alike in both operands and reading a column-major B through the 16x16 GEMM's transpose_b layout. Taken per shader where nothing but the products can tell K order (k_relabel_legal()). llama.cpp prompt processing on M5 Max: +4.5% (Llama-2-7B Q4_0) to +7.3% (Qwen3.8-2B Q4_K_M).
Tested: J775c (M5 Max) at head 7dfa5481dae, default command line, default vs AGX_MESA_DEBUG=nocmatk in the same library (5 reps, ABBA ×2): Llama-2-7B Q4_0 -fa 1 pp512 3,234 → 3,386 (+4.7%; macOS Metal 3,325), pp2048 3,039 → 3,178 (+4.6%), tg128 unchanged (110.1 / 110.1); Qwen3.8-2B Q4_K_M pp512 7,889 → 8,499 (+7.7%), tg128 unchanged. dEQP-VK cooperative_matrix 0 differing of 47,031 vs 40acc58f345; llama.cpp test-backend-ops MUL_MAT, MUL_MAT_ID, FLASH_ATTN_EXT all pass; layout probe 18/18 same/A*B with and without nocmatk. Offline: default codegen identical to the flagged build on all 572 llama.cpp shaders, nocmatk identical to aurora-wip; meson asahi 272/273 (hk_query_copy, a build-VM mmap failure, fails the same way without this).
Risk: two-way door · M5 (AGX3C) only: 16x16 operands exist only where the device offers the 16x16 GEMM, so other GPUs compile exactly as before.
Not yet: nothing on M5 Max. In flash-attention cooperative matrix shaders the change saves instructions in all 167 changed shaders but raises GPRs in 75 and lowers occupancy in 55 (offline); the end-to-end -fa 1 runs above are faster, and test-backend-ops FLASH_ATTN_EXT passes.

Details

Problem. A lane of a 16x16 A operand holds K = k0, k0 + 2, k0 + 4, k0 + 6 of each of two rows, so a row-major A is a four-word load plus a select of a half per element (#1358), or two four-element loads of which half is used. B holds four consecutive N of a row, so a column-major B is four two-element loads. llama.cpp's mul_mm keeps A row-major and B column-major in shared memory and pays this on every product.

Change. The GEMM pairs A's column bits with B's row bits register by register, so the bits of K can be renamed freely as long as A and B are renamed alike: every product stays as it was. Renaming bits 1, 2, 0, 3 of K to bits 0, 1, 2, 3 (layout_16_ak, layout_16_bk, layout_16_btk) puts a lane's four K at four consecutive places in memory. With transpose_b set, the GEMM reads B with, in element e of lane l,

k = ((l >> 3) & 1) | ((e & 3) << 1) | ((l & 1) << 3)
n = ((l >> 1) & 3) | ((l >> 2) & 4) | ((e >> 2) << 3)

so K is placed exactly as in A, and a column-major B loads as a row-major A does. A row-major A, a column-major B with transpose_b and a row-major B without it become two four-element loads each. A column-major A is four two-element loads.

Why it is legal. A relabelling renames the bits of K in the layouts, so every access through matrix_layout() still finds each element where it is; only the pairing inside the GEMM cannot be carried by the layouts. k_relabel_legal() therefore takes it for all of a shader's 16x16 16-bit A and B operands or for none, and only where those are reached by nothing but coopMatLoad, products, construction, copies and elementwise operations: no store, element access, conversion, transpose, bitcast, reduction, tensor access or call, and no product of a candidate with a non-candidate. B takes the transpose_b layout where all of its loads are column-major. Tensor loads (cooperative_matrix2) are lowered to element inserts first, so those shaders keep today's layouts.

Commits.

  1. agx: let cmat_muladd_agx ask for transpose_b on a 16x16 GEMM: a matrix_type bit the packer encodes as transpose_b, requested through bit 1 of cmat_muladd_agx's saturate index. No codegen change.
  2. agx: relabel K in 16x16 16-bit cooperative matrix operands: the lowering. AGX_MESA_DEBUG=nocmatk restores the previous codegen.
  3. asahi: add a 16x16 GEMM operand layout probe: src/asahi/tools/gemm16-transpose-probe, a Vulkan program (checked-in SPIR-V, builds with cc) that runs one 16x16x16 f16 product in six load variants on identity and random inputs and decodes which A and B elements the GEMM used. Every variant must give same/same/A*B, with and without nocmatk.
Evidence

Hardware at the head (J775c, 7dfa5481dae, default vs nocmatk, same library, 5 reps per arm, ABBA ×2; every default run above every nocmatk run):

test nocmatk default change
Llama-2-7B Q4_0 -fa 1 pp512 3234 3386 +4.7% (macOS Metal: 3325)
Llama-2-7B Q4_0 -fa 1 pp2048 3039 3178 +4.6%
Llama-2-7B Q4_0 -fa 1 tg128 110.1 110.1 0
Qwen3.8-2B Q4_K_M pp512 7889 8499 +7.7%

Hardware, earlier (J775c, M5 Max; the lowering of this PR behind AGX_MESA_DEBUG=cmatk at 5dd842fa against its base 40acc58f345, same session, 5 reps per arm, ABBA ×2; every flagged run above every baseline run):

test 40acc58f345 with the lowering change
Llama-2-7B Q4_0 -fa 1 pp512 3238 3383 +4.5% (macOS Metal: 3325)
Llama-2-7B Q4_0 -fa 1 pp2048 3039 3176 +4.5%
Qwen3.8-2B Q4_K_M pp512 7925 8500 +7.3%
  • dEQP-VK cooperative_matrix: 47,031 cases, 0 differing from 40acc58f345.
  • gemm16-transpose-probe: all 18 results (6 variants × 3 inputs, including ab_shared_padded and a_col_global) same/A*B.

Offline (at 79946772aa3, the same patch as the head on 138a5228631; llama.cpp test-backend-ops compiled for M5 through the asahi drm shim with AGX_MESA_DEBUG=shaderdb: MUL_MAT for q4_0, q4_K, q5_K, q6_K, q8_0, f16; FLASH_ATTN_EXT; and 16 non-matmul ops; 572 shaders):

  • head (default) vs the flagged series rebased onto aurora-wip with cmatk: all 572 stat lines identical.
  • head with nocmatk vs aurora-wip 138a5228631: all 572 identical.
  • head vs aurora-wip:
group shaders changed instrs ALU GPRs up / down threads up / down
MUL_MAT (6 types) 75 18 −1484 −1314 0 / 12 10 / 0
FLASH_ATTN_EXT 356 167 −14645 −18972 75 / 12 10 / 55
other ops 131 2 −140 −124 0 / 2 2 / 0

Every changed shader has fewer instructions. The two changed shaders in the non-matmul group are the only ones there with a matmad; no shader without a cooperative matrix changed.

The 16x16 GEMM can read its B operand in a second register layout, the
one selected by its transpose_b bit, but the compiler had no way to ask
for it: the packer always encoded the bit clear.

Give 16x16 GEMMs a matrix_type bit for it (AGX3_MATRIX_TRANSPOSE_B),
which the packer encodes as transpose_b, and let cmat_muladd_agx ask for
it with bit 1 of its saturate index (bit 0 stays the integer saturation
it already carried). Nothing sets the new bit yet, so code generation is
unchanged.

Signed-off-by: Satya Benson <satya@benson.earth>
A lane of a 16x16 A operand holds K = k0, k0 + 2, k0 + 4, k0 + 6 of a
row, so a row-major A is a load of four words and a select of a half per
element, or two four-element loads of which half is used. B holds four
consecutive N of a row, so a column-major B is four two-element loads.
Matrix multiplication shaders such as llama.cpp's keep A row-major and B
column-major in shared memory and spend these loads and selects on every
product.

The GEMM pairs A's column bits with B's row bits register by register,
so the bits of K can be renamed freely as long as A and B are renamed
alike: every product stays as it was. Renaming bits 1, 2, 0, 3 of K to
bits 0, 1, 2, 3 puts a lane's four K at four consecutive places in
memory. With transpose_b set the GEMM reads B with K placed as in A, so
a column-major B then loads exactly as a row-major A does. A row-major
A, a column-major B with transpose_b and a row-major B without it become
two four-element loads each, with no selects.

The renaming is invisible only where nothing but the products can tell K
order, so k_relabel_legal() takes it per shader, for all of its 16x16
16-bit A and B operands or for none, and only where those are reached by
nothing but coopMatLoad, products, construction, copies and elementwise
operations: no store, element access, conversion, transpose, bitcast,
reduction, tensor access or call, and no product of a candidate with a
non-candidate. Every access that remains goes through the relabelled
layouts, so loads still find each element where it is. B takes the
transpose_b layout where all of its loads are column-major.

The 16x16 operands exist only where the device offers the 16x16 GEMM, so
nothing changes elsewhere. AGX_MESA_DEBUG=nocmatk restores the previous
code generation.

Signed-off-by: Satya Benson <satya@benson.earth>
A Vulkan test program that runs one 16x16x16 f16 cooperative matrix
product in six shader variants (B row- or column-major from the storage
buffer or shared memory, A and B in padded shared memory, A
column-major) on identity and random inputs, and decodes which element
of A and B the GEMM used at each place. It checks the relabelled
operand layouts and the transpose_b B layout on hardware: every variant
must give the product A*B with A and B read unchanged, with and without
AGX_MESA_DEBUG=nocmatk.

The SPIR-V is generated by gen-spv.sh and checked in, so building the
probe needs only a C compiler and the Vulkan loader.

Signed-off-by: Satya Benson <satya@benson.earth>
@satchlj
satchlj marked this pull request as ready for review November 30, 2026 21:42
@satchlj
satchlj requested review from a team and DjDeveloperr as code owners November 30, 2026 21:42

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

Blockers: None.

Checked:

  • Audited full production diff (agx3_matrix_core.h, agx3_pack_binary.c, agx_compile.c, agx_debug.h, agx_nir_lower_cmat.c) and the standalone gemm16-transpose-probe tool:
    • Verified the K-bit permutation (1, 2, 0, 3) -> (0, 1, 2, 3) across layout_16_ak, layout_16_bk, and layout_16_btk matches the 16x16 GEMM and transpose_b operand bit positions, and layout_run() computes contiguous width/step runs accurately for all row-major and column-major combinations.
    • Verified k_relabel_legal() conservatively whitelists only K-order-invariant intrinsics (cmat_load, cmat_muladd with matching candidate A and B, cmat_construct, cmat_copy, cmat_length, elementwise unary/binary/scalar ops, deref load/store/copy) and fails closed on stores, element extract/insert, conversions, transposes, bitcasts, calls, or any non-intrinsic touching a candidate deref.
    • Verified AGX3_CMAT_MULADD_TRANSPOSE_B (2u) does not collide with integer saturation (bit 0, which is restricted to 8-bit integer GEMM), _Thread_local cmatk has no early exit before reset at the end of agx_nir_lower_cmat(), and AGX_DBG_NOCMATK is confined to the existing AGX_MESA_DEBUG debug bitmask.
    • Scope is strictly M5 (AGX3C) 16x16 cooperative matrices only; all other GPUs compile unchanged.
  • Verified exact-head 7dfa5481dae J775c (M5 Max) hardware evidence: dEQP-VK cooperative_matrix 0 differing of 47,031, llama.cpp test-backend-ops (MUL_MAT, MUL_MAT_ID, FLASH_ATTN_EXT) pass, layout probe 18/18 same/A*B with and without nocmatk, Llama-2-7B Q4_0 -fa 1 pp512 +4.7% (3,234 -> 3,386, tg128 unchanged) and Qwen3.8-2B Q4_K_M pp512 +7.7% (7,889 -> 8,499, tg128 unchanged), and meson --suite asahi 272/273 matching base.
Evidence
  • Head commit: 7dfa5481dae080ffd1f89f40c95d6f552e935771 (MERGEABLE into aurora-wip).
  • Commits: 69072539 (agx: let cmat_muladd_agx ask for transpose_b on a 16x16 GEMM), 4b91783c (agx: relabel K in 16x16 16-bit cooperative matrix operands), 7dfa5481 (asahi: add a 16x16 GEMM operand layout probe).
  • Non-blocking nit: a small host compiler unit test exercising k_relabel_legal() rejection cases (cmat_store, cmat_extract, cmat_convert) would be a nice follow-up.

@RyanTheTide

Copy link
Copy Markdown

@DjDeveloperr, code-owner heads-up: App approval at 7dfa5481dae0 from a different lane. This PR goes into the merge train's next combined A/B (aurora-wip 84ba0bee + #570 #1128 #1180 #1184 #1260 #1266 #1424 #1428 #1430 #1435) on M1 Pro G13S + M4 G16G. If that row is clean, I plan to land it via --admin under the 6-point rule no earlier than one hour from now, unless you post a hold.

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