mlx icon indicating copy to clipboard operation
mlx copied to clipboard

support python mlx.array creation from list of mlx.array's

Open Alex-X-W opened this issue 1 year ago • 6 comments

Proposed changes

Adds support for python mlx.array create from list of mlx.array's. Trying to tackle issue 287.

Checklist

Put an x in the boxes that apply.

  • [x] I have read the CONTRIBUTING document
  • [x] I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • [x] I have added tests that prove my fix is effective or that my feature works
  • [ ] I have updated the necessary documentation (if needed)

Alex-X-W avatar Dec 31 '23 03:12 Alex-X-W

Thanks for adding this. I was thinking we should use concatenate and have this be part of the compute graph rather than force an eval.

Concretely when you do mx.array([a1, ...]), it's basically using concatenate under the hood. Does that make sense?

awni avatar Dec 31 '23 05:12 awni

@awni Thanks. I see, yeah that makes sense. I am gonna renovate the code a bit.

Alex-X-W avatar Dec 31 '23 05:12 Alex-X-W

Hi @awni ! May I bug you to review again? Thanks! And... Happy New Year 🥳

Alex-X-W avatar Jan 01 '24 08:01 Alex-X-W

Hi @Alex-X-W ,

There's two things I think we need to fix with this:

  1. The main point to using stack is to avoid needing to eval the input arrays. I see that you eval them then get the raw pointer based on the type and copy it. That should change to be much simpler. Just put the arrays in a vector, the stack will handle type promotion.
  2. If you are creating an array from a big list (or list of lists) of python scalars it will be really inefficient to convert these to arrays and do a stack. I would change the code so that when there is no nested array it follows a different path and puts the values in vector of the appropriate type and then uses the array constructor.

awni avatar Jan 02 '24 18:01 awni

Thank you @awni for the pointers! They all make sense to me. Please help take another look, thanks!

Alex-X-W avatar Jan 04 '24 06:01 Alex-X-W

PS checked cpu/gpu tests pass + formatting passes

awni avatar Jan 04 '24 15:01 awni