test_topi_math.py 1.59 KB
Newer Older
1 2 3
import numpy as np
import tvm
import topi
4
import topi.testing
5 6 7 8 9 10 11 12 13 14 15 16 17 18
from topi import util


def test_util():
    x = tvm.const(100)
    assert util.get_const_int(x) == 100
    assert util.get_const_tuple((x, x)) == (100, 100)


def test_ewise():
    m = tvm.var('m')
    l = tvm.var('l')
    A = tvm.placeholder((m, l), name='A')

19 20 21
    shape = (20, 3)

    def test_apply(func, name, f_numpy):
22 23 24
        B = func(A)
        assert tuple(B.shape) == tuple(A.shape)
        assert B.op.body[0].name == name
25
        a_np = np.random.uniform(low=1e-5, size=shape).astype(A.dtype)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
        a_np = np.abs(a_np)
        b_np = f_numpy(a_np)

        def check_device(device):
            ctx = tvm.context(device, 0)
            if not ctx.exist:
                print("Skip because %s is not enabled" % device)
                return
            print("Running on target: %s" % device)
            with tvm.target.create(device):
                s = topi.generic.schedule_injective(B)
            a = tvm.nd.array(a_np, ctx)
            b = tvm.nd.array(np.zeros_like(b_np), ctx)
            foo = tvm.build(s, [A, B], device, name=name)
            foo(a, b)
41
            np.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5, atol=1e-5)
42 43 44

        for device in ['cuda', 'opencl', 'metal', 'rocm', 'vulkan', 'llvm']:
            check_device(device)
45

46 47 48 49 50
    test_apply(topi.exp, "exp", np.exp)
    test_apply(topi.tanh, "tanh", np.tanh)
    test_apply(topi.sigmoid, "sigmoid", lambda x:1/(1+np.exp(-x)))
    test_apply(topi.log, "log", np.log)
    test_apply(topi.sqrt, "sqrt", np.sqrt)
51 52 53 54

if __name__ == "__main__":
    test_util()
    test_ewise()