test_codegen_vm_basic.py 3.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17 18 19
import tvm
import numpy as np

20
def run_jit(fapi, check):
21
    for target in ["llvm", "stackvm"]:
22
        if not tvm.module.enabled(target):
23
            continue
24
        f = tvm.codegen.build_module(fapi, target)
25
        s = f.get_source()
26 27
        check(f)

28 29 30 31
def test_stack_vm_basic():
    a = tvm.nd.array(np.zeros(10, dtype='float32'))
    @tvm.register_func
    def tvm_call_back_get_shape(shape0):
32
        print(shape0)
33 34
        assert shape0 == a.shape[0]

35 36
    n = tvm.var('n')
    Ab = tvm.decl_buffer((n, ), tvm.float32)
37
    stmt = tvm.make.Evaluate(tvm.call_packed("tvm_call_back_get_shape", Ab.shape[0]))
38
    fapi = tvm.ir_pass.MakeAPI(stmt, "print_shape", [Ab], 0, True)
39
    fapi = tvm.ir_pass.LowerTVMBuiltin(fapi)
40
    run_jit(fapi, lambda f: f(a))
41 42 43 44 45 46 47 48


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

def test_stack_vm_loop():
    dtype = 'int64'
49 50 51
    n = tvm.var('n')
    Ab = tvm.decl_buffer((n, ), dtype)
    i = tvm.var('i')
52 53 54 55 56 57 58 59

    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()
60
    fapi = tvm.ir_pass.MakeAPI(stmt, "ramp", [Ab], 0, True)
61
    fapi = tvm.ir_pass.LowerTVMBuiltin(fapi)
62
    a = tvm.nd.array(np.zeros(10, dtype=dtype))
63 64 65 66
    def check(f):
        f(a)
        np.testing.assert_equal(a.asnumpy(), np.arange(a.shape[0]))
    run_jit(fapi, check)
67 68 69 70


def test_stack_vm_cond():
    dtype = 'int64'
71 72
    n = tvm.var('n')
    Ab = tvm.decl_buffer((n, ), dtype)
73 74 75 76 77 78 79 80 81 82

    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()
83
    fapi = tvm.ir_pass.MakeAPI(stmt, "test", [Ab], 0, True)
84
    fapi = tvm.ir_pass.LowerTVMBuiltin(fapi)
85 86 87 88 89 90 91
    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)
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
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)


112
if __name__ == "__main__":
113 114
    test_vm_parallel()
    test_stack_vm_loop()
115
    test_stack_vm_basic()
116
    test_stack_vm_cond()