add.py 4.9 KB
Newer Older
PanZezhong's avatar
PanZezhong committed
1
import torch
PanZezhongQY's avatar
PanZezhongQY committed
2
import ctypes
3
from ctypes import c_uint64
PanZezhong's avatar
PanZezhong committed
4
from libinfiniop import (
5
6
    LIBINFINIOP,
    TestTensor,
PanZezhong's avatar
PanZezhong committed
7
    get_test_devices,
PanZezhongQY's avatar
PanZezhongQY committed
8
    check_error,
PanZezhong's avatar
PanZezhong committed
9
10
11
12
13
    test_operator,
    get_args,
    debug,
    get_tolerance,
    profile_operation,
14
15
16
17
18
    TestWorkspace,
    InfiniDtype,
    InfiniDtypeNames,
    InfiniDeviceNames,
    infiniopOperatorDescriptor_t,
PanZezhongQY's avatar
PanZezhongQY committed
19
20
)
from enum import Enum, auto
PanZezhong's avatar
PanZezhong committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

# ==============================================================================
#  Configuration (Internal Use Only)
# ==============================================================================
# These are not meant to be imported from other modules
_TEST_CASES_ = [
    # shape, a_stride, b_stride, c_stride
    ((13, 4), None, None, None),
    ((13, 4), (10, 1), (10, 1), (10, 1)),
    ((13, 4), (0, 1), None, None),
    ((13, 4, 4), None, None, None),
    ((13, 4, 4), (20, 4, 1), (20, 4, 1), (20, 4, 1)),
    ((13, 4, 4), (4, 0, 1), (0, 4, 1), None),
    ((16, 5632), None, None, None),
    ((16, 5632), (13312, 1), (13312, 1), (13312, 1)),
36
37
    ((13, 16, 2), (128, 4, 1), (0, 2, 1), (64, 4, 1)),
    ((13, 16, 2), (128, 4, 1), (2, 0, 1), (64, 4, 1)),
PanZezhong's avatar
PanZezhong committed
38
39
40
    ((4, 4, 5632), None, None, None),
    ((4, 4, 5632), (45056, 5632, 1), (45056, 5632, 1), (45056, 5632, 1)),
]
PanZezhongQY's avatar
PanZezhongQY committed
41
42
43
44
45
46
47
48


class Inplace(Enum):
    OUT_OF_PLACE = auto()
    INPLACE_A = auto()
    INPLACE_B = auto()


PanZezhong's avatar
PanZezhong committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Inplace options applied for each test case in _TEST_CASES_
_INPLACE = [
    Inplace.OUT_OF_PLACE,
    Inplace.INPLACE_A,
    Inplace.INPLACE_B,
]

# Form the test cases by appending each element of _INPLACE to each tuple in _TEST_CASES_
_TEST_CASES = [
    test_case + (inplace_item,)
    for test_case in _TEST_CASES_
    for inplace_item in _INPLACE
]

# Data types used for testing
64
_TENSOR_DTYPES = [InfiniDtype.F16, InfiniDtype.F32, InfiniDtype.BF16]
PanZezhong's avatar
PanZezhong committed
65
66
67

# Tolerance map for different data types
_TOLERANCE_MAP = {
68
69
    InfiniDtype.F16: {"atol": 1e-3, "rtol": 1e-3},
    InfiniDtype.F32: {"atol": 1e-7, "rtol": 1e-7},
70
    InfiniDtype.BF16: {"atol": 1e-3, "rtol": 1e-3},
PanZezhong's avatar
PanZezhong committed
71
72
73
74
75
76
77
78
}

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


79
80
def add(c, a, b):
    torch.add(a, b, out=c)
PanZezhong's avatar
PanZezhong committed
81
82


PanZezhongQY's avatar
PanZezhongQY committed
83
84
def test(
    handle,
85
    device,
PanZezhong's avatar
PanZezhong committed
86
87
88
89
    shape,
    a_stride=None,
    b_stride=None,
    c_stride=None,
PanZezhongQY's avatar
PanZezhongQY committed
90
    inplace=Inplace.OUT_OF_PLACE,
PanZezhong's avatar
PanZezhong committed
91
92
    dtype=torch.float16,
    sync=None,
PanZezhongQY's avatar
PanZezhongQY committed
93
):
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    a = TestTensor(shape, a_stride, dtype, device)
    b = TestTensor(shape, b_stride, dtype, device)
    if inplace == Inplace.INPLACE_A:
        if a_stride != c_stride:
            return
        c = a
    elif inplace == Inplace.INPLACE_B:
        if c_stride != b_stride:
            return
        c = b
    else:
        c = TestTensor(shape, c_stride, dtype, device, mode="ones")

    if c.is_broadcast():
        return

