Add support for matrix transpose - #4402
Conversation
|
Thanks for your contribution! Also, in LLM workloads, a typical permutation is something like: |
|
Hi, the main goal of most of my PR's is to make MLX Array API compliant, https://data-apis.org/array-api/latest/index.html just like numpy and jax so the code can be made backend agnostic downstream :) |
Exactly! Which is precisely the reason NumPy arrays have two attributes: The interpretation of
Absolutely! One typical use case is linear algebra with batched matrices. For example, a normal equation solution for the linear regression problem is just (modulo inv vs solve, of course) Note how this formula "just works" for solving multiple regressions at once, for a batched design matrix and/or batched Most Array API compatible array libraries implement the Finally, while the Array API spec itself requires that the "usual" All in all, and as far as experience with Array API is any guide, it is best that an array library supports both |
|
Regarding being framework agnostic, we don't want to mirror every API or behaviour from other frameworks that will unnecessarily increase the API and codebase, but rather focus on what makes sense for MLX and its use cases. Regarding That being said, I'm not opposed to adding it. My main concern is that I don't think “being framework-agnostic” is a strong enough reason for introducing the API. I think the rationale should instead be that May I ask what do you think, @zcbenz ? |
|
Re framework agnosticism I should probably explain our motivation a bit better. The short story is that Array API defines a minimum common API surface for array libraries. The primary use case is a higher-level library (scipy, scikit-learn, ...) using an Array API producer (pytorch, jax, cupy, dpnp, hopefully MLX) instead of just numpy. The way it works is that the internals of the consumer library are implemented against the Array API---and then users can just switch the array library: they feed MLX arrays to SciPy functions, all array processing is done by MLX, and users get MLX arrays back. Back to Of course, the full compatibility is not achievable, and is not even a goal. We fully understand that MLX focuses on what makes sense for MLX! Consumer libraries will work around fundamental design constraints (complex128, data-dependent shapes and so on). Small differences however ( The wall of text above is just hopefully useful as a high-level motivation of the stream of Array API PRs, and hopefully provides a use case for MLX maintainers to consider. EDIT: Here's the canonical "purpose and scope" text for the Array API movement, https://data-apis.org/array-api/latest/purpose_and_scope.html |
|
@nastya236 I'm generally open to conforming to Array API spec when it adds no maintenance burden, for example adding an So I think I'm not fond of the API itself, but if it is blocking scipy/scikit-learn from adopting MLX as a new backend, we should accept it. On the PR itself, since this is not a trivial one-liner, and there is also a functional version |
Just to add that in SciPy there is no dedicated issue for MLX specifically. There's however a body of "lore", spread over multiple issues and PR reviews over the last few years we've been working on the Array API support, where it's clear that there's significant interest for supporting "metal backends". |
Added matrix transpose
.mTproperty for the Array API compliance. Part of MLX compatibility: Constants, Datatypes and Array Attributes data-apis/array-api-compat#451None