|
| 1 | +from contextlib import contextmanager |
| 2 | + |
| 3 | +import dask |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | +import dask.array as da |
| 7 | + |
| 8 | +from array_api_compat import array_namespace |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def xp(): |
| 13 | + """Fixture returning the wrapped dask namespace""" |
| 14 | + return array_namespace(da.empty(0)) |
| 15 | + |
| 16 | + |
| 17 | +@contextmanager |
| 18 | +def assert_no_compute(): |
| 19 | + """ |
| 20 | + Context manager that raises if at any point inside it anything calls compute() |
| 21 | + or persist(), e.g. as it can be triggered implicitly by __bool__, __array__, etc. |
| 22 | + """ |
| 23 | + def get(dsk, *args, **kwargs): |
| 24 | + raise AssertionError("Called compute() or persist()") |
| 25 | + |
| 26 | + with dask.config.set(scheduler=get): |
| 27 | + yield |
| 28 | + |
| 29 | + |
| 30 | +def test_assert_no_compute(): |
| 31 | + """Test the assert_no_compute context manager""" |
| 32 | + a = da.asarray(True) |
| 33 | + with pytest.raises(AssertionError, match="Called compute"): |
| 34 | + with assert_no_compute(): |
| 35 | + bool(a) |
| 36 | + |
| 37 | + # Exiting the context manager restores the original scheduler |
| 38 | + assert bool(a) is True |
| 39 | + |
| 40 | + |
| 41 | +# Test no_compute for functions that use generic _aliases with xp=np |
| 42 | + |
| 43 | +def test_unary_ops_no_compute(xp): |
| 44 | + with assert_no_compute(): |
| 45 | + a = xp.asarray([1.5, -1.5]) |
| 46 | + xp.ceil(a) |
| 47 | + xp.floor(a) |
| 48 | + xp.trunc(a) |
| 49 | + xp.sign(a) |
| 50 | + |
| 51 | + |
| 52 | +def test_matmul_tensordot_no_compute(xp): |
| 53 | + A = da.ones((4, 4), chunks=2) |
| 54 | + B = da.zeros((4, 4), chunks=2) |
| 55 | + with assert_no_compute(): |
| 56 | + xp.matmul(A, B) |
| 57 | + xp.tensordot(A, B) |
| 58 | + |
| 59 | + |
| 60 | +# Test no_compute for functions that are fully bespoke for dask |
| 61 | + |
| 62 | +def test_asarray_no_compute(xp): |
| 63 | + with assert_no_compute(): |
| 64 | + a = xp.arange(10) |
| 65 | + xp.asarray(a) |
| 66 | + xp.asarray(a, dtype=np.int16) |
| 67 | + xp.asarray(a, dtype=a.dtype) |
| 68 | + xp.asarray(a, copy=True) |
| 69 | + xp.asarray(a, copy=True, dtype=np.int16) |
| 70 | + xp.asarray(a, copy=True, dtype=a.dtype) |
| 71 | + xp.asarray(a, copy=False) |
| 72 | + xp.asarray(a, copy=False, dtype=a.dtype) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("copy", [True, False]) |
| 76 | +def test_astype_no_compute(xp, copy): |
| 77 | + with assert_no_compute(): |
| 78 | + a = xp.arange(10) |
| 79 | + xp.astype(a, np.int16, copy=copy) |
| 80 | + xp.astype(a, a.dtype, copy=copy) |
| 81 | + |
| 82 | + |
| 83 | +def test_clip_no_compute(xp): |
| 84 | + with assert_no_compute(): |
| 85 | + a = xp.arange(10) |
| 86 | + xp.clip(a) |
| 87 | + xp.clip(a, 1) |
| 88 | + xp.clip(a, 1, 8) |
| 89 | + |
| 90 | + |
| 91 | +def test_generators_are_lazy(xp): |
| 92 | + """ |
| 93 | + Test that generator functions are fully lazy, e.g. that |
| 94 | + da.ones(n) is not implemented as da.asarray(np.ones(n)) |
| 95 | + """ |
| 96 | + size = 100_000_000_000 # 800 GB |
| 97 | + chunks = size // 10 # 10x 80 GB chunks |
| 98 | + |
| 99 | + with assert_no_compute(): |
| 100 | + xp.zeros(size, chunks=chunks) |
| 101 | + xp.ones(size, chunks=chunks) |
| 102 | + xp.empty(size, chunks=chunks) |
| 103 | + xp.full(size, fill_value=123, chunks=chunks) |
| 104 | + a = xp.arange(size, chunks=chunks) |
| 105 | + xp.zeros_like(a) |
| 106 | + xp.ones_like(a) |
| 107 | + xp.empty_like(a) |
| 108 | + xp.full_like(a, fill_value=123) |
0 commit comments