test_backend_interpreter.py 4.07 KB
Newer Older
1 2
import numpy as np
import tvm
3
import tvm.testing
4
from tvm import relay
5
from tvm.relay.backend.interpreter import Value, TupleValue
6
from tvm.relay.scope_builder import ScopeBuilder
7
from tvm.relay import testing, create_executor
8 9


10
def check_eval(expr, args, expected_result, mod=None, rtol=1e-07):
11 12 13 14 15 16 17 18 19 20
    # TODO(tqchen) add more types once the schedule register is fixed.
    for target in ["llvm"]:
        ctx = tvm.context(target, 0)
        if not ctx.exist:
            return
        intrp = create_executor(mod=mod, ctx=ctx, target=target)
        result = intrp.evaluate(expr)(*args)
        # use tvm.testing which also set atol
        tvm.testing.assert_allclose(
            result.asnumpy(), expected_result, rtol=rtol)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43


def test_from_scalar():
    np.testing.assert_allclose(Value.from_scalar(1, 'int32').asnumpy(), 1)
    np.testing.assert_allclose(Value.from_scalar(10.0, 'float32').asnumpy(), 10.0)
    np.testing.assert_allclose(Value.from_scalar(True).asnumpy(), True)


def test_tuple_value():
    tv = TupleValue(Value.from_scalar(
        1), Value.from_scalar(2), Value.from_scalar(3))
    np.testing.assert_allclose(tv[0].asnumpy(), 1)
    np.testing.assert_allclose(tv[1].asnumpy(), 2)
    np.testing.assert_allclose(tv[2].asnumpy(), 3)


def test_id():
    x = relay.var('x', 'float32')
    ident = relay.Function([x], x)
    check_eval(ident, [1.0], 1.0)


def test_add_const():
44
    two = relay.add(relay.const(1), relay.const(1))
45 46 47 48 49 50 51
    func = relay.Function([], two)
    check_eval(func, [], 2)


def test_mul_param():
    x = relay.var('x', shape=(10, 10))
    y = relay.var('y', shape=(1, 10))
52
    func = relay.Function([x, y], relay.multiply(x, y))
53 54 55 56 57 58 59 60
    x_data = np.random.rand(10, 10).astype('float32')
    y_data = np.random.rand(1, 10).astype('float32')
    check_eval(func, [x_data, y_data], x_data * y_data)


def test_equal():
    i = relay.var('i', shape=[], dtype='int32')
    j = relay.var('i', shape=[], dtype='int32')
61
    z = relay.equal(i, j)
62 63 64 65 66
    func = relay.Function([i, j], z, ret_type=relay.TensorType([], 'bool'))
    i_data = relay.const(0)
    j_data = relay.const(0)
    check_eval(func, [i_data, j_data], True)

67

68 69
def test_subtract():
    i = relay.var('i', shape=[], dtype='int32')
70
    sub = relay.subtract(i, relay.const(1, dtype='int32'))
71 72 73 74
    func = relay.Function([i], sub, ret_type=relay.TensorType([], 'int32'))
    i_data = np.array(1, dtype='int32')
    check_eval(func, [i_data], 0)

75

76
def test_simple_loop():
77
    mod = relay.module.Module({})
78 79 80
    sum_up = relay.GlobalVar('sum_up')
    i = relay.var('i', shape=[], dtype='int32')
    sb = ScopeBuilder()
81
    with sb.if_scope(relay.equal(i, relay.const(0, dtype='int32'))):
82 83
        sb.ret(i)
    with sb.else_scope():
84
        one_less = relay.subtract(i, relay.const(1, dtype='int32'))
85
        rec_call = relay.Call(sum_up, [one_less])
86
        sb.ret(relay.add(rec_call, i))
87
    func = relay.Function([i], sb.get(), ret_type=relay.TensorType([], 'int32'))
88
    mod[sum_up] = func
89
    i_data = np.array(10, dtype='int32')
90
    check_eval(sum_up, [i_data], sum(range(1, 11)), mod=mod)
91

92

93
def test_loop():
94
    mod = relay.module.Module({})
95 96 97 98
    sum_up = relay.GlobalVar('sum_up')
    i = relay.var('i', shape=[], dtype='int32')
    accum = relay.var('accum', shape=[], dtype='int32')
    sb = ScopeBuilder()
99
    with sb.if_scope(relay.equal(i, relay.const(0))):
100 101
        sb.ret(accum)
    with sb.else_scope():
102 103
        one_less = relay.subtract(i, relay.const(1))
        new_accum = relay.add(accum, i)
104 105
        sb.ret(relay.Call(sum_up, [one_less, new_accum]))
    func = relay.Function([i, accum], sb.get())
106
    mod[sum_up] = func
107 108
    i_data = np.array(10, dtype='int32')
    accum_data = np.array(0, dtype='int32')
109
    check_eval(sum_up, [i_data, accum_data], sum(range(1, 11)), mod=mod)
110

111 112 113 114 115 116 117 118 119

def test_binds():
    x = relay.var("x")
    y = relay.add(x, x)
    intrp = create_executor("debug")
    xx = np.ones((10, 20))
    res = intrp.evaluate(y, binds={x: xx}).asnumpy()
    tvm.testing.assert_allclose(xx + xx, res)

120 121 122 123 124 125 126 127

if __name__ == "__main__":
    test_id()
    test_add_const()
    test_equal()
    test_subtract()
    test_simple_loop()
    test_loop()
128
    test_binds()