test_codegen_vm_basic.py 2.75 KB
Newer Older
1 2 3
import tvm
import numpy as np

4
def run_jit(fapi, check):
5
    for target in ["llvm", "stackvm"]:
6
        if not tvm.module.enabled(target):
7
            continue
8
        f = tvm.codegen.build_module(fapi, target)
9
        s = f.get_source()
10 11
        check(f)

12 13 14 15
def test_stack_vm_basic():
    a = tvm.nd.array(np.zeros(10, dtype='float32'))
    @tvm.register_func
    def tvm_call_back_get_shape(shape0):
16
        print(shape0)
17 18
        assert shape0 == a.shape[0]

19 20
    n = tvm.var('n')
    Ab = tvm.decl_buffer((n, ), tvm.float32)
21
    stmt = tvm.make.Evaluate(tvm.call_packed("tvm_call_back_get_shape", Ab.shape[0]))
22
    fapi = tvm.ir_pass.MakeAPI(stmt, "print_shape", [Ab], 0, True)
23
    fapi = tvm.ir_pass.LowerTVMBuiltin(fapi)
24
    run_jit(fapi, lambda f: f(a))
25 26 27 28 29 30 31 32


@tvm.register_func
def tvm_stack_vm_print(*x):
    print(x)

def test_stack_vm_loop():
    dtype = 'int64'
33 34 35
    n = tvm.var('n')
    Ab = tvm.decl_buffer((n, ), dtype)
    i = tvm.var('i')
36 37 38 39 40 41 42 43

    ib = tvm.ir_builder.create()
    A = ib.buffer_ptr(Ab)
    with ib.for_range(0, n - 1, "i") as i:
        A[i + 1] = A[i] + 1
        ib.emit(tvm.call_packed("tvm_stack_vm_print", i))

    stmt = ib.get()
44
    fapi = tvm.ir_pass.MakeAPI(stmt, "ramp", [Ab], 0, True)
45
    fapi = tvm.ir_pass.LowerTVMBuiltin(fapi)
46
    a = tvm.nd.array(np.zeros(10, dtype=dtype))
47 48 49 50
    def check(f):
        f(a)
        np.testing.assert_equal(a.asnumpy(), np.arange(a.shape[0]))
    run_jit(fapi, check)
51 52 53 54


def test_stack_vm_cond():
    dtype = 'int64'
55 56
    n = tvm.var('n')
    Ab = tvm.decl_buffer((n, ), dtype)
57 58 59 60 61 62 63 64 65 66

    ib = tvm.ir_builder.create()
    A = ib.buffer_ptr(Ab)
    with ib.for_range(0, n - 1, "i") as i:
        with ib.if_scope(tvm.make.EQ(i,  4)):
            A[i + 1] = A[i] + 1
        with ib.else_scope():
            A[i + 1] = A[i] + 2

    stmt = ib.get()
67
    fapi = tvm.ir_pass.MakeAPI(stmt, "test", [Ab], 0, True)
68
    fapi = tvm.ir_pass.LowerTVMBuiltin(fapi)
69 70 71 72 73 74 75
    def check(f):
        a = tvm.nd.array(np.zeros(10, dtype=dtype))
        f(a)
        y = np.arange(a.shape[0]) * 2
        y[5:] -= 1
        np.testing.assert_equal(a.asnumpy(), y)
    run_jit(fapi, check)
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
def test_vm_parallel():
    dtype = 'int64'
    n = tvm.var('n')
    Ab = tvm.decl_buffer((n, ), dtype)
    i = tvm.var('i')
    ib = tvm.ir_builder.create()
    A = ib.buffer_ptr(Ab)
    with ib.for_range(0, n, "i", for_type="parallel") as i:
        A[i] = A[i] + 1
    stmt = ib.get()
    fapi = tvm.ir_pass.MakeAPI(stmt, "ramp", [Ab], 0, True)
    fapi = tvm.ir_pass.LowerTVMBuiltin(fapi)
    def check(f):
        a = tvm.nd.array(np.zeros(10, dtype=dtype))
        f(a)
        np.testing.assert_equal(a.asnumpy(), np.ones(a.shape[0]))
    run_jit(fapi, check)


96
if __name__ == "__main__":
97 98
    test_vm_parallel()
    test_stack_vm_loop()
99
    test_stack_vm_basic()
100
    test_stack_vm_cond()