Skip to content

Integrate upstream fixes to transforms and ops (vmap, VJP/JVP, compile, conv, stream arguments) - #50

Merged
jjang-ai merged 21 commits into
osaurus-ai:mainfrom
rcfa:apple/transforms-ops
Nov 22, 2026
Merged

jjang-ai merged 21 commits into
osaurus-ai:mainfrom
rcfa:apple/transforms-ops

Conversation

@rcfa

@rcfa rcfa commented Nov 5, 2026

Copy link
Copy Markdown

Branch apple/transforms-ops: 19 upstream commits and 2 of ours on origin/main 3b39b5e.

Bring in nineteen bug fixes from ml-explore/mlx main to backend-independent code: vmap rules, VJP/JVP rules,
compile, conv_general, a few ops, and ops that ignored the caller's stream. 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, and these change results on every backend. The fork
has not changed this code since the merge base 64ea011 (its own ops.cpp work is in the quantization ops), and
each fix applies to origin/main unchanged, apart from the hunks listed under conflicts, so each bug is still here.
One of ml-explore#4746's hunks the fork had already fixed itself (c0a51a0, quantized_matmul casts on the requested stream).

Included, in upstream order:

Upstream Commit Here What it fixes
ml-explore#4586 a76fbdd 828bbdd vmap of view over the last axis, when the item size changes, resized the batch axis.
ml-explore#4606 5ff1905 0dbb031 Complex VJPs of arccos, arccosh, arcsin, arcsinh, arctan, arctanh, cos, sqrt and rsqrt were missing the conjugate; complex arccosh JVP had the wrong sign for Re(x) < 0.
ml-explore#4322 2553287 62f626a vmap of scatter put the batch axis of the updates in the wrong place.
ml-explore#4625 2b2bb49 aa1694b logcumsumexp of an integer array returned an integer dtype; now promotes to float.
ml-explore#4637 9fcc72b 3027e73 vmap of an inverse real FFT to an odd length returned the even length.
ml-explore#4658 84a72e3 0ecea38 compile merged equal scalars even when they were outputs.
ml-explore#4662 0ab821b 9ed9dfe Conv input gradient used padding_hi where padding_lo belongs and passed negative padding on; wrong shape with uneven or large padding.
ml-explore#4663 0fdcdc0 5242cfb layer_norm and SDPA fallbacks ran a multiply and the mask where on the default stream.
ml-explore#4687 bebd642 de1bb96 The gather matmul index cast ran on the default stream.
ml-explore#4729 7cfbc6a f41244e take(a, int index) did not normalise a negative index.
ml-explore#4730 81770e8 90f534d random.normal with loc/scale that broadcast past shape returned an array whose data did not match its shape; now raises.
ml-explore#4736 8e88e50 aa238e6 Pad VJP/JVP ignored the pad-value input; the gradient with respect to the pad value was wrong.
ml-explore#4746 303763f cafdecc Many ops called sub-ops without the caller's stream (gather/scatter index broadcast, affine_quantize, depends, number_of_elements, imag, KeySequence::next, linalg, fft, LayerNorm::vjp, and in transforms.cpp the vjp/jvp tracers, vmap outputs and custom_function). Adds .github/scripts/check_stream_args.py, a pre-commit hook and CI step that run it, and C++ tests.
ml-explore#4748 001a2da 1750004 vmap of tri_inv ignored the triangular and upper flags.
ml-explore#4770 bdbde25 a561f03 vmap of SliceUpdate/DynamicSliceUpdate with an unbatched update did not broadcast it over the batch.
ml-explore#4778 a21f892 4926729 Complex power VJP was missing the conjugate.
ml-explore#4688 6a304d1 3b58b9c conv_general with negative padding and input dilation > 1 cropped the wrong input elements; CUDA conv no longer picks cuDNN modes that cannot express stride or input dilation.
ml-explore#4612 bd63d38 cce319b custom_function VJP/JVP traversal walked into the trailing inputs of CustomTransforms (its outputs), reaching opaque bodies such as custom Metal kernels.
ml-explore#4790 55626cb 9c193ba import_function did not check that a file's trace input ids and trace inputs match in count (out-of-bounds read on a corrupt file).

On top, two commits of ours. The first (f3ab09f): integer floor_divide built its step mask with logical_and on the default
stream. ml-explore#4746's check_stream_args.py reports it on this fork. Upstream no longer has this code: ml-explore#4642 replaced it
with a FloorDivide primitive, which is a feature and not part of this intake. The fork still has ml-explore#4515's version, so
the commit passes s.

The second (1b04184) keeps the fork building with clang < 16. ml-explore#4612 makes get_inputs() in transforms.cpp
return std::views::take(...) and walks it with std::ranges::for_each. Clang 14 with libstdc++ 12, the default on
Debian 12 and Ubuntu 22.04, cannot compile these views (39 errors, all in transforms.cpp; with #40 every other file
builds). get_inputs() now returns a std::span over the same inputs, and the loops are range-for loops, the form
this code had before ml-explore#4612.

