From 3d9f85875ab4a8760ab055722103a75392548f8f Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Tue, 8 Nov 2022 14:39:42 -0600 Subject: [PATCH 1/4] Added support for arrays for fill_falue for full() function --- dpctl/tensor/_ctors.py | 2 ++ dpctl/tests/test_usm_ndarray_ctor.py | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index 904f828958..fb18e8733d 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -763,6 +763,8 @@ def full( order = order[0].upper() dpctl.utils.validate_usm_type(usm_type, allow_none=False) sycl_queue = normalize_queue_device(sycl_queue=sycl_queue, device=device) + if dtype is None and isinstance(fill_value, (dpt.usm_ndarray, np.ndarray)): + dtype = fill_value.dtype dtype = _get_dtype(dtype, sycl_queue, ref_type=type(fill_value)) res = dpt.usm_ndarray( sh, diff --git a/dpctl/tests/test_usm_ndarray_ctor.py b/dpctl/tests/test_usm_ndarray_ctor.py index 0531eda402..2c8fae0d8e 100644 --- a/dpctl/tests/test_usm_ndarray_ctor.py +++ b/dpctl/tests/test_usm_ndarray_ctor.py @@ -989,6 +989,17 @@ def test_full_dtype_inference(): assert np.issubdtype(dpt.full(10, 0.3 - 2j).dtype, np.complexfloating) +def test_full_fill_array(): + q = get_queue_or_skip() + + dtype = np.float16 + X = dpt.full(10, dpt.usm_ndarray(1, dtype=dtype), sycl_queue=q) + assert dtype == X.dtype + + X = dpt.full(10, np.ndarray(1, dtype=dtype), sycl_queue=q) + assert dtype == X.dtype + + @pytest.mark.parametrize( "dt", _all_dtypes[1:], From c78974af88e148e9ca97df63f5188f5008907100 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Tue, 8 Nov 2022 15:01:36 -0600 Subject: [PATCH 2/4] Changed dtype in test for full() function --- dpctl/tests/test_usm_ndarray_ctor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpctl/tests/test_usm_ndarray_ctor.py b/dpctl/tests/test_usm_ndarray_ctor.py index 2c8fae0d8e..d8746c4829 100644 --- a/dpctl/tests/test_usm_ndarray_ctor.py +++ b/dpctl/tests/test_usm_ndarray_ctor.py @@ -992,7 +992,7 @@ def test_full_dtype_inference(): def test_full_fill_array(): q = get_queue_or_skip() - dtype = np.float16 + dtype = np.int32 X = dpt.full(10, dpt.usm_ndarray(1, dtype=dtype), sycl_queue=q) assert dtype == X.dtype From 8dcec9ae16e767c0aae105a0b7831ec778347873 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Tue, 15 Nov 2022 13:46:54 -0600 Subject: [PATCH 3/4] Added array support for fill_value for full() function. --- dpctl/tensor/_ctors.py | 18 ++++++++++++++---- dpctl/tests/test_usm_ndarray_ctor.py | 13 ++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index fb18e8733d..3de25c6e20 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -726,7 +726,7 @@ def full( dtype=None, order="C", device=None, - usm_type="device", + usm_type=None, sycl_queue=None, ): """ @@ -761,10 +761,20 @@ def full( ) else: order = order[0].upper() - dpctl.utils.validate_usm_type(usm_type, allow_none=False) + dpctl.utils.validate_usm_type(usm_type, allow_none=True) sycl_queue = normalize_queue_device(sycl_queue=sycl_queue, device=device) - if dtype is None and isinstance(fill_value, (dpt.usm_ndarray, np.ndarray)): - dtype = fill_value.dtype + + if isinstance(fill_value, (dpt.usm_ndarray, np.ndarray, tuple, list)): + X = dpt.asarray( + fill_value, + dtype=dtype, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ) + return dpt.broadcast_to(X, sh) + + usm_type = usm_type if usm_type is not None else "device" dtype = _get_dtype(dtype, sycl_queue, ref_type=type(fill_value)) res = dpt.usm_ndarray( sh, diff --git a/dpctl/tests/test_usm_ndarray_ctor.py b/dpctl/tests/test_usm_ndarray_ctor.py index d8746c4829..2639e389d4 100644 --- a/dpctl/tests/test_usm_ndarray_ctor.py +++ b/dpctl/tests/test_usm_ndarray_ctor.py @@ -992,12 +992,15 @@ def test_full_dtype_inference(): def test_full_fill_array(): q = get_queue_or_skip() - dtype = np.int32 - X = dpt.full(10, dpt.usm_ndarray(1, dtype=dtype), sycl_queue=q) - assert dtype == X.dtype + Xnp = np.array([1, 2, 3], dtype=np.int32) + X = dpt.asarray(Xnp, sycl_queue=q) - X = dpt.full(10, np.ndarray(1, dtype=dtype), sycl_queue=q) - assert dtype == X.dtype + shape = (3, 3) + Y = dpt.full(shape, X) + Ynp = np.full(shape, Xnp) + + assert np.array_equal(dpt.asnumpy(Y), Ynp) + assert Ynp.dtype == Y.dtype @pytest.mark.parametrize( From 09852181c1d624eb0aaf59502362ff41b844cf63 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Wed, 16 Nov 2022 14:58:56 -0600 Subject: [PATCH 4/4] Added tests for full() function --- dpctl/tensor/_ctors.py | 13 +++++++++++-- dpctl/tests/test_usm_ndarray_ctor.py | 25 +++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index 3de25c6e20..a7821d4ef2 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -762,18 +762,27 @@ def full( else: order = order[0].upper() dpctl.utils.validate_usm_type(usm_type, allow_none=True) - sycl_queue = normalize_queue_device(sycl_queue=sycl_queue, device=device) if isinstance(fill_value, (dpt.usm_ndarray, np.ndarray, tuple, list)): + if ( + isinstance(fill_value, dpt.usm_ndarray) + and sycl_queue is None + and device is None + ): + sycl_queue = fill_value.sycl_queue + else: + sycl_queue = normalize_queue_device( + sycl_queue=sycl_queue, device=device + ) X = dpt.asarray( fill_value, dtype=dtype, - device=device, usm_type=usm_type, sycl_queue=sycl_queue, ) return 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" dtype = _get_dtype(dtype, sycl_queue, ref_type=type(fill_value)) res = dpt.usm_ndarray( diff --git a/dpctl/tests/test_usm_ndarray_ctor.py b/dpctl/tests/test_usm_ndarray_ctor.py index 2639e389d4..6aed5d31b4 100644 --- a/dpctl/tests/test_usm_ndarray_ctor.py +++ b/dpctl/tests/test_usm_ndarray_ctor.py @@ -992,15 +992,36 @@ def test_full_dtype_inference(): def test_full_fill_array(): q = get_queue_or_skip() - Xnp = np.array([1, 2, 3], dtype=np.int32) + Xnp = np.array([1, 2, 3], dtype="i4") X = dpt.asarray(Xnp, sycl_queue=q) shape = (3, 3) Y = dpt.full(shape, X) Ynp = np.full(shape, Xnp) + assert Y.dtype == Ynp.dtype + assert Y.usm_type == "device" assert np.array_equal(dpt.asnumpy(Y), Ynp) - assert Ynp.dtype == Y.dtype + + +def test_full_compute_follows_data(): + q1 = get_queue_or_skip() + q2 = get_queue_or_skip() + + X = dpt.arange(10, dtype="i4", sycl_queue=q1, usm_type="shared") + Y = dpt.full(10, X[3]) + + assert Y.dtype == X.dtype + assert Y.usm_type == X.usm_type + assert dpctl.utils.get_execution_queue((Y.sycl_queue, X.sycl_queue)) + assert np.array_equal(dpt.asnumpy(Y), np.full(10, 3, dtype="i4")) + + Y = dpt.full(10, X[3], dtype="f4", sycl_queue=q2, usm_type="host") + + assert Y.dtype == dpt.dtype("f4") + assert Y.usm_type == "host" + assert dpctl.utils.get_execution_queue((Y.sycl_queue, q2)) + assert np.array_equal(dpt.asnumpy(Y), np.full(10, 3, dtype="f4")) @pytest.mark.parametrize(