forked from ROCm/aiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rmsnorm2dFusedAddQuant.py
More file actions
376 lines (358 loc) · 11.9 KB
/
Copy pathtest_rmsnorm2dFusedAddQuant.py
File metadata and controls
376 lines (358 loc) · 11.9 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# SPDX-License-Identifier: MIT
# Copyright (C) 2024-2026, Advanced Micro Devices, Inc. All rights reserved.
import torch
import torch.nn.functional as F
import aiter
import argparse
from aiter.test_common import checkAllclose, perftest, benchmark
from aiter import dtypes, QuantType, get_torch_quant, get_gfx
from aiter.utility import fp4_utils
from functools import partial
import pandas as pd
torch.set_default_device("cuda")
@perftest(num_warmup=0, num_iters=10)
def run_torch(
input,
weight,
eps,
residual=None,
x_scale=None,
q_dtype=None,
quant_type=QuantType.per_Token,
):
quant_func = get_torch_quant(quant_type)
if residual is None:
residual_out = None
output = F.rms_norm(
input=input, normalized_shape=(input.shape[-1],), weight=weight, eps=eps
)
else:
residual_out = input + residual
output = F.rms_norm(
input=residual_out,
normalized_shape=(input.shape[-1],),
weight=weight,
eps=eps,
)
if q_dtype is None:
y_scale = None
output_q = output
else:
if x_scale is None:
output_q, y_scale = quant_func(output, quant_dtype=q_dtype)
else:
output_q, y_scale = quant_func(output, x_scale=x_scale, quant_dtype=q_dtype)
return output_q, residual_out, y_scale, output
@perftest()
def run_ck(
input,
weight,
eps,
residual=None,
x_scale=None,
q_dtype=None,
quant_type=QuantType.No,
model_sensitive=0,
):
out_before_quant = None
if quant_type == QuantType.No:
y_scale = None
if residual is None:
residual_out = None
output = aiter.rmsnorm2d_fwd_ck(input, weight, eps)
elif residual is not None:
residual_out = torch.empty_like(input)
output = torch.empty_like(input)
aiter.rmsnorm2d_fwd_with_add_ck(
output, input, residual, residual_out, weight, eps
)
elif x_scale is None:
y_scale = torch.empty(input.shape[0], 1, dtype=dtypes.fp32)
output = torch.empty(input.shape, dtype=q_dtype)
if residual is None:
residual_out = None
aiter.rmsnorm2d_fwd_with_dynamicquant_ck(
output, input, y_scale, weight, eps, model_sensitive
)
elif residual is not None:
residual_out = torch.empty_like(input)
aiter.rmsnorm2d_fwd_with_add_dynamicquant_ck(
output,
input,
residual,
residual_out,
y_scale,
weight,
eps,
model_sensitive,
)
else:
y_scale = torch.empty(input.shape[0], 1, dtype=dtypes.fp32)
output = torch.empty(input.shape, dtype=q_dtype)
if residual is None:
residual_out = None
aiter.rmsnorm2d_fwd_with_smoothquant(
output, input, x_scale, y_scale, weight, eps, model_sensitive
)
elif residual is not None:
residual_out = torch.empty_like(input)
out_before_quant = torch.empty_like(input)
aiter.rmsnorm2d_fwd_with_add_smoothquant(
output,
input,
residual,
residual_out,
x_scale,
y_scale,
weight,
eps,
out_before_quant=out_before_quant,
)
return output, residual_out, y_scale, out_before_quant
@perftest()
def run_hip(
input,
weight,
eps,
residual,
q_dtype=None,
quant_type=QuantType.No,
):
if quant_type == QuantType.No:
group_size = 0
elif quant_type == QuantType.per_Token:
group_size = 0
scale_shape = (input.shape[0], 1)
elif quant_type == QuantType.per_1x32:
group_size = 32
elif quant_type == QuantType.per_1x128:
group_size = 128
else:
raise ValueError(f"Unsupported quant type: {quant_type}")
if quant_type in [QuantType.per_1x32, QuantType.per_1x128]:
group_per_row = (input.shape[1] + group_size - 1) // group_size
if q_dtype == dtypes.fp4x2:
scale_per_row = (group_per_row + 7) // 8 * 8 // 4
else:
scale_per_row = group_per_row
scale_shape = (input.shape[0], scale_per_row)
residual_out = torch.empty_like(input)
if quant_type == QuantType.No:
scale = None
output = torch.empty_like(input)
if residual is None:
residual_out = None
aiter.rmsnorm(output, input, weight, eps)
else:
residual_out = torch.empty_like(input)
aiter.add_rmsnorm(output, input, residual, residual_out, weight, eps)
else:
if q_dtype == dtypes.fp4x2:
output = torch.empty((input.shape[0], input.shape[1] // 2), dtype=q_dtype)
else:
output = torch.empty(input.shape, dtype=q_dtype)
scale = torch.empty(scale_shape, dtype=dtypes.fp32)
if residual is None:
residual_out = None
aiter.rmsnorm_quant(output, input, scale, weight, eps, group_size)
else:
residual_out = torch.empty_like(input)
aiter.add_rmsnorm_quant(
output, input, residual, residual_out, scale, weight, eps, group_size
)
return output, residual_out, scale, None
@benchmark()
def test_rmsnorm(
m,
n,
dtype=torch.bfloat16,
add_residual=False,
smoothquant=False,
quant_dtype=None,
quant_type=QuantType.No,
):
if quant_dtype is dtypes.fp4x2 and quant_type == QuantType.per_Token:
print("fp4x2 per token is not supported")
return {}
elif quant_type == QuantType.per_1x32 and (
quant_dtype is not dtypes.fp4x2 or get_gfx() not in ["gfx950"]
):
print("per_1x32 is only supported for fp4x2 on gfx950")
return {}
dim = (m, n)
scale_type = dtypes.fp32
input = torch.randn(dim, dtype=dtype)
weight = torch.randn(n, dtype=dtype)
res = torch.randn(dim, dtype=dtype) if add_residual else None
xscale = torch.randn(n, dtype=scale_type) if smoothquant else None
def calculateTensorsSize(*args):
num_btype = 0
for el in args:
if isinstance(el, torch.Tensor):
num_btype += el.element_size() * el.numel()
return num_btype
read_datasize = calculateTensorsSize(input, weight, res, xscale)
atol = 1 if quant_dtype == dtypes.i8 else 1e-2
ret = {}
(a, res_a, yscale_a, _), avg_a = run_torch(
input,
weight,
1e-5,
residual=res,
x_scale=xscale,
q_dtype=quant_dtype,
quant_type=quant_type,
)
write_datasize = calculateTensorsSize(a, res_a, yscale_a)
ret["torch us"] = avg_a
if quant_type in [QuantType.per_Token, QuantType.No] and quant_dtype in [
None,
dtypes.fp8,
dtypes.i8,
]:
(b, res_b, yscale_b, _), avg_b = run_ck(
input,
weight,
1e-5,
residual=res,
x_scale=xscale,
q_dtype=quant_dtype,
quant_type=quant_type,
)
err_ck = checkAllclose(
a.to(dtypes.fp32), b.to(dtypes.fp32), rtol=0, atol=atol, msg="check ck out"
)
if add_residual:
checkAllclose(res_a, res_b, msg="check ck res")
if quant_type != QuantType.No:
checkAllclose(yscale_a, yscale_b, msg="check ck scale")
ret["ck us"] = avg_b
ret["ck err"] = err_ck
ret["ck bw(GB/s)"] = (
(read_datasize + write_datasize) / avg_b / 1024 / 1024 / 1024 * 1e6
)
if not smoothquant and n <= 8192:
(c, res_c, yscale_c, _), avg_c = run_hip(
input, weight, 1e-5, res, q_dtype=quant_dtype, quant_type=quant_type
)
if quant_dtype == dtypes.fp4x2:
a = fp4_utils.mxfp4_to_f32(a)
c = fp4_utils.mxfp4_to_f32(c)
err_hip = checkAllclose(
a.to(dtypes.fp32), c.to(dtypes.fp32), rtol=0, atol=atol, msg="check hip out"
)
if add_residual:
checkAllclose(res_a, res_c, msg="check hip res")
if quant_type != QuantType.No:
checkAllclose(
yscale_a.view(torch.float32),
yscale_c.view(torch.float32),
msg="check hip scale",
)
ret["hip us"] = avg_c
ret["hip err"] = err_hip
ret["hip bw(GB/s)"] = (
(read_datasize + write_datasize) / avg_c / 1024 / 1024 / 1024 * 1e6
)
return ret
if __name__ == "__main__":
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
prog="test_rmsnorm2dFusedSQuant",
description="Test ck rmsnorm2d Fused add and SmoothQuant",
)
parser.add_argument(
"--mode",
type=int,
choices=[1, 2, 3, 4, 5, 6, 7, 8],
help="1: test_rmsnorm2d, \n2:test_rmsnorm2d_fuseAdd, \n"
+ "3:test_rmsnorm2d_fuseSmoothquant, \n4:test_rmsnorm2d_fuseAdd_Smoothquant"
+ "5:test_rmsnorm2d_fuseDynamicquant_per_Token, \n6:test_rmsnorm2d_fuseAdd_Dynamicquant_per_Token"
+ "7:test_rmsnorm2d_fuseAdd_fuseDynamicquant_per_1x128, \n8:test_rmsnorm2d_fuseAdd_fuseDynamicquant_per_1x32",
default=1,
)
parser.add_argument(
"-q",
"--quant_dtype",
type=dtypes.str2Dtype,
default=dtypes.d_dtypes["i8"],
# nargs="*",
choices=[
dtypes.d_dtypes["i8"],
dtypes.d_dtypes["fp8"],
dtypes.d_dtypes["fp4x2"],
],
help="""Quantization data types.
e.g.: --quant_dtype i8 fp8 fp4x2""",
)
parser.add_argument(
"-m",
type=int,
default=[8, 256, 256 * 8, 256 * 10, 32768],
nargs="*",
help="""M of mnk.
e.g.: -m 32""",
)
parser.add_argument(
"-n",
type=int,
default=[1024, 2048, 4096, 8192],
nargs="*",
help="""N of mnk.
e.g.: -n 1024""",
)
parser.add_argument(
"-d",
"--dtype",
type=dtypes.str2Dtype,
default=[dtypes.d_dtypes["bf16"]],
nargs="*",
choices=[dtypes.d_dtypes["bf16"], dtypes.d_dtypes["fp16"]],
)
args = parser.parse_args()
if args.mode == 1:
test_rmsnorm_func = partial(
test_rmsnorm, quant_type=QuantType.No, add_residual=False
)
elif args.mode == 2:
test_rmsnorm_func = partial(
test_rmsnorm, quant_type=QuantType.No, add_residual=True
)
elif args.mode == 3:
test_rmsnorm_func = partial(
test_rmsnorm, quant_type=QuantType.No, add_residual=False, smoothquant=True
)
elif args.mode == 4:
test_rmsnorm_func = partial(
test_rmsnorm, quant_type=QuantType.No, add_residual=True, smoothquant=True
)
elif args.mode == 5:
test_rmsnorm_func = partial(
test_rmsnorm, quant_type=QuantType.per_Token, add_residual=False
)
elif args.mode == 6:
test_rmsnorm_func = partial(
test_rmsnorm, quant_type=QuantType.per_Token, add_residual=True
)
elif args.mode == 7:
test_rmsnorm_func = partial(
test_rmsnorm, quant_type=QuantType.per_1x128, add_residual=True
)
elif args.mode == 8:
test_rmsnorm_func = partial(
test_rmsnorm, quant_type=QuantType.per_1x32, add_residual=True
)
df = []
for n in args.n:
for m in args.m:
for dtype in args.dtype:
ret = test_rmsnorm_func(
m,
n,
dtype=dtype,
quant_dtype=args.quant_dtype if args.mode not in [1, 2] else None,
)
df.append(ret)
df = pd.DataFrame(df)
df_md = df.to_markdown(index=False)
aiter.logger.info("rmsnorm2d summary (markdown):\n%s", df_md)