Interoperability between array references of different libraries #1577
Replies: 7 comments
-
|
i would be happy to discuss what a shared interop api could look like if there's interest in having that discussion. @Andlon would probably also be interested |
Beta Was this translation helpful? Give feedback.
-
|
Definitely count me in! |
Beta Was this translation helpful? Give feedback.
-
|
I agree, good initiative! One idea is to have something similar to AsRef/AsMut in std. For example to have AsArrayRef<T>/AsArrayMut<T> with methods for various things like data pointer and shape. Having a common memory representation for Deref would of course be very good, but could also be difficult to achieve. |
Beta Was this translation helpful? Give feedback.
-
|
Glad that you are interested! Perhaps we can start with an overview of the array storage schemes supported/used by the different libraries. I’ll start with mdarray (@fre-hu please correct me if necessary), and with a few questions regarding the other libraries. I know that there exist even more libraries, but finding some kind of consensus will be already challenging with these four. Still, please feel free to invite/join. mdarray
ndarrayLooking at
faer
nalgebra(@sebcrozet, perhaps you are interested in this discussion as well?)
|
Beta Was this translation helpful? Give feedback.
-
|
@grothesque you've got it mostly right for Also, I agree with @fre-hu that a good first goal would be interop rather than common memory format. As far as I see it, there are essentially 3 "attributes" for interop that we'd want to capture:
All told, that would make for 8 traits. Not terrible, but if we wanted to skip the shape/stride mutability then we could reduce it to 4. |
Beta Was this translation helpful? Give feedback.
-
|
I would be interested in this. My use case is that I maintain an ML inference runtime which internally uses its own tensor library but most users will be more used to working with ndarray. It would be useful to have a way to accept an array from ndarray or other libraries without needing a direct dependency on each library. Additionally it would be useful to have a way to convert owned arrays containing inference results back into the source array library type. The representation I use internally for layouts is one of two types, abstracted by a Layout trait: struct DynLayout {
shape_and_strides: SmallVec<[usize; 8]>,
}
struct NdLayout<const N: usize> {
shape: [usize; N],
strides: [usize; N],
}The backing storage I use is |
Beta Was this translation helpful? Give feedback.
-
|
Happy to continue this discussion, but going to convert it to a "Discussion". Just cleaning up the Issues! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The recent addition of
ArrayRefto ndarray explicitly unifies the in-memory representation of arrays in ndarray to be something like a tuple of (shape, strides, data ptr). This allows implementingDereffrom all array types to the reference type. This in turn allows functions to accept array parameters as nothing more than a pointer to such a tuple, no matter whether the actual type is an owned array, a view, or something else. This avoids a lot of needlessly generic code and greatly simplifies APIs, just like the built-in slices of Rust do.The next step of this harmonization would be realizing that to a large extent the different multi-dimensional array libraries for Rust work with a very similar model of what is an array. Perhaps a standard memory representation (i.e. order and data type for shape and strides) of array reference could be established that is shared across multiple crates?
Anyway, without looking too much into the technical details at this moment, I would like to bring up the question whether there is interest in some degree of harmonization. I believe that this has the potential to greatly benefit the Rust numerical array “ecosystem”. If the array references became compatible, libraries could add features that enable
Derefimplementations from the array ref of an external library to the native one.@sarah-quinones, @fre-hu, @akern40, what do you think?
Beta Was this translation helpful? Give feedback.
All reactions