fp8_utils.py 21.5 KB
Newer Older
HandH1998's avatar
HandH1998 committed
1
from typing import List, Optional, Tuple
HAI's avatar
HAI committed
2
3

import torch
HandH1998's avatar
HandH1998 committed
4

Lianmin Zheng's avatar
Lianmin Zheng committed
5
6
7
8
9
10
11
try:
    from vllm import _custom_ops as vllm_ops

    VLLM_AVAILABLE = True
except ImportError:
    VLLM_AVAILABLE = False

HandH1998's avatar
HandH1998 committed
12
from sglang.srt.layers.quantization.fp8_kernel import (
13
    _enable_jit_deepgemm,
HandH1998's avatar
HandH1998 committed
14
    per_token_group_quant_fp8,
Lianmin Zheng's avatar
Lianmin Zheng committed
15
16
    scaled_fp8_quant,
    sglang_per_token_quant_fp8,
HandH1998's avatar
HandH1998 committed
17
    static_quant_fp8,
HandH1998's avatar
HandH1998 committed
18
19
    w8a8_block_fp8_matmul,
)
HandH1998's avatar
HandH1998 committed
20
21
22
23
from sglang.srt.utils import (
    get_bool_env_var,
    get_cuda_version,
    get_device_capability,
Lianmin Zheng's avatar
Lianmin Zheng committed
24
    is_cuda,
HandH1998's avatar
HandH1998 committed
25
26
27
    is_hip,
)

28
_is_hip = is_hip()
Lianmin Zheng's avatar
Lianmin Zheng committed
29
30
_is_cuda = is_cuda()

31
if _is_hip and get_bool_env_var("CK_MOE"):
yigex's avatar
yigex committed
32
33
    from aiter import gemm_a8w8_blockscale

34
if _is_cuda:
35
    from sgl_kernel import fp8_blockwise_scaled_mm, fp8_scaled_mm
HAI's avatar
HAI committed
36

Lianmin Zheng's avatar
Lianmin Zheng committed
37
use_vllm_cutlass_w8a8_fp8_kernel = get_bool_env_var("USE_VLLM_CUTLASS_W8A8_FP8_KERNEL")
HandH1998's avatar
HandH1998 committed
38

HandH1998's avatar
HandH1998 committed
39
40
# Input scaling factors are no longer optional in _scaled_mm starting
# from pytorch 2.5. Allocating a dummy tensor to pass as input_scale
Lianmin Zheng's avatar
Lianmin Zheng committed
41
TORCH_DEVICE_IDENTITY = None
HandH1998's avatar
HandH1998 committed
42

43
44
45
46
47
48
49
50
51
52
53
54
55
56
_TORCH_VERSION = torch.__version__.split("+")[0]
try:
    _TORCH_VERSION_TUPLE = tuple(map(int, _TORCH_VERSION.split(".")[:3]))
except ValueError:
    _TORCH_VERSION_TUPLE = (0, 0, 0)

# The condition to determine if it is on a platform that supports
# torch._scaled_mm rowwise feature.
# The condition is determined once as the operations
# are time consuming.
USE_ROWWISE_TORCH_SCALED_MM = (
    _is_hip and get_device_capability() >= (9, 4) and _TORCH_VERSION_TUPLE >= (2, 7, 0)
)

HandH1998's avatar
HandH1998 committed
57
58
59
60
61
62
63
64
65
66
67
68

def cutlass_fp8_supported():
    if not _is_cuda:
        return False
    major, minor = get_device_capability()
    cuda_version = get_cuda_version()
    if major >= 9:
        return cuda_version >= (12, 0)
    elif major == 8 and minor == 9:
        return cuda_version >= (12, 4)
    return False

HAI's avatar
HAI committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

def normalize_e4m3fn_to_e4m3fnuz(
    weight: torch.Tensor,
    weight_scale: torch.Tensor,
    input_scale: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]]:
    assert weight.dtype == torch.float8_e4m3fn
    # The bits pattern 10000000(-128) represents zero in e4m3fn
    # but NaN in e4m3fnuz. So here we set it to 0.
    # https://onnx.ai/onnx/technical/float8.html
    weight_as_int8 = weight.view(torch.int8)
    ROCM_FP8_NAN_AS_INT = -128
    weight_as_int8[weight_as_int8 == ROCM_FP8_NAN_AS_INT] = 0
    weight = weight_as_int8.view(torch.float8_e4m3fnuz)

    # For the same bits representation, e4m3fnuz value is half of
    # the e4m3fn value, so we should double the scaling factor to
    # get the same dequantized value.
    # https://onnx.ai/onnx/technical/float8.html
    weight_scale = weight_scale * 2.0
    if input_scale is not None:
        input_scale = input_scale * 2.0
    return weight, weight_scale, input_scale
