File size: 11,874 Bytes
9022070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
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
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
223
224
225
226
227
228
229
230
231
232
233
234
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""FlashRT FP8 GEMM kernels."""

from __future__ import annotations

import torch

from ._ops import add_op_namespace_prefix, ops


@torch.library.register_fake(add_op_namespace_prefix("fp8_linear_bf16"))
def _fp8_linear_bf16_fake(
    input: torch.Tensor,
    weight: torch.Tensor,
    alpha: float,
    variant: int,
    out: torch.Tensor,
) -> None:
    if input.dim() != 2 or weight.dim() != 2:
        raise RuntimeError("input and weight must be rank-2 tensors")
    if out.shape != (input.shape[0], weight.shape[0]):
        raise RuntimeError("out must have shape (input.shape[0], weight.shape[0])")
    return None


@torch.library.register_fake(add_op_namespace_prefix("fp8_linear_residual_bf16"))
def _fp8_linear_residual_bf16_fake(
    input: torch.Tensor,
    weight: torch.Tensor,
    alpha: float,
    variant: int,
    residual: torch.Tensor,
) -> None:
    if input.shape[0] != 1:
        raise RuntimeError("residual path supports only M=1")
    if residual.shape != (1, weight.shape[0]):
        raise RuntimeError("residual must have shape (1, weight.shape[0])")
    return None


def _check_bias_linear_shapes(input, weight, bias, out) -> None:
    if input.dim() != 2 or weight.dim() != 2:
        raise RuntimeError("input and weight must be rank-2 tensors")
    if input.shape[1] != weight.shape[1]:
        raise RuntimeError("input and weight K dimensions must match")
    if bias.shape != (weight.shape[0],):
        raise RuntimeError("bias must have shape (weight.shape[0],)")
    if out.shape != (input.shape[0], weight.shape[0]):
        raise RuntimeError("out must have shape (input.shape[0], weight.shape[0])")


@torch.library.register_fake(add_op_namespace_prefix("fp8_linear_bias_bf16"))
def _fp8_linear_bias_bf16_fake(input, weight, bias, alpha: float, out) -> None:
    _check_bias_linear_shapes(input, weight, bias, out)


@torch.library.register_fake(add_op_namespace_prefix("fp8_linear_bias_residual_bf16"))
def _fp8_linear_bias_residual_bf16_fake(
    input, weight, bias, alpha: float, residual
) -> None:
    _check_bias_linear_shapes(input, weight, bias, residual)


@torch.library.register_fake(add_op_namespace_prefix("fp8_linear_bias_gelu_bf16"))
def _fp8_linear_bias_gelu_bf16_fake(input, weight, bias, alpha: float, out) -> None:
    _check_bias_linear_shapes(input, weight, bias, out)


@torch.library.register_fake(add_op_namespace_prefix("fp8_blockwise_linear_bf16"))
def _fp8_blockwise_linear_bf16_fake(
    input: torch.Tensor,
    weight: torch.Tensor,
    input_scale: torch.Tensor,
    weight_scale: torch.Tensor,
    out: torch.Tensor,
) -> None:
    if input.dim() != 2 or weight.dim() != 2:
        raise RuntimeError("input and weight must be rank-2 tensors")
    m, k = input.shape
    n = weight.shape[0]
    if weight.shape[1] != k or n % 128 or k % 128:
        raise RuntimeError("weight shape is invalid or N/K are not divisible by 128")
    if input_scale.shape != (m, k // 128):
        raise RuntimeError("input_scale must have shape (M, K / 128)")
    if weight_scale.shape != (n // 128, k // 128):
        raise RuntimeError("weight_scale must have shape (N / 128, K / 128)")
    if out.shape != (m, n):
        raise RuntimeError("out must have shape (M, N)")
    return None


@torch.library.register_fake(
    add_op_namespace_prefix("fp8_blockwise_swiglu_quantize_fp8")
)
def _fp8_blockwise_swiglu_quantize_fp8_fake(
    input: torch.Tensor,
    gate_up_weight: torch.Tensor,
    input_scale: torch.Tensor,
    gate_up_weight_scale: torch.Tensor,
    output: torch.Tensor,
    output_scale: torch.Tensor,
) -> None:
    m, k = input.shape
    if gate_up_weight.dim() != 2 or gate_up_weight.shape[0] % 2:
        raise RuntimeError("gate_up_weight must have shape (2*N, K)")
    n = gate_up_weight.shape[0] // 2
    if gate_up_weight.shape[1] != k or n % 128 or k % 128:
        raise RuntimeError("gate_up_weight shape is invalid or N/K are not divisible by 128")
    if input_scale.shape != (m, k // 128):
        raise RuntimeError("input_scale must have shape (M, K/128)")
    if gate_up_weight_scale.shape != (2 * n // 128, k // 128):
        raise RuntimeError("gate_up_weight_scale must have shape (2*N/128, K/128)")
    if output.shape != (m, n) or output_scale.shape != (m, n // 128):
        raise RuntimeError("output buffers have invalid shapes")
    return None


def select_fp8_linear_tile(m: int, n: int, k: int, variant: int = 0) -> str:
    """Return the FlashRT tile selected by the public dispatcher."""

    m = int(m)
    n = int(n)
    k = int(k)
    variant = int(variant)
    if m <= 0 or n <= 0 or k <= 0:
        raise RuntimeError("m, n, and k must be positive")
    if k % 16 != 0:
        raise RuntimeError("k must be divisible by 16")
    capability = torch.cuda.get_device_capability() if torch.cuda.is_available() else None
    if capability == (11, 0):
        forced = {1: "sm110_sq_bf16", 2: "sm110_t1_bf16", 3: "sm110_wide_bf16"}
        if variant not in {0, *forced}:
            raise RuntimeError("SM110 variant must be 0 (auto), 1 (Sq), 2 (T1), or 3 (Wide)")
        if n % 16 or k % 16:
            raise RuntimeError("SM110 requires n and k divisible by 16")
        if variant:
            return forced[variant]
        if m >= 512 and k == 2048 and 2048 <= n <= 2560:
            return "sm110_sq_bf16"
        if m >= 512 and n >= 16 * k:
            return "sm110_t1_bf16"
        if m >= 512 and k >= 4 * n:
            return "sm110_wide_bf16"
        if n >= 8 * k:
            return "sm110_wide_bf16"
        if m >= 128 and k >= 4 * n:
            return "sm110_sq_bf16"
        if n == k and m >= 512:
            return "sm110_sq_bf16" if k <= 1024 else "sm110_wide_bf16"
        if n == k and m >= 128:
            return "sm110_wide_bf16"
        return "sm110_t1_bf16"
    if m == 1:
        if k % 32:
            raise RuntimeError("SM120 requires k divisible by 32")
        if variant == 4:
            return "gemv_fp8_m1_w4"
        if variant == 8:
            return "gemv_fp8_m1_w8"
        if variant == 16:
            return "gemv_fp8_m1_w16"
        if variant != 0:
            raise RuntimeError("M=1 variant must be 0, 4, 8, or 16")
        if n <= 2048:
            return "gemv_fp8_m1_w4"
        if n <= 8192:
            return "gemv_fp8_m1_w8"
        return "gemv_fp8_m1_w16"
    if variant != 0:
        raise RuntimeError("small-M dispatcher currently supports variant=0 only")
    if k % 32:
        raise RuntimeError("SM120 requires k divisible by 32")
    if m <= 16:
        if k % 256 == 0:
            return "ld_fp8_gemm_16x128x256_w4" if n % 128 == 0 else "ld_fp8_gemm_16x64x256_w4"
        if n % 256 == 0:
            return "ld_fp8_gemm_16x256x128_w8"
        if n % 192 == 0:
            return "ld_fp8_gemm_16x192x128_w4"
        if n % 128 == 0:
            return "ld_fp8_gemm_16x128x128_w4"
        return "ld_fp8_gemm_16x64x128_w4"
    if m <= 32:
        if k % 256 == 0:
            return "ld_fp8_gemm_32x128x256_w4" if n % 128 == 0 else "ld_fp8_gemm_32x64x256_w4"
        if n % 192 == 0:
            return "ld_fp8_gemm_32x192x128_w4"
        if n % 128 == 0:
            return "ld_fp8_gemm_32x128x128_w4"
        return "ld_fp8_gemm_32x64x128_w4"
    if m <= 64:
        if k % 256 == 0:
            return "ld_fp8_gemm_64x128x256_w4" if n % 128 == 0 else "ld_fp8_gemm_64x64x256_w4"
        if n % 128 == 0:
            return "ld_fp8_gemm_64x128x128_w4"
        return "ld_fp8_gemm_64x64x128_w4"
    raise RuntimeError("only M=1 decode or 2 <= M <= 64 small-M rows are supported")


def fp8_linear_bf16(
    input: torch.Tensor,
    weight: torch.Tensor,
    alpha: float = 1.0,
    out: torch.Tensor | None = None,
    variant: int = 0,
) -> torch.Tensor:
    """Compute ``(input @ weight.T) * alpha`` with BF16 output.

    ``input`` and ``weight`` must be FP8 E4M3 CUDA tensors with shapes
    ``(M, K)`` and ``(N, K)``. ``alpha`` is a host float, normally the product
    of static per-tensor input and weight scales. SM110 uses the production
    CUTLASS Sq/T1/Wide dispatcher over full model row counts; SM120 uses the
    hand-tuned M<=64 path.
    """

    if out is None:
        out = torch.empty(
            (input.shape[0], weight.shape[0]),
            device=input.device,
            dtype=torch.bfloat16,
        )
    ops.fp8_linear_bf16(input, weight, float(alpha), int(variant), out)
    return out


def fp8_linear_residual_bf16(
    input: torch.Tensor,
    weight: torch.Tensor,
    residual: torch.Tensor,
    alpha: float = 1.0,
    variant: int = 0,
) -> torch.Tensor:
    """In-place ``residual += (input @ weight.T) * alpha`` for M=1 decode."""

    ops.fp8_linear_residual_bf16(input, weight, float(alpha), int(variant), residual)
    return residual


def fp8_linear_bias_bf16(
    input: torch.Tensor,
    weight: torch.Tensor,
    bias: torch.Tensor,
    alpha: float = 1.0,
    out: torch.Tensor | None = None,
) -> torch.Tensor:
    """SM110 FP8 linear with fused BF16 bias and BF16 output."""
    if out is None:
        out = torch.empty(
            (input.shape[0], weight.shape[0]),
            device=input.device,
            dtype=torch.bfloat16,
        )
    ops.fp8_linear_bias_bf16(input, weight, bias, float(alpha), out)
    return out


def fp8_linear_bias_residual_bf16(
    input: torch.Tensor,
    weight: torch.Tensor,
    bias: torch.Tensor,
    residual: torch.Tensor,
    alpha: float = 1.0,
) -> torch.Tensor:
    """SM110 fused ``residual += alpha * input @ weight.T + bias``."""
    ops.fp8_linear_bias_residual_bf16(
        input, weight, bias, float(alpha), residual
    )
    return residual


def fp8_linear_bias_gelu_bf16(
    input: torch.Tensor,
    weight: torch.Tensor,
    bias: torch.Tensor,
    alpha: float = 1.0,
    out: torch.Tensor | None = None,
) -> torch.Tensor:
    """SM110 FP8 linear with fused BF16 bias and GELU epilogue."""
    if out is None:
        out = torch.empty(
            (input.shape[0], weight.shape[0]),
            device=input.device,
            dtype=torch.bfloat16,
        )
    ops.fp8_linear_bias_gelu_bf16(input, weight, bias, float(alpha), out)
    return out


def fp8_blockwise_linear_bf16(
    input: torch.Tensor,
    weight: torch.Tensor,
    input_scale: torch.Tensor,
    weight_scale: torch.Tensor,
    out: torch.Tensor | None = None,
) -> torch.Tensor:
    """Block-128 scaled FP8 linear with BF16 output on SM89/SM120."""

    if out is None:
        out = torch.empty(
            (input.shape[0], weight.shape[0]),
            device=input.device,
            dtype=torch.bfloat16,
        )
    ops.fp8_blockwise_linear_bf16(
        input, weight, input_scale, weight_scale, out
    )
    return out


def fp8_blockwise_swiglu_quantize_fp8(
    input: torch.Tensor,
    gate_up_weight: torch.Tensor,
    input_scale: torch.Tensor,
    gate_up_weight_scale: torch.Tensor,
    *,
    output: torch.Tensor | None = None,
    output_scale: torch.Tensor | None = None,
) -> tuple[torch.Tensor, torch.Tensor]:
    """SM89 block-128 FP8 gate/up GEMM + SiLU + FP8 requant producer."""

    n = gate_up_weight.shape[0] // 2
    if output is None:
        output = torch.empty(
            (input.shape[0], n), device=input.device, dtype=torch.float8_e4m3fn
        )
    if output_scale is None:
        output_scale = torch.empty(
            (input.shape[0], n // 128), device=input.device, dtype=torch.float32
        )
    ops.fp8_blockwise_swiglu_quantize_fp8(
        input, gate_up_weight, input_scale, gate_up_weight_scale,
        output, output_scale
    )
    return output, output_scale


__all__ = [
    "fp8_linear_bf16",
    "fp8_linear_residual_bf16",
    "fp8_linear_bias_bf16",
    "fp8_linear_bias_residual_bf16",
    "fp8_linear_bias_gelu_bf16",
    "fp8_blockwise_linear_bf16",
    "fp8_blockwise_swiglu_quantize_fp8",
    "select_fp8_linear_tile",
]