Skip to content

feat(mlx): add Metal kernels for MACaQ MXFP4 and NF12 - #4097

Merged
Qubitium merged 1 commit into
mainfrom
feat/mlx-macaq-nf12
Nov 15, 2026
Merged

Qubitium merged 1 commit into
mainfrom
feat/mlx-macaq-nf12

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Adds the two packed formats that had no MLX runtime kernel on Apple silicon. Both keep their checkpoint representation between forwards; neither installs a dense weight cache.

MACaQ MXFP4 (W4A4 / W4A16). MACaQ's storage is already OCP MXFP4 in exactly MLX's mxfp4 layout: E2M1 nibbles with the low nibble holding the even column, and one E8M0 scale byte per 32 values. The transfer is therefore a uint8 -> uint32 reinterpretation with no repack and no requantization. MlxMACaQLinear re-applies MACaQ's diagonal pre-scale and 32-wide block Hadamard rotation to the activations, then calls MLX's native MXFP4 matmul:

  • w4a16 -> quantized_matmul.
  • w4a4 -> fused qqmm through 16 rows; above that an explicit quantize/dequantize pair, which is 5x faster on wide token blocks and numerically identical for BF16 activations.

NF12 (lossless 12-bit BF16). MlxNF12Linear decodes the 12-byte tiled group stream on Metal: a single-row forward fuses the decode into the dot product, and a wider forward decodes once into BF16 and multiplies. Both paths reproduce the checkpoint's exact BF16 weights; a decoded layer is bit-identical to the Torch codec's decode().

