Skip to content
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

Support MLXArray initialization from nested Swift Arrays #161

Open
awni opened this issue Nov 11, 2024 · 6 comments
Open

Support MLXArray initialization from nested Swift Arrays #161

awni opened this issue Nov 11, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@awni
Copy link
Member

awni commented Nov 11, 2024

This is mostly a convenience but it can be quite handy and we support it in Python:

let a = MLXArray([[7, 8], [9, 10]])
@awni awni added the enhancement New feature or request label Nov 11, 2024
@davidkoski
Copy link
Collaborator

I need to play around with that. We can certainly do it for a fixed number of dimensions (we need an initializer for each number of dimensions). Maybe we can do something fancy with generics and gt an arbitrary number, but I think the types will be tricky.

@JimWallace
Copy link

MLXArray to nested Swift arrays would also be handy (I find that I'm re-writing this code a lot).

@davidkoski
Copy link
Collaborator

MLXArray to nested Swift arrays would also be handy (I find that I'm re-writing this code a lot).

How do you deal with the return type? asArray2() -> [[T]] and asArray3() -> [[[T]]]?

@JimWallace
Copy link

I’ve just been working with 2D matrices, and flipping between MLXArray and [[Float]], so pretty pedestrian but probably one of the most common use cases. Maybe I missed asArray2, is that already a thing?

@davidkoski
Copy link
Collaborator

No, not a thing -- just pondering how the types would work.

@JimWallace
Copy link

Oh I see! Yes, I’d imagine what you have would be useful for T: HasDType in very much the same way asArray works now, but maintaining the shape in Swift’s array.

I don’t know how to gracefully handle cases where shape doesn’t match, but I think it would be fair to throw an error much like the other precondition checks in MLX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants