Skip to content

Added MLXCustomFunction + MLXClosure - #301

Merged
davidkoski merged 4 commits into
ml-explore:mainfrom
nataliyakosmyna:mlx-custom-function
Dec 1, 2025
Merged

davidkoski merged 4 commits into
ml-explore:mainfrom
nataliyakosmyna:mlx-custom-function

Conversation

@nataliyakosmyna

Copy link
Copy Markdown
Contributor

Proposed changes

Implemented custom function and VJP functionality in Swift.
Based on python implementation.
Examples implemented follow Python structure too.

Feedback and suggestions are welcome.

Addresses

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

import MLX

@main
struct GridSampleExample {

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.

These examples might make good tests -- both to show an example of use and to exercise it on the CI system

Comment thread Source/MLX/MLXClosure.swift Outdated
import Foundation

// MARK: - MLXClosure wrapper
public struct MLXClosure {

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.

I don't think we want MLXClosure as a public type. Look at how compile works (e.g. CompiledFunction to encapsulate the state) and a signature like this:

public func compile(
    inputs: [any Updatable] = [], outputs: [any Updatable] = [], shapeless: Bool = false,
    _ f: @escaping ([MLXArray]) -> [MLXArray]
) -> @Sendable ([MLXArray]) -> [MLXArray] {

I think this should be something similar -- it will hold the mlx_closure and look like a callable function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, migrated everything inside the MLXCustomFunction

Comment thread Source/MLX/MLXCustomFunction.swift Outdated

// MARK: - Example: bridging helpers
// Convert a C mlx_vector_array pointer to a Swift array of MLXArray
func mlx_vector_array_to_swift(_ v: UnsafePointer<mlx_vector_array>?) -> [MLXArray] {

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.

See mlx_vector_array_values

Comment thread Source/MLX/MLXCustomFunction.swift Outdated
}

// Convert Swift array of MLXArray back to C mlx_vector_array
func swift_to_mlx_vector_array(_ arrays: [MLXArray]) -> mlx_vector_array {

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.

See new_mlx_vector_array

Comment thread Source/MLX/MLXCustomFunction.swift Outdated

// MARK: - Result Builder
@resultBuilder
public enum MLXCustomFunctionBuilder {

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.

This looks like an interesting approach -- I wonder if it would make sense to make a function (something like customFunction()) that takes a @MLXCustomFunctionBuilder argument and encapsulate the use of this.

Something like:

let f = customFunction {
    Forward { ... }
    VJP { ... }
}

I am not sure if that is the exact syntax but perhaps something like that is doable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, that's great, just moved to use this syntax.

@davidkoski

Copy link
Copy Markdown
Member

Looks really interesting!

@nataliyakosmyna

Copy link
Copy Markdown
Contributor Author

@davidkoski thank you for the feedback, just pushed the changes, let me know if anything else stands out.

@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.

Very cool, thank you!

@davidkoski
davidkoski merged commit 9594dbe into ml-explore:main Dec 1, 2025
This was referenced Jan 13, 2026
jeethu added a commit to numen-tech/mlx-swift that referenced this pull request Oct 15, 2026
…FAST_SYNCH test (gemma4-qat#301)"

This reverts commit 98ee695.

The fence probe (gemma4-qat#301) found no fence_wait / input_coherent
in the shipped app metallib: Xcode compiles the package's default.metallib
at the package floor (`metal -c -target air64-apple-macos14.0`), i.e.
Metal 3.1, so the `__METAL_VERSION__ >= 320` guard empties fence.metal in
every consumer build, not only in this package's own test build. The kernel
can't ship from a SwiftPM package with a macOS 14 / iOS 17 floor, so the
vendored file and its test are dropped; ml-explore#301's fence item stays open.
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.

2 participants