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

[Feature request] pytorch like operations with arrays #377

Open
anfedoro opened this issue Jan 5, 2024 · 3 comments
Open

[Feature request] pytorch like operations with arrays #377

anfedoro opened this issue Jan 5, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@anfedoro
Copy link

anfedoro commented Jan 5, 2024

Nice to have some pytorch-like operations very useful with tensor (arrays) manipulations.
e.g. tensor.unique().. which returns an array of unique numbers with or without counting them.
or - tensor.unfold()... much more simple and straightforward way to apply rolling window to an array than np.lib.stride_tricks.as_strided()

Also mx.where() function is missing important functionality when it only receives the condition as an argument and returns list of array indices. Current implementation always requires three arguments.
Both numpy and pytorch has such capability.

I only mean that have more compatible functions may simplify existing torch code migration at least for testing and verifications.

@anfedoro anfedoro changed the title [Feature request] pytorch like operations [Feature request] pytorch like operations with arrays Jan 5, 2024
@awni
Copy link
Member

awni commented Jan 5, 2024

Cool, we follow the NumPy API so the where and unique are definitely on the table for feature enhancements. They are both tricky to expose in MLX since they produce outputs that have shapes based on the input data which a big reason we don't have them yet.

Unfold is something we could discuss if it's especially useful. We aren't opposed to including more than pure NumPy but usually want a really good reason to do so. (E.g. on going use cases that would really benefit from having it).

@awni awni added the enhancement New feature or request label Jan 5, 2024
@anfedoro
Copy link
Author

anfedoro commented Jan 5, 2024

well.. from my view unford is mode efficient than np.lib.stride_tricks.as_strided() in two reasons:

  • in my understanding, it does not create a separate array and thus doesn't consume additional memory, instead, it just changes the tensor data representation
  • it is much more straightforward from a function call perspective... just three clear arguments.. look here https://pytorch.org/docs/stable/generated/torch.Tensor.unfold.html#torch.Tensor.unfold.. of course, this may be only a matter of habit... as, frankly, I do use torch much more than numpy :-)

technically nothing prevent us to make general manipulations using numpy or torch.. while mls use for modeling and training.

@mzbac
Copy link
Contributor

mzbac commented Jan 16, 2024

I am facing the same issue when I try to implement the Moe block for Mixtral model. My understanding is that mx.where doesn't support using only condition blocks for vectorized computation in the selected experts(so we have to explicit eval inds and using np.where instead).

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