Repository navigation
agx,borealis: implement VK_NV_cooperative_matrix2 cooperativeMatrixWorkgroupScope - #1278
Conversation
When lowering flexible dimensions on cooperative matrices: 1. Add the piece row/column offset to existing per_element_op row/column parameters instead of overwriting them with immediates, so upstream passes (such as workgroup-scope lowering) can supply subgroup base coordinates that compose cleanly with flexible piece splitting. 2. Handle nir_cmat_call_op_reduce_finish in split_matrix_impl() so partial reductions produced before flexible-dimension splitting split element-for-element across the resulting sub-tiles. Signed-off-by: Satya Benson <satya@benson.earth>
…pScope Lower SCOPE_WORKGROUP cooperative matrices to SCOPE_SUBGROUP by tiling the W = workgroup_size / 32 subgroups into an (sg_m x sg_n) grid: - Accumulator (M x N) is partitioned across (sg_m x sg_n) subgroups; - A (M x K) is partitioned across sg_m rows and replicated across sg_n; - B (K x N) is partitioned across sg_n columns and replicated across sg_m. cmat_muladd, same-use cmat_convert, elementwise ops, tensor_load, and tensor_store execute locally per subgroup without shared memory or barriers. Cross-use cmat_convert, cmat_transpose (including mixed element types), and cross-subgroup cmat_reduce exchange partial tiles through a single shared scratch array allocated only when needed. Also extend lower_reduce_2x2() to handle 8x8 pieces (needed when a workgroup-scope 2x2 reduction divides an accumulator tile to an 8x8 granularity), fix rectangular 16x8 / 8x16 splitting in type_needs_split(), and advertise cooperativeMatrixWorkgroupScope with 128-invocation (32x16x16) and 256-invocation (32x32x16) workgroup flexible dimension shapes. Keep cooperativeMatrixFlexibleDimensionsMaxDimension at 256 because AGX3 has 256 16-bit GPRs/thread (128 32-bit GPRs/thread) and advertising >= 512 causes ggml-vulkan to pick 128x256 f32 workgroup tiles (128 f32 accumulators/lane = 100% of the register file, causing severe spilling). Signed-off-by: Satya Benson <satya@benson.earth>
|
Verified combined
|
There was a problem hiding this comment.
Verdict: MERGE at 715fabe8
Blockers: none (note: the 8 workgroupscope.matrixmuladd_dequant.*.decode CTS failures are the pre-existing AGX3 asr imm#0 packer bug fixed in #1279; merge #1279 alongside or before this).
Checked: no Apple material, no AI attribution, no new board/firmware gates (reuses existing agx_chip_g17_m5() Neural Accelerator path), human Signed-off-by, nir_lower_cooperative_matrix.c split_cmat_call_reduce_finish() and preserved per-element row/col base offsets, agx_nir_lower_cmat.c workgroup-scope lowering, 337/337 asahi unit tests, J775c CTS 1,203 workgroup-scope PASS + 198/198 subgroup reduce* regression PASS, llama-bench neutral with 0 GPU faults.
Evidence
- Inspected
nir_lower_cooperative_matrix.c,hk_shader.c,hk_physical_device.c,agx_nir.h, andagx_nir_lower_cmat.c. - Verified
split_cmat_call_per_element_op()addscmat_desc.rows * r/cmat_desc.cols * conto incomingcall->params[1]/[2]instead of overwriting them, andsplit_cmat_call_reduce_finish()mirrorssplit_cmat_call_reduce(). - Verified on J775c M5 Max (
G17C B1) at exact head715fabe8: 337/337 asahi unit tests, CTS*workgroupscope*1,203 PASS (8 pre-existing dequant failures fixed by #1279),subgroupscope.reduce*198/198 PASS,llama-benchneutral, 0 GPU faults. - Non-blocking naming note: PR title and commit subject use
hk:; preferborealis:in PR titles to match org naming.
|
@DjDeveloperr, code-owner heads-up: this PR has an aurora-silicon-review App approval at |
|
Merge-train landing at
Revert: |
Resolve conflicts with merged #1187 (AGX3_MATRIX_ZERO_ACCUM / FP16xFP16->FP16 coopmat) and #1278 (cooperativeMatrixWorkgroupScope): - Move AGX3_MATRIX_SATURATE to (1u << 10) alongside AGX3_MATRIX_ZERO_ACCUM (1u << 9) - Combine zero_accum and is_int8 operand checks in agx3_pack_binary.c and agx_validate.c - Skip INT8/UINT8 in 8x8-dependent workgroup wg_shapes (32x16@128, 32x32@256) in hk_physical_device.c and guard needs_16x16_pieces()/agx_nir_lower_cmat_workgroup() against falling back to 8x8 tiles for INT8/UINT8 (iGeMM16x16x16 is 16x16 only) Signed-off-by: Satya Benson <satya@benson.earth>
What
Implements
VK_NV_cooperative_matrix2cooperativeMatrixWorkgroupScopein Borealis (agx+hk) on Apple M5 (G17C) and later Neural Accelerator GPUs:nir/cmat: preserve per-element offsets and split reduce_finish(src/compiler/nir/nir_lower_cooperative_matrix.c)(row, col)offsets onto existingper_element_oprow/col parameters instead of overwriting them with immediates, so subgroup base coordinates from workgroup-scope lowering compose cleanly with flexible-dimension splitting.nir_cmat_call_op_reduce_finishinsplit_matrix_impl()so cross-subgroup partial reductions split element-for-element across flexible sub-tiles.agx,hk: implement VK_NV_cooperative_matrix2 cooperativeMatrixWorkgroupScope(src/asahi/compiler/agx_nir.h,src/asahi/compiler/agx_nir_lower_cmat.c,src/asahi/vulkan/hk_shader.c,src/asahi/vulkan/hk_physical_device.c)agx_nir_lower_cmat_workgroup(), solving a(sg_m x sg_n)subgroup grid per shader (sg_m * sg_n == workgroup_size / 32, preferring 16x16 tiles with 8x8 fallback) and partitioningSCOPE_WORKGROUPmatrices intoSCOPE_SUBGROUPslices:(M x N)->(M/sg_m) x (N/sg_n)per subgroup(M x K)->(M/sg_m) x K(replicated acrosssg_n)(K x N)->K x (N/sg_n)(replicated acrosssg_m)cmat_muladd, same-usecmat_convert, elementwise ops,tensor_load, andtensor_storerun purely subgroup-locally with zero shared memory and zero barriers. Cross-usecmat_convert,cmat_transpose(including mixed element types), and cross-subgroupcmat_reduce(Row / Column / RowAndColumn / 2x2) exchange partial tiles through a single shared scratch array allocated only when needed.lower_reduce_2x2()/reduce_2x2_layouts_match()to support 8x8 pieces (needed when workgroup-scope 2x2 reduction divides an accumulator tile to 8x8 granularity), and fixes rectangular16x8/8x16splitting intype_needs_split()..cooperativeMatrixWorkgroupScope = supported_extensions->NV_cooperative_matrix2, adds(FP16, FP16) -> FP16to flexible dimension configs, and advertisesVK_SCOPE_WORKGROUP_KHRflexible dimension properties for 128-invocation (32x16x16) and 256-invocation (32x32x16) workgroups.cooperativeMatrixFlexibleDimensionsMaxDimensionat256(see Risk/Not yet below).Tested
Tested at exact HEAD
715fabe8b9aon Apple M5 Max (J775c, 40-coreG17C B1, Borealis):meson test -C build --suite asahi— 337/337 OK, 0 FAIL.dEQP-VK.compute.pipeline.cooperative_matrix.*workgroupscope*):matrixmuladd,add/sub/mul,convert,transposeincluding mixed-typetranspose_dst_khr_b,reduce_*_row,reduce_*_col,reduce_*_row_and_col,reduce_*_2x2for both 16x16 and 8x8,per_element,tensorlayout,buffer,workgroup,physical_buffer).workgroupscope.matrixmuladd_dequant.*.decode, which hit the same pre-existingaurora-wipAGX3 binary packer refusal (asr [d32 s0=32 s1=16:imm#0]) that already failssubgroupscope.matrixmuladd_dequant.*.decode.dEQP-VK.compute.pipeline.cooperative_matrix.khr_r.subgroupscope.reduce*): 198/198 PASS, 0 FAIL.llama-bench(Llama-2-7B Q4_0,-fa 1,DRIRC_CONFIGDIR=/usr/share/drirc.d):NV_coopmat2(MaxDimension = 256):pp512 = 2,292.97 ± 0.16 tok/s,tg128 = 107.79 ± 0.06 tok/s.GGML_VK_DISABLE_COOPMAT2=1(KHR_coopmat):pp512 = 2,293.95 ± 1.37 tok/s,tg128 = 108.30 ± 0.02 tok/s(zero regression).dmesg: 0 GPU faults / 0 hangs.Risk
agx_nir_lower_cmat_workgroup()is a no-op when noSCOPE_WORKGROUPcooperative matrix variables exist.lower_reduce_2x2()8x8 support andnir_lower_cooperative_matrix.cchanges were verified againstsubgroupscope.reduce*(198/198 PASS) and--suite asahi(337/337 OK).Not yet
cooperativeMatrixFlexibleDimensionsMaxDimensionremains 256 (not 512): When tested at512, upstreamllama.cpp(ggml-vulkan) selects its NVIDIA Hopper/Ada workgroup tilel_warptile_mmq = {256, 128, 256, 64, 1}(128x256f32accumulators across 256 threads = 128f32registers/lane = 256 half-registers = 100% of AGX3's architectural register file), explodingmul_mm_cm2to 11,835 spills and regressingpp512from2,293to448 tok/s. RaisingMaxDimensionto 512 should wait untilggml-vulkanadds AGX-sized coopmat2 tile shapes (e.g.64x128/128x64orf16accumulation).matrixmuladd_dequant.*.decodetests (both subgroup and workgroup scope) remain blocked on the separate pre-existing AGX3asrimmediate-0 binary packer issue.G17C B1);G17P/G17S/G17Dhardware rows not yet run.