forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fp8.py
309 lines (256 loc) · 12.5 KB
/
test_fp8.py
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
# Owner(s): ["module: inductor"]
import functools
import unittest
import torch
from torch import Tensor
from torch._inductor import utils
from torch._inductor.test_case import run_tests, TestCase
from torch.testing._internal.common_cuda import SM90OrLater
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
TEST_WITH_ROCM,
)
from torch.testing._internal.inductor_utils import HAS_CUDA
torch.set_float32_matmul_precision("high")
# define the e4m3/e5m2 constants
E4M3_MAX_POS = 448.0
E5M2_MAX_POS = 57344.0
def _to_fp8_saturated(x: Tensor, float8_dtype: torch.dtype) -> Tensor:
# The default behavior in PyTorch for casting to `float8_e4m3fn`
# and `e5m2` is to not saturate. In this context, we should saturate.
# A common case where we want to saturate is when the history of a
# tensor has a maximum value of `amax1`, and the current amax value
# is `amax2`, where `amax1 < amax2`. This is common when using delayed
# scaling.
if float8_dtype == torch.float8_e4m3fn:
x = x.clamp(min=-1 * E4M3_MAX_POS, max=E4M3_MAX_POS)
else:
x = x.clamp(min=-1 * E5M2_MAX_POS, max=E5M2_MAX_POS)
return x.to(float8_dtype)
@instantiate_parametrized_tests
class TestFP8Types(TestCase):
@unittest.skipIf(TEST_WITH_ROCM, "FP8 is not supported on ROCM")
@unittest.skipIf(not SM90OrLater, "FP8 is only supported on H100+")
@parametrize("dtype", (torch.float16, torch.bfloat16))
def test_eager_fallback(self, dtype: torch.dtype):
weight_shape = (32, 16)
def fp8_matmul_unwrapped(x):
a_scale = torch.Tensor([1.0]).to(device="cuda")
b_scale = torch.Tensor([1.0]).to(device="cuda")
output_scale = None
input_bias = torch.rand(32, device="cuda", dtype=dtype)
weight = torch.rand(*weight_shape, device="cuda", dtype=dtype).T.to(
torch.float8_e4m3fn
)
a_inverse_scale = 1 / a_scale
b_inverse_scale = 1 / b_scale
output, updated_amax = torch._scaled_mm(
x,
weight,
bias=input_bias,
out_dtype=dtype,
scale_a=a_inverse_scale,
scale_b=b_inverse_scale,
scale_result=output_scale,
)
return output
compiled_fp8_matmul = torch.compile(
fp8_matmul_unwrapped, backend="inductor", dynamic=True
)
x_shape = (16, 16)
x = torch.rand(*x_shape, device="cuda", dtype=dtype).to(torch.float8_e4m3fn)
y_fp8 = compiled_fp8_matmul(x)
x_shape = (15, 16)
x = torch.rand(*x_shape, device="cuda", dtype=dtype).to(torch.float8_e4m3fn)
y_fp8 = compiled_fp8_matmul(x)
@unittest.skipIf(TEST_WITH_ROCM, "FP8 is not supported on ROCM")
@unittest.skipIf(not SM90OrLater, "FP8 is only supported on H100+")
@parametrize("dtype", (torch.float16, torch.bfloat16, torch.float))
@parametrize("shape", ("15,3,13", "4,2048,4096"))
def test_valid_cast(self, dtype: torch.dtype, shape: str):
def fp8_cast(x):
y0 = x.to(dtype=torch.float8_e4m3fn).to(dtype)
y1 = x.to(dtype=torch.float8_e5m2).to(dtype)
return y0, y1
compiled_fp8_cast = torch.compile(fp8_cast, backend="inductor", dynamic=True)
shape = [int(dim) for dim in shape.split(",")]
x = torch.rand(*shape, device="cuda", dtype=dtype)
y0_fp8, y1_fp8 = compiled_fp8_cast(x)
torch.testing.assert_close(y0_fp8, x, rtol=5e-1, atol=5e-1)
torch.testing.assert_close(y1_fp8, x, rtol=5e-1, atol=5e-1)
@unittest.skipIf(TEST_WITH_ROCM, "FP8 is not supported on ROCM")
@unittest.skipIf(not SM90OrLater, "FP8 is only supported on H100+")
def test_bad_cast(self):
def fp8_cast(x, dtype):
return x.to(dtype=dtype)
compiled_fp8_cast = torch.compile(fp8_cast, backend="inductor", dynamic=True)
x_shape = (16, 16, 16)
with self.assertRaisesRegex(
torch._dynamo.exc.BackendCompilerFailed,
"Conversions between float8_e5m2 and float8_e4m3fn is not supported!",
):
x = torch.rand(*x_shape, device="cuda").to(dtype=torch.float8_e4m3fn)
y = compiled_fp8_cast(x, torch.float8_e5m2)
with self.assertRaisesRegex(
torch._dynamo.exc.BackendCompilerFailed,
"Conversions between float8_e5m2 and float8_e4m3fn is not supported!",
):
x = torch.rand(*x_shape, device="cuda").to(dtype=torch.float8_e5m2)
y = compiled_fp8_cast(x, torch.float8_e4m3fn)
@unittest.skipIf(TEST_WITH_ROCM, "FP8 is not supported on ROCM")
@unittest.skipIf(not SM90OrLater, "FP8 is only supported on H100+")
@parametrize("src_dtype", (torch.float16, torch.bfloat16, torch.float))
@parametrize("dst_dtype", (torch.float8_e4m3fn, torch.float8_e5m2))
@parametrize("shape", ("16,16,16", "4,2048,4096"))
def test_to_fp8_saturated(
self, src_dtype: torch.dtype, dst_dtype: torch.dtype, shape: str
):
def fp8_saturated(x, dtype):
return _to_fp8_saturated(x, dtype)
compiled_fp8_cast = torch.compile(
fp8_saturated, backend="inductor", dynamic=True
)
shape = [int(dim) for dim in shape.split(",")]
x = torch.rand(*shape, device="cuda", dtype=src_dtype)
y_compiled = compiled_fp8_cast(x, dst_dtype)
y = fp8_saturated(x, dst_dtype)
torch.testing.assert_close(y_compiled.half(), y.half(), rtol=5e-1, atol=5e-1)
@unittest.skipIf(TEST_WITH_ROCM, "FP8 is not supported on ROCM")
@unittest.skipIf(not SM90OrLater, "FP8 is only supported on H100+")
@parametrize("float8_dtype", (torch.float8_e4m3fn, torch.float8_e5m2))
@parametrize("shape", ("1,1,15", "1,10,15", "1,10,512", "1,10,4096", "4,2048,4096"))
def test_amax_fp8_quant(self, float8_dtype: torch.dtype, shape: str):
shape = [int(dim) for dim in shape.split(",")]
batch_size, sequence_length, hidden_size = shape
def amax_fp8(x: Tensor, scale: Tensor):
y = torch.amax(torch.abs(x))
y_scaled = y.to(dtype=torch.float) * scale
bits_fp8 = _to_fp8_saturated(y_scaled, float8_dtype)
return bits_fp8
compiled_amax_fp8_quant = torch.compile(amax_fp8, backend="inductor")
x_shape = (batch_size, sequence_length, hidden_size)
x = torch.rand(*x_shape, device="cuda", dtype=torch.half)
scale = torch.tensor(0.2, device="cuda", dtype=torch.float)
y_compiled = compiled_amax_fp8_quant(x, scale)
y = amax_fp8(x, scale)
torch.testing.assert_close(y_compiled.half(), y.half(), rtol=1e-2, atol=1e-2)
@unittest.skipIf(TEST_WITH_ROCM, "FP8 is not supported on ROCM")
@unittest.skipIf(not SM90OrLater, "FP8 is only supported on H100+")
@parametrize("float8_dtype", (torch.float8_e4m3fn, torch.float8_e5m2))
@parametrize("shape", ("1,1,15", "1,10,15", "1,10,512", "1,10,4096", "4,2048,4096"))
def test_amax_along_with_fp8_quant(self, float8_dtype: torch.dtype, shape: str):
shape = [int(dim) for dim in shape.split(",")]
batch_size, sequence_length, hidden_size = shape
def amax_fp8(x: Tensor, scale: Tensor, amax_buffer: Tensor):
amax_buffer.fill_(torch.amax(torch.abs(x)))
x_scaled = x.to(dtype=torch.float) * scale
bits_fp8 = _to_fp8_saturated(x_scaled, float8_dtype)
return bits_fp8
compiled_amax_fp8_quant = torch.compile(amax_fp8, backend="inductor")
x_shape = (batch_size, sequence_length, hidden_size)
x = torch.rand(*x_shape, device="cuda", dtype=torch.half)
scale = torch.tensor(1.0, device="cuda", dtype=torch.float)
amax_buffer_compiled = torch.zeros((1), device="cuda", dtype=torch.half)
y_compiled = compiled_amax_fp8_quant(x, scale, amax_buffer_compiled)
amax_buffer = torch.zeros((1), device="cuda", dtype=torch.half)
y = amax_fp8(x, scale, amax_buffer)
torch.testing.assert_close(y_compiled.half(), y.half(), rtol=1e-1, atol=1e-1)
torch.testing.assert_close(
amax_buffer_compiled, amax_buffer, rtol=1e-2, atol=1e-2
)
@unittest.skipIf(TEST_WITH_ROCM, "FP8 is not supported on ROCM")
@unittest.skipIf(not SM90OrLater, "FP8 is only supported on H100+")
@parametrize("float8_dtype", (torch.float8_e4m3fn, torch.float8_e5m2))
@parametrize("amax_keep_dim", (True, False))
@parametrize("shape", ("1,1,15", "1,10,15", "1,10,512", "1,10,4096", "4,2048,4096"))
def test_layernorm_fp8_quant(
self, float8_dtype: torch.dtype, amax_keep_dim: bool, shape: str
):
shape = [int(dim) for dim in shape.split(",")]
batch_size, sequence_length, hidden_size = shape
def ln_fp8(x: Tensor, scale: Tensor, amax_buffer: Tensor):
x = torch.nn.functional.layer_norm(
x.to(dtype=torch.float),
[hidden_size],
weight=None,
bias=None,
eps=1e-05,
)
amax_buffer.fill_(
torch.amax(torch.abs(x), keepdim=amax_keep_dim).reshape(-1)[0]
)
x_scaled = x * scale
bits_fp8 = _to_fp8_saturated(x_scaled, float8_dtype)
return bits_fp8
compiled_ln_fp8_quant = torch.compile(ln_fp8, backend="inductor")
x_shape = (batch_size, sequence_length, hidden_size)
x = torch.rand(*x_shape, device="cuda", dtype=torch.half)
scale = torch.tensor(0.2, device="cuda", dtype=torch.float)
amax_buffer_compiled = torch.zeros((1), device="cuda", dtype=torch.half)
y_compiled = compiled_ln_fp8_quant(x, scale, amax_buffer_compiled)
amax_buffer = torch.zeros((1), device="cuda", dtype=torch.half)
y = ln_fp8(x, scale, amax_buffer)
torch.testing.assert_close(y_compiled.half(), y.half(), rtol=1e-1, atol=1e-1)
torch.testing.assert_close(
amax_buffer_compiled, amax_buffer, rtol=1e-2, atol=1e-2
)
@unittest.skipIf(TEST_WITH_ROCM, "FP8 is not supported on ROCM")
@unittest.skipIf(not SM90OrLater, "FP8 is only supported on H100+")
@parametrize("float8_dtype", (torch.float8_e4m3fn, torch.float8_e5m2))
@parametrize("shape", ("4,2048,4096",))
@parametrize("keepdim", (False, True))
def test_layernorm_fp8_quant_benchmark(
self,
float8_dtype: torch.dtype,
shape: str,
keepdim: bool,
):
shape = [int(dim) for dim in shape.split(",")]
batch_size, sequence_length, hidden_size = shape
def ln(x: Tensor):
x = torch.nn.functional.layer_norm(
x.to(dtype=torch.float),
[hidden_size],
weight=None,
bias=None,
eps=1e-05,
)
return x
def ln_fp8(x: Tensor, scale: Tensor, amax_buffer: Tensor):
x = torch.nn.functional.layer_norm(
x.to(dtype=torch.float),
[hidden_size],
weight=None,
bias=None,
eps=1e-05,
)
amax = torch.amax(torch.abs(x), keepdim=keepdim)
amax_buffer.view_as(amax).copy_(amax)
x_scaled = x * scale
bits_fp8 = _to_fp8_saturated(x_scaled, float8_dtype)
return bits_fp8
compiled_ln_fp8_quant = torch.compile(ln_fp8, backend="inductor")
x_shape = (batch_size, sequence_length, hidden_size)
x = torch.rand(*x_shape, device="cuda", dtype=torch.half)
scale = torch.tensor(0.2, device="cuda", dtype=torch.float)
amax_buffer_compiled = torch.zeros((1), device="cuda", dtype=torch.half)
amax_buffer = torch.zeros((1), device="cuda", dtype=torch.half)
_ = compiled_ln_fp8_quant(x, scale, amax_buffer_compiled)
compiled_latency = utils.do_bench_using_profiling(
functools.partial(compiled_ln_fp8_quant, x, scale, amax_buffer_compiled)
)
eager_latency = utils.do_bench_using_profiling(
functools.partial(ln_fp8, x, scale, amax_buffer)
)
compiled_ln = torch.compile(ln, backend="inductor")
_ = compiled_ln(x)
ln_latency = utils.do_bench_using_profiling(functools.partial(compiled_ln, x))
print(
f"Config: {float8_dtype=}, {shape=}, {keepdim=}. "
f"Benchmark results: Inductor: {compiled_latency}ms, Eager: {eager_latency}ms, "
f"LN only Inductor: {ln_latency}ms."
)
if __name__ == "__main__":
if HAS_CUDA:
run_tests()