Dependency: ml-explore#4612 needs ml-explore#4746 (its transforms.cpp hunks sit on ml-explore#4746's make_tracer(p, fallback_stream)), so
both are here.

Not included here: other core/CPU/io fixes are in apple/cpu-kernels, apple/array-views and apple/io. Left out
of the whole intake: ml-explore#4694 (already our #41) and ml-explore#4634 (allocator API change; listed for the fork's maintainer).

Conflict resolutions

Overlaps with open PRs

Before merging

Validation

Compared with main (3b39b5e), built and run the same way:

  • Linux arm64 (Debian in a VM on an M4 Max), C++ suite on the CPU, GCC 12 Debug and clang 21 Debug with UBSan:
    the same failing cases as main and no UBSan report that main lacks. On main GCC fails only test one-bit affine endpoints CPU GPU and small-K dispatch (needs a GPU), and clang the same plus 16 compile/fusion cases whose CPU
    JIT fails in that VM.

  • macOS (M4 Max, Apple clang 21), C++ suite, Release, on the CPU and on Metal.

  • Python (macOS, Python 3.14, the extension in Release with Metal), on the GPU and with DEVICE=cpu: the test files
    this PR touches, with this branch and with main. Each upstream test method this PR adds or changes also ran alone
    against main's build, with this branch's version of its file.

  • Linux: 287 cases (main 284). check_stream_args.py --self-test passes, and the hook over mlx/ exits 0.

  • macOS: 323 cases (main 320), all pass on the CPU; on Metal all but test concurrent allocator statistics, which
    fails intermittently on main too.

  • Python, test_autograd, test_compile, test_conv, test_conv_transpose, test_fast, test_fft, test_ops,
    test_random, test_vmap: the failing subtests of main are unchanged; the new methods pass on both devices.
    Against main, on both devices, these fail or error: test_vmap_view, test_complex_unary_vjps,
    test_vmap_scatter_higher_rank, test_fft_vmap_explicit_size, test_compile_scalar_constant_output,
    test_padding_input_grad, test_take, test_normal_loc_scale_shape, test_vmap_tri_inverse,
    test_vmap_slice_update, test_complex_power_vjps, test_input_dilation_grad,
    test_custom_function_metal_kernel_vmap_jvp. test_pad_value_grad (Fix/padding jvp vjp ml-explore/mlx#4736) crashes on main, and
    test_scan_invalid_axis (Promote integer inputs in logcumsumexp ml-explore/mlx#4625) errors on the GPU only. The three test_torch_conv_transpose_1D* tests need torch and
    were skipped.

  • clang 14.0.6 with libstdc++ 12 (Debian 12), with Build with clang < 16: capture and typename in the CPU backend #40 applied: before the second commit of ours, transforms.cpp
    failed with 39 errors in Fix custom_function vmap and jvp traversal with opaque bodies ml-explore/mlx#4612's <ranges> code; with it, the branch builds with 0 errors and the suite fails the
    same 17 cases as main with Build with clang < 16: capture and typename in the CPU backend #40.

🤖 Generated with Claude Code

michalk8 and others added 21 commits November 4, 2026 16:41
…-explore#4322)

Co-authored-by: Cheng <git@zcbenz.com>
(cherry picked from commit 2553287)
Co-authored-by: Cheng <git@zcbenz.com>
(cherry picked from commit 2b2bb49)
Co-authored-by: Cheng <git@zcbenz.com>
(cherry picked from commit 84a72e3)
Co-authored-by: Cheng <git@zcbenz.com>
(cherry picked from commit 0fdcdc0)
Co-authored-by: Cheng <git@zcbenz.com>
(cherry picked from commit 303763f)
…lore#4612)

Co-authored-by: Cheng <git@zcbenz.com>
(cherry picked from commit bd63d38)
floor_divide for integers built its step mask with logical_and on the
default stream, while every other op in the function uses s. The check
that ml-explore#4746 adds (.github/scripts/check_stream_args.py) reports it.

Apple's main no longer has this code: ml-explore#4642 replaced it with a
FloorDivide primitive. This fork still has ml-explore#4515's version, so the
stream is passed here.
ml-explore#4612 (cherry-picked above) makes get_inputs() return
std::views::take(a.inputs(), count) and walks it with
std::ranges::for_each. Clang 14, the default clang of Debian 12 and
Ubuntu 22.04, cannot compile these views with libstdc++ 12: 39 errors
in transforms.cpp, such as "constraints not satisfied for alias
template 'sentinel_t'". With osaurus-ai#40 every other file of the
fork builds with clang 14.

get_inputs() now returns a std::span over the same inputs, and the
five for_each calls are range-for loops, the form this code had before
ml-explore#4612. The traversal is the same.
@jjang-ai
jjang-ai merged commit 64fe2a6 into osaurus-ai:main Nov 22, 2026
@rcfa
rcfa deleted the apple/transforms-ops branch November 23, 2026 00:43
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.