Skip to content

feat: MLXArray initializers for nested Swift arrays (#161)#392

Open
VDurocher wants to merge 2 commits intoml-explore:mainfrom
VDurocher:feat/nested-array-init
Open

feat: MLXArray initializers for nested Swift arrays (#161)#392
VDurocher wants to merge 2 commits intoml-explore:mainfrom
VDurocher:feat/nested-array-init

Conversation

@VDurocher
Copy link
Copy Markdown

Closes #161

What

Adds MLXArray initializers that accept nested Swift arrays of any depth, matching the ergonomics of the Python mx.array([[1, 2], [3, 4]]) API.

How

  • Introduces MLXNestedArray protocol with mlxShape and mlxFlattenedValues() requirements
  • Array conforms when its Element also conforms (recursive) — supports any depth
  • Scalar types (Float32, Float64, Int32, Int64, UInt8, UInt16, UInt32, Bool, Float16) conform as leaf types
  • Dedicated MLXArray.init(_ rows: [[T]]) (2D) and MLXArray.init(_ slices: [[[T]]]) (3D) overloads for clean type inference at call-site
  • Separate [[Int]] and [[[Int]]] overloads that mirror the existing [Int] behaviour (converts to Int32, with bounds-checking precondition)
  • Shape validation via precondition — jagged arrays fail fast with a descriptive message

Examples

// 2D — shape: [2, 3], dtype: .float32
let matrix = MLXArray([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])

// 2D — shape: [2, 2], dtype: .int32  (mirrors existing [Int] → Int32 convention)
let ints = MLXArray([[7, 8], [9, 10]])

// 3D — shape: [2, 2, 2], dtype: .int32
let cube = MLXArray([[[1, 0], [0, 1]], [[2, 0], [0, 2]]])

// 3D — shape: [2, 3, 4], arbitrary element type
let tensor = MLXArray([[[Float16]]]())

Files changed

File Description
Source/MLX/MLXArray+NestedInit.swift New — protocol + initialiseurs
Tests/MLXTests/MLXArray+NestedInitTests.swift New — 16 test cases couvrant 2D, 3D, types scalaires, tableaux vides, indexation

Notes

  • No macro magic — pure Swift generics
  • No change to any existing initializer or public API
  • The MLXNestedArray generic init is 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 the Float64: MLXNestedArray conformance (dtype .float64) — consistent with the existing init<T: HasDType>(_ value: [T]) behaviour for Double

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

Support MLXArray initialization from nested Swift Arrays

1 participant