Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bf1e335
Move utility method out, so it can be used by other modules.
itamarst Jun 21, 2021
4582bc8
A sketch of sorts.
itamarst Jun 30, 2021
b82dd24
A bit closer to working, maybe.
itamarst Jul 2, 2021
0cc9d7c
Even closer, maybe.
itamarst Jul 2, 2021
ada5b42
Actually works for some values.
itamarst Jul 7, 2021
372e34c
Fix off by one bug that caused segfaults for some axis.
itamarst Jul 7, 2021
d7c7985
Some documentation and comments for np.take_along_axis.
itamarst Jul 9, 2021
df2f29a
Tests for error cases of np.take_along_axis.
itamarst Jul 9, 2021
fe60ca9
Fix axis=None case.
itamarst Jul 9, 2021
d95a4bd
More test cases for np.take_along_axis().
itamarst Jul 9, 2021
0fcfb09
Tests (and corresponding bug fix) for take_along_axis() semantics.
itamarst Jul 12, 2021
dcab297
Fix flakes.
itamarst Jul 27, 2021
0dc3b76
Merge branch 'master' into np-take_along_axis
itamarst Aug 10, 2021
e759d98
Sometimes we take NumPy code from examples or other parts of the docu…
itamarst Aug 10, 2021
75ded55
Function was renamed.
itamarst Aug 10, 2021
da3b6e4
More succinct testing strategy
itamarst Aug 18, 2021
cfc28ed
Better explanation
itamarst Aug 18, 2021
7a70596
Point to stable location, in source code.
itamarst Aug 18, 2021
1ae5f53
Get rid of additional, erroneous outer loop.
itamarst Aug 18, 2021
b676024
Bounds check is up to user.
itamarst Aug 18, 2021
dbcf274
These functions will only ever be used from JITed code.
itamarst Aug 18, 2021
9ec64da
Minor optimization.
itamarst Aug 18, 2021
bd64769
Bit nicer testing.
itamarst Aug 18, 2021
7e65814
More input validation.
itamarst Aug 18, 2021
ede8d01
Flakes.
itamarst Aug 18, 2021
c86836a
A failing test.
itamarst Aug 18, 2021
f7b94d9
Get rid of combinatoric code repetition by doing a little song and da…
itamarst Aug 30, 2021
4ee6bb2
Better explanation.
itamarst Aug 30, 2021
b951a63
Merge remote-tracking branch 'origin/master' into np-take_along_axis
itamarst Aug 30, 2021
fd3c73d
Support for pre-broadcasting.
itamarst Aug 30, 2021
1d180d5
Additional tests for new broadcasting support.
itamarst Aug 30, 2021
47a32e9
Flake fixes.
itamarst Aug 30, 2021
dbedd9c
Apply suggestions from code review
itamarst Sep 13, 2021
01f9c9f
Fix flake.
itamarst Sep 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Actually works for some values.
  • Loading branch information
itamarst committed Jul 7, 2021
commit ada5b4204be812045ccfe93928bf4e86cbc49e0b
69 changes: 31 additions & 38 deletions numba/np/arrayobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -5381,58 +5381,53 @@ def impl(arr, axis1, axis2):


@register_jitable
Comment thread
itamarst marked this conversation as resolved.
Resolved
def copy_to_tuple(listlike, tuple_buffer, offset=0):
if len(tuple_buffer) == 0:
return tuple_buffer
for i in range(len(listlike)):
tuple_buffer = tuple_setitem(tuple_buffer, i+offset, listlike[i+offset])
return tuple_buffer
def _take_along_axis_impl_set_ni(arr, indices, axis, Ni, Nk):
for i in range(len(Ni)):
Ni = tuple_setitem(Ni, i, arr.shape[i])
return _take_along_axis_impl(arr, indices, axis, Ni, Nk)


@register_jitable
def _take_along_axis_impl_set_ni(arr, indices, axis, Ni, Nk, lookup):
Ni = copy_to_tuple(list(arr.shape)[:axis], Ni)
return _take_along_axis_impl(arr, indices, axis, Ni, Ni, lookup)
def _take_along_axis_impl_set_nk(arr, indices, axis, Ni, Nk):
for i in range(len(Nk)):
Nk = tuple_setitem(Nk, i, arr.shape[axis + i])
return _take_along_axis_impl(arr, indices, axis, Ni, Nk)


