| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import numpy as np |
| 12 |
import pytest |
| 13 |
from pytest import param |
| 14 |
|
| 15 |
from hypothesis import HealthCheck, example, given, note, settings, strategies as st |
| 16 |
from hypothesis.extra import numpy as nps |
| 17 |
from hypothesis.extra._array_helpers import _hypothesis_parse_gufunc_signature |
| 18 |
|
| 19 |
from tests.common.debug import find_any, minimal |
| 20 |
|
| 21 |
|
| 22 |
def use_signature_examples(func): |
| 23 |
for sig in [ |
| 24 |
"(),()->()", |
| 25 |
"(i)->()", |
| 26 |
"(i),(i)->()", |
| 27 |
"(m,n),(n,p)->(m,p)", |
| 28 |
"(n),(n,p)->(p)", |
| 29 |
"(m,n),(n)->(m)", |
| 30 |
"(m?,n),(n,p?)->(m?,p?)", |
| 31 |
"(3),(3)->(3)", |
| 32 |
]: |
| 33 |
func = example(sig)(func) |
| 34 |
return func |
| 35 |
|
| 36 |
|
| 37 |
def hy_sig_2_np_sig(hy_sig): |
| 38 |
return ( |
| 39 |
[tuple(d.strip("?") for d in shape) for shape in hy_sig.input_shapes], |
| 40 |
[tuple(d.strip("?") for d in hy_sig.result_shape)], |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
def test_frozen_dims_signature(): |
| 45 |
_hypothesis_parse_gufunc_signature("(2),(3)->(4)") |
| 46 |
|
| 47 |
|
| 48 |
@st.composite |
| 49 |
def gufunc_arrays(draw, shape_strat, **kwargs): |
| 50 |
"""An example user strategy built on top of mutually_broadcastable_shapes.""" |
| 51 |
input_shapes, result_shape = draw(shape_strat) |
| 52 |
arrays_strat = st.tuples(*(nps.arrays(shape=s, **kwargs) for s in input_shapes)) |
| 53 |
return draw(arrays_strat), result_shape |
| 54 |
|
| 55 |
|
| 56 |
@given( |
| 57 |
gufunc_arrays( |
| 58 |
nps.mutually_broadcastable_shapes(signature=np.matmul.signature), |
| 59 |
dtype="float64", |
| 60 |
elements=st.floats(0, 1000), |
| 61 |
) |
| 62 |
) |
| 63 |
def test_matmul_gufunc_shapes(everything): |
| 64 |
arrays, result_shape = everything |
| 65 |
out = np.matmul(*arrays) |
| 66 |
assert out.shape == result_shape |
| 67 |
|
| 68 |
|
| 69 |
@settings(deadline=None, max_examples=10, suppress_health_check=list(HealthCheck)) |
| 70 |
@pytest.mark.parametrize( |
| 71 |
"target_sig", |
| 72 |
("(i),(i)->()", "(m,n),(n,p)->(m,p)", "(n),(n,p)->(p)", "(m,n),(n)->(m)"), |
| 73 |
) |
| 74 |
@given(data=st.data()) |
| 75 |
def test_matmul_signature_can_exercise_all_combination_of_optional_dims( |
| 76 |
target_sig, data |
| 77 |
): |
| 78 |
target_shapes = data.draw( |
| 79 |
nps.mutually_broadcastable_shapes(signature=target_sig, max_dims=0) |
| 80 |
) |
| 81 |
find_any( |
| 82 |
nps.mutually_broadcastable_shapes( |
| 83 |
signature="(m?,n),(n,p?)->(m?,p?)", max_dims=0 |
| 84 |
), |
| 85 |
lambda shapes: shapes == target_shapes, |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
@settings( |
| 90 |
deadline=None, max_examples=50, suppress_health_check=[HealthCheck.nested_given] |
| 91 |
) |
| 92 |
@given( |
| 93 |
min_dims=st.integers(0, 4), |
| 94 |
min_side=st.integers(2, 3), |
| 95 |
n_fixed=st.booleans(), |
| 96 |
data=st.data(), |
| 97 |
) |
| 98 |
def test_matmul_sig_shrinks_as_documented(min_dims, min_side, n_fixed, data): |
| 99 |
sig = "(m?,n),(n,p?)->(m?,p?)" |
| 100 |
if n_fixed: |
| 101 |
n_value = data.draw(st.integers(0, 4)) |
| 102 |
sig = sig.replace("n", str(n_value)) |
| 103 |
else: |
| 104 |
n_value = min_side |
| 105 |
|
| 106 |
note(f"signature: {sig}") |
| 107 |
max_dims = data.draw(st.none() | st.integers(min_dims, 4), label="max_dims") |
| 108 |
max_side = data.draw(st.none() | st.integers(min_side, 6), label="max_side") |
| 109 |
|
| 110 |
smallest_shapes, result = minimal( |
| 111 |
nps.mutually_broadcastable_shapes( |
| 112 |
signature=sig, |
| 113 |
min_side=min_side, |
| 114 |
max_side=max_side, |
| 115 |
min_dims=min_dims, |
| 116 |
max_dims=max_dims, |
| 117 |
) |
| 118 |
) |
| 119 |
note(f"(smallest_shapes, result): {(smallest_shapes, result)}") |
| 120 |
|
| 121 |
# if min_dims >= 1 then core dims are never excluded |
| 122 |
# otherwise, should shrink towards excluding all optional dims |
| 123 |
expected_input_0 = ( |
| 124 |
(n_value,) if min_dims == 0 else (min_side,) * min_dims + (min_side, n_value) |
| 125 |
) |
| 126 |
assert expected_input_0 == smallest_shapes[0] |
| 127 |
|
| 128 |
expected_input_1 = ( |
| 129 |
(n_value,) if min_dims == 0 else (min_side,) * min_dims + (n_value, min_side) |
| 130 |
) |
| 131 |
assert expected_input_1 == smallest_shapes[1] |
| 132 |
|
| 133 |
|
| 134 |
def gufunc_sig_to_einsum_sig(gufunc_sig): |
| 135 |
"""E.g. (i,j),(j,k)->(i,k) becomes ...ij,...jk->...ik""" |
| 136 |
|
| 137 |
def einlabels(labels): |
| 138 |
assert "x" not in labels, "we reserve x for fixed-dimensions" |
| 139 |
return "..." + "".join(i if not i.isdigit() else "x" for i in labels) |
| 140 |
|
| 141 |
gufunc_sig = _hypothesis_parse_gufunc_signature(gufunc_sig) |
| 142 |
input_sig = ",".join(map(einlabels, gufunc_sig.input_shapes)) |
| 143 |
return input_sig + "->" + einlabels(gufunc_sig.result_shape) |
| 144 |
|
| 145 |
|
| 146 |
@pytest.mark.parametrize( |
| 147 |
"gufunc_sig", |
| 148 |
[ |
| 149 |
param("()->()", id="unary sum"), |
| 150 |
param("(),()->()", id="binary sum"), |
| 151 |
param("(),(),()->()", id="trinary sum"), |
| 152 |
param("(i)->()", id="sum1d"), |
| 153 |
param("(i,j)->(j)", id="sum rows"), |
| 154 |
param("(i),(i)->()", id="inner1d"), |
| 155 |
param("(i),(i),(i)->()", id="trinary inner1d"), |
| 156 |
param("(m,n),(n,p)->(m,p)", id="matmat"), |
| 157 |
param("(n),(n,p)->(p)", id="vecmat"), |
| 158 |
param("(i,t),(j,t)->(i,j)", id="outer-inner"), |
| 159 |
param("(3),(3)->(3)", id="cross1d"), |
| 160 |
param("(i,j)->(j,i)", id="transpose"), |
| 161 |
param("(i),(j)->(i,j)", id="outer"), |
| 162 |
param("(i,3),(3,k)->(3,i,k)", id="fixed dim outer product"), |
| 163 |
param("(i),(j),(k)->(i,j,k)", id="trinary outer"), |
| 164 |
param("(i,i)->(i)", id="trace"), |
| 165 |
param("(j,i,i,j)->(i,j)", id="bigger trace"), |
| 166 |
param("(k),(j,i,k,i,j),(j,i)->(i,j)", id="trace product"), |
| 167 |
], |
| 168 |
) |
| 169 |
@given(data=st.data()) |
| 170 |
def test_einsum_gufunc_shapes(gufunc_sig, data): |
| 171 |
arrays, result_shape = data.draw( |
| 172 |
gufunc_arrays( |
| 173 |
nps.mutually_broadcastable_shapes(signature=gufunc_sig), |
| 174 |
dtype="float64", |
| 175 |
elements=st.floats(0, 1000), |
| 176 |
), |
| 177 |
label="arrays, result_shape", |
| 178 |
) |
| 179 |
out = np.einsum(gufunc_sig_to_einsum_sig(gufunc_sig), *arrays) |
| 180 |
assert out.shape == result_shape |