Repository navigation
fix(mlx): keep FP32 QQQ activations wide before dynamic quantization (#4537) - #4569
Merged
Merged
Conversation
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.
Fixes #4537
Root cause
_quantization_dtype()returnedmx.float16for every activation dtype exceptBF16, and
MlxQQQLinear.__call__applied that cast before_dynamic_quant().FP32 is a supported input (
_finalize_kernelhas anfp32output variant), butan FP32 value with
|x| > 65504becameinfin FP16. The quantizer thencomputed a row max of
inf, a scale ofinf, andinf / inf = NaNfor thewhole 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: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
floatand computes the row maximum and scale infloat, sothe quantization math is unchanged.
Host-side regression evidence
tests/test_mlx_qqq_dtype_decision_4221.pynow pins:_quantization_dtype(mx.float32) is mx.float32(and FP16/BF16 unchanged);MlxQQQLinear.__call__path passes the activation to_dynamic_quantat its original dtype for FP16, BF16, and FP32;float local_max,metal::abs(float(x[...])), and the FP32scale/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_finitecoveringFP32
x = 100000at M=1 and M=2. With the fixture (signed code 1, affine scale16, bias -128, channel scale 0.0625) the represented weight is 1 and the finite
output is
100000 * 64 = 6.4e6; the old FP16 cast producedinf/NaN.Apple-device validation instructions (NOT executed here)
No MLX/Metal runtime exists on this Linux host (
import mlxraisesModuleNotFoundError), so device numerics were not executed. On Applesilicon with MLX/Metal:
Expected: 2 passed (M=1 finalizer, M=2 Python epilogue), with finite FP32
output. Also run the rest of
tests/test_mlx_qqq.pyfor the updated dtype andepilogue coverage.
Tests run on this host
CUDA venv (Python 3.14t, torch 2.16.0.dev20261102+cu130), thread caps = 4:
The skip is the whole
tests/test_mlx_qqq.pymodule (no MLX).ROCm venv (torch 2.16.0.dev20261025+rocm10.1),
HIP_VISIBLE_DEVICES=3, thread caps = 4:Lint:
python -m py_compileon the changed module/test files passed. Final run wasafter
git fetch origin main && git merge --no-edit origin/main(already up todate at
cf56aa662).Hardware
CPU-only host-side contract verification; AMD Eng Sample
100-000001458-01.No Apple/Metal device available.
Limitations
the host test pins the dtype decision, quantizer operand, and wide range
computation, not device numerics.