-
Notifications
You must be signed in to change notification settings - Fork 249
MaterializedArray is a Sendable MLXArray #418
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
Open
davidkoski
wants to merge
6
commits into
main
Choose a base branch
from
materialized-array
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6c48e53
MaterializedArray is a Sendable MLXArray
davidkoski 18106c8
add documentation
davidkoski ba24c6d
fix build issue
davidkoski 0ceeada
no cross module links
davidkoski 500ef2b
base needs to be public so other packages can extend
davidkoski a3fde72
relax the throw
davidkoski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ import Cmlx | |
| import Foundation | ||
| import Numerics | ||
|
|
||
| public final class MLXArray { | ||
| public class MLXArray: ExpressibleByArrayLiteral { | ||
|
Collaborator
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.
|
||
|
|
||
| /// Internal pointer to the mlx-c wrapper on `mlx::core::array`, used with `Cmlx` interop. | ||
| public internal(set) var ctx: mlx_array | ||
|
|
@@ -19,6 +19,37 @@ public final class MLXArray { | |
| self.ctx = ctx | ||
| } | ||
|
|
||
| // Note: MLXArray does not implement ExpressibleByFloatLiteral etc. because | ||
| // we want to create arrays in the context of the other arrays. For example: | ||
| // | ||
| // let x = MLXArray(1.5, dtype: .float16) | ||
| // let r = x + 2.5 | ||
| // | ||
| // We expect r to have a dtype of float16. See ``ScalarOrArray``. | ||
|
|
||
| /// Initializer allowing creation of 1d `MLXArray` from an array literal. | ||
| /// | ||
| /// ```swift | ||
| /// let a: MLXArray = [1, 2, 3] | ||
| /// ``` | ||
| /// | ||
| /// This is convenient for methods that have `MLXArray` parameters: | ||
| /// | ||
| /// ```swift | ||
| /// print(array.take([1, 2, 3], axis: 0)) | ||
| /// ``` | ||
| /// | ||
| /// ### See Also | ||
| /// - <doc:initialization> | ||
| required public convenience init(arrayLiteral elements: Int32...) { | ||
| let ctx = elements.withUnsafeBufferPointer { ptr in | ||
| let shape = [Int32(elements.count)] | ||
| return mlx_array_new_data( | ||
| ptr.baseAddress!, shape, Int32(shape.count), Int32.dtype.cmlxDtype) | ||
| } | ||
| self.init(ctx) | ||
| } | ||
|
|
||
| /// return the equivalent of a `.none` MLXArray (for the C API). | ||
| /// | ||
| /// Not called `.none` to avoid ambiguity with `Optional`. This can be used | ||
|
|
@@ -37,7 +68,7 @@ public final class MLXArray { | |
| } | ||
|
|
||
| /// Number of bytes per element | ||
| public var itemSize: Int { mlx_array_itemsize(ctx) } | ||
| final public var itemSize: Int { mlx_array_itemsize(ctx) } | ||
|
|
||
| /// Total number of elements in the array | ||
| /// | ||
|
|
@@ -46,7 +77,7 @@ public final class MLXArray { | |
| /// print(array.size) | ||
| /// // 12 | ||
| /// ``` | ||
| public var size: Int { mlx_array_size(ctx) } | ||
| final public var size: Int { mlx_array_size(ctx) } | ||
|
|
||
| /// Number of elements in the 0th dimension. | ||
| /// | ||
|
|
@@ -62,10 +93,10 @@ public final class MLXArray { | |
| /// ... | ||
| /// } | ||
| /// ``` | ||
| public var count: Int { dim(0) } | ||
| final public var count: Int { dim(0) } | ||
|
|
||
| /// Number of bytes in the array. | ||
| public var nbytes: Int { mlx_array_nbytes(ctx) } | ||
| final public var nbytes: Int { mlx_array_nbytes(ctx) } | ||
|
|
||
| /// Number of dimensions in the array. | ||
| /// | ||
|
|
@@ -74,7 +105,7 @@ public final class MLXArray { | |
| /// print(array.ndim) | ||
| /// // 2 | ||
| /// ``` | ||
| public var ndim: Int { mlx_array_ndim(ctx) } | ||
| final public var ndim: Int { mlx_array_ndim(ctx) } | ||
|
|
||
| /// Data type of the elements in the array. | ||
| /// | ||
|
|
@@ -83,7 +114,7 @@ public final class MLXArray { | |
| /// print(array.dtype) | ||
| /// // .int64 (aka Int.dtype) | ||
| /// ``` | ||
| public var dtype: DType { DType(mlx_array_dtype(ctx)) } | ||
| final public var dtype: DType { DType(mlx_array_dtype(ctx)) } | ||
|
|
||
| /// Dimensions of the array. | ||
| /// | ||
|
|
@@ -92,7 +123,7 @@ public final class MLXArray { | |
| /// print(array.shape) | ||
| /// // [3, 4] | ||
| /// ``` | ||
| public var shape: [Int] { | ||
| final public var shape: [Int] { | ||
| let ndim = mlx_array_ndim(ctx) | ||
| guard ndim > 0 else { return [] } | ||
| let cShape = mlx_array_shape(ctx)! | ||
|
|
@@ -104,7 +135,7 @@ public final class MLXArray { | |
| /// ```swift | ||
| /// let (w, h) = array.shape2 | ||
| /// ``` | ||
| public var shape2: (Int, Int) { | ||
| final public var shape2: (Int, Int) { | ||
| let ndim = mlx_array_ndim(ctx) | ||
| precondition(ndim == 2) | ||
| let cShape = mlx_array_shape(ctx)! | ||
|
|
@@ -116,7 +147,7 @@ public final class MLXArray { | |
| /// ```swift | ||
| /// let (w, h, c) = array.shape3 | ||
| /// ``` | ||
| public var shape3: (Int, Int, Int) { | ||
| final public var shape3: (Int, Int, Int) { | ||
| let ndim = mlx_array_ndim(ctx) | ||
| precondition(ndim == 3) | ||
| let cShape = mlx_array_shape(ctx)! | ||
|
|
@@ -128,7 +159,7 @@ public final class MLXArray { | |
| /// ```swift | ||
| /// let (b, w, h, c) = array.shape4 | ||
| /// ``` | ||
| public var shape4: (Int, Int, Int, Int) { | ||
| final public var shape4: (Int, Int, Int, Int) { | ||
| let ndim = mlx_array_ndim(ctx) | ||
| precondition(ndim == 4) | ||
| let cShape = mlx_array_shape(ctx)! | ||
|
|
@@ -149,7 +180,7 @@ public final class MLXArray { | |
| /// Strides of the array backing. | ||
| /// | ||
| /// Note: this is only stable once the array is evaluated. | ||
| var internalStrides: [Int] { | ||
| final var internalStrides: [Int] { | ||
| let ndim = mlx_array_ndim(ctx) | ||
| guard ndim > 0 else { return [] } | ||
| let strides = mlx_array_strides(ctx)! | ||
|
|
@@ -167,7 +198,7 @@ public final class MLXArray { | |
| /// // 4.5 | ||
| /// let value: Float = array[1].item() | ||
| /// ``` | ||
| public func item<T: HasDType>() -> T { | ||
| final public func item<T: HasDType>() -> T { | ||
| item(T.self) | ||
| } | ||
|
|
||
|
|
@@ -328,7 +359,7 @@ public final class MLXArray { | |
| /// // 4.5 | ||
| /// let value = array[1].item(Float.self) | ||
| /// ``` | ||
| public func item<T: HasDType>(_ type: T.Type) -> T { | ||
| final public func item<T: HasDType>(_ type: T.Type) -> T { | ||
| precondition(self.size == 1) | ||
| eval() | ||
|
|
||
|
|
@@ -466,7 +497,7 @@ public final class MLXArray { | |
| /// print(array.dim(1)) | ||
| /// // 4 | ||
| /// ``` | ||
| public func dim(_ dim: Int) -> Int { | ||
| final public func dim(_ dim: Int) -> Int { | ||
| Int(mlx_array_dim(ctx, MLX.resolve(axis: dim, ndim: mlx_array_ndim(ctx)).int32)) | ||
| } | ||
|
|
||
|
|
@@ -481,7 +512,7 @@ public final class MLXArray { | |
| /// print(array.dim(index)) | ||
| /// // 4 | ||
| /// ``` | ||
| func dim(_ dim: Int32) -> Int32 { | ||
| final func dim(_ dim: Int32) -> Int32 { | ||
| mlx_array_dim(ctx, MLX.resolve(axis: Int(dim), ndim: mlx_array_ndim(ctx)).int32) | ||
| } | ||
|
|
||
|
|
@@ -492,7 +523,7 @@ public final class MLXArray { | |
| /// | ||
| /// ### See Also | ||
| /// - <doc:conversion> | ||
| public func asType(_ type: DType, stream: StreamOrDevice = .default) -> MLXArray { | ||
| final public func asType(_ type: DType, stream: StreamOrDevice = .default) -> MLXArray { | ||
| guard type != self.dtype else { return self } | ||
| var result = mlx_array_new() | ||
| mlx_astype(&result, ctx, type.cmlxDtype, stream.ctx) | ||
|
|
@@ -506,8 +537,9 @@ public final class MLXArray { | |
| /// | ||
| /// ### See Also | ||
| /// - <doc:conversion> | ||
| public func asType(_ type: (some HasDType).Type, stream: StreamOrDevice = .default) -> MLXArray | ||
| { | ||
| final public func asType( | ||
| _ type: (some HasDType).Type, stream: StreamOrDevice = .default | ||
| ) -> MLXArray { | ||
| asType(type.dtype, stream: stream) | ||
| } | ||
|
|
||
|
|
@@ -524,7 +556,7 @@ public final class MLXArray { | |
| /// - ``realPart(stream:)`` | ||
| /// - ``imaginaryPart(stream:)`` | ||
| /// - <doc:conversion> | ||
| public func asImaginary(stream: StreamOrDevice = .default) -> MLXArray { | ||
| final public func asImaginary(stream: StreamOrDevice = .default) -> MLXArray { | ||
| precondition(!dtype.isComplex) | ||
| let i = MLXArray(real: 0, imaginary: 1) | ||
| return self * i | ||
|
|
@@ -534,7 +566,7 @@ public final class MLXArray { | |
| /// | ||
| /// ### See Also | ||
| /// - <doc:conversion> | ||
| public func realPart(stream: StreamOrDevice = .default) -> MLXArray { | ||
| final public func realPart(stream: StreamOrDevice = .default) -> MLXArray { | ||
| precondition(dtype.isComplex) | ||
| return asType(Float.self) | ||
| } | ||
|
|
@@ -543,7 +575,7 @@ public final class MLXArray { | |
| /// | ||
| /// ### See Also | ||
| /// - <doc:conversion> | ||
| public func imaginaryPart(stream: StreamOrDevice = .default) -> MLXArray { | ||
| final public func imaginaryPart(stream: StreamOrDevice = .default) -> MLXArray { | ||
| precondition(dtype.isComplex) | ||
| let i = MLXArray(real: 0, imaginary: 1) | ||
| return (self / i).asType(.float32) | ||
|
|
@@ -559,6 +591,23 @@ public final class MLXArray { | |
| } | ||
| } | ||
|
|
||
| /// Force evaluation and return a ``MaterializedArray`` snapshot of `self`. | ||
| /// | ||
| /// The returned array is fully evaluated, immutable, and `Sendable` — see | ||
| /// ``MaterializedArray`` for the guarantees and trade-offs. Use this when | ||
| /// you need to hand an array across task or actor boundaries, or when you | ||
| /// need a stable reference that cannot be mutated by other code holding | ||
| /// the original `MLXArray`. | ||
| /// | ||
| /// This is a thin convenience over ``materialize(_:)->MaterializedArray``. | ||
| /// | ||
| /// ### See Also | ||
| /// - ``MaterializedArray`` | ||
| /// - ``materialize(_:)->MaterializedArray`` | ||
| public func materialized() -> MaterializedArray { | ||
| MLX.materialize(self) | ||
| } | ||
|
|
||
| /// Replace the contents with a reference to a new array (INTERNAL). | ||
| /// | ||
| /// Note: this is an implementation detail and only visible because of the need to call it from | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Time to turn on the stricter concurrency checks.