Skip to content

Propagate read-only flag from sycl_usm_array_interface in asarray #1756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dpctl/tensor/_ctors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) > 1:
ro_field = _data_field[1]
else:
ro_field = False
if ro_field:
ary.flags["W"] = False
return ary


Expand Down
26 changes: 26 additions & 0 deletions dpctl/tests/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2368,3 +2368,29 @@ 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


@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"] = not ro_flag
wrapped = ObjWithSyclUsmArrayInterface(a)

b = dpt.asarray(wrapped)

assert b.flags["W"] == (not ro_flag)
assert b._pointer == a._pointer
Loading