Skip to content

fix(mlx): keep QQQ scaled reduction and bias wide until one cast (#4534) - #4539

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4534-mlx-qqq-wide-epilogue
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4534-mlx-qqq-wide-epilogue

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4534

Root cause

Both MLX QQQ epilogue bands narrowed the scaled product to FP16 before the
bias add and the visible dtype cast:

  • M=1 fused finalizer — half value = half(raw * input_scale * channel_scale), then half(value + half(bias)).
  • M>=2 Python epilogue — output.astype(mx.float16), then bias, then
    output.astype(original_dtype).

That destroys BF16 range (finite 65536 -> inf) and overflows a finite FP16
pre-bias sum before a cancelling bias (67200 - 2000 -> inf instead of
65216).

Fix

Keep the scaled reduction and bias in the wide product dtype and cast once:

  • Finalizer: float value = float(raw[index]) * input_scale[row] * channel_scale[column]; then value += float(bias[column]) when biased, then
    a single output[index] = OUTPUT_TYPE(value);.
  • Python epilogue: raw * input_scale * channel_scale, optional bias add, then
    one 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.py loads the shipped module against
stand-in MLX primitives and pins the epilogue boundary:

  • the generated finalizer source uses a float accumulator, a float bias add,
    and exactly one output cast;
  • the M>=2 path records a wide product, a wide bias add, and exactly one
    astype(original_dtype) with no intervening FP16 cast;
  • the finalizer launch receives a wide (float32) raw product and an
    original_dtype output.

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):

Case Narrow order Wide order / FP64 oracle
BF16, x=1024, signed code 1, channel 0.0625 inf 65536 (finite)
FP16, x=12.5, signed code 7, channel 0.75, bias -2000 inf 65216 visible (65200 exact)

Existing MLX tests updated for the new contract

  • test_qqq_qwen_down_wide_reduction_matches_previous_and_torch is now
    ..._matches_fp64_oracle: it no longer asserts equality with the historical
    FP16-intermediate previous path, and rounds the wide product directly to
    the activation dtype.
  • test_qqq_mlx_matches_torch_quantized_arithmetic now computes
    (quantized @ weight * scale * channel + bias).half() instead of rounding
    the pre-bias product to FP16 first.
  • Added Apple-only test_qqq_mlx_wide_epilogue_keeps_finite_range covering the
    issue fixture at M=1 and M=2.

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_wide_epilogue_keeps_finite_range -q

Expected: 4 passed (M=1 and M=2 x two fixtures). The fixture is
N=K=64, group_size=64, bits=4, uint32 words 0x99999999 / 0xffffffff,
affine scales 16, affine biases -128, channel scales .0625 / .75, and
optional bias -2000. Expected visible outputs are finite 65536 (BF16) and
65216 (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:

PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_mlx_qqq_wide_epilogue_4534.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_wide_epilogue_4534.py -q
-> 5 passed

Lint:

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

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

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 regression test were not executed here;
    the host test pins cast/accumulator order, not device numerics.
  • No performance, full-model, or checkpoint-through-forward claims.
  • Quantization math and packed layout are unchanged.

@Qubitium
Qubitium merged commit 52f7293 into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4534-mlx-qqq-wide-epilogue branch November 17, 2026 01:23
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 narrows the scaled reduction through FP16 before the bias add and the visible cast (residual of #4471)

1 participant