Repository navigation
Integrate upstream fixes to transforms and ops (vmap, VJP/JVP, compile, conv, stream arguments) - #50
Merged
Merged
Conversation
(cherry picked from commit a76fbdd)
…rsqrt (ml-explore#4606) (cherry picked from commit 5ff1905)
…-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)
(cherry picked from commit 9fcc72b)
Co-authored-by: Cheng <git@zcbenz.com> (cherry picked from commit 84a72e3)
Co-authored-by: Cheng <git@zcbenz.com> (cherry picked from commit 0fdcdc0)
(cherry picked from commit bebd642)
(cherry picked from commit 7cfbc6a)
(cherry picked from commit 81770e8)
(cherry picked from commit 8e88e50)
Co-authored-by: Cheng <git@zcbenz.com> (cherry picked from commit 303763f)
(cherry picked from commit 001a2da)
(cherry picked from commit bdbde25)
(cherry picked from commit a21f892)
…lore#4612) Co-authored-by: Cheng <git@zcbenz.com> (cherry picked from commit bd63d38)
…plore#4790) (cherry picked from commit 55626cb)
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.
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/transforms-ops: 19 upstream commits and 2 of ours onorigin/main3b39b5e.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 itscontributors': 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, and these change results on every backend. The fork
has not changed this code since the merge base 64ea011 (its own
ops.cppwork is in the quantization ops), andeach fix applies to
origin/mainunchanged, 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:
viewover the last axis, when the item size changes, resized the batch axis.scatterput the batch axis of the updates in the wrong place.logcumsumexpof an integer array returned an integer dtype; now promotes to float.compilemerged equal scalars even when they were outputs.padding_hiwherepadding_lobelongs and passed negative padding on; wrong shape with uneven or large padding.layer_normand SDPA fallbacks ran a multiply and the maskwhereon the default stream.take(a, int index)did not normalise a negative index.random.normalwithloc/scalethat broadcast pastshapereturned an array whose data did not match its shape; now raises.PadVJP/JVP ignored the pad-value input; the gradient with respect to the pad value was wrong.depends,number_of_elements,imag,KeySequence::next, linalg, fft,LayerNorm::vjp, and intransforms.cppthe vjp/jvp tracers, vmap outputs andcustom_function). Adds.github/scripts/check_stream_args.py, a pre-commit hook and CI step that run it, and C++ tests.tri_invignored the triangular and upper flags.SliceUpdate/DynamicSliceUpdatewith an unbatched update did not broadcast it over the batch.powerVJP was missing the conjugate.conv_generalwith 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.custom_functionVJP/JVP traversal walked into the trailing inputs ofCustomTransforms(its outputs), reaching opaque bodies such as custom Metal kernels.import_functiondid 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_dividebuilt its step mask withlogical_andon the defaultstream. ml-explore#4746's
check_stream_args.pyreports it on this fork. Upstream no longer has this code: ml-explore#4642 replaced itwith a
FloorDivideprimitive, which is a feature and not part of this intake. The fork still has ml-explore#4515's version, sothe commit passes
s.The second (1b04184) keeps the fork building with clang < 16. ml-explore#4612 makes
get_inputs()intransforms.cppreturn
std::views::take(...)and walks it withstd::ranges::for_each. Clang 14 with libstdc++ 12, the default onDebian 12 and Ubuntu 22.04, cannot compile these views (39 errors, all in
transforms.cpp; with #40 every other filebuilds).
get_inputs()now returns astd::spanover the same inputs, and the loops are range-for loops, the formthis code had before ml-explore#4612.
Dependency: ml-explore#4612 needs ml-explore#4746 (its
transforms.cpphunks sit on ml-explore#4746'smake_tracer(p, fallback_stream)), soboth are here.
Not included here: other core/CPU/io fixes are in
apple/cpu-kernels,apple/array-viewsandapple/io. Left outof the whole intake: ml-explore#4694 (already our #41) and ml-explore#4634 (allocator API change; listed for the fork's maintainer).
Conflict resolutions
mlx/ops.cpp(quantized_matmul, affine branch): upstream addssto the threeastypecalls; the fork'sversion (mixed BF16/FP16 path) already passes
sin both arms. Kept the fork's lines.mlx/primitives.cpp(Scan::jvp): the hunk passesstream()to the outerexpcalls of theLogAddExpJVP branch. That branch came with upstream's Add jvp support for logcumsumexp ml-explore/mlx#4611 (logcumsumexp JVP, a feature), which the fork does not have, so
there is nothing to change. Kept the fork's lines. All other hunks of Fix C++ op implementations missing stream arg ml-explore/mlx#4746 in both files
are in; checked line by line against the upstream diff.
tests/ops_tests.cpp, two adaptations so that its new test builds and runs on the fork:array::primitive().stream(), so the file now includesmlx/primitives.h. Upstream's file hasincluded it since Add FloorDivide primitive for integer floor division ml-explore/mlx#4642 (FloorDivide primitive, a feature the fork does not have).
jvpssubcase also checks the JVP oflogcumsumexp, which needs Add jvp support for logcumsumexp ml-explore/mlx#4611; that one 7-line check is left out.The
vjpscheck oflogcumsumexpstays..pre-commit-config.yaml: kept the fork's hooks (black, isort) and added only Fix C++ op implementations missing stream arg ml-explore/mlx#4746's new localcheck-stream-argshook, not upstream's separate switch to ruff.python/tests/test_fast.py: kept the fork's tests and added Fix custom_function vmap and jvp traversal with opaque bodies ml-explore/mlx#4612'stest_custom_function_metal_kernel_vmap_jvp(+47 lines, as upstream); the neighbouring upstream tests in the conflict belong to other commits.
Overlaps with open PRs
tests/ops_tests.cpponly. Fix C++ op implementations missing stream arg ml-explore/mlx#4746 appends C++ tests at the end of the file, where these PRs alsoappend; keep both.
apple/metal-runtime:python/tests/test_fast.py, adjacent additions; keep both.Before merging
check_stream_args.pyruns from pre-commit overmlx/**/*.{cpp,h}, so it also checks the fork's own code. On thisbranch it passes
--self-test. Over the default file set, it first reportedfloor_divide(fixed by the commit ontop) and now exits 0.
make_tracerto share the buffer of an already evaluated primal instead of copying it.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
mainand no UBSan report thatmainlacks. OnmainGCC fails onlytest one-bit affine endpoints CPU GPU and small-K dispatch(needs a GPU), and clang the same plus 16compile/fusioncases whose CPUJIT 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 filesthis PR touches, with this branch and with
main. Each upstream test method this PR adds or changes also ran aloneagainst
main's build, with this branch's version of its file.Linux: 287 cases (
main284).check_stream_args.py --self-testpasses, and the hook overmlx/exits 0.macOS: 323 cases (
main320), all pass on the CPU; on Metal all buttest concurrent allocator statistics, whichfails intermittently on
maintoo.Python,
test_autograd,test_compile,test_conv,test_conv_transpose,test_fast,test_fft,test_ops,test_random,test_vmap: the failing subtests ofmainare 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 onmain, andtest_scan_invalid_axis(Promote integer inputs in logcumsumexp ml-explore/mlx#4625) errors on the GPU only. The threetest_torch_conv_transpose_1D*tests need torch andwere 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.cppfailed 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 thesame 17 cases as
mainwith Build with clang < 16: capture and typename in the CPU backend #40.🤖 Generated with Claude Code