@register_jitable
def _take_along_axis_impl_set_nk(arr, indices, axis, Ni, Nk, lookup):
Nk = copy_to_tuple(list(arr.shape)[axis+1:], Nk)
return _take_along_axis_impl(arr, indices, axis, Ni, Ni, lookup)
def _take_along_axis_impl_set_ni_nk(arr, indices, axis, Ni, Nk):
for i in range(len(Ni)):
Ni = tuple_setitem(Ni, i, arr.shape[i])
for i in range(len(Nk)):
Nk = tuple_setitem(Nk, i, arr.shape[axis + i])
return _take_along_axis_impl(arr, indices, axis, Ni, Nk)


@register_jitable
def _take_along_axis_impl_set_ni_nk(arr, indices, axis, Ni, Nk, lookup):
Ni = copy_to_tuple(list(arr.shape)[:axis], Ni)
Nk = copy_to_tuple(list(arr.shape)[axis+1:], Nk)
return _take_along_axis_impl(arr, indices, axis, Ni, Ni, lookup)


@register_jitable
def _take_along_axis_impl(arr, indices, axis, Ni, Nk, lookup):
def _take_along_axis_impl(arr, indices, axis, Ni, Nk):
M = arr.shape[axis]
J = indices.shape[axis] # Need not equal M
out = np.empty(tuple_setitem(arr.shape, axis, J))
out = np.empty(Ni + (J,) + Nk, arr.dtype)

for ii in np.ndindex(Ni):
lookup = copy_to_tuple(ii, lookup)
for kk in np.ndindex(Nk):
lookup = copy_to_tuple(kk, lookup, axis+1)
a_1d = arr[lookup]
indices_1d = indices[lookup]
out_1d = out[lookup]
out_1d[:] = a_1d[indices_1d]
for ii in np.ndindex(Ni):
for kk in np.ndindex(Nk):
np_s_ = (slice(None, None, None),)
a_1d = arr [ii + np_s_ + kk]
indices_1d = indices[ii + np_s_ + kk]
out_1d = out [ii + np_s_ + kk]
for j in range(J):
out_1d[j] = a_1d[indices_1d[j]]
return out


@overload(np.take_along_axis)
@overload(np.take_along_axis, jit_options={'boundscheck':True})
Comment thread
itamarst marked this conversation as resolved.
Resolved
def arr_take_along_axis(arr, indices, axis):
Comment thread
itamarst marked this conversation as resolved.
Resolved
if is_nonelike(axis):
def take_along_axis_impl(arr, indices, axis):
arr = arr.flat
axis = 0
return _take_along_axis_impl(arr.flat, indices, 0,
(), (), (slice(None, None, None),))
return _take_along_axis_impl(arr.flat, indices, 0, (), ())
else:
check_is_integer(axis, "axis")
axis = axis.literal_value
Expand All @@ -5442,24 +5437,22 @@ def take_along_axis_impl(arr, indices, axis):
if axis < 0 or axis >= arr.ndim:
raise ValueError("axis is out of bounds")

n = arr.ndim
Ni = tuple(range(axis))
Nk = tuple(range(axis+1, arr.ndim))
lookup = [0] * arr.ndim
lookup[axis] = slice(None, None, None)
lookup = tuple(lookup)

if Ni:
if Nk:
def take_along_axis_impl(arr, indices, axis):
return _take_along_axis_impl_set_ni_ki(arr, indices, axis, Ni, Nk, lookup)
return _take_along_axis_impl_set_ni_nk(arr, indices, axis, Ni, Nk)
else:
def take_along_axis_impl(arr, indices, axis):
return _take_along_axis_impl_set_ni(arr, indices, axis, Ni, Nk, lookup)
return _take_along_axis_impl_set_ni(arr, indices, axis, Ni, Nk)
else:
if Nk:
def take_along_axis_impl(arr, indices, axis):
return _take_along_axis_impl_set_ki(arr, indices, axis, Ni, Nk, lookup)
return _take_along_axis_impl_set_nk(arr, indices, axis, Ni, Nk)
else:
def take_along_axis_impl(arr, indices, axis):
return _take_along_axis_impl(arr, indices, axis, Ni, Nk, lookup)
return _take_along_axis_impl(arr, indices, axis, Ni, Nk)
return take_along_axis_impl
Comment thread
itamarst marked this conversation as resolved.
Resolved