test_target_codegen_static_init.py 2.2 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
import tvm
18
from tvm import te
19
import ctypes
20 21
import numpy as np

22
def test_static_callback():
23
    dtype = 'int64'
24 25 26 27
    n = te.size_var('n')
    Ab = tvm.tir.decl_buffer((n, ), dtype)
    i = te.size_var('i')
    ib = tvm.tir.ir_builder.create()
28
    A = ib.buffer_ptr(Ab)
29
    cp = te.thread_axis((0, 1), "cop")
30
    finit = tvm.tir.StringImm("TVMBackendRunOnce")
31 32 33 34
    ib.scope_attr(cp, "coproc_uop_scope", finit)
    with ib.for_range(0, n, "i", for_type="parallel") as i:
        A[i] = A[i] + 1
    stmt = ib.get()
35
    fapi = tvm.tir.ir_pass.MakeAPI(stmt, "ramp", [Ab], 0, True)
36
    f = tvm.driver.build(fapi, target="llvm")
37 38 39 40 41
    a = tvm.nd.array(np.zeros(10, dtype=dtype))
    f(a)
    f(a)
    np.testing.assert_equal(a.asnumpy(), np.ones(a.shape[0]))

42 43
def test_static_init():
    dtype = 'int64'
44 45 46 47 48
    n = te.size_var('n')
    Ab = tvm.tir.decl_buffer((n, ), dtype)
    i = te.size_var('i')
    ib = tvm.tir.ir_builder.create()
    handle = tvm.tir.call_intrin("handle", "tvm_static_handle")
49
    ib.emit(
50
        tvm.tir.call_packed("test_static_callback", handle, Ab))
51 52 53 54 55 56 57

    @tvm.register_func("test_static_callback")
    def test_cb(sh, A):
        assert isinstance(sh, ctypes.c_void_p)
        return sh

    stmt = ib.get()
58
    fapi = tvm.tir.ir_pass.MakeAPI(stmt, "ramp", [Ab], 0, True)
59
    f = tvm.driver.build(fapi, target="llvm")
60 61 62
    a = tvm.nd.array(np.zeros(10, dtype=dtype))
    f(a)

63 64

if __name__ == "__main__":
65
    test_static_callback()
66
    test_static_init()