HandH1998's avatar
HandH1998 committed
92
93


94
def cutlass_block_fp8_supported() -> bool:
95
    if not get_bool_env_var("SUPPORT_CUTLASS_BLOCK_FP8"):
96
        return False
97
98
99
100
101
102
103
104
105
106
107
108
    if _is_cuda:
        major, minor = torch.cuda.get_device_capability()
        sm_version = major * 10 + minor
        cuda_version = tuple(map(int, torch.version.cuda.split(".")))
        if cuda_version >= (12, 0) and sm_version >= 90:
            return True
    return False


CUTLASS_BLOCK_FP8_SUPPORTED = cutlass_block_fp8_supported()


HandH1998's avatar
HandH1998 committed
109
110
111
112
113
114
115
116
117
118
119
120
def apply_w8a8_block_fp8_linear(
    input: torch.Tensor,
    weight: torch.Tensor,
    block_size: List[int],
    weight_scale: torch.Tensor,
    input_scale: Optional[torch.Tensor] = None,
    bias: Optional[torch.Tensor] = None,
) -> torch.Tensor:
    assert input_scale is None
    # View input as 2D matrix for fp8 methods
    input_2d = input.view(-1, input.shape[-1])
    output_shape = [*input.shape[:-1], weight.shape[0]]
121
122
123
    # TODO: add more robust shape check here
    shape_supported_by_cutlass = (
        weight.shape[0] % 128 == 0 and weight.shape[1] % 128 == 0
HandH1998's avatar
HandH1998 committed
124
    )
125
126
127
128
129
130
131
    if CUTLASS_BLOCK_FP8_SUPPORTED and shape_supported_by_cutlass:
        q_input, x_scale = per_token_group_quant_fp8(
            input_2d, block_size[1], column_major_scales=True
        )
        output = fp8_blockwise_scaled_mm(
            q_input, weight.T, x_scale, weight_scale.T, out_dtype=input.dtype
        )
132
    elif _is_hip and get_bool_env_var("CK_MOE"):
yigex's avatar
yigex committed
133
134
135
136
137
138
139
140
141
        q_input, x_scale = per_token_group_quant_fp8(
            input_2d, block_size[1], column_major_scales=False
        )
        output = torch.zeros(
            [q_input.shape[0], weight.shape[0]],
            dtype=input.dtype,
            device=q_input.device,
        )
        gemm_a8w8_blockscale(q_input, weight, x_scale, weight_scale, output)
142
    else:
143
        if _enable_jit_deepgemm:
144
            q_input, x_scale = sglang_per_token_group_quant_fp8(
145
146
147
148
149
150
151
152
153
                input_2d,
                block_size[1],
                column_major_scales=True,
                scale_tma_aligned=True,
            )
        else:
            q_input, x_scale = per_token_group_quant_fp8(
                input_2d, block_size[1], column_major_scales=False
            )
154
155
156
        output = w8a8_block_fp8_matmul(
            q_input, weight, x_scale, weight_scale, block_size, output_dtype=input.dtype
        )
HandH1998's avatar
HandH1998 committed
157
158
159
160
161
162
163
164
165
166
167
168

    if bias is not None:
        output = output + bias
    return output.to(dtype=input.dtype).view(*output_shape)


def input_to_float8(
    x: torch.Tensor, dtype: torch.dtype = torch.float8_e4m3fn
) -> Tuple[torch.Tensor, torch.Tensor]:
    """This function quantizes input values to float8 values with tensor-wise quantization."""
    finfo = torch.finfo(dtype)
    min_val, max_val = x.aminmax()
169
    amax = torch.maximum(min_val.abs(), max_val.abs()).float().clamp(min=1e-12)
170
    fp8_max = finfo.max
171
    if _is_hip:
