From 6fd61c35fb1f069a69c69a3084c037e15d67c3a7 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Wed, 23 Oct 2024 09:30:17 -0500 Subject: [PATCH 1/5] Correctly handle inputs which are all Python scalars (weak types) If dtypes list is empty, populate it with first Python scalar from weak_dtypes list. --- dpctl/tensor/_type_utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dpctl/tensor/_type_utils.py b/dpctl/tensor/_type_utils.py index 5defd154df..f279052f94 100644 --- a/dpctl/tensor/_type_utils.py +++ b/dpctl/tensor/_type_utils.py @@ -767,6 +767,9 @@ def result_type(*arrays_and_dtypes): target_dev = d inspected = True + if not dtypes and weak_dtypes: + dtypes.append(weak_dtypes[0].get()) + if not (has_fp16 and has_fp64): for dt in dtypes: if not _dtype_supported_by_device_impl(dt, has_fp16, has_fp64): From 7fc36a6c329fbef227dfa79043d17fde081596d9 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Wed, 23 Oct 2024 09:44:05 -0500 Subject: [PATCH 2/5] Add test that result_types(dtypes) works the same for Python/NumPy scalars --- dpctl/tests/test_usm_ndarray_manipulation.py | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/dpctl/tests/test_usm_ndarray_manipulation.py b/dpctl/tests/test_usm_ndarray_manipulation.py index 17262e2141..80f34bfd6d 100644 --- a/dpctl/tests/test_usm_ndarray_manipulation.py +++ b/dpctl/tests/test_usm_ndarray_manipulation.py @@ -15,6 +15,8 @@ # limitations under the License. +import itertools + import numpy as np import pytest from numpy.testing import assert_, assert_array_equal, assert_raises_regex @@ -1531,3 +1533,22 @@ def test_repeat_0_size(): res = dpt.repeat(x, repetitions, axis=1) axis_sz = 2 * x.shape[1] assert res.shape == (0, axis_sz, 0) + + +def test_result_type_bug_1874(): + dts_bool = [True, np.bool_(True)] + dts_ints = [int(1), np.int64(1)] + dts_floats = [float(1), np.float64(1)] + dts_complexes = [complex(1), np.complex128(1)] + + # iterate over two categories + for dts1, dts2 in itertools.product( + [dts_bool, dts_ints, dts_floats, dts_complexes], repeat=2 + ): + res_dts = [] + # iterate over Python scalar/NumPy scalar choices within categories + for dt1, dt2 in itertools.product(dts1, dts2): + res_dt = dpt.result_type(dt1, dt2) + res_dts.append(res_dt) + # check that all results are the same + assert res_dts and all(res_dts[0] == el for el in res_dts[1:]) From 625e07abd8c8f39016a106e7a93c8e2457a77aa2 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Wed, 23 Oct 2024 10:54:27 -0500 Subject: [PATCH 3/5] Record chagne to result_type in CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74b3569030..4f9ebae6d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.18.2] - Nov. XX, 2024 + +### Fixed +* Fix for `tensor.result_type` when all inputs are Python built-in scalars [gh-1877](https://github.com/IntelPython/dpctl/pull/1877) + ## [0.18.1] - Oct. 11, 2024 ### Changed From 411dd2fdc62069783797d2bd0788315ab77cb8fd Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Wed, 23 Oct 2024 13:32:10 -0500 Subject: [PATCH 4/5] Changed test_result_type_bug_1874 to work with NumPy 2 and older on Win --- dpctl/tests/test_usm_ndarray_manipulation.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dpctl/tests/test_usm_ndarray_manipulation.py b/dpctl/tests/test_usm_ndarray_manipulation.py index 80f34bfd6d..e9b121f8a1 100644 --- a/dpctl/tests/test_usm_ndarray_manipulation.py +++ b/dpctl/tests/test_usm_ndarray_manipulation.py @@ -1536,8 +1536,12 @@ def test_repeat_0_size(): def test_result_type_bug_1874(): - dts_bool = [True, np.bool_(True)] - dts_ints = [int(1), np.int64(1)] + py_sc = True + np_sc = np.asarray([py_sc])[0] + dts_bool = [py_sc, np_sc] + py_sc = int(1) + np_sc = np.asarray([py_sc])[0] + dts_ints = [py_sc, np_sc] dts_floats = [float(1), np.float64(1)] dts_complexes = [complex(1), np.complex128(1)] From bf71ca1a405d88e29da0e14d019a08b28a7eaa5f Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Wed, 20 Nov 2024 16:35:06 -0600 Subject: [PATCH 5/5] Change link to PR from 1877 to 1904 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f9ebae6d8..cb45db859d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.18.2] - Nov. XX, 2024 ### Fixed -* Fix for `tensor.result_type` when all inputs are Python built-in scalars [gh-1877](https://github.com/IntelPython/dpctl/pull/1877) +* Fix for `tensor.result_type` when all inputs are Python built-in scalars [gh-1904](https://github.com/IntelPython/dpctl/pull/1904) ## [0.18.1] - Oct. 11, 2024