From 7c9e1e1a293514ebd4029c6dc79409dcb6957db3 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Wed, 27 Sep 2023 13:32:01 -0500 Subject: [PATCH 1/2] identity and tri with device keywords --- dpnp/backend/include/dpnp_iface_fptr.hpp | 27 ++- .../kernels/dpnp_krnl_arraycreation.cpp | 40 ----- dpnp/dpnp_algo/dpnp_algo.pxd | 4 - dpnp/dpnp_algo/dpnp_algo_arraycreation.pxi | 50 ------ dpnp/dpnp_iface_arraycreation.py | 160 +++++++++++++----- dpnp/dpnp_iface_manipulation.py | 2 +- tests/test_arraycreation.py | 11 +- tests/test_sycl_queue.py | 2 + tests/test_usm_type.py | 33 +++- 9 files changed, 175 insertions(+), 154 deletions(-) diff --git a/dpnp/backend/include/dpnp_iface_fptr.hpp b/dpnp/backend/include/dpnp_iface_fptr.hpp index b61ccf20137c..8d8f14e1b3f8 100644 --- a/dpnp/backend/include/dpnp_iface_fptr.hpp +++ b/dpnp/backend/include/dpnp_iface_fptr.hpp @@ -204,8 +204,6 @@ enum class DPNPFuncName : size_t extra parameters */ DPNP_FN_HYPOT, /**< Used in numpy.hypot() impl */ DPNP_FN_IDENTITY, /**< Used in numpy.identity() impl */ - DPNP_FN_IDENTITY_EXT, /**< Used in numpy.identity() impl, requires extra - parameters */ DPNP_FN_INITVAL, /**< Used in numpy ones, ones_like, zeros, zeros_like impls */ DPNP_FN_INITVAL_EXT, /**< Used in numpy ones, ones_like, zeros, zeros_like @@ -438,19 +436,18 @@ enum class DPNPFuncName : size_t DPNP_FN_TAKE, /**< Used in numpy.take() impl */ DPNP_FN_TAN, /**< Used in numpy.tan() impl */ DPNP_FN_TANH, /**< Used in numpy.tanh() impl */ - DPNP_FN_TRANSPOSE, /**< Used in numpy.transpose() impl */ - DPNP_FN_TRACE, /**< Used in numpy.trace() impl */ - DPNP_FN_TRACE_EXT, /**< Used in numpy.trace() impl, requires extra - parameters */ - DPNP_FN_TRAPZ, /**< Used in numpy.trapz() impl */ - DPNP_FN_TRAPZ_EXT, /**< Used in numpy.trapz() impl, requires extra - parameters */ - DPNP_FN_TRI, /**< Used in numpy.tri() impl */ - DPNP_FN_TRI_EXT, /**< Used in numpy.tri() impl, requires extra parameters */ - DPNP_FN_TRIL, /**< Used in numpy.tril() impl */ - DPNP_FN_TRIU, /**< Used in numpy.triu() impl */ - DPNP_FN_TRUNC, /**< Used in numpy.trunc() impl */ - DPNP_FN_VANDER, /**< Used in numpy.vander() impl */ + DPNP_FN_TRANSPOSE, /**< Used in numpy.transpose() impl */ + DPNP_FN_TRACE, /**< Used in numpy.trace() impl */ + DPNP_FN_TRACE_EXT, /**< Used in numpy.trace() impl, requires extra + parameters */ + DPNP_FN_TRAPZ, /**< Used in numpy.trapz() impl */ + DPNP_FN_TRAPZ_EXT, /**< Used in numpy.trapz() impl, requires extra + parameters */ + DPNP_FN_TRI, /**< Used in numpy.tri() impl */ + DPNP_FN_TRIL, /**< Used in numpy.tril() impl */ + DPNP_FN_TRIU, /**< Used in numpy.triu() impl */ + DPNP_FN_TRUNC, /**< Used in numpy.trunc() impl */ + DPNP_FN_VANDER, /**< Used in numpy.vander() impl */ DPNP_FN_VANDER_EXT, /**< Used in numpy.vander() impl, requires extra parameters */ DPNP_FN_VAR, /**< Used in numpy.var() impl */ diff --git a/dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp b/dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp index 3ea782a60647..a655c03100fb 100644 --- a/dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp +++ b/dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp @@ -392,13 +392,6 @@ template void (*dpnp_identity_default_c)(void *, const size_t) = dpnp_identity_c<_DataType>; -template -DPCTLSyclEventRef (*dpnp_identity_ext_c)(DPCTLSyclQueueRef, - void *, - const size_t, - const DPCTLEventVectorRef) = - dpnp_identity_c<_DataType>; - template class dpnp_ones_c_kernel; @@ -851,15 +844,6 @@ template void (*dpnp_tri_default_c)(void *, const size_t, const size_t, const int) = dpnp_tri_c<_DataType>; -template -DPCTLSyclEventRef (*dpnp_tri_ext_c)(DPCTLSyclQueueRef, - void *, - const size_t, - const size_t, - const int, - const DPCTLEventVectorRef) = - dpnp_tri_c<_DataType>; - template DPCTLSyclEventRef dpnp_tril_c(DPCTLSyclQueueRef q_ref, void *array_in, @@ -1265,21 +1249,6 @@ void func_map_init_arraycreation(func_map_t &fmap) fmap[DPNPFuncName::DPNP_FN_IDENTITY][eft_C128][eft_C128] = { eft_C128, (void *)dpnp_identity_default_c>}; - fmap[DPNPFuncName::DPNP_FN_IDENTITY_EXT][eft_INT][eft_INT] = { - eft_INT, (void *)dpnp_identity_ext_c}; - fmap[DPNPFuncName::DPNP_FN_IDENTITY_EXT][eft_LNG][eft_LNG] = { - eft_LNG, (void *)dpnp_identity_ext_c}; - fmap[DPNPFuncName::DPNP_FN_IDENTITY_EXT][eft_FLT][eft_FLT] = { - eft_FLT, (void *)dpnp_identity_ext_c}; - fmap[DPNPFuncName::DPNP_FN_IDENTITY_EXT][eft_DBL][eft_DBL] = { - eft_DBL, (void *)dpnp_identity_ext_c}; - fmap[DPNPFuncName::DPNP_FN_IDENTITY_EXT][eft_BLN][eft_BLN] = { - eft_BLN, (void *)dpnp_identity_ext_c}; - fmap[DPNPFuncName::DPNP_FN_IDENTITY_EXT][eft_C64][eft_C64] = { - eft_C64, (void *)dpnp_identity_ext_c>}; - fmap[DPNPFuncName::DPNP_FN_IDENTITY_EXT][eft_C128][eft_C128] = { - eft_C128, (void *)dpnp_identity_ext_c>}; - fmap[DPNPFuncName::DPNP_FN_ONES][eft_INT][eft_INT] = { eft_INT, (void *)dpnp_ones_default_c}; fmap[DPNPFuncName::DPNP_FN_ONES][eft_LNG][eft_LNG] = { @@ -1431,15 +1400,6 @@ void func_map_init_arraycreation(func_map_t &fmap) fmap[DPNPFuncName::DPNP_FN_TRI][eft_DBL][eft_DBL] = { eft_DBL, (void *)dpnp_tri_default_c}; - fmap[DPNPFuncName::DPNP_FN_TRI_EXT][eft_INT][eft_INT] = { - eft_INT, (void *)dpnp_tri_ext_c}; - fmap[DPNPFuncName::DPNP_FN_TRI_EXT][eft_LNG][eft_LNG] = { - eft_LNG, (void *)dpnp_tri_ext_c}; - fmap[DPNPFuncName::DPNP_FN_TRI_EXT][eft_FLT][eft_FLT] = { - eft_FLT, (void *)dpnp_tri_ext_c}; - fmap[DPNPFuncName::DPNP_FN_TRI_EXT][eft_DBL][eft_DBL] = { - eft_DBL, (void *)dpnp_tri_ext_c}; - fmap[DPNPFuncName::DPNP_FN_TRIL][eft_INT][eft_INT] = { eft_INT, (void *)dpnp_tril_default_c}; fmap[DPNPFuncName::DPNP_FN_TRIL][eft_LNG][eft_LNG] = { diff --git a/dpnp/dpnp_algo/dpnp_algo.pxd b/dpnp/dpnp_algo/dpnp_algo.pxd index 9513f9e085ce..9770d3abe6ef 100644 --- a/dpnp/dpnp_algo/dpnp_algo.pxd +++ b/dpnp/dpnp_algo/dpnp_algo.pxd @@ -108,8 +108,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na DPNP_FN_FMOD_EXT DPNP_FN_FULL DPNP_FN_FULL_LIKE - DPNP_FN_IDENTITY - DPNP_FN_IDENTITY_EXT DPNP_FN_INV DPNP_FN_INV_EXT DPNP_FN_KRON @@ -242,8 +240,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na DPNP_FN_TRANSPOSE DPNP_FN_TRAPZ DPNP_FN_TRAPZ_EXT - DPNP_FN_TRI - DPNP_FN_TRI_EXT DPNP_FN_TRIL DPNP_FN_TRIL_EXT DPNP_FN_TRIU diff --git a/dpnp/dpnp_algo/dpnp_algo_arraycreation.pxi b/dpnp/dpnp_algo/dpnp_algo_arraycreation.pxi index 3525a42ca0d4..645ade4d813c 100644 --- a/dpnp/dpnp_algo/dpnp_algo_arraycreation.pxi +++ b/dpnp/dpnp_algo/dpnp_algo_arraycreation.pxi @@ -39,12 +39,10 @@ __all__ += [ "dpnp_copy", "dpnp_diag", "dpnp_geomspace", - "dpnp_identity", "dpnp_linspace", "dpnp_logspace", "dpnp_ptp", "dpnp_trace", - "dpnp_tri", "dpnp_vander", ] @@ -168,28 +166,6 @@ cpdef utils.dpnp_descriptor dpnp_geomspace(start, stop, num, endpoint, dtype, ax return result -cpdef utils.dpnp_descriptor dpnp_identity(n, result_dtype): - cdef DPNPFuncType dtype_in = dpnp_dtype_to_DPNPFuncType(result_dtype) - - cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_IDENTITY_EXT, dtype_in, DPNP_FT_NONE) - - cdef shape_type_c shape_in = (n, n) - cdef utils.dpnp_descriptor result = utils.create_output_descriptor(shape_in, kernel_data.return_type, None) - - result_sycl_queue = result.get_array().sycl_queue - - cdef c_dpctl.SyclQueue q = result_sycl_queue - cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref() - - cdef fptr_1out_t func = kernel_data.ptr - cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, result.get_data(), n, NULL) - - with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref) - c_dpctl.DPCTLEvent_Delete(event_ref) - - return result - - def dpnp_linspace(start, stop, num, dtype=None, device=None, usm_type=None, sycl_queue=None, endpoint=True, retstep=False, axis=0): usm_type_alloc, sycl_queue_alloc = utils_py.get_usm_allocations([start, stop]) @@ -401,32 +377,6 @@ cpdef utils.dpnp_descriptor dpnp_trace(utils.dpnp_descriptor arr, offset=0, axis return result -cpdef utils.dpnp_descriptor dpnp_tri(N, M=None, k=0, dtype=dpnp.float): - if M is None: - M = N - - cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(dtype) - - cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_TRI_EXT, param1_type, param1_type) - - cdef shape_type_c shape_in = (N, M) - cdef utils.dpnp_descriptor result = utils.create_output_descriptor(shape_in, kernel_data.return_type, None) - - result_sycl_queue = result.get_array().sycl_queue - - cdef c_dpctl.SyclQueue q = result_sycl_queue - cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref() - - cdef custom_indexing_1out_func_ptr_t func = kernel_data.ptr - - cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, result.get_data(), N, M, k, NULL) - - with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref) - c_dpctl.DPCTLEvent_Delete(event_ref) - - return result - - cpdef utils.dpnp_descriptor dpnp_vander(utils.dpnp_descriptor x1, int N, int increasing): cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype) cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_VANDER_EXT, param1_type, DPNP_FT_NONE) diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 22e7c1e0d9d1..644f6d649d36 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -90,6 +90,21 @@ "zeros_like", ] +i1 = numpy.iinfo(numpy.int8) +i2 = numpy.iinfo(numpy.int16) +i4 = numpy.iinfo(numpy.int32) + + +def _min_int(low, high): + """Get small int that fits the range""" + if high <= i1.max and low >= i1.min: + return numpy.int8 + if high <= i2.max and low >= i2.min: + return numpy.int16 + if high <= i4.max and low >= i4.min: + return numpy.int32 + return numpy.int64 + def arange( start, @@ -115,7 +130,7 @@ def arange( Limitations ----------- - Parameter ``like`` is supported only with default value ``None``. + Parameter `like` is supported only with default value ``None``. Otherwise the function will be executed sequentially on CPU. See Also @@ -668,8 +683,8 @@ def empty( Limitations ----------- - Parameter ``order`` is supported only with values ``"C"`` and ``"F"``. - Parameter ``like`` is supported only with default value ``None``. + Parameter `order` is supported only with values ``"C"`` and ``"F"``. + Parameter `like` is supported only with default value ``None``. Otherwise the function will be executed sequentially on CPU. See Also @@ -724,9 +739,9 @@ def empty_like( Limitations ----------- - Parameter ``x1`` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray` - Parameter ``order`` is supported with values ``"C"`` or ``"F"``. - Parameter ``subok`` is supported only with default value ``False``. + Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray` + Parameter `order` is supported with values ``"C"`` or ``"F"``. + Parameter `subok` is supported only with default value ``False``. Otherwise the function will be executed sequentially on CPU. See Also @@ -790,8 +805,8 @@ def eye( Limitations ----------- - Parameter ``order`` is supported only with values ``"C"`` and ``"F"``. - Parameter ``like`` is supported only with default value ``None``. + Parameter `order` is supported only with values ``"C"`` and ``"F"``. + Parameter `like` is supported only with default value ``None``. Otherwise the function will be executed sequentially on CPU. """ @@ -916,8 +931,8 @@ def full( Limitations ----------- - Parameter ``order`` is supported only with values ``"C"`` and ``"F"``. - Parameter ``like`` is supported only with default value ``None``. + Parameter `order` is supported only with values ``"C"`` and ``"F"``. + Parameter `like` is supported only with default value ``None``. Otherwise the function will be executed sequentially on CPU. See Also @@ -973,9 +988,9 @@ def full_like( Limitations ----------- - Parameter ``x1`` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray` - Parameter ``order`` is supported only with values ``"C"`` and ``"F"``. - Parameter ``subok`` is supported only with default value ``False``. + Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray` + Parameter `order` is supported only with values ``"C"`` and ``"F"``. + Parameter `subok` is supported only with default value ``False``. Otherwise the function will be executed sequentially on CPU. See Also @@ -1061,7 +1076,9 @@ def geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0): return call_origin(numpy.geomspace, start, stop, num, endpoint, dtype, axis) -def identity(n, dtype=None, *, like=None): +def identity( + n, dtype=None, *, device=None, usm_type="device", sycl_queue=None, like=None +): """ Return the identity array. @@ -1069,9 +1086,22 @@ def identity(n, dtype=None, *, like=None): For full documentation refer to :obj:`numpy.identity`. + Returns + ------- + out : dpnp.ndarray + `n` x `n` array with its main diagonal set to one, + and all other elements 0. + Limitations ----------- - Parameter ``like`` is currently not supported . + Parameter `like` is currently not supported. + Otherwise the function will be executed sequentially on CPU. + + See Also + -------- + :obj:`dpnp.eye` : Return a 2-D array with ones on the diagonal and zeros elsewhere. + :obj:`dpnp.ones` : Return a new array setting values to one. + :obj:`dpnp.diag` : Return diagonal 2-D array from an input 1-D array. Examples -------- @@ -1086,11 +1116,16 @@ def identity(n, dtype=None, *, like=None): if like is not None: pass elif n < 0: - pass + raise ValueError("negative dimensions are not allowed") else: _dtype = dpnp.default_float_type() if dtype is None else dtype - return dpnp_identity(n, _dtype).get_pyobj() - + 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) @@ -1115,8 +1150,8 @@ def linspace( Limitations ----------- - Parameter ``axis`` is supported only with default value ``0``. - Parameter ``retstep`` is supported only with default value ``False``. + Parameter `axis` is supported only with default value ``0``. + Parameter `retstep` is supported only with default value ``False``. Otherwise the function will be executed sequentially on CPU. See Also @@ -1198,7 +1233,7 @@ def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0): Limitations ----------- - Parameter ``axis`` is supported only with default value ``0``. + Parameter `axis` is supported only with default value ``0``. See Also -------- @@ -1252,8 +1287,8 @@ def meshgrid(*xi, copy=True, sparse=False, indexing="xy"): Limitations ----------- Each array instance from `xi` is supported as either :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`. - Parameter ``copy`` is supported only with default value ``True``. - Parameter ``sparse`` is supported only with default value ``False``. + Parameter `copy` is supported only with default value ``True``. + Parameter `sparse` is supported only with default value ``False``. Otherwise the function will be executed sequentially on CPU. Examples @@ -1379,8 +1414,8 @@ def ones( Limitations ----------- - Parameter ``order`` is supported only with values ``"C"`` and ``"F"``. - Parameter ``like`` is supported only with default value ``None``. + Parameter `order` is supported only with values ``"C"`` and ``"F"``. + Parameter `like` is supported only with default value ``None``. Otherwise the function will be executed sequentially on CPU. See Also @@ -1439,9 +1474,9 @@ def ones_like( Limitations ----------- - Parameter ``x1`` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray` - Parameter ``order`` is supported with values ``"C"`` or ``"F"``. - Parameter ``subok`` is supported only with default value ``False``. + Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray` + Parameter `order` is supported with values ``"C"`` or ``"F"``. + Parameter `subok` is supported only with default value ``False``. Otherwise the function will be executed sequentially on CPU. See Also @@ -1523,7 +1558,7 @@ def trace(x1, offset=0, axis1=0, axis2=1, dtype=None, out=None): Limitations ----------- Input array is supported as :obj:`dpnp.ndarray`. - Parameters ``axis1``, ``axis2``, ``out`` and ``dtype`` are supported only with default values. + Parameters `axis1`, `axis2`, `out` and `dtype` are supported only with default values. """ x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False) @@ -1546,12 +1581,37 @@ def trace(x1, offset=0, axis1=0, axis2=1, dtype=None, out=None): return call_origin(numpy.trace, x1, offset, axis1, axis2, dtype, out) -def tri(N, M=None, k=0, dtype=dpnp.float, **kwargs): +def tri( + N, + M=None, + k=0, + dtype=dpnp.float, + device=None, + usm_type="device", + sycl_queue=None, + **kwargs, +): """ An array with ones at and below the given diagonal and zeros elsewhere. For full documentation refer to :obj:`numpy.tri`. + Returns + ------- + out : 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. + 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. + :obj:`dpnp.triu` : Return upper triangle of an array. + Examples -------- >>> import dpnp as np @@ -1572,12 +1632,8 @@ def tri(N, M=None, k=0, dtype=dpnp.float, **kwargs): pass elif not isinstance(N, int): pass - 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 else: @@ -1586,7 +1642,31 @@ def tri(N, M=None, k=0, dtype=dpnp.float, **kwargs): if dtype in (dpnp.float, None) else dtype ) - return dpnp_tri(N, M, k, _dtype).get_pyobj() + if M is None: + M = N + + m = dpnp.greater_equal( + dpnp.arange( + N, + dtype=_min_int(0, N), + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + )[:, dpnp.newaxis], + dpnp.arange( + -k, + M - k, + dtype=_min_int(-k, M - k), + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ), + ) + + # Avoid making a copy if the requested type is already bool + m = m.astype(_dtype, copy=False) + + return m return call_origin(numpy.tri, N, M, k, dtype, **kwargs) @@ -1737,8 +1817,8 @@ def zeros( Limitations ----------- - Parameter ``order`` is supported only with values ``"C"`` and ``"F"``. - Parameter ``like`` is supported only with default value ``None``. + Parameter `order` is supported only with values ``"C"`` and ``"F"``. + Parameter `like` is supported only with default value ``None``. Otherwise the function will be executed sequentially on CPU. See Also @@ -1796,9 +1876,9 @@ def zeros_like( Limitations ----------- - Parameter ``x1`` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray` - Parameter ``order`` is supported with values ``"C"`` or ``"F"``. - Parameter ``subok`` is supported only with default value ``False``. + Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray` + Parameter `order` is supported with values ``"C"`` or ``"F"``. + Parameter `subok` is supported only with default value ``False``. Otherwise the function will be executed sequentially on CPU. See Also diff --git a/dpnp/dpnp_iface_manipulation.py b/dpnp/dpnp_iface_manipulation.py index f71aa751cb2c..41e55ee80d49 100644 --- a/dpnp/dpnp_iface_manipulation.py +++ b/dpnp/dpnp_iface_manipulation.py @@ -1223,7 +1223,7 @@ def stack(arrays, /, *, axis=0, out=None, dtype=None, **kwargs): or :class:`dpctl.tensor.usm_ndarray`. Otherwise ``TypeError`` exception will be raised. Parameters `out` and `dtype` are supported with default value. - Keyword argument ``kwargs`` is currently unsupported. + Keyword argument `kwargs` is currently unsupported. Otherwise the function will be executed sequentially on CPU. See Also diff --git a/tests/test_arraycreation.py b/tests/test_arraycreation.py index aead310b5dcf..dfb2f44fb864 100644 --- a/tests/test_arraycreation.py +++ b/tests/test_arraycreation.py @@ -292,17 +292,22 @@ def test_trace(array, offset, type, dtype): assert_array_equal(trace_func(dpnp, ia), trace_func(numpy, a)) -@pytest.mark.parametrize("N", [0, 1, 2, 3, 4], ids=["0", "1", "2", "3", "4"]) -@pytest.mark.parametrize("M", [0, 1, 2, 3, 4], ids=["0", "1", "2", "3", "4"]) +@pytest.mark.parametrize( + "N", [-1, 0, 1, 2, 3, 4], ids=["-1", "0", "1", "2", "3", "4"] +) +@pytest.mark.parametrize( + "M", [-1, None, 0, 1, 2, 3, 4], ids=["-1", "None", "0", "1", "2", "3", "4"] +) @pytest.mark.parametrize( "k", [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], ids=["-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5"], ) -@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True)) +@pytest.mark.parametrize("dtype", get_all_dtypes()) def test_tri(N, M, k, dtype): func = lambda xp: xp.tri(N, M, k, dtype=dtype) assert_array_equal(func(dpnp), func(numpy)) + assert func(dpnp).shape == func(numpy).shape def test_tri_default_dtype(): diff --git a/tests/test_sycl_queue.py b/tests/test_sycl_queue.py index a11880d9d444..864bacf0d93f 100644 --- a/tests/test_sycl_queue.py +++ b/tests/test_sycl_queue.py @@ -85,8 +85,10 @@ def vvsort(val, vec, size, xp): pytest.param("arange", [-25.7], {"stop": 10**8, "step": 15}), pytest.param("full", [(2, 2)], {"fill_value": 5}), pytest.param("eye", [4, 2], {}), + pytest.param("identity", [4], {}), pytest.param("linspace", [0, 4, 8], {}), pytest.param("ones", [(2, 2)], {}), + pytest.param("tri", [3, 5, 2], {}), pytest.param("zeros", [(2, 2)], {}), ], ) diff --git a/tests/test_usm_type.py b/tests/test_usm_type.py index a124d7e9a1e6..dcd29b7af9e9 100644 --- a/tests/test_usm_type.py +++ b/tests/test_usm_type.py @@ -2,10 +2,14 @@ import dpctl.tensor as dpt import dpctl.utils as du +import numpy import pytest +from numpy.testing import assert_allclose import dpnp as dp +from .helper import assert_dtype_allclose + list_of_usm_types = ["device", "shared", "host"] @@ -147,7 +151,7 @@ def test_coerced_usm_types_power(usm_type_x, usm_type_y): ) @pytest.mark.parametrize("usm_type_x", list_of_usm_types, ids=list_of_usm_types) @pytest.mark.parametrize("usm_type_y", list_of_usm_types, ids=list_of_usm_types) -def test_array_creation(func, args, usm_type_x, usm_type_y): +def test_array_creation_from_an_array(func, args, usm_type_x, usm_type_y): x0 = dp.full(10, 3, usm_type=usm_type_x) new_args = [eval(val, {"x0": x0}) for val in args] @@ -158,6 +162,33 @@ def test_array_creation(func, args, usm_type_x, usm_type_y): assert y.usm_type == usm_type_y +@pytest.mark.parametrize( + "func, arg, kwargs", + [ + pytest.param("arange", [-25.7], {"stop": 10**8, "step": 15}), + pytest.param("full", [(2, 2)], {"fill_value": 5}), + pytest.param("eye", [4, 2], {}), + pytest.param("identity", [4], {}), + pytest.param("linspace", [0, 4, 8], {}), + pytest.param("ones", [(2, 2)], {}), + pytest.param("tri", [3, 5, 2], {}), + pytest.param("zeros", [(2, 2)], {}), + ], +) +@pytest.mark.parametrize("usm_type", list_of_usm_types, ids=list_of_usm_types) +def test_array_creation_from_scratch(func, arg, kwargs, usm_type): + dpnp_kwargs = dict(kwargs) + dpnp_kwargs["usm_type"] = usm_type + dpnp_array = getattr(dp, func)(*arg, **dpnp_kwargs) + numpy_array = getattr(numpy, func)(*arg, dtype=dpnp_array.dtype, **kwargs) + + tol = 1e-06 + assert_allclose(dpnp_array, numpy_array, rtol=tol, atol=tol) + assert dpnp_array.shape == numpy_array.shape + assert_dtype_allclose(dpnp_array, numpy_array) + assert dpnp_array.usm_type == usm_type + + @pytest.mark.parametrize( "func", [ From af8adaaa5a3827d0d9fb36857a600d470a1afe39 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Thu, 5 Oct 2023 18:41:54 -0500 Subject: [PATCH 2/2] address comments --- dpnp/dpnp_iface_arraycreation.py | 47 ++++++++------------------------ tests/test_arraycreation.py | 7 ++--- 2 files changed, 13 insertions(+), 41 deletions(-) diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 644f6d649d36..33a1f871d79a 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -90,21 +90,6 @@ "zeros_like", ] -i1 = numpy.iinfo(numpy.int8) -i2 = numpy.iinfo(numpy.int16) -i4 = numpy.iinfo(numpy.int32) - - -def _min_int(low, high): - """Get small int that fits the range""" - if high <= i1.max and low >= i1.min: - return numpy.int8 - if high <= i2.max and low >= i2.min: - return numpy.int16 - if high <= i4.max and low >= i4.min: - return numpy.int32 - return numpy.int64 - def arange( start, @@ -1632,8 +1617,12 @@ def tri( pass elif not isinstance(N, int): pass + 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 else: @@ -1645,28 +1634,14 @@ def tri( if M is None: M = N - m = dpnp.greater_equal( - dpnp.arange( - N, - dtype=_min_int(0, N), - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, - )[:, dpnp.newaxis], - dpnp.arange( - -k, - M - k, - dtype=_min_int(-k, M - k), - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, - ), + m = dpnp.ones( + (N, M), + dtype=_dtype, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, ) - - # Avoid making a copy if the requested type is already bool - m = m.astype(_dtype, copy=False) - - return m + return dpnp.tril(m, k=k) return call_origin(numpy.tri, N, M, k, dtype, **kwargs) diff --git a/tests/test_arraycreation.py b/tests/test_arraycreation.py index dfb2f44fb864..9c7fd6bf0602 100644 --- a/tests/test_arraycreation.py +++ b/tests/test_arraycreation.py @@ -292,11 +292,9 @@ def test_trace(array, offset, type, dtype): assert_array_equal(trace_func(dpnp, ia), trace_func(numpy, a)) +@pytest.mark.parametrize("N", [0, 1, 2, 3, 4], ids=["0", "1", "2", "3", "4"]) @pytest.mark.parametrize( - "N", [-1, 0, 1, 2, 3, 4], ids=["-1", "0", "1", "2", "3", "4"] -) -@pytest.mark.parametrize( - "M", [-1, None, 0, 1, 2, 3, 4], ids=["-1", "None", "0", "1", "2", "3", "4"] + "M", [None, 0, 1, 2, 3, 4], ids=["None", "0", "1", "2", "3", "4"] ) @pytest.mark.parametrize( "k", @@ -307,7 +305,6 @@ def test_trace(array, offset, type, dtype): def test_tri(N, M, k, dtype): func = lambda xp: xp.tri(N, M, k, dtype=dtype) assert_array_equal(func(dpnp), func(numpy)) - assert func(dpnp).shape == func(numpy).shape def test_tri_default_dtype():