Skip to content

Add Sendable closures and 3-parameter VJP support to MLXCustomFunction - #481

Merged
davidkoski merged 2 commits into
ml-explore:mainfrom
shubhransh-gupta:vjp-sendable-three-params
Oct 19, 2026
Merged

davidkoski merged 2 commits into
ml-explore:mainfrom
shubhransh-gupta:vjp-sendable-three-params

Conversation

@shubhransh-gupta

Copy link
Copy Markdown
Contributor

Description

Following discussion on #475:

  1. Make Forward and VJP components conform to @Sendable, ensuring custom functions are pure and safe across concurrency contexts.
  2. Make CustomFunction return @Sendable ([MLXArray]) -> [MLXArray].
  3. Add a 3-parameter overload for VJP:
    public func VJP(
        _ f: @escaping @Sendable (_ primals: [MLXArray], _ cotangents: [MLXArray], _ outputs: [MLXArray]) -> [MLXArray]
    ) -> MLXCustomFunctionComponent
    This passes through the forward outputs already provided by the underlying C closure (mlx_closure_custom_new_func_payload) without needing extra C++/Cmlx changes.
  4. Maintain full backward compatibility for the 2-parameter VJP (_ primals: [MLXArray], _ cotangents: [MLXArray]) signature.
  5. Add unit tests in TransformTests covering 2-parameter VJP, 3-parameter VJP using outputs, and cross-task Sendability.

Closes #475

@davidkoski

Copy link
Copy Markdown
Member

This needs a swift-format run (the lint failure)

@shubhransh-gupta

Copy link
Copy Markdown
Contributor Author

Hi @davidkoski,

I've run swift-format and pushed the formatted changes in commit 5efc98a. All CI checks (including lint, macOS, and Linux/CUDA builds & tests) are now passing green.

Please take a look when you have a chance!

@davidkoski davidkoski mentioned this pull request Sep 28, 2026
7 tasks done
@davidkoski
davidkoski force-pushed the vjp-sendable-three-params branch from bdc5e3c to 215b4cd Compare October 19, 2026 15:56
@davidkoski

Copy link
Copy Markdown
Member

Rebased to main

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good, thank you!

@davidkoski
davidkoski merged commit 863ee9f into ml-explore:main Oct 19, 2026
7 checks passed
tsnAnh pushed a commit to TitoniumLab/mlx-swift that referenced this pull request Nov 7, 2026
Brings in the seven upstream commits after 35c10b4: ml-explore#481 (Sendable closures and
3-parameter VJP for MLXCustomFunction), ml-explore#503 (shardedToAllPredicate), ml-explore#494 (iOS
26.2 launch crash from Logger.isEnabled), ml-explore#498 (GPU.deviceInfo device selection),
ml-explore#418 (MaterializedArray is Sendable), ml-explore#505 (tripletLoss fix). The cLogSupport
removal in release.yml comes in with the merge; no fork-side edit was needed.
No conflicts.

Co-Authored-By: Claude Haiku 5.5 <[email protected]>
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.

[BUG] VJP should be Sendable

2 participants