Skip to content

Speed up QQQ processor quantization with MLX - #3231

Merged
Qubitium merged 1 commit into
mainfrom
feature/mlx-qqq-processor-quant
Sep 27, 2026
Merged

Qubitium merged 1 commit into
mainfrom
feature/mlx-qqq-processor-quant

Conversation

@Qubitium

@Qubitium Qubitium commented Sep 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Connect the existing native MLX QQQ weight-quantization kernel to the standard QQQ processor on Apple silicon. Eligible 4-bit symmetric QQQ layers now run the Hessian-compensated quantization stage through MLX, while unsupported and small configurations retain the existing Torch path.

This is one focused QQQ quantization-stage PR. It does not change another quantization method or QQQ inference arithmetic.

What Changed

  • Added a guarded Torch↔MLX QQQ processor bridge for CPU and MPS tensors.
  • Routed eligible QQQ processor weights (4-bit symmetric, group size -1 or 128, block size 128, dynamic groups, no MSE) through the native MLX kernel above a measured crossover threshold.
  • Preserved processor loss reporting and recomputed post-INT8 channel scales from the original Torch weight, including dead-Hessian-column cases.
  • Selected the portable QQQ checkpoint holder on macOS so processor finalization does not instantiate the Linux/CUDA-only holder.
  • Kept MSE, static groups, asymmetric/nonstandard layouts, small tensors, non-Apple platforms, and unavailable-MLX environments on Torch.
  • Expanded independent Torch-oracle tests for loss accounting, boundary behavior, FP16/BF16 Qwen3.8-27B projection shapes, activation ordering, dead columns, and CPU/MPS bridges.
  • Benchmarked the complete processor stage, including Torch↔MLX transfers and output materialization.

Accuracy and Performance

Apple M5 Ultra, resident processor inputs, Torch↔MLX transfers included, 10 warmups, alternating execution order, 301 measured samples per path and row. Torch is the existing processor arithmetic; MLX is the changed path.

All 36 Qwen3.8-27B projection/dtype/group cases pass. Changed codes are independently adjudicated direct rounding ties or immediate one-step error-feedback descendants. Maximum drift away from changed codes is 1.1920929e-7; maximum scale drift is 1.49011612e-8; extra-scale drift is exactly 0. “Loss drift” is absolute drift in the aggregate diagnostic sum over the entire matrix, not per-value quantization drift.

