|
| 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 | + rearrange_tensor, |
| 18 | +) |
| 19 | + |
| 20 | +from operatorspy.tests.test_utils import get_args |
| 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 ExpandDescriptor(Structure): |
| 32 | + _fields_ = [("device", c_int32)] |
| 33 | + |
| 34 | + |
| 35 | +infiniopExpandDescriptor_t = POINTER(ExpandDescriptor) |
| 36 | + |
| 37 | + |
| 38 | +def expand(x, y): |
| 39 | + if PROFILE: |
| 40 | + ans = x.expand_as(y).clone() |
| 41 | + torch.cuda.synchronize() |
| 42 | + return ans |
| 43 | + return x.expand_as(y) |
| 44 | + |
| 45 | + |
| 46 | +def test( |
| 47 | + lib, |
| 48 | + handle, |
| 49 | + torch_device, |
| 50 | + y_shape, |
| 51 | + x_shape, |
| 52 | + y_stride=None, |
| 53 | + x_stride=None, |
| 54 | + tensor_dtype=torch.float16, |
| 55 | +): |
| 56 | + print( |
| 57 | + f"Testing Expand on {torch_device} with x_shape:{x_shape} y_shape:{y_shape} x_stride:{x_stride} y_stride:{y_stride} dtype:{tensor_dtype}" |
| 58 | + ) |
| 59 | + |
| 60 | + x = torch.rand(x_shape, dtype=tensor_dtype).to(torch_device) |
| 61 | + y = torch.rand(y_shape, dtype=tensor_dtype).to(torch_device) |
| 62 | + |
| 63 | + if x_stride is not None: |
| 64 | + x = rearrange_tensor(x, x_stride) |
| 65 | + if y_stride is not None: |
| 66 | + y = rearrange_tensor(y, y_stride) |
| 67 | + |
| 68 | + for i in range(NUM_PRERUN if PROFILE else 1): |
| 69 | + ans = expand(x, y) |
| 70 | + if PROFILE: |
| 71 | + start_time = time.time() |
| 72 | + for i in range(NUM_ITERATIONS): |
| 73 | + _ = expand(x, y) |
| 74 | + elapsed = (time.time() - start_time) / NUM_ITERATIONS |
| 75 | + print(f"pytorch time: {elapsed :6f}") |
| 76 | + |
| 77 | + x_tensor = to_tensor(x, lib) |
| 78 | + y_tensor = to_tensor(y, lib) |
| 79 | + descriptor = infiniopExpandDescriptor_t() |
| 80 | + |
| 81 | + check_error( |
| 82 | + lib.infiniopCreateExpandDescriptor( |
| 83 | + handle, |
| 84 | + ctypes.byref(descriptor), |
| 85 | + y_tensor.descriptor, |
| 86 | + x_tensor.descriptor, |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + for i in range(NUM_PRERUN if PROFILE else 1): |
| 91 | + lib.infiniopExpand( |
| 92 | + descriptor, y_tensor.data, x_tensor.data, None |
| 93 | + ) |
| 94 | + if PROFILE: |
| 95 | + start_time = time.time() |
| 96 | + for i in range(NUM_ITERATIONS): |
| 97 | + lib.infiniopExpand( |
| 98 | + descriptor, y_tensor.data, x_tensor.data, None |
| 99 | + ) |
| 100 | + elapsed = (time.time() - start_time) / NUM_ITERATIONS |
| 101 | + print(f" lib time: {elapsed :6f}") |
| 102 | + assert torch.allclose(y, ans, atol=0, rtol=1e-3) |
| 103 | + check_error(lib.infiniopDestroyExpandDescriptor(descriptor)) |
| 104 | + |
| 105 | + |
| 106 | +def test_cpu(lib, test_cases): |
| 107 | + device = DeviceEnum.DEVICE_CPU |
| 108 | + handle = create_handle(lib, device) |
| 109 | + for y_shape, x_shape, y_stride, x_stride in test_cases: |
| 110 | + test(lib, handle, "cpu", y_shape, x_shape, y_stride, x_stride, tensor_dtype=torch.float16) |
| 111 | + test(lib, handle, "cpu", y_shape, x_shape, y_stride, x_stride, tensor_dtype=torch.float32) |
| 112 | + destroy_handle(lib, handle) |
| 113 | + |
| 114 | + |
| 115 | +def test_cuda(lib, test_cases): |
| 116 | + device = DeviceEnum.DEVICE_CUDA |
| 117 | + handle = create_handle(lib, device) |
| 118 | + for y_shape, x_shape, y_stride, x_stride in test_cases: |
| 119 | + test(lib, handle, "cuda", y_shape, x_shape, y_stride, x_stride, tensor_dtype=torch.float16) |
| 120 | + test(lib, handle, "cuda", y_shape, x_shape, y_stride, x_stride, tensor_dtype=torch.float32) |
| 121 | + destroy_handle(lib, handle) |
| 122 | + |
| 123 | + |
| 124 | +def test_bang(lib, test_cases): |
| 125 | + import torch_mlu |
| 126 | + |
| 127 | + device = DeviceEnum.DEVICE_BANG |
| 128 | + handle = create_handle(lib, device) |
| 129 | + for y_shape, x_shape, y_stride, x_stride in test_cases: |
| 130 | + test(lib, handle, "mlu", y_shape, x_shape, y_stride, x_stride, tensor_dtype=torch.float16) |
| 131 | + test(lib, handle, "mlu", y_shape, x_shape, y_stride, x_stride, tensor_dtype=torch.float32) |
| 132 | + destroy_handle(lib, handle) |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + test_cases = [ |
| 137 | + # y_shape, x_shape, y_stride, x_stride |
| 138 | + ((), (), None, None), |
| 139 | + ((3, 3), (1,), None, None), |
| 140 | + ((5, 4, 3), (4, 3,), None, (6, 1)), |
| 141 | + ((99, 111), (111,), None, None), |
| 142 | + ((2, 4, 3), (1, 3), None, None), |
| 143 | + ((2, 20, 3), (2, 1, 3), None, None), |
| 144 | + ((2, 3, 4, 5), (5,), None, None), |
| 145 | + ((3, 2, 4, 5), (3, 2, 1, 1), None, None), |
| 146 | + ((32, 256, 112, 112), (32, 256, 112, 1), None, None), |
| 147 | + ] |
| 148 | + args = get_args() |
| 149 | + lib = open_lib() |
| 150 | + lib.infiniopCreateExpandDescriptor.restype = c_int32 |
| 151 | + lib.infiniopCreateExpandDescriptor.argtypes = [ |
| 152 | + infiniopHandle_t, |
| 153 | + POINTER(infiniopExpandDescriptor_t), |
| 154 | + infiniopTensorDescriptor_t, |
| 155 | + infiniopTensorDescriptor_t, |
| 156 | + ] |
| 157 | + lib.infiniopExpand.restype = c_int32 |
| 158 | + lib.infiniopExpand.argtypes = [ |
| 159 | + infiniopExpandDescriptor_t, |
| 160 | + c_void_p, |
| 161 | + c_void_p, |
| 162 | + c_void_p, |
| 163 | + ] |
| 164 | + lib.infiniopDestroyExpandDescriptor.restype = c_int32 |
| 165 | + lib.infiniopDestroyExpandDescriptor.argtypes = [ |
| 166 | + infiniopExpandDescriptor_t, |
| 167 | + ] |
| 168 | + |
| 169 | + if args.cpu: |
| 170 | + test_cpu(lib, test_cases) |
| 171 | + if args.cuda: |
| 172 | + test_cuda(lib, test_cases) |
| 173 | + if args.bang: |
| 174 | + test_bang(lib, test_cases) |
| 175 | + if not (args.cpu or args.cuda or args.bang): |
| 176 | + test_cpu(lib, test_cases) |
| 177 | + print("\033[92mTest passed!\033[0m") |
0 commit comments