Skip to content

Fix for errors reported in #729 when reshaping 0-elems array #756

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
Feb 1, 2022
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
5 changes: 4 additions & 1 deletion dpctl/tensor/_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ def reshape(X, newshape, order="C"):
newshape = [v if d == -1 else d for d in newshape]
if X.size != np.prod(newshape):
raise ValueError("Can not reshape into {}".format(newshape))
newsts = reshaped_strides(X.shape, X.strides, newshape, order=order)
if X.size:
newsts = reshaped_strides(X.shape, X.strides, newshape, order=order)
else:
newsts = (1,) * len(newshape)
if newsts is None:
# must perform a copy
flat_res = dpt.usm_ndarray(
Expand Down
17 changes: 11 additions & 6 deletions dpctl/tensor/_usmarray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ cdef class usm_ndarray:
cdef int contig_flag = 0
cdef Py_ssize_t *shape_ptr = NULL
cdef Py_ssize_t *strides_ptr = NULL
cdef Py_ssize_t size = -1
import operator

from ._reshape import reshaped_strides
Expand All @@ -439,15 +440,19 @@ cdef class usm_ndarray:
raise TypeError(
"Target shape must be a finite iterable of integers"
)
if not np.prod(new_shape) == shape_to_elem_count(self.nd_, self.shape_):
size = shape_to_elem_count(self.nd_, self.shape_)
if not np.prod(new_shape) == size:
raise TypeError(
f"Can not reshape array of size {self.size} into {new_shape}"
)
new_strides = reshaped_strides(
self.shape,
self.strides,
new_shape
)
if size > 0:
new_strides = reshaped_strides(
self.shape,
self.strides,
new_shape
)
else:
new_strides = (1,) * len(new_shape)
if new_strides is None:
raise AttributeError(
"Incompatible shape for in-place modification. "
Expand Down
41 changes: 41 additions & 0 deletions dpctl/tests/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,21 @@ def relaxed_strides_equal(st1, st2, sh):
X = dpt.usm_ndarray((4, 4), dtype="d")[::2, ::2]
with pytest.raises(AttributeError):
X.shape = (4,)
X = dpt.usm_ndarray((0,), dtype="i4")
X.shape = (0,)
X.shape = (
2,
0,
)
X.shape = (
0,
2,
)
X.shape = (
1,
0,
1,
)


def test_len():
Expand Down Expand Up @@ -816,6 +831,32 @@ def test_reshape():
Y = dpt.reshape(X, X.shape)
assert Y.flags == X.flags

A = dpt.usm_ndarray((0,), "i4")
A1 = dpt.reshape(A, (0,))
assert A1.shape == (0,)
A2 = dpt.reshape(
A,
(
2,
0,
),
)
assert A2.shape == (
2,
0,
)
A3 = dpt.reshape(A, (0, 2))
assert A3.shape == (
0,
2,
)
A4 = dpt.reshape(A, (1, 0, 2))
assert A4.shape == (
1,
0,
2,
)


def test_transpose():
n, m = 2, 3
Expand Down