What changed

  • gptqmodel/nn_modules/qlinear/mlx_macaq.py — MACaQ MXFP4 Metal module.
  • gptqmodel/nn_modules/qlinear/mlx_nf12.py — NF12 decode and fused GEMV Metal kernels.
  • gptqmodel/nn_modules/qlinear/mlx.py — MacaqMlxQuantLinear and NF12MlxQuantLinear registry holders with fail-closed source_compatible checks and exact-transfer payloads.
  • gptqmodel/utils/mlx.py — holder resolution, payload emission, module replacement, and conversion-format gates (NF12's 12-bit width is exempt from the 1-8 integer-bit check).
  • docs/mlx-runtime.md, README.md — flow description and measured results.

AUTO selection and explicit backend=BACKEND.MLX both resolve the new holders; no environment switches are introduced.

Measured on this host

M5 Ultra (36-core CPU, 256 GiB unified memory), one 4096x4096 BF16 layer, median of 20 timed repetitions after three warmups, against the portable Torch MPS path for the same packed checkpoint:

Format Rows Torch MPS MLX Metal Speedup
MACaQ W4A16 1 4.46 ms 0.26 ms 17x
MACaQ W4A16 512 3.67 ms 0.46 ms 8x
MACaQ W4A4 1 4.46 ms 0.24 ms 19x
MACaQ W4A4 512 5.45 ms 0.47 ms 12x
NF12 1 5.10 ms 0.26 ms 20x
NF12 512 5.17 ms 0.50 ms 10x

NF12 prefill is decode-bound: at this layer size a 32 MB elementwise pass already takes 0.27 ms on this GPU, so both the fused single-row kernel and the decode sit at the small-workload floor rather than at the ~650 GB/s the device reaches on 512 MB working sets.

Correctness

  • MACaQ MXFP4 words are verified byte-for-byte against MLX's mxfp4 dequantization of the same checkpoint.
  • W4A16 matches the Torch MACaQ kernel tightly (rtol=2e-2, atol=5e-3); W4A4 matches an oracle built from the same MLX activation quantization, and tracks the Torch reference within the 4-bit activation grid (MLX picks its own E8M0 exponent and tie handling, documented).
  • NF12 decode is bit-exact against nf12_codec.decode() for both clean and escaped-group weights; forwards match the Torch oracle for 1, 4, and 33 rows.
  • FP16/BF16 activation dtypes and arbitrary leading dimensions are preserved.
  • A full convert_gptq_to_mlx_weights pass on a tiny Llama replaces the layer with the new module and runs a model forward for both formats.
PYTHONPATH=. python -m pytest -q tests/test_mlx_macaq.py tests/test_mlx_nf12.py
21 passed in 1.22s

python -m ruff check gptqmodel/nn_modules/qlinear/mlx_macaq.py \
  gptqmodel/nn_modules/qlinear/mlx_nf12.py gptqmodel/nn_modules/qlinear/mlx.py \
  gptqmodel/utils/mlx.py tests/test_mlx_macaq.py tests/test_mlx_nf12.py
All checks passed!

git diff --check
(no output)

The wider MLX/selection suites (tests/test_mlx*.py, tests/kernels/test_selection.py, tests/test_macaq.py, tests/test_nf12_codec.py) have the same pass/fail set before and after this change on this host; the remaining failures are the pre-existing CUDA/Triton/bitsandbytes/network environment gaps.

Notes

  • MACaQ and NF12 keep SUPPORTS_FORMATS priority 0, matching the other MLX holders: they are registered for explicit BACKEND.MLX and the loader's AUTO probe, but are not part of the native kernel auto-order.
  • MLX 0.32.4 (the pinned minimum) is what the kernels were validated against.

Add the two packed formats that had no MLX runtime kernel on Apple silicon.

MACaQ: the checkpoint's OCP MXFP4 words are already MLX's `mxfp4` layout
(E2M1 nibbles with the low nibble holding the even column, one E8M0 byte
per 32 values), so the transfer is a `uint8 -> uint32` reinterpretation
with no repack and no dense expansion. `MlxMACaQLinear` re-applies the
MACaQ diagonal pre-scale and 32-wide block Hadamard rotation to the
activations and calls MLX's native MXFP4 matmul: `quantized_matmul` for
W4A16, and `qqmm` for W4A4 up to 16 rows. Above 16 rows the fused
activation quantization is replaced by an explicit quantize/dequantize
pair, which is 5x faster there (measured) and numerically identical for
BF16 activations.

NF12: `MlxNF12Linear` decodes the 12-byte tiled group stream on Metal.
A single-row forward fuses the decode into the dot product; a wider
forward decodes once into BF16 and multiplies. Both paths are exact for
the checkpoint's BF16 weights and bit-identical to the Torch codec's
`decode()`.

Also:
- Register `MacaqMlxQuantLinear` / `NF12MlxQuantLinear` holders so the
  loader's AUTO MLX selection and explicit `backend=MLX` both resolve.
- Teach `_packed_mlx_weights` the exact-transfer payloads, and let the
  conversion accept the MXFP4_MACAQ/NV12 formats (NF12's 12-bit width is
  exempt from the 1-8 integer-bit gate).
- Document the flow and the measured Metal results.

Measured on an M5 Ultra, one 4096x4096 BF16 layer, median of 20 runs
against the portable Torch MPS path for the same packed checkpoint:

  MACaQ W4A16  1 row    4.46 ms -> 0.26 ms  (17x)
  MACaQ W4A16  512 rows 3.67 ms -> 0.46 ms  ( 8x)
  MACaQ W4A4   1 row    4.46 ms -> 0.24 ms  (19x)
  MACaQ W4A4   512 rows 5.45 ms -> 0.47 ms  (12x)
  NF12         1 row    5.10 ms -> 0.26 ms  (20x)
  NF12         512 rows 5.17 ms -> 0.50 ms  (10x)

Tests: `tests/test_mlx_macaq.py` (8) and `tests/test_mlx_nf12.py` (13)
cover holder validation, byte-level MXFP4 equivalence, bit-exact NF12
decode (including escaped groups), W4A16/W4A4 parity oracles, dtype
preservation, and a full `convert_gptq_to_mlx_weights` conversion of a
Llama layer that then runs a model forward.
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