Repository navigation
Borealis,agx: enable AGX2 cooperative matrix and bfloat16 - #954
Conversation
…uard Define the AGX2 8x8x8 simd_matrix_fmadd32 and simd_matrix_fmadd16 opcodes, pack bit 63, forbid saturate and inline-immediate/fmov folding on paired fragment operands, add RA register-width accounting, and prevent early kills from aliasing destination D with A or B (D == C stays allowed). Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
…or AGX2 Add widen_bf16_inputs to agx_nir_lower_cmat(), lower cmat_muladd_agx to simd_matrix_fmadd32/16 on AGX2, and run agx_nir_lower_f2bf_rtne plus lower_bfloat16_conversions on AGX2 while restoring nir->options. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
…ring Add agx_nir_lower_cmat_sw() to lower cooperative matrices for AGX2 workgroups without guaranteed 32-lane occupancy using distributed ownership, subgroup shuffles, scalar ALU, and exact bf16 arithmetic. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
… AGX2 Enable AGX_CAP_COOPERATIVE_MATRIX for admitted AGX2 chips (G13/G14, excluding G15P), advertise f16 accumulation for 8x8x8 shapes in Borealis, and route AGX2 compute shaders between hardware lowering and agx_nir_lower_cmat_sw() while re-lowering compute system values. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Cover bf16 widening and f16 accumulation in NIR lowering, AGX2 end-to-end compilation + disassembly across all five 8x8x8 shapes, RA non-overlap between destination and A/B inputs, and partial-subgroup software fallback compilation with 64-bit PhysicalStorageBuffer access. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Codex Review Summary: ❌ FailedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
The account paying for this security review has reached its Codex usage limits. The payer can check the Codex usage dashboard. For personal accounts, using credits requires enabling “Use credits for security reviews” in Code review settings. If you do not manage the paying account, contact this repository's admins. |
|
Timing on Apple M1 Max (G13C), 5-pair ABBA vs the shipped lab-default Borealis build (Qwen3-4B, VK_ICD_FILENAMES arm switch, gpu-turn serialized):
ids_sha identical on every leg (a6fad159d on ctx512) — output is bit-exact vs lab default, deterministic run-to-run. For scale: bare aurora-wip head measures 8820 ms on the same prefill cell, so this port closes ~92% of the prefill gap and its barrier handling is correct (a straight cherry-pick of the shipped barrier commits produced run-to-run nondeterminism here). Caveat on the decode deltas: the staged ICD is a debugoptimized build (88 MB .so vs ~27 MB release). Part of the ~7-11% decode gap is likely build-type overhead; a release-build retime is worth one pass before calling the delta final. |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Verdict: FINISH at a29dd51a05b4
Blockers:
PR body / Tested:(§5) — TheTested:section cites the pre-rebase diff (d4eb305ca003+194c2386c1c9) rather than exact PR heada29dd51a05b4, and omits kernel version plus the regression health check (0SELinux denials,0failed units). Please updateTested:with an exact-heada29dd51a05b4hardware row (confirming the CTS/unit results and release-build decode timing without thedebugoptimized-7.4%/-11.2%decode delta, plus0SELinux denials /0failed units /0new kernel GPU errors), and record or request aG14(M2) + AGX3 non-regression check since shared compiler/Borealis paths are touched.
Checked: All 5 commits (3350d63ec6f7..a29dd51a05b4, 0 code blockers, MERGEABLE vs aurora-wip 62ccb5f402b1), §1–§6 pre-PR checklist (§1–§4 & §6 pass; §5 exact-head Tested: row pending), agx_chip.h/agx_chip_caps.h parity, AGX2 ISA/RA/lowering/SW-fallback, and AGX3 non-impact.
Evidence
Checklist (§1–§6)
- §1 No model gating: Pass. Gated via
AGX_ISA_ENCODING_AGX2,agx_chip_cooperative_matrix_supported(), andAGX_CAP_COOPERATIVE_MATRIX.chip != AGX_CHIP_G15Pis documented (uncharacterized A16 part, not admitted byagx_chip_driver_supported()). - §2 Production-ready: Pass. Default command line, no new env vars, flags, delays, or lab-only paths.
- §3 Safety: Pass (N/A; userspace Mesa compiler/driver).
- §4 IP & Attribution: Pass. All 5 commits carry human author/committer (
Joshua Warren) andSigned-off-by:; zero AI attribution in commits, PR title/body, diff, or comments; no copyrighted Apple material. - §5 Evidence: Needs follow-up (blocker above). PR body
Tested:cites pre-rebased4eb305ca003+194c2386c1c9; follow-up comment tests MLX timing at exact heada29dd51a05b4on Apple M1 Max (G13C) with bit-exact outputs (ids_shaidentical) and ~92% prefill gap closure vs bareaurora-wip, but on adebugoptimizedbuild (-7.4%/-11.2%decode vs lab default).G14(M2) hardware verification is listed inNot yet:.What:,Tested:,Risk:,Not yet:lines are all present. - §6 Naming: Pass. Uses Borealis in prose; existing
hk_/src/asahi/vulkan/identifiers preserved in code.
Code & Commit Audit
3350d63ec6f7(asahi/isa,compiler: add AGX2 simd_matrix_fmadd32/16 encoding and RA guard):AGX2.xml&agx_opcodes.py: opcode0x6F, bit 27 =1(disjoint fromshufflebit 27 =0andSubgroup arithmeticbits 26:27 =00), bit 26 = precision (1fp32 /0fp16), bit 63 =1, fixed 8-byte non-extensible encoding; extend bits (54..61) do not collide with bit 63.agx_pack.c,agx_optimizer.c,agx_validate.c,agx_performance.c:is_float = Falseblocksfmov/saturatefolding;agx_optimizer_inline_immskips both opcodes;agx_pack_aluasserts!I->saturate;agx_validate.caccounts for paired-register widths viaagx_index_size_16.agx_register_allocate.c:early_killable()prevents early-killingA/B(s < 2) while allowingD == C(s == 2). Becauseagx_liveness_ins_updatesets.killonly on the first SSA source occurrence,Ccannot be early-killed if it aliasesAorB.
a83071f1af5c(asahi/compiler: lower 8x8 cooperative matrices and bf16 conversions for AGX2):widen_bf16_inputsinagx_nir_lower_cmat()isfalsefor AGX3B/AGX3C (no change to M3/M4/M5).agx_compile.clowerscmat_muladd_agxonAGX_ISA_ENCODING_AGX2tosimd_matrix_fmadd16/32, runsagx_nir_lower_f2bf_rtne+lower_bfloat16_conversionson AGX2 (both epilog preopt and main compile), and restoresnir->options = original_optionsunconditionally (with no early returns in between).
525cdea94a69(asahi/compiler: add partial-subgroup software cooperative matrix lowering):agx_nir_lower_cmat_sw.c: distributed ownershipe % A(A = min(32, W - 32 * subgroup_id)), subgroup shuffles formuladd/convert/transpose, local ownership forextract/insert/copy/bitcast/alu/store, exact fp32 compute + RTNE roundback for bf16, handles 64-bitPhysicalStorageBufferand 32-bit pointers, andnir_foreach_block_reverse_safe/nir_foreach_instr_reverse_safeis safe withnir_push_ifsplits inlower_store.
49e3384c7dfce(asahi: enable VK_KHR_cooperative_matrix and VK_KHR_shader_bfloat16 on AGX2):agx_chip.hvsagx_chip_caps.h: exact parity across all 19 chips +AGX_CHIP_UNKNOWN(G13G/G13X/G14G/G14Xenabled;G15PandG18Gdisabled; AGX3B/M5 unchanged;AGX_CAP_COOPERATIVE_MATRIX = 1u << 7reused with no bit collision). The 2asahi-agx-chip-capsfailures noted by the author are pre-existingaurora-wipG16G/G17Pmismatches (AGX_CAP_NATIVE_PREAMBLE/AGX_CAP_VDM_INDIRECT_INDEX_BOUNDS).hk_physical_device.c&hk_shader.c: enablesKHR_cooperative_matrixandKHR_shader_bfloat16viaagx_chip_cooperative_matrix_supported(), advertisesf16accum on AGX2 + AGX3B (NV_cooperative_matrix2stays M5-only), routes AGX2 compute shaders between hardware lowering (nonzero static multiple of 32) andagx_nir_lower_cmat_sw(), and re-runsnir_lower_compute_system_valuesafter lowering. AGX3B/AGX3C path is unchanged.
a29dd51a05b4(asahi/compiler: add AGX2 cooperative matrix unit tests):test-agx2-cmat.cppcovers NIR bf16 widening + f16 accum, end-to-end AGX2 compilation + disassembly across all five 8x8x8 shapes, RA non-overlap betweenDandA/B, and partial-subgroup 64-bitPhysicalStorageBufferbf16 software fallback; updatestest-agx3-cmat-mixed-mma8.c,test-native-form-regressions.cpp,test-chip-admission.c, andtest-chip.c.
CI Status
statusCheckRollup: empty (expected onaurora-silicon/mesa; CI runs on mirror).- Mergeability:
MERGEABLEagainstaurora-wip(62ccb5f402b1).
Non-Blocking Notes
src/asahi/isa/AGX2.xml:987-990/src/asahi/isa/test/cases.txt:406:simd_matrix_fmadd32uses<fsrc1/>(bits16:27), which overlaps<exact bit="26">1</exact>and<exact bit="27">1</exact>, causingagx2_disassembleto print spurious.abs.negonsrc1(simd_matrix_fmadd32 r2l, ^r1l.abs.neg, ^r3l, ^r4l). Fine to clean up in a follow-up with a non-modifier source encoding.src/asahi/compiler/agx_pack.c:545-549: optional hardening nit —pack_assert(I, I->src[s].type == AGX_INDEX_REGISTER)forsimd_matrix_fmadd32/16sources would guard against accidental non-register operand folding in future passes.
satchlj
left a comment
There was a problem hiding this comment.
Verdict at a29dd51a05b4
APPROVE — Clean AGX2 cooperative matrix (simd_matrix_fmadd32/16, RA non-aliasing guard, NIR lowering + partial-subgroup software fallback) and VK_KHR_shader_bfloat16 enablement with exact agx_chip.h/agx_chip_caps.h parity for AGX2 and verified zero regression on Apple M5 Max (G17C B1) Borealis cooperative-matrix and compute workloads.
Blockers
None (code, capability-table parity, unit suite, and M5 Max non-regression all clean; see follow-up note in the earlier review comment regarding refreshing the PR body Tested: metadata to exact head a29dd51a05b4).
Checked
- Full 5-commit diff (
+1034/-45across 22 files):- AGX2 ISA & RA:
simd_matrix_fmadd32/16(0x6F, bit 27 =1, bit 26 precision, bit 63 =1, fixed 8-byte) cleanly guarded inagx_pack.c/agx_optimizer.c/agx_validate.c;early_killable()inagx_register_allocate.ckeepsA/B(s < 2) live untilDis placed while still allowingD == C. - NIR lowering & software fallback:
widen_bf16_inputsstaysfalseon AGX3B/AGX3C;agx_compile.clowersf2bfRTNE and restoresnir->optionsunconditionally;agx_nir_lower_cmat_sw.chandles partial subgroups, 64-bitPhysicalStorageBuffer, and in-place aliasing safely, withhk_shader.cre-runningnir_lower_compute_system_valuesright after lowering. - Capability parity:
AGX_CAPS_AGX2addsAGX_CAP_COOPERATIVE_MATRIXwhileG15Pstays0, matchingagx_chip_cooperative_matrix_supported(). Verified viaasahi-agx-chip-caps(0failures onG13G/G13X/G14G/G14X/G15PorAGX_CAP_COOPERATIVE_MATRIX; only the 2 pre-existingaurora-wipG16G/G17Pchecks fail, resolved in #955). - Feature advertisement:
hk_physical_device.cadvertisesshaderBFloat16TypeandshaderBFloat16CooperativeMatrix, keepsshaderBFloat16DotProduct = false, and keepsNV_cooperative_matrix2M5-only.
- AGX2 ISA & RA:
- Isolated release build (
-Dtools=drm-shim -Dbuild-tests=true), full non-layoutasahiunit suite (262/263PASS; sole failure is pre-existingaurora-wipasahi-agx-chip-capsG16G/G17P), plusAGX2Cmat.*,NativeForms.CooperativeMatrix*,agx2_disasm(558/558),agx3_cmat_mixed_mma8,asahi-agx-chip(839/839), andasahi-chip-admission. - Live Apple M5 Max (
G17C B1), Linux 7.3 Borealis Vulkan CTS non-regression (MESA_SHADER_CACHE_DISABLE=1,0kernel GPU faults). - Untested scope: Live AGX2 (
G13/G14) hardware execution (tested on this host via AGX2 compiler/RA/disasm/SW-fallback unit suites and live M5 Max AGX3C non-regression);asahi_layout_testsexcluded due to pre-existing GCC 16 C++20 designated-initializer build issue.
Evidence
- Exact head tested:
a29dd51a05b4 - Unit suite (non-layout
asahi):262/263PASS (1pre-existingaurora-wipasahi-agx-chip-capsG16G/G17Pmismatch;0failures on AGX2/G15P/AGX_CAP_COOPERATIVE_MATRIX)agx_tests --gtest_filter="AGX2Cmat.*:NativeForms.CooperativeMatrix*":5/5PASSagx2_disasm:558/558PASSagx3_cmat_mixed_mma8:PASSasahi-agx-chip:839/839PASSasahi-chip-admission:ok
- Live Vulkan CTS non-regression (Apple M5 Max
G17C B1, Linux 7.3,MESA_SHADER_CACHE_DISABLE=1):dEQP-VK.api.info.get_physical_device_properties2.features.cooperative_matrix_features_khr:1/1PASSdEQP-VK.api.info.get_physical_device_properties2.features.shader_bfloat16_features_khr:1/1PASSdEQP-VK.compute.pipeline.cooperative_matrix.khr_a.subgroupscope.add.float16_float16.buffer.*:2/2PASSdEQP-VK.compute.pipeline.cooperative_matrix.khr_a.subgroupscope.add.float32_float32.buffer.*:2/2PASSdEQP-VK.compute.pipeline.basic.*:80/81PASS (1Not supported,0failed)dEQP-VK.glsl.loops.special.*:552/552PASS
- Kernel
dmesg:0new GPU faults/errors.
|
The approved head |
Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
|
Exact-head
The release build remains
|
|
Timing on M1 (13-inch, G13G), merged release head e44adb8 ICD (sha256 867de407), 5-pair ABBA vs lab default, Qwen3-4B 4-bit:
Notes:
|
|
Timing on M1 Max (16-inch), same merged release head e44adb8 ICD (sha256 867de407), 5-pair ABBA vs lab default, Qwen3-4B 4-bit:
Bit-exact on every leg on both machines. Combined with the M1 row below: decode delta is -1.8%/-3.5% on M1 (G13G) and -3.9%/-9.0% on M1 Max (G13D); the +13% d4b prefill on M1 Max is the one number that stands out, worth a look at the 512-token path. |
What: Port AGX2 (
G13/G14) subgroup-scope cooperative matrix and bfloat16 support ontoaurora-wip:simd_matrix_fmadd32/simd_matrix_fmadd16ISA decoding, opcode packing (bit 63, nosaturateor scalar operand folding), register-width validation, and RA non-aliasing guard preventing destinationDfrom overwriting killedAorBinputs (D == Cstays permitted).widen_bf16_inputstoagx_nir_lower_cmat(), lowercmat_muladd_agxtosimd_matrix_fmadd32/16on AGX2, and runagx_nir_lower_f2bf_rtne+lower_bfloat16_conversionson AGX2 while restoringnir->optionsunconditionally.agx_nir_lower_cmat_sw()as a partial-subgroup software fallback (distributed element ownership, subgroup shuffles, scalar ALU, and exact bf16 arithmetic) for workgroups whose static size is not a multiple of 32 or is variable.AGX_CAP_COOPERATIVE_MATRIXfor admitted AGX2 chips (G13G/G13X/G14G/G14X, excluding uncharacterizedG15P), advertisef16accumulation for 8x8x8 shapes in Borealis, and route AGX2 compute shaders between hardware lowering andagx_nir_lower_cmat_sw().AGX2Cmatunit tests covering NIR lowering, AGX2 compilation/disassembly across all five 8x8x8 shapes, RA input/output disjointness, and partial-subgroup 64-bitPhysicalStorageBuffersoftware lowering.Tested:
--suite asahiat coopmat diffd4eb305ca003+ PR agx: classify helper/stack ops as barriers and serialize stack_adjust #951 (194c2386c1c9): 181/182 binaries passed (AGX2Cmat.*4/4,M5CopySchedule.AGX2Helper*/StackAdjust*2/2,asahi-agx-chip839/839,agx2_disasm,agx3_cmat_mixed_mma8,asahi-chip-admission,hk_pack); the sole failing binary wasasahi-agx-chip-capson the two pre-existingaurora-wipG16G/G17Pmismatches (364/366checks passed,0failures onG13/G14/G15P).Apple M1 Max (G13C C0)) using cross-builtlibvulkan_asahi.so(sha256 d5dcf4cc92a614153f804b7f72d198ef1f47157ea4ba7533815a2ea4163cbbca) from the identical coopmat diff (d4eb305ca003) + PR agx: classify helper/stack ops as barriers and serialize stack_adjust #951 (now landed inaurora-wipbase62ccb5f402b1): verifiedVK_KHR_cooperative_matrix,VK_KHR_shader_bfloat16,shaderBFloat16Type,shaderBFloat16CooperativeMatrix, and all five8x8x8shapes (fp32->fp32,fp16->fp32,fp16->fp16,bf16->fp32,bf16->bf16).Apple M1 Max (G13C C0):624Pass,2902NotSupported,0Fail across 3526 selectedapi.*cooperative_matrix/bfloat16,compute.pipeline.cooperative_matrix.64b_indexing.*,khr_{a,b,r}.subgroupscope.*linear,glsl.bfloat16.*, andspirv_assembly.instruction.*cases (including 200matrixmuladd*passes), plus207Pass /2939NotSupported /0Fail across 3146 remaining non-tensorsubgroupscopecases, and bit-exact 64/64 compute probes for partial-subgrouplocal_size_x=16software fallback (fp32,bf16->bf16) andlocal_size_x=32hardware (fp16->fp16,bf16->bf16).0newdmesgGPU faults or timeouts.Risk: two-way · M1, M2 (G13, G14) (reverting cleanly disables
VK_KHR_cooperative_matrixandVK_KHR_shader_bfloat16advertisement and lowering on AGX2 without affecting AGX3/M5).Not yet:
G14(M2) hardware verification and full MLX prefill (pf2048) / decode benchmark comparison.