Proposal: add APIs for getting and setting elements via a list of indices (i.e., `take`, `put`, etc)
See original GitHub issueProposal
Add APIs for getting and setting elements via a list of indices.
Motivation
Currently, the array API specification does not provide a direct means of extracting and setting a list of elements along an axis. Such operations are relatively common in NumPy usage either via “fancy indexing” or via explicit take
and put
APIs.
Two main arguments come to mind for supporting at least basic take
and put
APIs:
-
Indexing does not currently support providing a list of indices to index into an array. The principal reason for not supporting fancy indexing stems from dynamic shapes and compatibility with accelerator libraries. However, use of fancy indexing is relatively common in NumPy and similar libraries where dynamically extracting rows/cols/values is possible and can be readily implemented.
-
Currently, the output of a subset of APIs currently included in the standard cannot be readily consumed without manual workarounds if a specification-conforming library implemented only the APIs in the standard. For example,
Background
The following table summarizes library implementations of such APIs:
op | NumPy | CuPy | Dask | MXNet | Torch | TensorFlow |
---|---|---|---|---|---|---|
extracting elements along axis | take |
take |
take |
take |
take /gather |
gather /numpy.take |
setting elements along axis | put |
put |
– | – | scatter |
scatter_nd /tensor_scatter_nd_update |
extracting elements over matching 1d slices | take_along_axis |
take_along_axis |
– | – | – | gather_nd /numpy.take_alongaxis |
setting elements over matching 1d slices | put_along_axis |
– | – | – | – | – |
While most libraries implement some form of take
, fewer implement other complementary APIs.
Issue Analytics
- State:
- Created 2 years ago
- Comments:22 (20 by maintainers)
Top GitHub Comments
Support for
take
has been merged, see gh-416.Thanks for the ping on this issue @arogozhnikov - and nice to see the experimental work on indexing in
einops
. I’d like to see gh-416 finished and merged in the coming days to indeed addtake
support with 1-D indices.