From e0baee152bef28aa56cd343c47937a95ebedf5ba Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Tue, 1 Oct 2024 11:56:00 -0500 Subject: [PATCH 1/5] update order --- dpnp/dpnp_iface.py | 16 +- dpnp/dpnp_iface_arraycreation.py | 192 +++++++++++------- tests/test_arraycreation.py | 30 ++- .../cupy/creation_tests/test_basic.py | 58 +++--- 4 files changed, 165 insertions(+), 131 deletions(-) diff --git a/dpnp/dpnp_iface.py b/dpnp/dpnp_iface.py index 62cd0683fe5a..041ec608aa19 100644 --- a/dpnp/dpnp_iface.py +++ b/dpnp/dpnp_iface.py @@ -320,14 +320,10 @@ def as_usm_ndarray(a, dtype=None, device=None, usm_type=None, sycl_queue=None): ) -def check_limitations( - order=None, subok=False, like=None, initial=None, where=True -): +def check_limitations(subok=False, like=None, initial=None, where=True): """ Checking limitation kwargs for their supported values. - Parameter `order` is only supported with values ``"C"``, ``"F"``, - and ``None``. Parameter `subok` is only supported with default value ``False``. Parameter `like` is only supported with default value ``None``. Parameter `initial` is only supported with default value ``None``. @@ -340,16 +336,6 @@ def check_limitations( """ - if order in ("A", "a", "K", "k"): - raise NotImplementedError( - "Keyword argument `order` is supported only with " - f"values 'C' and 'F', but got '{order}'" - ) - if order not in ("C", "c", "F", "f", None): - raise ValueError( - "Unrecognized `order` keyword value, expecting " - f"'C' or 'F', but got '{order}'" - ) if like is not None: raise NotImplementedError( "Keyword argument `like` is supported only with " diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 2c8f55dd57c9..73a1bd296b48 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -97,6 +97,78 @@ ] +def _get_empty_array( + a, + /, + *, + dtype=None, + order="K", + shape=None, + device=None, + usm_type=None, + sycl_queue=None, +): + """ + Get an empty array as the base for empty_like, ones_like, zeros_like, + and full_like. + + """ + strides = None + if shape is None: + _shape = a.shape + elif dpnp.isscalar(shape): + _shape = (shape,) + else: + _shape = 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 + ) + + if order is None: + order = "C" + elif order == "A": + if a.flags.fnc: + order = "F" + else: + order = "C" + elif order == "K": + if len(_shape) != a.ndim: + order = "C" + elif a.flags.f_contiguous: + order = "F" + elif a.flags.f_contiguous: + order = "C" + else: + strides = _get_strides_for_order_k(a, _shape) + order = "C" + + return dpnp_array( + _shape, + dtype=_dtype, + strides=strides, + order=order, + usm_type=_usm_type, + sycl_queue=_sycl_queue, + ) + + +def _get_strides_for_order_k(x, shape=None): + """ + Calculate strides when order='K' for empty_like, ones_like, zeros_like, + and full_like where `shape` is ``None`` or len(shape) == x.ndim. + + """ + stride_and_index = sorted([(abs(s), -i) for i, s in enumerate(x.strides)]) + strides = [0] * x.ndim + stride = 1 + for _, i in stride_and_index: + strides[-i] = stride + stride *= shape[-i] if shape else x.shape[-i] + return strides + + def arange( start, /, @@ -1206,7 +1278,7 @@ def empty_like( /, *, dtype=None, - order="C", + order="K", subok=False, shape=None, device=None, @@ -1227,9 +1299,9 @@ def empty_like( 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 : {None, "C", "F"}, optional + order : {None, "C", "F", "A", "K"}, optional Memory layout of the newly output array. - Default: ``"C"``. + Default: ``"K"``. shape : {None, int, sequence of ints} Overrides the shape of the result. device : {None, string, SyclDevice, SyclQueue}, optional @@ -1256,8 +1328,6 @@ 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 raises `NotImplementedError` exception. @@ -1295,20 +1365,16 @@ def empty_like( """ dpnp.check_supported_arrays_type(a) - dpnp.check_limitations(order=order, subok=subok) + dpnp.check_limitations(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.empty( - _shape, - dtype=_dtype, + return _get_empty_array( + a, + dtype=dtype, order=order, - usm_type=_usm_type, - sycl_queue=_sycl_queue, + shape=shape, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, ) @@ -2063,7 +2129,7 @@ def full_like( fill_value, *, dtype=None, - order="C", + order="K", subok=False, shape=None, device=None, @@ -2088,9 +2154,9 @@ def full_like( 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 : {None, "C", "F"}, optional + order : {None, "C", "F", "A", "K"}, optional Memory layout of the newly output array. - Default: ``"C"``. + Default: ``"K"``. shape : {None, int, sequence of ints} Overrides the shape of the result. device : {None, string, SyclDevice, SyclQueue}, optional @@ -2117,8 +2183,6 @@ def full_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 raises `NotImplementedError` exception. @@ -2156,23 +2220,19 @@ def full_like( """ dpnp.check_supported_arrays_type(a) - dpnp.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 - ) + dpnp.check_limitations(subok=subok) - return dpnp_container.full( - _shape, - fill_value, - dtype=_dtype, + res = _get_empty_array( + a, + dtype=dtype, order=order, - usm_type=_usm_type, - sycl_queue=_sycl_queue, + shape=shape, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, ) + dpnp.copyto(res, fill_value, casting="unsafe") + return res def geomspace( @@ -3112,7 +3172,7 @@ def ones_like( /, *, dtype=None, - order="C", + order="K", subok=False, shape=None, device=None, @@ -3133,9 +3193,9 @@ def ones_like( 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 : {None, "C", "F"}, optional + order : {None, "C", "F", "A", "K"}, optional Memory layout of the newly output array. - Default: ``"C"``. + Default: ``"K"``. shape : {None, int, sequence of ints} Overrides the shape of the result. device : {None, string, SyclDevice, SyclQueue}, optional @@ -3162,8 +3222,6 @@ 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 raises `NotImplementedError` exception. @@ -3202,21 +3260,19 @@ def ones_like( """ dpnp.check_supported_arrays_type(a) - dpnp.check_limitations(order=order, subok=subok) + dpnp.check_limitations(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.ones( - _shape, - dtype=_dtype, + res = _get_empty_array( + a, + dtype=dtype, order=order, - usm_type=_usm_type, - sycl_queue=_sycl_queue, + shape=shape, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, ) + res.fill(1) + return res def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None): @@ -3759,7 +3815,7 @@ def zeros_like( /, *, dtype=None, - order="C", + order="K", subok=False, shape=None, device=None, @@ -3780,9 +3836,9 @@ def zeros_like( 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 : {None, "C", "F"}, optional + order : {None, "C", "F", "A", "K"}, optional Memory layout of the newly output array. - Default: ``"C"``. + Default: ``"K"``. shape : {None, int, sequence of ints} Overrides the shape of the result. device : {None, string, SyclDevice, SyclQueue}, optional @@ -3809,8 +3865,6 @@ 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 raises `NotImplementedError` exception. @@ -3850,18 +3904,16 @@ def zeros_like( """ dpnp.check_supported_arrays_type(a) - dpnp.check_limitations(order=order, subok=subok) + dpnp.check_limitations(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, + res = _get_empty_array( + a, + dtype=dtype, order=order, - usm_type=_usm_type, - sycl_queue=_sycl_queue, + shape=shape, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, ) + res.fill(0) + return res diff --git a/tests/test_arraycreation.py b/tests/test_arraycreation.py index a0978107e4ea..6db3547d2a24 100644 --- a/tests/test_arraycreation.py +++ b/tests/test_arraycreation.py @@ -74,18 +74,6 @@ def test_out(self): pytest.param("full_like", [dpnp.ones(10), 7]), pytest.param("ones_like", [dpnp.ones(10)]), pytest.param("zeros_like", [dpnp.ones(10)]), - ], -) -def test_exception_order1(func, args): - with pytest.raises(NotImplementedError): - getattr(dpnp, func)(*args, order="K") - with pytest.raises(ValueError): - getattr(dpnp, func)(*args, order="S") - - -@pytest.mark.parametrize( - "func, args", - [ pytest.param("empty", [3]), pytest.param("eye", [3]), pytest.param("full", [3, 7]), @@ -93,7 +81,7 @@ def test_exception_order1(func, args): pytest.param("zeros", [3]), ], ) -def test_exception_order2(func, args): +def test_exception_order(func, args): with pytest.raises(ValueError): getattr(dpnp, func)(*args, order="S") @@ -552,7 +540,9 @@ def test_full(shape, fill_value, dtype, order): "fill_value", [1.5, 2, 1.5 + 0.0j], ids=["1.5", "2", "1.5+0.j"] ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False)) -@pytest.mark.parametrize("order", [None, "C", "F"], ids=["None", "C", "F"]) +@pytest.mark.parametrize( + "order", [None, "C", "F", "A", "K"], ids=["None", "C", "F", "A", "K"] +) def test_full_like(array, fill_value, dtype, order): func = lambda xp, x: xp.full_like(x, fill_value, dtype=dtype, order=order) @@ -611,7 +601,9 @@ def test_zeros(shape, dtype, order): ids=["[]", "0", "[1, 2, 3]", "[[1, 2], [3, 4]]"], ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False)) -@pytest.mark.parametrize("order", [None, "C", "F"], ids=["None", "C", "F"]) +@pytest.mark.parametrize( + "order", [None, "C", "F", "A", "K"], ids=["None", "C", "F", "A", "K"] +) def test_zeros_like(array, dtype, order): func = lambda xp, x: xp.zeros_like(x, dtype=dtype, order=order) @@ -638,7 +630,9 @@ def test_empty(shape, dtype, order): ids=["[]", "0", "[1, 2, 3]", "[[1, 2], [3, 4]]"], ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False)) -@pytest.mark.parametrize("order", [None, "C", "F"], ids=["None", "C", "F"]) +@pytest.mark.parametrize( + "order", [None, "C", "F", "A", "K"], ids=["None", "C", "F", "A", "K"] +) def test_empty_like(array, dtype, order): func = lambda xp, x: xp.empty_like(x, dtype=dtype, order=order) @@ -665,7 +659,9 @@ def test_ones(shape, dtype, order): ids=["[]", "0", "[1, 2, 3]", "[[1, 2], [3, 4]]"], ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False)) -@pytest.mark.parametrize("order", [None, "C", "F"], ids=["None", "C", "F"]) +@pytest.mark.parametrize( + "order", [None, "C", "F", "A", "K"], ids=["None", "C", "F", "A", "K"] +) def test_ones_like(array, dtype, order): func = lambda xp, x: xp.ones_like(x, dtype=dtype, order=order) diff --git a/tests/third_party/cupy/creation_tests/test_basic.py b/tests/third_party/cupy/creation_tests/test_basic.py index e571ee9f4621..b692622eb0ee 100644 --- a/tests/third_party/cupy/creation_tests/test_basic.py +++ b/tests/third_party/cupy/creation_tests/test_basic.py @@ -82,7 +82,7 @@ def test_empty_int_huge_size_fill0(self): del a cupy.get_default_memory_pool().free_all_blocks() - @testing.for_CF_orders() + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_empty_like(self, xp, dtype, order): @@ -91,7 +91,7 @@ def test_empty_like(self, xp, dtype, order): b.fill(0) return b - @testing.for_CF_orders() + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_empty_like_contiguity(self, xp, dtype, order): @@ -104,7 +104,7 @@ def test_empty_like_contiguity(self, xp, dtype, order): assert b.flags.c_contiguous return b - @testing.for_orders("CF") + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_empty_like_contiguity2(self, xp, dtype, order): @@ -118,7 +118,7 @@ def test_empty_like_contiguity2(self, xp, dtype, order): assert b.flags.f_contiguous return b - @testing.for_orders("CF") + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_empty_like_contiguity3(self, xp, dtype, order): @@ -138,7 +138,6 @@ def test_empty_like_contiguity3(self, xp, dtype, order): assert not b.flags.f_contiguous return b - @pytest.mark.skip("order 'K' is not supported") @testing.for_all_dtypes() def test_empty_like_K_strides(self, dtype): # test strides that are both non-contiguous and non-descending @@ -154,7 +153,10 @@ def test_empty_like_K_strides(self, dtype): bg.fill(0) # make sure NumPy and CuPy strides agree - assert b.strides == bg.strides + scaled_numpy_strides = b.strides + scale = b.itemsize + numpy_strides = tuple(i / scale for i in scaled_numpy_strides) + assert numpy_strides == bg.strides return @testing.with_requires("numpy>=1.19") @@ -165,10 +167,9 @@ def test_empty_like_invalid_order(self, dtype): with pytest.raises(ValueError): xp.empty_like(a, order="Q") - @pytest.mark.skip("subok keyword is not supported") def test_empty_like_subok(self): a = testing.shaped_arange((2, 3, 4), cupy) - with pytest.raises(TypeError): + with pytest.raises(NotImplementedError): cupy.empty_like(a, subok=True) @pytest.mark.skip("strides for zero sized array is different") @@ -225,17 +226,16 @@ def test_zeros_strides(self, order): b_strides = tuple(x * b.itemsize for x in b.strides) assert b_strides == a.strides - @testing.for_CF_orders() + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_zeros_like(self, xp, dtype, order): a = xp.ndarray((2, 3, 4), dtype=dtype) return xp.zeros_like(a, order=order) - @pytest.mark.skip("subok keyword is not supported") def test_zeros_like_subok(self): a = cupy.ndarray((2, 3, 4)) - with pytest.raises(TypeError): + with pytest.raises(NotImplementedError): cupy.zeros_like(a, subok=True) @testing.for_CF_orders() @@ -244,17 +244,16 @@ def test_zeros_like_subok(self): def test_ones(self, xp, dtype, order): return xp.ones((2, 3, 4), dtype=dtype, order=order) - @testing.for_CF_orders() + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_ones_like(self, xp, dtype, order): a = xp.ndarray((2, 3, 4), dtype=dtype) return xp.ones_like(a, order=order) - @pytest.mark.skip("subok keyword is not supported") def test_ones_like_subok(self): a = cupy.ndarray((2, 3, 4)) - with pytest.raises(TypeError): + with pytest.raises(NotImplementedError): cupy.ones_like(a, subok=True) @testing.for_CF_orders() @@ -278,7 +277,7 @@ def test_full_dtypes_cpu_input(self, xp, dtype1, dtype2): (2, 3, 4), numpy.array(1, dtype=dtype1), dtype=dtype2 ) - @testing.for_CF_orders() + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_full_like(self, xp, dtype, order): @@ -293,10 +292,9 @@ def test_full_like_dtypes_cpu_input(self, xp, dtype1, dtype2): warnings.simplefilter("ignore", ComplexWarning) return xp.full_like(a, numpy.array(1, dtype=dtype1)) - @pytest.mark.skip("subok keyword is not supported") def test_full_like_subok(self): a = cupy.ndarray((2, 3, 4)) - with pytest.raises(TypeError): + with pytest.raises(NotImplementedError): cupy.full_like(a, 1, subok=True) @@ -309,7 +307,7 @@ def test_full_like_subok(self): ) class TestBasicReshape: @testing.with_requires("numpy>=1.17.0") - @testing.for_CF_orders() + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_empty_like_reshape(self, xp, dtype, order): @@ -330,7 +328,7 @@ def test_empty_like_reshape_cupy_only(self, dtype, order): testing.assert_array_equal(b, c) @testing.with_requires("numpy>=1.17.0") - @testing.for_CF_orders() + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_empty_like_reshape_contiguity(self, xp, dtype, order): @@ -343,7 +341,7 @@ def test_empty_like_reshape_contiguity(self, xp, dtype, order): assert b.flags.c_contiguous return b - @testing.for_CF_orders() + @testing.for_orders("CFAK") @testing.for_all_dtypes() def test_empty_like_reshape_contiguity_cupy_only(self, dtype, order): a = testing.shaped_arange((2, 3, 4), cupy, dtype) @@ -358,7 +356,7 @@ def test_empty_like_reshape_contiguity_cupy_only(self, dtype, order): testing.assert_array_equal(b, c) @testing.with_requires("numpy>=1.17.0") - @testing.for_orders("CF") + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_empty_like_reshape_contiguity2(self, xp, dtype, order): @@ -375,7 +373,7 @@ def test_empty_like_reshape_contiguity2(self, xp, dtype, order): assert b.flags.f_contiguous return b - @testing.for_orders("CF") + @testing.for_orders("CFAK") @testing.for_all_dtypes() def test_empty_like_reshape_contiguity2_cupy_only(self, dtype, order): a = testing.shaped_arange((2, 3, 4), cupy, dtype) @@ -394,7 +392,7 @@ def test_empty_like_reshape_contiguity2_cupy_only(self, dtype, order): testing.assert_array_equal(b, c) @testing.with_requires("numpy>=1.17.0") - @testing.for_orders("CF") + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_empty_like_reshape_contiguity3(self, xp, dtype, order): @@ -418,7 +416,7 @@ def test_empty_like_reshape_contiguity3(self, xp, dtype, order): assert not b.flags.f_contiguous return b - @testing.for_orders("CF") + @testing.for_orders("CFAK") @testing.for_all_dtypes() def test_empty_like_reshape_contiguity3_cupy_only(self, dtype, order): a = testing.shaped_arange((2, 3, 4), cupy, dtype) @@ -444,7 +442,6 @@ def test_empty_like_reshape_contiguity3_cupy_only(self, dtype, order): c.fill(0) testing.assert_array_equal(b, c) - @pytest.mark.skip("order 'K' is not supported") @testing.with_requires("numpy>=1.17.0") @testing.for_all_dtypes() def test_empty_like_K_strides_reshape(self, dtype): @@ -461,11 +458,14 @@ def test_empty_like_K_strides_reshape(self, dtype): bg.fill(0) # make sure NumPy and CuPy strides agree - assert b.strides == bg.strides + scaled_numpy_strides = b.strides + scale = b.itemsize + numpy_strides = tuple(i / scale for i in scaled_numpy_strides) + assert numpy_strides == bg.strides return @testing.with_requires("numpy>=1.17.0") - @testing.for_CF_orders() + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_zeros_like_reshape(self, xp, dtype, order): @@ -482,7 +482,7 @@ def test_zeros_like_reshape_cupy_only(self, dtype, order): testing.assert_array_equal(b, c) @testing.with_requires("numpy>=1.17.0") - @testing.for_CF_orders() + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_ones_like_reshape(self, xp, dtype, order): @@ -498,7 +498,7 @@ def test_ones_like_reshape_cupy_only(self, dtype): testing.assert_array_equal(b, c) @testing.with_requires("numpy>=1.17.0") - @testing.for_CF_orders() + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_full_like_reshape(self, xp, dtype, order): From 5da201ced730606d57878f87c866b4a8cb8d3453 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Fri, 4 Oct 2024 23:01:37 -0500 Subject: [PATCH 2/5] use flag.c_contiguous --- dpnp/dpnp_iface_arraycreation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 73a1bd296b48..3a48ad51552a 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -138,7 +138,7 @@ def _get_empty_array( order = "C" elif a.flags.f_contiguous: order = "F" - elif a.flags.f_contiguous: + elif a.flags.c_contiguous: order = "C" else: strides = _get_strides_for_order_k(a, _shape) From bc65f46741ec4aa3b69cd6cd03dbcfa3748dcb25 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Mon, 7 Oct 2024 11:30:42 -0500 Subject: [PATCH 3/5] address comments --- dpnp/dpnp_iface_arraycreation.py | 10 +++++++--- tests/test_arraycreation.py | 16 ++++------------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 3a48ad51552a..dd27b23da857 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -127,13 +127,13 @@ def _get_empty_array( ) if order is None: - order = "C" - elif order == "A": + order = "K" + elif order in "aA": if a.flags.fnc: order = "F" else: order = "C" - elif order == "K": + elif order in "kK": if len(_shape) != a.ndim: order = "C" elif a.flags.f_contiguous: @@ -1301,6 +1301,7 @@ def empty_like( input array is allocated. order : {None, "C", "F", "A", "K"}, optional Memory layout of the newly output array. + ``order=None`` is an alias for ``order="K"``. Default: ``"K"``. shape : {None, int, sequence of ints} Overrides the shape of the result. @@ -2156,6 +2157,7 @@ def full_like( input array is allocated. order : {None, "C", "F", "A", "K"}, optional Memory layout of the newly output array. + ``order=None`` is an alias for ``order="K"``. Default: ``"K"``. shape : {None, int, sequence of ints} Overrides the shape of the result. @@ -3195,6 +3197,7 @@ def ones_like( input array is allocated. order : {None, "C", "F", "A", "K"}, optional Memory layout of the newly output array. + ``order=None`` is an alias for ``order="K"``. Default: ``"K"``. shape : {None, int, sequence of ints} Overrides the shape of the result. @@ -3838,6 +3841,7 @@ def zeros_like( input array is allocated. order : {None, "C", "F", "A", "K"}, optional Memory layout of the newly output array. + ``order=None`` is an alias for ``order="K"``. Default: ``"K"``. shape : {None, int, sequence of ints} Overrides the shape of the result. diff --git a/tests/test_arraycreation.py b/tests/test_arraycreation.py index 6db3547d2a24..8d913151f3a4 100644 --- a/tests/test_arraycreation.py +++ b/tests/test_arraycreation.py @@ -540,9 +540,7 @@ def test_full(shape, fill_value, dtype, order): "fill_value", [1.5, 2, 1.5 + 0.0j], ids=["1.5", "2", "1.5+0.j"] ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False)) -@pytest.mark.parametrize( - "order", [None, "C", "F", "A", "K"], ids=["None", "C", "F", "A", "K"] -) +@pytest.mark.parametrize("order", [None, "C", "F", "A", "K"]) def test_full_like(array, fill_value, dtype, order): func = lambda xp, x: xp.full_like(x, fill_value, dtype=dtype, order=order) @@ -601,9 +599,7 @@ def test_zeros(shape, dtype, order): ids=["[]", "0", "[1, 2, 3]", "[[1, 2], [3, 4]]"], ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False)) -@pytest.mark.parametrize( - "order", [None, "C", "F", "A", "K"], ids=["None", "C", "F", "A", "K"] -) +@pytest.mark.parametrize("order", [None, "C", "F", "A", "K"]) def test_zeros_like(array, dtype, order): func = lambda xp, x: xp.zeros_like(x, dtype=dtype, order=order) @@ -630,9 +626,7 @@ def test_empty(shape, dtype, order): ids=["[]", "0", "[1, 2, 3]", "[[1, 2], [3, 4]]"], ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False)) -@pytest.mark.parametrize( - "order", [None, "C", "F", "A", "K"], ids=["None", "C", "F", "A", "K"] -) +@pytest.mark.parametrize("order", [None, "C", "F", "A", "K"]) def test_empty_like(array, dtype, order): func = lambda xp, x: xp.empty_like(x, dtype=dtype, order=order) @@ -659,9 +653,7 @@ def test_ones(shape, dtype, order): ids=["[]", "0", "[1, 2, 3]", "[[1, 2], [3, 4]]"], ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False)) -@pytest.mark.parametrize( - "order", [None, "C", "F", "A", "K"], ids=["None", "C", "F", "A", "K"] -) +@pytest.mark.parametrize("order", [None, "C", "F", "A", "K"]) def test_ones_like(array, dtype, order): func = lambda xp, x: xp.ones_like(x, dtype=dtype, order=order) From c9708ee072882eb39294d561fed3d98132d5e735 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Mon, 7 Oct 2024 11:36:22 -0500 Subject: [PATCH 4/5] raise ValueError with correct msg when an invalid order is given --- dpnp/dpnp_iface_arraycreation.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index dd27b23da857..f045a2b6440f 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -143,6 +143,10 @@ def _get_empty_array( else: strides = _get_strides_for_order_k(a, _shape) order = "C" + elif order not in "cfCF": + raise ValueError( + f"order must be None, 'C', 'F', 'A', or 'K' (got '{order}')" + ) return dpnp_array( _shape, From 3422e2729e36715146a82ffffe8cd5c39c342d84 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Mon, 7 Oct 2024 12:20:10 -0500 Subject: [PATCH 5/5] fix a bug when order is None --- dpnp/dpnp_iface_arraycreation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index f045a2b6440f..0083bca966e5 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -128,7 +128,7 @@ def _get_empty_array( if order is None: order = "K" - elif order in "aA": + if order in "aA": if a.flags.fnc: order = "F" else: