diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 26464d40256..3c31b1642e9 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -40,7 +40,6 @@ import operator -import dpctl.tensor as dpt import numpy import dpnp @@ -95,6 +94,42 @@ ] +def _check_limitations(order=None, subok=False, like=None): + """ + Checking limitation kwargs for their supported values. + + Parameter `order` is supported only with values ``"C"``, ``"F"`` and ``None``. + Parameter `subok` is supported only with default value ``False``. + Parameter `like` is supported only with default value ``None``. + + Raises + ------ + NotImplementedError + If any input kwargs is of unsupported value. + """ + + if order in ("A", "a", "K", "k"): + raise NotImplementedError( + "Keyword argument `order` is supported only with " + f"values ``'C'`` and ``'F'``, but got {order}" + ) + elif order not in ("C", "c", "F", "f", None): + raise ValueError( + "Unrecognized `order` keyword value, expecting " + f"``'C'`` or ``'F'``, but got {order}" + ) + elif like is not None: + raise NotImplementedError( + "Keyword argument `like` is supported only with " + f"default value ``None``, but got {like}" + ) + elif subok is not False: + raise NotImplementedError( + "Keyword argument `subok` is supported only with " + f"default value ``False``, but got {subok}" + ) + + def arange( start, /, @@ -144,7 +179,7 @@ def arange( Limitations ----------- Parameter `like` is supported only with default value ``None``. - Otherwise the function will be executed sequentially on CPU. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -176,19 +211,16 @@ def arange( """ - if like is None: - return dpnp_container.arange( - start, - stop=stop, - step=step, - dtype=dtype, - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, - ) + _check_limitations(like=like) - return call_origin( - numpy.arange, start, stop=stop, step=step, dtype=dtype, like=like + return dpnp_container.arange( + start, + stop=stop, + step=step, + dtype=dtype, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, ) @@ -243,7 +275,7 @@ def array( Parameter `subok` is supported only with default value ``False``. Parameter `ndmin` is supported only with default value ``0``. Parameter `like` is supported only with default value ``None``. - Otherwise, the function raises `ValueError` exception. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -290,21 +322,12 @@ def array( """ - if subok is not False: - raise ValueError( - "Keyword argument `subok` is supported only with " - f"default value ``False``, but got {subok}" - ) - elif ndmin != 0: - raise ValueError( + _check_limitations(subok=subok, like=like) + if ndmin != 0: + raise NotImplementedError( "Keyword argument `ndmin` is supported only with " f"default value ``0``, but got {ndmin}" ) - elif like is not None: - raise ValueError( - "Keyword argument `like` is supported only with " - f"default value ``None``, but got {like}" - ) # `False`` in numpy means exactly the same like `None` in python array API: # that is to reuse existing memory buffer if possible or to copy otherwise. @@ -366,7 +389,7 @@ def asanyarray( Limitations ----------- Parameter `like` is supported only with default value ``None``. - Otherwise, the function raises `ValueError` exception. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -403,11 +426,7 @@ def asanyarray( """ - if like is not None: - raise ValueError( - "Keyword argument `like` is supported only with " - f"default value ``None``, but got {like}" - ) + _check_limitations(like=like) return asarray( a, @@ -463,7 +482,7 @@ def asarray( Limitations ----------- Parameter `like` is supported only with default value ``None``. - Otherwise, the function raises `ValueError` exception. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -500,11 +519,7 @@ def asarray( """ - if like is not None: - raise ValueError( - "Keyword argument `like` is supported only with " - f"default value ``None``, but got {like}" - ) + _check_limitations(like=like) return dpnp_container.asarray( a, @@ -552,7 +567,7 @@ def ascontiguousarray( Limitations ----------- Parameter `like` is supported only with default value ``None``. - Otherwise, the function raises `ValueError` exception. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -605,11 +620,7 @@ def ascontiguousarray( """ - if like is not None: - raise ValueError( - "Keyword argument `like` is supported only with " - f"default value ``None``, but got {like}" - ) + _check_limitations(like=like) # at least 1-d array has to be returned if dpnp.isscalar(a) or hasattr(a, "ndim") and a.ndim == 0: @@ -660,7 +671,7 @@ def asfortranarray( Limitations ----------- Parameter `like` is supported only with default value ``None``. - Otherwise, the function raises `ValueError` exception. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -716,11 +727,7 @@ def asfortranarray( """ - if like is not None: - raise ValueError( - "Keyword argument `like` is supported only with " - f"default value ``None``, but got {like}" - ) + _check_limitations(like=like) # at least 1-d array has to be returned if dpnp.isscalar(a) or hasattr(a, "ndim") and a.ndim == 0: @@ -765,7 +772,7 @@ def copy( Limitations ----------- Parameter `subok` is supported only with default value ``False``. - Otherwise, the function raises `ValueError` exception. + Otherwise, the function raises `NotImplementedError` exception. Returns ------- @@ -816,11 +823,7 @@ def copy( """ - if subok is not False: - raise ValueError( - "Keyword argument `subok` is supported only with " - f"default value ``False``, but got {subok}" - ) + _check_limitations(subok=subok) if dpnp.is_supported_array_type(a): sycl_queue_normalized = dpnp.get_normalized_queue_device( @@ -993,11 +996,6 @@ def diagflat(v, /, k=0, *, device=None, usm_type=None, sycl_queue=None): :obj:`diagonal` : Return specified diagonals. :obj:`trace` : Return sum along diagonals. - Limitations - ----------- - Parameter `k` is only supported as integer data type. - Otherwise ``TypeError`` exception will be raised. - Examples -------- >>> import dpnp as np @@ -1072,7 +1070,7 @@ def empty( dtype : dtype, optional The desired dtype for the array, e.g., dpnp.int32. Default is the default floating point data type for the device where input array is allocated. - order : {"C", "F"}, optional + order : {"C", "F", None}, optional Memory layout of the newly output array. Default: "C". device : {None, string, SyclDevice, SyclQueue}, optional An array API concept of device where the output array is created. @@ -1092,8 +1090,9 @@ def empty( Limitations ----------- + Parameter `order` is supported only with values ``"C"``, ``"F"`` and ``None``. Parameter `like` is supported only with default value ``None``. - Otherwise the function will be executed sequentially on CPU. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -1124,21 +1123,15 @@ def empty( """ - if like is not None: - pass - elif order not in ("C", "c", "F", "f", None): - pass - else: - return dpnp_container.empty( - shape, - dtype=dtype, - order=order, - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, - ) - - return call_origin(numpy.empty, shape, dtype=dtype, order=order, like=like) + _check_limitations(order=order, like=like) + return dpnp_container.empty( + shape, + dtype=dtype, + order=order, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ) def empty_like( @@ -1165,7 +1158,7 @@ def empty_like( dtype : dtype, optional The desired dtype for the array, e.g., dpnp.int32. Default is the default floating point data type for the device where input array is allocated. - order : {"C", "F"}, optional + order : {"C", "F", None}, optional Memory layout of the newly output array. Default: "C". shape : {int, sequence of ints} Overrides the shape of the result. @@ -1187,8 +1180,9 @@ def empty_like( Limitations ----------- + Parameter `order` is supported only with values ``"C"``, ``"F"`` and ``None``. Parameter `subok` is supported only with default value ``False``. - Otherwise the function will be executed sequentially on CPU. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -1220,28 +1214,22 @@ def empty_like( """ - if not isinstance(a, (dpnp.ndarray, dpt.usm_ndarray)): - pass - elif order not in ("C", "c", "F", "f", None): - pass - elif subok is not False: - pass - else: - _shape = a.shape if shape is None else shape - _dtype = a.dtype if dtype is None else dtype - _usm_type = a.usm_type if usm_type is None else usm_type - _sycl_queue = dpnp.get_normalized_queue_device( - a, sycl_queue=sycl_queue, device=device - ) - return dpnp_container.empty( - _shape, - dtype=_dtype, - order=order, - usm_type=_usm_type, - sycl_queue=_sycl_queue, - ) + dpnp.check_supported_arrays_type(a) + _check_limitations(order=order, subok=subok) - return call_origin(numpy.empty_like, a, dtype, order, subok, shape) + _shape = a.shape if shape is None else shape + _dtype = a.dtype if dtype is None else dtype + _usm_type = a.usm_type if usm_type is None else usm_type + _sycl_queue = dpnp.get_normalized_queue_device( + a, sycl_queue=sycl_queue, device=device + ) + return dpnp_container.empty( + _shape, + dtype=_dtype, + order=order, + usm_type=_usm_type, + sycl_queue=_sycl_queue, + ) def eye( @@ -1274,7 +1262,7 @@ def eye( dtype : dtype, optional The desired dtype for the array, e.g., dpnp.int32. Default is the default floating point data type for the device where input array is allocated. - order : {"C", "F"}, optional + order : {"C", "F", None}, optional Memory layout of the newly output array. Default: "C". device : {None, string, SyclDevice, SyclQueue}, optional An array API concept of device where the output array is created. @@ -1295,9 +1283,9 @@ def eye( Limitations ----------- - Parameter `order` is supported only with values ``"C"`` and ``"F"``. + Parameter `order` is supported only with values ``"C"``, ``"F"`` and ``None``. Parameter `like` is supported only with default value ``None``. - Otherwise the function will be executed sequentially on CPU. + Otherwise, the function raises `NotImplementedError` exception. Examples -------- @@ -1329,24 +1317,18 @@ def eye( [0, 1]]), Device(level_zero:gpu:0), 'host') """ - if order not in ("C", "c", "F", "f", None): - pass - elif like is not None: - pass - else: - return dpnp_container.eye( - N, - M, - k=k, - dtype=dtype, - order=order, - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, - ) - return call_origin( - numpy.eye, N, M, k=k, dtype=dtype, order=order, like=None + _check_limitations(order=order, like=like) + + return dpnp_container.eye( + N, + M, + k=k, + dtype=dtype, + order=order, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, ) @@ -1458,7 +1440,7 @@ def full( dtype : dtype, optional The desired dtype for the array, e.g., dpnp.int32. Default is the default floating point data type for the device where input array is allocated. - order : {"C", "F"}, optional + order : {"C", "F", None}, optional Memory layout of the newly output array. Default: "C". device : {None, string, SyclDevice, SyclQueue}, optional An array API concept of device where the output array is created. @@ -1478,9 +1460,9 @@ def full( Limitations ----------- - Parameter `order` is supported only with values ``"C"`` and ``"F"``. + Parameter `order` is supported only with values ``"C"``, ``"F"`` and ``None``. Parameter `like` is supported only with default value ``None``. - Otherwise the function will be executed sequentially on CPU. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -1511,22 +1493,17 @@ def full( """ - if like is not None: - pass - elif order not in ("C", "c", "F", "f", None): - pass - else: - return dpnp_container.full( - shape, - fill_value, - dtype=dtype, - order=order, - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, - ) + _check_limitations(order=order, like=like) - return call_origin(numpy.full, shape, fill_value, dtype, order, like=like) + return dpnp_container.full( + shape, + fill_value, + dtype=dtype, + order=order, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ) def full_like( @@ -1557,7 +1534,7 @@ def full_like( dtype : dtype, optional The desired dtype for the array, e.g., dpnp.int32. Default is the default floating point data type for the device where input array is allocated. - order : {"C", "F"}, optional + order : {"C", "F", None}, optional Memory layout of the newly output array. Default: "C". shape : {int, sequence of ints} Overrides the shape of the result. @@ -1579,9 +1556,9 @@ def full_like( Limitations ----------- - Parameter `order` is supported only with values ``"C"`` and ``"F"``. + Parameter `order` is supported only with values ``"C"``, ``"F"`` and ``None``. Parameter `subok` is supported only with default value ``False``. - Otherwise the function will be executed sequentially on CPU. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -1612,29 +1589,25 @@ def full_like( (array([1, 1, 1, 1, 1, 1]), Device(level_zero:gpu:0), 'host') """ - if not isinstance(a, (dpnp.ndarray, dpt.usm_ndarray)): - pass - elif order not in ("C", "c", "F", "f", None): - pass - elif subok is not False: - pass - else: - _shape = a.shape if shape is None else shape - _dtype = a.dtype if dtype is None else dtype - _usm_type = a.usm_type if usm_type is None else usm_type - _sycl_queue = dpnp.get_normalized_queue_device( - a, sycl_queue=sycl_queue, device=device - ) - return dpnp_container.full( - _shape, - fill_value, - dtype=_dtype, - order=order, - usm_type=_usm_type, - sycl_queue=_sycl_queue, - ) - return numpy.full_like(a, fill_value, dtype, order, subok, shape) + dpnp.check_supported_arrays_type(a) + _check_limitations(order=order, subok=subok) + + _shape = a.shape if shape is None else shape + _dtype = a.dtype if dtype is None else dtype + _usm_type = a.usm_type if usm_type is None else usm_type + _sycl_queue = dpnp.get_normalized_queue_device( + a, sycl_queue=sycl_queue, device=device + ) + + return dpnp_container.full( + _shape, + fill_value, + dtype=_dtype, + order=order, + usm_type=_usm_type, + sycl_queue=_sycl_queue, + ) def geomspace( @@ -1798,7 +1771,7 @@ def identity( Limitations ----------- Parameter `like` is currently not supported. - Otherwise the function will be executed sequentially on CPU. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -1836,20 +1809,19 @@ def identity( """ - if like is not None: - pass - elif n < 0: + if n < 0: raise ValueError("negative dimensions are not allowed") - else: - _dtype = dpnp.default_float_type() if dtype is None else dtype - return dpnp.eye( - n, - dtype=_dtype, - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, - ) - return call_origin(numpy.identity, n, dtype=dtype, like=like) + + _check_limitations(like=like) + + _dtype = dpnp.default_float_type() if dtype is None else dtype + return dpnp.eye( + n, + dtype=_dtype, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ) def linspace( @@ -2178,8 +2150,7 @@ def meshgrid(*xi, copy=True, sparse=False, indexing="xy"): """ - if not dpnp.check_supported_arrays_type(*xi): - raise TypeError("Each input array must be any of supported type") + dpnp.check_supported_arrays_type(*xi) ndim = len(xi) @@ -2361,7 +2332,7 @@ def ones( dtype : dtype, optional The desired dtype for the array, e.g., dpnp.int32. Default is the default floating point data type for the device where input array is allocated. - order : {"C", "F"}, optional + order : {"C", "F", None}, optional Memory layout of the newly output array. Default: "C". device : {None, string, SyclDevice, SyclQueue}, optional An array API concept of device where the output array is created. @@ -2381,9 +2352,9 @@ def ones( Limitations ----------- - Parameter `order` is supported only with values ``"C"`` and ``"F"``. + Parameter `order` is supported only with values ``"C"``, ``"F"`` and ``None``. Parameter `like` is supported only with default value ``None``. - Otherwise the function will be executed sequentially on CPU. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -2420,21 +2391,16 @@ def ones( """ - if like is not None: - pass - elif order not in ("C", "c", "F", "f", None): - pass - else: - return dpnp_container.ones( - shape, - dtype=dtype, - order=order, - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, - ) + _check_limitations(order=order, like=like) - return call_origin(numpy.ones, shape, dtype=dtype, order=order, like=like) + return dpnp_container.ones( + shape, + dtype=dtype, + order=order, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ) def ones_like( @@ -2461,7 +2427,7 @@ def ones_like( dtype : dtype, optional The desired dtype for the array, e.g., dpnp.int32. Default is the default floating point data type for the device where input array is allocated. - order : {"C", "F"}, optional + order : {"C", "F", None}, optional Memory layout of the newly output array. Default: "C". shape : {int, sequence of ints} Overrides the shape of the result. @@ -2483,8 +2449,9 @@ def ones_like( Limitations ----------- + Parameter `order` is supported only with values ``"C"``, ``"F"`` and ``None``. Parameter `subok` is supported only with default value ``False``. - Otherwise the function will be executed sequentially on CPU. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -2517,28 +2484,22 @@ def ones_like( (array([1, 1, 1, 1, 1, 1]), Device(level_zero:gpu:0), 'host') """ - if not isinstance(a, (dpnp.ndarray, dpt.usm_ndarray)): - pass - elif order not in ("C", "c", "F", "f", None): - pass - elif subok is not False: - pass - else: - _shape = a.shape if shape is None else shape - _dtype = a.dtype if dtype is None else dtype - _usm_type = a.usm_type if usm_type is None else usm_type - _sycl_queue = dpnp.get_normalized_queue_device( - a, sycl_queue=sycl_queue, device=device - ) - return dpnp_container.ones( - _shape, - dtype=_dtype, - order=order, - usm_type=_usm_type, - sycl_queue=_sycl_queue, - ) + dpnp.check_supported_arrays_type(a) + _check_limitations(order=order, subok=subok) - return call_origin(numpy.ones_like, a, dtype, order, subok, shape) + _shape = a.shape if shape is None else shape + _dtype = a.dtype if dtype is None else dtype + _usm_type = a.usm_type if usm_type is None else usm_type + _sycl_queue = dpnp.get_normalized_queue_device( + a, sycl_queue=sycl_queue, device=device + ) + return dpnp_container.ones( + _shape, + dtype=_dtype, + order=order, + usm_type=_usm_type, + sycl_queue=_sycl_queue, + ) def trace(x1, offset=0, axis1=0, axis2=1, dtype=None, out=None): @@ -2583,7 +2544,6 @@ def tri( device=None, usm_type="device", sycl_queue=None, - **kwargs, ): """ An array with ones at and below the given diagonal and zeros elsewhere. @@ -2618,12 +2578,6 @@ def tri( out : dpnp.ndarray of shape (N, M) Array with its lower triangle filled with ones and zeros elsewhere. - Limitations - ----------- - Parameter `M`, `N`, and `k` are only supported as integer data type and when they are positive. - Keyword argument `kwargs` is currently unsupported. - Otherwise the function will be executed sequentially on CPU. - See Also -------- :obj:`dpnp.tril` : Return lower triangle of an array. @@ -2664,35 +2618,39 @@ def tri( """ - if len(kwargs) != 0: - pass - elif not isinstance(N, int): - pass + if not isinstance(N, int): + raise TypeError(f"`N` must be a integer data type, but got {type(N)}") elif N < 0: - pass - elif M is not None and not isinstance(M, int): - pass - elif M is not None and M < 0: - pass - elif not isinstance(k, int): - pass + raise ValueError("negative dimensions are not allowed") + + if M is not None: + if not isinstance(M, int): + raise TypeError( + f"`M` must be a integer data type, but got {type(M)}" + ) + elif M < 0: + raise ValueError("negative dimensions are not allowed") else: - _dtype = ( - dpnp.default_float_type() if dtype in (dpnp.float, None) else dtype - ) - if M is None: - M = N + M = N - m = dpnp.ones( - (N, M), - dtype=_dtype, - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, - ) - return dpnp.tril(m, k=k) + _k = None + try: + _k = operator.index(k) + except TypeError: + pass + if _k is None: + raise TypeError(f"`k` must be a integer data type, but got {type(k)}") - return call_origin(numpy.tri, N, M, k, dtype, **kwargs) + _dtype = dpnp.default_float_type() if dtype in (dpnp.float, None) else dtype + + m = dpnp.ones( + (N, M), + dtype=_dtype, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ) + return dpnp.tril(m, k=_k) def tril(m, /, *, k=0): @@ -2716,11 +2674,6 @@ def tril(m, /, *, k=0): out : dpnp.ndarray of shape (N, M) Lower triangle of `m`, of same shape and dtype as `m`. - Limitations - ----------- - Parameter `k` is supported only of integer data type. - Otherwise the function will be executed sequentially on CPU. - Examples -------- >>> import dpnp as np @@ -2738,17 +2691,18 @@ def tril(m, /, *, k=0): _k = operator.index(k) except TypeError: pass + if _k is None: + raise TypeError(f"`k` must be a integer data type, but got {type(k)}") - if not isinstance(m, (dpnp.ndarray, dpt.usm_ndarray)): - pass - elif m.ndim < 2: - pass - elif _k is None: - pass - else: - return dpnp_container.tril(m, k=_k) + dpnp.check_supported_arrays_type(m) - return call_origin(numpy.tril, m, k) + if m.ndim == 0: + raise ValueError("Input array must have 1 or more dimensions") + + if m.ndim == 1: + m = dpnp.broadcast_to(m, (m.shape[0], m.shape[0])) + + return dpnp_container.tril(m, k=_k) def triu(m, /, *, k=0): @@ -2773,11 +2727,6 @@ def triu(m, /, *, k=0): out : dpnp.ndarray of shape (N, M) Upper triangle of `m`, of same shape and dtype as `m`. - Limitations - ----------- - Parameter `k` is supported only of integer data type. - Otherwise the function will be executed sequentially on CPU. - Examples -------- >>> import dpnp as np @@ -2795,17 +2744,18 @@ def triu(m, /, *, k=0): _k = operator.index(k) except TypeError: pass + if _k is None: + raise TypeError(f"`k` must be a integer data type, but got {type(k)}") - if not isinstance(m, (dpnp.ndarray, dpt.usm_ndarray)): - pass - elif m.ndim < 2: - pass - elif _k is None: - pass - else: - return dpnp_container.triu(m, k=_k) + dpnp.check_supported_arrays_type(m) + + if m.ndim == 0: + raise ValueError("Input array must have 1 or more dimensions") + + if m.ndim == 1: + m = dpnp.broadcast_to(m, (m.shape[0], m.shape[0])) - return call_origin(numpy.triu, m, k) + return dpnp_container.triu(m, k=_k) def vander( @@ -2849,11 +2799,6 @@ def vander( out : dpnp.ndarray Vandermonde matrix. - Limitations - ----------- - Parameter `N`, if it is not ``None``, is only supported as integer data type. - Otherwise ``TypeError`` exception will be raised. - Examples -------- >>> import dpnp as np @@ -2950,7 +2895,7 @@ def zeros( dtype : dtype, optional The desired dtype for the array, e.g., dpnp.int32. Default is the default floating point data type for the device where input array is allocated. - order : {"C", "F"}, optional + order : {"C", "F", None}, optional Memory layout of the newly output array. Default: "C". device : {None, string, SyclDevice, SyclQueue}, optional An array API concept of device where the output array is created. @@ -2970,9 +2915,9 @@ def zeros( Limitations ----------- - Parameter `order` is supported only with values ``"C"`` and ``"F"``. + Parameter `order` is supported only with values ``"C"``, ``"F"`` and ``None``. Parameter `like` is supported only with default value ``None``. - Otherwise the function will be executed sequentially on CPU. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -3008,21 +2953,17 @@ def zeros( (array([0., 0., 0.]), Device(level_zero:gpu:0), 'host') """ - if like is not None: - pass - elif order not in ("C", "c", "F", "f", None): - pass - else: - return dpnp_container.zeros( - shape, - dtype=dtype, - order=order, - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, - ) - return call_origin(numpy.zeros, shape, dtype=dtype, order=order, like=like) + _check_limitations(order=order, like=like) + + return dpnp_container.zeros( + shape, + dtype=dtype, + order=order, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ) def zeros_like( @@ -3049,7 +2990,7 @@ def zeros_like( dtype : dtype, optional The desired dtype for the array, e.g., dpnp.int32. Default is the default floating point data type for the device where input array is allocated. - order : {"C", "F"}, optional + order : {"C", "F", None}, optional Memory layout of the newly output array. Default: "C". shape : {int, sequence of ints} Overrides the shape of the result. @@ -3071,8 +3012,9 @@ def zeros_like( Limitations ----------- + Parameter `order` is supported only with values ``"C"``, ``"F"`` and ``None``. Parameter `subok` is supported only with default value ``False``. - Otherwise the function will be executed sequentially on CPU. + Otherwise, the function raises `NotImplementedError` exception. See Also -------- @@ -3105,25 +3047,20 @@ def zeros_like( (array([0, 0, 0, 0, 0, 0]), Device(level_zero:gpu:0), 'host') """ - if not isinstance(a, (dpnp.ndarray, dpt.usm_ndarray)): - pass - elif order not in ("C", "c", "F", "f", None): - pass - elif subok is not False: - pass - else: - _shape = a.shape if shape is None else shape - _dtype = a.dtype if dtype is None else dtype - _usm_type = a.usm_type if usm_type is None else usm_type - _sycl_queue = dpnp.get_normalized_queue_device( - a, sycl_queue=sycl_queue, device=device - ) - return dpnp_container.zeros( - _shape, - dtype=_dtype, - order=order, - usm_type=_usm_type, - sycl_queue=_sycl_queue, - ) - return call_origin(numpy.zeros_like, a, dtype, order, subok, shape) + dpnp.check_supported_arrays_type(a) + _check_limitations(order=order, subok=subok) + + _shape = a.shape if shape is None else shape + _dtype = a.dtype if dtype is None else dtype + _usm_type = a.usm_type if usm_type is None else usm_type + _sycl_queue = dpnp.get_normalized_queue_device( + a, sycl_queue=sycl_queue, device=device + ) + return dpnp_container.zeros( + _shape, + dtype=_dtype, + order=order, + usm_type=_usm_type, + sycl_queue=_sycl_queue, + ) diff --git a/tests/test_arraycreation.py b/tests/test_arraycreation.py index 267842c749c..7e70af6b765 100644 --- a/tests/test_arraycreation.py +++ b/tests/test_arraycreation.py @@ -21,28 +21,64 @@ @pytest.mark.parametrize( - "func, kwargs", + "func, args", [ - pytest.param("array", {"subok": True}), - pytest.param("array", {"ndmin": 1}), - pytest.param("array", {"like": dpnp.ones(10)}), - pytest.param("asanyarray", {"like": dpnp.array(7)}), - pytest.param("asarray", {"like": dpnp.array([1, 5])}), - pytest.param("ascontiguousarray", {"like": dpnp.zeros(4)}), - pytest.param("asfortranarray", {"like": dpnp.empty((2, 4))}), - pytest.param("copy", {"subok": True}), + pytest.param("empty", [3]), + pytest.param("empty_like", [dpnp.ones(10)]), + pytest.param("eye", [3]), + pytest.param("full", [3, 7]), + pytest.param("full_like", [dpnp.ones(10), 7]), + pytest.param("ones", [3]), + pytest.param("ones_like", [dpnp.ones(10)]), + pytest.param("zeros", [3]), + pytest.param("zeros_like", [dpnp.ones(10)]), ], ) -def test_array_copy_exception(func, kwargs): - sh = (3, 5) - x = dpnp.arange(1, prod(sh) + 1, 1).reshape(sh) +def test_exception_order(func, args): + with pytest.raises(NotImplementedError): + getattr(dpnp, func)(*args, order="K") + with pytest.raises(ValueError): + getattr(dpnp, func)(*args, order="S") - with pytest.raises( - ValueError, - match=f"Keyword argument `{list(kwargs.keys())[0]}` is supported " - "only with default value", - ): - getattr(dpnp, func)(x, **kwargs) + +@pytest.mark.parametrize( + "func, args", + [ + pytest.param("arange", [2]), + pytest.param("array", [2]), + pytest.param("asanyarray", [2]), + pytest.param("asarray", [2]), + pytest.param("ascontiguousarray", [2]), + pytest.param("asfortranarray", [2]), + pytest.param("empty", [(2,)]), + pytest.param("eye", [2]), + pytest.param("full", [(2,), 4]), + pytest.param("identity", [2]), + pytest.param("ones", [(2,)]), + pytest.param("zeros", [(2,)]), + ], +) +def test_exception_like(func, args): + like = dpnp.array([1, 2]) + with pytest.raises(NotImplementedError): + getattr(dpnp, func)(*args, like=like) + + +@pytest.mark.parametrize( + "func, args", + [ + pytest.param("array", []), + pytest.param("copy", []), + pytest.param("empty_like", []), + pytest.param("full_like", [5]), + pytest.param("ones_like", []), + pytest.param("zeros_like", []), + ], +) +def test_exception_subok(func, args): + x = dpnp.ones((3,)) + with pytest.raises(NotImplementedError): + getattr(dpnp, func)(x, *args, subok=True) @pytest.mark.parametrize( @@ -923,3 +959,30 @@ def test_meshgrid_raise_error(): b = dpnp.array([1, 2, 3, 4]) with pytest.raises(ValueError): dpnp.meshgrid(b, indexing="ab") + + +def test_exception_tri(): + x = dpnp.ones((2, 2)) + with pytest.raises(TypeError): + dpnp.tri(x) + with pytest.raises(TypeError): + dpnp.tri(1, x) + with pytest.raises(TypeError): + dpnp.tri(1, 1, k=1.2) + with pytest.raises(TypeError): + dpnp.tril(x, k=1.2) + with pytest.raises(TypeError): + dpnp.triu(x, k=1.2) + with pytest.raises(TypeError): + dpnp.tril(1) + with pytest.raises(TypeError): + dpnp.triu(1) + + with pytest.raises(ValueError): + dpnp.tri(-1) + with pytest.raises(ValueError): + dpnp.tri(1, -1) + with pytest.raises(ValueError): + dpnp.tril(dpnp.array(5)) + with pytest.raises(ValueError): + dpnp.triu(dpnp.array(5)) diff --git a/tests/third_party/cupy/creation_tests/test_matrix.py b/tests/third_party/cupy/creation_tests/test_matrix.py index 2308ecee00c..48f5c5b230b 100644 --- a/tests/third_party/cupy/creation_tests/test_matrix.py +++ b/tests/third_party/cupy/creation_tests/test_matrix.py @@ -132,7 +132,6 @@ def test_tri_posi(self, xp, dtype): {"shape": (2, 3, 4)}, ) @testing.gpu -@pytest.mark.usefixtures("allow_fall_back_on_numpy") class TestTriLowerAndUpper(unittest.TestCase): @testing.for_all_dtypes(no_complex=True) @testing.numpy_cupy_array_equal() @@ -140,6 +139,7 @@ def test_tril(self, xp, dtype): m = testing.shaped_arange(self.shape, xp, dtype) return xp.tril(m) + @pytest.mark.skip("list as input arg is not supported") @testing.numpy_cupy_array_equal() def test_tril_array_like(self, xp): return xp.tril([[1, 2], [3, 4]]) @@ -162,6 +162,7 @@ def test_triu(self, xp, dtype): m = testing.shaped_arange(self.shape, xp, dtype) return xp.triu(m) + @pytest.mark.skip("list as input arg is not supported") @testing.numpy_cupy_array_equal() def test_triu_array_like(self, xp): return xp.triu([[1, 2], [3, 4]])