|
| 1 | +from ctypes import POINTER, Structure, c_int32, c_void_p |
| 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 | +from enum import Enum, auto |
| 21 | +import torch |
| 22 | + |
| 23 | +# constant for control whether profile the pytorch and lib functions |
| 24 | +# NOTE: need to manually add synchronization function to the lib function, |
| 25 | +# e.g., cudaDeviceSynchronize() for CUDA |
| 26 | +PROFILE = False |
| 27 | +NUM_PRERUN = 10 |
| 28 | +NUM_ITERATIONS = 1000 |
| 29 | + |
| 30 | + |
| 31 | +class Inplace(Enum): |
| 32 | + OUT_OF_PLACE = auto() |
| 33 | + INPLACE_X = auto() |
| 34 | + |
| 35 | + |
| 36 | +class ReluDescriptor(Structure): |
| 37 | + _fields_ = [("device", c_int32)] |
| 38 | + |
| 39 | + |
| 40 | +infiniopReluDescriptor_t = POINTER(ReluDescriptor) |
| 41 | + |
| 42 | + |
| 43 | +def relu(x): |
| 44 | + if PROFILE: |
| 45 | + ans = torch.nn.functional.relu(x).to(x.dtype) |
| 46 | + torch.cuda.synchronize() |
| 47 | + return ans |
| 48 | + return torch.nn.functional.relu(x).to(x.dtype) |
| 49 | + |
| 50 | + |
| 51 | +def test( |
| 52 | + lib, |
| 53 | + handle, |
| 54 | + torch_device, |
| 55 | + tensor_shape, |
| 56 | + tensor_dtype=torch.float16, |
| 57 | + inplace=Inplace.OUT_OF_PLACE, |
| 58 | +): |
| 59 | + print( |
| 60 | + f"Testing Relu on {torch_device} with tensor_shape:{tensor_shape} dtype:{tensor_dtype} inplace: {inplace.name}" |
| 61 | + ) |
| 62 | + |
| 63 | + x = torch.rand(tensor_shape, dtype=tensor_dtype).to(torch_device) * 2 - 1 |
| 64 | + y = torch.rand(tensor_shape, dtype=tensor_dtype).to(torch_device) if inplace == Inplace.OUT_OF_PLACE else x |
| 65 | + |
| 66 | + for i in range(NUM_PRERUN if PROFILE else 1): |
| 67 | + ans = relu(x) |
| 68 | + if PROFILE: |
| 69 | + start_time = time.time() |
| 70 | + for i in range(NUM_ITERATIONS): |
| 71 | + _ = relu(x) |
| 72 | + elapsed = (time.time() - start_time) / NUM_ITERATIONS |
| 73 | + print(f"pytorch time: {elapsed :6f}") |
| 74 | + |
| 75 | + x_tensor = to_tensor(x, lib) |
| 76 | + y_tensor = to_tensor(y, lib) if inplace == Inplace.OUT_OF_PLACE else x_tensor |
| 77 | + descriptor = infiniopReluDescriptor_t() |
| 78 | + |
| 79 | + check_error( |
| 80 | + lib.infiniopCreateReluDescriptor( |
| 81 | + handle, |
| 82 | + ctypes.byref(descriptor), |
| 83 | + y_tensor.descriptor, |
| 84 | + x_tensor.descriptor, |
| 85 | + ) |
| 86 | + ) |
| 87 | + for i in range(NUM_PRERUN if PROFILE else 1): |
| 88 | + lib.infiniopRelu( |
| 89 | + descriptor, y_tensor.data, x_tensor.data, None |
| 90 | + ) |
| 91 | + if PROFILE: |
| 92 | + start_time = time.time() |
| 93 | + for i in range(NUM_ITERATIONS): |
| 94 | + lib.infiniopRelu( |
| 95 | + descriptor, y_tensor.data, x_tensor.data, None |
| 96 | + ) |
| 97 | + elapsed = (time.time() - start_time) / NUM_ITERATIONS |
| 98 | + print(f" lib time: {elapsed :6f}") |
| 99 | + |
| 100 | + assert torch.allclose(y, ans, atol=0, rtol=1e-3) |
| 101 | + check_error(lib.infiniopDestroyReluDescriptor(descriptor)) |
| 102 | + |
| 103 | + |
| 104 | +def test_cpu(lib, test_cases): |
| 105 | + device = DeviceEnum.DEVICE_CPU |
| 106 | + handle = create_handle(lib, device) |
| 107 | + for tensor_shape, inplace in test_cases: |
| 108 | + test(lib, handle, "cpu", tensor_shape, tensor_dtype=torch.float16, inplace=inplace) |
| 109 | + test(lib, handle, "cpu", tensor_shape, tensor_dtype=torch.float32, inplace=inplace) |
| 110 | + destroy_handle(lib, handle) |
| 111 | + |
| 112 | + |
| 113 | +def test_cuda(lib, test_cases): |
| 114 | + device = DeviceEnum.DEVICE_CUDA |
| 115 | + handle = create_handle(lib, device) |
| 116 | + for tensor_shape, inplace in test_cases: |
| 117 | + test(lib, handle, "cuda", tensor_shape, tensor_dtype=torch.float16, inplace=inplace) |
| 118 | + test(lib, handle, "cuda", tensor_shape, tensor_dtype=torch.float32, inplace=inplace) |
| 119 | + destroy_handle(lib, handle) |
| 120 | + |
| 121 | + |
| 122 | +def test_bang(lib, test_cases): |
| 123 | + import torch_mlu |
| 124 | + |
| 125 | + device = DeviceEnum.DEVICE_BANG |
| 126 | + handle = create_handle(lib, device) |
| 127 | + for tensor_shape, inplace in test_cases: |
| 128 | + test(lib, handle, "mlu", tensor_shape, tensor_dtype=torch.float16, inplace=inplace) |
| 129 | + test(lib, handle, "mlu", tensor_shape, tensor_dtype=torch.float32, inplace=inplace) |
| 130 | + destroy_handle(lib, handle) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + test_cases = [ |
| 135 | + # tensor_shape, inplace |
| 136 | + ((), Inplace.OUT_OF_PLACE), |
| 137 | + ((), Inplace.INPLACE_X), |
| 138 | + ((1, 3), Inplace.OUT_OF_PLACE), |
| 139 | + ((3, 3), Inplace.OUT_OF_PLACE), |
| 140 | + ((3, 3, 13, 9, 17), Inplace.INPLACE_X), |
| 141 | + ((32, 20, 512), Inplace.INPLACE_X), |
| 142 | + ((33, 333, 333), Inplace.OUT_OF_PLACE), |
| 143 | + ((32, 256, 112, 112), Inplace.OUT_OF_PLACE), |
| 144 | + ] |
| 145 | + args = get_args() |
| 146 | + lib = open_lib() |
| 147 | + lib.infiniopCreateReluDescriptor.restype = c_int32 |
| 148 | + lib.infiniopCreateReluDescriptor.argtypes = [ |
| 149 | + infiniopHandle_t, |
| 150 | + POINTER(infiniopReluDescriptor_t), |
| 151 | + infiniopTensorDescriptor_t, |
| 152 | + infiniopTensorDescriptor_t, |
| 153 | + ] |
| 154 | + lib.infiniopRelu.restype = c_int32 |
| 155 | + lib.infiniopRelu.argtypes = [ |
| 156 | + infiniopReluDescriptor_t, |
| 157 | + c_void_p, |
| 158 | + c_void_p, |
| 159 | + c_void_p, |
| 160 | + ] |
| 161 | + lib.infiniopDestroyReluDescriptor.restype = c_int32 |
| 162 | + lib.infiniopDestroyReluDescriptor.argtypes = [ |
| 163 | + infiniopReluDescriptor_t, |
| 164 | + ] |
| 165 | + |
| 166 | + if args.cpu: |
| 167 | + test_cpu(lib, test_cases) |
| 168 | + if args.cuda: |
| 169 | + test_cuda(lib, test_cases) |
| 170 | + if args.bang: |
| 171 | + test_bang(lib, test_cases) |
| 172 | + if not (args.cpu or args.cuda or args.bang): |
| 173 | + test_cpu(lib, test_cases) |
| 174 | + print("\033[92mTest passed!\033[0m") |
| 175 | + |
0 commit comments