scaled_mm_int8.py 6.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import torch
import ctypes
from ctypes import c_uint64
from libinfiniop import (
    LIBINFINIOP,
    TestTensor,
    get_test_devices,
    check_error,
    test_operator,
    get_args,
    debug,
    get_tolerance,
    profile_operation,
    TestWorkspace,
    InfiniDtype,
    InfiniDtypeNames,
    InfiniDeviceNames,
    infiniopOperatorDescriptor_t,
)
from enum import Enum, auto

# ==============================================================================
#  Configuration (Internal Use Only)
# ==============================================================================
# These are not meant to be imported from other modules
_TEST_CASES_ = [
    # x_shape, w_shape, y_shape, alpha, beta
    ((128, 512), (512, 1024), (128, 1024)),
    ((256, 1024), (1024, 2048), (256, 2048)),
    ((1024, 2048), (2048, 1024), (1024, 1024)),
]


class Inplace(Enum):
    OUT_OF_PLACE = auto()
    INPLACE = auto()


# Inplace options applied for each test case in _TEST_CASES_
_INPLACE = [
    Inplace.INPLACE,
]

_TEST_CASES = [
    test_case + (inplace_item,)
    for test_case in _TEST_CASES_
    for inplace_item in _INPLACE
]

# Data types used for testing
_TENSOR_DTYPES = [InfiniDtype.BF16, InfiniDtype.F16]

# Tolerance map for different data types
_TOLERANCE_MAP = {
    InfiniDtype.F16: {"atol": 3e-1, "rtol": 1e-2},
    InfiniDtype.BF16: {"atol": 3e-1, "rtol": 1e-2},
}

DEBUG = False
PROFILE = False
NUM_PRERUN = 10
62
63
NUM_ITERATIONS = 100

64
65
66
67
68
69
70
71
72

def torch_scaled_mm(a, b, scale_a, scale_b, out_dtype, bias):
    o = torch.matmul(a.to(torch.float32), b.to(torch.float32))
    if bias is not None:
        o = o.to(torch.float32) * scale_a.view(-1, 1) * scale_b.view(1, -1) + bias
    else:
        o = o.to(torch.float32) * scale_a.view(-1, 1) * scale_b.view(1, -1)
    return o.to(out_dtype)

73

74
75
76
77
78
79
80
81
82
83
84
def test(
    handle,
    device,
    x_shape,
    w_shape,
    y_shape,
    inplace=Inplace.OUT_OF_PLACE,
    dtype=InfiniDtype.BF16,
    sync=None,
):
    print(
85
        f"Testing scaled_mm_int8 on {InfiniDeviceNames[device]} with x_shape:{x_shape}, w_shape:{w_shape}, inplace:{inplace} dtype:{InfiniDtypeNames[dtype]}"
86
87
88
    )
    M, K = x_shape
    N = w_shape[1]
89

90
91
92
93
94
    # --- Tensor Descriptor ---
    # orig: create a random int8 tensor as the reference data source
    # torch: extract the torch view to adjust layout/stride
    # final: wrap it back as TestTensor with explicit stride for device execution
    x_packed_orig = TestTensor(
95
96
97
98
99
100
101
        (M, K),
        None,
        InfiniDtype.I8,
        device,
        mode="randint",
        randint_low=-128,
        randint_high=127,
102
    )
