From b92cb12b876c928d3cfe2512676c97ab4afb5dcc Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Tue, 9 Jul 2024 02:19:20 +0000 Subject: [PATCH 1/2] Resolves gh-1724 Moves `order` validation to the start of `_from_input_shape_strides` --- dpctl/tensor/_stride_utils.pxi | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/dpctl/tensor/_stride_utils.pxi b/dpctl/tensor/_stride_utils.pxi index 55361d8041..ebbfd4ec6d 100644 --- a/dpctl/tensor/_stride_utils.pxi +++ b/dpctl/tensor/_stride_utils.pxi @@ -72,6 +72,9 @@ cdef int _from_input_shape_strides( cdef Py_ssize_t* shape_arr cdef Py_ssize_t* strides_arr + if (int(order) not in [ord('C'), ord('F'), ord('c'), ord('f')]): + return ERROR_INCORRECT_ORDER + # 0-d array if (nd == 0): contig[0] = (USM_ARRAY_C_CONTIGUOUS | USM_ARRAY_F_CONTIGUOUS) @@ -109,10 +112,6 @@ cdef int _from_input_shape_strides( nelems[0] = elem_count if (strides is None): # no need to allocate and populate strides - if (int(order) not in [ord('C'), ord('F'), ord('c'), ord('f')]): - PyMem_Free(shape_ptr[0]); - shape_ptr[0] = (0) - return ERROR_INCORRECT_ORDER if order == ord('C') or order == ord('c'): contig[0] = USM_ARRAY_C_CONTIGUOUS else: From 19b573587e3cdf0b48727b9efae961941eb809e9 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Tue, 9 Jul 2024 07:09:32 +0000 Subject: [PATCH 2/2] Adds tests for fixed usm_ndarray ctor order validation --- dpctl/tests/test_usm_ndarray_ctor.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dpctl/tests/test_usm_ndarray_ctor.py b/dpctl/tests/test_usm_ndarray_ctor.py index 33a95af849..d9ce0eff50 100644 --- a/dpctl/tests/test_usm_ndarray_ctor.py +++ b/dpctl/tests/test_usm_ndarray_ctor.py @@ -485,6 +485,10 @@ def test_ctor_invalid_order(): get_queue_or_skip() with pytest.raises(ValueError): dpt.usm_ndarray((5, 5, 3), order="Z") + with pytest.raises(ValueError): + dpt.usm_ndarray((10), strides=(1,), order="Z") + with pytest.raises(ValueError): + dpt.usm_ndarray((), order="Z") def test_ctor_buffer_kwarg():