From f7919493535c49a5e037cc0ba3d1e2c6a056de31 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Wed, 9 Jul 2025 14:40:26 -0700 Subject: [PATCH 1/3] fix gh-2121 underlying memory of a boolean array may not be 0 and 1 when a boolean view is made from a non-boolean array, leading to unexpected results when copying element-wise, especially in astype use a special path for boolean inputs to astype to make sure output is 0 and 1 --- dpctl/tensor/_copy_utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dpctl/tensor/_copy_utils.py b/dpctl/tensor/_copy_utils.py index c80733f77f..700c2002b6 100644 --- a/dpctl/tensor/_copy_utils.py +++ b/dpctl/tensor/_copy_utils.py @@ -739,6 +739,9 @@ def astype( order=copy_order, buffer_ctor_kwargs={"queue": usm_ary.sycl_queue}, ) + # see #2121 + if ary_dtype == dpt.bool: + usm_ary = dpt.not_equal(usm_ary, 0, order=order) _copy_from_usm_ndarray_to_usm_ndarray(R, usm_ary) return R From 8ab92e1fe00790d95946b6cf3d1976af18b15f6f Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Thu, 10 Jul 2025 09:19:20 -0700 Subject: [PATCH 2/3] fix typo in astype logic for boolean input pass copy_order, not order --- dpctl/tensor/_copy_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpctl/tensor/_copy_utils.py b/dpctl/tensor/_copy_utils.py index 700c2002b6..6597febef3 100644 --- a/dpctl/tensor/_copy_utils.py +++ b/dpctl/tensor/_copy_utils.py @@ -741,7 +741,7 @@ def astype( ) # see #2121 if ary_dtype == dpt.bool: - usm_ary = dpt.not_equal(usm_ary, 0, order=order) + usm_ary = dpt.not_equal(usm_ary, 0, order=copy_order) _copy_from_usm_ndarray_to_usm_ndarray(R, usm_ary) return R From 8e952d1658aa6d98cd235920bdc2743043b43056 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Fri, 11 Jul 2025 15:55:05 -0700 Subject: [PATCH 3/3] add a test for gh-2121 resolution --- dpctl/tests/test_usm_ndarray_ctor.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/dpctl/tests/test_usm_ndarray_ctor.py b/dpctl/tests/test_usm_ndarray_ctor.py index c6e33b600c..b8e6929dcf 100644 --- a/dpctl/tests/test_usm_ndarray_ctor.py +++ b/dpctl/tests/test_usm_ndarray_ctor.py @@ -1541,6 +1541,16 @@ def test_astype_gh_1926(): assert x is x__ +def test_astype_gh_2121(): + get_queue_or_skip() + + x_np = np.asarray([0, 3, 1, 2, 0, 1], dtype="u1").view("?") + x = dpt.asarray(x_np) + res = dpt.astype(x, dpt.uint8) + expected = dpt.asarray([0, 1, 1, 1, 0, 1], dtype="u1") + assert dpt.all(res == expected) + + def test_copy(): try: X = dpt.usm_ndarray((5, 5), "i4")[2:4, 1:4]