172
        dtype = torch.float8_e4m3fnuz
173
174
        fp8_max = 224.0
    scale = fp8_max / amax
175
    x_scl_sat = (x.float() * scale).clamp(min=-fp8_max, max=fp8_max)
HandH1998's avatar
HandH1998 committed
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
    return x_scl_sat.to(dtype).contiguous(), scale.float().reciprocal()


def block_quant_to_tensor_quant(
    x_q_block: torch.Tensor,
    x_s: torch.Tensor,
    block_size: List[int],
) -> Tuple[torch.Tensor, torch.Tensor]:
    """This function converts block-wise quantization to tensor-wise quantization.
    The inputs are block-wise quantization tensor `x_q_block`, block-wise quantization scale
    and the block size.
    The outputs are tensor-wise quantization tensor and tensor-wise quantization scale.
    Note only float8 is supported for now.
    """
    block_n, block_k = block_size[0], block_size[1]
    n, k = x_q_block.shape
    n_tiles = (n + block_n - 1) // block_n
    k_tiles = (k + block_k - 1) // block_k
    assert n_tiles == x_s.shape[0]
    assert k_tiles == x_s.shape[1]

    x_dq_block = x_q_block.to(torch.float32)

    x_dq_block_tiles = [
        [
            x_dq_block[
                j * block_n : min((j + 1) * block_n, n),
                i * block_k : min((i + 1) * block_k, k),
            ]
            for i in range(k_tiles)
        ]
        for j in range(n_tiles)
    ]

    for i in range(k_tiles):
        for j in range(n_tiles):
            x_dq_block_tiles[j][i][:, :] = x_dq_block_tiles[j][i] * x_s[j][i]

214
    x_q_tensor, scale = (
Lianmin Zheng's avatar
Lianmin Zheng committed
215
        scaled_fp8_quant(x_dq_block)
216
217
218
        if _is_cuda
        else input_to_float8(x_dq_block, dtype=x_q_block.dtype)
    )
HandH1998's avatar
HandH1998 committed
219
220
221
    return x_q_tensor, scale


222
223
224
225
226
def channel_quant_to_tensor_quant(
    x_q_channel: torch.Tensor,
    x_s: torch.Tensor,
) -> Tuple[torch.Tensor, torch.Tensor]:
    x_dq_channel = x_q_channel.to(torch.float32) * x_s
227
    x_q_tensor, scale = (
Lianmin Zheng's avatar
Lianmin Zheng committed
228
        scaled_fp8_quant(x_dq_channel)
229
230
231
        if _is_cuda
        else input_to_float8(x_dq_channel, dtype=x_q_channel.dtype)
    )
232
233
234
    return x_q_tensor, scale


HandH1998's avatar
HandH1998 committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
def apply_fp8_linear(
    input: torch.Tensor,
    weight: torch.Tensor,
    weight_scale: torch.Tensor,
    input_scale: Optional[torch.Tensor] = None,
    input_scale_ub: Optional[torch.Tensor] = None,
    bias: Optional[torch.Tensor] = None,
    cutlass_fp8_supported: bool = True,
    use_per_token_if_dynamic: bool = False,
) -> torch.Tensor:
    # View input as 2D matrix for fp8 methods
    input_2d = input.view(-1, input.shape[-1])
    output_shape = [*input.shape[:-1], weight.shape[1]]

    # cutlass w8a8 fp8 sgl-kernel only supports per-token scale
    if input_scale is not None:
        assert input_scale.numel() == 1
        # broadcast per-tensor scale to per-token scale when supporting cutlass
        qinput, x_scale = static_quant_fp8(
            input_2d, input_scale, repeat_scale=cutlass_fp8_supported
        )
    else:
        # default use per-token quantization if dynamic
        if _is_cuda:
            qinput, x_scale = sglang_per_token_quant_fp8(input_2d)
        else:
261
262
263
264
            # TODO(kkhuang): temporarily enforce per-tensor activation scaling if weight is per-tensor scaling
            # final solution should be: 1. add support to per-tensor activation scaling.
            # 2. solve the torch.compile error from weight_scale.numel() == 1 and x_scale.numel() > 1 (below line#308)
            if _is_hip and weight_scale.numel() == 1:
