test_topi_dense.py 2.01 KB
Newer Older
1 2 3 4
"""Test code for dense operator"""
import numpy as np
import tvm
import topi
5
import topi.testing
6 7 8
from topi.util import get_const_tuple
from tvm.contrib.pickle_memoize import memoize

9
from common import get_all_backend
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

def verify_dense(batch, in_dim, out_dim, use_bias=True):
    A = tvm.placeholder((batch, in_dim), name='A')
    B = tvm.placeholder((out_dim, in_dim), name='B')
    C = tvm.placeholder((out_dim,), name='C')
    dtype = A.dtype

    # use memoize to pickle the test data for next time use
    @memoize("topi.tests.test_topi_dense")
    def get_ref_data():
        a_np = np.random.uniform(size=(batch, in_dim)).astype(dtype)
        b_np = np.random.uniform(size=(out_dim, in_dim)).astype(dtype)
        c_np = np.random.uniform(size=(out_dim,)).astype(dtype)
        if use_bias:
            d_np = np.maximum(np.dot(a_np, b_np.T) + c_np, 0.0)
        else:
            d_np = np.maximum(np.dot(a_np, b_np.T), 0.0)
        return (a_np, b_np, c_np, d_np)
    # get the test data
    a_np, b_np, c_np, d_np = get_ref_data()

    def check_device(device):
32 33
        ctx = tvm.context(device, 0)
        if not ctx.exist:
34 35
            print("Skip because %s is not enabled" % device)
            return
36
        print("Running on target: %s" % device)
37
        with tvm.target.create(device):
38 39
            D = topi.nn.dense(A, B, C if use_bias else None)
            D = topi.nn.relu(D)
40
            s = topi.generic.schedule_dense([D])
41 42 43 44 45 46
        a = tvm.nd.array(a_np, ctx)
        b = tvm.nd.array(b_np, ctx)
        c = tvm.nd.array(c_np, ctx)
        d = tvm.nd.array(np.zeros(get_const_tuple(D.shape), dtype=dtype), ctx)
        f = tvm.build(s, [A, B, C, D], device, name="dense")
        f(a, b, c, d)
47
        tvm.testing.assert_allclose(d.asnumpy(), d_np, rtol=1e-5)
48

49
    for device in get_all_backend():
50 51 52 53 54 55
        check_device(device)

def test_dense():
    verify_dense(1, 1024, 1000, use_bias=True)
    verify_dense(1, 1024, 1000, use_bias=False)

56 57
    verify_dense(2, 1024, 1000, use_bias=True)

58 59 60

if __name__ == "__main__":
    test_dense()