From e76953e9e2bbfd12c7e468163de7d43e5e1d97e1 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 29 Nov 2021 10:24:53 -0600 Subject: [PATCH 1/2] array_info_sequence should handle empty sequences as host data Added a test for this input --- dpctl/tensor/_ctors.py | 3 +++ dpctl/tests/test_tensor_asarray.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index d0f459d1c5..72b28f1238 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -63,6 +63,9 @@ def _array_info_sequence(li): raise ValueError( "Inconsistent dimensions, {} and {}".format(dim, el_dim) ) + if dim is None: + dim = tuple() + device = _host_set return (n,) + dim, dt, device diff --git a/dpctl/tests/test_tensor_asarray.py b/dpctl/tests/test_tensor_asarray.py index c7734b8194..c39471497b 100644 --- a/dpctl/tests/test_tensor_asarray.py +++ b/dpctl/tests/test_tensor_asarray.py @@ -80,6 +80,12 @@ def test_asarray_from_sequence(): Y = dpt.asarray(X, usm_type="device") assert type(Y) is dpt.usm_ndarray assert Y.ndim == 2 + assert Y.shape == (len(X), 2) + + X = [] + Y = dpt.asarray(X, usm_type="device") + assert type(Y) is dpt.usm_ndarray + assert Y.shape == (0,) def test_asarray_from_object_with_suai(): From 5f279d6142265f992f22d5bc65c17e72137f42e8 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 29 Nov 2021 10:50:35 -0600 Subject: [PATCH 2/2] Fixes #690 Since bool is a subclass of int, the lack of missing test for instance of bool was being handled by test for instance of int. Fixed implementation, added a test. --- dpctl/tensor/_ctors.py | 2 ++ dpctl/tests/test_tensor_asarray.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index 72b28f1238..5126f40837 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -32,6 +32,8 @@ def _array_info_dispatch(obj): return obj.shape, obj.dtype, _host_set elif isinstance(obj, range): return (len(obj),), int, _host_set + elif isinstance(obj, bool): + return _empty_tuple, bool, _host_set elif isinstance(obj, float): return _empty_tuple, float, _host_set elif isinstance(obj, int): diff --git a/dpctl/tests/test_tensor_asarray.py b/dpctl/tests/test_tensor_asarray.py index c39471497b..8c61fc9f13 100644 --- a/dpctl/tests/test_tensor_asarray.py +++ b/dpctl/tests/test_tensor_asarray.py @@ -87,6 +87,11 @@ def test_asarray_from_sequence(): assert type(Y) is dpt.usm_ndarray assert Y.shape == (0,) + X = [True, False] + Y = dpt.asarray(X, usm_type="device") + assert type(Y) is dpt.usm_ndarray + assert Y.dtype.kind == "b" + def test_asarray_from_object_with_suai(): """Test that asarray can deal with opaque objects implementing SUAI"""