Repository navigation
feat(core): run affine 1-bit checkpoints with fused Metal kernels - #2035
Merged
Merged
Conversation
inureyes
added a commit
that referenced
this pull request
Sep 29, 2026
6 of 9 tasks
MLX implements affine quantized kernels for 2, 3, 4, 5, 6 and 8 bits only, so a checkpoint with `quantization.bits == 1` (prism-ml Bonsai, plain qwen3) loaded fine and then aborted on the first forward pass with "Unable to load kernel affine_dequantize_float16_t_gs_128_b_1". The new cpp/mlx_cxx_one_bit.cpp adds a Metal qmv kernel for fewer than 16 rows, a tiled simdgroup qmm kernel for larger batches, and a dequantize graph used as the reference, the non-Metal fallback, and the MLXCEL_ONE_BIT_KERNEL=0 kill switch. The quantized_matmul, quantized_linear_forward(_global_scale), dequantize and quantized_embedding bridge primitives route an affine 1-bit triple there, so every dense linear, embedding lookup and tied lm_head is covered. The UnifiedLinear accessors that feed fused C++ helpers (which call MLX's quantized_matmul directly) now answer None for 1-bit weights, so those callers take their graph fallback. The loader accepts 1 bit through a separate LOADABLE_AFFINE_BITS set, leaving SUPPORTED_AFFINE_BITS as the producer allowlist. 1-bit without .biases, 1-bit MoE expert stacks and 1-bit MLA per-head projections fail at load with the prefix named. CUDA and ROCm take the dequantize graph until a port can be validated on hardware. Validated with 17 unit tests against a host f64 oracle and on Bonsai-1.7B and Bonsai-8B, where kernel and fallback greedy outputs are identical over 64 and 128 tokens. Refs #1370
inureyes
force-pushed
the
feature/issue-1370-one-bit-affine
branch
from
September 29, 2026 09:54
ecab741 to
b7c7437
Compare
10 tasks done
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
Affine 1-bit checkpoints (
quantization.bits == 1, e.g.prism-ml/Bonsai-*-mlx-1bit) now load and generate. Before this change, main aborted on the first token ofBonsai-1.7B-mlx-1bitwithlibc++abi: terminating due to uncaught exception of type std::runtime_error: [metal::Device] Unable to load kernel affine_dequantize_float16_t_gs_128_b_1(exit 134).cpp/mlx_cxx_one_bit.cpp: Metal qmv kernel (M < 16, factoredscale * masked_sum + bias * total_sum), tiled simdgroup qmm kernel (M >= 16, f32 accumulation), and a dequantize graph used as the reference, the non-Metal fallback, and theMLXCEL_ONE_BIT_KERNEL=0kill switch.quantized_matmulin both layouts,quantized_linear_forward,_global_scale,dequantize,quantized_embedding), so every dense linear, embedding lookup and tied lm_head is covered, including callers outsidelayers.rs.UnifiedLinear::quantized_weight()/as_quantized_weight()answerNonefor 1-bit weights (same mechanism as runtime LoRA), so every fused helper that calls MLX'squantized_matmulinternally takes its graph fallback. The su-scaled-rope and compiledSwiGLUMLPpaths decline explicitly.LOADABLE_AFFINE_BITS;SUPPORTED_AFFINE_BITSstays the producer set (split-mtp, gemma4 overrides). 1-bit without.biases, 1-bit expert stacks (SwitchLinear) and 1-bit MLA (QuantizedMultiLinear) fail at load with the prefix named.supported-models.mdquantization row,MLXCEL_ONE_BIT_KERNELinenvironment-variables.md.Differences from the issue text
forward_fused_rope_appendneeds no decline: it consumesproject_concat, which goes throughUnifiedLinear::forward.one_bit_quantized_matmul,one_bit_dequantize) returnResultand are used by tests; the load path reaches the same code through the bridge primitives, which keeps call sites unchanged.inspectmemory estimate needed no change: it reads packed sizes from the safetensors header.Test plan
cargo test --release -p mlxcel-core --lib one_bit_tests: 17 pass. Kernels and fallback are each checked against a host f64 oracle (M 1/3/7/16/17/18/20/33/40, K 256 to 4096, N 64 to 1003 incl. unaligned, G 32/64/128, f16/bf16/f32), kill switch is bit-identical to the graph, rejections name the prefix.layers::tests(92),models::switch_layers::tests(26),cli_help_consistency,dead_doc_pointers,llama_compat_manifestpass.infer_bits_rejects_non_canonical_widthsupdated: it asserted that 1 bit is rejected, and now uses 7.cargo fmt --all -- --check,cargo clippy --release -p mlxcel-core --lib --tests -- -D warnings, same for-p mlxcel.Bonsai-1.7B-mlx-1bit, "The capital of France is",-n 64 -t 0: kernel andMLXCEL_ONE_BIT_KERNEL=0outputs identical over all 64 tokens, answer "Paris".Bonsai-8B-mlx-1bit, chess prompt,-n 128 -t 0: kernel and fallback identical over 128 tokens, fluent English.mlxcel-serveron the 1.7B, two concurrent greedy chat requests: "Paris is the capital of France." and a correct primary-colors answer.mlx-community/Qwen3-8B-4bit: deferred. The machine was shared with other GPU jobs during this run, so no timing is reported here.Closes #1370