|
| 1 | +""" |
| 2 | +https://github.com/data-apis/array-api/blob/master/spec/API_specification/broadcasting.md |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +from hypothesis import assume, given |
| 7 | +from hypothesis import strategies as st |
| 8 | + |
| 9 | +from .. import _array_module as xp |
| 10 | +from .. import dtype_helpers as dh |
| 11 | +from .. import hypothesis_helpers as hh |
| 12 | +from .. import pytest_helpers as ph |
| 13 | +from .._array_module import _UndefinedStub |
| 14 | +from ..algos import BroadcastError, _broadcast_shapes |
| 15 | +from ..function_stubs import elementwise_functions |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize( |
| 19 | + "shape1, shape2, expected", |
| 20 | + [ |
| 21 | + [(8, 1, 6, 1), (7, 1, 5), (8, 7, 6, 5)], |
| 22 | + [(5, 4), (1,), (5, 4)], |
| 23 | + [(5, 4), (4,), (5, 4)], |
| 24 | + [(15, 3, 5), (15, 1, 5), (15, 3, 5)], |
| 25 | + [(15, 3, 5), (3, 5), (15, 3, 5)], |
| 26 | + [(15, 3, 5), (3, 1), (15, 3, 5)], |
| 27 | + ], |
| 28 | +) |
| 29 | +def test_broadcast_shapes(shape1, shape2, expected): |
| 30 | + assert _broadcast_shapes(shape1, shape2) == expected |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize( |
| 34 | + "shape1, shape2", |
| 35 | + [ |
| 36 | + [(3,), (4,)], # dimension does not match |
| 37 | + [(2, 1), (8, 4, 3)], # second dimension does not match |
| 38 | + [(15, 3, 5), (15, 3)], # singleton dimensions can only be prepended |
| 39 | + ], |
| 40 | +) |
| 41 | +def test_broadcast_shapes_fails_on_bad_shapes(shape1, shape2): |
| 42 | + with pytest.raises(BroadcastError): |
| 43 | + _broadcast_shapes(shape1, shape2) |
| 44 | + |
| 45 | + |
| 46 | +# TODO: Extend this to all functions (not just elementwise), and handle |
| 47 | +# functions that take more than 2 args |
| 48 | +@pytest.mark.parametrize( |
| 49 | + "func_name", [i for i in elementwise_functions.__all__ if ph.nargs(i) > 1] |
| 50 | +) |
| 51 | +@given(shape1=hh.shapes(), shape2=hh.shapes(), data=st.data()) |
| 52 | +def test_broadcasting_hypothesis(func_name, shape1, shape2, data): |
| 53 | + dtype = data.draw(st.sampled_from(dh.func_in_dtypes[func_name]), label="dtype") |
| 54 | + if hh.FILTER_UNDEFINED_DTYPES: |
| 55 | + assume(not isinstance(dtype, _UndefinedStub)) |
| 56 | + func = getattr(xp, func_name) |
| 57 | + if isinstance(func, xp._UndefinedStub): |
| 58 | + func._raise() |
| 59 | + args = [xp.ones(shape1, dtype=dtype), xp.ones(shape2, dtype=dtype)] |
| 60 | + try: |
| 61 | + broadcast_shape = _broadcast_shapes(shape1, shape2) |
| 62 | + except BroadcastError: |
| 63 | + ph.raises( |
| 64 | + Exception, |
| 65 | + lambda: func(*args), |
| 66 | + f"{func_name} should raise an exception from not being able to broadcast inputs with hh.shapes {(shape1, shape2)}", |
| 67 | + ) |
| 68 | + else: |
| 69 | + result = ph.doesnt_raise( |
| 70 | + lambda: func(*args), |
| 71 | + f"{func_name} raised an unexpected exception from broadcastable inputs with hh.shapes {(shape1, shape2)}", |
| 72 | + ) |
| 73 | + assert result.shape == broadcast_shape, "broadcast hh.shapes incorrect" |
0 commit comments