test_topi_relu.py 1.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
"""Test code for relu activation"""
import os
import numpy as np
import tvm
import topi
from topi.util import get_const_tuple

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

    a_np = np.random.uniform(size=get_const_tuple(A.shape)).astype(A.dtype)
    b_np = a_np * (a_np > 0)

    def check_device(device):
16 17
        ctx = tvm.context(device, 0)
        if not ctx.exist:
18 19
            print("Skip because %s is not enabled" % device)
            return
20
        print("Running on target: %s" % device)
21 22
        with tvm.target.create(device):
            s = topi.generic.schedule_elemwise(B)
23

24 25 26 27 28 29
        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)
        np.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5)

30
    for device in ['cuda', 'opencl', 'metal', 'rocm', 'vulkan']:
31 32
        check_device(device)

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

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)
    np.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5)


49 50 51
def test_relu():
    verify_relu(10, 128)

52 53 54
def test_leaky_relu():
    verify_leaky_relu(100, 0.1)

55 56 57

if __name__ == "__main__":
    test_relu()
58
    test_leaky_relu()