Skip to content

[BUG] MLX QQQ narrows the scaled reduction through FP16 before the bias add and the visible cast (residual of #4471) #4534

Closed

Description

@Qubitium

Residual of #4471. PR #4533 kept the Torch portable reductions and bias epilogues wide (ParoQuant CPU ATen, bias-free FP8 fallback, Mamba selective_state_update), but it explicitly did not touch the MLX QQQ case: no MLX/Metal runtime exists on the Linux validation host, and the existing MLX QQQ tests encode the historical FP16 intermediate as their expected policy. This issue tracks that remaining case so it is not lost.

Contract

Keep the scaled reduction and the bias add wide until exactly one visible output cast. Do not destroy BF16 range, and do not overflow a finite final FP16 linear + bias result.

Two manifestations, one root (epilogue ownership)

Both come from narrowing the scaled product to FP16 before the bias add and the visible-dtype cast:

  • (a) BF16 range loss — finite BF16 output 65536 becomes inf when passed through FP16.
  • (b) FP16 bias cancellation — pre-bias 67200 plus bias -2000 should yield finite 65216, but it overflows before the cancellation.

Repro (portable CPU IEEE proof, not an MLX forward)

Supported whole-row QQQ INT4 holder, K = N = 64. M=1 uses the fused finalizer; M>=2 uses the Python epilogue — both must be covered.

import torch
torch.set_num_threads(1)
for dtype, x, signed_code, channel, bias in [
    (torch.bfloat16, 1024., 1, .0625, 0.),
    (torch.float16,   12.5, 7, .75,  -2000.),
]:
    a = torch.full((2, 64), x, dtype=dtype)
    s = a.float().abs().amax(-1, keepdim=True) / 127
    q = (a.float() / s).round().clamp(-128, 127)
    raw = q @ torch.full((64, 64), signed_code * 16., dtype=torch.float32)
    scaled = raw * s * channel
    exact = a.double() @ torch.full((64, 64), signed_code * 16 * channel, dtype=torch.float64) + bias
    expected = exact.to(dtype)
    narrowed = (scaled.half().float() + bias).to(dtype)
    print(float(scaled[0, 0]), float(exact[0, 0]), float(expected[0, 0]), bool(torch.isinf(narrowed).all()))

Observed output: 65536.0 65536.0 65536.0 True and 67200.0 65200.0 65216.0 True.

Observed vs expected

  • BF16: oracle expects finite 65536; current order yields inf.
  • FP16: oracle expects 65200 exact → 65216 visible; current order yields inf.
  • The shipped source encodes that cast order in both row bands, so the existing tests passing does not refute the defect — they assert the historical FP16-intermediate policy.

Validation constraints

  • No MLX/Metal runtime on the Linux CI host (import mlx → ModuleNotFoundError), so the fix cannot be numerically qualified here.
  • Acceptable evidence: (1) a host-side test that pins the cast order / accumulator dtype at the epilogue boundary (as was done for the MLX NF12 row-dispatch fix in fix(mlx): cover NF12 short-row output tails in dot-product dispatch (#4478) #4523), plus (2) explicit, reproducible Apple-device instructions for M=1 and M=2 using the fixture below. Do not claim MLX numerics that were not executed.
  • Fixture: N=K=64, group_size=64 (whole row), bits=4, uint32 words 0x99999999 / 0xffffffff, affine scales 16, affine biases -128, channel scales .0625 / .75, optional output bias -2000.

Source / history

The premature-FP16 cast order is in the MLX QQQ epilogue for both the fused-finalizer (M=1) and Python-epilogue (M>=2) row bands. Introduced with the MLX QQQ backend; #4471 documented it, #4533 left it unfixed by design.

Software / device info

Linux x86_64 validation host only; no Apple/Metal device available. Torch portable IEEE proof executed with an independent FP64 oracle.

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed. This issue is now owned by a dedicated worker; a fix PR referencing it will follow.

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed by an AI agent. Will keep the scaled reduction and bias add in a wide accumulator with exactly one visible cast, pin the cast order with a host-side test (no MLX runtime here), and document reproducible Apple-device validation steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions