Skip to content

metal: port Muse Glimmer NAX quantized GEMM/QMV kernels - #79

Open
GumbiiDigital wants to merge 3 commits into
mainfrom
muse-glimmer/challenge-kernels
Open

GumbiiDigital wants to merge 3 commits into
mainfrom
muse-glimmer/challenge-kernels

Conversation

@GumbiiDigital

@GumbiiDigital GumbiiDigital commented Dec 3, 2026 •

Copy link
Copy Markdown

Branch muse-glimmer/challenge-kernels @ 7581e61e (3 commits on main @ 25c482f4). Pinned to merged Layr-Labs/mlx#73 (merge commit 4ef15bc7d4 on Layr-Labs/mlx main, including 500c5d7e's opt-in MLX_NAX_KSPLIT=0 default, allow_ksplit plumbing for #71, and main's empty-CPU-reduction fix from #54). Companion to Layr-Labs/mlx-swift-lm#580 (ports the production-safe Metal NAX kernel subset from the 9 Vendor/mlx-swift/ files in Layr-Labs/mlxfast-muse-glimmer-30b-engine commits 772ed549..4a28444e / 5b816f3a excluded from #580). None of jonathan308's branches or PRs are touched.

Summary

Advances Source/Cmlx/mlx from 72dffa2d to 4ef15bc7d4 (merged Layr-Labs/mlx#73 on Layr-Labs/mlx main), regenerates the matching Metal JIT sources and copied headers under Source/Cmlx/mlx-generated/, syncs Source/Cmlx/include-framework/mlx-utils.h (env::nax_ksplit(), opt-in default 0), adds M5-routed Swift quantized/MoE/misaligned-fallback parity tests, and updates fork.yaml. Source/Cmlx/mlx-c stays at 2ac897e3; .gitmodules is unchanged.

Ported vs. omitted / dropped audit (all 9 challenge Vendor/mlx-swift/ files)

Challenge file (mlxfast-muse-glimmer-30b-engine 4a28444e) Status Rationale
mlx-generated/metal/quantized_nax.h Ported + Gated (opt-in) (via merged Layr-Labs/mlx#73 + regenerated) Adds qmm_t_nax_staged_ksplit_impl: when M <= SM (M <= 16; reachable for 8 <= M <= 16 via qmm_splitk_nax and 12/13 <= M <= 16 via direct qmm_nax), splits BK=64 into four 16-col slices across all 4 simdgroups, reducing FP32 partials in Ws (12,288 <= 18,432 bytes) with uniform barrier participation. Gated by Metal function constant use_ksplit (index 203) via opt-in MLX_NAX_KSPLIT (default 0), plumbed with allow_ksplit for #71 coordination, and disabled automatically under MLX_QMV_BATCH_INVARIANT=1.
mlx-generated/quantized_nax.cpp Ported + Gated (opt-in) (regenerated) Matching JIT source string for quantized_nax.h.
mlx-generated/metal/fp_quantized_nax.h Ported + Hardened (via merged Layr-Labs/mlx#73 + regenerated) Adds FPQuantizedNAXBlockLoader::fetch_nvfp4 / store_nvfp4, double-buffered Ws (kDoubleBuf, 18 KiB) in fp_qmm_t_impl / fp_qmm_t_nax, and bit-manipulation dequantize_fp4_x4_scaled + M <= 8 (6 <= M <= 8 host-routed) peeled 1-ahead prefetch in fp_qmv_nax. Hardened with threadgroup-uniform (K > 0) && ((K & 31) == 0) and 16-byte wl / 2-byte scales alignment guards, plus if (kn < K) around the final peeled prefetch shift. sizeof(T) == 2 / sizeof(Wtype) == 2 checks the 16-bit threadgroup tile storage width (Wtype = bfloat across transposed/gather kernels for all activation dtypes; activation T in fp_qmm_n_impl).
mlx-generated/fp_quantized_nax.cpp Ported + Hardened (regenerated) Matching JIT source string for fp_quantized_nax.h.
mlx-generated/metal/steel_gemm_fused_nax.h Dropped Early return on sgp_sm <= 0 || sgp_sn <= 0 before mlx::steel::gemm_loop(...) (steel/gemm/gemm_nax.h:54), which executes threadgroup_barrier(mem_flags::mem_none) every kk0 iteration — divergent threadgroup barrier UB (same class of NAX inactive-simdgroup hang already reverted in Layr-Labs/mlx fork.yaml). gemm_loop already skips inactive simdgroups safely after the barrier.
mlx-generated/steel_gemm_fused_nax.cpp Dropped Matching JIT source for dropped steel_gemm_fused_nax.h.
mlx-generated/metal/steel_gemm_splitk_nax.h Dropped Same divergent-barrier early return before mlx::steel::gemm_loop(...) UB as steel_gemm_fused_nax.h.
mlx-generated/steel_gemm_splitk_nax.cpp Dropped Matching JIT source for dropped steel_gemm_splitk_nax.h.
mlx/mlx/backend/metal/kernels/gather_front.cpp (cbv2_array_backing) Dropped Dead contest-only symbol; Layr-Labs/mlx-swift-lm#580 (CBv2ParameterResidency.swift) uses public mlx_array_get_buffer_info + mlx_array_data_uint8 with zero references to cbv2_array_backing.

Additional files in this PR

  • Source/Cmlx/mlx — submodule pin 72dffa2d -> 4ef15bc7d4 (merged Metal NAX: port Muse Glimmer small-M K-split affine GEMM and double-buffered NVFP4 GEMM/QMV mlx#73 on Layr-Labs/mlx main; includes 500c5d7e's opt-in MLX_NAX_KSPLIT=0 default and allow_ksplit parameter, plus main's empty-CPU-reduction guard from #54).
  • Source/Cmlx/include-framework/mlx-utils.h — adds env::nax_ksplit() defaulting to 0 (!= 0), matching mlx/utils.h.
  • Tests/MLXTests/NAXQuantizedKernelTests.swift — 4 XCTests whose shapes match QuantizedMatmul::eval_gpu M5 NAX routing:
    • Affine K-split split-K (N=K=2048, biases present, M in {8, 12, 16, 17}) and direct staged qmm_nax (N=K=4160 = 65*64, M in {13, 16, 17}), asserting bitwise sameBytes parity (M=13/16 vs M=17) whenever K-split is disabled (default MLX_NAX_KSPLIT=0 or MLX_QMV_BATCH_INVARIANT=1), and verifying opt-in MLX_NAX_KSPLIT=1.
    • Unbatched (M=512, N=1024, K=256) and batched (B=2, M=64, N in {128, 96}, K=128) double-buffered NVFP4 fp_qmm_t_nax, plus genuinely misaligned 4-byte w / 1-byte scales offset fallback asserting bitwise sameBytes parity against the aligned fast path.
    • Peeled fp_qmv_nax (K=N=2048, M in {6, 7, 8, 9}) and NVFP4 MoE gather (fp_gather_qmm_t_nax for sortedIndices: false, fp_gather_qmm_rhs_nax for sortedIndices: true) with 4B/1B misaligned bitwise sameBytes parity.
    • NVFP4 QuantizedLinear(..., globalScale:) GEMM route (M=512, N=1024, K=128 -> fp_qmm_t_nax_..._hgs, kDoubleBuf=false).
  • fork.yaml — documents the mlx pin advance (72dffa2d -> 4ef15bc7), mlx-utils.h (env::nax_ksplit() default 0), full NVFP4 blast radius, and NAXQuantizedKernelTests.

Placeholders / pending rollout items

Blast Radius / What This Could Affect

Shared seams touched

File / Symbol Behavior changed Models / subsystems affected (M5+ NAX only) Gating / isolation
Source/Cmlx/mlx (quantized_nax.h, quantized.cpp, mlx/utils.h) + mlx-generated/quantized_nax.{h,cpp} + include-framework/mlx-utils.h qmm_t_nax_staged_ksplit_impl splits BK=64 across 4 simdgroups when M <= 16 (reachable for 8 <= M <= 16 via qmm_splitk_nax and 12/13 <= M <= 16 via qmm_nax). Opt-in only (off by default); when enabled (MLX_NAX_KSPLIT=1), changes FP32 K-reduction summation order (~1 ULP / possible KV-hash delta vs. stock staged and M >= 17). Only when explicitly opted in via MLX_NAX_KSPLIT=1: 16-bit (bf16/fp16) affine-quantized models running 8 <= M <= 16 on M5+ NAX (batched decode, spec-verify, MTP): Muse Glimmer, Qwen, GLM5-Next, Nemotron, Gemma 4, Inkling 2, MiMo. Zero behavior change under default MLX_NAX_KSPLIT=0. metal::is_nax_available() + use_ksplit FC 203 (allow_ksplit && env::nax_ksplit() && !env::qmv_batch_invariant(); defaults to false).
Source/Cmlx/mlx (fp_quantized_nax.h) + mlx-generated/fp_quantized_nax.{h,cpp} Vectorized uint4/uchar2 loads, bit-manipulation half2 E2M1 dequantization, and 18 KiB double-buffered Ws in fp_qmm_t_nax (kDoubleBuf = true for non-hgs NVFP4). 0-ULP bit-identical output (confirmed on M5 Max applegpu_g17s). All NVFP4 models on M5+ NAX across all activation dtypes (float, fp16, bf16): fp_qmm_t_nax (double-buffered), fp_gather_qmm_t_nax (MoE gather, single-buffered fast path), fp_gather_qmm_rhs_nax (nt/nn sorted MoE hot path, via load_unsafe()), and fp_qmm_n_nax (16-bit T when instantiated; not host-routed today). metal::is_nax_available(), bits == 4 && group_size == 16 && !has_global_scale, K > 0 && (K & 31) == 0, 16B wl & 2B scales alignment.
Source/Cmlx/mlx (fp_quantized_nax.h: fp_qmv_nax + dequantize_fp4_x4_scaled) + mlx-generated/fp_quantized_nax.{h,cpp} Bit-manipulation half2 dequantization + 1-ahead peeled prefetch for M <= 8 (6 <= M <= 8 host-routed via qmv_mma). 0-ULP bit-identical output. All NVFP4 models running 6 <= M <= 8 (N >= 2048) on M5+ NAX (Muse Glimmer 30B NVFP4 and other NVFP4 models). metal::is_nax_available(), bits == 4 && group_size == 16, 6 <= M <= 8; MLX_QMV_BATCH_INVARIANT=1 bypasses qmv_mma.
Source/Cmlx/mlx (mlx/backend/cpu/reduce.cpp) Early return filling init on empty CPU reductions (from Layr-Labs/mlx#54 on main). Empty CPU reductions only (bug fix). x.size() == 0 guard.

Non-interference

  • Default affine NAX execution (even on M5+): Zero numerical or routing change because MLX_NAX_KSPLIT defaults to 0 (opt-in).
  • M1–M4 Macs and CPU: Zero Metal kernel runtime paths change (metal::is_nax_available() == false).
  • MXFP4 / MXFP8 models (e.g., GPT-OSS 20B MXFP4): Completely untouched (bits == 4 && group_size == 16 excludes group_size == 32).
  • Sibling models unaffected on non-NAX hardware or outside the exact regimes above: Inkling 2 / Inkling 2 Mini, Gemma 4, GLM5-Next, Nemotron, Qwen, GPT-OSS, MiMo on M1–M4 Macs, MXFP4 builds, has_global_scale NVFP4 (kDoubleBuf = false), M > 16 affine NAX GEMM, and mlx-c (2ac897e3) are unaffected.

Rollback / fail-closed profile

Before and After

flowchart LR
  subgraph Before["Before (main @ 25c482f4 / mlx @ 72dffa2d)"]
    B1["NAX affine qmm_t_nax_staged_impl (8 <= M <= 16)"] --> B2["Only sgid 0 computes; 3 of 4 simdgroups idle"]
    B3["NAX NVFP4 fp_qmm_t_nax / gather / fp_qmv_nax"] --> B4["Single-buffered Ws; scalar load_unsafe; non-peeled QMV"]
  end

  subgraph After["After (7581e61e / mlx @ 4ef15bc7d4)"]
    A1["NAX affine qmm_t_nax_staged_impl (8 <= M <= 16)"] --> A2{"use_ksplit FC 203<br/>(allow_ksplit && MLX_NAX_KSPLIT=1 && !MLX_QMV_BATCH_INVARIANT)"}
    A2 -->|opt-in true| A3["qmm_t_nax_staged_ksplit_impl: 4 simdgroups split BK=64 + Ws FP32 reduction"]
    A2 -->|default false| B2
    A4["NAX NVFP4 GEMM / MoE gather / QMV"] --> A5["Aligned fast_nvfp4 (double-buffered Ws in fp_qmm_t_nax, bit-manip dequant in gather/QMV) + scalar fallback"]
  end
Loading

Before any rollout

  1. Run a side-by-side NVFP4 microbenchmark against base 72dffa2d (including attributing the M=32 latency cliff) and benchmark end-to-end real-checkpoint Muse Glimmer 30B prefill + decode throughput and KV-hash / greedy-token parity on M5 Max with Muse Glimmer 30B: port production-safe challenge inference optimizations (4a28444e) mlx-swift-lm#580.
  2. Wire allow_ksplit = (ref_rows <= 0) when [Metal] Prefill-row invariance scope: prompt chunks round like the reference rows (opt-in, per thread) mlx#71 / GPU.withPrefillRowInvariance(rows:_:): prompt chunks round like the reference rows (opt-in) #78 merge before enabling MLX_NAX_KSPLIT=1 in production.

Validation

1. Apple M4 Max (m4-max-1, macOS 27.2.1, Swift 6.4.2, Metal 410) on current head 7581e61e (mlx @ 4ef15bc7d4)

Check Command Result
Generated Metal self-tests python3 scripts/test-check-generated-metal-sources.py PASS — 11/11 OK (0.28 s)
Generated Metal consistency gate python3 scripts/check-generated-metal-sources.py (--all also checked) PASS — 4 copied files, 4 JIT resources (03b61445...); --all: 112 copied, 53 JIT PASS
fork.yaml gate uv run --with pyyaml python3 scripts/check_forkdiff.py PASS — 225/225 changed files described
Swift formatting xcrun swift-format lint --strict --configuration .swift-format Tests/MLXTests/NAXQuantizedKernelTests.swift PASS — 0 diagnostics
SwiftPM build + AOT mlx.metallib build/install swift build --build-tests + scripts/build-metallib.sh PASS — 0 errors
NAXQuantizedKernelTests (default, MLX_NAX_KSPLIT unset -> 0) env -u MLX_NAX_KSPLIT -u MLX_QMV_BATCH_INVARIANT swift test --skip-build --filter NAXQuantizedKernelTests PASS — 4/4 passed, 0 failures (9.44 s; bitwise M=13/16 vs M=17 verified)
NAXQuantizedKernelTests (MLX_NAX_KSPLIT=1) MLX_NAX_KSPLIT=1 swift test --skip-build --filter NAXQuantizedKernelTests PASS — 4/4 passed, 0 failures (9.39 s)
NAXQuantizedKernelTests (MLX_QMV_BATCH_INVARIANT=1) MLX_QMV_BATCH_INVARIANT=1 swift test --skip-build --filter NAXQuantizedKernelTests PASS — 4/4 passed, 0 failures (9.39 s)
NAXQuantizedKernelTests (MLX_NAX_KSPLIT=1 MLX_QMV_BATCH_INVARIANT=1) MLX_NAX_KSPLIT=1 MLX_QMV_BATCH_INVARIANT=1 swift test --skip-build --filter NAXQuantizedKernelTests PASS — 4/4 passed, 0 failures (9.38 s)
Exhaustive NVFP4 bit-manipulation parity Python check over all 4,096 (fp8_e4m3, fp4_e2m1) pairs (bf16 + fp16) PASS — 0/4,096 mismatches (0 ULP)

2. Apple M5 Max (applegpu_g17s, arch_gen=17, 40 GPU cores, metal::is_nax_available() == true, macOS 27.2.1, Swift 6.4.2, Metal 4) — verified on identical Metal kernel sources (979ad5da / ceea81fd; zero .h kernel diff to 4ef15bc7d4)

Check Command Result
SwiftPM build + AOT mlx.metallib install swift build --build-tests + scripts/build-metallib.sh install mlx.metallib (215 MiB, 1,176 NAX symbols) PASS — 18.29 s, 0 errors
NAXQuantizedKernelTests (MLX_NAX_KSPLIT=1) MLX_NAX_KSPLIT=1 swift test --skip-build --filter NAXQuantizedKernelTests PASS — 4/4 passed, 0 failures (13.85 s)
NAXQuantizedKernelTests (MLX_NAX_KSPLIT=0) MLX_NAX_KSPLIT=0 swift test --skip-build --filter NAXQuantizedKernelTests PASS — 4/4 passed, 0 failures (8.12 s; bitwise M=13/16 vs M=17 verified)
NAXQuantizedKernelTests (MLX_QMV_BATCH_INVARIANT=1) MLX_QMV_BATCH_INVARIANT=1 swift test --skip-build --filter NAXQuantizedKernelTests PASS — 4/4 passed, 0 failures (8.30 s)
Broader Swift --filter Quantized regression QuantizationTests (8/8), QuantizedBiasAccumulatorTests (3/3), QuantizedConstantCastTests (2/2), SortedGatherQuantizedTailTests (4/4), QuantizedLayerTests (MLX_ENABLE_TF32=0: 17/17), generated: Quantization PASS — 0 regressions (see upstream M5 TF32 note below)
Companion C++ Layr-Labs/mlx#73 All 12 quantized C++ test cases + 4 CTest NAX/batch-invariant entries PASS — 12/12 C++ (654/654 assertions), 4/4 CTest (1.76 s)
Combined #73 + #79 + #579 + #580 stack (mlx-swift-lm) Core MuseGlimmer/VLM/Video/Image/DFlash/CBv2/NVFP4 (164 Swift Testing + 11 XCTest) + ExactVerify/ExactWindow (17) + TF32-sensitive float32 (TF32=0, 26) + KSPLIT=0 (18) + BATCH_INVARIANT=1 (18) PASS — 254/254 test executions passed across all tested modes, 0 failures (--no-parallel)

Pre-existing upstream M5 TF32 note (not from #73/#79): On M5 (arch_gen >= 17), upstream MLX defaults MLX_ENABLE_TF32=1 (mlx/utils.h:269), routing float32 matrix products through NAX TF32 (~1e-3 rounding). An independent control build on clean base mlx-swift c633d22 / mlx 72dffa2d reproduced the identical 6/17 float32 QuantizedLayerTests tolerance failures with TF32 on and 17/17 pass with MLX_ENABLE_TF32=0. Production Muse Glimmer loads weights and runs all matmuls/SDPA in bf16, so TF32 does not apply in production.

M5 Max (applegpu_g17s) NAX Microbenchmarks (bf16, transpose=true, host wall-clock alternating KS1→KS0→KS1→KS0)

Methodology notes: Timings are host wall-clock (including graph construction and eval dispatch overhead). Each timed batch reuses one weight tensor across back-to-back ops, so reported GB/s values are cache-warm reused-weight throughput (not DRAM bandwidth). No affine K-split regression was observed outside ±5% run-to-run noise.

Workload Shape (N×K) M Result
Affine Q4 K-split A/B (bits=4, gs=64, KS=1 vs 0) 2048×6144 8 / 12 / 13 / 16 1.13x (1.12–1.14x, 33.51 vs 37.80 µs) / 1.06x / 1.06x / 1.05x
Affine Q4 K-split A/B (bits=4, gs=64, KS=1 vs 0) 2048×2048 8 / 12 / 16 1.10x (16.88 vs 18.54 µs) / 1.02x / 1.01x
Affine Q4 K-split A/B (bits=4, gs=64, KS=1 vs 0) 6144×6144 8 / 12 / 13 / 16 1.07x (48.33 vs 51.92 µs) / 1.04x / 1.04x / 1.03x
Affine Q4 K-split A/B (bits=4, gs=64, KS=1 vs 0) 4160×4160 8 / 12 / 13 / 16 0.98–1.01x (flat at M=8/12) / 1.01x / 1.01x
Affine Q8 K-split A/B (bits=8, gs=64, KS=1 vs 0) 2048×6144 8 / 12 / 16 1.04–1.09x across pairings (medians 1.08x / 1.05x / 1.07x)
Affine Q8 K-split A/B (bits=8, gs=64, KS=1 vs 0) 4160×4160 8 / 12 / 13 / 16 0.98–1.01x (flat at M=8/12) / 1.02x / 1.03x
Affine Q8 K-split A/B (bits=8, gs=64, KS=1 vs 0) 6144×6144 8 / 12 / 16 0.95–1.03x across pairings (medians 0.99x / 1.00x / 1.00x, ~6% run variance)
NVFP4 (bits=4, gs=16, absolute on #73, no 72dffa2d A/B yet) 6144×6144 1 / 8 / 32 / 64 / 512 731 GB/s cache-warm (29.1 µs) / 538 GB/s cache-warm (39.8 µs) / 9.5 TF (255.1 µs) / 45.7 TF (105.8 µs) / 53.8 TF (718.7 µs)
MXFP8 (bits=8, gs=32, absolute on #73, no 72dffa2d A/B yet) 6144×6144 1 / 8 / 32 / 64 / 512 816 GB/s cache-warm (47.7 µs) / 829 GB/s cache-warm (47.2 µs) / 9.3 TF (259.3 µs) / 42.6 TF (113.3 µs) / 50.5 TF (764.9 µs)

Note: NVFP4 and MXFP8 numbers above are absolute measurements on #73 (no side-by-side comparison against base 72dffa2d yet, since MLX_NAX_KSPLIT toggles affine only), and the observed M=32 latency cliff (255 µs at M=32 vs 106 µs at M=64 on 6144×6144) has not been attributed to #73 vs. base 72dffa2d yet.

Skipped / Ignored

  • Real-checkpoint Hugging Face weight downloads on M5 Max (auto-skipped cleanly in local verification).
  • pre-commit run --all-files locally (ran xcrun swift-format lint --strict, check-generated-metal-sources.py, and check_forkdiff.py directly; GitHub Actions CI Lint triggered on 7581e61e).
  • Re-running M5 Max tests after the 500c5d7e default-to-0 re-pin (zero Metal kernel .h diff between 979ad5da and 4ef15bc7d4; both MLX_NAX_KSPLIT=0 and MLX_NAX_KSPLIT=1 modes already passed on M5 Max).

Not proven

  • NVFP4 speedup vs. base 72dffa2d and M=32 cliff attribution: NVFP4/MXFP8 microbenchmarks above are absolute measurements on #73 with no side-by-side 72dffa2d baseline run yet.
  • End-to-end real-checkpoint Muse Glimmer 30B throughput & KV-hash soak on M5 Max (synthetic unit/integration across the full #73 + #79 + #579 + #580 stack — 254/254 test executions — and M5 NAX microbenchmarks are proven, while real-weight end-to-end tok/s and long-context KV-hash comparison remain).

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes (ran xcrun swift-format lint --strict, check-generated-metal-sources.py, and check_forkdiff.py directly)
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed) (fork.yaml)

AI usage

  • AI usage disclosure: Cursor cloud agent drafted the port, use_ksplit gate, hardening guards, M5-routed unit tests, merged mlx#73 (4ef15bc7d4) re-pin, and this PR description, and ran local M4 Max (m4-max-1) and M5 Max (applegpu_g17s) validation checks at the user's request.

Reviewer Notes

  • Inspect Source/Cmlx/mlx pin (4ef15bc7d4, merged Layr-Labs/mlx#73) and Source/Cmlx/include-framework/mlx-utils.h (env::nax_ksplit() opt-in default 0).
  • Inspect Tests/MLXTests/NAXQuantizedKernelTests.swift against QuantizedMatmul::eval_gpu (Source/Cmlx/mlx/mlx/backend/metal/quantized.cpp) and verify default (MLX_NAX_KSPLIT unset -> 0) bitwise parity assertion.
  • Inspect Source/Cmlx/mlx-generated/metal/quantized_nax.h (use_ksplit FC 203 gate) and Source/Cmlx/mlx-generated/metal/fp_quantized_nax.h (guard comments and alignment fallback).

Advance Source/Cmlx/mlx from 72dffa2d to ce27c709 (head of Layr-Labs/mlx#73)
and regenerate the matching mlx-generated Metal headers and JIT sources for
quantized_nax and fp_quantized_nax:

- quantized_nax: add qmm_t_nax_staged_ksplit_impl (splits BK=64 across all
  4 simdgroups when M <= SM in qmm_t_nax_staged_impl for 16-bit affine
  transposed GEMM and split-K on M5+ NAX).
- fp_quantized_nax: add double-buffered Ws NVFP4 NAX GEMM loader and
  M <= 8 peeled-prefetch QMV with LUT-backed dequantize_fp4_x4_scaled,
  hardened with threadgroup-uniform K/alignment/sizeof(T) guards.

Intentionally excluded from mlxfast-muse-glimmer-30b-engine (4a28444e):
- steel_gemm_fused_nax.{h,cpp} and steel_gemm_splitk_nax.{h,cpp} early-return
  hunks (divergent threadgroup barrier UB before mlx::steel::gemm_loop).
- gather_front.cpp (dead contest-only cbv2_array_backing helper; mlx-swift-lm#580
  uses public mlx_array_get_buffer_info / mlx_array_data_uint8).

Add Tests/MLXTests/NAXQuantizedKernelTests.swift covering small-M affine
quantizedMM, NVFP4 quantizedMM/QMV, unaligned/sliced fallback, and NVFP4
globalScale parity, and document the pin + test in fork.yaml.

Stacked on Layr-Labs/mlx#73; companion to Layr-Labs/mlx-swift-lm#580.
Address adversarial review findings (H1, M1, M2, L1, L4):

- Advance Source/Cmlx/mlx from ce27c709 to 979ad5da (head of Layr-Labs/mlx#73),
  regenerate mlx-generated/{quantized_nax,fp_quantized_nax}.{h,cpp}, and sync
  Source/Cmlx/include-framework/mlx-utils.h (adds env::nax_ksplit()).
- Gate qmm_t_nax_staged_ksplit_impl via Metal function constant 203
  (use_ksplit), controlled by MLX_NAX_KSPLIT (default 1) and automatically
  disabled when MLX_QMV_BATCH_INVARIANT=1 is set.
- Rewrite Tests/MLXTests/NAXQuantizedKernelTests.swift so every shape routes
  to its target M5 NAX kernel path:
  - Affine K-split split-K (N=K=2048, M=8..16, biases present) and direct
    staged qmm_nax (N=K=4160 = 65*64, M=13..16), plus bitwise M=13/16 vs
    M=17 parity when MLX_NAX_KSPLIT=0 or MLX_QMV_BATCH_INVARIANT=1.
  - Unbatched (M=512, N=1024) and batched (B=2, M=64, aligned N=128 and
    unaligned N=96) double-buffered NVFP4 fp_qmm_t_nax.
  - Peeled fp_qmv_nax (K=N=2048, 6 <= M <= 8 and M=9).
  - NVFP4 MoE gather (fp_gather_qmm_t_nax and fp_gather_qmm_rhs_nax).
  - Genuinely misaligned 4B weight / 1B scale offset fallback with bitwise
    sameBytes parity against the aligned fast paths.
  - NVFP4 globalScale GEMM route (M=512, N=1024, K=128).
- Update fork.yaml with the 979ad5da pin, accurate NVFP4 blast radius
  (including MoE gather paths), and updated test coverage description.
@GumbiiDigital
GumbiiDigital marked this pull request as ready for review December 4, 2026 14:40
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T14:42:17.622421Z ceea81f Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@jonathan308 jonathan308 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.

The vendored headers and JIT sources match mlx#73 exactly (only include paths differ), so this is a real pin. My concerns are sequencing:

  1. This pins 979ad5da, which exists only on the unmerged mlx#73 branch. mlx#73 hasn't run CI yet (no runner picked up its jobs), and a rebase there will orphan this SHA. Please land #73 first (with MLX_NAX_KSPLIT defaulting to 0, see my comment there), then re-pin to the merged commit on mlx main.
  2. Until that's done, this inherits #73's default-on K-split. That changes M5 output bits for affine bf16/fp16 at 8–16 rows.

Once it's re-pinned to merged mlx with K-split off by default, I'm happy to approve. CI here is green.

Advance Source/Cmlx/mlx from 979ad5da to 4ef15bc7d43527e2018596a23445aa4f0c698662
(merged Layr-Labs/mlx#73 on Layr-Labs/mlx main, including 500c5d7e's opt-in
MLX_NAX_KSPLIT=0 default, allow_ksplit plumbing for #71 coordination, and
the main empty-CPU-reduction fix from #54):

- Sync Source/Cmlx/include-framework/mlx-utils.h so env::nax_ksplit() matches
  mlx/utils.h (default 0, != 0).
- Update Tests/MLXTests/NAXQuantizedKernelTests.swift so unset MLX_NAX_KSPLIT
  asserts bitwise parity (default disabled), while explicit MLX_NAX_KSPLIT=1
  exercises K-split unless MLX_QMV_BATCH_INVARIANT=1 is set.
- Verified Source/Cmlx/mlx-generated/ matches 4ef15bc7 (zero Metal kernel
  header diff vs 979ad5da; check-generated-metal-sources.py passes).
- Update fork.yaml with the 4ef15bc7 pin and opt-in default.

This branch has not been deployed

No deployments
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