Lianmin Zheng's avatar
Lianmin Zheng committed
265
                qinput, x_scale = vllm_ops.scaled_fp8_quant(
266
267
268
269
270
271
272
273
                    input_2d,
                    input_scale,
                    use_per_token_if_dynamic=use_per_token_if_dynamic,
                )
            else:
                qinput, x_scale = per_token_group_quant_fp8(
                    input_2d, group_size=input_2d.shape[1]
                )
HandH1998's avatar
HandH1998 committed
274
275

    if cutlass_fp8_supported:
Lianmin Zheng's avatar
Lianmin Zheng committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
        if VLLM_AVAILABLE and use_vllm_cutlass_w8a8_fp8_kernel:
            # Fall back to vllm cutlass w8a8 fp8 kernel
            output = vllm_ops.cutlass_scaled_mm(
                qinput,
                weight,
                out_dtype=input.dtype,
                scale_a=x_scale,
                scale_b=weight_scale,
                bias=bias,
            )
        else:
            assert (
                weight_scale.numel() == weight.shape[1]
            ), "cutlass w8a8 fp8 sgl-kernel only supports per-channel scale"
            output = fp8_scaled_mm(
                qinput,
                weight,
                x_scale,
                weight_scale,
                out_dtype=input.dtype,
                bias=bias,
            )
        return output.view(*output_shape)
HandH1998's avatar
HandH1998 committed
299
300
301

    # torch.scaled_mm supports per tensor weights + activations only
    # so fallback to naive if per channel or per token
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
    else:
        per_tensor_weights = weight_scale.numel() == 1
        per_tensor_activations = x_scale.numel() == 1

        if per_tensor_weights and per_tensor_activations:
            # Fused GEMM_DQ
            output = torch._scaled_mm(
                qinput,
                weight,
                out_dtype=input.dtype,
                scale_a=x_scale,
                scale_b=weight_scale,
                bias=bias,
            )
            # A fix for discrepancy in scaled_mm which returns tuple
            # for torch < 2.5 and a single value in torch >= 2.5
            if type(output) is tuple and len(output) == 2:
                output = output[0]
HandH1998's avatar
HandH1998 committed
320

321
            return torch.narrow(output, 0, 0, input_2d.shape[0]).view(*output_shape)
HandH1998's avatar
HandH1998 committed
322

323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
        else:
            # Fallback for channelwise case, where we use unfused DQ
            # due to limitations with scaled_mm

            # Symmetric quantized GEMM by definition computes the following:
            #   C = (s_x * X) (s_w * W) + bias
            # This is equivalent to dequantizing the weights and activations
            # before applying a GEMM.
            #
            # In order to compute quantized operands, a quantized kernel
            # will rewrite the above like so:
            #   C = s_w * s_x * (X * W) + bias
            #
            # For the scaled_mm fallback case, we break this down, since it
            # does not support s_w being a vector.

            # Making sure the dummy tensor is on the same device as the weight
            global TORCH_DEVICE_IDENTITY
Lianmin Zheng's avatar
Lianmin Zheng committed
341
342
343
344
            if TORCH_DEVICE_IDENTITY is None:
                TORCH_DEVICE_IDENTITY = torch.ones(
                    1, dtype=torch.float32, device=weight.device
                )
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369

            # GEMM
            # This computes C = (X * W).
            # Output in fp32 to allow subsequent ops to happen in-place
            output = torch._scaled_mm(
                qinput,
                weight,
                scale_a=TORCH_DEVICE_IDENTITY,
                scale_b=TORCH_DEVICE_IDENTITY,
                out_dtype=torch.float32,
            )
            # A fix for discrepancy in scaled_mm which returns tuple
            # for torch < 2.5 and a single value in torch >= 2.5
            if type(output) is tuple and len(output) == 2:
                output = output[0]
            # Unpad (undo num_token_padding)
            output = torch.narrow(output, 0, 0, input_2d.shape[0])
            x_scale = torch.narrow(x_scale, 0, 0, input_2d.shape[0])

            # DQ
            # C = sw * sx * (X * W) + bias
            output = output * x_scale * weight_scale.t()
            if bias is not None:
                output = output + bias
            return output.to(dtype=input.dtype).view(*output_shape)
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397


# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/model_executor/layers/quantization/utils/w8a8_utils.py
# TODO(luka): follow similar pattern for marlin and block-fp8-linear
#  https://github.com/vllm-project/vllm/issues/14397
class Fp8LinearOp:
    """
    This class executes a FP8 linear layer using cutlass if supported and
    torch.scaled_mm otherwise.
    It needs to be a class instead of a method so that config can be read
    in the __init__ method, as reading config is not allowed inside forward.
    """

    def __init__(
        self,
        cutlass_fp8_supported: bool = cutlass_fp8_supported(),
        use_per_token_if_dynamic: bool = False,
        pad_output: Optional[bool] = None,
    ):
        self.cutlass_fp8_supported = cutlass_fp8_supported
        self.use_per_token_if_dynamic = use_per_token_if_dynamic

        # Note: we pad the input because torch._scaled_mm is more performant
        # for matrices with batch dimension > 16.
        # This could change in the future.
        # We also don't pad when using torch.compile,
        # as it breaks with dynamic shapes.
        if pad_output is None:
Lianmin Zheng's avatar
Lianmin Zheng committed
398
            enable_torch_compile = get_bool_env_var("SGLANG_ENABLE_TORCH_COMPILE")
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
            pad_output = not enable_torch_compile
        self.output_padding = 17 if pad_output else None

    def apply(
        self,
        input: torch.Tensor,
        weight: torch.Tensor,
        weight_scale: torch.Tensor,
        input_scale: Optional[torch.Tensor] = None,
        input_scale_ub: Optional[torch.Tensor] = None,
        bias: Optional[torch.Tensor] = None,
        # TODO(luka) remove this parameter in favor of __init__
        use_per_token_if_dynamic: Optional[bool] = None,
    ) -> torch.Tensor:
        # ops.scaled_fp8_quant supports both dynamic and static quant.
        #   If dynamic, layer.input_scale is None and x_scale computed from x.
        #   If static, layer.input_scale is scalar and x_scale is input_scale.

        # View input as 2D matrix for fp8 methods
        input_2d = input.view(-1, input.shape[-1])
        output_shape = [*input.shape[:-1], weight.shape[1]]

        # TODO(luka) this is here because currently MLA only decides this
        #  during the forward method instead of in __init__.
        if use_per_token_if_dynamic is None:
            use_per_token_if_dynamic = self.use_per_token_if_dynamic

        # cutlass_scaled_mm supports per tensor/channel W and per tensor/token A
        # for sgl-kernel fp8_scaled_mm, it support per channel W now
        if self.cutlass_fp8_supported and weight_scale.numel() == weight.shape[1]:
            if _is_cuda:
Lianmin Zheng's avatar
Lianmin Zheng committed
430
                qinput, x_scale = scaled_fp8_quant(
431
432
433
434
435
                    input_2d,
                    input_scale,
                    use_per_token_if_dynamic=use_per_token_if_dynamic,
                )
            else:
Lianmin Zheng's avatar
Lianmin Zheng committed
436
                qinput, x_scale = vllm_ops.scaled_fp8_quant(
437
438
439
440
441
442
443
444
445
                    input_2d,
                    input_scale,
                    scale_ub=input_scale_ub,
                    use_per_token_if_dynamic=use_per_token_if_dynamic,
                )

            # Fused GEMM_DQ
            if VLLM_AVAILABLE and use_vllm_cutlass_w8a8_fp8_kernel:
                # Fall back to vllm cutlass w8a8 fp8 kernel
Lianmin Zheng's avatar
Lianmin Zheng committed
446
                output = vllm_ops.cutlass_scaled_mm(
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
                    qinput,
                    weight,
                    out_dtype=input.dtype,
                    scale_a=x_scale,
                    scale_b=weight_scale,
                    bias=bias,
                )
            else:
                assert (
                    weight_scale.numel() == weight.shape[1]
                ), "cutlass w8a8 fp8 sgl-kernel only supports per-channel scale"
                output = fp8_scaled_mm(
                    qinput,
                    weight,
                    x_scale,
                    weight_scale,
                    out_dtype=input.dtype,
                    bias=bias,
                )
            return output.view(*output_shape)

        # torch.scaled_mm supports per tensor weights + activations only
        # so fallback to naive if per channel or per token
        else:
            # Maybe apply padding to output, see comment in __init__
            if _is_cuda:
