Skip to content

Commit

Permalink
Merge pull request #1369 from IntelPython/fix-gh-1352
Browse files Browse the repository at this point in the history
Fix gh-1352 dpnp.sum() for empty array crashes.
  • Loading branch information
npolina4 authored Apr 6, 2023
2 parents c389b9d + 17f9977 commit d65a635
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dpnp/dpnp_algo/dpnp_algo_mathematical.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,9 @@ cpdef utils.dpnp_descriptor dpnp_sum(utils.dpnp_descriptor x1,
usm_type=x1_obj.usm_type,
sycl_queue=x1_obj.sycl_queue)

if x1.size == 0 and axis is None:
return result

result_sycl_queue = result.get_array().sycl_queue

cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
Expand Down
6 changes: 6 additions & 0 deletions dpnp/dpnp_iface_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1629,10 +1629,16 @@ def sum(x1, axis=None, dtype=None, out=None, keepdims=False, initial=None, where
if where is not True:
pass
else:
if dpnp.isscalar(out):
raise TypeError("output must be an array")
out_desc = dpnp.get_dpnp_descriptor(out, copy_when_nondefault_queue=False) if out is not None else None
result_obj = dpnp_sum(x1_desc, axis, dtype, out_desc, keepdims, initial, where).get_pyobj()
result = dpnp.convert_single_elem_array_to_scalar(result_obj, keepdims)

if x1_desc.size == 0 and axis is None:
result = dpnp.zeros_like(result)
if out is not None:
out[...] = result
return result

return call_origin(numpy.sum, x1, axis=axis, dtype=dtype, out=out, keepdims=keepdims, initial=initial, where=where)
Expand Down
32 changes: 32 additions & 0 deletions tests/test_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,3 +923,35 @@ def test_float_to_inf(self):
dpnp_res = dpnp.array(a) ** dpnp.array(b)

assert_allclose(numpy_res, dpnp_res.asnumpy())


@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True, no_bool=True))
@pytest.mark.parametrize("axis", [None, 0, 1, 2, 3])
def test_sum_empty(dtype, axis):
a = numpy.empty((1, 2, 0, 4), dtype=dtype)
numpy_res = a.sum(axis=axis)
dpnp_res = dpnp.array(a).sum(axis=axis)
assert_array_equal(numpy_res, dpnp_res.asnumpy())


@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True, no_bool=True))
def test_sum_empty_out(dtype):
a = dpnp.empty((1, 2, 0, 4), dtype=dtype)
out = dpnp.ones(())
res = a.sum(out=out)
assert_array_equal(out.asnumpy(), res.asnumpy())
assert_array_equal(out.asnumpy(), numpy.array(0, dtype=dtype))


@pytest.mark.parametrize("shape", [(), (1, 2, 3), (1, 0, 2), (10), (3, 3, 3), (5, 5), (0, 6)])
@pytest.mark.parametrize("dtype_in", get_all_dtypes(no_complex=True, no_bool=True))
@pytest.mark.parametrize("dtype_out", get_all_dtypes(no_complex=True, no_bool=True))
def test_sum(shape, dtype_in, dtype_out):
a_np = numpy.ones(shape, dtype=dtype_in)
a = dpnp.ones(shape, dtype=dtype_in)
axes = [None, 0, 1, 2]
for axis in axes:
if axis is None or axis < a.ndim:
numpy_res = a_np.sum(axis=axis, dtype=dtype_out)
dpnp_res = a.sum(axis=axis, dtype=dtype_out)
assert_array_equal(numpy_res, dpnp_res.asnumpy())

0 comments on commit d65a635

Please sign in to comment.