Skip to content

fix(mlx): keep FP32 QQQ activations wide before dynamic quantization (#4537) - #4569

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4537-mlx-qqq-activation-range
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4537-mlx-qqq-activation-range

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4537

Root cause

_quantization_dtype() returned mx.float16 for every activation dtype except
BF16, and MlxQQQLinear.__call__ applied that cast before _dynamic_quant().
FP32 is a supported input (_finalize_kernel has an fp32 output variant), but
an FP32 value with |x| > 65504 became inf in FP16. The quantizer then
computed a row max of inf, a scale of inf, and inf / inf = NaN for the
whole row. Values below 65504 also lost 13 mantissa bits before the per-token
scale was computed.

This is the input operand cast, separate from the #4534 output-epilogue
widening.

Fix

_quantization_dtype() now preserves every supported activation dtype:

return dtype if dtype in (mx.float16, mx.bfloat16, mx.float32) else mx.float16

FP16 still uses FP16; BF16 and FP32 stay at their own width; an unknown dtype
keeps the historical FP16 fallback. The dynamic-quantization kernel already
reads elements as float and computes the row maximum and scale in float, so
the quantization math is unchanged.

Host-side regression evidence

tests/test_mlx_qqq_dtype_decision_4221.py now pins:

  • _quantization_dtype(mx.float32) is mx.float32 (and FP16/BF16 unchanged);
  • the shipped MlxQQQLinear.__call__ path passes the activation to
    _dynamic_quant at its original dtype for FP16, BF16, and FP32;
  • the generated dynamic-quant kernel still computes float local_max,
    metal::abs(float(x[...])), and the FP32 scale/division.

Before fix (source change stashed, tests kept): 2 failed, 3 passed.
After fix: 5 passed.

Added an Apple-only test_qqq_mlx_fp32_activation_range_stays_finite covering
FP32 x = 100000 at M=1 and M=2. With the fixture (signed code 1, affine scale
16, bias -128, channel scale 0.0625) the represented weight is 1 and the finite
output is 100000 * 64 = 6.4e6; the old FP16 cast produced inf/NaN.

Apple-device validation instructions (NOT executed here)

No MLX/Metal runtime exists on this Linux host (import mlx raises
ModuleNotFoundError), so device numerics were not executed. On Apple
silicon with MLX/Metal:

cd <checkout>
PYTHONPATH=$PWD python -m pytest \
  tests/test_mlx_qqq.py::test_qqq_mlx_fp32_activation_range_stays_finite -q

Expected: 2 passed (M=1 finalizer, M=2 Python epilogue), with finite FP32
output. Also run the rest of tests/test_mlx_qqq.py for the updated dtype and
epilogue coverage.

Tests run on this host

CUDA venv (Python 3.14t, torch 2.16.0.dev20261102+cu130), thread caps = 4:

PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_mlx_qqq_dtype_decision_4221.py tests/test_mlx_qqq.py -q
-> 5 passed, 1 skipped

The skip is the whole tests/test_mlx_qqq.py module (no MLX).

ROCm venv (torch 2.16.0.dev20261025+rocm10.1), HIP_VISIBLE_DEVICES=3, thread caps = 4:

PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest \
  tests/test_mlx_qqq_dtype_decision_4221.py -q
-> 5 passed

Lint:

ruff check gptqmodel docs/eora tests setup.py
-> All checks passed!

python -m py_compile on the changed module/test files passed. Final run was
after git fetch origin main && git merge --no-edit origin/main (already up to
date at cf56aa662).

Hardware

CPU-only host-side contract verification; AMD Eng Sample 100-000001458-01.
No Apple/Metal device available.

Limitations

  • The Metal kernel and the Apple-only FP32 range test were not executed here;
    the host test pins the dtype decision, quantizer operand, and wide range
    computation, not device numerics.
  • No performance, full-model, or checkpoint-through-forward claims.
  • Quantization math and packed layout are unchanged.

@Qubitium
Qubitium merged commit d0d44e0 into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4537-mlx-qqq-activation-range branch November 17, 2026 01:49
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.

[BUG] MLX QQQ still narrows FP32 activations to FP16 before dynamic quantization (|x|>65504 -> NaN row; residual of #4221)

1 participant