Repository navigation
agx: relabel K in 16x16 16-bit cooperative matrix operands - #1430
Merged
Merged
Conversation
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
marked this pull request as ready for review
November 30, 2026 21:42
There was a problem hiding this comment.
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 standalonegemm16-transpose-probetool:- Verified the K-bit permutation
(1, 2, 0, 3) -> (0, 1, 2, 3)acrosslayout_16_ak,layout_16_bk, andlayout_16_btkmatches the 16x16 GEMM andtranspose_boperand bit positions, andlayout_run()computes contiguouswidth/stepruns accurately for all row-major and column-major combinations. - Verified
k_relabel_legal()conservatively whitelists only K-order-invariant intrinsics (cmat_load,cmat_muladdwith 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 cmatkhas no early exit before reset at the end ofagx_nir_lower_cmat(), andAGX_DBG_NOCMATKis confined to the existingAGX_MESA_DEBUGdebug bitmask. - Scope is strictly M5 (
AGX3C) 16x16 cooperative matrices only; all other GPUs compile unchanged.
- Verified the K-bit permutation
- Verified exact-head
7dfa5481daeJ775c (M5 Max) hardware evidence:dEQP-VKcooperative_matrix0differing of47,031,llama.cpptest-backend-ops(MUL_MAT,MUL_MAT_ID,FLASH_ATTN_EXT) pass, layout probe18/18same/A*Bwith and withoutnocmatk, Llama-2-7B Q4_0-fa 1pp512+4.7%(3,234 -> 3,386, tg128 unchanged) and Qwen3.8-2B Q4_K_M pp512+7.7%(7,889 -> 8,499, tg128 unchanged), andmeson --suite asahi272/273matching base.
Evidence
- Head commit:
7dfa5481dae080ffd1f89f40c95d6f552e935771(MERGEABLEintoaurora-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.
This was referenced Nov 30, 2026
Merged
borealis,agx: skip provably dead shared zero-init and fold saturating pixel-centre conversions
#1266
Merged
|
@DjDeveloperr, code-owner heads-up: App approval at |
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.
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_blayout. 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 vsAGX_MESA_DEBUG=nocmatkin the same library (5 reps, ABBA ×2): Llama-2-7B Q4_0-fa 1pp512 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-VKcooperative_matrix0 differing of 47,031 vs40acc58f345; llama.cpptest-backend-opsMUL_MAT,MUL_MAT_ID,FLASH_ATTN_EXTall pass; layout probe 18/18same/A*Bwith and withoutnocmatk. Offline: default codegen identical to the flagged build on all 572 llama.cpp shaders,nocmatkidentical toaurora-wip; mesonasahi272/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 1runs above are faster, andtest-backend-opsFLASH_ATTN_EXTpasses.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_mmkeeps 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. Withtranspose_bset, the GEMM reads B with, in element e of lane l,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_band 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 butcoopMatLoad, 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 thetranspose_blayout 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.
agx: let cmat_muladd_agx ask for transpose_b on a 16x16 GEMM: a matrix_type bit the packer encodes astranspose_b, requested through bit 1 ofcmat_muladd_agx's saturate index. No codegen change.agx: relabel K in 16x16 16-bit cooperative matrix operands: the lowering.AGX_MESA_DEBUG=nocmatkrestores the previous codegen.asahi: add a 16x16 GEMM operand layout probe:src/asahi/tools/gemm16-transpose-probe, a Vulkan program (checked-in SPIR-V, builds withcc) 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 givesame/same/A*B, with and withoutnocmatk.Evidence
Hardware at the head (J775c,
7dfa5481dae, default vsnocmatk, same library, 5 reps per arm, ABBA ×2; every default run above everynocmatkrun):nocmatk-fa 1pp512-fa 1pp2048-fa 1tg128Hardware, earlier (J775c, M5 Max; the lowering of this PR behind
AGX_MESA_DEBUG=cmatkat5dd842faagainst its base40acc58f345, same session, 5 reps per arm, ABBA ×2; every flagged run above every baseline run):40acc58f345-fa 1pp512-fa 1pp2048cooperative_matrix: 47,031 cases, 0 differing from40acc58f345.gemm16-transpose-probe: all 18 results (6 variants × 3 inputs, includingab_shared_paddedanda_col_global)same/A*B.Offline (at
79946772aa3, the same patch as the head on138a5228631; llama.cpptest-backend-opscompiled for M5 through the asahi drm shim withAGX_MESA_DEBUG=shaderdb:MUL_MATfor q4_0, q4_K, q5_K, q6_K, q8_0, f16;FLASH_ATTN_EXT; and 16 non-matmul ops; 572 shaders):aurora-wipwithcmatk: all 572 stat lines identical.nocmatkvsaurora-wip138a5228631: all 572 identical.aurora-wip: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.