swiglu.py 5.2 KB
Newer Older
xgqdut2016's avatar
xgqdut2016 committed
1
import torch
PanZezhongQY's avatar
PanZezhongQY committed
2
import ctypes
3
from ctypes import POINTER, Structure, c_int32, c_void_p
xgqdut2016's avatar
xgqdut2016 committed
4
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
    rearrange_if_needed,
    test_operator,
    get_args,
    debug,
    get_tolerance,
    profile_operation,
PanZezhongQY's avatar
PanZezhongQY committed
17
)
xgqdut2016's avatar
xgqdut2016 committed
18
from enum import Enum, auto
PanZezhongQY's avatar
PanZezhongQY committed
19

xgqdut2016's avatar
xgqdut2016 committed
20
21
22
23
# ==============================================================================
#  Configuration (Internal Use Only)
# ==============================================================================
# These are not meant to be imported from other modules
24
_TEST_CASES_ = [
25
    # shape, a_stride, b_stride, c_stride
26
27
    ((13, 4), None, None, None),
    ((13, 4), (10, 1), (10, 1), (10, 1)),
28
29
    # ((13, 4, 4), None, None, None),
    # ((13, 4, 4), (20, 4, 1), (20, 4, 1), (20, 4, 1)),
30
31
    ((16, 5632), None, None, None),
    ((16, 5632), (13312, 1), (13312, 1), (13312, 1)),
32
33
    # ((4, 4, 5632), None, None, None),
    # ((4, 4, 5632), (45056, 5632, 1), (45056, 5632, 1), (45056, 5632, 1)),
34
35
36
37
38
39
40
41
42
43
]

# 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_
xgqdut2016's avatar
xgqdut2016 committed
44
_TEST_CASES = [
45
46
47
    test_case + (inplace_item,)
    for test_case in _TEST_CASES_
    for inplace_item in _INPLACE
xgqdut2016's avatar
xgqdut2016 committed
48
]
xgqdut2016's avatar
xgqdut2016 committed
49

xgqdut2016's avatar
xgqdut2016 committed
50
# Data types used for testing
xgqdut2016's avatar
xgqdut2016 committed
51
_TENSOR_DTYPES = [torch.float16]
xgqdut2016's avatar
xgqdut2016 committed
52
53
54

# Tolerance map for different data types
_TOLERANCE_MAP = {
xgqdut2016's avatar
xgqdut2016 committed
55
    torch.float16: {"atol": 1e-4, "rtol": 1e-2},
xgqdut2016's avatar
xgqdut2016 committed
56
57
58
59
60
61
}

DEBUG = False
PROFILE = False
NUM_PRERUN = 10
NUM_ITERATIONS = 1000
PanZezhongQY's avatar
PanZezhongQY committed
62

xgqdut2016's avatar
xgqdut2016 committed
63
64
65
66
67
68
69

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


PanZezhongQY's avatar
PanZezhongQY committed
70
71
72
73
74
75
76
77
78
79
class SwiGLUDescriptor(Structure):
    _fields_ = [("device", c_int32)]


infiniopSwiGLUDescriptor_t = POINTER(SwiGLUDescriptor)


def swiglu(a, b):
    return a * b / (1 + torch.exp(-b.float()).to(b.dtype))

80

xgqdut2016's avatar
xgqdut2016 committed
81
def test(
PanZezhongQY's avatar
PanZezhongQY committed
82
83
84
85
86
87
88
    lib,
    handle,
    torch_device,
    shape,
    a_stride=None,
    b_stride=None,
    c_stride=None,
xgqdut2016's avatar
xgqdut2016 committed
89
    inplace=Inplace.OUT_OF_PLACE,
PanZezhongQY's avatar
PanZezhongQY committed
90
91
92
93
    dtype=torch.float16,
    sync=None,
):
    print(
94
95
        f"Testing SwiGLU on {torch_device} with shape:{shape} a_stride:{a_stride} b_stride:{b_stride} c_stride:{c_stride} "
        f"dtype:{dtype} inplace:{inplace}"
PanZezhongQY's avatar
PanZezhongQY committed
96
    )
xgqdut2016's avatar
xgqdut2016 committed
97

PanZezhongQY's avatar
PanZezhongQY committed
98
99
    a = torch.rand(shape, dtype=dtype).to(torch_device)
    b = torch.rand(shape, dtype=dtype).to(torch_device)
100
    c = torch.rand(shape, dtype=dtype).to(torch_device)
