From c9802701df4d4ad7473152edd5eeaf33d047c7cc Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 5 Feb 2024 21:20:00 -0600 Subject: [PATCH 1/4] Write _set_writable_flag using helper function _copy_writable --- dpctl/tensor/_usmarray.pyx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/dpctl/tensor/_usmarray.pyx b/dpctl/tensor/_usmarray.pyx index 5a8220db0c..1d77553fec 100644 --- a/dpctl/tensor/_usmarray.pyx +++ b/dpctl/tensor/_usmarray.pyx @@ -586,10 +586,8 @@ cdef class usm_ndarray: return _flags.Flags(self, self.flags_) cdef _set_writable_flag(self, int flag): - cdef int arr_fl = self.flags_ - arr_fl ^= (arr_fl & USM_ARRAY_WRITABLE) # unset WRITABLE flag - arr_fl |= (USM_ARRAY_WRITABLE if flag else 0) - self.flags_ = arr_fl + cdef int mask = (USM_ARRAY_WRITABLE if flag else 0) + self.flags_ = _copy_writable(self.flags_, mask) @property def usm_type(self): From 6bd34c32dfa3062f84861b6fff29d4ec59f5a1d8 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 5 Feb 2024 21:21:20 -0600 Subject: [PATCH 2/4] Added test that readonly out raises --- .../elementwise/test_elementwise_classes.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/dpctl/tests/elementwise/test_elementwise_classes.py b/dpctl/tests/elementwise/test_elementwise_classes.py index c8680078b7..9823c30a66 100644 --- a/dpctl/tests/elementwise/test_elementwise_classes.py +++ b/dpctl/tests/elementwise/test_elementwise_classes.py @@ -14,6 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import pytest + import dpctl.tensor as dpt from dpctl.tests.helper import get_queue_or_skip @@ -49,6 +51,15 @@ def test_unary_class_str_repr(): assert kl_n in r +def test_unary_read_only_out(): + get_queue_or_skip() + x = dpt.arange(32, dtype=dpt.int32) + r = dpt.empty_like(x) + r.flags["W"] = False + with pytest.raises(ValueError): + unary_fn(x, out=r) + + def test_binary_class_getters(): fn = binary_fn.get_implementation_function() assert callable(fn) @@ -105,3 +116,13 @@ def test_binary_class_nout(): nout = binary_fn.nout assert isinstance(nout, int) assert nout == 1 + + +def test_biary_read_only_out(): + get_queue_or_skip() + x1 = dpt.ones(32, dtype=dpt.float32) + x2 = dpt.ones_like(x1) + r = dpt.empty_like(x1) + r.flags["W"] = False + with pytest.raises(ValueError): + binary_fn(x1, x2, out=r) From 03349280ad597df9ebfd704eae4a636fdffb88b1 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 5 Feb 2024 21:21:43 -0600 Subject: [PATCH 3/4] Added test that readonly out raises --- dpctl/tests/test_tensor_clip.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/dpctl/tests/test_tensor_clip.py b/dpctl/tests/test_tensor_clip.py index 11c93ecf1f..3aaf40a3f8 100644 --- a/dpctl/tests/test_tensor_clip.py +++ b/dpctl/tests/test_tensor_clip.py @@ -748,3 +748,22 @@ def test_clip_compute_follows_data(): with pytest.raises(ExecutionPlacementError): dpt.clip(x, out=res) + + +def test_clip_readonly_out(): + get_queue_or_skip() + x = dpt.arange(32, dtype=dpt.int32) + r = dpt.empty_like(x) + r.flags["W"] = False + + with pytest.raises(ValueError): + dpt.clip(x, min=0, max=10, out=r) + + with pytest.raises(ValueError): + dpt.clip(x, max=10, out=r) + + with pytest.raises(ValueError): + dpt.clip(x, min=0, out=r) + + with pytest.raises(ValueError): + dpt.clip(x, out=r) From 4e558f371aea2e9e0bc985d5410e189bba8c741e Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 5 Feb 2024 21:21:57 -0600 Subject: [PATCH 4/4] Added test that readonly out raises --- dpctl/tests/test_usm_ndarray_linalg.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/dpctl/tests/test_usm_ndarray_linalg.py b/dpctl/tests/test_usm_ndarray_linalg.py index c0195a02f2..e51c0a2ac7 100644 --- a/dpctl/tests/test_usm_ndarray_linalg.py +++ b/dpctl/tests/test_usm_ndarray_linalg.py @@ -332,6 +332,16 @@ def test_matmul_out(): assert np.allclose(ref, dpt.asnumpy(res)) +def test_matmul_readonly_out(): + get_queue_or_skip() + m = dpt.ones((10, 10), dtype=dpt.int32) + r = dpt.empty_like(m) + r.flags["W"] = False + + with pytest.raises(ValueError): + dpt.matmul(m, m, out=r) + + def test_matmul_dtype(): get_queue_or_skip()