Repository navigation
fix(metal,kquant): keep split-K partials in float32 and relax gen-17-only test gates - #437
Conversation
…only test gates - Mirror upstream MLX PR #4641 in our K-quant / Tiled64 split-K Metal kernel (kquant_qmm_t_splitk + mlx_kquant_metal.cpp): accumulate split-K partials in float32 and sum/cast once via steel_gemm_splitk_accum instead of rounding each K-partition to bfloat16/float16 before reduction. Fixes the 47.7% element drift on M1/M2 (applegpu_g13s/g14s) in DFlash2 merged-vs-separate projections. - Version the split-K Metal symbols with `_f32` in kquant.metal + mlx_kquant_metal.cpp and gate them across all three per-dtype AIR units (float, float16_t, bfloat16_t) in packages/core/metallib-select.ts so stale prebuilt paged_attn.metallib files fail fast instead of silently corrupting outputs. - Replace strict `gpu_gen == 17` test gates in dflash2/tests.rs (now using is_nax_available() + >=33-core T=1 check + qmv_vector_limit) and quantized_linear.rs::kquant_tiled_tests (`gpu_gen >= 17`). - Add BF16 split-K float32-partial regression tests for q4k (row-major & Tiled64) and a4g64@t64 plus stale/mixed-dtype metallib packaging tests. Refs #201
Codex Review Summary: ✅ CompletedThis 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. |
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
…02/#4781/#4682 NAX, #4893 D=256 SDPA, #4961 small-M GEMM) (#442) ## Summary Rebases the `crates/mlx-sys/mlx` submodule from `76276123a` (upstream base `bebd6422f`, 17 patches) to **`84be893fe`** (`mlx-node/rebase-2026-11-29`, upstream base **`225e9c6e6`**, +184 upstream commits, **15 linear patches**). ``` ml-explore/mlx main 225e9c6e6 (2026-11-29) │ │ + 9 patches fn-d1 (dropped #4568 / #4728 / #4892; squashed CUDA ctor into patch 1) │ + 1 patch thread-scoped TF32 │ + 1 patch per-graph compile_with_mode │ + 3 patches CUDA cuBLASLt/WDDM/NVRTC, dynamic graph toggle, NVRTC include paths │ + 1 patch harden #4802 int8 NAX affine quantization (Splash guards + bf16-only gate) ▼ mlx-node/mlx 84be893fe ◄── crates/mlx-sys/mlx ``` ### Key Upstream & Fork Changes - **Metal JIT & kernel perf**: `#4953` compiled graph multi-consumer fusion, `#4802` default-on INT8 NAX affine prefill (`M >= 32`), `#4781` `affine_qmv_nax`, `#4682` TF32 `qmm_splitk_nax`, `#4893` D=256 `q >= 512` Metal SDPA, `#4961` small-M `32×32` Steel GEMM tiles, `#5006` float32 `hadamard_n`, `#5016` `Compiled::output_shapes` broadcast fix. - **Fork Patch 15 (`84be893fe`) — `#4802` INT8 NAX hardening**: adds Splash's bit-level NaN/Inf quiet-NaN propagation, `2^-100` subnormal floor, `precise::divide` + `[-127, 127]` `char` clamp, finite `sum_hi` guard in `quantized_nax.h`, and restricts `use_qmm_int_nax` to `bfloat16` (preventing `float16` group-sum overflow above `65504`). - **Bridge & packaging updates**: `#4734` `scaled_dot_product_attention` `window_size` & portable SDPA FC 304; `#4568` `CustomKernel::state()` index shift & `fast::CompileOptions`; `#4647` `get_default_max_ops_mb_per_buffer` & live `get_env_int` in `mlx_segmented_sdpa.cpp`; `#4893` `q >= 512` D=256 predicate; `#5003` Windows `CMAKE_INSTALL_BINDIR="."` (build key `4f58dd63`); C++20 bridge standard; `metallib-select.ts` new-pin markers (`sdpa_split_reduce`, `affine_qmv_nax`, `affine_qmm_t_int_nax`, `affine_qmv_nax_`). - **`kquant_qmv_t64` `KQ_AFFINE` fix**: FP32 partial sum in `load_vector` for `KQ_AFFINE` only (`a4g64@t64` `M=1` error `5.52e-3 -> 0.00e0`, zero K-quant kernel bits changed). - **Golden update**: re-captured only `promoted` (MLX F32 `QuantizedMatmul` graph) lines in all 14 `affine_mixed_{matmul,shapeless}.applegpu_*.txt` files; **0 `native` lines changed**. - **Prebuilt MLX manifest**: `crates/mlx-sys/mlx-prebuilt.json` pinned to published `84be893fe6d6` releases for macOS (`mlx-prebuilt-84be893fe6d6-3d951e0b`), Linux arm64 CUDA (`mlx-prebuilt-84be893fe6d6-23ca1e3b`), and Windows arm64 CUDA (`mlx-prebuilt-84be893fe6d6-4f58dd63`). --- ## ABBA Benchmarks (Apple M5 Max, 40-core GPU, 128 GiB, macOS 27.2.1) ### Microbenchmarks & Operator Benchmarks | Benchmark | Shape / Case | Old (`76276123a`) | New (`48ddeaaee` / `84be893fe`) | Speedup | | --- | --- | ---: | ---: | ---: | | `#4953` JIT `silu_bwd` (3+→1 kernel) | 2 / 32 / 128 MB | — | — | **1.37× / 1.67× / 2.19×** | | `#4953` JIT `swiglu_bwd` | 8 / 32 / 64 MB | — | — | **1.42× / 2.04× / 2.20×** | | `#4953` JIT `gelu_approx_bwd` / `multi_out_gate_fwd` | 32 MB | — | — | **1.44× / 1.47×** | | Metal QMM affine Q4 (`17408×5120`) | `M=8 / 16 / 512` | — | — | **1.32× / 1.37× / 1.34×** | | Metal QMM affine Q8 (`17408×5120`) | `M=8 / 16 / 64 / 512` | — | — | **1.10× / 1.30× / 1.34× / 1.47×** | | Metal QMM MXFP4 (`17408×5120`) | `M=8 / 16 / 512` | — | — | **1.75× / 1.47× / 1.16×** | | `#4893` `qwen35_paged_prefill_operator_bench` | `ctx=16384, q=512` | 22.71 ms (564.3 MB) | **12.68 ms (146.9 MB)** | **1.79× (3.84× less RAM)** | | `#4893` `qwen35_paged_prefill_operator_bench` | `ctx=65536, q=1024` | 62.33 ms | 63.31 ms | parity | ### End-to-End Models (ABBA) | Model & Mode | Workload | Old (`76276123a`) | New | Delta | | --- | --- | ---: | ---: | ---: | | **Qwen 3.8 27B UD-Q4_K_XL** (DFlash2) | `6k` (6,219 in, 512 out) decode | 46.69 tok/s | **48.87 tok/s** | **+4.7%** (`acceptTotal=2.954` exact) | | **Qwen 3.8 27B UD-Q4_K_XL** (DFlash2) | `6k` prefill | 744.2 tok/s | **761.3 tok/s** | **+2.3%** | | **Qwen 3.8 27B UD-Q4_K_XL** (AR BF16 KV) | `6k` decode / warm prefill | 23.48 / 746.3 tok/s | 23.55 / 752.3 tok/s | parity | | **Gemma-4-E2B-it-4bit** | 620 in prefill / decode | 5,530 / 148.0 tok/s | **6,559 / 151.2 tok/s** | **+18.6% prefill / +2.1% decode** | | **Qwen3-0.6B BF16 SFT** (`ckpt=true`, `b=2×512`) | step time | 1114.8 ms | **1089.1 ms** | **+2.4%** (exact loss `9.98818`) | --- ## Accuracy & Verification - **Teacher-forced NLL/KL** (2,680 tokens, `--kv-format bf16 --activations bf16`): - `qwen3.5-4b-nvfp4-mtp`: vs BF16 teacher KL `0.05119 -> 0.05081`; vs self-OLD KL `3.35e-3`, top-1 `97.46%` - `qwen3.5-9B-unsloth`: vs self-OLD KL `1.47e-3`, top-1 `97.76%` - **Golden route-back proof** (`MLX_QMM_SPLITK_MIN_M=34`): **10,469 promoted cases / 80,628,416 elements across all 7 GPU routes: 0 mismatches**; host `f64` across 1,861 moved cases: `max|e|` `29 -> 29`, `rel L2` `1.662457e-03 -> 1.693176e-03`. - **Tests run on `84be893fe` after rebasing onto `origin/main` (`97fa4804c`)**: - MLX C++ `./tests`: **336/336** (6,043 assertions) - `mlx-sys` `build.rs` `prebuilt_tests`: **11/11** - `yarn build:native` + `vp test __test__/core/`: **127/127** - `kquant_suite`: **87/87** (incl. `#437` split-K float32 regressions) - `affine_mixed_golden_gate`: **16/16** across native + all 7 simulated GPU routes - `segmented_sdpa_golden_gate` (3/3), `qwen35_d256_sdpa` (2/2), `portable_d256_sdpa` (2/2), `bridge_metallib_names`, `kquant_metallib_names`, `mlx-paged-attn`, `mlx-core --lib` (`qwen4_exp`, `paged_int8_query_tests`, `gguf_planes`, `target_schedule_tests`, `kquant_tiled_tests`, `merged_draft_projections_match_separate_bits`) - `cargo clippy --locked -p mlx-core -p mlx-sys -p mlx-paged-attn --all-targets -- -D warnings`, `cargo fmt --all -- --check`, `vp fmt --check`, `vp lint --type-aware`, `yarn typecheck` — all pass.
Summary
Fixes the concrete leftover bugs found while auditing #201 (no CI changes, since CI already runs on M1; no
mlxsubmodule bump needed):kquant_qmm_t_splitkpartials infloat32(crates/mlx-sys/src/metal/kquant/kquant.h,crates/mlx-sys/src/metal/mlx_kquant_metal.cpp):494c8a6c9, already in our pinnedmlxsubmodule) inside our K-quant / Tiled64 bridge.qmm_splitkallocated its partial-sum buffer inx.dtype()(bfloat16/float16) and summed viastrided_reduce_general_dispatch, truncating every K-partition to 7-bit BF16 before summing. On M1/M2 (applegpu_g13s/g14s, whereqmv_vector_limit = 6), DFlash2 merged-vs-separate projections atT = 6..8hitqmm_t_splitk_t64with different split counts (4vs2) and flipped 47.7% of elements (11726/24576).kquant_qmm_t_splitkwritesfloat32partials and sums/casts once via MLX'ssteel_gemm_splitk_accumkernel (0.00%drift ona4g64@t64split_k 32 vs 2;0.01%onq4k)._f32) + packaging integrity gate (crates/mlx-sys/src/metal/kquant/kquant.metal,crates/mlx-sys/src/metal/mlx_kquant_metal.cpp,packages/core/metallib-select.ts,__test__/core/metallib-select.test.ts):_f32suffix to both row-major and Tiled64 split-K Metal symbol names so an older prebuiltpaged_attn.metallibthrowsPrebuiltKernelMissinginstead of silently writing 16-bit partials into afloat32buffer._f32split-K markers across all three per-dtypekquantAIR units (float,float16_t,bfloat16_t) and all three split-K forms (row-major, Tiled64, affine Tiled64) with exact NUL-delimited matching inassertPagedMetallibIntegrity.gpu_gen == 17test gates (crates/mlx-core/src/models/qwen3_5/dflash2/tests.rs,crates/mlx-core/src/models/quantized_linear.rs):merged_draft_projections_match_separate_bits: switchesgpu_gen == 17tomlx_metal_is_nax_available(), keepsT > merged_rows()(T = 87) exact on every GPU, checksqmv_vector_limit, and gatesT = 1exactness onmlx_gpu_core_count() >= 33(matchingtiled_qmv_rows).kquant_tiled_tests::same_kernel_for_m: switchesgpu_gen() == 17togpu_gen() >= 17so gen-18+ isn't dropped from the exact-bit arm.kquant_tiled::qmm_splitk_bf16_partials_stay_float32(q4krow-major & Tiled64,split_k = 32vs2).kquant_affine_tiled::affine_qmm_splitk_bf16_partials_stay_float32(a4g64@t64,split_k = 32vs2)._f32split-K fixtures in__test__/core/metallib-select.test.ts.Verification (Apple M5 Max,
MLX_METALLIB_STRICT=1)merged_draft_projections_match_separate_bits: PASS acrossnative,applegpu_g00g,g13s,g14s,g14d,g15s,g16s,g17p,g18s.kquant_tiled_tests: PASS acrossnative,g00g,g13s,g16s,g17p,g18s.kquant_suite: 87/87 PASS (native); new split-K regressions PASS acrossnative,g00g,g13s,g16s,g18s.kquant_metallib_names: 5/5 PASS (3,084 functions + JIT preamble).paged_attn.metallibrejected both at packaging (assertPagedMetallibIntegrity) and at runtime (Cannot load K-quant kernel ..._f32).cargo fmt --all -- --check,cargo clippy --locked -p mlx-core -p mlx-sys --all-targets -- -D warnings,vp fmt --check,vp lint, and Vitest (metallib-select.test.ts,payload.test.ts— 103 passed) all green.Notes / Out-of-scope items from review
dflash2_state/disk.rs): intentionally left unchanged (consistent with PR perf(qwen3_5,inkling2,metal): Metal 4 INT8 NAX prefill, multi-lane DFlash2 batching, and Inkling2 engine hardening #412's INT8 NAX prefill) so upgrades don't invalidate users' existing SSD prefix caches.kquant_golden_gatedigests: pre-existing stale since PR chore(mlx): bump fork to upstream bebd6422f (2026-10-14), 4 patches #219 (2026-10-14; gather/dequantize and non-split-K cases already differ onmain), left untouched to avoid mixing unrelated digest churn into this PR.Refs #201