-
Notifications
You must be signed in to change notification settings - Fork 122
update: unlock native Linux build for CMake and CPU backend only #293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,3 +90,6 @@ fastlane/test_output | |
|
|
||
| iOSInjectionProject/ | ||
| .swiftpm | ||
|
|
||
| # VS Code | ||
| .vscode/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,10 +96,14 @@ struct Tutorial { | |
|
|
||
| assert(df2dx2.item() == Float(2)) | ||
| } | ||
|
|
||
| static func main() { | ||
| scalarBasics() | ||
| arrayBasics() | ||
| automaticDifferentiation() | ||
| let osName = ProcessInfo.processInfo.operatingSystemVersionString.lowercased() | ||
| let device: Device = osName.contains("linux") ? .cpu : .gpu | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we need to do something inside the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, and I like your suggestion to just wait this one out and see how the CUDA integration turns out to be for Linux. |
||
|
|
||
| Stream.withNewDefaultStream(device: device) { | ||
| scalarBasics() | ||
| arrayBasics() | ||
| automaticDifferentiation() | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright © 2025 Apple Inc. | ||
|
|
||
| import Cmlx | ||
| import Foundation | ||
| import Metal | ||
|
|
||
| // MARK: - Metal | ||
|
|
||
| extension MLXArray { | ||
|
|
||
| /// Return the contents as a Metal buffer in the native ``dtype``. | ||
| /// | ||
| /// > If you can guarantee the lifetime of the ``MLXArray`` will exceed the MTLBuffer and that | ||
| /// the array will not be mutated (e.g. using indexing or other means) it is possible to pass `noCopy: true` | ||
| /// to reference the backing bytes. | ||
| /// | ||
| /// ### See Also | ||
| /// - <doc:conversion> | ||
| /// - ``asArray(_:)`` | ||
| /// - ``asData(access:)`` | ||
| public func asMTLBuffer(device: any MTLDevice, noCopy: Bool = false) -> (any MTLBuffer)? { | ||
| self.eval() | ||
|
|
||
| if noCopy && self.contiguousToDimension() == 0 { | ||
| // the backing is contiguous, we can provide a wrapper | ||
| // for the contents without a copy (if requested) | ||
| let source = UnsafeMutableRawPointer(mutating: mlx_array_data_uint8(self.ctx))! | ||
| return device.makeBuffer(bytesNoCopy: source, length: self.nbytes) | ||
| } else { | ||
| let data = asDataCopy() | ||
| return data.data.withUnsafeBytes { ptr in | ||
| device.makeBuffer(bytes: ptr.baseAddress!, length: ptr.count) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
|
|
||
| import Cmlx | ||
| import Foundation | ||
| import Metal | ||
| import Numerics | ||
|
|
||
| public final class MLXArray { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of the API in here has moved to memory.h and is no longer GPU specific:
we don't need to deal with it here but perhaps this needs some refactoring -- we can forward the "GPU" methods to these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought down the road it could be nice to have
GPU.swift(cross-platform) andGPU+Metal.swift(only Apple Silicon builds) files and later aGPU+CUDA.swift(cross-platform) file ... wasn't quite yet too sure how to split GPU.swift. If you believe it's ok to add into this PR, happy to do so.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it could be done here or left until later. I was actually thinking a
Memory.swiftlike this:Those are actually functions and properties currently on
GPUbut they have moved on the python side, so I think we would want to deprecate current ones and forward to the (moved) API.I don't know if we would want a
Metal.swiftas it would collide with the framework. So yes, perhaps GPU+X and keep the GPU specific parts under theGPUtype.Anyway, I don't think this is critical and we could pick it up later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can take a look and let you know (I am just starting out to learn this code base). Plus we need new CI for sure -- sorry forgot about this for a second.