Repository navigation
Expand file tree
/
Copy pathtest_array_api_info.py
More file actions
133 lines (110 loc) · 3.74 KB
/
Copy pathtest_array_api_info.py
File metadata and controls
133 lines (110 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import pytest
from dpctl import get_devices, select_default_device
import dpnp
from dpnp.exceptions import SyclDeviceCreationError
from dpnp.tensor._tensor_impl import default_device_complex_type
from dpnp.tests.helper import (
has_support_aspect64,
is_win_platform,
numpy_version,
)
info = dpnp.__array_namespace_info__()
default_device = select_default_device()
def test_capabilities():
caps = info.capabilities()
assert caps["boolean indexing"] is True
assert caps["data-dependent shapes"] is True
assert caps["max dimensions"] == None
def test_default_device():
assert info.default_device() == default_device
def test_default_dtypes():
dtypes = info.default_dtypes()
assert (
dtypes["real floating"]
== dpnp.default_float_type()
== dpnp.asarray(0.0).dtype
)
# TODO: add dpnp.default_complex_type() function
assert (
dtypes["complex floating"]
== default_device_complex_type(default_device)
== dpnp.asarray(0.0j).dtype
)
if not is_win_platform() or numpy_version() >= "2.0.0":
# numpy changed default integer on Windows since 2.0
assert dtypes["integral"] == dpnp.intp == dpnp.asarray(0).dtype
assert (
dtypes["indexing"] == dpnp.intp == dpnp.argmax(dpnp.zeros(10)).dtype
)
with pytest.raises(
TypeError, match="Unsupported type for device argument:"
):
info.default_dtypes(device=1)
def test_dtypes_all():
dtypes = info.dtypes()
assert dtypes == (
{
"bool": dpnp.bool_,
"int8": dpnp.int8,
"int16": dpnp.int16,
"int32": dpnp.int32,
"int64": dpnp.int64,
"uint8": dpnp.uint8,
"uint16": dpnp.uint16,
"uint32": dpnp.uint32,
"uint64": dpnp.uint64,
"float32": dpnp.float32,
}
| ({"float64": dpnp.float64} if has_support_aspect64() else {})
| {"complex64": dpnp.complex64}
| ({"complex128": dpnp.complex128} if has_support_aspect64() else {})
)
dtype_categories = {
"bool": {"bool": dpnp.bool_},
"signed integer": {
"int8": dpnp.int8,
"int16": dpnp.int16,
"int32": dpnp.int32,
"int64": dpnp.int64,
},
"unsigned integer": {
"uint8": dpnp.uint8,
"uint16": dpnp.uint16,
"uint32": dpnp.uint32,
"uint64": dpnp.uint64,
},
"integral": ("signed integer", "unsigned integer"),
"real floating": {"float32": dpnp.float32}
| ({"float64": dpnp.float64} if has_support_aspect64() else {}),
"complex floating": {"complex64": dpnp.complex64}
| ({"complex128": dpnp.complex128} if has_support_aspect64() else {}),
"numeric": ("integral", "real floating", "complex floating"),
}
@pytest.mark.parametrize("kind", dtype_categories)
def test_dtypes_kind(kind):
expected = dtype_categories[kind]
if isinstance(expected, tuple):
assert info.dtypes(kind=kind) == info.dtypes(kind=expected)
else:
assert info.dtypes(kind=kind) == expected
def test_dtypes_tuple():
dtypes = info.dtypes(kind=("bool", "integral"))
assert dtypes == {
"bool": dpnp.bool_,
"int8": dpnp.int8,
"int16": dpnp.int16,
"int32": dpnp.int32,
"int64": dpnp.int64,
"uint8": dpnp.uint8,
"uint16": dpnp.uint16,
"uint32": dpnp.uint32,
"uint64": dpnp.uint64,
}
def test_dtypes_invalid_kind():
with pytest.raises(ValueError, match="Unrecognized data type kind"):
info.dtypes(kind="invalid")
def test_dtypes_invalid_device():
with pytest.raises(SyclDeviceCreationError, match="Could not create"):
info.dtypes(device="str")
def test_devices():
assert info.devices() == get_devices()