Skip to content

fix(foem): apply block-boundary beta correction on MLX path (#4548) - #4588

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4548-foem-mlx-block-beta
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4548-foem-mlx-block-beta

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4548

Root cause

FOEM.quantize auto-selects the native MLX path on Darwin/arm64 when
alpha=0, desc_act=False, static_groups=False, mse=0, and
blocksize == group_size. The Metal kernel (gptqmodel/quantization/mlx_foem.py)
applies FOEM's beta correction only inside a group (if (k + 1 < G)), and the
Python host loop carried the next group forward with

remaining = remaining[:, group_size:] - _accurate_matmul_mlx(error, inverse_hessian[start:end, end:])

without correcting that next group's first column. The Torch loop has applied
that correction at every block boundary since #4046
(W[:, i2] -= beta * (W[:, i2] - W_raw[:, i2])), so MLX silently kept the
pre-#4046 behaviour and checkpoints diverged on Apple Silicon.

Fix

gptqmodel/quantization/mlx_foem.py now applies the identical rule after the
error propagation and before the next group is quantized:

corrected = _apply_group_boundary_beta(remaining[:, 0], raw[:, end], beta)
remaining = mx.concatenate([mx.expand_dims(corrected, axis=1), remaining[:, 1:]], axis=1)

_apply_group_boundary_beta(column, raw_column, beta) returns
column - beta * (column - raw_column), matching the Torch boundary condition
exactly (beta=0 is the identity; beta=1 restores the raw column). MLX arrays
are immutable, so the corrected column is spliced back with mx.concatenate
instead of an in-place assignment. The Torch implementation is untouched.

tests/test_mlx_foem_quantization.py::_torch_foem_oracle is updated with the
same boundary correction so the macOS parity test compares against a corrected
oracle.

Tests

New Linux-runnable test: tests/test_mlx_foem_block_boundary_4548.py runs the
shipped MLX host loop against stand-in MLX primitives (numpy arrays + a recorder
Metal kernel returning a known constant error) and asserts that the second
group's first input column equals an independent float64 oracle. It does
not call the helper under test. Parametrized over beta = 0.2, 1.0, 0.0.

Fail-before (the gptqmodel/quantization/mlx_foem.py hunk stashed):

pytest -q tests/test_mlx_foem_block_boundary_4548.py
2 failed, 1 passed   # beta=0.2 and beta=1.0 fail; beta=0.0 is the identity

After the fix:

Command Result
pytest -q tests/test_mlx_foem_block_boundary_4548.py tests/test_mlx_foem_quantization.py (CUDA venv, CPU) 3 passed, 1 skipped
pytest -q tests/test_mlx_foem_block_boundary_4548.py (ROCm venv, CPU) 3 passed
ruff check gptqmodel docs/eora tests setup.py All checks passed

The skip is tests/test_mlx_foem_quantization.py, which is module-level
pytest.skip("Metal kernels require macOS") on non-Darwin hosts.

What was NOT executed

  • The MLX/Metal path itself: this host is Linux x86_64 with no MLX runtime, so
    no device numerics were measured. The new test pins the host-loop control
    flow and the correction arithmetic; it does not run a Metal kernel.
  • tests/test_mlx_foem_quantization.py::test_foem_processor_mlx_matches_torch_oracle
    and the updated _torch_foem_oracle were skipped here (macOS-only module).

Apple Silicon validation

On macOS arm64 with mlx installed:

python -m pytest -q tests/test_mlx_foem_quantization.py
# especially: tests/test_mlx_foem_quantization.py::test_foem_processor_mlx_matches_torch_oracle

That test quantizes a 128-column weight with group_size=64 (two blocks,
beta=0.2) through both the MLX path and the corrected Torch oracle; it should
now pass. A direct spot check: FOEM(alpha=0, group_size=64) on any layer wider
than 64 columns must produce the same packed weights as the Torch path
(_should_use_mlx_foem_quantization forced off).

Hardware

CPU only (Linux x86_64). No GPU was used; the CUDA/ROCm venvs were used for
import/CPU-test isolation only.

The Metal kernel applies FOEM's beta correction only inside a group
(k + 1 < G). The host loop carried the next group forward without
correcting its first column, so the auto-selected MLX path kept the
pre-#4046 behaviour and diverged from the Torch path on Apple Silicon.

Apply the same boundary rule after the error propagation, before the next
group is quantized, and update the macOS-only Torch oracle the same way.
The Torch implementation is unchanged.

Add a Linux-runnable test that runs the MLX host loop against stand-in
MLX primitives with a recorder kernel and compares the next group's first
column to an independent float64 oracle.
@Qubitium
Qubitium merged commit 68cc727 into main Nov 17, 2026
8 checks passed
@Qubitium
Qubitium deleted the fix/issue-4548-foem-mlx-block-beta branch November 17, 2026 02:00
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] FOEM block-boundary beta fix (#4046) applied only to the Torch path; auto-selected MLX path still skips it (Apple Silicon results diverge)

1 participant