From 886a4473b1aba6fbaa3b0e8bcd58f7df0b05bc2d Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Thu, 20 Oct 2022 20:10:54 -0500 Subject: [PATCH 1/4] Fixed issue in asarray_from_numpy for uint64 dtype. And changed call copy_to function --- dpctl/tensor/_ctors.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index dab6303dd1..f3f36f3a6e 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -186,8 +186,10 @@ def _asarray_from_usm_ndarray( order=order, buffer_ctor_kwargs={"queue": copy_q}, ) - # FIXME: call copy_to when implemented - res[(slice(None, None, None),) * res.ndim] = usm_ndary + hev, _ = ti._copy_usm_ndarray_into_usm_ndarray( + src=usm_ndary, dst=res, sycl_queue=copy_q + ) + hev.wait() return res @@ -207,7 +209,7 @@ def _asarray_from_numpy_ndarray( if dtype is None: ary_dtype = ary.dtype dtype = _get_dtype(dtype, copy_q, ref_type=ary_dtype) - if dtype.itemsize > ary_dtype.itemsize: + if dtype.itemsize > ary_dtype.itemsize or ary_dtype == np.uint64: dtype = ary_dtype f_contig = ary.flags["F"] c_contig = ary.flags["C"] @@ -244,8 +246,10 @@ def _asarray_from_numpy_ndarray( order=order, buffer_ctor_kwargs={"queue": copy_q}, ) - # FIXME: call copy_to when implemented - res[(slice(None, None, None),) * res.ndim] = ary + hev, _ = ti._copy_numpy_ndarray_into_usm_ndarray( + src=ary, dst=res, sycl_queue=copy_q + ) + hev.wait() return res From 888a24bf08bef306dcf087ca4e29da41c7caa0ad Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Thu, 20 Oct 2022 20:10:54 -0500 Subject: [PATCH 2/4] Fixed issue in asarray_from_numpy for uint64 dtype. And changed call copy_to function --- dpctl/tensor/_ctors.py | 13 ++++++++----- dpctl/tests/test_usm_ndarray_ctor.py | 5 +++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index dab6303dd1..66996f8cef 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -186,8 +186,10 @@ def _asarray_from_usm_ndarray( order=order, buffer_ctor_kwargs={"queue": copy_q}, ) - # FIXME: call copy_to when implemented - res[(slice(None, None, None),) * res.ndim] = usm_ndary + hev, _ = ti._copy_usm_ndarray_into_usm_ndarray( + src=usm_ndary, dst=res, sycl_queue=copy_q + ) + hev.wait() return res @@ -207,7 +209,7 @@ def _asarray_from_numpy_ndarray( if dtype is None: ary_dtype = ary.dtype dtype = _get_dtype(dtype, copy_q, ref_type=ary_dtype) - if dtype.itemsize > ary_dtype.itemsize: + if dtype.itemsize > ary_dtype.itemsize or ary_dtype == np.uint64: dtype = ary_dtype f_contig = ary.flags["F"] c_contig = ary.flags["C"] @@ -244,8 +246,9 @@ def _asarray_from_numpy_ndarray( order=order, buffer_ctor_kwargs={"queue": copy_q}, ) - # FIXME: call copy_to when implemented - res[(slice(None, None, None),) * res.ndim] = ary + ti._copy_numpy_ndarray_into_usm_ndarray( + src=ary, dst=res, sycl_queue=copy_q + ) return res diff --git a/dpctl/tests/test_usm_ndarray_ctor.py b/dpctl/tests/test_usm_ndarray_ctor.py index e7abdffb63..7f23cfd767 100644 --- a/dpctl/tests/test_usm_ndarray_ctor.py +++ b/dpctl/tests/test_usm_ndarray_ctor.py @@ -1458,3 +1458,8 @@ def test_flags(): f.writable # check comparison with generic types f == Ellipsis + +def test_asarray_uint64(): + Xnp = np.ndarray(1, dtype=np.uint64) + X = dpt.asarray(Xnp) + assert X.dtype == Xnp.dtype From 04d194c5c8a241e6252335e2283c3e9d24983f5b Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Thu, 20 Oct 2022 20:47:46 -0500 Subject: [PATCH 3/4] Fix pre commit --- dpctl/tensor/_ctors.py | 4 +--- dpctl/tests/test_usm_ndarray_ctor.py | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index 66996f8cef..8aa8d5dc12 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -246,9 +246,7 @@ def _asarray_from_numpy_ndarray( order=order, buffer_ctor_kwargs={"queue": copy_q}, ) - ti._copy_numpy_ndarray_into_usm_ndarray( - src=ary, dst=res, sycl_queue=copy_q - ) + ti._copy_numpy_ndarray_into_usm_ndarray(src=ary, dst=res, sycl_queue=copy_q) return res diff --git a/dpctl/tests/test_usm_ndarray_ctor.py b/dpctl/tests/test_usm_ndarray_ctor.py index 7f23cfd767..0531eda402 100644 --- a/dpctl/tests/test_usm_ndarray_ctor.py +++ b/dpctl/tests/test_usm_ndarray_ctor.py @@ -1459,6 +1459,7 @@ def test_flags(): # check comparison with generic types f == Ellipsis + def test_asarray_uint64(): Xnp = np.ndarray(1, dtype=np.uint64) X = dpt.asarray(Xnp) From eb9b6526731d6fe651aab9fab701ead21121f2b4 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 21 Oct 2022 11:37:21 -0500 Subject: [PATCH 4/4] asarray to use mapping from ndarray data type to usm_ndarray type This mapping taking into account device aspects. --- dpctl/tensor/_ctors.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index 8aa8d5dc12..d72fc51e2b 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -193,6 +193,28 @@ def _asarray_from_usm_ndarray( return res +def _map_to_device_dtype(dt, q): + if dt.char == "?" or np.issubdtype(dt, np.integer): + return dt + d = q.sycl_device + dtc = dt.char + if np.issubdtype(dt, np.floating): + if dtc == "f": + return dt + else: + if dtc == "d" and d.has_aspect_fp64: + return dt + if dtc == "h" and d.has_aspect_fp16: + return dt + return dpt.dtype("f4") + elif np.issubdtype(dt, np.complexfloating): + if dtc == "F": + return dt + if dtc == "D" and d.has_aspect_fp64: + return dt + return dpt.dtype("c8") + + def _asarray_from_numpy_ndarray( ary, dtype=None, usm_type=None, sycl_queue=None, order="K" ): @@ -207,10 +229,8 @@ def _asarray_from_numpy_ndarray( "Please convert the input to an array with numeric data type." ) if dtype is None: - ary_dtype = ary.dtype - dtype = _get_dtype(dtype, copy_q, ref_type=ary_dtype) - if dtype.itemsize > ary_dtype.itemsize or ary_dtype == np.uint64: - dtype = ary_dtype + # deduce device-representable output data type + dtype = _map_to_device_dtype(ary.dtype, copy_q) f_contig = ary.flags["F"] c_contig = ary.flags["C"] fc_contig = f_contig or c_contig @@ -246,7 +266,7 @@ def _asarray_from_numpy_ndarray( order=order, buffer_ctor_kwargs={"queue": copy_q}, ) - ti._copy_numpy_ndarray_into_usm_ndarray(src=ary, dst=res, sycl_queue=copy_q) + res[...] = ary return res