Repository navigation
fix(mlx): keep QQQ scaled reduction and bias wide until one cast (#4534) - #4539
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 #4534
Root cause
Both MLX QQQ epilogue bands narrowed the scaled product to FP16 before the
bias add and the visible dtype cast:
half value = half(raw * input_scale * channel_scale), thenhalf(value + half(bias)).output.astype(mx.float16), then bias, thenoutput.astype(original_dtype).That destroys BF16 range (finite
65536->inf) and overflows a finite FP16pre-bias sum before a cancelling bias (
67200 - 2000->infinstead of65216).Fix
Keep the scaled reduction and bias in the wide product dtype and cast once:
float value = float(raw[index]) * input_scale[row] * channel_scale[column];thenvalue += float(bias[column])when biased, thena single
output[index] = OUTPUT_TYPE(value);.raw * input_scale * channel_scale, optional bias add, thenone
output.astype(original_dtype).Quantization math is unchanged; only accumulation/cast order changed.
Host-side regression evidence
New
tests/test_mlx_qqq_wide_epilogue_4534.pyloads the shipped module againststand-in MLX primitives and pins the epilogue boundary:
floataccumulator, a float bias add,and exactly one output cast;
astype(original_dtype)with no intervening FP16 cast;float32) raw product and anoriginal_dtypeoutput.Before fix (source change stashed, tests kept): 5 failed.
After fix: 5 passed.
Portable CPU IEEE proof (not an MLX execution; same fixture arithmetic as the
issue):
inf65536(finite)inf65216visible (65200exact)Existing MLX tests updated for the new contract
test_qqq_qwen_down_wide_reduction_matches_previous_and_torchis now..._matches_fp64_oracle: it no longer asserts equality with the historicalFP16-intermediate
previouspath, and rounds the wide product directly tothe activation dtype.
test_qqq_mlx_matches_torch_quantized_arithmeticnow computes(quantized @ weight * scale * channel + bias).half()instead of roundingthe pre-bias product to FP16 first.
test_qqq_mlx_wide_epilogue_keeps_finite_rangecovering theissue fixture at M=1 and M=2.
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: 4 passed (M=1 and M=2 x two fixtures). The fixture is
N=K=64,group_size=64,bits=4, uint32 words0x99999999/0xffffffff,affine scales
16, affine biases-128, channel scales.0625/.75, andoptional bias
-2000. Expected visible outputs are finite65536(BF16) and65216(FP16). Also run the updated existing MLX QQQ tests.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 and test files passed. Final runwas after
git fetch origin main && git merge --no-edit origin/main(alreadyup to date at
4b6592381).Hardware
CPU-only host-side contract verification; AMD Eng Sample
100-000001458-01.No Apple/Metal device available.
Limitations
the host test pins cast/accumulator order, not device numerics.