Skip to content

Commit 925e21b

Browse files
authored
Reuse dpctl.tensor.reshape (#1391)
* Rework transpose methods to call dpctl.tensor functions * Reuse dpctl.tensor.reshape * added dpnp.shape() and unmuted more tests * fixed compiling issue & unmuted reshaper tests with order param * Resolve merge issues * resolve type mismatch on Win
1 parent 2e8f40c commit 925e21b

File tree

7 files changed

+190
-147
lines changed

7 files changed

+190
-147
lines changed

dpnp/dpnp_array.py

+15-23
Original file line numberDiff line numberDiff line change
@@ -870,14 +870,21 @@ def prod(self, axis=None, dtype=None, out=None, keepdims=False, initial=None, wh
870870
# 'real',
871871
# 'repeat',
872872

873-
def reshape(self, d0, *dn, order=b'C'):
873+
def reshape(self, *sh, **kwargs):
874874
"""
875875
Returns an array containing the same data with a new shape.
876876
877-
Refer to `dpnp.reshape` for full documentation.
877+
For full documentation refer to :obj:`numpy.ndarray.reshape`.
878878
879-
.. seealso::
880-
:meth:`numpy.ndarray.reshape`
879+
Returns
880+
-------
881+
y : dpnp.ndarray
882+
This will be a new view object if possible;
883+
otherwise, it will be a copy.
884+
885+
See Also
886+
--------
887+
:obj:`dpnp.reshape` : Equivalent function.
881888
882889
Notes
883890
-----
@@ -888,17 +895,9 @@ def reshape(self, d0, *dn, order=b'C'):
888895
889896
"""
890897

891-
if dn:
892-
if not isinstance(d0, int):
893-
msg_tmpl = "'{}' object cannot be interpreted as an integer"
894-
raise TypeError(msg_tmpl.format(type(d0).__name__))
895-
shape = [d0, *dn]
896-
else:
897-
shape = d0
898-
899-
shape_tup = dpnp.dpnp_utils._object_to_tuple(shape)
900-
901-
return dpnp.reshape(self, shape_tup)
898+
if len(sh) == 1:
899+
sh = sh[0]
900+
return dpnp.reshape(self, sh, **kwargs)
902901

903902
# 'resize',
904903

@@ -940,14 +939,7 @@ def shape(self, newshape):
940939
941940
"""
942941

943-
dpnp.reshape(self, newshape)
944-
945-
@property
946-
def shape(self):
947-
"""
948-
"""
949-
950-
return self._array_obj.shape
942+
dpnp.reshape(self, newshape=newshape)
951943

952944
@property
953945
def size(self):

dpnp/dpnp_iface_manipulation.py

+108-9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"repeat",
6969
"reshape",
7070
"rollaxis",
71+
"shape",
7172
"squeeze",
7273
"stack",
7374
"swapaxes",
@@ -501,26 +502,81 @@ def repeat(x1, repeats, axis=None):
501502
return call_origin(numpy.repeat, x1, repeats, axis)
502503

503504

504-
def reshape(x1, newshape, order='C'):
505+
def reshape(x, /, newshape, order='C', copy=None):
505506
"""
506507
Gives a new shape to an array without changing its data.
507508
508509
For full documentation refer to :obj:`numpy.reshape`.
509510
511+
Parameters
512+
----------
513+
x : {dpnp_array, usm_ndarray}
514+
Array to be reshaped.
515+
newshape : int or tuple of ints
516+
The new shape should be compatible with the original shape. If
517+
an integer, then the result will be a 1-D array of that length.
518+
One shape dimension can be -1. In this case, the value is
519+
inferred from the length of the array and remaining dimensions.
520+
order : {'C', 'F'}, optional
521+
Read the elements of `x` using this index order, and place the
522+
elements into the reshaped array using this index order. 'C'
523+
means to read / write the elements using C-like index order,
524+
with the last axis index changing fastest, back to the first
525+
axis index changing slowest. 'F' means to read / write the
526+
elements using Fortran-like index order, with the first index
527+
changing fastest, and the last index changing slowest. Note that
528+
the 'C' and 'F' options take no account of the memory layout of
529+
the underlying array, and only refer to the order of indexing.
530+
copy : bool, optional
531+
Boolean indicating whether or not to copy the input array.
532+
If ``True``, the result array will always be a copy of input `x`.
533+
If ``False``, the result array can never be a copy
534+
and a ValueError exception will be raised in case the copy is necessary.
535+
If ``None``, the result array will reuse existing memory buffer of `x`
536+
if possible and copy otherwise. Default: None.
537+
538+
Returns
539+
-------
540+
y : dpnp.ndarray
541+
This will be a new view object if possible; otherwise, it will
542+
be a copy. Note there is no guarantee of the *memory layout* (C- or
543+
Fortran- contiguous) of the returned array.
544+
510545
Limitations
511546
-----------
512-
Only 'C' order is supported.
547+
Parameter `order` is supported only with values ``"C"`` and ``"F"``.
548+
549+
See Also
550+
--------
551+
:obj:`dpnp.ndarray.reshape` : Equivalent method.
552+
553+
Examples
554+
--------
555+
>>> import dpnp as dp
556+
>>> a = dp.array([[1, 2, 3], [4, 5, 6]])
557+
>>> dp.reshape(a, 6)
558+
array([1, 2, 3, 4, 5, 6])
559+
>>> dp.reshape(a, 6, order='F')
560+
array([1, 4, 2, 5, 3, 6])
561+
562+
>>> dp.reshape(a, (3, -1)) # the unspecified value is inferred to be 2
563+
array([[1, 2],
564+
[3, 4],
565+
[5, 6]])
513566
514567
"""
515568

516-
x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
517-
if x1_desc:
518-
if order != 'C':
519-
pass
520-
else:
521-
return dpnp_reshape(x1_desc, newshape, order).get_pyobj()
569+
if newshape is None:
570+
newshape = x.shape
522571

523-
return call_origin(numpy.reshape, x1, newshape, order)
572+
if order is None:
573+
order = 'C'
574+
elif not order in "cfCF":
575+
raise ValueError(f"order must be one of 'C' or 'F' (got {order})")
576+
577+
usm_arr = dpnp.get_usm_ndarray(x)
578+
usm_arr = dpt.reshape(usm_arr, shape=newshape, order=order, copy=copy)
579+
return dpnp_array._create_from_usm_ndarray(usm_arr)
524580

525581

526582
def rollaxis(x1, axis, start=0):
@@ -571,6 +627,49 @@ def rollaxis(x1, axis, start=0):
571627
return call_origin(numpy.rollaxis, x1, axis, start)
572628

573629

630+
def shape(a):
631+
"""
632+
Return the shape of an array.
633+
634+
For full documentation refer to :obj:`numpy.shape`.
635+
636+
Parameters
637+
----------
638+
a : array_like
639+
Input array.
640+
641+
Returns
642+
-------
643+
shape : tuple of ints
644+
The elements of the shape tuple give the lengths of the
645+
corresponding array dimensions.
646+
647+
See Also
648+
--------
649+
len : ``len(a)`` is equivalent to ``np.shape(a)[0]`` for N-D arrays with
650+
``N>=1``.
651+
:obj:`dpnp.ndarray.shape` : Equivalent array method.
652+
653+
Examples
654+
--------
655+
>>> import dpnp as dp
656+
>>> dp.shape(dp.eye(3))
657+
(3, 3)
658+
>>> dp.shape([[1, 3]])
659+
(1, 2)
660+
>>> dp.shape([0])
661+
(1,)
662+
>>> dp.shape(0)
663+
()
664+
665+
"""
666+
667+
if dpnp.is_supported_array_type(a):
668+
return a.shape
669+
else:
670+
return numpy.shape(a)
671+
672+
574673
def squeeze(x, /, axis=None):
575674
"""
576675
Removes singleton dimensions (axes) from array `x`.

tests/skipped_tests.tbl

+3-49
Original file line numberDiff line numberDiff line change
@@ -680,55 +680,9 @@ tests/third_party/cupy/manipulation_tests/test_shape.py::TestRavel::test_ravel2
680680
tests/third_party/cupy/manipulation_tests/test_shape.py::TestRavel::test_ravel3
681681
tests/third_party/cupy/manipulation_tests/test_shape.py::TestRavel::test_external_ravel
682682
tests/third_party/cupy/manipulation_tests/test_shape.py::TestRavel::test_ravel
683-
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
684-
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
685-
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
686-
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
687-
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
688-
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
689-
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
690-
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
691-
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
692-
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
693-
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
694-
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
695-
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
696-
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
697-
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
698-
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
699-
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
700-
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
701-
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
702-
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
703-
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
704-
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
705-
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
706-
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
707-
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
708-
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
709-
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
710-
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
711-
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
712-
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
713-
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
714-
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
715-
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
716-
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
717-
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
718-
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
719-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_external_reshape
720-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_nocopy_reshape
721-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_nocopy_reshape_with_order
722-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_reshape2
723-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_reshape_strides
724-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_reshape_with_unknown_dimension
725-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_transposed_reshape2
726-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestShape_param_0_{shape=(2, 3)}::test_shape
727-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestShape_param_0_{shape=(2, 3)}::test_shape_list
728-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestShape_param_1_{shape=()}::test_shape
729-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestShape_param_1_{shape=()}::test_shape_list
730-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestShape_param_2_{shape=(4,)}::test_shape
731-
tests/third_party/cupy/manipulation_tests/test_shape.py::TestShape_param_2_{shape=(4,)}::test_shape_list
683+
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_reshape_zerosize
684+
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_reshape_zerosize2
685+
732686
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestRepeatRepeatsNdarray::test_func
733687
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestRepeatRepeatsNdarray::test_method
734688
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTileFailure_param_0_{reps=-1}::test_tile_failure

0 commit comments

Comments
 (0)