test_topi_pooling.py 4.79 KB
Newer Older
1 2 3 4
"""Test code for pooling"""
import numpy as np
import tvm
import topi
5
import math
6 7
from topi.util import get_const_tuple

8 9
from common import get_all_backend

10
def verify_pool(n, ic, ih, kh, sh, padding, pool_type, ceil_mode, count_include_pad=True):
Yuwei HU committed
11 12 13
    iw = ih
    kw = kh
    sw = sh
14
    pt, pl, pb, pr = padding
15
    layout = "NCHW"
Yuwei HU committed
16
    A = tvm.placeholder((n, ic, ih, iw), name='A')
17
    B = topi.nn.pool(A, kernel=[kh, kw], stride=[sh, sw], padding=padding,
18 19
                     pool_type=pool_type, ceil_mode=ceil_mode,
                     layout="NCHW", count_include_pad=count_include_pad)
Yuwei HU committed
20 21 22
    B = topi.nn.relu(B)
    dtype = A.dtype

23 24 25
    bshape = get_const_tuple(B.shape)
    ashape = get_const_tuple(A.shape)
    if ceil_mode:
26 27
        assert bshape[2] == int(math.ceil(float(ashape[2] - kh + pt + pb) / sh) + 1)
        assert bshape[3] == int(math.ceil(float(ashape[3] - kw + pl + pr) / sw) + 1)
28
    else:
29 30
        assert bshape[2] == int(math.floor(float(ashape[2] - kh + pt + pb) / sh) + 1)
        assert bshape[3] == int(math.floor(float(ashape[3] - kw + pl + pr) / sw) + 1)
31

32
    a_np = np.random.uniform(low=0.001, size=(n, ic, ih, iw)).astype(dtype)
33 34
    pad_np = np.zeros(shape=(n, ic, ih+pt+pb, iw+pl+pr)).astype(dtype)
    no_zero = (range(n), range(ic), (range(pt, ih+pt)), (range(pl, iw+pl)))
Yuwei HU committed
35 36 37 38 39 40 41
    pad_np[np.ix_(*no_zero)] = a_np
    _, oc, oh, ow = get_const_tuple(B.shape)
    b_np = np.zeros(shape=(n, oc, oh, ow)).astype(dtype)

    if pool_type == 'avg':
        for i in range(oh):
            for j in range(ow):
42 43 44 45 46 47
                if count_include_pad:
                    b_np[:,:,i,j] = np.mean(pad_np[:, :, i*sh:i*sh+kh, j*sw:j*sw+kw], axis=(2,3))
                else:
                    pad_count = np.sum(pad_np[:, :, i*sh:i*sh+kh, j*sw:j*sw+kw] > 0, axis=(2,3))
                    b_np[:,:,i,j] = np.sum(pad_np[:, :, i*sh:i*sh+kh, j*sw:j*sw+kw], axis=(2,3)) / np.maximum(pad_count, 1)

Yuwei HU committed
48 49 50 51 52 53 54
    elif pool_type =='max':
        for i in range(oh):
            for j in range(ow):
                b_np[:,:,i,j] = np.max(pad_np[:, :, i*sh:i*sh+kh, j*sw:j*sw+kw], axis=(2,3))
    b_np = np.maximum(b_np, 0.0)

    def check_device(device):
55 56
        ctx = tvm.context(device, 0)
        if not ctx.exist:
Yuwei HU committed
57 58
            print("Skip because %s is not enabled" % device)
            return
59
        print("Running on target: %s" % device)
60
        with tvm.target.create(device):
61
            s = topi.generic.schedule_pool(B, layout)
62

Yuwei HU committed
63 64 65 66 67 68
        a = tvm.nd.array(a_np, ctx)
        b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=dtype), ctx)
        f = tvm.build(s, [A, B], device)
        f(a, b)
        np.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5)

69
    for device in get_all_backend():
Yuwei HU committed
70 71 72
        check_device(device)

def test_pool():
73 74 75 76 77 78 79 80
    verify_pool(1, 256, 32, 2, 2, [0, 0, 0, 0], 'avg', False, True)
    verify_pool(1, 256, 31, 3, 3, [1, 2, 1, 2], 'avg', False, True)
    verify_pool(1, 256, 32, 2, 2, [1, 2, 1, 2], 'avg', False, False)
    verify_pool(1, 256, 31, 4, 4, [3, 3, 3, 3], 'avg', False, False)
    verify_pool(1, 256, 31, 4, 4, [0, 0, 0, 0], 'avg', False, False)
    verify_pool(1, 256, 32, 2, 2, [0, 0, 0, 0], 'max', False)
    verify_pool(1, 256, 31, 3, 3, [2, 1, 2, 1], 'max', False)
    verify_pool(1, 256, 31, 3, 3, [2, 1, 2, 1], 'max', True)
81

82 83 84 85
    verify_pool(1, 256, 31, 3, 3, [2, 1, 0, 3], 'avg', False, True)
    verify_pool(1, 256, 32, 2, 2, [0, 3, 2, 1], 'avg', False, False)
    verify_pool(1, 256, 31, 3, 3, [1, 0, 3, 2], 'max', False)
    verify_pool(1, 256, 31, 3, 3, [3, 2, 1, 0], 'max', True)
Yuwei HU committed
86 87


88 89 90 91 92 93 94 95 96 97 98 99 100
def verify_global_pool(n, c, h, w, pool_type):
    A = tvm.placeholder((n, c, h, w), name='A')
    B = topi.nn.global_pool(A, pool_type=pool_type)
    B = topi.nn.relu(B)

    a_np = np.random.uniform(size=get_const_tuple(A.shape)).astype(A.dtype)
    if pool_type == 'avg':
        b_np = np.mean(a_np, axis=(2,3), keepdims=True)
    elif pool_type =='max':
        b_np = np.max(a_np, axis=(2,3), keepdims=True)
    b_np = np.maximum(b_np, 0.0)

    def check_device(device):
101 102
        ctx = tvm.context(device, 0)
        if not ctx.exist:
103 104
            print("Skip because %s is not enabled" % device)
            return
105
        print("Running on target: %s" % device)
106 107
        with tvm.target.create(device):
            s = topi.generic.schedule_global_pool(B)
108 109
        a = tvm.nd.array(a_np, ctx)
        b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=B.dtype), ctx)
Yuwei HU committed
110
        f = tvm.build(s, [A, B], device)
111 112 113
        f(a, b)
        np.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5)

114
    for device in get_all_backend():
115 116 117 118 119 120 121 122 123 124
        check_device(device)

def test_global_pool():
    verify_global_pool(1, 1024, 7, 7, 'avg')
    verify_global_pool(4, 1024, 7, 7, 'avg')
    verify_global_pool(1, 1024, 7, 7, 'max')
    verify_global_pool(4, 1024, 7, 7, 'max')


if __name__ == "__main__":
Yuwei HU committed
125
    test_pool()
126
    test_global_pool()