|
| 1 | +from ctypes import POINTER, Structure, c_int32, c_void_p, c_uint64 |
| 2 | +import ctypes |
| 3 | +import sys |
| 4 | +import os |
| 5 | +import time |
| 6 | + |
| 7 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))) |
| 8 | +from operatorspy import ( |
| 9 | + open_lib, |
| 10 | + to_tensor, |
| 11 | + DeviceEnum, |
| 12 | + infiniopHandle_t, |
| 13 | + infiniopTensorDescriptor_t, |
| 14 | + create_handle, |
| 15 | + destroy_handle, |
| 16 | + check_error, |
| 17 | +) |
| 18 | + |
| 19 | +from operatorspy.tests.test_utils import get_args |
| 20 | +import torch, time |
| 21 | + |
| 22 | +# constant for control whether profile the pytorch and lib functions |
| 23 | +# NOTE: need to manually add synchronization function to the lib function, |
| 24 | +# e.g., cudaDeviceSynchronize() for CUDA |
| 25 | +PROFILE = False |
| 26 | +NUM_PRERUN = 10 |
| 27 | +NUM_ITERATIONS = 1000 |
| 28 | + |
| 29 | + |
| 30 | +class GlobalAvgPoolDescriptor(Structure): |
| 31 | + _fields_ = [("device", c_int32)] |
| 32 | + |
| 33 | + |
| 34 | +infiniopGlobalAvgPoolDescriptor_t = POINTER(GlobalAvgPoolDescriptor) |
| 35 | + |
| 36 | + |
| 37 | +def inferShape(x): |
| 38 | + return x.shape[:2] + (1,) * (x.dim() - 2) |
| 39 | + |
| 40 | + |
| 41 | +def globalAvgPool(x): |
| 42 | + y = torch.mean(x, dim=tuple(range(2, x.dim())), keepdim=True) |
| 43 | + if PROFILE: |
| 44 | + torch.cuda.synchronize() |
| 45 | + return y.view(*inferShape(x)) |
| 46 | + |
| 47 | + |
| 48 | +def test( |
| 49 | + lib, |
| 50 | + handle, |
| 51 | + torch_device, |
| 52 | + x_shape, |
| 53 | + tensor_dtype=torch.float16, |
| 54 | +): |
| 55 | + print( |
| 56 | + f"Testing GlobalAvgPool on {torch_device} with input tensor_shape: {x_shape} dtype: {tensor_dtype}" |
| 57 | + ) |
| 58 | + |
| 59 | + x = torch.rand(x_shape, dtype=tensor_dtype).to(torch_device) |
| 60 | + y = torch.zeros(inferShape(x), dtype=tensor_dtype).to(torch_device) |
| 61 | + |
| 62 | + for i in range(NUM_PRERUN if PROFILE else 1): |
| 63 | + ans = globalAvgPool(x) |
| 64 | + if PROFILE: |
| 65 | + start_time = time.time() |
| 66 | + for i in range(NUM_ITERATIONS): |
| 67 | + _ = globalAvgPool(x) |
| 68 | + elapsed = (time.time() - start_time) / NUM_ITERATIONS |
| 69 | + print(f"pytorch time: {elapsed :6f}") |
| 70 | + |
| 71 | + x_tensor = to_tensor(x, lib) |
| 72 | + y_tensor = to_tensor(y, lib) |
| 73 | + descriptor = infiniopGlobalAvgPoolDescriptor_t() |
| 74 | + |
| 75 | + check_error( |
| 76 | + lib.infiniopCreateGlobalAvgPoolDescriptor( |
| 77 | + handle, |
| 78 | + ctypes.byref(descriptor), |
| 79 | + y_tensor.descriptor, |
| 80 | + x_tensor.descriptor, |
| 81 | + ) |
| 82 | + ) |
| 83 | + workspaceSize = ctypes.c_uint64(0) |
| 84 | + check_error( |
| 85 | + lib.infiniopGetGlobalAvgPoolWorkspaceSize( |
| 86 | + descriptor, ctypes.byref(workspaceSize) |
| 87 | + ) |
| 88 | + ) |
| 89 | + workspace = torch.zeros(int(workspaceSize.value), dtype=torch.uint8).to( |
| 90 | + torch_device |
| 91 | + ) |
| 92 | + workspace_ptr = ctypes.cast(workspace.data_ptr(), ctypes.POINTER(ctypes.c_uint8)) |
| 93 | + |
| 94 | + |
| 95 | + for i in range(NUM_PRERUN if PROFILE else 1): |
| 96 | + check_error( |
| 97 | + lib.infiniopGlobalAvgPool( |
| 98 | + descriptor, workspace_ptr, workspaceSize, y_tensor.data, x_tensor.data, None |
| 99 | + ) |
| 100 | + ) |
| 101 | + if PROFILE: |
| 102 | + start_time = time.time() |
| 103 | + for i in range(NUM_ITERATIONS): |
| 104 | + lib.infiniopGlobalAvgPool( |
| 105 | + descriptor, workspace_ptr, workspaceSize, y_tensor.data, x_tensor.data, None |
| 106 | + ) |
| 107 | + elapsed = (time.time() - start_time) / NUM_ITERATIONS |
| 108 | + print(f" lib time: {elapsed :6f}") |
| 109 | + |
| 110 | + assert torch.allclose(y, ans, atol=0, rtol=1e-3) |
| 111 | + check_error(lib.infiniopDestroyGlobalAvgPoolDescriptor(descriptor)) |
| 112 | + |
| 113 | + |
| 114 | +def test_cpu(lib, test_cases): |
| 115 | + device = DeviceEnum.DEVICE_CPU |
| 116 | + handle = create_handle(lib, device) |
| 117 | + for x_shape in test_cases: |
| 118 | + test(lib, handle, "cpu", x_shape, tensor_dtype=torch.float16) |
| 119 | + test(lib, handle, "cpu", x_shape, tensor_dtype=torch.float32) |
| 120 | + destroy_handle(lib, handle) |
| 121 | + |
| 122 | + |
| 123 | +def test_cuda(lib, test_cases): |
| 124 | + device = DeviceEnum.DEVICE_CUDA |
| 125 | + handle = create_handle(lib, device) |
| 126 | + for x_shape in test_cases: |
| 127 | + test(lib, handle, "cuda", x_shape, tensor_dtype=torch.float16) |
| 128 | + test(lib, handle, "cuda", x_shape, tensor_dtype=torch.float32) |
| 129 | + destroy_handle(lib, handle) |
| 130 | + |
| 131 | + |
| 132 | +def test_bang(lib, test_cases): |
| 133 | + import torch_mlu |
| 134 | + |
| 135 | + device = DeviceEnum.DEVICE_BANG |
| 136 | + handle = create_handle(lib, device) |
| 137 | + for x_shape in test_cases: |
| 138 | + test(lib, handle, "mlu", x_shape, tensor_dtype=torch.float16) |
| 139 | + test(lib, handle, "mlu", x_shape, tensor_dtype=torch.float32) |
| 140 | + destroy_handle(lib, handle) |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + test_cases = [ |
| 145 | + # x_shape |
| 146 | + ((1, 3, 3)), |
| 147 | + ((1, 3, 1, 1, 3)), |
| 148 | + ((1, 3, 1, 1, 257)), |
| 149 | + ((1, 2, 1, 1, 514)), |
| 150 | + ((1, 3, 1, 1, 1025)), |
| 151 | + ((32, 256, 1, 112, 112)), |
| 152 | + ((2, 3, 2048000)), |
| 153 | + ((2, 1, 10243)), |
| 154 | + ((2, 20, 100)), |
| 155 | + ((3, 33, 333)), |
| 156 | + ((32, 20, 512)), |
| 157 | + ((3, 3, 11, 11, 11, 3, 2)), |
| 158 | + ((32, 256, 1, 112, 112)), |
| 159 | + ((32, 256, 112, 112)), |
| 160 | + ] |
| 161 | + args = get_args() |
| 162 | + lib = open_lib() |
| 163 | + lib.infiniopCreateGlobalAvgPoolDescriptor.restype = c_int32 |
| 164 | + lib.infiniopCreateGlobalAvgPoolDescriptor.argtypes = [ |
| 165 | + infiniopHandle_t, |
| 166 | + POINTER(infiniopGlobalAvgPoolDescriptor_t), |
| 167 | + infiniopTensorDescriptor_t, |
| 168 | + infiniopTensorDescriptor_t, |
| 169 | + ] |
| 170 | + lib.infiniopGetGlobalAvgPoolWorkspaceSize.restype = c_int32 |
| 171 | + lib.infiniopGetGlobalAvgPoolWorkspaceSize.argtypes = [ |
| 172 | + infiniopGlobalAvgPoolDescriptor_t, |
| 173 | + POINTER(c_uint64), |
| 174 | + ] |
| 175 | + lib.infiniopGlobalAvgPool.restype = c_int32 |
| 176 | + lib.infiniopGlobalAvgPool.argtypes = [ |
| 177 | + infiniopGlobalAvgPoolDescriptor_t, |
| 178 | + c_void_p, |
| 179 | + c_uint64, |
| 180 | + c_void_p, |
| 181 | + c_void_p, |
| 182 | + c_void_p, |
| 183 | + ] |
| 184 | + lib.infiniopDestroyGlobalAvgPoolDescriptor.restype = c_int32 |
| 185 | + lib.infiniopDestroyGlobalAvgPoolDescriptor.argtypes = [ |
| 186 | + infiniopGlobalAvgPoolDescriptor_t, |
| 187 | + ] |
| 188 | + |
| 189 | + if args.cpu: |
| 190 | + test_cpu(lib, test_cases) |
| 191 | + if args.cuda: |
| 192 | + test_cuda(lib, test_cases) |
| 193 | + if args.bang: |
| 194 | + test_bang(lib, test_cases) |
| 195 | + if not (args.cpu or args.cuda or args.bang): |
| 196 | + test_cpu(lib, test_cases) |
| 197 | + print("\033[92mTest passed!\033[0m") |
0 commit comments