Skip to content

Integrate upstream Metal fixes (JIT build, bool sort, runtime-compile switch, gemv_t tiles, main-thread check, NAX query) - #60

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

Conversation

@rcfa

@rcfa rcfa commented Nov 26, 2026

Copy link
Copy Markdown

Branch apple/r3-metal, 6 commits on origin/main 23f364e (tip 06abe72).

Bring in six Metal changes from ml-explore/mlx main (round 3 of the intake): a JIT build error, a missing sort
instantiation, a switch that turns off runtime compilation of Metal libraries, gemv_t tile tuning, the main-thread
check that Metal command-queue retention depends on, and a Python query for NAX. 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.

Why we push them: osaurus and vmlx-swift ship from this fork on Apple GPUs. None of these is in origin/main, and
four apply unchanged. ml-explore#4908 is ported to the fork's settings reader; ml-explore#4956 applies with context differences only.

For consumers: the public header mlx/backend/metal/metal.h gains is_nax_available() (ml-explore#4956). The vmlx-swift pin
bump must run tools/update-mlx.sh.

Included, in upstream order:

Upstream Commit Here What it fixes
ml-explore#4833 07e7bda b2cc729 The steel integral_const_binop macro used metal::remove_addrspace_t, which broke the JIT compilation of kernels that include it; it now takes the result type from decltype(tv op uv).
ml-explore#4861 ebe3de6 d456872 The Metal sort kernels had no bool instantiation, so sort and argsort of a bool array failed on the GPU; adds the block and multi-block bool kernels and a test.
ml-explore#4908 1579e1c 81ab7d9 Metal always built a missing library from source at run time. MLX_METAL_RUNTIME_COMPILE=0 now makes Device::get_library throw instead; libraries already loaded, also by name from a .metallib, are still used. Read with env::get_var here (Apple: config::get).
ml-explore#4850 b835fb3 4aea896 Performance: gemv with a transposed matrix, a long K and few outputs launched too few simdgroups to fill the GPU. Adds two gemv_t tile instantiations and picks smaller threadgroups when fewer than 128 simdgroups would run. Adds tests.
ml-explore#4936 4b15eff ae59c89 is_main_thread() took the first thread that called it for the main thread, so Metal command-queue retention went wrong when a worker thread initialized MLX first. It now asks the OS: pthread_main_np() on Apple, gettid() == getpid() on Linux.
ml-explore#4956 5c89894 06abe72 Python could not ask whether MLX kernels can use the GPU neural accelerators (NAX); test_sdpa_full_head_dim_512_nax probed for it by catching a ValueError. Adds mx.metal.is_nax_available() (the C++ declaration moves to the public metal.h) and uses it in that test.

Not included here: ml-explore#4893 (SDPA routing) is in apple/r3-metal-sdpa-vjp, because Apple wrote it on top of the fused
SDPA backward (ml-explore/mlx PR 4563); ml-explore#4930 is in apple/r3-core, before ml-explore#4929, which needs it. Left out of the
batch, by ml-explore/mlx PR number: 4903 (NAX dsplit JIT template arguments: the same fix as the fork's #17, no test);
4883 (stop wiring user buffers: waits for a decode A/B on a large mmap-loaded model); 4647 (config module: not
needed, see ml-explore#4908). The other round-3 commits are in apple/r3-core, apple/r3-cuda, apple/r3-python-docs-build,
apple/r3-affine-qmv and apple/fixes-2.

Conflict resolutions

  • Allow disabling Metal runtime compilation ml-explore/mlx#4908, mlx/backend/metal/device.cpp: Apple calls config::get("MLX_METAL_RUNTIME_COMPILE", 1); the config module
    (ml-explore/mlx PR 4647) is not in the fork, so this reads env::get_var("MLX_METAL_RUNTIME_COMPILE", 1), as the
    fork's other settings do. docs/src/usage/environment_variables.rst: only the new MLX_METAL_RUNTIME_COMPILE block
    (Apple's context has MLX_CONV_WINOGRAD, which the fork lacks).
  • [Metal] Expose NAX kernel availability to Python ml-explore/mlx#4956: context only; the added and removed lines equal Apple's. is_nax_available() moves to metal.h as in
    Apple's; device.h keeps the fork's metal_kernel.h include and has no gpu_core_count(). In
    python/tests/test_device.py, Apple's context test test_cuda_get_arch_list is not taken (mx.cuda.get_arch_list
    is not in the fork).
  • The message of the Allow disabling Metal runtime compilation ml-explore/mlx#4908 commit says what differs from Apple's.

Overlaps

None with the batch's other branches (merge-tree).

Validation

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

  • Linux arm64 (container vmlx-arm64, clang Release, Highway), C++ suite on the CPU.

  • macOS (M4 Max, Release, Metal on), C++ suite (tests/tests) on the CPU and on Metal.

  • Python (macOS, Python 3.14.5), on the GPU and with DEVICE=cpu: the 16 test modules the batch touches, among
    them this branch's test_blas, test_device, test_fast_sdpa and test_ops, on main and on the integration
    tree 63ac1f9df (origin/main with all batch branches merged, this one included).

  • Linux: 412 of 412 cases pass at the tip 06abe72, as main.

  • macOS: 390 of 390 cases pass on the CPU and on Metal, as main.

  • Python: no test that passes on main fails on the integration tree. GPU: main fails 3 tests, the integration tree
    1 (test_input_dilation_grad, as main). CPU: both fail the same 4 (GPU-only tests that do not skip on the CPU,
    one of them in test_fast_sdpa, and the Winograd budget). All new tests of the batch pass: 27 on the GPU, 85 on the
    CPU (the rest skip). main's CPU run stops at test_reduce: test_init_reduce_large_output (from ml-explore/mlx
    PR 4437) crashes it with SIGSEGV, the overflow that this batch's fix/reduce-int64-loops fixes. So test_reduce,
    test_threads, test_vmap and test_zero_copy have no main CPU result; on the integration tree none of their
    tests fails on the CPU.

  • Not run: a GPU with NAX (the is_nax_available() true path); a test of MLX_METAL_RUNTIME_COMPILE=0 and of Fix Metal command queue retention when a worker thread initializes MLX ml-explore/mlx#4936
    (neither brings one); a speed measurement of Tune gemv_t tiles for long K and small N ml-explore/mlx#4850.

Validation of the batch in shared

This batch's PRs but the last (fix/float-floor-divide, which came out of this validation), merged together on
main 23f364e with the two overlap resolutions (apple/fixes-2 and apple/r3-core in
python/tests/test_quantized.py; apple/r3-core and apple/fixes-3 in mlx/allocator.h), are mlx 8de1a51; vmlx-swift 31458a0d3 builds on it (osaurus-ai/vmlx-swift main bbd237750 with our open vmlx-swift
patches). Built and run, compared with main:

  • macOS (M4 Max, Release, Metal on): C++ suite 410 of 410 on the CPU and on Metal. vmlx-swift builds for macOS 14.0;
    its suite (swift test --no-parallel, 1046 tests) fails no test that passes on its main.
  • Linux arm64 and amd64 (containers, clang Release, Highway): C++ suite 431 of 431 on both (main 412 of 412).
    vmlx-swift's Linux CI steps pass: VMLXLinuxTests 37 of 37, 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). arm64: main 881 pass, 92 skip, 2 fail;
    the batch 906 pass, 96 skip, 2 fail (tree c43a37eec, which differs from 8de1a51 only in a C++ test). amd64: main
    882 pass, 92 skip, 1 fail; the batch 906 pass, 96 skip, 1 fail, 1 error. 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 off by up to 8.8 against float64 (MLX is within 0.06). test_async_eval_error_in_synchronize failed once on amd64; it fails about 1 run in 5 there on main too: the error it expects from synchronize can come from async_eval (the same code is on ml-explore/mlx main).
  • Three tests of main fail at random, on every branch alike, because they draw unseeded inputs or race:
    test_double.TestDouble.test_unary_ops in about 8% of runs (float32 erf is accurate to 1.5e-7 absolute, which
    misses rtol 1e-5 near 0 against the fork's accurate float64 erf); test_ops.TestOps.test_divmod in about 1%
    (the float16 quotient is off by one when a / b rounds up to an integer, e.g. 73.625 / 1.248 = 58.99 gives 59;
    ml-explore/mlx main has the same code); and, on amd64, test_async_eval_error_in_synchronize (above). This batch's
    fix/float-floor-divide fixes the second; the first gets a PR of its own.
  • 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.
  • This branch alone, the same Linux Python run (arm64): 883 pass, 92 skip, 2 fail: test_compile_sigmoid_matches_eager, test_torch_conv_2D as on main.

🤖 Generated with Claude Code

aleroot and others added 6 commits November 24, 2026 07:00
Ported: upstream reads the variable with config::get, from the config
module of ml-explore/mlx PR 4647, which this tree does not have.
env::get_var reads the same variable with the same default.

(cherry picked from commit 1579e1c)
@jjang-ai
jjang-ai merged commit 555ee88 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.

8 participants