From 97061ee31bcea3ed078b4689c87a5d505d32a16c Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Wed, 24 Jul 2024 08:27:57 -0500 Subject: [PATCH 1/4] asarray on objects with sycl_usm_array_interface now propage RO flag --- dpctl/tensor/_ctors.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index fb400178f9..11c7199230 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -202,6 +202,13 @@ def _usm_ndarray_from_suai(obj): buffer=membuf, strides=sua_iface.get("strides", None), ) + _data_field = sua_iface["data"] + if isinstance(_data_field, tuple) and len(_data_field) == 2: + ro_field = _data_field[1] + else: + ro_field = False + if ro_field: + ary.flags["W"] = False return ary From b5265a8c1ce6eda51e1906aa753db91036d860b9 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Wed, 24 Jul 2024 08:34:32 -0500 Subject: [PATCH 2/4] Test to check that RO flags is preserved by asarray --- dpctl/tests/test_usm_ndarray_ctor.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/dpctl/tests/test_usm_ndarray_ctor.py b/dpctl/tests/test_usm_ndarray_ctor.py index d9ce0eff50..8751e0a365 100644 --- a/dpctl/tests/test_usm_ndarray_ctor.py +++ b/dpctl/tests/test_usm_ndarray_ctor.py @@ -2368,3 +2368,28 @@ def test_gh_1201(): c = dpt.flip(dpt.empty(a.shape, dtype=a.dtype)) c[:] = a assert (dpt.asnumpy(c) == a).all() + + +class ObjWithSyclUsmArrayInterface: + def __init__(self, ary): + self._array_obj = ary + + @property + def __sycl_usm_array_interface__(self): + _suai = self._array_obj.__sycl_usm_array_interface__ + return _suai + + +def test_asarray_writable_flag(): + try: + a = dpt.empty(8) + except dpctl.SyclDeviceCreationError: + pytest.skip("No SYCL devices available") + + a.flags["W"] = False + wrapped = ObjWithSyclUsmArrayInterface(a) + + b = dpt.asarray(wrapped) + + assert not b.flags["W"] + assert b._pointer == a._pointer From cbfec84078321515375e195fcb128a36dfc46215 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Thu, 25 Jul 2024 08:47:29 -0500 Subject: [PATCH 3/4] Update dpctl/tensor/_ctors.py Co-authored-by: Anton <100830759+antonwolfy@users.noreply.github.com> --- dpctl/tensor/_ctors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index 11c7199230..6c5f3888a6 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -203,7 +203,7 @@ def _usm_ndarray_from_suai(obj): strides=sua_iface.get("strides", None), ) _data_field = sua_iface["data"] - if isinstance(_data_field, tuple) and len(_data_field) == 2: + if isinstance(_data_field, tuple) and len(_data_field) > 1: ro_field = _data_field[1] else: ro_field = False From 9a5715f94cbcd7f88c7f47bffb2c9a93223ed7be Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Thu, 25 Jul 2024 13:10:24 -0500 Subject: [PATCH 4/4] Invoke test of readonly flag in asarray to test arrays with both values of the flag to improve coverage --- dpctl/tests/test_usm_ndarray_ctor.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dpctl/tests/test_usm_ndarray_ctor.py b/dpctl/tests/test_usm_ndarray_ctor.py index 8751e0a365..6e702832e5 100644 --- a/dpctl/tests/test_usm_ndarray_ctor.py +++ b/dpctl/tests/test_usm_ndarray_ctor.py @@ -2380,16 +2380,17 @@ def __sycl_usm_array_interface__(self): return _suai -def test_asarray_writable_flag(): +@pytest.mark.parametrize("ro_flag", [True, False]) +def test_asarray_writable_flag(ro_flag): try: a = dpt.empty(8) except dpctl.SyclDeviceCreationError: pytest.skip("No SYCL devices available") - a.flags["W"] = False + a.flags["W"] = not ro_flag wrapped = ObjWithSyclUsmArrayInterface(a) b = dpt.asarray(wrapped) - assert not b.flags["W"] + assert b.flags["W"] == (not ro_flag) assert b._pointer == a._pointer