Skip to content

Integrate upstream Metal fixes (affine quantization of all-negative groups, FFT over many axes) - #77

Merged
jjang-ai merged 2 commits into
osaurus-ai:mainfrom
rcfa:apple/r4-metal
Nov 28, 2026
Merged

jjang-ai merged 2 commits into
osaurus-ai:mainfrom
rcfa:apple/r4-metal

Conversation

@rcfa

@rcfa rcfa commented Nov 26, 2026 •

Copy link
Copy Markdown

Branch apple/r4-metal, 2 commits on origin/main 23f364e (tip c257e3b).

Two Metal bug fixes from ml-explore/mlx main: ml-explore#4775, one of the fixes in the fork's own quantization kernels that the
first intake rounds left for later, and ml-explore#4968, which landed on 2026-11-26. The code is Apple's and its contributors':
each commit is a git cherry-pick -x of the upstream commit, with the upstream author kept and the
(cherry picked from commit …) line recording provenance. ml-explore#4775 is ported (below); ml-explore#4968 applies cleanly.

Upstream Commit Here What it fixes
ml-explore#4775 32a183e d5067fe Metal and CUDA affine quantization started the group maximum at 0, not at -max, so a group of only negative values was quantized over [min, 0] and lost precision. Adds test_affine_quantize_signed_groups (bits 1 to 8, group sizes 32 to 128; 1-bit is skipped on CUDA).
ml-explore#4968 8feb9fe c257e3b On Metal, fftn, ifftn, rfftn and irfftn over four or more axes reused their temporary buffers in place also for sizes whose kernels replace the output buffer (size 1, and the multi-upload Bluestein path), and gave wrong results. Adds test_fftn_many_axes.

