avg_pool.py 7.74 KB
Newer Older
PanZezhongQY's avatar
PanZezhongQY committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from ctypes import POINTER, Structure, c_int32, c_void_p, c_uint64
import ctypes
import sys
import os
import time

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
from operatorspy import (
    open_lib,
    to_tensor,
    DeviceEnum,
    infiniopHandle_t,
    infiniopTensorDescriptor_t,
    create_handle,
    destroy_handle,
    check_error,
)

from operatorspy.tests.test_utils import get_args
import torch
from typing import Tuple

# constant for control whether profile the pytorch and lib functions
# NOTE: need to manually add synchronization function to the lib function,
#       e.g., cudaDeviceSynchronize() for CUDA
PROFILE = False
NUM_PRERUN = 10
NUM_ITERATIONS = 1000


class AvgPoolDescriptor(Structure):
    _fields_ = [("device", c_int32)]


infiniopAvgPoolDescriptor_t = POINTER(AvgPoolDescriptor)


38
def pool(x, k, padding, stride, dilation=1):
PanZezhongQY's avatar
PanZezhongQY committed
39
40
41
42
43
44
45
46
47
48
49
50
    pooling_layers = {
        1: torch.nn.AvgPool1d,
        2: torch.nn.AvgPool2d,
        3: torch.nn.AvgPool3d,
    }

    ndim = len(x.shape) - 2
    if ndim not in pooling_layers:
        print("Error: Pytorch -> Unsupported tensor dimension")
        return None

    if ndim == 3 and x.dtype == torch.float16:
51
52
53
        ans = pooling_layers[ndim](k, stride=stride, padding=padding)(
            x.to(torch.float32)
        ).to(torch.float16)
PanZezhongQY's avatar
PanZezhongQY committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    else:
        ans = pooling_layers[ndim](k, stride=stride, padding=padding)(x)
    if PROFILE:
        torch.cuda.synchronize()
    return ans


def inferShape(x_shape, kernel_shape, padding, strides):
    assert (
        len(x_shape) - 2 == len(kernel_shape) == len(padding) == len(strides)
    ), "kernel, pads, and strides should have the same length; the length of input x should be 2 more than that of kernel"
    input_shape = x_shape[2:]
    output_shape = []

    for dim, k, p, s in zip(input_shape, kernel_shape, padding, strides):
        output_dim = (dim + 2 * p - k) // s + 1
        output_shape.append(output_dim)

    return x_shape[:2] + tuple(output_shape)

74

PanZezhongQY's avatar
PanZezhongQY committed
75
76
77
78
79
80
# convert a python tuple to a ctype void pointer
def tuple_to_void_p(py_tuple: Tuple):
    array = ctypes.c_int64 * len(py_tuple)
    data_array = array(*py_tuple)
    return ctypes.cast(data_array, ctypes.c_void_p)

81

PanZezhongQY's avatar
PanZezhongQY committed
82
83
84
85
def test(
    lib,
    handle,
    torch_device,
86
87
    x_shape,
    k_shape,
PanZezhongQY's avatar
PanZezhongQY committed
88
89
90
    padding,
    strides,
    tensor_dtype=torch.float16,
91
    sync=None
PanZezhongQY's avatar
PanZezhongQY committed
92
93
94
95
96
97
):
    print(
        f"Testing AvgPool on {torch_device} with x_shape:{x_shape} kernel_shape:{k_shape} padding:{padding} strides:{strides} dtype:{tensor_dtype}"
    )

    x = torch.rand(x_shape, dtype=tensor_dtype).to(torch_device)
98
99
100
    y = torch.rand(
        inferShape(x_shape, k_shape, padding, strides), dtype=tensor_dtype
    ).to(torch_device)
PanZezhongQY's avatar
PanZezhongQY committed
101
102
103
104
105
106
107
108
109
110
111
112

    for i in range(NUM_PRERUN if PROFILE else 1):
        ans = pool(x, k_shape, padding, strides)
    if PROFILE:
        start_time = time.time()
        for i in range(NUM_ITERATIONS):
            _ = pool(x, k_shape, padding, strides)
        elapsed = (time.time() - start_time) / NUM_ITERATIONS
        print(f"pytorch time: {elapsed :6f}")

    x_tensor = to_tensor(x, lib)
    y_tensor = to_tensor(y, lib)
113
114
115
116
    
    if sync is not None:
        sync()

PanZezhongQY's avatar
PanZezhongQY committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    descriptor = infiniopAvgPoolDescriptor_t()

    check_error(
        lib.infiniopCreateAvgPoolDescriptor(
            handle,
            ctypes.byref(descriptor),
            y_tensor.descriptor,
            x_tensor.descriptor,
            tuple_to_void_p(k_shape),
            tuple_to_void_p(padding),
            tuple_to_void_p(strides),
            len(k_shape),
        )
    )

    # Invalidate the shape and strides in the descriptor to prevent them from being directly used by the kernel
    x_tensor.descriptor.contents.invalidate()
    y_tensor.descriptor.contents.invalidate()

    workspaceSize = ctypes.c_uint64(0)
    check_error(
        lib.infiniopGetAvgPoolWorkspaceSize(descriptor, ctypes.byref(workspaceSize))
    )
