Repository navigation
Integrate upstream Metal fixes (JIT build, bool sort, runtime-compile switch, gemv_t tiles, main-thread check, NAX query) - #60
Merged
Conversation
…lore#4833) (cherry picked from commit 07e7bda)
(cherry picked from commit ebe3de6)
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)
(cherry picked from commit b835fb3)
ml-explore#4936) (cherry picked from commit 4b15eff)
(cherry picked from commit 5c89894)
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.
Branch
apple/r3-metal, 6 commits onorigin/main23f364e (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 -xof 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, andfour 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.hgainsis_nax_available()(ml-explore#4956). The vmlx-swift pinbump must run
tools/update-mlx.sh.Included, in upstream order:
integral_const_binopmacro usedmetal::remove_addrspace_t, which broke the JIT compilation of kernels that include it; it now takes the result type fromdecltype(tv op uv).sortandargsortof a bool array failed on the GPU; adds the block and multi-block bool kernels and a test.MLX_METAL_RUNTIME_COMPILE=0now makesDevice::get_librarythrow instead; libraries already loaded, also by name from a.metallib, are still used. Read withenv::get_varhere (Apple:config::get).gemv_ttile instantiations and picks smaller threadgroups when fewer than 128 simdgroups would run. Adds tests.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.test_sdpa_full_head_dim_512_naxprobed for it by catching aValueError. Addsmx.metal.is_nax_available()(the C++ declaration moves to the publicmetal.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 fusedSDPA backward (ml-explore/mlx PR 4563); ml-explore#4930 is in
apple/r3-core, before ml-explore#4929, which needs it. Left out of thebatch, 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-qmvandapple/fixes-2.Conflict resolutions
mlx/backend/metal/device.cpp: Apple callsconfig::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 thefork's other settings do.
docs/src/usage/environment_variables.rst: only the newMLX_METAL_RUNTIME_COMPILEblock(Apple's context has
MLX_CONV_WINOGRAD, which the fork lacks).is_nax_available()moves tometal.has inApple's;
device.hkeeps the fork'smetal_kernel.hinclude and has nogpu_core_count(). Inpython/tests/test_device.py, Apple's context testtest_cuda_get_arch_listis not taken (mx.cuda.get_arch_listis not in the fork).
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, amongthem this branch's
test_blas,test_device,test_fast_sdpaandtest_ops, onmainand on the integrationtree 63ac1f9df (
origin/mainwith 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
mainfails on the integration tree. GPU:mainfails 3 tests, the integration tree1 (
test_input_dilation_grad, asmain). 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 theCPU (the rest skip).
main's CPU run stops attest_reduce:test_init_reduce_large_output(from ml-explore/mlxPR 4437) crashes it with SIGSEGV, the overflow that this batch's
fix/reduce-int64-loopsfixes. Sotest_reduce,test_threads,test_vmapandtest_zero_copyhave nomainCPU result; on the integration tree none of theirtests fails on the CPU.
Not run: a GPU with NAX (the
is_nax_available()true path); a test ofMLX_METAL_RUNTIME_COMPILE=0and 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
sharedThis batch's PRs but the last (
fix/float-floor-divide, which came out of this validation), merged together onmain23f364e with the two overlap resolutions (apple/fixes-2andapple/r3-coreinpython/tests/test_quantized.py;apple/r3-coreandapple/fixes-3inmlx/allocator.h), are mlx 8de1a51; vmlx-swift 31458a0d3 builds on it (osaurus-ai/vmlx-swiftmainbbd237750 with our open vmlx-swiftpatches). Built and run, compared with
main:its suite (
swift test --no-parallel, 1046 tests) fails no test that passes on itsmain.main412 of 412).vmlx-swift's Linux CI steps pass:
VMLXLinuxTests37 of 37, the CPU kernel tests 61 of 61 at both thread settings.DEVICE=cpu,python/testsmodule by module, torch 2.15 for the CPU). arm64:main881 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:
main882 pass, 92 skip, 1 fail; the batch 906 pass, 96 skip, 1 fail, 1 error. No test that passes on
mainfails on the batch. The failures aremain's own:test_compile_sigmoid_matches_eager(bfloat16: the compiled CPU sigmoid is less accurate than theeager Highway kernel) and, on arm64 only, bfloat16
test_torch_conv_2D, where torch 2.15's own aarch64 bfloat16conv2d is off by up to 8.8 against float64 (MLX is within 0.06).
test_async_eval_error_in_synchronizefailed once on amd64; it fails about 1 run in 5 there onmaintoo: the error it expects fromsynchronizecan come fromasync_eval(the same code is on ml-explore/mlx main).mainfail at random, on every branch alike, because they draw unseeded inputs or race:test_double.TestDouble.test_unary_opsin about 8% of runs (float32erfis accurate to 1.5e-7 absolute, whichmisses
rtol1e-5 near 0 against the fork's accurate float64erf);test_ops.TestOps.test_divmodin about 1%(the float16 quotient is off by one when
a / brounds 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'sfix/float-floor-dividefixes the second; the first gets a PR of its own.its Linux and macOS jobs.
test_compile_sigmoid_matches_eager,test_torch_conv_2Das onmain.🤖 Generated with Claude Code