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

In-Place operators (+=, -=) for MLXArray #86

Closed
kemchenj opened this issue May 14, 2024 · 4 comments · Fixed by #91
Closed

In-Place operators (+=, -=) for MLXArray #86

kemchenj opened this issue May 14, 2024 · 4 comments · Fixed by #91
Labels
enhancement New feature or request

Comments

@kemchenj
Copy link
Contributor

kemchenj commented May 14, 2024

I have been using mlx-swift for my projects and have found it to be incredibly useful. However, I noticed that MLXArray does not support in-place operators such as += and -=.

Is there a specific reason why these operators have not been implemented? Additionally, are there any plans to add these in-place operators in the future?

These operators would be very beneficial for performance-critical applications where minimizing memory allocations and copies is crucial.

Thank you for your time and for the great work on mlx-swift!

@davidkoski
Copy link
Collaborator

Although they can be added syntactically I don't know if there is really such a thing under the hood.

@awni are there any in-place operations in mlx core?

@awni
Copy link
Member

awni commented May 14, 2024

Right from the user standpoint there are in-place operations. So if you do an in place operation then the object is unchanged:

orig_id = id(a)
a += b
new_id = id(a)

Then orig_id == new_id.

However, when it comes to memory management, that is completely opaque to the user. MLX often re-uses memory regardless of if the op is in place or not. It will reuse memory when it is safe to do so (typically if you don't retain references to arrays and the buffer sizes match).

So in the following:

def fun():
  a = mx.array([1,2,3])
  a = a + mx.array([2])
  return a

out = fun()
mx.eval(out)

Even though the op is not in-place since the reference to the original array is out of scope before the call to eval the memory buffer is likely reused

@kemchenj
Copy link
Contributor Author

Thank you for the detailed explanation. I understand that these operators may not significantly impact memory management due to MLX's efficient handling of memory reuse. However, from a syntactical perspective, having in-place operators like += and -= would still be valuable.

Additionally, aligning these APIs with the Python version, which implements iadd/isub/..., could help developers transition from Python to Swift more smoothly, reducing potential glitches.

@davidkoski davidkoski added the enhancement New feature or request label May 16, 2024
@davidkoski
Copy link
Collaborator

Agreed, these += and friends operators should be added.

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

Successfully merging a pull request may close this issue.

3 participants