forked from data-apis/array-api-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_searching_functions.py
247 lines (218 loc) · 7.9 KB
/
test_searching_functions.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import math
import pytest
from hypothesis import given, note
from hypothesis import strategies as st
from . import _array_module as xp
from . import dtype_helpers as dh
from . import hypothesis_helpers as hh
from . import pytest_helpers as ph
from . import shape_helpers as sh
from . import xps
pytestmark = pytest.mark.unvectorized
@given(
x=hh.arrays(
dtype=hh.real_dtypes,
shape=hh.shapes(min_dims=1, min_side=1),
elements={"allow_nan": False},
),
data=st.data(),
)
def test_argmax(x, data):
kw = data.draw(
hh.kwargs(
axis=st.none() | st.integers(-x.ndim, max(x.ndim - 1, 0)),
keepdims=st.booleans(),
),
label="kw",
)
keepdims = kw.get("keepdims", False)
out = xp.argmax(x, **kw)
ph.assert_default_index("argmax", out.dtype)
axes = sh.normalize_axis(kw.get("axis", None), x.ndim)
ph.assert_keepdimable_shape(
"argmax", in_shape=x.shape, out_shape=out.shape, axes=axes, keepdims=keepdims, kw=kw
)
scalar_type = dh.get_scalar_type(x.dtype)
for indices, out_idx in zip(sh.axes_ndindex(x.shape, axes), sh.ndindex(out.shape)):
max_i = int(out[out_idx])
elements = []
for idx in indices:
s = scalar_type(x[idx])
elements.append(s)
expected = max(range(len(elements)), key=elements.__getitem__)
ph.assert_scalar_equals("argmax", type_=int, idx=out_idx, out=max_i,
expected=expected, kw=kw)
@given(
x=hh.arrays(
dtype=hh.real_dtypes,
shape=hh.shapes(min_dims=1, min_side=1),
elements={"allow_nan": False},
),
data=st.data(),
)
def test_argmin(x, data):
kw = data.draw(
hh.kwargs(
axis=st.none() | st.integers(-x.ndim, max(x.ndim - 1, 0)),
keepdims=st.booleans(),
),
label="kw",
)
keepdims = kw.get("keepdims", False)
out = xp.argmin(x, **kw)
ph.assert_default_index("argmin", out.dtype)
axes = sh.normalize_axis(kw.get("axis", None), x.ndim)
ph.assert_keepdimable_shape(
"argmin", in_shape=x.shape, out_shape=out.shape, axes=axes, keepdims=keepdims, kw=kw
)
scalar_type = dh.get_scalar_type(x.dtype)
for indices, out_idx in zip(sh.axes_ndindex(x.shape, axes), sh.ndindex(out.shape)):
min_i = int(out[out_idx])
elements = []
for idx in indices:
s = scalar_type(x[idx])
elements.append(s)
expected = min(range(len(elements)), key=elements.__getitem__)
ph.assert_scalar_equals("argmin", type_=int, idx=out_idx, out=min_i, expected=expected)
# XXX: the strategy for x is problematic on JAX unless JAX_ENABLE_X64 is on
# the problem is tha for ints >iinfo(int32) it runs into essentially this:
# >>> jnp.asarray[2147483648], dtype=jnp.int64)
# .... https://github.com/jax-ml/jax/pull/6047 ...
# Explicitly limiting the range in elements(...) runs into problems with
# hypothesis where floating-point numbers are not exactly representable.
@pytest.mark.min_version("2024.12")
@given(
x=hh.arrays(
dtype=hh.all_dtypes,
shape=hh.shapes(min_dims=1, min_side=1),
elements={"allow_nan": False},
),
data=st.data(),
)
def test_count_nonzero(x, data):
kw = data.draw(
hh.kwargs(
axis=st.none() | st.integers(-x.ndim, max(x.ndim - 1, 0)),
keepdims=st.booleans(),
),
label="kw",
)
keepdims = kw.get("keepdims", False)
out = xp.count_nonzero(x, **kw)
ph.assert_default_index("count_nonzero", out.dtype)
axes = sh.normalize_axis(kw.get("axis", None), x.ndim)
ph.assert_keepdimable_shape(
"count_nonzero", in_shape=x.shape, out_shape=out.shape, axes=axes, keepdims=keepdims, kw=kw
)
scalar_type = dh.get_scalar_type(x.dtype)
for indices, out_idx in zip(sh.axes_ndindex(x.shape, axes), sh.ndindex(out.shape)):
count = int(out[out_idx])
elements = []
for idx in indices:
s = scalar_type(x[idx])
elements.append(s)
expected = sum(el != 0 for el in elements)
ph.assert_scalar_equals("count_nonzero", type_=int, idx=out_idx, out=count, expected=expected)
@given(hh.arrays(dtype=hh.all_dtypes, shape=()))
def test_nonzero_zerodim_error(x):
with pytest.raises(Exception):
xp.nonzero(x)
@pytest.mark.data_dependent_shapes
@given(hh.arrays(dtype=hh.all_dtypes, shape=hh.shapes(min_dims=1, min_side=1)))
def test_nonzero(x):
out = xp.nonzero(x)
assert len(out) == x.ndim, f"{len(out)=}, but should be {x.ndim=}"
out_size = math.prod(out[0].shape)
for i in range(len(out)):
assert out[i].ndim == 1, f"out[{i}].ndim={x.ndim}, but should be 1"
size_at = math.prod(out[i].shape)
assert size_at == out_size, (
f"prod(out[{i}].shape)={size_at}, "
f"but should be prod(out[0].shape)={out_size}"
)
ph.assert_default_index("nonzero", out[i].dtype, repr_name=f"out[{i}].dtype")
indices = []
if x.dtype == xp.bool:
for idx in sh.ndindex(x.shape):
if x[idx]:
indices.append(idx)
else:
for idx in sh.ndindex(x.shape):
if x[idx] != 0:
indices.append(idx)
if x.ndim == 0:
assert out_size == len(
indices
), f"prod(out[0].shape)={out_size}, but should be {len(indices)}"
else:
for i in range(out_size):
idx = tuple(int(x[i]) for x in out)
f_idx = f"Extrapolated index (x[{i}] for x in out)={idx}"
f_element = f"x[{idx}]={x[idx]}"
assert idx in indices, f"{f_idx} results in {f_element}, a zero element"
assert (
idx == indices[i]
), f"{f_idx} is in the wrong position, should be {indices.index(idx)}"
@given(
shapes=hh.mutually_broadcastable_shapes(3),
dtypes=hh.mutually_promotable_dtypes(),
data=st.data(),
)
def test_where(shapes, dtypes, data):
cond = data.draw(hh.arrays(dtype=xp.bool, shape=shapes[0]), label="condition")
x1 = data.draw(hh.arrays(dtype=dtypes[0], shape=shapes[1]), label="x1")
x2 = data.draw(hh.arrays(dtype=dtypes[1], shape=shapes[2]), label="x2")
out = xp.where(cond, x1, x2)
shape = sh.broadcast_shapes(*shapes)
ph.assert_shape("where", out_shape=out.shape, expected=shape)
# TODO: generate indices without broadcasting arrays
_cond = xp.broadcast_to(cond, shape)
_x1 = xp.broadcast_to(x1, shape)
_x2 = xp.broadcast_to(x2, shape)
for idx in sh.ndindex(shape):
if _cond[idx]:
ph.assert_0d_equals(
"where",
x_repr=f"_x1[{idx}]",
x_val=_x1[idx],
out_repr=f"out[{idx}]",
out_val=out[idx]
)
else:
ph.assert_0d_equals(
"where",
x_repr=f"_x2[{idx}]",
x_val=_x2[idx],
out_repr=f"out[{idx}]",
out_val=out[idx]
)
@pytest.mark.min_version("2023.12")
@given(data=st.data())
def test_searchsorted(data):
# TODO: test side="right"
# TODO: Allow different dtypes for x1 and x2
_x1 = data.draw(
st.lists(xps.from_dtype(dh.default_float), min_size=1, unique=True),
label="_x1",
)
x1 = xp.asarray(_x1, dtype=dh.default_float)
if data.draw(st.booleans(), label="use sorter?"):
sorter = xp.argsort(x1)
else:
sorter = None
x1 = xp.sort(x1)
note(f"{x1=}")
x2 = data.draw(
st.lists(st.sampled_from(_x1), unique=True, min_size=1).map(
lambda o: xp.asarray(o, dtype=dh.default_float)
),
label="x2",
)
out = xp.searchsorted(x1, x2, sorter=sorter)
ph.assert_dtype(
"searchsorted",
in_dtype=[x1.dtype, x2.dtype],
out_dtype=out.dtype,
expected=xp.__array_namespace_info__().default_dtypes()["indexing"],
)
# TODO: shapes and values testing