Skip to content

fix(metal,kquant): keep split-K partials in float32 and relax gen-17-only test gates - #437

Merged
Brooooooklyn merged 1 commit into
mainfrom
fix/issue-201-splitk-and-arch-gates
Nov 29, 2026
Merged

Brooooooklyn merged 1 commit into
mainfrom
fix/issue-201-splitk-and-arch-gates

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Contributor

Summary

Fixes the concrete leftover bugs found while auditing #201 (no CI changes, since CI already runs on M1; no mlx submodule bump needed):

  1. Keep kquant_qmm_t_splitk partials in float32 (crates/mlx-sys/src/metal/kquant/kquant.h, crates/mlx-sys/src/metal/mlx_kquant_metal.cpp):
    • Mirrors upstream MLX PR #4641 (494c8a6c9, already in our pinned mlx submodule) inside our K-quant / Tiled64 bridge.
    • Previously, qmm_splitk allocated its partial-sum buffer in x.dtype() (bfloat16/float16) and summed via strided_reduce_general_dispatch, truncating every K-partition to 7-bit BF16 before summing. On M1/M2 (applegpu_g13s/g14s, where qmv_vector_limit = 6), DFlash2 merged-vs-separate projections at T = 6..8 hit qmm_t_splitk_t64 with different split counts (4 vs 2) and flipped 47.7% of elements (11726/24576).
    • Now kquant_qmm_t_splitk writes float32 partials and sums/casts once via MLX's steel_gemm_splitk_accum kernel (0.00% drift on a4g64@t64 split_k 32 vs 2; 0.01% on q4k).
  2. Version split-K Metal symbols (_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):
    • Adds an _f32 suffix to both row-major and Tiled64 split-K Metal symbol names so an older prebuilt paged_attn.metallib throws PrebuiltKernelMissing instead of silently writing 16-bit partials into a float32 buffer.
    • Adds _f32 split-K markers across all three per-dtype kquant AIR units (float, float16_t, bfloat16_t) and all three split-K forms (row-major, Tiled64, affine Tiled64) with exact NUL-delimited matching in assertPagedMetallibIntegrity.
  3. Relax strict gpu_gen == 17 test 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: switches gpu_gen == 17 to mlx_metal_is_nax_available(), keeps T > merged_rows() (T = 87) exact on every GPU, checks qmv_vector_limit, and gates T = 1 exactness on mlx_gpu_core_count() >= 33 (matching tiled_qmv_rows).
    • kquant_tiled_tests::same_kernel_for_m: switches gpu_gen() == 17 to gpu_gen() >= 17 so gen-18+ isn't dropped from the exact-bit arm.
  4. Regression tests:
    • kquant_tiled::qmm_splitk_bf16_partials_stay_float32 (q4k row-major & Tiled64, split_k = 32 vs 2).
    • kquant_affine_tiled::affine_qmm_splitk_bf16_partials_stay_float32 (a4g64@t64, split_k = 32 vs 2).
    • Stale / mixed-dtype pre-_f32 split-K fixtures in __test__/core/metallib-select.test.ts.

Verification (Apple M5 Max, MLX_METALLIB_STRICT=1)

  • merged_draft_projections_match_separate_bits: PASS across native, applegpu_g00g, g13s, g14s, g14d, g15s, g16s, g17p, g18s.
  • kquant_tiled_tests: PASS across native, g00g, g13s, g16s, g17p, g18s.
  • kquant_suite: 87/87 PASS (native); new split-K regressions PASS across native, g00g, g13s, g16s, g18s.
  • kquant_metallib_names: 5/5 PASS (3,084 functions + JIT preamble).
  • Stale paged_attn.metallib rejected 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

Refs #201

…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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 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-11-29T05:35:59.414107Z ffb8a0e PR opened
ℹ️ 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.

@coderabbitai

coderabbitai Bot commented Nov 29, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 305e35f5-e32a-4573-9366-103dbb149088

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@Brooooooklyn
Brooooooklyn merged commit c2d820d into main Nov 29, 2026
21 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/issue-201-splitk-and-arch-gates branch November 29, 2026 06:20
Brooooooklyn added a commit that referenced this pull request Nov 29, 2026
…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.
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.

1 participant