Projection Dtype Group Shape Changed codes Direct ties Tie descendants Max direct margin Max weight drift Max drift away Max scale drift Max extra-scale drift Loss drift Torch ms MLX ms Speedup
full_attn.q_proj FP16 -1 12288x5120 0 0 0 0 0 0 0 0 0.01171875 1428.697 169.834 8.412x
full_attn.q_proj FP16 128 12288x5120 4 4 0 6.34998875e-7 0.0658968389 5.96046448e-8 7.4505806e-9 0 0.013671875 1428.880 174.440 8.191x
full_attn.q_proj BF16 -1 12288x5120 1 1 0 1.4310631e-8 0.122209825 0 0 0 0.0390625 1429.360 171.185 8.350x
full_attn.q_proj BF16 128 12288x5120 7 7 0 7.97998486e-7 0.070237577 1.1920929e-7 1.49011612e-8 0 0.0244140625 1435.652 178.641 8.037x
full_attn.k_proj FP16 -1 1024x5120 0 0 0 0 0 0 0 0 0.00048828125 106.901 44.351 2.410x
full_attn.k_proj FP16 128 1024x5120 0 0 0 0 5.96046448e-8 5.96046448e-8 7.4505806e-9 0 0.000854492188 114.311 45.247 2.526x
full_attn.k_proj BF16 -1 1024x5120 0 0 0 0 0 0 0 0 0.00317382812 104.380 44.497 2.346x
full_attn.k_proj BF16 128 1024x5120 0 0 0 0 5.96046448e-8 5.96046448e-8 7.4505806e-9 0 0.00305175781 118.736 45.470 2.611x
full_attn.v_proj FP16 -1 1024x5120 0 0 0 0 0 0 0 0 0.00048828125 104.242 44.204 2.358x
full_attn.v_proj FP16 128 1024x5120 0 0 0 0 5.96046448e-8 5.96046448e-8 7.4505806e-9 0 0.000854492188 118.702 45.180 2.627x
full_attn.v_proj BF16 -1 1024x5120 0 0 0 0 0 0 0 0 0.00317382812 104.378 43.931 2.376x
full_attn.v_proj BF16 128 1024x5120 0 0 0 0 5.96046448e-8 5.96046448e-8 7.4505806e-9 0 0.00305175781 118.713 45.318 2.620x
full_attn.o_proj FP16 -1 5120x6144 0 0 0 0 0 0 0 0 0.00390625 863.093 95.974 8.993x
full_attn.o_proj FP16 128 5120x6144 3 3 0 7.75042633e-7 0.0782439113 1.1920929e-7 1.49011612e-8 0 0.009765625 868.711 98.603 8.810x
full_attn.o_proj BF16 -1 5120x6144 2 1 1 5.81968576e-9 0.106026784 0 0 0 0.013671875 861.793 96.171 8.961x
full_attn.o_proj BF16 128 5120x6144 1 1 0 1.95673748e-7 0.0770833343 5.96046448e-8 7.4505806e-9 0 0.0009765625 867.931 99.574 8.716x
linear_attn.in_proj_qkv FP16 -1 10240x5120 1 1 0 2.66984289e-8 0.126743868 0 0 0 0.005859375 1201.509 150.725 7.972x
linear_attn.in_proj_qkv FP16 128 10240x5120 3 3 0 5.78990037e-7 0.0956243873 1.1920929e-7 1.49011612e-8 0 0.0419921875 1211.689 155.982 7.768x
linear_attn.in_proj_qkv BF16 -1 10240x5120 0 0 0 0 0 0 0 0 0.099609375 1195.900 149.028 8.025x
linear_attn.in_proj_qkv BF16 128 10240x5120 4 3 1 5.91957441e-7 0.0805642605 5.96046448e-8 7.4505806e-9 0 0.005859375 1203.974 157.772 7.631x
linear_attn.out_proj FP16 -1 5120x6144 0 0 0 0 0 0 0 0 0.00390625 866.598 96.002 9.027x
linear_attn.out_proj FP16 128 5120x6144 3 3 0 7.75042633e-7 0.0782439113 1.1920929e-7 1.49011612e-8 0 0.009765625 868.130 98.826 8.784x
linear_attn.out_proj BF16 -1 5120x6144 2 1 1 5.81968576e-9 0.106026784 0 0 0 0.013671875 866.774 96.237 9.007x
linear_attn.out_proj BF16 128 5120x6144 1 1 0 1.95673748e-7 0.0770833343 5.96046448e-8 7.4505806e-9 0 0.0009765625 868.293 99.511 8.726x
mlp.gate_proj FP16 -1 17408x5120 1 1 0 1.85018339e-7 0.121651769 0 0 0 0.0625 2017.159 228.898 8.812x
mlp.gate_proj FP16 128 17408x5120 4 3 1 1.97080566e-7 0.0717691183 1.1920929e-7 1.49011612e-8 0 0.013671875 2024.907 238.238 8.500x
mlp.gate_proj BF16 -1 17408x5120 0 0 0 0 0 0 0 0 0.03515625 2016.722 233.406 8.640x
mlp.gate_proj BF16 128 17408x5120 4 4 0 7.66592887e-7 0.0808973312 1.1920929e-7 1.49011612e-8 0 0.029296875 2021.347 239.374 8.444x
mlp.up_proj FP16 -1 17408x5120 1 1 0 1.85018339e-7 0.121651769 0 0 0 0.0625 1986.586 227.333 8.739x
mlp.up_proj FP16 128 17408x5120 4 3 1 1.97080566e-7 0.0717691183 1.1920929e-7 1.49011612e-8 0 0.013671875 1993.768 243.617 8.184x
mlp.up_proj BF16 -1 17408x5120 0 0 0 0 0 0 0 0 0.03515625 1985.893 227.721 8.721x
mlp.up_proj BF16 128 17408x5120 4 4 0 7.66592887e-7 0.0808973312 1.1920929e-7 1.49011612e-8 0 0.029296875 1995.272 238.402 8.369x
mlp.down_proj FP16 -1 5120x17408 1 1 0 1.85018339e-7 0.121651769 0 0 0 0.23828125 2212.834 749.517 2.952x
mlp.down_proj FP16 128 5120x17408 4 3 1 1.97080566e-7 0.0717691183 1.1920929e-7 1.49011612e-8 0 0.0078125 2238.058 784.230 2.854x
mlp.down_proj BF16 -1 5120x17408 0 0 0 0 0 0 0 0 0.0703125 2210.709 757.595 2.918x
mlp.down_proj BF16 128 5120x17408 4 4 0 7.66592887e-7 0.0808973312 1.1920929e-7 1.49011612e-8 0 0.072265625 2230.608 769.016 2.901x

Median speedup: 8.188x. Minimum: 2.346x. Maximum: 9.027x.

Tests

.venv-pypi/bin/python -m pytest -q tests/test_mlx_qqq_quantization.py tests/test_mlx_qqq_processor.py
# 66 passed

.venv-pypi/bin/python -m pytest -q tests/test_mlx_qqq.py
# 18 passed

uvx [email protected] check --config format/ruff.toml --ignore E712 \
  gptqmodel/looper/qqq_processor.py \
  gptqmodel/quantization/qqq.py \
  gptqmodel/quantization/mlx_qqq.py \
  tests/test_mlx_qqq_quantization.py \
  tests/test_mlx_qqq.py \
  tests/test_mlx_qqq_processor.py \
  tests/benchmark_mlx_qwen38_qqq_quant.py
# All checks passed

git diff --check
# clean

Review Requirements

I or an AI agent (such as Codex or Claude) have:

  • Reviewed this PR.
  • Unit tests have been executed and passed.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.
  • For this kernel-related change, measured accuracy drift against an independent Torch FP32 oracle.

@Qubitium
Qubitium force-pushed the feature/mlx-qqq-processor-quant branch from 476a08c to 116b753 Compare September 27, 2026 09:26
Comment thread gptqmodel/quantization/mlx_qqq.py Fixed
Comment thread tests/test_mlx_qqq_quantization.py Fixed
Comment thread tests/test_mlx_qqq_quantization.py Fixed
@Qubitium
Qubitium force-pushed the feature/mlx-qqq-processor-quant branch from b523509 to c6380d5 Compare September 27, 2026 10:28
@Qubitium
Qubitium force-pushed the feature/mlx-qqq-processor-quant branch from c6380d5 to b4b38ef Compare September 27, 2026 10:30
@Qubitium
Qubitium marked this pull request as ready for review September 27, 2026 10:31
@Qubitium
Qubitium merged commit 8819654 into main Sep 27, 2026
6 checks passed
@Qubitium
Qubitium deleted the feature/mlx-qqq-processor-quant branch September 27, 2026 10:35
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