Skip to content
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

Reuse dpctl.tensor.reshape #1391

Merged
merged 7 commits into from
Jun 13, 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
38 changes: 15 additions & 23 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,14 +870,21 @@ def prod(self, axis=None, dtype=None, out=None, keepdims=False, initial=None, wh
# 'real',
# 'repeat',

def reshape(self, d0, *dn, order=b'C'):
def reshape(self, *sh, **kwargs):
"""
Returns an array containing the same data with a new shape.

Refer to `dpnp.reshape` for full documentation.
For full documentation refer to :obj:`numpy.ndarray.reshape`.

.. seealso::
:meth:`numpy.ndarray.reshape`
Returns
-------
y : dpnp.ndarray
This will be a new view object if possible;
otherwise, it will be a copy.

See Also
--------
:obj:`dpnp.reshape` : Equivalent function.

Notes
-----
Expand All @@ -888,17 +895,9 @@ def reshape(self, d0, *dn, order=b'C'):

"""

if dn:
if not isinstance(d0, int):
msg_tmpl = "'{}' object cannot be interpreted as an integer"
raise TypeError(msg_tmpl.format(type(d0).__name__))
shape = [d0, *dn]
else:
shape = d0

shape_tup = dpnp.dpnp_utils._object_to_tuple(shape)

return dpnp.reshape(self, shape_tup)
if len(sh) == 1:
sh = sh[0]
return dpnp.reshape(self, sh, **kwargs)

# 'resize',

Expand Down Expand Up @@ -940,14 +939,7 @@ def shape(self, newshape):

"""

dpnp.reshape(self, newshape)

@property
def shape(self):
"""
"""

return self._array_obj.shape
dpnp.reshape(self, newshape=newshape)

@property
def size(self):
Expand Down
117 changes: 108 additions & 9 deletions dpnp/dpnp_iface_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"repeat",
"reshape",
"rollaxis",
"shape",
"squeeze",
"stack",
"swapaxes",
Expand Down Expand Up @@ -501,26 +502,81 @@ def repeat(x1, repeats, axis=None):
return call_origin(numpy.repeat, x1, repeats, axis)


def reshape(x1, newshape, order='C'):
def reshape(x, /, newshape, order='C', copy=None):
"""
Gives a new shape to an array without changing its data.

For full documentation refer to :obj:`numpy.reshape`.

Parameters
----------
x : {dpnp_array, usm_ndarray}
Array to be reshaped.
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If
an integer, then the result will be a 1-D array of that length.
One shape dimension can be -1. In this case, the value is
inferred from the length of the array and remaining dimensions.
order : {'C', 'F'}, optional
Read the elements of `x` using this index order, and place the
elements into the reshaped array using this index order. 'C'
means to read / write the elements using C-like index order,
with the last axis index changing fastest, back to the first
axis index changing slowest. 'F' means to read / write the
elements using Fortran-like index order, with the first index
changing fastest, and the last index changing slowest. Note that
the 'C' and 'F' options take no account of the memory layout of
the underlying array, and only refer to the order of indexing.
copy : bool, optional
Boolean indicating whether or not to copy the input array.
If ``True``, the result array will always be a copy of input `x`.
If ``False``, the result array can never be a copy
and a ValueError exception will be raised in case the copy is necessary.
If ``None``, the result array will reuse existing memory buffer of `x`
if possible and copy otherwise. Default: None.

Returns
-------
y : dpnp.ndarray
This will be a new view object if possible; otherwise, it will
be a copy. Note there is no guarantee of the *memory layout* (C- or
Fortran- contiguous) of the returned array.

Limitations
-----------
Only 'C' order is supported.
Parameter `order` is supported only with values ``"C"`` and ``"F"``.

See Also
--------
:obj:`dpnp.ndarray.reshape` : Equivalent method.

Examples
--------
>>> import dpnp as dp
>>> a = dp.array([[1, 2, 3], [4, 5, 6]])
>>> dp.reshape(a, 6)
array([1, 2, 3, 4, 5, 6])
>>> dp.reshape(a, 6, order='F')
array([1, 4, 2, 5, 3, 6])

>>> dp.reshape(a, (3, -1)) # the unspecified value is inferred to be 2
array([[1, 2],
[3, 4],
[5, 6]])

"""

x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
if x1_desc:
if order != 'C':
pass
else:
return dpnp_reshape(x1_desc, newshape, order).get_pyobj()
if newshape is None:
newshape = x.shape

return call_origin(numpy.reshape, x1, newshape, order)
if order is None:
order = 'C'
elif not order in "cfCF":
raise ValueError(f"order must be one of 'C' or 'F' (got {order})")

usm_arr = dpnp.get_usm_ndarray(x)
usm_arr = dpt.reshape(usm_arr, shape=newshape, order=order, copy=copy)
return dpnp_array._create_from_usm_ndarray(usm_arr)


def rollaxis(x1, axis, start=0):
Expand Down Expand Up @@ -571,6 +627,49 @@ def rollaxis(x1, axis, start=0):
return call_origin(numpy.rollaxis, x1, axis, start)


def shape(a):
"""
Return the shape of an array.

For full documentation refer to :obj:`numpy.shape`.

Parameters
----------
a : array_like
Input array.

Returns
-------
shape : tuple of ints
The elements of the shape tuple give the lengths of the
corresponding array dimensions.

See Also
--------
len : ``len(a)`` is equivalent to ``np.shape(a)[0]`` for N-D arrays with
``N>=1``.
:obj:`dpnp.ndarray.shape` : Equivalent array method.

Examples
--------
>>> import dpnp as dp
>>> dp.shape(dp.eye(3))
(3, 3)
>>> dp.shape([[1, 3]])
(1, 2)
>>> dp.shape([0])
(1,)
>>> dp.shape(0)
()

"""

if dpnp.is_supported_array_type(a):
return a.shape
else:
return numpy.shape(a)


def squeeze(x, /, axis=None):
"""
Removes singleton dimensions (axes) from array `x`.
Expand Down
52 changes: 3 additions & 49 deletions tests/skipped_tests.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -680,55 +680,9 @@ tests/third_party/cupy/manipulation_tests/test_shape.py::TestRavel::test_ravel2
tests/third_party/cupy/manipulation_tests/test_shape.py::TestRavel::test_ravel3
tests/third_party/cupy/manipulation_tests/test_shape.py::TestRavel::test_external_ravel
tests/third_party/cupy/manipulation_tests/test_shape.py::TestRavel::test_ravel
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_0_{order_init='C', order_reshape='C', shape_in_out=((2, 3), (1, 6, 1))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_10_{order_init='C', order_reshape='c', shape_in_out=((6,), (2, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_11_{order_init='C', order_reshape='c', shape_in_out=((3, 3, 3), (9, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_12_{order_init='C', order_reshape='f', shape_in_out=((2, 3), (1, 6, 1))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_13_{order_init='C', order_reshape='f', shape_in_out=((6,), (2, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_14_{order_init='C', order_reshape='f', shape_in_out=((3, 3, 3), (9, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_15_{order_init='C', order_reshape='a', shape_in_out=((2, 3), (1, 6, 1))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_16_{order_init='C', order_reshape='a', shape_in_out=((6,), (2, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_17_{order_init='C', order_reshape='a', shape_in_out=((3, 3, 3), (9, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_18_{order_init='F', order_reshape='C', shape_in_out=((2, 3), (1, 6, 1))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_19_{order_init='F', order_reshape='C', shape_in_out=((6,), (2, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_1_{order_init='C', order_reshape='C', shape_in_out=((6,), (2, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_20_{order_init='F', order_reshape='C', shape_in_out=((3, 3, 3), (9, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_21_{order_init='F', order_reshape='F', shape_in_out=((2, 3), (1, 6, 1))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_22_{order_init='F', order_reshape='F', shape_in_out=((6,), (2, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_23_{order_init='F', order_reshape='F', shape_in_out=((3, 3, 3), (9, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_24_{order_init='F', order_reshape='A', shape_in_out=((2, 3), (1, 6, 1))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_25_{order_init='F', order_reshape='A', shape_in_out=((6,), (2, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_26_{order_init='F', order_reshape='A', shape_in_out=((3, 3, 3), (9, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_27_{order_init='F', order_reshape='c', shape_in_out=((2, 3), (1, 6, 1))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_28_{order_init='F', order_reshape='c', shape_in_out=((6,), (2, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_29_{order_init='F', order_reshape='c', shape_in_out=((3, 3, 3), (9, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_2_{order_init='C', order_reshape='C', shape_in_out=((3, 3, 3), (9, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_30_{order_init='F', order_reshape='f', shape_in_out=((2, 3), (1, 6, 1))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_31_{order_init='F', order_reshape='f', shape_in_out=((6,), (2, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_32_{order_init='F', order_reshape='f', shape_in_out=((3, 3, 3), (9, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_33_{order_init='F', order_reshape='a', shape_in_out=((2, 3), (1, 6, 1))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_34_{order_init='F', order_reshape='a', shape_in_out=((6,), (2, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_35_{order_init='F', order_reshape='a', shape_in_out=((3, 3, 3), (9, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_3_{order_init='C', order_reshape='F', shape_in_out=((2, 3), (1, 6, 1))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_4_{order_init='C', order_reshape='F', shape_in_out=((6,), (2, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_5_{order_init='C', order_reshape='F', shape_in_out=((3, 3, 3), (9, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_6_{order_init='C', order_reshape='A', shape_in_out=((2, 3), (1, 6, 1))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_7_{order_init='C', order_reshape='A', shape_in_out=((6,), (2, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_8_{order_init='C', order_reshape='A', shape_in_out=((3, 3, 3), (9, 3))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshapeOrder_param_9_{order_init='C', order_reshape='c', shape_in_out=((2, 3), (1, 6, 1))}::test_reshape_contiguity
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_external_reshape
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_nocopy_reshape
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_nocopy_reshape_with_order
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_reshape2
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_reshape_strides
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_reshape_with_unknown_dimension
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_transposed_reshape2
tests/third_party/cupy/manipulation_tests/test_shape.py::TestShape_param_0_{shape=(2, 3)}::test_shape
tests/third_party/cupy/manipulation_tests/test_shape.py::TestShape_param_0_{shape=(2, 3)}::test_shape_list
tests/third_party/cupy/manipulation_tests/test_shape.py::TestShape_param_1_{shape=()}::test_shape
tests/third_party/cupy/manipulation_tests/test_shape.py::TestShape_param_1_{shape=()}::test_shape_list
tests/third_party/cupy/manipulation_tests/test_shape.py::TestShape_param_2_{shape=(4,)}::test_shape
tests/third_party/cupy/manipulation_tests/test_shape.py::TestShape_param_2_{shape=(4,)}::test_shape_list
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_reshape_zerosize
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_reshape_zerosize2

tests/third_party/cupy/manipulation_tests/test_tiling.py::TestRepeatRepeatsNdarray::test_func
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestRepeatRepeatsNdarray::test_method
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTileFailure_param_0_{reps=-1}::test_tile_failure
Expand Down
Loading