PanZezhongQY's avatar
PanZezhongQY committed
110
    print(
111
112
        f"Testing Add on {InfiniDeviceNames[device]} with shape:{shape} a_stride:{a_stride} b_stride:{b_stride} c_stride:{c_stride} "
        f"dtype:{InfiniDtypeNames[dtype]} inplace:{inplace}"
PanZezhongQY's avatar
PanZezhongQY committed
113
114
    )

115
    add(c.torch_tensor(), a.torch_tensor(), b.torch_tensor())
PanZezhongQY's avatar
PanZezhongQY committed
116

PanZezhong's avatar
PanZezhong committed
117
118
    if sync is not None:
        sync()
PanZezhongQY's avatar
PanZezhongQY committed
119

120
    descriptor = infiniopOperatorDescriptor_t()
PanZezhongQY's avatar
PanZezhongQY committed
121
    check_error(
122
        LIBINFINIOP.infiniopCreateAddDescriptor(
PanZezhongQY's avatar
PanZezhongQY committed
123
124
            handle,
            ctypes.byref(descriptor),
125
126
127
            c.descriptor,
            a.descriptor,
            b.descriptor,
PanZezhongQY's avatar
PanZezhongQY committed
128
129
130
131
        )
    )

    # Invalidate the shape and strides in the descriptor to prevent them from being directly used by the kernel
132
133
    for tensor in [a, b, c]:
        tensor.destroy_desc()
PanZezhongQY's avatar
PanZezhongQY committed
134

PanZezhong's avatar
PanZezhong committed
135
    workspace_size = c_uint64(0)
PanZezhongQY's avatar
PanZezhongQY committed
136
    check_error(
137
138
139
        LIBINFINIOP.infiniopGetAddWorkspaceSize(
            descriptor, ctypes.byref(workspace_size)
        )
PanZezhongQY's avatar
PanZezhongQY committed
140
    )
141
    workspace = TestWorkspace(workspace_size.value, c.device)
PanZezhong's avatar
PanZezhong committed
142
143
144

    def lib_add():
        check_error(
145
            LIBINFINIOP.infiniopAdd(
PanZezhong's avatar
PanZezhong committed
146
                descriptor,
147
148
149
150
151
                workspace.data(),
                workspace.size(),
                c.data(),
                a.data(),
                b.data(),
PanZezhong's avatar
PanZezhong committed
152
153
154
                None,
            )
        )
PanZezhongQY's avatar
PanZezhongQY committed
155

PanZezhong's avatar
PanZezhong committed
156
    lib_add()
PanZezhongQY's avatar
PanZezhongQY committed
157

PanZezhong's avatar
PanZezhong committed
158
159
    atol, rtol = get_tolerance(_TOLERANCE_MAP, dtype)
    if DEBUG:
160
161
        debug(c.actual_tensor(), c.torch_tensor(), atol=atol, rtol=rtol)
    assert torch.allclose(c.actual_tensor(), c.torch_tensor(), atol=atol, rtol=rtol)
PanZezhongQY's avatar
PanZezhongQY committed
162

PanZezhong's avatar
PanZezhong committed
163
164
    # Profiling workflow
    if PROFILE:
165
        # fmt: off
166
167
        profile_operation("PyTorch", lambda: add(c.torch_tensor(), a.torch_tensor(), b.torch_tensor()), device, NUM_PRERUN, NUM_ITERATIONS)
        profile_operation("    lib", lambda: lib_add(), device, NUM_PRERUN, NUM_ITERATIONS)
168
        # fmt: on
169
    check_error(LIBINFINIOP.infiniopDestroyAddDescriptor(descriptor))
PanZezhongQY's avatar
PanZezhongQY committed
170
171
172
173
174


if __name__ == "__main__":
    args = get_args()

PanZezhong's avatar
PanZezhong committed
175
176
177
178
179
180
181
    # Configure testing options
    DEBUG = args.debug
    PROFILE = args.profile
    NUM_PRERUN = args.num_prerun
    NUM_ITERATIONS = args.num_iterations

    for device in get_test_devices(args):
182
        test_operator(device, test, _TEST_CASES, _TENSOR_DTYPES)
PanZezhong's avatar
PanZezhong committed
183

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