forked from data-apis/array-api-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_statistical_functions.py
340 lines (307 loc) · 11.3 KB
/
test_statistical_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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import cmath
import math
from typing import Optional
import pytest
from hypothesis import assume, given
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, api_version
from ._array_module import _UndefinedStub
from .typing import DataType
pytestmark = pytest.mark.ci
def kwarg_dtypes(dtype: DataType) -> st.SearchStrategy[Optional[DataType]]:
dtypes = [d2 for d1, d2 in dh.promotion_table if d1 == dtype]
dtypes = [d for d in dtypes if not isinstance(d, _UndefinedStub)]
assert len(dtypes) > 0 # sanity check
return st.none() | st.sampled_from(dtypes)
@given(
x=hh.arrays(
dtype=xps.real_dtypes(),
shape=hh.shapes(min_side=1),
elements={"allow_nan": False},
),
data=st.data(),
)
def test_max(x, data):
kw = data.draw(hh.kwargs(axis=hh.axes(x.ndim), keepdims=st.booleans()), label="kw")
keepdims = kw.get("keepdims", False)
out = xp.max(x, **kw)
ph.assert_dtype("max", in_dtype=x.dtype, out_dtype=out.dtype)
_axes = sh.normalise_axis(kw.get("axis", None), x.ndim)
ph.assert_keepdimable_shape(
"max", in_shape=x.shape, out_shape=out.shape, axes=_axes, keepdims=keepdims, kw=kw
)
scalar_type = dh.get_scalar_type(out.dtype)
for indices, out_idx in zip(sh.axes_ndindex(x.shape, _axes), sh.ndindex(out.shape)):
max_ = scalar_type(out[out_idx])
elements = []
for idx in indices:
s = scalar_type(x[idx])
elements.append(s)
expected = max(elements)
ph.assert_scalar_equals("max", type_=scalar_type, idx=out_idx, out=max_, expected=expected)
@given(
x=hh.arrays(
dtype=xps.floating_dtypes(),
shape=hh.shapes(min_side=1),
elements={"allow_nan": False},
),
data=st.data(),
)
def test_mean(x, data):
kw = data.draw(hh.kwargs(axis=hh.axes(x.ndim), keepdims=st.booleans()), label="kw")
keepdims = kw.get("keepdims", False)
out = xp.mean(x, **kw)
ph.assert_dtype("mean", in_dtype=x.dtype, out_dtype=out.dtype)
_axes = sh.normalise_axis(kw.get("axis", None), x.ndim)
ph.assert_keepdimable_shape(
"mean", in_shape=x.shape, out_shape=out.shape, axes=_axes, keepdims=keepdims, kw=kw
)
# Values testing mean is too finicky
@given(
x=hh.arrays(
dtype=xps.real_dtypes(),
shape=hh.shapes(min_side=1),
elements={"allow_nan": False},
),
data=st.data(),
)
def test_min(x, data):
kw = data.draw(hh.kwargs(axis=hh.axes(x.ndim), keepdims=st.booleans()), label="kw")
keepdims = kw.get("keepdims", False)
out = xp.min(x, **kw)
ph.assert_dtype("min", in_dtype=x.dtype, out_dtype=out.dtype)
_axes = sh.normalise_axis(kw.get("axis", None), x.ndim)
ph.assert_keepdimable_shape(
"min", in_shape=x.shape, out_shape=out.shape, axes=_axes, keepdims=keepdims, kw=kw
)
scalar_type = dh.get_scalar_type(out.dtype)
for indices, out_idx in zip(sh.axes_ndindex(x.shape, _axes), sh.ndindex(out.shape)):
min_ = scalar_type(out[out_idx])
elements = []
for idx in indices:
s = scalar_type(x[idx])
elements.append(s)
expected = min(elements)
ph.assert_scalar_equals("min", type_=scalar_type, idx=out_idx, out=min_, expected=expected)
@given(
x=hh.arrays(
dtype=xps.numeric_dtypes(),
shape=hh.shapes(min_side=1),
elements={"allow_nan": False},
),
data=st.data(),
)
def test_prod(x, data):
kw = data.draw(
hh.kwargs(
axis=hh.axes(x.ndim),
dtype=kwarg_dtypes(x.dtype),
keepdims=st.booleans(),
),
label="kw",
)
keepdims = kw.get("keepdims", False)
with hh.reject_overflow():
out = xp.prod(x, **kw)
dtype = kw.get("dtype", None)
if dtype is None:
if dh.is_int_dtype(x.dtype):
if x.dtype in dh.uint_dtypes:
default_dtype = dh.default_uint
else:
default_dtype = dh.default_int
if default_dtype is None:
_dtype = None
else:
m, M = dh.dtype_ranges[x.dtype]
d_m, d_M = dh.dtype_ranges[default_dtype]
if m < d_m or M > d_M:
_dtype = x.dtype
else:
_dtype = default_dtype
elif dh.is_float_dtype(x.dtype, include_complex=False):
if dh.dtype_nbits[x.dtype] > dh.dtype_nbits[dh.default_float]:
_dtype = x.dtype
else:
_dtype = dh.default_float
elif api_version > "2021.12":
# Complex dtype
if dh.dtype_nbits[x.dtype] > dh.dtype_nbits[dh.default_complex]:
_dtype = x.dtype
else:
_dtype = dh.default_complex
else:
raise RuntimeError("Unexpected dtype. This indicates a bug in the test suite.")
else:
_dtype = dtype
if _dtype is None:
# If a default uint cannot exist (i.e. in PyTorch which doesn't support
# uint32 or uint64), we skip testing the output dtype.
# See https://github.com/data-apis/array-api-tests/issues/106
if x.dtype in dh.uint_dtypes:
assert dh.is_int_dtype(out.dtype) # sanity check
else:
ph.assert_dtype("prod", in_dtype=x.dtype, out_dtype=out.dtype, expected=_dtype)
_axes = sh.normalise_axis(kw.get("axis", None), x.ndim)
ph.assert_keepdimable_shape(
"prod", in_shape=x.shape, out_shape=out.shape, axes=_axes, keepdims=keepdims, kw=kw
)
scalar_type = dh.get_scalar_type(out.dtype)
for indices, out_idx in zip(sh.axes_ndindex(x.shape, _axes), sh.ndindex(out.shape)):
prod = scalar_type(out[out_idx])
assume(cmath.isfinite(prod))
elements = []
for idx in indices:
s = scalar_type(x[idx])
elements.append(s)
expected = math.prod(elements)
if dh.is_int_dtype(out.dtype):
m, M = dh.dtype_ranges[out.dtype]
assume(m <= expected <= M)
ph.assert_scalar_equals("prod", type_=scalar_type, idx=out_idx, out=prod, expected=expected)
@given(
x=hh.arrays(
dtype=xps.floating_dtypes(),
shape=hh.shapes(min_side=1),
elements={"allow_nan": False},
).filter(lambda x: math.prod(x.shape) >= 2),
data=st.data(),
)
def test_std(x, data):
axis = data.draw(hh.axes(x.ndim), label="axis")
_axes = sh.normalise_axis(axis, x.ndim)
N = sum(side for axis, side in enumerate(x.shape) if axis not in _axes)
correction = data.draw(
st.floats(0.0, N, allow_infinity=False, allow_nan=False) | st.integers(0, N),
label="correction",
)
_keepdims = data.draw(st.booleans(), label="keepdims")
kw = data.draw(
hh.specified_kwargs(
("axis", axis, None),
("correction", correction, 0.0),
("keepdims", _keepdims, False),
),
label="kw",
)
keepdims = kw.get("keepdims", False)
out = xp.std(x, **kw)
ph.assert_dtype("std", in_dtype=x.dtype, out_dtype=out.dtype)
ph.assert_keepdimable_shape(
"std", in_shape=x.shape, out_shape=out.shape, axes=_axes, keepdims=keepdims, kw=kw
)
# We can't easily test the result(s) as standard deviation methods vary a lot
@given(
x=hh.arrays(
dtype=xps.numeric_dtypes(),
shape=hh.shapes(min_side=1),
elements={"allow_nan": False},
),
data=st.data(),
)
def test_sum(x, data):
kw = data.draw(
hh.kwargs(
axis=hh.axes(x.ndim),
dtype=kwarg_dtypes(x.dtype),
keepdims=st.booleans(),
),
label="kw",
)
keepdims = kw.get("keepdims", False)
with hh.reject_overflow():
out = xp.sum(x, **kw)
dtype = kw.get("dtype", None)
if dtype is None:
if dh.is_int_dtype(x.dtype):
if x.dtype in dh.uint_dtypes:
default_dtype = dh.default_uint
else:
default_dtype = dh.default_int
if default_dtype is None:
_dtype = None
else:
m, M = dh.dtype_ranges[x.dtype]
d_m, d_M = dh.dtype_ranges[default_dtype]
if m < d_m or M > d_M:
_dtype = x.dtype
else:
_dtype = default_dtype
elif dh.is_float_dtype(x.dtype, include_complex=False):
if dh.dtype_nbits[x.dtype] > dh.dtype_nbits[dh.default_float]:
_dtype = x.dtype
else:
_dtype = dh.default_float
elif api_version > "2021.12":
# Complex dtype
if dh.dtype_nbits[x.dtype] > dh.dtype_nbits[dh.default_complex]:
_dtype = x.dtype
else:
_dtype = dh.default_complex
else:
raise RuntimeError("Unexpected dtype. This indicates a bug in the test suite.")
else:
_dtype = dtype
if _dtype is None:
# If a default uint cannot exist (i.e. in PyTorch which doesn't support
# uint32 or uint64), we skip testing the output dtype.
# See https://github.com/data-apis/array-api-tests/issues/160
if x.dtype in dh.uint_dtypes:
assert dh.is_int_dtype(out.dtype) # sanity check
else:
ph.assert_dtype("sum", in_dtype=x.dtype, out_dtype=out.dtype, expected=_dtype)
_axes = sh.normalise_axis(kw.get("axis", None), x.ndim)
ph.assert_keepdimable_shape(
"sum", in_shape=x.shape, out_shape=out.shape, axes=_axes, keepdims=keepdims, kw=kw
)
scalar_type = dh.get_scalar_type(out.dtype)
for indices, out_idx in zip(sh.axes_ndindex(x.shape, _axes), sh.ndindex(out.shape)):
sum_ = scalar_type(out[out_idx])
assume(cmath.isfinite(sum_))
elements = []
for idx in indices:
s = scalar_type(x[idx])
elements.append(s)
expected = sum(elements)
if dh.is_int_dtype(out.dtype):
m, M = dh.dtype_ranges[out.dtype]
assume(m <= expected <= M)
ph.assert_scalar_equals("sum", type_=scalar_type, idx=out_idx, out=sum_, expected=expected)
@pytest.mark.skip(reason="flaky") # TODO: fix!
@given(
x=hh.arrays(
dtype=xps.floating_dtypes(),
shape=hh.shapes(min_side=1),
elements={"allow_nan": False},
).filter(lambda x: math.prod(x.shape) >= 2),
data=st.data(),
)
def test_var(x, data):
axis = data.draw(hh.axes(x.ndim), label="axis")
_axes = sh.normalise_axis(axis, x.ndim)
N = sum(side for axis, side in enumerate(x.shape) if axis not in _axes)
correction = data.draw(
st.floats(0.0, N, allow_infinity=False, allow_nan=False) | st.integers(0, N),
label="correction",
)
_keepdims = data.draw(st.booleans(), label="keepdims")
kw = data.draw(
hh.specified_kwargs(
("axis", axis, None),
("correction", correction, 0.0),
("keepdims", _keepdims, False),
),
label="kw",
)
keepdims = kw.get("keepdims", False)
out = xp.var(x, **kw)
ph.assert_dtype("var", in_dtype=x.dtype, out_dtype=out.dtype)
ph.assert_keepdimable_shape(
"var", in_shape=x.shape, out_shape=out.shape, axes=_axes, keepdims=keepdims, kw=kw
)
# We can't easily test the result(s) as variance methods vary a lot