rearrange.py 4.23 KB
Newer Older
xgqdut2016's avatar
xgqdut2016 committed
1
import torch
PanZezhongQY's avatar
PanZezhongQY committed
2
import ctypes
xgqdut2016's avatar
xgqdut2016 committed
3
4
from ctypes import POINTER, Structure, c_int32, c_size_t, c_uint64, c_void_p, c_float
from libinfiniop import (
PanZezhongQY's avatar
PanZezhongQY committed
5
6
    infiniopHandle_t,
    infiniopTensorDescriptor_t,
xgqdut2016's avatar
xgqdut2016 committed
7
8
9
    open_lib,
    to_tensor,
    get_test_devices,
PanZezhongQY's avatar
PanZezhongQY committed
10
    check_error,
xgqdut2016's avatar
xgqdut2016 committed
11
12
13
14
15
16
17
    rearrange_if_needed,
    create_workspace,
    test_operator,
    get_args,
    debug,
    get_tolerance,
    profile_operation,
PanZezhongQY's avatar
PanZezhongQY committed
18
19
)

xgqdut2016's avatar
xgqdut2016 committed
20
21
22
23
24
25
# ==============================================================================
#  Configuration (Internal Use Only)
# ==============================================================================
# These are not meant to be imported from other modules
_TEST_CASES = [
    # ((src_shape, src_stride), (dst_shape, dst_stride))
xgqdut2016's avatar
xgqdut2016 committed
26
27
28
29
30
31
32
    (((2, 4, 32), None), ((2, 4, 32), (256, 64, 1))),
    (((32, 6, 64), (64, 2560, 1)), ((32, 6, 64), None)),
    (((4, 6, 64), (64, 2560, 1)), ((4, 6, 64), (131072, 64, 1))),
    (((1, 32, 64), (2048, 64, 1)), ((1, 32, 64), (2048, 64, 1))),
    (((32, 1, 64), (64, 2560, 1)), ((32, 1, 64), (64, 64, 1))),
    (((4, 1, 64), (64, 2560, 1)), ((4, 1, 64), (64, 11264, 1))),
    (((64,), (1,)), ((64,), (1,))),
xgqdut2016's avatar
xgqdut2016 committed
33
34
35
36
37
38
39
]

# Data types used for testing
_TENSOR_DTYPES = [torch.float16, torch.float32]

# Tolerance map for different data types
_TOLERANCE_MAP = {
xgqdut2016's avatar
xgqdut2016 committed
40
41
    torch.float16: {"atol": 0, "rtol": 0},
    torch.float32: {"atol": 0, "rtol": 0},
xgqdut2016's avatar
xgqdut2016 committed
42
43
44
45
46
47
48
}

DEBUG = False
PROFILE = False
NUM_PRERUN = 10
NUM_ITERATIONS = 1000

PanZezhongQY's avatar
PanZezhongQY committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

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


infiniopRearrangeDescriptor_t = POINTER(RerrangeDescriptor)


def test(
    lib,
    handle,
    torch_device,
    x_shape,
    x_stride,
    y_shape,
    y_stride,
    x_dtype=torch.float16,
):
    print(
        f"Testing Rerrange on {torch_device} with x_shape:{x_shape} x_stride:{x_stride} y_shape:{y_shape} y_stride:{y_stride} x_dtype:{x_dtype}"
    )
xgqdut2016's avatar
xgqdut2016 committed
70

PanZezhongQY's avatar
PanZezhongQY committed
71
72
    x = torch.rand(x_shape, dtype=x_dtype).to(torch_device)
    y = torch.zeros(y_shape, dtype=x_dtype).to(torch_device)
xgqdut2016's avatar
xgqdut2016 committed
73

xgqdut2016's avatar
xgqdut2016 committed
74
75
76
77
78
79
    x, y = [
        rearrange_if_needed(tensor, stride)
        for tensor, stride in zip([x, y], [x_stride, y_stride])
    ]
    x_tensor, y_tensor = [to_tensor(tensor, lib) for tensor in [x, y]]

