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