103
104
105
106
107
108
109
110
111
112
113
114
    x_packed_torch = x_packed_orig.torch_tensor()
    x_packed = TestTensor(
        (M, K),
        x_packed_torch.stride(),
        InfiniDtype.I8,
        device,
        mode="manual",
        set_tensor=x_packed_torch,
    )

    weights_orig = TestTensor(
        (N, K),
115
116
117
118
119
120
        None,
        InfiniDtype.I8,
        device,
        mode="randint",
        randint_low=-128,
        randint_high=127,
121
    )
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    weights_torch = weights_orig.torch_tensor().t()
    weights = TestTensor(
        (K, N),
        weights_torch.stride(),
        InfiniDtype.I8,
        device,
        mode="manual",
        set_tensor=weights_torch,
    )

    x_scale_orig = TestTensor((M,), None, InfiniDtype.F32, device, mode="random")
    x_scale_torch = x_scale_orig.torch_tensor()
    x_scale = TestTensor(
        (M,),
        x_scale_torch.stride(),
        InfiniDtype.F32,
        device,
        mode="manual",
        set_tensor=x_scale_torch,
    )

    weights_scale_orig = TestTensor((N,), None, InfiniDtype.F32, device, mode="random")
    weights_scale_torch = weights_scale_orig.torch_tensor()
    weights_scale = TestTensor(
        (N,),
        weights_scale_torch.stride(),
        InfiniDtype.F32,
        device,
        mode="manual",
        set_tensor=weights_scale_torch,
    )

    bias_orig = TestTensor((N,), None, dtype, device, mode="random")
    bias_torch = bias_orig.torch_tensor()
    bias = TestTensor(
        (N,), bias_torch.stride(), dtype, device, mode="manual", set_tensor=bias_torch
    )

160
161
162
163
164
165
166
167
168
    y = TestTensor(y_shape, None, dtype, device, mode="zeros")

    ans = torch_scaled_mm(
        x_packed.torch_tensor(),
        weights.torch_tensor(),
        x_scale.torch_tensor(),
        weights_scale.torch_tensor(),
        out_dtype=torch.float16 if dtype == InfiniDtype.F16 else torch.bfloat16,
        bias=bias.torch_tensor(),
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    )

    descriptor = infiniopOperatorDescriptor_t()
    check_error(
        LIBINFINIOP.infiniopCreateI8GemmDescriptor(
            handle,
            ctypes.byref(descriptor),
            y.descriptor,
            bias.descriptor,
            x_packed.descriptor,
            x_scale.descriptor,
            weights.descriptor,
            weights_scale.descriptor,
        )
    )

    workspace_size = c_uint64(0)
    check_error(
        LIBINFINIOP.infiniopGetI8GemmWorkspaceSize(
            descriptor, ctypes.byref(workspace_size)
        )
    )
    workspace = TestWorkspace(workspace_size.value, x_packed.device)

    def lib_linear():
        check_error(
            LIBINFINIOP.infiniopI8Gemm(
                descriptor,
                workspace.data(),
                workspace_size.value,
                y.data(),
                bias.data(),
                x_packed.data(),
                x_scale.data(),
                weights.data(),
                weights_scale.data(),
                None,
            )
        )

    lib_linear()

    if sync is not None:
        sync()

    atol, rtol = get_tolerance(_TOLERANCE_MAP, dtype)
    if DEBUG:
        debug(y.actual_tensor(), ans, atol=atol, rtol=rtol)

    assert torch.allclose(y.actual_tensor(), ans, atol=atol, rtol=rtol)

    # Profiling workflow
    if PROFILE:
        # fmt: off
223
224
225
226
227
228
229
230
231
232
233
234
235
236
        profile_operation(
            "PyTorch",
            lambda: torch_scaled_mm(
                x_packed.torch_tensor(),
                weights.torch_tensor(),
                x_scale.torch_tensor(),
                weights_scale.torch_tensor(),
                out_dtype=torch.float16 if dtype == InfiniDtype.F16 else torch.bfloat16,
                bias=bias.torch_tensor()
            ),
            device,
            NUM_PRERUN,
            NUM_ITERATIONS
        )
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
        profile_operation("    lib", lambda: lib_linear(), device, NUM_PRERUN, NUM_ITERATIONS)
        # fmt: on

    check_error(LIBINFINIOP.infiniopDestroyI8GemmDescriptor(descriptor))


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

    # 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):
253
254
255
256
257
258
259
        # muDNN(v3101): INT8 quantized multiplication → BF16 output.
        # Moore backend: BF16 output only.
        if args.moore == True:
            _TENSOR_DTYPES_MOORE = [InfiniDtype.BF16]
            test_operator(device, test, _TEST_CASES, _TENSOR_DTYPES_MOORE)
        else:
            test_operator(device, test, _TEST_CASES, _TENSOR_DTYPES)
260
261

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