Repository navigation
feat(mlx): add Metal kernels for MACaQ MXFP4 and NF12 - #4097
Merged
Merged
Conversation
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.
This was referenced Nov 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
mxfp4layout: E2M1 nibbles with the low nibble holding the even column, and one E8M0 scale byte per 32 values. The transfer is therefore auint8 -> uint32reinterpretation with no repack and no requantization.MlxMACaQLinearre-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-> fusedqqmmthrough 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).
MlxNF12Lineardecodes 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'sdecode().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—MacaqMlxQuantLinearandNF12MlxQuantLinearregistry holders with fail-closedsource_compatiblechecks 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.MLXboth 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:
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
mxfp4dequantization of the same checkpoint.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_codec.decode()for both clean and escaped-group weights; forwards match the Torch oracle for 1, 4, and 33 rows.convert_gptq_to_mlx_weightspass on a tiny Llama replaces the layer with the new module and runs a model forward for both formats.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
SUPPORTS_FORMATSpriority0, matching the other MLX holders: they are registered for explicitBACKEND.MLXand the loader's AUTO probe, but are not part of the native kernel auto-order.