From 05402b0c75b690676dacb69240dbb1be0970d9c5 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Thu, 2 Feb 2023 09:57:27 -0600 Subject: [PATCH 1/4] Use tril() function from dpctl.tensor --- dpnp/backend/include/dpnp_iface_fptr.hpp | 1 - .../kernels/dpnp_krnl_arraycreation.cpp | 16 ------- dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx | 45 ------------------- dpnp/dpnp_container.py | 7 +++ dpnp/dpnp_iface.py | 13 +++++- dpnp/dpnp_iface_arraycreation.py | 22 ++++++--- tests/skipped_tests.tbl | 3 ++ tests/skipped_tests_gpu.tbl | 3 ++ tests/test_arraycreation.py | 21 ++++----- .../cupy/creation_tests/test_matrix.py | 4 +- 10 files changed, 51 insertions(+), 84 deletions(-) diff --git a/dpnp/backend/include/dpnp_iface_fptr.hpp b/dpnp/backend/include/dpnp_iface_fptr.hpp index f77a37aade89..a6e6b6fe502b 100644 --- a/dpnp/backend/include/dpnp_iface_fptr.hpp +++ b/dpnp/backend/include/dpnp_iface_fptr.hpp @@ -370,7 +370,6 @@ enum class DPNPFuncName : size_t 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_TRIL_EXT, /**< Used in numpy.tril() impl, requires extra parameters */ DPNP_FN_TRIU, /**< Used in numpy.triu() impl */ DPNP_FN_TRIU_EXT, /**< Used in numpy.triu() impl, requires extra parameters */ DPNP_FN_TRUNC, /**< Used in numpy.trunc() impl */ diff --git a/dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp b/dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp index 8727e37fafc2..45be43021c36 100644 --- a/dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp +++ b/dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp @@ -1055,17 +1055,6 @@ void (*dpnp_tril_default_c)(void*, const size_t, const size_t) = dpnp_tril_c<_DataType>; -template -DPCTLSyclEventRef (*dpnp_tril_ext_c)(DPCTLSyclQueueRef, - void*, - void*, - const int, - shape_elem_type*, - shape_elem_type*, - const size_t, - const size_t, - const DPCTLEventVectorRef) = dpnp_tril_c<_DataType>; - template DPCTLSyclEventRef dpnp_triu_c(DPCTLSyclQueueRef q_ref, void* array_in, @@ -1439,11 +1428,6 @@ void func_map_init_arraycreation(func_map_t& fmap) fmap[DPNPFuncName::DPNP_FN_TRIL][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_tril_default_c}; fmap[DPNPFuncName::DPNP_FN_TRIL][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_tril_default_c}; - fmap[DPNPFuncName::DPNP_FN_TRIL_EXT][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_tril_ext_c}; - fmap[DPNPFuncName::DPNP_FN_TRIL_EXT][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_tril_ext_c}; - fmap[DPNPFuncName::DPNP_FN_TRIL_EXT][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_tril_ext_c}; - fmap[DPNPFuncName::DPNP_FN_TRIL_EXT][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_tril_ext_c}; - fmap[DPNPFuncName::DPNP_FN_TRIU][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_triu_default_c}; fmap[DPNPFuncName::DPNP_FN_TRIU][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_triu_default_c}; fmap[DPNPFuncName::DPNP_FN_TRIU][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_triu_default_c}; diff --git a/dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx b/dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx index 6986bf0ec702..b0e08d857b1f 100644 --- a/dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx +++ b/dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx @@ -45,7 +45,6 @@ __all__ += [ "dpnp_ptp", "dpnp_trace", "dpnp_tri", - "dpnp_tril", "dpnp_triu", "dpnp_vander", ] @@ -426,50 +425,6 @@ cpdef utils.dpnp_descriptor dpnp_tri(N, M=None, k=0, dtype=dpnp.float): return result -cpdef utils.dpnp_descriptor dpnp_tril(utils.dpnp_descriptor m, int k): - cdef shape_type_c input_shape = m.shape - cdef shape_type_c result_shape - - if m.ndim == 1: - result_shape = (m.shape[0], m.shape[0]) - else: - result_shape = m.shape - - cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(m.dtype) - cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_TRIL_EXT, param1_type, param1_type) - - m_obj = m.get_array() - - # ceate result array with type given by FPTR data - cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape, - kernel_data.return_type, - None, - device=m_obj.sycl_device, - usm_type=m_obj.usm_type, - sycl_queue=m_obj.sycl_queue) - - 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_1in_1out_func_ptr_t func = kernel_data.ptr - cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, - m.get_data(), - result.get_data(), - k, - input_shape.data(), - result_shape.data(), - m.ndim, - result.ndim, - NULL) # dep_events_ref - - with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref) - c_dpctl.DPCTLEvent_Delete(event_ref) - - return result - - cpdef utils.dpnp_descriptor dpnp_triu(utils.dpnp_descriptor m, int k): cdef shape_type_c input_shape = m.shape cdef shape_type_c result_shape diff --git a/dpnp/dpnp_container.py b/dpnp/dpnp_container.py index 7065e497652d..16f7e6fceec8 100644 --- a/dpnp/dpnp_container.py +++ b/dpnp/dpnp_container.py @@ -48,6 +48,7 @@ "eye", "full", "ones" + "tril" "zeros", ] @@ -200,6 +201,12 @@ def ones(shape, return dpnp_array(array_obj.shape, buffer=array_obj, order=order) +def tril(x1, /, *, k=0): + """"Creates `dpnp_array` as lower triangle of an input array.""" + array_obj = dpt.tril(x1.get_array() if isinstance(x1, dpnp_array) else x1, k) + return dpnp_array(array_obj.shape, buffer=array_obj) + + def zeros(shape, *, dtype=None, diff --git a/dpnp/dpnp_iface.py b/dpnp/dpnp_iface.py index 4806b511aff4..ec3c7fc19d1a 100644 --- a/dpnp/dpnp_iface.py +++ b/dpnp/dpnp_iface.py @@ -66,7 +66,8 @@ "dpnp_queue_is_cpu", "get_dpnp_descriptor", "get_include", - "get_normalized_queue_device" + "get_normalized_queue_device", + "isarray" ] from dpnp import ( @@ -338,3 +339,13 @@ def get_normalized_queue_device(obj=None, if hasattr(dpt._device, 'normalize_queue_device'): return dpt._device.normalize_queue_device(sycl_queue=sycl_queue, device=device) return sycl_queue + + +def isarray(obj): + """ + Return True if: + `obj` has a supported array type + Return False if: + `obj` has an unsupported array type or other data type + """ + return isinstance(obj, (dpnp_array, dpt.usm_ndarray)) diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 01b9ac6b792f..3c36cb84939a 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -1331,7 +1331,7 @@ def tri(N, M=None, k=0, dtype=dpnp.float, **kwargs): return call_origin(numpy.tri, N, M, k, dtype, **kwargs) -def tril(x1, k=0): +def tril(x1, /, *, k=0): """ Lower triangle of an array. @@ -1339,6 +1339,12 @@ def tril(x1, k=0): For full documentation refer to :obj:`numpy.tril`. + Limitations + ----------- + Parameter ``x1`` is supported only as :class:`dpnp.dpnp_array` with two or more dimensions. + Parameter ``k`` is supported only as int data type. + Otherwise the function will be executed sequentially on CPU. + Examples -------- >>> import dpnp as np @@ -1350,12 +1356,14 @@ def tril(x1, k=0): """ - x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False) - if x1_desc: - if not isinstance(k, int): - pass - else: - return dpnp_tril(x1_desc, k).get_pyobj() + if not dpnp.isarray(x1): + pass + elif x1.ndim < 2: + pass + elif not isinstance(k, int): + pass + else: + return dpnp_container.tril(x1, k=k) return call_origin(numpy.tril, x1, k) diff --git a/tests/skipped_tests.tbl b/tests/skipped_tests.tbl index 63c6cbd0d133..6b9479d4166b 100644 --- a/tests/skipped_tests.tbl +++ b/tests/skipped_tests.tbl @@ -430,6 +430,9 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_ tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_mixed_start_stop tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_mixed_start_stop2 tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_start_stop_list +tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril +tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_nega +tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_posi tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_AxisConcatenator_init1 tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_len tests/third_party/cupy/indexing_tests/test_generate.py::TestC_::test_c_1 diff --git a/tests/skipped_tests_gpu.tbl b/tests/skipped_tests_gpu.tbl index c64c7fa45f99..ba4b3713355d 100644 --- a/tests/skipped_tests_gpu.tbl +++ b/tests/skipped_tests_gpu.tbl @@ -190,6 +190,9 @@ tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_cons tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_tuple tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extraction_from_nested_list tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extraction_from_nested_tuple +tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril +tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_nega +tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_posi tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_4_{shape=(3, 3), val=(2,), wrap=True}::test_1darray tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_4_{shape=(3, 3), val=(2,), wrap=True}::test_fill_diagonal diff --git a/tests/test_arraycreation.py b/tests/test_arraycreation.py index d428b1ab7260..ac2e6f23aa7d 100644 --- a/tests/test_arraycreation.py +++ b/tests/test_arraycreation.py @@ -258,28 +258,25 @@ def test_tri_default_dtype(): @pytest.mark.parametrize("k", - [-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6], - ids=['-6', '-5', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4', '5', '6']) + [-3, -2, -1, 0, 1, 2, 3, 4, 5], + ids=['-3', '-2', '-1', '0', '1', '2', '3', '4', '5']) @pytest.mark.parametrize("m", - [[0, 1, 2, 3, 4], - [1, 1, 1, 1, 1], - [[0, 0], [0, 0]], + [[[0, 0], [0, 0]], [[1, 2], [1, 2]], [[1, 2], [3, 4]], [[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]], - ids=['[0, 1, 2, 3, 4]', - '[1, 1, 1, 1, 1]', - '[[0, 0], [0, 0]]', + ids=['[[0, 0], [0, 0]]', '[[1, 2], [1, 2]]', '[[1, 2], [3, 4]]', '[[0, 1, 2], [3, 4, 5], [6, 7, 8]]', '[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]']) -def test_tril(m, k): - a = numpy.array(m) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False)) +def test_tril(m, k, dtype): + a = numpy.array(m, dtype=dtype) ia = dpnp.array(a) - expected = numpy.tril(a, k) - result = dpnp.tril(ia, k) + expected = numpy.tril(a, k=k) + result = dpnp.tril(ia, k=k) assert_array_equal(expected, result) diff --git a/tests/third_party/cupy/creation_tests/test_matrix.py b/tests/third_party/cupy/creation_tests/test_matrix.py index a5471f213ebf..b7cac893ef7d 100644 --- a/tests/third_party/cupy/creation_tests/test_matrix.py +++ b/tests/third_party/cupy/creation_tests/test_matrix.py @@ -157,13 +157,13 @@ def test_tril_array_like(self, xp): @testing.numpy_cupy_array_equal() def test_tril_nega(self, xp, dtype): m = testing.shaped_arange(self.shape, xp, dtype) - return xp.tril(m, -1) + return xp.tril(m, k=-1) @testing.for_all_dtypes(no_complex=True) @testing.numpy_cupy_array_equal() def test_tril_posi(self, xp, dtype): m = testing.shaped_arange(self.shape, xp, dtype) - return xp.tril(m, 1) + return xp.tril(m, k=1) @testing.for_all_dtypes(no_complex=True) @testing.numpy_cupy_array_equal() From 76dafc18913fcbf7d8b55f1ec99064ba62fa011f Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Mon, 6 Feb 2023 05:15:07 -0600 Subject: [PATCH 2/4] Use triu() function from dpctl.tensor --- dpnp/backend/include/dpnp_iface_fptr.hpp | 1 - .../kernels/dpnp_krnl_arraycreation.cpp | 16 ------- dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx | 45 ------------------- dpnp/dpnp_container.py | 18 ++++++-- dpnp/dpnp_iface.py | 13 +----- dpnp/dpnp_iface_arraycreation.py | 31 ++++++++----- tests/skipped_tests.tbl | 3 ++ tests/skipped_tests_gpu.tbl | 3 ++ tests/test_arraycreation.py | 19 ++++---- tests/test_sycl_queue.py | 10 +++++ tests/test_usm_type.py | 7 +++ .../cupy/creation_tests/test_matrix.py | 4 +- 12 files changed, 71 insertions(+), 99 deletions(-) diff --git a/dpnp/backend/include/dpnp_iface_fptr.hpp b/dpnp/backend/include/dpnp_iface_fptr.hpp index a6e6b6fe502b..ba3311bc460c 100644 --- a/dpnp/backend/include/dpnp_iface_fptr.hpp +++ b/dpnp/backend/include/dpnp_iface_fptr.hpp @@ -371,7 +371,6 @@ enum class DPNPFuncName : size_t 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_TRIU_EXT, /**< Used in numpy.triu() impl, requires extra parameters */ DPNP_FN_TRUNC, /**< Used in numpy.trunc() impl */ DPNP_FN_TRUNC_EXT, /**< Used in numpy.trunc() impl, requires extra parameters */ DPNP_FN_VANDER, /**< Used in numpy.vander() impl */ diff --git a/dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp b/dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp index 45be43021c36..a29fcca0975b 100644 --- a/dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp +++ b/dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp @@ -1207,17 +1207,6 @@ void (*dpnp_triu_default_c)(void*, const size_t, const size_t) = dpnp_triu_c<_DataType>; -template -DPCTLSyclEventRef (*dpnp_triu_ext_c)(DPCTLSyclQueueRef, - void*, - void*, - const int, - shape_elem_type*, - shape_elem_type*, - const size_t, - const size_t, - const DPCTLEventVectorRef) = dpnp_triu_c<_DataType>; - template DPCTLSyclEventRef dpnp_zeros_c(DPCTLSyclQueueRef q_ref, void* result, @@ -1433,11 +1422,6 @@ void func_map_init_arraycreation(func_map_t& fmap) fmap[DPNPFuncName::DPNP_FN_TRIU][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_triu_default_c}; fmap[DPNPFuncName::DPNP_FN_TRIU][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_triu_default_c}; - fmap[DPNPFuncName::DPNP_FN_TRIU_EXT][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_triu_ext_c}; - fmap[DPNPFuncName::DPNP_FN_TRIU_EXT][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_triu_ext_c}; - fmap[DPNPFuncName::DPNP_FN_TRIU_EXT][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_triu_ext_c}; - fmap[DPNPFuncName::DPNP_FN_TRIU_EXT][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_triu_ext_c}; - fmap[DPNPFuncName::DPNP_FN_ZEROS][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_zeros_default_c}; fmap[DPNPFuncName::DPNP_FN_ZEROS][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_zeros_default_c}; fmap[DPNPFuncName::DPNP_FN_ZEROS][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_zeros_default_c}; diff --git a/dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx b/dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx index b0e08d857b1f..cb44a08db598 100644 --- a/dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx +++ b/dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx @@ -45,7 +45,6 @@ __all__ += [ "dpnp_ptp", "dpnp_trace", "dpnp_tri", - "dpnp_triu", "dpnp_vander", ] @@ -425,50 +424,6 @@ cpdef utils.dpnp_descriptor dpnp_tri(N, M=None, k=0, dtype=dpnp.float): return result -cpdef utils.dpnp_descriptor dpnp_triu(utils.dpnp_descriptor m, int k): - cdef shape_type_c input_shape = m.shape - cdef shape_type_c result_shape - - if m.ndim == 1: - result_shape = (m.shape[0], m.shape[0]) - else: - result_shape = m.shape - - cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(m.dtype) - cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_TRIU_EXT, param1_type, param1_type) - - m_obj = m.get_array() - - # ceate result array with type given by FPTR data - cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape, - kernel_data.return_type, - None, - device=m_obj.sycl_device, - usm_type=m_obj.usm_type, - sycl_queue=m_obj.sycl_queue) - - 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_1in_1out_func_ptr_t func = kernel_data.ptr - cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, - m.get_data(), - result.get_data(), - k, - input_shape.data(), - result_shape.data(), - m.ndim, - result.ndim, - NULL) # dep_events_ref - - 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_container.py b/dpnp/dpnp_container.py index 16f7e6fceec8..5ab2b7b7f0cc 100644 --- a/dpnp/dpnp_container.py +++ b/dpnp/dpnp_container.py @@ -39,6 +39,7 @@ from dpnp.dpnp_array import dpnp_array import dpnp +import operator __all__ = [ @@ -48,7 +49,8 @@ "eye", "full", "ones" - "tril" + "tril", + "triu", "zeros", ] @@ -202,9 +204,19 @@ def ones(shape, def tril(x1, /, *, k=0): - """"Creates `dpnp_array` as lower triangle of an input array.""" + k = operator.index(k) + order = "F" if (x1.flags.f_contiguous) else "C" + """"Creates `dpnp_array` as lower triangular part of an input array.""" array_obj = dpt.tril(x1.get_array() if isinstance(x1, dpnp_array) else x1, k) - return dpnp_array(array_obj.shape, buffer=array_obj) + return dpnp_array(array_obj.shape, buffer=array_obj, order=order) + + +def triu(x1, /, *, k=0): + k = operator.index(k) + order = "F" if (x1.flags.f_contiguous) else "C" + """"Creates `dpnp_array` as upper triangular part of an input array.""" + array_obj = dpt.triu(x1.get_array() if isinstance(x1, dpnp_array) else x1, k) + return dpnp_array(array_obj.shape, buffer=array_obj, order=order) def zeros(shape, diff --git a/dpnp/dpnp_iface.py b/dpnp/dpnp_iface.py index ec3c7fc19d1a..4806b511aff4 100644 --- a/dpnp/dpnp_iface.py +++ b/dpnp/dpnp_iface.py @@ -66,8 +66,7 @@ "dpnp_queue_is_cpu", "get_dpnp_descriptor", "get_include", - "get_normalized_queue_device", - "isarray" + "get_normalized_queue_device" ] from dpnp import ( @@ -339,13 +338,3 @@ def get_normalized_queue_device(obj=None, if hasattr(dpt._device, 'normalize_queue_device'): return dpt._device.normalize_queue_device(sycl_queue=sycl_queue, device=device) return sycl_queue - - -def isarray(obj): - """ - Return True if: - `obj` has a supported array type - Return False if: - `obj` has an unsupported array type or other data type - """ - return isinstance(obj, (dpnp_array, dpt.usm_ndarray)) diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 3c36cb84939a..c6a6fd7c0e50 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -48,6 +48,7 @@ from dpnp.dpnp_utils import * import dpnp.dpnp_container as dpnp_container +import dpctl.tensor as dpt __all__ = [ @@ -1341,8 +1342,8 @@ def tril(x1, /, *, k=0): Limitations ----------- - Parameter ``x1`` is supported only as :class:`dpnp.dpnp_array` with two or more dimensions. - Parameter ``k`` is supported only as int data type. + Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray` with two or more dimensions. + Parameter `k` is supported only of integer data type. Otherwise the function will be executed sequentially on CPU. Examples @@ -1356,19 +1357,20 @@ def tril(x1, /, *, k=0): """ - if not dpnp.isarray(x1): + if not isinstance(x1, (dpnp.ndarray, dpt.usm_ndarray)): pass elif x1.ndim < 2: pass elif not isinstance(k, int): - pass + if not (isinstance(k, (dpnp.ndarray, dpt.usm_ndarray)) and numpy.issubdtype(k.dtype, int)): + pass else: return dpnp_container.tril(x1, k=k) return call_origin(numpy.tril, x1, k) -def triu(x1, k=0): +def triu(x1, /, *, k=0): """ Upper triangle of an array. @@ -1377,6 +1379,12 @@ def triu(x1, k=0): For full documentation refer to :obj:`numpy.triu`. + Limitations + ----------- + Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray` with two or more dimensions. + Parameter `k` is supported only of integer data type. + Otherwise the function will be executed sequentially on CPU. + Examples -------- >>> import dpnp as np @@ -1388,12 +1396,15 @@ def triu(x1, k=0): """ - x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False) - if x1_desc: - if not isinstance(k, int): + if not isinstance(x1, (dpnp.ndarray, dpt.usm_ndarray)): + pass + elif x1.ndim < 2: + pass + elif not isinstance(k, int): + if not (isinstance(k, (dpnp.ndarray, dpt.usm_ndarray)) and numpy.issubdtype(k.dtype, int)): pass - else: - return dpnp_triu(x1_desc, k).get_pyobj() + else: + return dpnp_container.triu(x1, k=k) return call_origin(numpy.triu, x1, k) diff --git a/tests/skipped_tests.tbl b/tests/skipped_tests.tbl index 6b9479d4166b..1a88b6ae36e0 100644 --- a/tests/skipped_tests.tbl +++ b/tests/skipped_tests.tbl @@ -433,6 +433,9 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_ tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_nega tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_posi +tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu +tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu_nega +tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu_posi tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_AxisConcatenator_init1 tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_len tests/third_party/cupy/indexing_tests/test_generate.py::TestC_::test_c_1 diff --git a/tests/skipped_tests_gpu.tbl b/tests/skipped_tests_gpu.tbl index ba4b3713355d..b20dba660909 100644 --- a/tests/skipped_tests_gpu.tbl +++ b/tests/skipped_tests_gpu.tbl @@ -193,6 +193,9 @@ tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extr tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_nega tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_posi +tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu +tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu_nega +tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu_posi tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_4_{shape=(3, 3), val=(2,), wrap=True}::test_1darray tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_4_{shape=(3, 3), val=(2,), wrap=True}::test_fill_diagonal diff --git a/tests/test_arraycreation.py b/tests/test_arraycreation.py index ac2e6f23aa7d..5387d97b7787 100644 --- a/tests/test_arraycreation.py +++ b/tests/test_arraycreation.py @@ -281,22 +281,21 @@ def test_tril(m, k, dtype): @pytest.mark.parametrize("k", - [-4, -3, -2, -1, 0, 1, 2, 3, 4], - ids=['-4', '-3', '-2', '-1', '0', '1', '2', '3', '4']) + [-3, -2, -1, 0, 1, 2, 3, 4, 5], + ids=['-3', '-2', '-1', '0', '1', '2', '3', '4', '5']) @pytest.mark.parametrize("m", - [[0, 1, 2, 3, 4], - [[1, 2], [3, 4]], + [[[1, 2], [3, 4]], [[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]], - ids=['[0, 1, 2, 3, 4]', - '[[1, 2], [3, 4]]', + ids=['[[1, 2], [3, 4]]', '[[0, 1, 2], [3, 4, 5], [6, 7, 8]]', '[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]']) -def test_triu(m, k): - a = numpy.array(m) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False)) +def test_triu(m, k, dtype): + a = numpy.array(m, dtype=dtype) ia = dpnp.array(a) - expected = numpy.triu(a, k) - result = dpnp.triu(ia, k) + expected = numpy.triu(a, k=k) + result = dpnp.triu(ia, k=k) assert_array_equal(expected, result) diff --git a/tests/test_sycl_queue.py b/tests/test_sycl_queue.py index 413596e2cc76..aee4e848d8bc 100644 --- a/tests/test_sycl_queue.py +++ b/tests/test_sycl_queue.py @@ -161,6 +161,16 @@ def test_array_creation_like(func, kwargs, device_x, device_y): assert_sycl_queue_equal(y.sycl_queue, x.to_device(device_y).sycl_queue) +@pytest.mark.parametrize("func", ["tril", "triu"], ids=["tril", "triu"]) +@pytest.mark.parametrize("device", + valid_devices, + ids=[device.filter_string for device in valid_devices]) +def test_tril_triu(func, device): + x0 = dpnp.ones((3,3), device=device) + x = getattr(dpnp, func)(x0) + assert x.sycl_device == device + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") @pytest.mark.parametrize( "func,data", diff --git a/tests/test_usm_type.py b/tests/test_usm_type.py index 094fe419c263..9aeba1cdc9df 100644 --- a/tests/test_usm_type.py +++ b/tests/test_usm_type.py @@ -61,3 +61,10 @@ def test_array_creation(func, args, usm_type_x, usm_type_y): assert x.usm_type == usm_type_x assert y.usm_type == usm_type_y + +@pytest.mark.parametrize("func", ["tril", "triu"], ids=["tril", "triu"]) +@pytest.mark.parametrize("usm_type", list_of_usm_types, ids=list_of_usm_types) +def test_tril_triu(func, usm_type): + x0 = dp.ones((3,3), usm_type=usm_type) + x = getattr(dp, func)(x0) + assert x.usm_type == usm_type diff --git a/tests/third_party/cupy/creation_tests/test_matrix.py b/tests/third_party/cupy/creation_tests/test_matrix.py index b7cac893ef7d..ba03c052c7e7 100644 --- a/tests/third_party/cupy/creation_tests/test_matrix.py +++ b/tests/third_party/cupy/creation_tests/test_matrix.py @@ -180,10 +180,10 @@ def test_triu_array_like(self, xp): @testing.numpy_cupy_array_equal() def test_triu_nega(self, xp, dtype): m = testing.shaped_arange(self.shape, xp, dtype) - return xp.triu(m, -1) + return xp.triu(m, k=-1) @testing.for_all_dtypes(no_complex=True) @testing.numpy_cupy_array_equal() def test_triu_posi(self, xp, dtype): m = testing.shaped_arange(self.shape, xp, dtype) - return xp.triu(m, 1) + return xp.triu(m, k=1) From 6e22482afcd257bb45e299baf0a0fc5650f8cbd4 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Mon, 6 Feb 2023 14:05:17 -0600 Subject: [PATCH 3/4] Changed tests for tril() and triu() functions. --- dpnp/dpnp_container.py | 9 ++----- dpnp/dpnp_iface_arraycreation.py | 27 +++++++++++++------ tests/skipped_tests.tbl | 6 ----- tests/skipped_tests_gpu.tbl | 6 ----- tests/test_arraycreation.py | 21 +++++++++------ tests/test_sycl_queue.py | 2 +- .../cupy/creation_tests/test_matrix.py | 3 +-- 7 files changed, 36 insertions(+), 38 deletions(-) diff --git a/dpnp/dpnp_container.py b/dpnp/dpnp_container.py index 5ab2b7b7f0cc..75e20f8a0cb6 100644 --- a/dpnp/dpnp_container.py +++ b/dpnp/dpnp_container.py @@ -39,7 +39,6 @@ from dpnp.dpnp_array import dpnp_array import dpnp -import operator __all__ = [ @@ -204,19 +203,15 @@ def ones(shape, def tril(x1, /, *, k=0): - k = operator.index(k) - order = "F" if (x1.flags.f_contiguous) else "C" """"Creates `dpnp_array` as lower triangular part of an input array.""" array_obj = dpt.tril(x1.get_array() if isinstance(x1, dpnp_array) else x1, k) - return dpnp_array(array_obj.shape, buffer=array_obj, order=order) + return dpnp_array(array_obj.shape, buffer=array_obj, order="K") def triu(x1, /, *, k=0): - k = operator.index(k) - order = "F" if (x1.flags.f_contiguous) else "C" """"Creates `dpnp_array` as upper triangular part of an input array.""" array_obj = dpt.triu(x1.get_array() if isinstance(x1, dpnp_array) else x1, k) - return dpnp_array(array_obj.shape, buffer=array_obj, order=order) + return dpnp_array(array_obj.shape, buffer=array_obj, order="K") def zeros(shape, diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index c6a6fd7c0e50..8e43d3060211 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -42,6 +42,7 @@ import numpy import dpnp +import operator import dpnp.config as config from dpnp.dpnp_algo import * @@ -1357,15 +1358,20 @@ def tril(x1, /, *, k=0): """ + _k = None + try: + _k = operator.index(k) + except TypeError: + pass + if not isinstance(x1, (dpnp.ndarray, dpt.usm_ndarray)): pass elif x1.ndim < 2: pass - elif not isinstance(k, int): - if not (isinstance(k, (dpnp.ndarray, dpt.usm_ndarray)) and numpy.issubdtype(k.dtype, int)): - pass + elif _k is None: + pass else: - return dpnp_container.tril(x1, k=k) + return dpnp_container.tril(x1, k=_k) return call_origin(numpy.tril, x1, k) @@ -1396,15 +1402,20 @@ def triu(x1, /, *, k=0): """ + _k = None + try: + _k = operator.index(k) + except TypeError: + pass + if not isinstance(x1, (dpnp.ndarray, dpt.usm_ndarray)): pass elif x1.ndim < 2: pass - elif not isinstance(k, int): - if not (isinstance(k, (dpnp.ndarray, dpt.usm_ndarray)) and numpy.issubdtype(k.dtype, int)): - pass + elif _k is None: + pass else: - return dpnp_container.triu(x1, k=k) + return dpnp_container.triu(x1, k=_k) return call_origin(numpy.triu, x1, k) diff --git a/tests/skipped_tests.tbl b/tests/skipped_tests.tbl index 1a88b6ae36e0..63c6cbd0d133 100644 --- a/tests/skipped_tests.tbl +++ b/tests/skipped_tests.tbl @@ -430,12 +430,6 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_ tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_mixed_start_stop tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_mixed_start_stop2 tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_start_stop_list -tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril -tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_nega -tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_posi -tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu -tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu_nega -tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu_posi tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_AxisConcatenator_init1 tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_len tests/third_party/cupy/indexing_tests/test_generate.py::TestC_::test_c_1 diff --git a/tests/skipped_tests_gpu.tbl b/tests/skipped_tests_gpu.tbl index b20dba660909..c64c7fa45f99 100644 --- a/tests/skipped_tests_gpu.tbl +++ b/tests/skipped_tests_gpu.tbl @@ -190,12 +190,6 @@ tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_cons tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_tuple tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extraction_from_nested_list tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extraction_from_nested_tuple -tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril -tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_nega -tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_posi -tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu -tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu_nega -tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu_posi tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_4_{shape=(3, 3), val=(2,), wrap=True}::test_1darray tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_4_{shape=(3, 3), val=(2,), wrap=True}::test_fill_diagonal diff --git a/tests/test_arraycreation.py b/tests/test_arraycreation.py index 5387d97b7787..5c1458b06730 100644 --- a/tests/test_arraycreation.py +++ b/tests/test_arraycreation.py @@ -15,6 +15,7 @@ ) import tempfile +import operator @pytest.mark.parametrize("start", @@ -258,8 +259,10 @@ def test_tri_default_dtype(): @pytest.mark.parametrize("k", - [-3, -2, -1, 0, 1, 2, 3, 4, 5], - ids=['-3', '-2', '-1', '0', '1', '2', '3', '4', '5']) + [-3, -2, -1, 0, 1, 2, 3, 4, 5, + numpy.array(1), dpnp.array(2), dpt.asarray(3)], + ids=['-3', '-2', '-1', '0', '1', '2', '3', '4', '5', + 'np.array(1)', 'dpnp.array(2)', 'dpt.asarray(3)']) @pytest.mark.parametrize("m", [[[0, 0], [0, 0]], [[1, 2], [1, 2]], @@ -275,14 +278,16 @@ def test_tri_default_dtype(): def test_tril(m, k, dtype): a = numpy.array(m, dtype=dtype) ia = dpnp.array(a) - expected = numpy.tril(a, k=k) + expected = numpy.tril(a, k=operator.index(k)) result = dpnp.tril(ia, k=k) assert_array_equal(expected, result) @pytest.mark.parametrize("k", - [-3, -2, -1, 0, 1, 2, 3, 4, 5], - ids=['-3', '-2', '-1', '0', '1', '2', '3', '4', '5']) + [-3, -2, -1, 0, 1, 2, 3, 4, 5, + numpy.array(1), dpnp.array(2), dpt.asarray(3)], + ids=['-3', '-2', '-1', '0', '1', '2', '3', '4', '5', + 'np.array(1)', 'dpnp.array(2)', 'dpt.asarray(3)']) @pytest.mark.parametrize("m", [[[1, 2], [3, 4]], [[0, 1, 2], [3, 4, 5], [6, 7, 8]], @@ -294,7 +299,7 @@ def test_tril(m, k, dtype): def test_triu(m, k, dtype): a = numpy.array(m, dtype=dtype) ia = dpnp.array(a) - expected = numpy.triu(a, k=k) + expected = numpy.triu(a, k=operator.index(k)) result = dpnp.triu(ia, k=k) assert_array_equal(expected, result) @@ -305,8 +310,8 @@ def test_triu(m, k, dtype): def test_triu_size_null(k): a = numpy.ones(shape=(1, 2, 0)) ia = dpnp.array(a) - expected = numpy.triu(a, k) - result = dpnp.triu(ia, k) + expected = numpy.triu(a, k=k) + result = dpnp.triu(ia, k=k) assert_array_equal(expected, result) diff --git a/tests/test_sycl_queue.py b/tests/test_sycl_queue.py index aee4e848d8bc..f068e5c6935b 100644 --- a/tests/test_sycl_queue.py +++ b/tests/test_sycl_queue.py @@ -168,7 +168,7 @@ def test_array_creation_like(func, kwargs, device_x, device_y): def test_tril_triu(func, device): x0 = dpnp.ones((3,3), device=device) x = getattr(dpnp, func)(x0) - assert x.sycl_device == device + assert_sycl_queue_equal(x.sycl_queue, x0.sycl_queue) @pytest.mark.usefixtures("allow_fall_back_on_numpy") diff --git a/tests/third_party/cupy/creation_tests/test_matrix.py b/tests/third_party/cupy/creation_tests/test_matrix.py index ba03c052c7e7..fe144cbc58c4 100644 --- a/tests/third_party/cupy/creation_tests/test_matrix.py +++ b/tests/third_party/cupy/creation_tests/test_matrix.py @@ -140,6 +140,7 @@ 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) @@ -148,7 +149,6 @@ def test_tril(self, xp, dtype): m = testing.shaped_arange(self.shape, xp, dtype) return xp.tril(m) - @pytest.mark.usefixtures("allow_fall_back_on_numpy") @testing.numpy_cupy_array_equal() def test_tril_array_like(self, xp): return xp.tril([[1, 2], [3, 4]]) @@ -171,7 +171,6 @@ def test_triu(self, xp, dtype): m = testing.shaped_arange(self.shape, xp, dtype) return xp.triu(m) - @pytest.mark.usefixtures("allow_fall_back_on_numpy") @testing.numpy_cupy_array_equal() def test_triu_array_like(self, xp): return xp.triu([[1, 2], [3, 4]]) From 60ab25372aa6493a1d09ec7443ee846b5f650282 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Wed, 15 Feb 2023 15:31:43 -0600 Subject: [PATCH 4/4] Skip tests for tril() and triu() functions with usm_type. --- tests/test_usm_type.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_usm_type.py b/tests/test_usm_type.py index bfeede66ddb3..91831648813d 100644 --- a/tests/test_usm_type.py +++ b/tests/test_usm_type.py @@ -64,6 +64,7 @@ def test_array_creation(func, args, usm_type_x, usm_type_y): assert y.usm_type == usm_type_y +@pytest.mark.skip() @pytest.mark.parametrize("func", ["tril", "triu"], ids=["tril", "triu"]) @pytest.mark.parametrize("usm_type", list_of_usm_types, ids=list_of_usm_types) def test_tril_triu(func, usm_type):