PanZezhongQY's avatar
PanZezhongQY committed
80
81
82
83
84
85
86
87
    descriptor = infiniopRearrangeDescriptor_t()
    check_error(
        lib.infiniopCreateRearrangeDescriptor(
            handle, ctypes.byref(descriptor), y_tensor.descriptor, x_tensor.descriptor
        )
    )

    # Invalidate the shape and strides in the descriptor to prevent them from being directly used by the kernel
xgqdut2016's avatar
xgqdut2016 committed
88
89
90
91
92
    for tensor in [x_tensor, y_tensor]:
        tensor.descriptor.contents.invalidate()

    def lib_rearrange():
        check_error(
xgqdut2016's avatar
xgqdut2016 committed
93
            lib.infiniopRearrange(descriptor, y_tensor.data, x_tensor.data, None)
xgqdut2016's avatar
xgqdut2016 committed
94
        )
xgqdut2016's avatar
xgqdut2016 committed
95

xgqdut2016's avatar
xgqdut2016 committed
96
    lib_rearrange()
xgqdut2016's avatar
xgqdut2016 committed
97

xgqdut2016's avatar
xgqdut2016 committed
98
99
100
101
102
103
104
105
106
107
108
109
    # Validate results
    atol, rtol = get_tolerance(_TOLERANCE_MAP, dtype)
    if DEBUG:
        debug(x, y, atol=atol, rtol=rtol)
    assert torch.allclose(x, y, atol=atol, rtol=rtol)

    # Profiling workflow
    if PROFILE:
        # fmt: off
        profile_operation("PyTorch", lambda: rearrange_tensor(y, y_stride), torch_device, NUM_PRERUN, NUM_ITERATIONS)
        profile_operation("    lib", lambda: lib_rearrange(), torch_device, NUM_PRERUN, NUM_ITERATIONS)
        # fmt: on
PanZezhongQY's avatar
PanZezhongQY committed
110
111
112
113
114
115
116

    check_error(lib.infiniopDestroyRearrangeDescriptor(descriptor))


if __name__ == "__main__":
    args = get_args()
    lib = open_lib()
xgqdut2016's avatar
xgqdut2016 committed
117

PanZezhongQY's avatar
PanZezhongQY committed
118
119
120
121
122
123
124
    lib.infiniopCreateRearrangeDescriptor.restype = c_int32
    lib.infiniopCreateRearrangeDescriptor.argtypes = [
        infiniopHandle_t,
        POINTER(infiniopRearrangeDescriptor_t),
        infiniopTensorDescriptor_t,
        infiniopTensorDescriptor_t,
    ]
xgqdut2016's avatar
xgqdut2016 committed
125

PanZezhongQY's avatar
PanZezhongQY committed
126
127
128
129
130
131
132
    lib.infiniopRearrange.restype = c_int32
    lib.infiniopRearrange.argtypes = [
        infiniopRearrangeDescriptor_t,
        c_void_p,
        c_void_p,
        c_void_p,
    ]
xgqdut2016's avatar
xgqdut2016 committed
133

PanZezhongQY's avatar
PanZezhongQY committed
134
135
    lib.infiniopDestroyRearrangeDescriptor.restype = c_int32
    lib.infiniopDestroyRearrangeDescriptor.argtypes = [infiniopRearrangeDescriptor_t]
xgqdut2016's avatar
xgqdut2016 committed
136

xgqdut2016's avatar
xgqdut2016 committed
137
138
139
140
141
142
143
144
145
146
    # Configure testing options
    DEBUG = args.debug
    PROFILE = args.profile
    NUM_PRERUN = args.num_prerun
    NUM_ITERATIONS = args.num_iterations

    # Execute tests
    for device in get_test_devices(args):
        test_operator(lib, device, test, _TEST_CASES, _TENSOR_DTYPES)

PanZezhongQY's avatar
PanZezhongQY committed
147
    print("\033[92mTest passed!\033[0m")