140
141
142
    workspace = torch.zeros(int(workspaceSize.value), dtype=torch.uint8).to(
        torch_device
    )
PanZezhongQY's avatar
PanZezhongQY committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    workspace_ptr = ctypes.cast(workspace.data_ptr(), ctypes.POINTER(ctypes.c_uint8))

    for i in range(NUM_PRERUN if PROFILE else 1):
        check_error(
            lib.infiniopAvgPool(
                descriptor,
                workspace_ptr,
                workspaceSize,
                y_tensor.data,
                x_tensor.data,
                None,
            )
        )
    if PROFILE:
        start_time = time.time()
        for i in range(NUM_ITERATIONS):
            check_error(
                lib.infiniopAvgPool(
                    descriptor,
                    workspace_ptr,
                    workspaceSize,
                    y_tensor.data,
                    x_tensor.data,
                    None,
                )
            )
        elapsed = (time.time() - start_time) / NUM_ITERATIONS
        print(f"    lib time: {elapsed :6f}")

    assert torch.allclose(y, ans, atol=0, rtol=1e-3)
    check_error(lib.infiniopDestroyAvgPoolDescriptor(descriptor))


def test_cpu(lib, test_cases):
    device = DeviceEnum.DEVICE_CPU
    handle = create_handle(lib, device)
    for x_shape, kernel_shape, padding, strides in test_cases:
180
        # fmt: off
PanZezhongQY's avatar
PanZezhongQY committed
181
182
        test(lib, handle, "cpu", x_shape, kernel_shape, padding, strides, tensor_dtype=torch.float16)
        test(lib, handle, "cpu", x_shape, kernel_shape, padding, strides, tensor_dtype=torch.float32)
183
        # fmt: on
PanZezhongQY's avatar
PanZezhongQY committed
184
185
186
187
188
189
190
    destroy_handle(lib, handle)


def test_cuda(lib, test_cases):
    device = DeviceEnum.DEVICE_CUDA
    handle = create_handle(lib, device)
    for x_shape, kernel_shape, padding, strides in test_cases:
191
        # fmt: off
PanZezhongQY's avatar
PanZezhongQY committed
192
193
        test(lib, handle, "cuda", x_shape, kernel_shape, padding, strides, tensor_dtype=torch.float16)
        test(lib, handle, "cuda", x_shape, kernel_shape, padding, strides, tensor_dtype=torch.float32)
194
        # fmt: on
PanZezhongQY's avatar
PanZezhongQY committed
195
196
197
198
199
200
201
202
203
    destroy_handle(lib, handle)


def test_bang(lib, test_cases):
    import torch_mlu

    device = DeviceEnum.DEVICE_BANG
    handle = create_handle(lib, device)
    for x_shape, kernel_shape, padding, strides in test_cases:
204
        # fmt: off
PanZezhongQY's avatar
PanZezhongQY committed
205
206
        test(lib, handle, "mlu", x_shape, kernel_shape, padding, strides, tensor_dtype=torch.float16)
        test(lib, handle, "mlu", x_shape, kernel_shape, padding, strides, tensor_dtype=torch.float32)
207
        # fmt: on
PanZezhongQY's avatar
PanZezhongQY committed
208
209
210
211
212
    destroy_handle(lib, handle)


if __name__ == "__main__":
    test_cases = [
213
        # fmt: off
PanZezhongQY's avatar
PanZezhongQY committed
214
215
216
217
        # x_shape, kernel_shape, padding, strides
        ((1, 1, 10), (3,), (1,), (1,)),
        ((32, 3, 224, 224), (3, 3), (1, 1), (2, 2)),
        ((1, 1, 16, 16, 16), (5, 5, 5), (2, 2, 2), (2, 2, 2)),
218
        # fmt: on
PanZezhongQY's avatar
PanZezhongQY committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    ]
    args = get_args()
    lib = open_lib()
    lib.infiniopCreateAvgPoolDescriptor.restype = c_int32
    lib.infiniopCreateAvgPoolDescriptor.argtypes = [
        infiniopHandle_t,
        POINTER(infiniopAvgPoolDescriptor_t),
        infiniopTensorDescriptor_t,
        infiniopTensorDescriptor_t,
        c_void_p,
        c_void_p,
        c_void_p,
        c_uint64,
    ]
    lib.infiniopGetAvgPoolWorkspaceSize.restype = c_int32
    lib.infiniopGetAvgPoolWorkspaceSize.argtypes = [
        infiniopAvgPoolDescriptor_t,
        POINTER(c_uint64),
    ]
    lib.infiniopAvgPool.restype = c_int32
    lib.infiniopAvgPool.argtypes = [
        infiniopAvgPoolDescriptor_t,
        c_void_p,
        c_uint64,
        c_void_p,
        c_void_p,
        c_void_p,
    ]
    lib.infiniopDestroyAvgPoolDescriptor.restype = c_int32
    lib.infiniopDestroyAvgPoolDescriptor.argtypes = [
        infiniopAvgPoolDescriptor_t,
    ]

    if args.cpu:
        test_cpu(lib, test_cases)
    if args.cuda:
        test_cuda(lib, test_cases)
    if args.bang:
        test_bang(lib, test_cases)
    if not (args.cpu or args.cuda or args.bang):
        test_cpu(lib, test_cases)
    print("\033[92mTest passed!\033[0m")