From 1d82b34b44fe995439e1ad55c1b9a8cca953b7ba Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Fri, 18 Nov 2022 16:53:46 -0600 Subject: [PATCH 1/2] Fixed missing copy for full() function if fill_value is array --- dpctl/tensor/_ctors.py | 3 ++- dpctl/tests/test_usm_ndarray_ctor.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index 763e1c239a..218e3bf51d 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -766,10 +766,11 @@ def full( X = dpt.asarray( fill_value, dtype=dtype, + order=order, usm_type=usm_type, sycl_queue=sycl_queue, ) - return dpt.broadcast_to(X, sh) + return dpt.copy(dpt.broadcast_to(X, sh)) sycl_queue = normalize_queue_device(sycl_queue=sycl_queue, device=device) usm_type = usm_type if usm_type is not None else "device" diff --git a/dpctl/tests/test_usm_ndarray_ctor.py b/dpctl/tests/test_usm_ndarray_ctor.py index 2a41dfbddc..d649bba71c 100644 --- a/dpctl/tests/test_usm_ndarray_ctor.py +++ b/dpctl/tests/test_usm_ndarray_ctor.py @@ -1027,6 +1027,31 @@ def test_full_compute_follows_data(): assert np.array_equal(dpt.asnumpy(Y), np.full(10, 3, dtype="f4")) +@pytest.mark.parametrize("order1", ["F", "C"]) +@pytest.mark.parametrize("order2", ["F", "C"]) +def test_full_order(order1, order2): + q = get_queue_or_skip() + Xnp = np.array([1,2,3], order=order1) + Ynp = np.full((3), Xnp, order=order2) + Y = dpt.full((3), Xnp, order=order2, sycl_queue=q) + assert Y.flags.f_contiguous == Ynp.flags.f_contiguous + assert Y.flags.c_contiguous == Ynp.flags.c_contiguous + assert np.array_equal(dpt.asnumpy(Y), Ynp) + + +def test_full_strides(): + q = get_queue_or_skip() + X = dpt.full((3,3), dpt.arange(3, dtype='i4'), sycl_queue=q) + Xnp = np.full((3,3), np.arange(3, dtype='i4')) + assert X.strides == tuple( el // Xnp.itemsize for el in Xnp.strides) + assert np.array_equal(dpt.asnumpy(X), Xnp) + + X = dpt.full((3,3), dpt.arange(6, dtype='i4')[::2], sycl_queue=q) + Xnp = np.full((3,3), np.arange(6, dtype='i4')[::2]) + assert X.strides == tuple( el // Xnp.itemsize for el in Xnp.strides) + assert np.array_equal(dpt.asnumpy(X), Xnp) + + @pytest.mark.parametrize( "dt", _all_dtypes[1:], From 6c9a8cdcb78b9b0f359498b3c0718856fe8eef49 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Fri, 18 Nov 2022 17:22:31 -0600 Subject: [PATCH 2/2] Fixed test_full_order --- dpctl/tensor/_ctors.py | 2 +- dpctl/tests/test_usm_ndarray_ctor.py | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index 218e3bf51d..9513251780 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -770,7 +770,7 @@ def full( usm_type=usm_type, sycl_queue=sycl_queue, ) - return dpt.copy(dpt.broadcast_to(X, sh)) + return dpt.copy(dpt.broadcast_to(X, sh), order=order) sycl_queue = normalize_queue_device(sycl_queue=sycl_queue, device=device) usm_type = usm_type if usm_type is not None else "device" diff --git a/dpctl/tests/test_usm_ndarray_ctor.py b/dpctl/tests/test_usm_ndarray_ctor.py index d649bba71c..9e24847f28 100644 --- a/dpctl/tests/test_usm_ndarray_ctor.py +++ b/dpctl/tests/test_usm_ndarray_ctor.py @@ -1031,24 +1031,24 @@ def test_full_compute_follows_data(): @pytest.mark.parametrize("order2", ["F", "C"]) def test_full_order(order1, order2): q = get_queue_or_skip() - Xnp = np.array([1,2,3], order=order1) - Ynp = np.full((3), Xnp, order=order2) - Y = dpt.full((3), Xnp, order=order2, sycl_queue=q) - assert Y.flags.f_contiguous == Ynp.flags.f_contiguous + Xnp = np.array([1, 2, 3], order=order1) + Ynp = np.full((3, 3), Xnp, order=order2) + Y = dpt.full((3, 3), Xnp, order=order2, sycl_queue=q) assert Y.flags.c_contiguous == Ynp.flags.c_contiguous + assert Y.flags.f_contiguous == Ynp.flags.f_contiguous assert np.array_equal(dpt.asnumpy(Y), Ynp) def test_full_strides(): q = get_queue_or_skip() - X = dpt.full((3,3), dpt.arange(3, dtype='i4'), sycl_queue=q) - Xnp = np.full((3,3), np.arange(3, dtype='i4')) - assert X.strides == tuple( el // Xnp.itemsize for el in Xnp.strides) + X = dpt.full((3, 3), dpt.arange(3, dtype="i4"), sycl_queue=q) + Xnp = np.full((3, 3), np.arange(3, dtype="i4")) + assert X.strides == tuple(el // Xnp.itemsize for el in Xnp.strides) assert np.array_equal(dpt.asnumpy(X), Xnp) - X = dpt.full((3,3), dpt.arange(6, dtype='i4')[::2], sycl_queue=q) - Xnp = np.full((3,3), np.arange(6, dtype='i4')[::2]) - assert X.strides == tuple( el // Xnp.itemsize for el in Xnp.strides) + X = dpt.full((3, 3), dpt.arange(6, dtype="i4")[::2], sycl_queue=q) + Xnp = np.full((3, 3), np.arange(6, dtype="i4")[::2]) + assert X.strides == tuple(el // Xnp.itemsize for el in Xnp.strides) assert np.array_equal(dpt.asnumpy(X), Xnp)