Skip to content

agx,borealis: implement VK_NV_cooperative_matrix2 cooperativeMatrixWorkgroupScope - #1278

Merged
RyanTheTide merged 2 commits into
aurora-wipfrom
satchlj/borealis-coopmat2-workgroup
Nov 28, 2026
Merged

RyanTheTide merged 2 commits into
aurora-wipfrom
satchlj/borealis-coopmat2-workgroup

Conversation

@satchlj

@satchlj satchlj commented Nov 28, 2026

Copy link
Copy Markdown
Member

What

Implements VK_NV_cooperative_matrix2 cooperativeMatrixWorkgroupScope in Borealis (agx + hk) on Apple M5 (G17C) and later Neural Accelerator GPUs:

  1. nir/cmat: preserve per-element offsets and split reduce_finish (src/compiler/nir/nir_lower_cooperative_matrix.c)

    • Adds piece (row, col) offsets onto existing per_element_op row/col parameters instead of overwriting them with immediates, so subgroup base coordinates from workgroup-scope lowering compose cleanly with flexible-dimension splitting.
    • Splits nir_cmat_call_op_reduce_finish in split_matrix_impl() so cross-subgroup partial reductions split element-for-element across flexible sub-tiles.
  2. 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)

    • Adds 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 partitioning SCOPE_WORKGROUP matrices into SCOPE_SUBGROUP slices:
      • Accumulator (M x N) -> (M/sg_m) x (N/sg_n) per subgroup
      • A (M x K) -> (M/sg_m) x K (replicated across sg_n)
      • B (K x N) -> K x (N/sg_n) (replicated across sg_m)
    • cmat_muladd, same-use cmat_convert, elementwise ops, tensor_load, and tensor_store run purely subgroup-locally with zero shared memory and zero barriers. Cross-use cmat_convert, cmat_transpose (including mixed element types), and cross-subgroup cmat_reduce (Row / Column / RowAndColumn / 2x2) exchange partial tiles through a single shared scratch array allocated only when needed.
    • Extends 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 rectangular 16x8 / 8x16 splitting in type_needs_split().
    • Enables .cooperativeMatrixWorkgroupScope = supported_extensions->NV_cooperative_matrix2, adds (FP16, FP16) -> FP16 to flexible dimension configs, and advertises VK_SCOPE_WORKGROUP_KHR flexible dimension properties for 128-invocation (32x16x16) and 256-invocation (32x32x16) workgroups.
    • Keeps cooperativeMatrixFlexibleDimensionsMaxDimension at 256 (see Risk/Not yet below).

Tested

Tested at exact HEAD 715fabe8b9a on Apple M5 Max (J775c, 40-core G17C B1, Borealis):

  • Unit tests: meson test -C build --suite asahi — 337/337 OK, 0 FAIL.
  • Vulkan CTS (dEQP-VK.compute.pipeline.cooperative_matrix.*workgroupscope*):
    • 1,203 PASS, 8 FAIL, 7,080 NotSupported, 0 GPU faults, 0 crashes.
    • All 1,203 non-dequant runnable workgroup-scope tests pass (matrixmuladd, add/sub/mul, convert, transpose including mixed-type transpose_dst_khr_b, reduce_*_row, reduce_*_col, reduce_*_row_and_col, reduce_*_2x2 for both 16x16 and 8x8, per_element, tensorlayout, buffer, workgroup, physical_buffer).
    • The 8 failures are workgroupscope.matrixmuladd_dequant.*.decode, which hit the same pre-existing aurora-wip AGX3 binary packer refusal (asr [d32 s0=32 s1=16:imm#0]) that already fails subgroupscope.matrixmuladd_dequant.*.decode.
  • Vulkan CTS regression check (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):
    • With NV_coopmat2 (MaxDimension = 256): pp512 = 2,292.97 ± 0.16 tok/s, tg128 = 107.79 ± 0.06 tok/s.
    • Baseline 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

  • Low risk to existing subgroup-scope shaders: agx_nir_lower_cmat_workgroup() is a no-op when no SCOPE_WORKGROUP cooperative matrix variables exist.
  • lower_reduce_2x2() 8x8 support and nir_lower_cooperative_matrix.c changes were verified against subgroupscope.reduce* (198/198 PASS) and --suite asahi (337/337 OK).

Not yet

  • cooperativeMatrixFlexibleDimensionsMaxDimension remains 256 (not 512): When tested at 512, upstream llama.cpp (ggml-vulkan) selects its NVIDIA Hopper/Ada workgroup tile l_warptile_mmq = {256, 128, 256, 64, 1} (128x256 f32 accumulators across 256 threads = 128 f32 registers/lane = 256 half-registers = 100% of AGX3's architectural register file), exploding mul_mm_cm2 to 11,835 spills and regressing pp512 from 2,293 to 448 tok/s. Raising MaxDimension to 512 should wait until ggml-vulkan adds AGX-sized coopmat2 tile shapes (e.g. 64x128 / 128x64 or f16 accumulation).
  • The 8 matrixmuladd_dequant.*.decode tests (both subgroup and workgroup scope) remain blocked on the separate pre-existing AGX3 asr immediate-0 binary packer issue.
  • Only tested on Apple M5 Max (G17C B1); G17P / G17S / G17D hardware rows not yet run.

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

satchlj commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

Verified combined #1278 (satchlj/borealis-coopmat2-workgroup) + #1279 (satchlj/agx3-asr-imm-range) on Apple M5 Max (J775c, G17C B1, Borealis Vulkan, libvulkan_asahi.so SHA-256 a31d5c00e3b85e01e11c4a9b8245289c8c2b72915bf858bd68ea1ff06f67c9d8):

  • *workgroupscope*matrixmuladd_dequant*: 24/24 supported Passed (0 Failed, 36 Not supported — all previously failing dequant cases now PASS).
  • *subgroupscope*matrixmuladd_dequant*: 24/24 supported Passed (0 Failed, 36 Not supported).
  • dmesg: 0 GPU/MMU/asahi faults.

@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 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, and agx_nir_lower_cmat.c.
  • Verified split_cmat_call_per_element_op() adds cmat_desc.rows * r / cmat_desc.cols * c onto incoming call->params[1]/[2] instead of overwriting them, and split_cmat_call_reduce_finish() mirrors split_cmat_call_reduce().
  • Verified on J775c M5 Max (G17C B1) at exact head 715fabe8: 337/337 asahi unit tests, CTS *workgroupscope* 1,203 PASS (8 pre-existing dequant failures fixed by #1279), subgroupscope.reduce* 198/198 PASS, llama-bench neutral, 0 GPU faults.
  • Non-blocking naming note: PR title and commit subject use hk:; prefer borealis: in PR titles to match org naming.

@satchlj satchlj changed the title agx,hk: implement VK_NV_cooperative_matrix2 cooperativeMatrixWorkgroupScope agx,borealis: implement VK_NV_cooperative_matrix2 cooperativeMatrixWorkgroupScope Nov 28, 2026
@RyanTheTide

Copy link
Copy Markdown

@DjDeveloperr, code-owner heads-up: this PR has an aurora-silicon-review App approval at 715fabe8b9ac from a different lane plus the author's exact-head hardware row. I plan to land it no earlier than 06:29Z, after a combined A/B regression row on two lab GPU families (M1 Pro G13S + M4 G16G). The App approval doesn't count toward the ruleset, so that merge uses --admin under the 6-point rule. Please post a review here if you want it held.

@RyanTheTide

Copy link
Copy Markdown

Merge-train landing at 715fabe8b9ac.

  • Review: aurora-silicon-review App APPROVE at this head by NEON-PR-REVIEW (Gemini lane, different model family from the author), with the author's exact-head J775c (M5 Max, G17C) row (non-author).
  • Feature row: the author's exact-head hardware row on this PR.
  • Regression: combined A/B overlay of aurora-wip plus this PR and the other queued PRs, user space, default kernel command line. M1 Pro (G13S, fleet89): 0 status changes. M4 Pro (G16S, fleet89): 0 regressions. The only flips there are 5 synchronization multi_queue timeouts under 4-job load; all 5 pass 3/3 alone on both arms, and the base arm shows the same flake. Each Mac ran 26,595 dEQP-VK + 8,153 dEQP-GLES cases, with 0 GPU firmware crashes, Enforcing, 0 failed units. Evidence traces/gpu/20261128-mergetrain-mesa.
  • --admin under the 6-point rule: the App approval does not count toward the ruleset. Dj was pinged here more than 1 h ago, and no blocking review is open.

Revert: git revert -m 1 <merge commit> on aurora-wip. A regression gets reverted first and fixed after.

@RyanTheTide
RyanTheTide merged commit 96a1e4c into aurora-wip Nov 28, 2026
@RyanTheTide
RyanTheTide deleted the satchlj/borealis-coopmat2-workgroup branch November 28, 2026 06:33
satchlj added a commit that referenced this pull request Nov 28, 2026
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>
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.

2 participants