test_topi_relu.py 3.54 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 20 21 22 23
"""Test code for relu activation"""
import os
import numpy as np
import tvm
import topi
from topi.util import get_const_tuple

24 25
from common import get_all_backend

26 27 28 29
def verify_relu(m, n):
    A = tvm.placeholder((m, n), name='A')
    B = topi.nn.relu(A)

30
    a_np = np.random.uniform(low=-1.0, high=1.0, size=get_const_tuple(A.shape)).astype(A.dtype)
31 32 33
    b_np = a_np * (a_np > 0)

    def check_device(device):
34 35
        ctx = tvm.context(device, 0)
        if not ctx.exist:
36 37
            print("Skip because %s is not enabled" % device)
            return
38
        print("Running on target: %s" % device)
39 40
        with tvm.target.create(device):
            s = topi.generic.schedule_elemwise(B)
41

42 43 44 45
        a = tvm.nd.array(a_np, ctx)
        b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=B.dtype), ctx)
        foo = tvm.build(s, [A, B], device, name="relu")
        foo(a, b)
46
        tvm.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5)
47

48
    for device in get_all_backend():
49 50
        check_device(device)

51 52 53 54 55 56 57 58 59 60 61 62 63

def verify_leaky_relu(m, alpha):
    A = tvm.placeholder((m,), name='A')
    B = topi.nn.leaky_relu(A, alpha)
    s = tvm.create_schedule([B.op])

    a_np = np.random.uniform(size=get_const_tuple(A.shape)).astype(A.dtype)
    b_np = a_np * (a_np > 0) + a_np * (a_np < 0) * alpha
    ctx = tvm.cpu(0)
    a = tvm.nd.array(a_np, ctx)
    b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=B.dtype), ctx)
    foo = tvm.build(s, [A, B], "llvm", name="leaky_relu")
    foo(a, b)
64
    tvm.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5)
65 66


67
def verify_prelu(x, w, axis, weight_reshape):
68 69 70 71 72 73
    X = tvm.placeholder((x), name='X')
    W = tvm.placeholder((w), name='W')
    x_np = np.random.uniform(low=-1.0, high=1.0, size=get_const_tuple(X.shape)).astype(X.dtype)
    w_np = np.random.uniform(low=-1.0, high=1.0, size=get_const_tuple(W.shape)).astype(W.dtype)

    def _prelu_numpy(x, W):
74
        return (x < 0) * (x *W.reshape(weight_reshape)) + (x>=0) * x
75

76
    B = topi.nn.prelu(X, W, axis)
77 78 79 80 81 82 83 84 85 86
    s = tvm.create_schedule([B.op])

    ctx = tvm.cpu(0)
    x_tvm = tvm.nd.array(x_np, ctx)
    w_tvm = tvm.nd.array(w_np, ctx)

    b = tvm.nd.array(np.zeros(get_const_tuple(X.shape), dtype=B.dtype), ctx)
    foo = tvm.build(s, [X, W, B], "llvm", name="prelu")
    foo(x_tvm, w_tvm, b)
    out_np = _prelu_numpy(x_np, w_np)
87
    tvm.testing.assert_allclose(b.asnumpy(), out_np, rtol=1e-5)
88

89 90 91
def test_relu():
    verify_relu(10, 128)

92 93 94 95
def test_schedule_big_array():
    verify_relu(1024 * 100 , 512)


96 97 98
def test_leaky_relu():
    verify_leaky_relu(100, 0.1)

99
def test_prelu():
100 101
    verify_prelu((1, 3, 2, 2), (3,), 1, (3, 1, 1))
    verify_prelu((1, 3, 2, 2), (2,), 2, (2, 1))
102
    verify_prelu((1, 3), (3,), 1, (3, ))
103 104

if __name__ == "__main__":
105
    test_schedule_big_array()
106
    test_relu()
107
    test_leaky_relu()
108
    test_prelu()