feat: MLXArray initializers for nested Swift arrays (#161)#392
Open
VDurocher wants to merge 2 commits into
Open
feat: MLXArray initializers for nested Swift arrays (#161)#392VDurocher wants to merge 2 commits into
VDurocher wants to merge 2 commits into
Conversation
Introduces MLXNestedArray protocol and MLXArray.init(_ nested:) to support ergonomic construction of multi-dimensional arrays from nested Swift array literals, mirroring the Python mx.array() API.
davidkoski
reviewed
May 15, 2026
Comment on lines
+38
to
+40
| /// Calcule la forme en ajoutant la dimension actuelle devant la shape de l'élément. | ||
| /// | ||
| /// Précondition : tous les éléments ont la même shape (tableau rectangulaire). |
Collaborator
There was a problem hiding this comment.
Please translate comment to English
Collaborator
|
I like this approach! |
Collaborator
|
I am not sure why I can't trigger CI to run, but this does fail swift-format -- please run: |
davidkoski
reviewed
May 15, 2026
| /// ### See Also | ||
| /// - <doc:initialization> | ||
| /// - ``init(_:)-([[MLXNestedArray]])`` | ||
| public convenience init<T: HasDType>(_ rows: [[T]]) { |
Collaborator
There was a problem hiding this comment.
Do you need these overrides? The tests do not build for me, seeming because of them:
/Users/dkoski/Developer/mlx-swift/Tests/MLXTests/MLXArray+NestedInitTests.swift:19:22: error: ambiguous use of 'init(_:)'
let matrix = MLXArray([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] as [[Float32]])
^
MLX.MLXArray.init:2:20: note: found this candidate in module 'MLX'
public convenience init<N>(_ nested: N) where N : MLX.MLXNestedArray, N : Collection, N.Element : MLX.MLXNestedArray}
^
MLX.MLXArray.init:2:20: note: found this candidate in module 'MLX'
public convenience init<T>(_ rows: [[T]]) where T : MLX.HasDType}If I comment all of these out, it does build but some tests fail.
davidkoski
reviewed
May 15, 2026
|
|
||
| func testInit2DFloat() { | ||
| // Tableau 2D de Float32 | ||
| let matrix = MLXArray([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] as [[Float32]]) |
Collaborator
There was a problem hiding this comment.
Ideally we could have something like the MLXArray(converting:) that would handle this case.
davidkoski
reviewed
May 15, 2026
|
|
||
| func testInit2DInt() { | ||
| // Tableau 2D d'Int (surcharge dédiée, produit du .int32) | ||
| let matrix = MLXArray([[7, 8], [9, 10]]) |
Collaborator
There was a problem hiding this comment.
Curiously this produces:
(lldb) p nested
([[Double]]) 2 values {
[0] = 2 values {
[0] = 7
[1] = 8
}
[1] = 2 values {
[0] = 9
[1] = 10
}
}
I think this is because the change is missing:
extension Int: MLXNestedArray {
public typealias ScalarType = Int
public var mlxShape: [Int] { [] }
public func mlxFlattenedValues() -> [Int] { [self] }
}so the next closest match is as a Double literal. (Int is not the same as Int64)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #161
What
Adds
MLXArrayinitializers that accept nested Swift arrays of any depth, matching the ergonomics of the Pythonmx.array([[1, 2], [3, 4]])API.How
MLXNestedArrayprotocol withmlxShapeandmlxFlattenedValues()requirementsArrayconforms when itsElementalso conforms (recursive) — supports any depthFloat32,Float64,Int32,Int64,UInt8,UInt16,UInt32,Bool,Float16) conform as leaf typesMLXArray.init(_ rows: [[T]])(2D) andMLXArray.init(_ slices: [[[T]]])(3D) overloads for clean type inference at call-site[[Int]]and[[[Int]]]overloads that mirror the existing[Int]behaviour (converts toInt32, with bounds-checking precondition)precondition— jagged arrays fail fast with a descriptive messageExamples
Files changed
Source/MLX/MLXArray+NestedInit.swiftTests/MLXTests/MLXArray+NestedInitTests.swiftNotes
MLXNestedArraygenericinitis available as an escape hatch for deeper nesting (4D+), but the 2D/3D typed overloads are preferred for day-to-day use because Swift's type inference works better with concrete overloads[[Double]]works through theFloat64: MLXNestedArrayconformance (dtype.float64) — consistent with the existinginit<T: HasDType>(_ value: [T])behaviour forDouble