The port

  • Fix Metal and CUDA affine quantization for all-negative groups ml-explore/mlx#4775, mlx/backend/metal/kernels/quantized.h: this fork's kernel already started the maximum at -max, but for
    1-bit only (bits == 1 ? -Limits<T>::max : 0, from the fork's own one-bit fix). Apple's line, which does it for
    every bit width, replaces that line and its 1-bit comment. The CUDA kernel and the test are as in Apple's commit.
    The commit message says so.

Not included

  • ml-explore/mlx PR 4594 (cooperative tensors on macOS 27): this fork builds on macOS 27 with its own change to
    gated_delta_update_nax.h; the rest of PR 4594 is in the gated-delta VJP code of PR 4565, which comes with that.
  • PR 4796 (route qqmm without global scales as quantized_matmul) builds on the NAX and qmv_mma routing (PR 4781,
    PR 4787), which this fork holds back.
  • PR 4675 fixes a sign warning in code that this fork does not have (from PR 4516). PR 4677 (split-K test on M5 and
    M6) is already in the fork.

Validation

Compared with main (23f364e), built and run the same way:

  • macOS (M4 Max, Release, Metal on), Python 3.14, the test modules this branch touches (test_fft, test_quantized), on the GPU and with
    DEVICE=cpu: GPU 73 pass, 3 skip, 2 fail; CPU 65 pass, 10 skip, 1 fail, 2 errors. main: GPU 71 pass,
    3 skip, the same 2 failures; CPU 63 pass, 10 skip, the same failure and errors. The branch adds
    test_affine_quantize_signed_groups and test_fftn_many_axes. With main's build, both fail on the GPU; on the
    CPU, where quantization and the FFT were right, both pass. main's own failures: on the GPU
    test_qmm_mid_m and test_qmm_splitk_precision; on the CPU test_fft_too_large, and
    test_gather_qmm_global_scale and test_qqmm_global_scale_matrix_kernels (errors: the CPU qqmm global scales,
    which osaurus-ai/mlx PR 57 brings).
  • macOS, CI's distributed step: 8 of 8 ranks pass (14 tests), as main.
  • macOS C++ suite (tests/tests): 390 of 390 cases on the CPU and on Metal, as main.
  • Linux arm64 (container vmlx-arm64), the CI-equivalent Python run (osaurus-ai's Linux job, built and run the same
    way: GCC, Debug, Highway, DEVICE=cpu, python/tests module by module, torch 2.15 for the CPU), then CI's
    distributed step (mlx.launch -n 8 python/tests/ring_test_distributed.py): 883 pass, 92 skip, 2 fail; ring 8 of 8 ranks pass (14 tests). main: 881 pass, 92 skip,
    2 fail, ring 8 of 8 ranks pass (14 tests). The two failures are main's own: test_compile_sigmoid_matches_eager
    (bfloat16: the compiled CPU sigmoid is less accurate than the eager Highway kernel) and bfloat16
    test_torch_conv_2D, where torch 2.15's own aarch64 bfloat16 conv2d is wrong.
  • Linux arm64 C++ suite (clang Release, Highway): 412 of 412 cases, as main.
  • CUDA (as for PR 75, the CUDA fixes of this batch: NVIDIA's CUDA 12.9 archives, Ubuntu 22.04 userspace, mlx's flags, sm_80):
    quantized/affine_quantize.cu compiles with no errors, on main and on this branch. Not run.
  • osaurus-ai/mlx's GitHub Actions have not run since 2026-10-08, so this PR shows no checks; the runs above follow
    its Linux and macOS jobs.

Validation of the batch in shared

This batch has five PRs: apple/r4-python, apple/r4-cuda, apple/r4-distributed, apple/r4-metal and
fix/erf-relative-accuracy. Merged on main 23f364e with the open osaurus-ai/mlx PRs 57 to 73 and their two overlap
resolutions, they give mlx 3898182. The five merge with no conflict, with each other and with PRs 57 to 73.
vmlx-swift c0f6255f0 builds on mlx 3898182 (it is osaurus-ai/vmlx-swift main 06e3aa2c7 with our open vmlx-swift
patches). Built and run, compared with main:

  • macOS (M4 Max, Release, Metal on): the C++ suite passes 411 of 411 cases on the CPU and on Metal (main 390 of
    390). vmlx-swift builds. Its suite (swift test --no-parallel, 1046 tests) fails no test that passes on vmlx-swift
    main 9bda77d3.
  • Linux arm64 and amd64 (containers, clang Release, Highway): the C++ suite passes 432 of 432 cases on both (main
    412 of 412). vmlx-swift's Linux CI steps pass: VMLXLinuxTests 37 of 37, and the CPU kernel tests 61 of 61 at both
    thread settings.
  • Linux, the CI-equivalent Python run (osaurus-ai's Linux job, built and run the same way: GCC, Debug, Highway,
    DEVICE=cpu, python/tests module by module, torch 2.15 for the CPU), then CI's distributed step
    (mlx.launch -n 8 python/tests/ring_test_distributed.py):
    • arm64: main 881 pass, 92 skip, 2 fail; the batch 915 pass, 96 skip, 2 fail.
    • amd64: main 882 pass, 92 skip, 1 fail; the batch 916 pass, 96 skip, 1 fail.
    • The distributed step: all 8 ranks pass 18 tests on both arches (main on arm64: 14).
    • No test that passes on main fails on the batch. The failures are main's own:
      test_compile_sigmoid_matches_eager (bfloat16: the compiled CPU sigmoid is less accurate than the eager Highway
      kernel) and, on arm64 only, bfloat16 test_torch_conv_2D, where torch 2.15's own aarch64 bfloat16 conv2d is
      wrong.
  • Tests of main that fail at random:
    • test_double.TestDouble.test_unary_ops: float32 erf against float64 at rtol 1e-5, failing in 16 of 200
      random draws on Linux arm64. fix/erf-relative-accuracy fixes it.
    • test_ops.TestOps.test_divmod: about 1% of runs. PR 72 fixes it.
    • test_async_eval_error_in_synchronize on amd64: about 1 run in 5 on main. It passed in this run.
  • osaurus-ai/mlx's GitHub Actions have not run since 2026-10-08, so this PR shows no checks. The runs above follow
    its Linux and macOS jobs.

🤖 Generated with Claude Code

lgyStoic and others added 2 commits November 26, 2026 20:55
…plore#4775)

Co-authored-by: Garry Ling <4526646+lgyStoic@users.noreply.github.com>

Ported: this fork's Metal affine_quantize started w_max at -max for 1-bit
only (bits == 1 ? -Limits<T>::max : 0), from the fork's own one-bit fix.
Apple's line, for every bit width, replaces it. The CUDA kernel and the
test are as in Apple's commit.

(cherry picked from commit 32a183e)
@jjang-ai
jjang-ai merged commit 6c7491b into osaurus-ai:main Nov 28, 2026
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.

4 participants