test_topi_tensor.py 4.59 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
"""Test code for tensor operator"""
import numpy as np
import tvm
20
from tvm import te
21
import topi
22
import topi.testing
23
from tvm.contrib.pickle_memoize import memoize
24
from tvm.contrib.nvcc import have_fp16
25 26 27 28 29 30 31

def verify_elemwise_sum(num_args, dtype):
    shape = (3,5,4)

    tvm_placeholders = []
    for i in range(num_args):
        tvm_placeholders.append(
32
            te.placeholder(shape, name="data"+str(i), dtype=dtype))
33
    esum = topi.elemwise_sum(tvm_placeholders)
34
    s = te.create_schedule([esum.op])
35 36 37 38 39 40 41 42 43

    @memoize("topi.tests.test_topi_elemwise_sum")
    def get_ref_data():
        np_nd = [np.random.uniform(0, 10, size=shape).astype(dtype)
                 for i in range(num_args)]
        return np_nd
    np_nd = get_ref_data()

    def check_device(device):
44
        if not tvm.runtime.enabled(device):
45 46 47 48 49 50 51 52 53
            print("Skip because %s is not enabled" % device)
            return

        ctx = tvm.context(device, 0)
        out = tvm.nd.array(np.zeros(shape, dtype=dtype), ctx)
        f = tvm.build(s, tvm_placeholders + [esum], device, name="elemwise_sum")
        tvm_nd = [tvm.nd.array(nd, ctx) for nd in np_nd] + [out]
        f(*tvm_nd)
        np_out = np.sum(np.array(np_nd), axis=0)
54
        tvm.testing.assert_allclose(out.asnumpy(), np_out, rtol=1e-5)
55 56 57 58 59 60

    for device in ["llvm"]:
        check_device(device)


def verify_full(shape, dtype, fill_value):
61
    A = te.placeholder(shape, dtype=dtype, name="A")
62 63
    B = topi.full_like(A, fill_value=fill_value)
    C = topi.full(shape=shape, dtype=dtype, fill_value=fill_value)
64 65
    s1 = te.create_schedule([B.op])
    s2 = te.create_schedule([C.op])
66 67 68 69 70 71 72

    @memoize("topi.tests.test_topi_full")
    def get_ref_data():
        return np.full(shape, fill_value, dtype)
    np_nd = get_ref_data()

    def check_device(device):
73
        if not tvm.runtime.enabled(device):
74 75 76 77 78 79 80
            print("Skip because %s is not enabled" % device)
            return

        ctx = tvm.context(device, 0)
        out = tvm.nd.array(np.zeros(shape, dtype=dtype), ctx)
        f = tvm.build(s1, [A, B], device, name="full_like")
        f(tvm.nd.array(np.zeros(shape, dtype), ctx), out)
81
        tvm.testing.assert_allclose(out.asnumpy(), np_nd, rtol=1e-5)
82 83 84

        f = tvm.build(s2, [C], device, name="full")
        f(out)
85
        tvm.testing.assert_allclose(out.asnumpy(), np_nd, rtol=1e-5)
86 87 88 89

    for device in ["llvm"]:
        check_device(device)

90 91 92 93 94
def verify_vectorization(n, m, dtype):
    def check_device(device):
        if not tvm.runtime.enabled(device):
            print("Skip because %s is not enabled" % device)
            return
95 96
        if dtype == "float16" and device == "cuda" and not have_fp16(tvm.gpu(0).compute_version):
            print("Skip because gpu does not have fp16 support")
97 98 99
            return
        with tvm.target.create(device):
            ctx = tvm.context(device, 0)
100 101 102
            A = te.placeholder((n, m), name='A', dtype=dtype)
            B = te.compute((n, m), lambda i, j:
                             A[i, j] + tvm.tir.const(1, A.dtype), name='B')
103
            S = topi.testing.get_elemwise_schedule(device)(B)
104 105 106 107 108 109 110 111 112 113 114 115 116

            fun = tvm.build(S, [A, B], device)
            np_A = tvm.nd.empty((n, m), A.dtype, ctx).copyfrom(
                                np.random.uniform(size=(n, m)))
            np_B = tvm.nd.empty((n, m), B.dtype, ctx)
            fun(np_A, np_B)
            tvm.testing.assert_allclose(np_B.asnumpy(), np_A.asnumpy() + 1, rtol=1e-5)

    for device in ["cuda"]:
        check_device(device)

def test_vectorization():
    verify_vectorization(128, 64, "float16")
117 118 119 120 121 122 123 124 125 126 127 128 129

def test_elemwise_sum():
    verify_elemwise_sum(1, "float32")
    verify_elemwise_sum(5, "float32")
    verify_elemwise_sum(4, "int32")

def test_full():
    verify_full((3,4,5), "float32", 3.14)
    verify_full((10,), "int32", 7)

if __name__ == "__main__":
    test_elemwise_sum()
    test_full()
130
    test_vectorization()