Skip to content

Fix gh-1293 for sum over zero-size array with keepdims set #1294

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

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions dpctl/tensor/_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ def sum(arr, axis=None, dtype=None, keepdims=False):

res_usm_type = arr.usm_type
if arr.size == 0:
if keepdims:
res_shape = res_shape + (1,) * red_nd
inv_perm = sorted(range(nd), key=lambda d: perm[d])
res_shape = tuple(res_shape[i] for i in inv_perm)
return dpt.zeros(
res_shape, dtype=res_dt, usm_type=res_usm_type, sycl_queue=q
)
Expand Down
23 changes: 23 additions & 0 deletions dpctl/tests/test_tensor_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,26 @@ def test_sum_arg_out_dtype_scalar(arg_dtype, out_dtype):
assert isinstance(r, dpt.usm_ndarray)
assert r.dtype == dpt.dtype(out_dtype)
assert dpt.asnumpy(r) == 1


def test_sum_keepdims_zero_size():
"""See gh-1293"""
get_queue_or_skip()
n = 10
a = dpt.ones((n, 0, n))

s1 = dpt.sum(a, keepdims=True)
assert s1.shape == (1, 1, 1)

s2 = dpt.sum(a, axis=(0, 1), keepdims=True)
assert s2.shape == (1, 1, n)

s3 = dpt.sum(a, axis=(1, 2), keepdims=True)
assert s3.shape == (n, 1, 1)

s4 = dpt.sum(a, axis=(0, 2), keepdims=True)
assert s4.shape == (1, 0, 1)

a0 = a[0]
s5 = dpt.sum(a0, keepdims=True)
assert s5.shape == (1, 1)