Lianmin Zheng's avatar
Lianmin Zheng committed
473
                qinput, x_scale = scaled_fp8_quant(
474
475
                    input_2d,
                    input_scale,
476
                    num_token_padding=self.output_padding,
477
478
479
                    use_per_token_if_dynamic=use_per_token_if_dynamic,
                )
            else:
Lianmin Zheng's avatar
Lianmin Zheng committed
480
                qinput, x_scale = vllm_ops.scaled_fp8_quant(
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
                    input_2d,
                    input_scale,
                    num_token_padding=self.output_padding,
                    use_per_token_if_dynamic=use_per_token_if_dynamic,
                )

            per_tensor_weights = weight_scale.numel() == 1
            per_tensor_activations = x_scale.numel() == 1

            if per_tensor_weights and per_tensor_activations:
                # Fused GEMM_DQ
                output = torch._scaled_mm(
                    qinput,
                    weight,
                    out_dtype=input.dtype,
                    scale_a=x_scale,
                    scale_b=weight_scale,
                    bias=bias,
                )
                # A fix for discrepancy in scaled_mm which returns tuple
                # for torch < 2.5 and a single value in torch >= 2.5
                if type(output) is tuple and len(output) == 2:
                    output = output[0]

                return torch.narrow(output, 0, 0, input_2d.shape[0]).view(*output_shape)

            elif (
                use_per_token_if_dynamic
                and not per_tensor_weights
                and not per_tensor_activations
                and USE_ROWWISE_TORCH_SCALED_MM
            ):
                # For now validated on ROCm platform
                # fp8 rowwise scaling in torch._scaled_mm is introduced in
                # https://github.com/pytorch/pytorch/pull/144432 using hipBLASLt
                # and ROCm 6.3, which only exists in torch 2.7 and above.
                # For CUDA platform please validate if the
                # torch._scaled_mm support rowwise scaled GEMM
                # Fused GEMM_DQ Rowwise GEMM
                output = torch._scaled_mm(
                    qinput,
                    weight,
                    out_dtype=input.dtype,
                    scale_a=x_scale,
                    scale_b=weight_scale.t(),
                    bias=bias,
                )

                output = torch.narrow(output, 0, 0, input_2d.shape[0])
                output = output.view(*output_shape)
                return output

            else:
                # Fallback for channelwise case, where we use unfused DQ
                # due to limitations with scaled_mm

                # Symmetric quantized GEMM by definition computes the following:
                #   C = (s_x * X) (s_w * W) + bias
                # This is equivalent to dequantizing the weights and activations
                # before applying a GEMM.
                #
                # In order to compute quantized operands, a quantized kernel
                # will rewrite the above like so:
                #   C = s_w * s_x * (X * W) + bias
                #
                # For the scaled_mm fallback case, we break this down, since it
                # does not support s_w being a vector.

                # GEMM
                # This computes C = (X * W).
                # Output in fp32 to allow subsequent ops to happen in-place

Lianmin Zheng's avatar
Lianmin Zheng committed
553
                # Making sure the dummy tensor is on the same device as the weight
554
                global TORCH_DEVICE_IDENTITY
Lianmin Zheng's avatar
Lianmin Zheng committed
555
556
557
558
                if TORCH_DEVICE_IDENTITY is None:
                    TORCH_DEVICE_IDENTITY = torch.ones(
                        1, dtype=torch.float32, device=weight.device
                    )
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580

                output = torch._scaled_mm(
                    qinput,
                    weight,
                    scale_a=TORCH_DEVICE_IDENTITY,
                    scale_b=TORCH_DEVICE_IDENTITY,
                    out_dtype=torch.float32,
                )
                # A fix for discrepancy in scaled_mm which returns tuple
                # for torch < 2.5 and a single value in torch >= 2.5
                if type(output) is tuple and len(output) == 2:
                    output = output[0]
                # Unpad (undo num_token_padding)
                output = torch.narrow(output, 0, 0, input_2d.shape[0])
                x_scale = torch.narrow(x_scale, 0, 0, input_2d.shape[0])

                # DQ
                # C = sw * sx * (X * W) + bias
                output = output * x_scale * weight_scale.t()
                if bias is not None:
                    output = output + bias
                return output.to(dtype=input.dtype).view(*output_shape)