test_topi_reduce.py 5.32 KB
Newer Older
1 2 3 4 5 6
"""Test code for reduce."""
import os
import numpy as np
import tvm
import topi

7 8
from common import get_all_backend

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
def _my_npy_argmax(arr, axis, keepdims):
    if not keepdims:
        return arr.argmax(axis=axis)
    else:
        if axis is not None:
            out_shape = list(arr.shape)
            out_shape[axis] = 1
        else:
            out_shape = [1 for _ in range(len(arr.shape))]
        return arr.argmax(axis=axis).reshape(out_shape)


def _my_npy_argmin(arr, axis, keepdims):
    if not keepdims:
        return arr.argmin(axis=axis)
    else:
        out_shape = list(arr.shape)
        out_shape[axis] = 1
        return arr.argmin(axis=axis).reshape(out_shape)


30
def verify_reduce_map_ele(in_shape, axis, keepdims, type="sum", dtype="float32"):
31
    # Build the logic and compile the function
32
    A = tvm.placeholder(shape=in_shape, name="A", dtype=dtype)
33
    A1 = topi.sqrt(topi.exp(A))
34
    out_dtype = dtype
35
    if type == "sum":
36
        B = topi.sum(A1, axis=axis, keepdims=keepdims)
37
    elif type == "max":
38
        B = topi.max(A1, axis=axis, keepdims=keepdims)
39
    elif type == "min":
40
        B = topi.min(A1, axis=axis, keepdims=keepdims)
41 42 43 44 45 46
    elif type == "argmax":
        B = topi.argmax(A1, axis=axis, keepdims=keepdims)
        out_dtype = "int32"
    elif type == "argmin":
        B = topi.argmin(A1, axis=axis, keepdims=keepdims)
        out_dtype = "int32"
47 48
    else:
        raise NotImplementedError
49

50
    def check_device(device):
51 52
        ctx = tvm.context(device, 0)
        if not ctx.exist:
53 54
            print("Skip because %s is not enabled" % device)
            return
55
        print("Running on target: %s" % device)
56 57
        with tvm.target.create(device):
            s = topi.generic.schedule_reduce(B)
58

Xingjian Shi committed
59
        foo = tvm.build(s, [A, B], device, name=type)
60
        # Test
61 62
        in_npy = np.random.uniform(size=in_shape).astype(dtype)
        in_npy_map = np.sqrt(np.exp(in_npy)).astype(dtype)
63
        if type == "sum":
64
            out_npy = in_npy_map.sum(axis=axis, keepdims=keepdims)
65
        elif type == "max":
66
            out_npy = in_npy_map.max(axis=axis, keepdims=keepdims)
67
        elif type == "min":
68
            out_npy = in_npy_map.min(axis=axis, keepdims=keepdims)
69 70 71 72
        elif type == "argmax":
            out_npy = _my_npy_argmax(in_npy_map, axis=axis, keepdims=keepdims)
        elif type == "argmin":
            out_npy = _my_npy_argmin(in_npy_map, axis=axis, keepdims=keepdims)
73 74 75
        else:
            raise NotImplementedError
        data_tvm = tvm.nd.array(in_npy, ctx=ctx)
76
        out_tvm = tvm.nd.empty(shape=out_npy.shape, ctx=ctx, dtype=out_dtype)
77 78
        for _ in range(1):
            foo(data_tvm, out_tvm)
Xingjian Shi committed
79 80 81 82 83 84 85 86 87 88 89
        if type == "argmax" or type == "argmin":
            out_tvm_indices = out_tvm.asnumpy()
            if keepdims:
                out_tvm_indices = np.take(out_tvm_indices, indices=0, axis=axis)
            if axis is None:
                out_tvm_val = in_npy_map.ravel()[out_tvm_indices]
            else:
                other_indices = tuple(np.indices(in_shape[0:axis] + in_shape[(axis+1):]))
                sel_indices = other_indices[0:axis] + (out_tvm_indices,) + other_indices[axis:]
                out_tvm_val = in_npy_map[sel_indices]
            if type == "argmax":
90
                tvm.testing.assert_allclose(out_tvm_val, in_npy_map.max(axis=axis), 1E-3, 1E-3)
Xingjian Shi committed
91
            elif type == "argmin":
92
                tvm.testing.assert_allclose(out_tvm_val, in_npy_map.min(axis=axis), 1E-3, 1E-3)
Xingjian Shi committed
93
        else:
94
            tvm.testing.assert_allclose(out_tvm.asnumpy(), out_npy, 1E-3, 1E-3)
95
    for device in get_all_backend():
96
        check_device(device)
97 98 99


def test_reduce_map():
100 101 102 103
    verify_reduce_map_ele(in_shape=(32,),
                          axis=0,
                          keepdims=False,
                          type="argmax")
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    verify_reduce_map_ele(in_shape=(128, 24, 128, 24),
                        axis=(1, 2, 3),
                        keepdims=True,
                        type="sum")
    verify_reduce_map_ele(in_shape=(128, 24 * 128 * 24),
                        axis=(1,),
                        keepdims=False,
                        type="max")
    verify_reduce_map_ele(in_shape=(32, 128, 24),
                        axis=None,
                        keepdims=True,
                        type="sum")
    verify_reduce_map_ele(in_shape=(128, 24, 128, 24),
                        axis=(0, 2),
                        keepdims=False,
                        type="min")
120 121 122 123 124 125 126 127 128 129 130 131
    verify_reduce_map_ele(in_shape=(32, 128),
                          axis=1,
                          keepdims=True,
                          type="argmax")
    verify_reduce_map_ele(in_shape=(32, 24, 32, 24),
                          axis=2,
                          keepdims=False,
                          type="argmin")
    verify_reduce_map_ele(in_shape=(31, 21, 15),
                          axis=None,
                          keepdims=True,
                          type="argmax")
132 133 134 135
    verify_reduce_map_ele(in_shape=(31, 21, 15),
                          axis=None,
                          keepdims=False,
                          type="sum")
136 137 138 139 140
    verify_reduce_map_ele(in_shape=(128, 24, 128, 24),
                          axis=(1, 2, 3),
                          keepdims=True,
                          type="sum",
                          dtype="float64")
141 142 143

if __name__ == "__main__":
    test_reduce_map()