Repository navigation
Added MLXCustomFunction + MLXClosure - #301
Conversation
| import MLX | ||
|
|
||
| @main | ||
| struct GridSampleExample { |
There was a problem hiding this comment.
These examples might make good tests -- both to show an example of use and to exercise it on the CI system
| import Foundation | ||
|
|
||
| // MARK: - MLXClosure wrapper | ||
| public struct MLXClosure { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed, migrated everything inside the MLXCustomFunction
|
|
||
| // 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] { |
| } | ||
|
|
||
| // Convert Swift array of MLXArray back to C mlx_vector_array | ||
| func swift_to_mlx_vector_array(_ arrays: [MLXArray]) -> mlx_vector_array { |
|
|
||
| // MARK: - Result Builder | ||
| @resultBuilder | ||
| public enum MLXCustomFunctionBuilder { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Oh, that's great, just moved to use this syntax.
|
Looks really interesting! |
…ied customFunction
|
@davidkoski thank you for the feedback, just pushed the changes, let me know if anything else stands out. |
…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.
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
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes