File size: 8,651 Bytes
2fee457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch

from kernels.benchmark import Benchmark


_original_allclose = torch.allclose


def _fp8_dtype() -> torch.dtype:
    if torch.version.hip is not None and hasattr(torch, "float8_e4m3fnuz"):
        return torch.float8_e4m3fnuz
    return torch.float8_e4m3fn


def _fp8_max() -> float:
    return 240.0 if torch.version.hip is not None else 448.0


def _is_fp8_dtype(dtype: torch.dtype) -> bool:
    return dtype == torch.float8_e4m3fn or (
        hasattr(torch, "float8_e4m3fnuz") and dtype == torch.float8_e4m3fnuz
    )


def _flashrt_allclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False):
    if _is_fp8_dtype(input.dtype) or _is_fp8_dtype(other.dtype):
        return _original_allclose(
            input.float(),
            other.float(),
            rtol=0,
            atol=0,
            equal_nan=equal_nan,
        )
    return _original_allclose(input, other, rtol=rtol, atol=atol, equal_nan=equal_nan)


torch.allclose = _flashrt_allclose


QUANT_SHAPES = [
    ("decode_m1", 1, 4096),
    ("decode_m2", 2, 4096),
    ("decode_m4", 4, 4096),
    ("decode_m8", 8, 4096),
    ("small_m16", 16, 4096),
    ("small_m32", 32, 4096),
    ("prefill_m64", 64, 4096),
    ("prefill_m128", 128, 4096),
    ("prefill_m256", 256, 4096),
    ("wide_n8192_m16", 16, 8192),
    ("wide_n8192_m128", 128, 8192),
    ("vla_n12288_m16", 16, 12288),
    ("vla_n12288_m64", 64, 12288),
    ("vla_n16384_m16", 16, 16384),
    ("vla_n16384_m64", 64, 16384),
]


class BiasGeluFp8QuantizeBenchmark(Benchmark):
    seed = 1

    def _setup_shape(self, m: int, n: int) -> None:
        self.input = torch.randn((m, n), device=self.device, dtype=torch.bfloat16)
        self.bias = torch.randn((n,), device=self.device, dtype=torch.bfloat16)
        self.scale = torch.tensor([0.25], device=self.device, dtype=torch.float32)
        self.out = torch.empty_like(self.input, dtype=_fp8_dtype())

    def _reference(self) -> torch.Tensor:
        y = self.input.float() + self.bias.float()
        y = torch.nn.functional.gelu(y, approximate="tanh")
        limit = _fp8_max()
        y = torch.clamp(y / self.scale.float(), -limit, limit)
        return y.to(_fp8_dtype())

    def setup_decode_m1(self) -> None:
        self._setup_shape(1, 4096)

    def benchmark_decode_m1(self) -> None:
        self.kernel.bias_gelu_quantize_fp8_static_bf16(
            self.input, self.bias, self.scale, out=self.out
        )

    def verify_decode_m1(self) -> torch.Tensor:
        return self._reference()

    def setup_decode_m8(self) -> None:
        self._setup_shape(8, 4096)

    def benchmark_decode_m8(self) -> None:
        self.kernel.bias_gelu_quantize_fp8_static_bf16(
            self.input, self.bias, self.scale, out=self.out
        )

    def verify_decode_m8(self) -> torch.Tensor:
        return self._reference()

    def setup_small_m16(self) -> None:
        self._setup_shape(16, 4096)

    def benchmark_small_m16(self) -> None:
        self.kernel.bias_gelu_quantize_fp8_static_bf16(
            self.input, self.bias, self.scale, out=self.out
        )

    def verify_small_m16(self) -> torch.Tensor:
        return self._reference()

    def setup_prefill_m64(self) -> None:
        self._setup_shape(64, 4096)

    def benchmark_prefill_m64(self) -> None:
        self.kernel.bias_gelu_quantize_fp8_static_bf16(
            self.input, self.bias, self.scale, out=self.out
        )

    def verify_prefill_m64(self) -> torch.Tensor:
        return self._reference()

    def setup_prefill_m128(self) -> None:
        self._setup_shape(128, 4096)

    def benchmark_prefill_m128(self) -> None:
        self.kernel.bias_gelu_quantize_fp8_static_bf16(
            self.input, self.bias, self.scale, out=self.out
        )

    def verify_prefill_m128(self) -> torch.Tensor:
        return self._reference()

    def setup_wide_n8192_m16(self) -> None:
        self._setup_shape(16, 8192)

    def benchmark_wide_n8192_m16(self) -> None:
        self.kernel.bias_gelu_quantize_fp8_static_bf16(
            self.input, self.bias, self.scale, out=self.out
        )

    def verify_wide_n8192_m16(self) -> torch.Tensor:
        return self._reference()

    def setup_wide_n8192_m128(self) -> None:
        self._setup_shape(128, 8192)

    def benchmark_wide_n8192_m128(self) -> None:
        self.kernel.bias_gelu_quantize_fp8_static_bf16(
            self.input, self.bias, self.scale, out=self.out
        )

    def verify_wide_n8192_m128(self) -> torch.Tensor:
        return self._reference()


def _register_bias_gelu_shapes() -> None:
    for label, m, n in QUANT_SHAPES:

        def setup(self, m=m, n=n) -> None:
            self._setup_shape(m, n)

        def benchmark(self) -> None:
            self.kernel.bias_gelu_quantize_fp8_static_bf16(
                self.input, self.bias, self.scale, out=self.out
            )

        def verify(self) -> torch.Tensor:
            return self._reference()

        setattr(BiasGeluFp8QuantizeBenchmark, f"setup_{label}", setup)
        setattr(BiasGeluFp8QuantizeBenchmark, f"benchmark_{label}", benchmark)
        setattr(BiasGeluFp8QuantizeBenchmark, f"verify_{label}", verify)


class GeluFp8QuantizeBenchmark(Benchmark):
    seed = 3

    def _setup_shape(self, m: int, n: int) -> None:
        self.input = torch.randn((m, n), device=self.device, dtype=torch.bfloat16)
        self.scale = torch.tensor([0.25], device=self.device, dtype=torch.float32)
        self.out = torch.empty_like(self.input, dtype=_fp8_dtype())

    def _reference(self) -> torch.Tensor:
        y = torch.nn.functional.gelu(self.input.float(), approximate="tanh")
        limit = _fp8_max()
        y = torch.clamp(y / self.scale.float(), -limit, limit)
        return y.to(_fp8_dtype())

    def setup_decode_m1(self) -> None:
        self._setup_shape(1, 4096)

    def benchmark_decode_m1(self) -> None:
        self.kernel.gelu_quantize_fp8_static_bf16(
            self.input, self.scale, out=self.out
        )

    def verify_decode_m1(self) -> torch.Tensor:
        return self._reference()

    def setup_decode_m8(self) -> None:
        self._setup_shape(8, 4096)

    def benchmark_decode_m8(self) -> None:
        self.kernel.gelu_quantize_fp8_static_bf16(
            self.input, self.scale, out=self.out
        )

    def verify_decode_m8(self) -> torch.Tensor:
        return self._reference()

    def setup_small_m16(self) -> None:
        self._setup_shape(16, 4096)

    def benchmark_small_m16(self) -> None:
        self.kernel.gelu_quantize_fp8_static_bf16(
            self.input, self.scale, out=self.out
        )

    def verify_small_m16(self) -> torch.Tensor:
        return self._reference()

    def setup_prefill_m64(self) -> None:
        self._setup_shape(64, 4096)

    def benchmark_prefill_m64(self) -> None:
        self.kernel.gelu_quantize_fp8_static_bf16(
            self.input, self.scale, out=self.out
        )

    def verify_prefill_m64(self) -> torch.Tensor:
        return self._reference()

    def setup_prefill_m128(self) -> None:
        self._setup_shape(128, 4096)

    def benchmark_prefill_m128(self) -> None:
        self.kernel.gelu_quantize_fp8_static_bf16(
            self.input, self.scale, out=self.out
        )

    def verify_prefill_m128(self) -> torch.Tensor:
        return self._reference()

    def setup_wide_n8192_m16(self) -> None:
        self._setup_shape(16, 8192)

    def benchmark_wide_n8192_m16(self) -> None:
        self.kernel.gelu_quantize_fp8_static_bf16(
            self.input, self.scale, out=self.out
        )

    def verify_wide_n8192_m16(self) -> torch.Tensor:
        return self._reference()

    def setup_wide_n8192_m128(self) -> None:
        self._setup_shape(128, 8192)

    def benchmark_wide_n8192_m128(self) -> None:
        self.kernel.gelu_quantize_fp8_static_bf16(
            self.input, self.scale, out=self.out
        )

    def verify_wide_n8192_m128(self) -> torch.Tensor:
        return self._reference()


def _register_gelu_shapes() -> None:
    for label, m, n in QUANT_SHAPES:

        def setup(self, m=m, n=n) -> None:
            self._setup_shape(m, n)

        def benchmark(self) -> None:
            self.kernel.gelu_quantize_fp8_static_bf16(
                self.input, self.scale, out=self.out
            )

        def verify(self) -> torch.Tensor:
            return self._reference()

        setattr(GeluFp8QuantizeBenchmark, f"setup_{label}", setup)
        setattr(GeluFp8QuantizeBenchmark, f"benchmark_{label}", benchmark)
        setattr(GeluFp8QuantizeBenchmark, f"verify_{label}", verify)


_register_bias_gelu_shapes()
_register_gelu_shapes()