Skip to content

Better undefined dtype behaviour #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion xptests/hypothesis_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ def _dtypes_sorter(dtype_pair: Tuple[DataType, DataType]):
key += 1
return key

promotable_dtypes: List[Tuple[DataType, DataType]] = sorted(dh.promotion_table.keys(), key=_dtypes_sorter)
_promotable_dtypes = list(dh.promotion_table.keys())
if FILTER_UNDEFINED_DTYPES:
_promotable_dtypes = [
(d1, d2) for d1, d2 in _promotable_dtypes
if not isinstance(d1, _UndefinedStub) or not isinstance(d2, _UndefinedStub)
]
promotable_dtypes: List[Tuple[DataType, DataType]] = sorted(_promotable_dtypes, key=_dtypes_sorter)

def mutually_promotable_dtypes(
max_size: Optional[int] = 2,
Expand Down
10 changes: 9 additions & 1 deletion xptests/test_array2scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@

def make_param(method_name: str, dtype: DataType) -> Param:
stype = method_stype[method_name]
if isinstance(dtype, xp._UndefinedStub):
marks = pytest.mark.skip(reason=f"xp.{dtype.name} not defined")
else:
marks = ()
return pytest.param(
method_name, dtype, stype, id=f"{method_name}({dh.dtype_to_name[dtype]})"
method_name,
dtype,
stype,
id=f"{method_name}({dh.dtype_to_name[dtype]})",
marks=marks,
)


Expand Down
4 changes: 4 additions & 0 deletions xptests/test_elementwise_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
def make_unary_params(
elwise_func_name: str, dtypes: Sequence[DataType]
) -> List[UnaryParam]:
if hh.FILTER_UNDEFINED_DTYPES:
dtypes = [d for d in dtypes if not isinstance(d, xp._UndefinedStub)]
strat = xps.arrays(dtype=st.sampled_from(dtypes), shape=hh.shapes())
func = getattr(xp, elwise_func_name)
op_name = func_to_op[elwise_func_name]
Expand Down Expand Up @@ -93,6 +95,8 @@ class FuncType(Enum):
def make_binary_params(
elwise_func_name: str, dtypes: Sequence[DataType]
) -> List[BinaryParam]:
if hh.FILTER_UNDEFINED_DTYPES:
dtypes = [d for d in dtypes if not isinstance(d, xp._UndefinedStub)]
dtypes_strat = st.sampled_from(dtypes)

def make_param(
Expand Down
46 changes: 19 additions & 27 deletions xptests/test_type_promotion.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""
https://data-apis.github.io/array-api/latest/API_specification/type_promotion.html
"""
import math
from collections import defaultdict
from typing import Tuple, Union, List
from typing import List, Tuple, Union

import pytest
from hypothesis import assume, given, reject
Expand All @@ -14,9 +13,8 @@
from . import hypothesis_helpers as hh
from . import pytest_helpers as ph
from . import xps
from .typing import DataType, ScalarType, Param
from .function_stubs import elementwise_functions

from .typing import DataType, Param, ScalarType

# TODO: move tests not covering elementwise funcs/ops into standalone tests
# result_type, meshgrid, tensordor, vecdot
Expand All @@ -28,29 +26,6 @@ def test_result_type(dtypes):
ph.assert_dtype("result_type", dtypes, out, repr_name="out")


# The number and size of generated arrays is arbitrarily limited to prevent
# meshgrid() running out of memory.
@given(
dtypes=hh.mutually_promotable_dtypes(5, dtypes=dh.numeric_dtypes),
data=st.data(),
)
def test_meshgrid(dtypes, data):
arrays = []
shapes = data.draw(
hh.mutually_broadcastable_shapes(
len(dtypes), min_dims=1, max_dims=1, max_side=5
),
label="shapes",
)
for i, (dtype, shape) in enumerate(zip(dtypes, shapes), 1):
x = data.draw(xps.arrays(dtype=dtype, shape=shape), label=f"x{i}")
arrays.append(x)
assert math.prod(x.size for x in arrays) <= hh.MAX_ARRAY_SIZE # sanity check
out = xp.meshgrid(*arrays)
for i, x in enumerate(out):
ph.assert_dtype("meshgrid", dtypes, x.dtype, repr_name=f"out[{i}].dtype")


bitwise_shift_funcs = [
"bitwise_left_shift",
"bitwise_right_shift",
Expand Down Expand Up @@ -78,6 +53,14 @@ def make_id(
return f"{func_name}({f_args}) -> {f_out_dtype}"


def mark_stubbed_dtypes(*dtypes):
for dtype in dtypes:
if isinstance(dtype, xp._UndefinedStub):
return pytest.mark.skip(reason=f"xp.{dtype.name} not defined")
else:
return ()


func_params: List[Param[str, Tuple[DataType, ...], DataType]] = []
for func_name in elementwise_functions.__all__:
valid_in_dtypes = dh.func_in_dtypes[func_name]
Expand All @@ -90,6 +73,7 @@ def make_id(
(in_dtype,),
out_dtype,
id=make_id(func_name, (in_dtype,), out_dtype),
marks=mark_stubbed_dtypes(in_dtype, out_dtype),
)
func_params.append(p)
elif ndtypes == 2:
Expand All @@ -103,6 +87,7 @@ def make_id(
(in_dtype1, in_dtype2),
out_dtype,
id=make_id(func_name, (in_dtype1, in_dtype2), out_dtype),
marks=mark_stubbed_dtypes(in_dtype1, in_dtype2, out_dtype),
)
func_params.append(p)
else:
Expand Down Expand Up @@ -143,6 +128,7 @@ def test_func_promotion(func_name, in_dtypes, out_dtype, data):
(dtype1, dtype2),
promoted_dtype,
id=make_id("", (dtype1, dtype2), promoted_dtype),
marks=mark_stubbed_dtypes(dtype1, dtype2, promoted_dtype),
)
promotion_params.append(p)

Expand Down Expand Up @@ -194,6 +180,7 @@ def test_vecdot(in_dtypes, out_dtype, shapes, data):
(in_dtype,),
out_dtype,
id=make_id(op, (in_dtype,), out_dtype),
marks=mark_stubbed_dtypes(in_dtype, out_dtype),
)
op_params.append(p)
else:
Expand All @@ -206,6 +193,7 @@ def test_vecdot(in_dtypes, out_dtype, shapes, data):
(in_dtype1, in_dtype2),
out_dtype,
id=make_id(op, (in_dtype1, in_dtype2), out_dtype),
marks=mark_stubbed_dtypes(in_dtype1, in_dtype2, out_dtype),
)
op_params.append(p)
# We generate params for abs seperately as it does not have an associated symbol
Expand All @@ -216,6 +204,7 @@ def test_vecdot(in_dtypes, out_dtype, shapes, data):
(in_dtype,),
in_dtype,
id=make_id("__abs__", (in_dtype,), in_dtype),
marks=mark_stubbed_dtypes(in_dtype),
)
op_params.append(p)

Expand Down Expand Up @@ -263,6 +252,7 @@ def test_op_promotion(op, expr, in_dtypes, out_dtype, data):
(in_dtype1, in_dtype2),
promoted_dtype,
id=make_id(op, (in_dtype1, in_dtype2), promoted_dtype),
marks=mark_stubbed_dtypes(in_dtype1, in_dtype2, promoted_dtype),
)
inplace_params.append(p)

Expand Down Expand Up @@ -301,6 +291,7 @@ def test_inplace_op_promotion(op, expr, in_dtypes, out_dtype, shapes, data):
in_stype,
out_dtype,
id=make_id(op, (in_dtype, in_stype), out_dtype),
marks=mark_stubbed_dtypes(in_dtype, out_dtype),
)
op_scalar_params.append(p)

Expand Down Expand Up @@ -333,6 +324,7 @@ def test_op_scalar_promotion(op, expr, in_dtype, in_stype, out_dtype, data):
dtype,
in_stype,
id=make_id(op, (dtype, in_stype), dtype),
marks=mark_stubbed_dtypes(dtype),
)
inplace_scalar_params.append(p)

Expand Down