PanZezhongQY's avatar
PanZezhongQY committed
101
102
103

    ans = swiglu(a, b)

xgqdut2016's avatar
xgqdut2016 committed
104
105
106
107
    a, b, c = [
        rearrange_if_needed(tensor, stride)
        for tensor, stride in zip([a, b, c], [a_stride, b_stride, c_stride])
    ]
108
109
110
111
112
    c = (
        c
        if inplace == Inplace.OUT_OF_PLACE
        else (a if inplace == Inplace.INPLACE_A else b)
    )
xgqdut2016's avatar
xgqdut2016 committed
113
114
115
116
117
118
    a_tensor, b_tensor = [to_tensor(tensor, lib) for tensor in [a, b]]
    c_tensor = (
        to_tensor(c, lib)
        if inplace == Inplace.OUT_OF_PLACE
        else (a_tensor if inplace == Inplace.INPLACE_A else b_tensor)
    )
PanZezhongQY's avatar
PanZezhongQY committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    if sync is not None:
        sync()

    descriptor = infiniopSwiGLUDescriptor_t()
    check_error(
        lib.infiniopCreateSwiGLUDescriptor(
            handle,
            ctypes.byref(descriptor),
            c_tensor.descriptor,
            a_tensor.descriptor,
            b_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
134
135
136
137
138
139
    for tensor in [a_tensor, b_tensor, c_tensor]:
        tensor.descriptor.contents.invalidate()

    def lib_swiglu():
        check_error(
            lib.infiniopSwiGLU(
xgqdut2016's avatar
xgqdut2016 committed
140
                descriptor, c_tensor.data, a_tensor.data, b_tensor.data, None
xgqdut2016's avatar
xgqdut2016 committed
141
            )
PanZezhongQY's avatar
PanZezhongQY committed
142
        )
xgqdut2016's avatar
xgqdut2016 committed
143

xgqdut2016's avatar
xgqdut2016 committed
144
    lib_swiglu()
PanZezhongQY's avatar
PanZezhongQY committed
145

xgqdut2016's avatar
xgqdut2016 committed
146
147
148
149
    atol, rtol = get_tolerance(_TOLERANCE_MAP, dtype)
    if DEBUG:
        debug(c, ans, atol=atol, rtol=rtol)
    assert torch.allclose(c, ans, atol=atol, rtol=rtol)
PanZezhongQY's avatar
PanZezhongQY committed
150

xgqdut2016's avatar
xgqdut2016 committed
151
152
153
154
155
156
    # Profiling workflow
    if PROFILE:
        # fmt: off
        profile_operation("PyTorch", lambda: swiglu(a, b), torch_device, NUM_PRERUN, NUM_ITERATIONS)
        profile_operation("    lib", lambda: lib_swiglu(), torch_device, NUM_PRERUN, NUM_ITERATIONS)
        # fmt: on
PanZezhongQY's avatar
PanZezhongQY committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    check_error(lib.infiniopDestroySwiGLUDescriptor(descriptor))


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

    lib.infiniopCreateSwiGLUDescriptor.restype = c_int32
    lib.infiniopCreateSwiGLUDescriptor.argtypes = [
        infiniopHandle_t,
        POINTER(infiniopSwiGLUDescriptor_t),
        infiniopTensorDescriptor_t,
        infiniopTensorDescriptor_t,
        infiniopTensorDescriptor_t,
    ]

    lib.infiniopSwiGLU.restype = c_int32
    lib.infiniopSwiGLU.argtypes = [
        infiniopSwiGLUDescriptor_t,
        c_void_p,
        c_void_p,
        c_void_p,
        c_void_p,
    ]

    lib.infiniopDestroySwiGLUDescriptor.restype = c_int32
    lib.infiniopDestroySwiGLUDescriptor.argtypes = [
        infiniopSwiGLUDescriptor_t,
    ]
xgqdut2016's avatar
xgqdut2016 committed
186

xgqdut2016's avatar
xgqdut2016 committed
187
188
189
190
191
    # Configure testing options
    DEBUG = args.debug
    PROFILE = args.profile
    NUM_PRERUN = args.num_prerun
    NUM_ITERATIONS = args.num_iterations
xgqdut2016's avatar
xgqdut2016 committed
192

xgqdut2016's avatar
xgqdut2016 committed
193
194
    for device in get_test_devices(args):
        test_operator(lib, device, test, _TEST_CASES, _TENSOR_DTYPES)
PanZezhongQY's avatar
PanZezhongQY committed
195
196

    print("\033[92mTest passed!\033[0m")