forked from data-apis/array-api-strict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_flags.py
559 lines (472 loc) · 18.5 KB
/
test_flags.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import sys
import subprocess
from .._flags import (set_array_api_strict_flags, get_array_api_strict_flags,
reset_array_api_strict_flags)
from .._fft import (fft, ifft, fftn, ifftn, rfft, irfft, rfftn, irfftn, hfft,
ihfft, fftfreq, rfftfreq, fftshift, ifftshift)
from .._linalg import (cholesky, cross, det, diagonal, eigh, eigvalsh, inv,
matmul, matrix_norm, matrix_power, matrix_rank, matrix_transpose, outer, pinv,
qr, slogdet, solve, svd, svdvals, tensordot, trace, vecdot, vector_norm)
from .. import (asarray, unique_all, unique_counts, unique_inverse,
unique_values, nonzero, repeat)
import array_api_strict as xp
import pytest
def test_flag_defaults():
flags = get_array_api_strict_flags()
assert flags == {
'api_version': '2024.12',
'boolean_indexing': True,
'data_dependent_shapes': True,
'enabled_extensions': ('linalg', 'fft'),
}
def test_reset_flags():
with pytest.warns(UserWarning):
set_array_api_strict_flags(
api_version='2021.12',
boolean_indexing=False,
data_dependent_shapes=False,
enabled_extensions=())
reset_array_api_strict_flags()
flags = get_array_api_strict_flags()
assert flags == {
'api_version': '2024.12',
'boolean_indexing': True,
'data_dependent_shapes': True,
'enabled_extensions': ('linalg', 'fft'),
}
def test_setting_flags():
set_array_api_strict_flags(data_dependent_shapes=False)
flags = get_array_api_strict_flags()
assert flags == {
'api_version': '2024.12',
'boolean_indexing': True,
'data_dependent_shapes': False,
'enabled_extensions': ('linalg', 'fft'),
}
set_array_api_strict_flags(enabled_extensions=('fft',))
flags = get_array_api_strict_flags()
assert flags == {
'api_version': '2024.12',
'boolean_indexing': True,
'data_dependent_shapes': False,
'enabled_extensions': ('fft',),
}
def test_flags_api_version_2021_12():
# Make sure setting the version to 2021.12 disables fft and issues a
# warning.
with pytest.warns(UserWarning) as record:
set_array_api_strict_flags(api_version='2021.12')
assert len(record) == 1
assert '2021.12' in str(record[0].message)
flags = get_array_api_strict_flags()
assert flags == {
'api_version': '2021.12',
'boolean_indexing': True,
'data_dependent_shapes': True,
'enabled_extensions': ('linalg',),
}
def test_flags_api_version_2022_12():
set_array_api_strict_flags(api_version='2022.12')
flags = get_array_api_strict_flags()
assert flags == {
'api_version': '2022.12',
'boolean_indexing': True,
'data_dependent_shapes': True,
'enabled_extensions': ('linalg', 'fft'),
}
def test_flags_api_version_2023_12():
set_array_api_strict_flags(api_version='2023.12')
flags = get_array_api_strict_flags()
assert flags == {
'api_version': '2023.12',
'boolean_indexing': True,
'data_dependent_shapes': True,
'enabled_extensions': ('linalg', 'fft'),
}
def test_flags_api_version_2024_12():
set_array_api_strict_flags(api_version='2024.12')
flags = get_array_api_strict_flags()
assert flags == {
'api_version': '2024.12',
'boolean_indexing': True,
'data_dependent_shapes': True,
'enabled_extensions': ('linalg', 'fft'),
}
def test_flags_api_version_2025_12():
# Make sure setting the version to 2025.12 issues a warning.
with pytest.warns(UserWarning) as record:
set_array_api_strict_flags(api_version='2025.12')
assert len(record) == 1
assert '2025.12' in str(record[0].message)
assert 'draft' in str(record[0].message)
flags = get_array_api_strict_flags()
assert flags == {
'api_version': '2025.12',
'boolean_indexing': True,
'data_dependent_shapes': True,
'enabled_extensions': ('linalg', 'fft'),
}
def test_setting_flags_invalid():
# Test setting flags with invalid values
pytest.raises(ValueError, lambda:
set_array_api_strict_flags(api_version='2020.12'))
pytest.raises(ValueError, lambda: set_array_api_strict_flags(
enabled_extensions=('linalg', 'fft', 'invalid')))
with pytest.warns(UserWarning):
pytest.raises(ValueError, lambda: set_array_api_strict_flags(
api_version='2021.12',
enabled_extensions=('linalg', 'fft')))
def test_api_version():
# Test defaults
assert xp.__array_api_version__ == '2024.12'
# Test setting the version
set_array_api_strict_flags(api_version='2023.12')
assert xp.__array_api_version__ == '2023.12'
set_array_api_strict_flags(api_version='2022.12')
assert xp.__array_api_version__ == '2022.12'
def test_data_dependent_shapes():
a = asarray([0, 0, 1, 2, 2])
mask = asarray([True, False, True, False, True])
repeats = asarray([1, 1, 2, 2, 2])
# Should not error
unique_all(a)
unique_counts(a)
unique_inverse(a)
unique_values(a)
nonzero(a)
a[mask]
repeat(a, repeats)
repeat(a, 2)
set_array_api_strict_flags(data_dependent_shapes=False)
pytest.raises(RuntimeError, lambda: unique_all(a))
pytest.raises(RuntimeError, lambda: unique_counts(a))
pytest.raises(RuntimeError, lambda: unique_inverse(a))
pytest.raises(RuntimeError, lambda: unique_values(a))
pytest.raises(RuntimeError, lambda: nonzero(a))
pytest.raises(RuntimeError, lambda: repeat(a, repeats))
repeat(a, 2) # Should never error
a[mask] # No error (boolean indexing is a separate flag)
def test_boolean_indexing():
a = asarray([0, 0, 1, 2, 2])
mask = asarray([True, False, True, False, True])
# Should not error
a[mask]
set_array_api_strict_flags(boolean_indexing=False)
pytest.raises(RuntimeError, lambda: a[mask])
linalg_examples = {
'cholesky': lambda: cholesky(xp.eye(3)),
'cross': lambda: cross(xp.asarray([1, 0, 0]), xp.asarray([0, 1, 0])),
'det': lambda: det(xp.eye(3)),
'diagonal': lambda: diagonal(xp.eye(3)),
'eigh': lambda: eigh(xp.eye(3)),
'eigvalsh': lambda: eigvalsh(xp.eye(3)),
'inv': lambda: inv(xp.eye(3)),
'matmul': lambda: matmul(xp.eye(3), xp.eye(3)),
'matrix_norm': lambda: matrix_norm(xp.eye(3)),
'matrix_power': lambda: matrix_power(xp.eye(3), 2),
'matrix_rank': lambda: matrix_rank(xp.eye(3)),
'matrix_transpose': lambda: matrix_transpose(xp.eye(3)),
'outer': lambda: outer(xp.asarray([1, 2, 3]), xp.asarray([4, 5, 6])),
'pinv': lambda: pinv(xp.eye(3)),
'qr': lambda: qr(xp.eye(3)),
'slogdet': lambda: slogdet(xp.eye(3)),
'solve': lambda: solve(xp.eye(3), xp.eye(3)),
'svd': lambda: svd(xp.eye(3)),
'svdvals': lambda: svdvals(xp.eye(3)),
'tensordot': lambda: tensordot(xp.eye(3), xp.eye(3)),
'trace': lambda: trace(xp.eye(3)),
'vecdot': lambda: vecdot(xp.asarray([1, 2, 3]), xp.asarray([4, 5, 6])),
'vector_norm': lambda: vector_norm(xp.asarray([1., 2., 3.])),
}
assert set(linalg_examples) == set(xp.linalg.__all__)
linalg_main_namespace_examples = {
'matmul': lambda: xp.matmul(xp.eye(3), xp.eye(3)),
'matrix_transpose': lambda: xp.matrix_transpose(xp.eye(3)),
'tensordot': lambda: xp.tensordot(xp.eye(3), xp.eye(3)),
'vecdot': lambda: xp.vecdot(xp.asarray([1, 2, 3]), xp.asarray([4, 5, 6])),
'mT': lambda: xp.eye(3).mT,
}
assert set(linalg_main_namespace_examples) == (set(xp.__all__) & set(xp.linalg.__all__)) | {"mT"}
@pytest.mark.parametrize('func_name', linalg_examples.keys())
def test_linalg(func_name):
func = linalg_examples[func_name]
if func_name in linalg_main_namespace_examples:
main_namespace_func = linalg_main_namespace_examples[func_name]
else:
main_namespace_func = lambda: None
# First make sure the example actually works
func()
main_namespace_func()
set_array_api_strict_flags(enabled_extensions=())
pytest.raises(RuntimeError, func)
main_namespace_func()
set_array_api_strict_flags(enabled_extensions=('linalg',))
func()
main_namespace_func()
fft_examples = {
'fft': lambda: fft(xp.asarray([0j, 1j, 0j, 0j])),
'ifft': lambda: ifft(xp.asarray([0j, 1j, 0j, 0j])),
'fftn': lambda: fftn(xp.asarray([[0j, 1j], [0j, 0j]])),
'ifftn': lambda: ifftn(xp.asarray([[0j, 1j], [0j, 0j]])),
'rfft': lambda: rfft(xp.asarray([0., 1., 0., 0.])),
'irfft': lambda: irfft(xp.asarray([0j, 1j, 0j, 0j])),
'rfftn': lambda: rfftn(xp.asarray([[0., 1.], [0., 0.]])),
'irfftn': lambda: irfftn(xp.asarray([[0j, 1j], [0j, 0j]])),
'hfft': lambda: hfft(xp.asarray([0j, 1j, 0j, 0j])),
'ihfft': lambda: ihfft(xp.asarray([0., 1., 0., 0.])),
'fftfreq': lambda: fftfreq(4),
'rfftfreq': lambda: rfftfreq(4),
'fftshift': lambda: fftshift(xp.asarray([0j, 1j, 0j, 0j])),
'ifftshift': lambda: ifftshift(xp.asarray([0j, 1j, 0j, 0j])),
}
assert set(fft_examples) == set(xp.fft.__all__)
@pytest.mark.parametrize('func_name', fft_examples.keys())
def test_fft(func_name):
func = fft_examples[func_name]
# First make sure the example actually works
func()
set_array_api_strict_flags(enabled_extensions=())
pytest.raises(RuntimeError, func)
set_array_api_strict_flags(enabled_extensions=('fft',))
func()
# Test functionality even if the info object is already created
_info = xp.__array_namespace_info__()
api_version_2023_12_examples = {
'__array_namespace_info__': lambda: xp.__array_namespace_info__(),
# Test these functions directly to ensure they are properly decorated
'capabilities': _info.capabilities,
'default_device': _info.default_device,
'default_dtypes': _info.default_dtypes,
'devices': _info.devices,
'dtypes': _info.dtypes,
'clip': lambda: xp.clip(xp.asarray([1, 2, 3]), 1, 2),
'copysign': lambda: xp.copysign(xp.asarray([1., 2., 3.]), xp.asarray([-1., -1., -1.])),
'cumulative_sum': lambda: xp.cumulative_sum(xp.asarray([1, 2, 3])),
'hypot': lambda: xp.hypot(xp.asarray([3., 4.]), xp.asarray([4., 3.])),
'maximum': lambda: xp.maximum(xp.asarray([1, 2, 3]), xp.asarray([2, 3, 4])),
'minimum': lambda: xp.minimum(xp.asarray([1, 2, 3]), xp.asarray([2, 3, 4])),
'moveaxis': lambda: xp.moveaxis(xp.ones((3, 3)), 0, 1),
'repeat': lambda: xp.repeat(xp.asarray([1, 2, 3]), 3),
'searchsorted': lambda: xp.searchsorted(xp.asarray([1, 2, 3]), xp.asarray([0, 1, 2, 3, 4])),
'signbit': lambda: xp.signbit(xp.asarray([-1., 0., 1.])),
'tile': lambda: xp.tile(xp.ones((3, 3)), (2, 3)),
'unstack': lambda: xp.unstack(xp.ones((3, 3)), axis=0),
}
@pytest.mark.parametrize('func_name', api_version_2023_12_examples.keys())
def test_api_version_2023_12(func_name):
func = api_version_2023_12_examples[func_name]
# By default, these functions should not error
func()
# In 2022.12, these functions should error
set_array_api_strict_flags(api_version='2022.12')
pytest.raises(RuntimeError, func)
# Test the behavior gets updated properly
set_array_api_strict_flags(api_version='2023.12')
func()
set_array_api_strict_flags(api_version='2022.12')
pytest.raises(RuntimeError, func)
api_version_2024_12_examples = {
'diff': lambda: xp.diff(xp.asarray([0, 1, 2])),
'nextafter': lambda: xp.nextafter(xp.asarray(0.), xp.asarray(1.)),
'reciprocal': lambda: xp.reciprocal(xp.asarray([2.])),
'take_along_axis': lambda: xp.take_along_axis(xp.zeros((2, 3)),
xp.zeros((1, 4), dtype=xp.int64)),
'count_nonzero': lambda: xp.count_nonzero(xp.arange(3)),
'cumulative_prod': lambda: xp.cumulative_prod(xp.arange(1, 5)),
}
@pytest.mark.parametrize('func_name', api_version_2024_12_examples.keys())
def test_api_version_2024_12(func_name):
func = api_version_2024_12_examples[func_name]
# By default, these functions should not error
func()
# In 2022.12 and 2023.12, these functions should error
set_array_api_strict_flags(api_version='2022.12')
pytest.raises(RuntimeError, func)
set_array_api_strict_flags(api_version='2023.12')
pytest.raises(RuntimeError, func)
# Test the behavior gets updated properly
set_array_api_strict_flags(api_version='2023.12')
pytest.raises(RuntimeError, func)
def test_disabled_extensions():
# Test that xp.extension errors when an extension is disabled, and that
# xp.__all__ is updated properly.
# First test that things are correct on the initial import. Since we have
# already called set_array_api_strict_flags many times throughout running
# the tests, we have to test this in a subprocess.
subprocess_tests = [('''\
import array_api_strict
array_api_strict.linalg # No error
array_api_strict.fft # No error
assert "linalg" in array_api_strict.__all__
assert "fft" in array_api_strict.__all__
assert len(array_api_strict.__all__) == len(set(array_api_strict.__all__))
''', {}),
# Test that the initial population of __all__ works correctly
('''\
from array_api_strict import * # No error
linalg # Should have been imported by the previous line
fft
''', {}),
('''\
from array_api_strict import * # No error
linalg # Should have been imported by the previous line
assert 'fft' not in globals()
''', {"ARRAY_API_STRICT_ENABLED_EXTENSIONS": "linalg"}),
('''\
from array_api_strict import * # No error
fft # Should have been imported by the previous line
assert 'linalg' not in globals()
''', {"ARRAY_API_STRICT_ENABLED_EXTENSIONS": "fft"}),
('''\
from array_api_strict import * # No error
assert 'linalg' not in globals()
assert 'fft' not in globals()
''', {"ARRAY_API_STRICT_ENABLED_EXTENSIONS": ""}),
]
for test, env in subprocess_tests:
try:
subprocess.run([sys.executable, '-c', test], check=True,
capture_output=True, encoding='utf-8', env=env)
except subprocess.CalledProcessError as e:
print(e.stdout, end='')
# Ensure the exception is shown in the output log
raise AssertionError(e.stderr)
assert 'linalg' in xp.__all__
assert 'fft' in xp.__all__
xp.linalg # No error
xp.fft # No error
ns = {}
exec('from array_api_strict import *', ns)
assert 'linalg' in ns
assert 'fft' in ns
set_array_api_strict_flags(enabled_extensions=('linalg',))
assert 'linalg' in xp.__all__
assert 'fft' not in xp.__all__
xp.linalg # No error
pytest.raises(AttributeError, lambda: xp.fft)
ns = {}
exec('from array_api_strict import *', ns)
assert 'linalg' in ns
assert 'fft' not in ns
set_array_api_strict_flags(enabled_extensions=('fft',))
assert 'linalg' not in xp.__all__
assert 'fft' in xp.__all__
pytest.raises(AttributeError, lambda: xp.linalg)
xp.fft # No error
ns = {}
exec('from array_api_strict import *', ns)
assert 'linalg' not in ns
assert 'fft' in ns
set_array_api_strict_flags(enabled_extensions=())
assert 'linalg' not in xp.__all__
assert 'fft' not in xp.__all__
pytest.raises(AttributeError, lambda: xp.linalg)
pytest.raises(AttributeError, lambda: xp.fft)
ns = {}
exec('from array_api_strict import *', ns)
assert 'linalg' not in ns
assert 'fft' not in ns
reset_array_api_strict_flags()
assert 'linalg' in xp.__all__
assert 'fft' in xp.__all__
xp.linalg # No error
xp.fft # No error
ns = {}
exec('from array_api_strict import *', ns)
assert 'linalg' in ns
assert 'fft' in ns
def test_environment_variables():
# Test that the environment variables work as expected
subprocess_tests = [
# ARRAY_API_STRICT_API_VERSION
('''\
import array_api_strict as xp
assert xp.__array_api_version__ == '2024.12'
assert xp.get_array_api_strict_flags()['api_version'] == '2024.12'
''', {}),
*[
(f'''\
import array_api_strict as xp
assert xp.__array_api_version__ == '{version}'
assert xp.get_array_api_strict_flags()['api_version'] == '{version}'
if {version} == '2021.12':
assert hasattr(xp, 'linalg')
assert not hasattr(xp, 'fft')
''', {"ARRAY_API_STRICT_API_VERSION": version}) for version in ('2021.12', '2022.12', '2023.12')],
# ARRAY_API_STRICT_BOOLEAN_INDEXING
('''\
import array_api_strict as xp
a = xp.ones(3)
mask = xp.asarray([True, False, True])
assert xp.all(a[mask] == xp.asarray([1., 1.]))
assert xp.get_array_api_strict_flags()['boolean_indexing'] == True
''', {}),
*[(f'''\
import array_api_strict as xp
a = xp.ones(3)
mask = xp.asarray([True, False, True])
if {boolean_indexing}:
assert xp.all(a[mask] == xp.asarray([1., 1.]))
else:
try:
a[mask]
except RuntimeError:
pass
else:
assert False
assert xp.get_array_api_strict_flags()['boolean_indexing'] == {boolean_indexing}
''', {"ARRAY_API_STRICT_BOOLEAN_INDEXING": boolean_indexing})
for boolean_indexing in ('True', 'False')],
# ARRAY_API_STRICT_DATA_DEPENDENT_SHAPES
('''\
import array_api_strict as xp
a = xp.ones(3)
xp.unique_all(a)
assert xp.get_array_api_strict_flags()['data_dependent_shapes'] == True
''', {}),
*[(f'''\
import array_api_strict as xp
a = xp.ones(3)
if {data_dependent_shapes}:
xp.unique_all(a)
else:
try:
xp.unique_all(a)
except RuntimeError:
pass
else:
assert False
assert xp.get_array_api_strict_flags()['data_dependent_shapes'] == {data_dependent_shapes}
''', {"ARRAY_API_STRICT_DATA_DEPENDENT_SHAPES": data_dependent_shapes})
for data_dependent_shapes in ('True', 'False')],
# ARRAY_API_STRICT_ENABLED_EXTENSIONS
('''\
import array_api_strict as xp
assert hasattr(xp, 'linalg')
assert hasattr(xp, 'fft')
assert xp.get_array_api_strict_flags()['enabled_extensions'] == ('linalg', 'fft')
''', {}),
*[(f'''\
import array_api_strict as xp
assert hasattr(xp, 'linalg') == ('linalg' in {extensions.split(',')})
assert hasattr(xp, 'fft') == ('fft' in {extensions.split(',')})
assert sorted(xp.get_array_api_strict_flags()['enabled_extensions']) == {sorted(set(extensions.split(','))-{''})}
''', {"ARRAY_API_STRICT_ENABLED_EXTENSIONS": extensions})
for extensions in ('', 'linalg', 'fft', 'linalg,fft')],
]
for test, env in subprocess_tests:
try:
subprocess.run([sys.executable, '-c', test], check=True,
capture_output=True, encoding='utf-8', env=env)
except subprocess.CalledProcessError as e:
print(e.stdout, end='')
# Ensure the exception is shown in the output log
raise AssertionError(f"""\
STDOUT:
{e.stderr}
STDERR:
{e.stderr}
TEST:
{test}
ENV:
{env}""")