def test_myia_struct_arg(backend): @myia(backend=backend) def f(pt): return pt.x x = f(Point(5, 6)) assert x == 5
def test_myia_return_struct(backend): @myia(backend=backend) def f(x, y): return Point(x, y) pt = f(5, 6) assert pt == Point(5, 6)
def test_myia_return_struct(_backend_fixture): backend = _backend_fixture @myia(backend=backend) def f(x, y): return Point(x, y) pt = f(5, 6) assert pt == Point(5, 6)
def test_myia_struct_arg(_backend_fixture): backend = _backend_fixture @myia(backend=backend) def f(pt): return pt.x x = f(Point(5, 6)) assert x == 5
def test_from_canonical(): def _convert(data, typ): return from_canonical(data, to_abstract_test(typ)) # Leaves assert _convert(True, Bool) is True assert _convert(False, Bool) is False assert _convert(10, i64) == 10 assert _convert(1.5, f64) == 1.5 # Tuple -> Class conversion pt = Point(1, 2) assert _convert((1, 2), Point(i64, i64)) == pt assert _convert(((1, 2), (1, 2)), (Point(i64, i64), Point(i64, i64))) == ( pt, pt, )
def test_hypermap_python(): # Normal assert hyper_map(scalar_add, 10, 20) == 30 assert hyper_map(scalar_add, (1, 2), (10, 20)) == (11, 22) assert hyper_map(scalar_add, [1, 2, 3], [3, 2, 1]) == [4, 4, 4] assert (hyper_map(scalar_add, numpy.ones((2, 2)), numpy.ones( (2, 2))) == 2 * numpy.ones((2, 2))).all() # Broadcast assert hyper_map(scalar_add, [1, 2, 3], 10) == [11, 12, 13] assert hyper_map(scalar_add, Point([1, 2], (3, 4)), Point(10, 100)) == Point([11, 12], (103, 104)) assert (hyper_map(scalar_add, numpy.ones((2, 2)), 9) == 10 * numpy.ones( (2, 2))).all() # Provide fn_leaf adder = HyperMap(fn_leaf=scalar_add) assert adder((1, 2), (10, 20)) == (11, 22) assert (adder(numpy.ones((2, 2)), numpy.ones((2, 2))) == 2 * numpy.ones( (2, 2))).all()
def test_build_value(): assert build_value(S(1)) == 1 with pytest.raises(ValueError): build_value(S(t=ty.Int[64])) assert build_value(S(t=ty.Int[64]), default=ANYTHING) is ANYTHING assert build_value(T([S(1), S(2)])) == (1, 2) loop = InferenceLoop(errtype=Exception) p = loop.create_pending(resolve=(lambda: None), priority=(lambda: None)) with pytest.raises(ValueError): assert build_value(S(p, t=ty.Int[64])) is p assert build_value(S(p, t=ty.Int[64]), default=ANYTHING) is ANYTHING p.set_result(1234) assert build_value(S(p, t=ty.Int[64])) == 1234 pt = Point(1, 2) assert build_value(to_abstract_test(pt)) == pt
def test_repr(): s1 = to_abstract_test(1) assert repr(s1) == "AbstractScalar(Int[64] = 1)" s2 = to_abstract_test(f32) assert repr(s2) == "AbstractScalar(Float[32])" t1 = to_abstract_test((1, f32)) assert repr(t1) == f"AbstractTuple((Int[64] = 1, Float[32]))" a1 = to_abstract_test(af32_of(4, 5)) assert repr(a1) == f"AbstractArray(Float[32] x 4 x 5)" p1 = to_abstract_test(Point(1, f32)) assert repr( p1) == f"AbstractClass(Point(x :: Int[64] = 1, y :: Float[32]))" j1 = AbstractJTagged(to_abstract_test(1)) assert repr(j1) == f"AbstractJTagged(J(Int[64] = 1))" h1 = AbstractHandle(to_abstract_test(1)) assert repr(h1) == f"AbstractHandle(H(Int[64] = 1))" kw1 = AbstractKeywordArgument("bucket", to_abstract_test(1)) assert repr(kw1) == f"AbstractKeywordArgument(KW(bucket :: Int[64] = 1))" ty1 = Ty(f32) assert repr(ty1) == "AbstractType(Ty(AbstractScalar(Float[32])))" e1 = AbstractError(DEAD) assert repr(e1) == "AbstractError(E(DEAD))" f1 = AbstractFunction(P.scalar_mul) assert repr(f1) == "AbstractFunction(scalar_mul)" fa = AbstractFunction(value=ANYTHING) assert repr(fa) == "AbstractFunction(ANYTHING)" tu1 = AbstractTaggedUnion([[13, s2], [4, to_abstract_test(i16)]]) assert repr(tu1) == "AbstractTaggedUnion(U(4 :: Int[16], 13 :: Float[32]))" bot = AbstractBottom() assert repr(bot) == "AbstractBottom(⊥)"
int1_np64 = np.int64(17) int2_np64 = np.int64(29) int1_np32 = np.int32(37) int2_np32 = np.int32(41) fp1 = 2.7 fp2 = 6.91 fp1_np64 = np.float64(3.3) fp2_np64 = np.float64(7.23) fp1_np32 = np.float32(3.9) fp2_np32 = np.float32(9.29) pt1 = Point(10, 20) pt2 = Point(100, 200) @mt( mono_scalar(int1, int2_np64), mono_scalar(int1_np64, int2_np64), mono_scalar(fp1, fp2_np64), mono_scalar(fp1_np64, fp2_np64), mono_scalar(fp1_np32, fp2_np32), mono_scalar(int1_np32, int2_np32), ) def test_prim_arithmetic_np_same_precision(x, y): def test_prim_mul_np(x, y): return x * y
def test_to_canonical(): def _convert(data, typ): return to_canonical(data, to_abstract_test(typ)) # Leaves assert _convert(True, Bool) is True assert _convert(False, Bool) is False assert _convert(10, i64) == 10 assert _convert(1.5, f64) == 1.5 assert _convert([], []) == () with pytest.raises(TypeError): _convert([], [f64]) with pytest.raises(TypeError): _convert([1, 2], []) # Class -> Tuple conversion pt = Point(1, 2) pt3 = Point3D(1, 2, 3) assert list(_convert(pt, Point(i64, i64))) == [1, 2] with pytest.raises(TypeError): _convert((1, 2), Point(i64, i64)) assert list(_convert((pt, pt), (Point(i64, i64), Point(i64, i64)))) == [ (1, 2), (1, 2), ] li = _convert([1], [i64]) assert (isinstance(li, tuple) and li[0] == 1 and isinstance(li[1], TaggedValue) and li[1].value == ()) # Arrays fmat = np.ones((5, 8)) imat = np.ones((5, 8), dtype="int32") assert _convert(fmat, af64_of(5, 8)) is fmat assert _convert(imat, ai32_of(5, 8)) is imat with pytest.raises(TypeError): _convert(imat, ai64_of(5, 8)) with pytest.raises(TypeError): _convert(imat, ai32_of(4, 8)) # Misc errors with pytest.raises(TypeError): _convert(10, f64) with pytest.raises(TypeError): _convert(1.5, i64) with pytest.raises(TypeError): _convert(10, (i64, i64)) with pytest.raises(TypeError): _convert((1, ), (i64, i64)) with pytest.raises(TypeError): _convert((1, 2, 3), (i64, i64)) with pytest.raises(TypeError): _convert((1, 2, 3), [i64]) with pytest.raises(TypeError): _convert(pt3, Point(i64, i64)) with pytest.raises(TypeError): _convert(10, ai64_of()) with pytest.raises(TypeError): _convert(10, ai64_of()) with pytest.raises(TypeError): _convert(1, Bool) with pytest.raises(TypeError): _convert(1, D(x=i64)) with pytest.raises(TypeError): _convert({"x": 2.0}, D(x=i64)) with pytest.raises(TypeError): _convert({"x": 2.0, "y": 1}, D(x=i64)) with pytest.raises(TypeError): _convert({"y": 2.0}, D(x=i64)) with pytest.raises(TypeError): _convert("x", 1.0) with pytest.raises(TypeError): _convert(1.0, to_abstract_test("x")) with pytest.raises(TypeError): _convert(1.0, to_abstract_test(HandleInstance(1.0))) v = to_device(22, None) with pytest.raises(TypeError): _convert(v, f64)
*args, expected = test assert myia_fn(*args) == expected @mt(run(None), run(42)) def test_is_(x): return x is None @mt(run(None), run(42)) def test_is_not(x): return x is not None @mt( run(1, 1.7, Point(3, 4), (8, 9)), run(0, 1.7, Point(3, 4), (8, 9)), run(-1, 1.7, Point(3, 4), (8, 9)), ) def test_tagged(c, x, y, z): if c > 0: return tagged(x) elif c == 0: return tagged(y) else: return tagged(z) @mt(run("hey", 2), run("idk", 5)) def test_string_eq(s, x): if s == "idk":
import pytest from myia.abstract import AbstractFunction, TypedPrimitive from myia.operations import partial, primitives as P from myia.pipeline import scalar_parse, scalar_pipeline, steps from myia.testing.common import Point, i64, to_abstract_test from myia.validate import ValidationError, validate, validate_abstract Point_a = Point(i64, i64) pip = scalar_pipeline.with_steps( steps.step_parse, steps.step_infer, steps.step_specialize, steps.step_validate, ) pip_ec = scalar_pipeline.with_steps( steps.step_parse, steps.step_infer, steps.step_specialize, steps.step_simplify_types, steps.step_validate, ) def run(pip, fn, types): res = pip(input=fn, argspec=[to_abstract_test(t) for t in types]) return res["graph"]
def test_arithmetic_data_python(): assert Point(1, 2) + Point(10, 20) == Point(11, 22) assert Point(1, 2) + 10 == Point(11, 12)
from ..test_algos import make_tree from ..test_infer import infer_scalar hyper_map_notuple = HyperMap(nonleaf=(lib.AbstractArray, lib.AbstractUnion, lib.AbstractClassBase)) hyper_map_nobroadcast = HyperMap(broadcast=False) @mt( infer_scalar(i64, i64, result=i64), infer_scalar(f64, f64, result=f64), infer_scalar([f64], [f64], result=[f64]), infer_scalar([[f64]], [[f64]], result=[[f64]]), infer_scalar((i64, f64), (i64, f64), result=(i64, f64)), infer_scalar(Point(i64, i64), Point(i64, i64), result=Point(i64, i64)), infer_scalar(ai64_of(2, 5), ai64_of(2, 5), result=ai64_of(2, 5)), infer_scalar(ai64_of(2, 5), i64, result=ai64_of(2, 5)), infer_scalar(ai64_of(1, 5), ai64_of(2, 1), result=ai64_of(2, 5)), infer_scalar(i64, f64, result=InferenceError), infer_scalar(ai64_of(2, 5), af64_of(2, 5), result=InferenceError), infer_scalar(U(i64, (i64, i64)), U(i64, (i64, i64)), result=U(i64, (i64, i64))), infer_scalar(U(i64, (i64, i64)), U(i64, (i64, i64, i64)), result=InferenceError), infer_scalar({ "x": i64, "y": i64 }, {
def f2(x, y): return Point(x, y)
@run_debug(2) def test_dict(x): return {"x": x} @run_lang(13) def test_tuple(x): return x, x + 1, x + 2 @run_lang((1, 2, 3, 4)) def test_getitem(x): return x[1] @run_debug(Point(x=5, y=2)) def test_getattr(pt): return pt.x @run_debug(Point(x=5, y=2)) def test_getattr_function(pt): return getattr(pt, "x") @run_debug(2, 3) def test_method(x, y): return x.__add__(y) ################
def f3(x, y): return Point(x, y)
import pytest from myia.operations import tagged from myia.testing.common import Point from myia.testing.multitest import Multiple, mt, run run_relay_debug = run.configure( backend=Multiple( pytest.param( ("relay", {"exec_kind": "debug"}), id="relay-cpu-debug", marks=pytest.mark.relay, ) ) ) @mt(run_relay_debug(0, 1.7, Point(3, 4), (8, 9)),) def test_tagged(c, x, y, z): if c > 0: return tagged(x) elif c == 0: return tagged(y) else: return tagged(z)
def f(x, y): return Point(x, y)
def test_struct(x, y): return Point(x, y)
def test_record(x): return Point(x, x)
def test_struct2(x, y): p = Point(x, y) return p.x + p.y