Repository navigation
fix(foem): apply block-boundary beta correction on MLX path (#4548) - #4588
Merged
Merged
Conversation
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.
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 #4548
Root cause
FOEM.quantizeauto-selects the native MLX path on Darwin/arm64 whenalpha=0,desc_act=False,static_groups=False,mse=0, andblocksize == group_size. The Metal kernel (gptqmodel/quantization/mlx_foem.py)applies FOEM's
betacorrection only inside a group (if (k + 1 < G)), and thePython host loop carried the next group forward with
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 thepre-#4046 behaviour and checkpoints diverged on Apple Silicon.
Fix
gptqmodel/quantization/mlx_foem.pynow applies the identical rule after theerror propagation and before the next group is quantized:
_apply_group_boundary_beta(column, raw_column, beta)returnscolumn - beta * (column - raw_column), matching the Torch boundary conditionexactly (
beta=0is the identity;beta=1restores the raw column). MLX arraysare immutable, so the corrected column is spliced back with
mx.concatenateinstead of an in-place assignment. The Torch implementation is untouched.
tests/test_mlx_foem_quantization.py::_torch_foem_oracleis updated with thesame boundary correction so the macOS parity test compares against a corrected
oracle.
Tests
New Linux-runnable test:
tests/test_mlx_foem_block_boundary_4548.pyruns theshipped 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.pyhunk stashed):After the fix:
pytest -q tests/test_mlx_foem_block_boundary_4548.py tests/test_mlx_foem_quantization.py(CUDA venv, CPU)pytest -q tests/test_mlx_foem_block_boundary_4548.py(ROCm venv, CPU)ruff check gptqmodel docs/eora tests setup.pyThe skip is
tests/test_mlx_foem_quantization.py, which is module-levelpytest.skip("Metal kernels require macOS")on non-Darwin hosts.What was NOT executed
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_oracleand the updated
_torch_foem_oraclewere skipped here (macOS-only module).Apple Silicon validation
On macOS arm64 with
mlxinstalled:python -m pytest -q tests/test_mlx_foem_quantization.py # especially: tests/test_mlx_foem_quantization.py::test_foem_processor_mlx_matches_torch_oracleThat 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 shouldnow pass. A direct spot check:
FOEM(alpha=0, group_size=64)on any layer widerthan 64 columns must produce the same packed weights as the Torch path
(
_should_use_mlx_foem_quantizationforced off).Hardware
CPU only (Linux x86_64). No GPU was used; the CUDA/ROCm venvs were used for
import/CPU-test isolation only.