forked from ROCm/aiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_layernorm2dFusedAddQuant.py
More file actions
315 lines (278 loc) · 11 KB
/
Copy pathtest_layernorm2dFusedAddQuant.py
File metadata and controls
315 lines (278 loc) · 11 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
# 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
from aiter import dtypes
@perftest()
def run_torch(
input, weight, bias, eps, residual=None, x_scale=None, y_scale_dtype=None
):
if residual is None:
residual_out = None
output = F.layer_norm(
input=input,
normalized_shape=(input.shape[-1],),
weight=weight,
bias=bias,
eps=eps,
)
else:
residual_out = input + residual
output = F.layer_norm(
input=residual_out,
normalized_shape=(input.shape[-1],),
weight=weight,
bias=bias,
eps=eps,
)
if y_scale_dtype is None:
y_scale = None
else:
output, y_scale = aiter.pertoken_quant(output, x_scale=x_scale)
return output, residual_out, y_scale
@perftest()
def run_ck(input, weight, bias, eps, residual=None, x_scale=None, y_scale_dtype=None):
if y_scale_dtype is None:
y_scale = None
if residual is None:
residual_out = None
output = aiter.layer_norm(input, weight, bias, eps)
elif residual is not None:
residual_out = torch.empty_like(input)
output = torch.empty_like(input)
aiter.layernorm2d_fwd_with_add(
output, input, residual, residual_out, weight, bias, eps
)
elif x_scale is None:
y_scale = torch.empty(input.shape[0], 1, dtype=y_scale_dtype, device="cuda")
output = torch.empty(input.shape, dtype=dtypes.i8, device="cuda")
if residual is None:
residual_out = None
aiter.layernorm2d_fwd_with_dynamicquant(
output, input, y_scale, weight, bias, eps
)
elif residual is not None:
residual_out = torch.empty_like(input)
aiter.layernorm2d_fwd_with_add_dynamicquant(
output, input, residual, residual_out, y_scale, weight, bias, eps
)
else:
y_scale = torch.empty(input.shape[0], 1, dtype=y_scale_dtype, device="cuda")
output = torch.empty(input.shape, dtype=dtypes.i8, device="cuda")
if residual is None:
residual_out = None
aiter.layernorm2d_fwd_with_smoothquant(
output, input, x_scale, y_scale, weight, bias, eps
)
elif residual is not None:
residual_out = torch.empty_like(input)
aiter.layernorm2d_fwd_with_add_smoothquant(
output,
input,
residual,
residual_out,
x_scale,
y_scale,
weight,
bias,
eps,
)
return output, residual_out, y_scale
def test_layernorm2d_instance(dtype, m, n):
dim = (m, n)
input = torch.randn(dim, dtype=dtype, device="cuda")
weight = torch.randn(n, dtype=dtype, device="cuda")
bias = torch.randn(n, dtype=dtype, device="cuda")
(a, *_), avg_a = run_torch(input, weight, bias, 1e-5)
(b, *_), avg_b = run_ck(input, weight, bias, 1e-5)
print(
f"[perf] dim: {dim}, dtype: {dtype}, torch avg: {avg_a:<8.2f} us, ck avg: {avg_b:<8.2f} us, uplift: {avg_a/avg_b-1:<5.1%}"
)
checkAllclose(a, b)
print("[passed~]")
def test_layernorm2d_fuseAdd_instance(dtype, m, n):
dim = (m, n)
input = torch.randn(dim, dtype=dtype, device="cuda")
weight = torch.randn(n, dtype=dtype, device="cuda")
bias = torch.randn(n, dtype=dtype, device="cuda")
res = torch.randn(dim, dtype=dtype, device="cuda")
(a, res_a, *_), avg_a = run_torch(input, weight, bias, 1e-5, residual=res)
(b, res_b, *_), avg_b = run_ck(input, weight, bias, 1e-5, residual=res)
print(
f"[perf] dim: {dim}, dtype: {dtype}, torch avg: {avg_a:<8.2f} us, ck avg: {avg_b:<8.2f} us, uplift: {avg_a/avg_b-1:<5.1%}"
)
checkAllclose(a, b, rtol=1e-2, atol=1e-1)
checkAllclose(res_a, res_b)
print(" [passed~]")
def test_layernorm2d_fuseSmoothquant_instance(dtype, m, n, xscaleType, yscaleType):
dim = (m, n)
input = torch.randn(dim, dtype=dtype, device="cuda")
weight = torch.randn(n, dtype=dtype, device="cuda")
bias = torch.randn(n, dtype=dtype, device="cuda")
xscale = torch.randn(n, dtype=xscaleType, device="cuda")
(a, _, yscale_a), avg_a = run_torch(
input, weight, bias, 1e-5, x_scale=xscale, y_scale_dtype=yscaleType
)
(b, _, yscale_b), avg_b = run_ck(
input, weight, bias, 1e-5, x_scale=xscale, y_scale_dtype=yscaleType
)
print(
f"[perf] dim: {dim}, dtype: {dtype}, torch avg: {avg_a:<8.2f} us, ck avg: {avg_b:<8.2f} us, uplift: {avg_a/avg_b-1:<5.1%}"
)
checkAllclose(a, b, rtol=0, atol=1)
checkAllclose(yscale_a, yscale_b, rtol=1e-3, atol=1e-3)
print(" [passed~]")
def test_layernorm2d_fuseAdd_Smoothquant_instance(dtype, m, n, xscaleType, yscaleType):
dim = (m, n)
input = torch.randn(dim, dtype=dtype, device="cuda")
weight = torch.randn(n, dtype=dtype, device="cuda")
bias = torch.randn(n, dtype=dtype, device="cuda")
res = torch.randn(dim, dtype=dtype, device="cuda")
xscale = torch.randn(n, dtype=xscaleType, device="cuda")
(a, res_a, yscale_a), avg_a = run_torch(
input,
weight,
bias,
1e-5,
residual=res,
x_scale=xscale,
y_scale_dtype=yscaleType,
)
(b, res_b, yscale_b), avg_b = run_ck(
input,
weight,
bias,
1e-5,
residual=res,
x_scale=xscale,
y_scale_dtype=yscaleType,
)
print(
f"[perf] dim: {dim}, dtype: {dtype}, torch avg: {avg_a:<8.2f} us, ck avg: {avg_b:<8.2f} us, uplift: {avg_a/avg_b-1:<5.1%}"
)
checkAllclose(a, b, rtol=0, atol=1)
checkAllclose(res_a, res_b)
checkAllclose(yscale_a, yscale_b, rtol=1e-3, atol=1e-3)
print(" [passed~]")
def test_layernorm2d_fuseDynamicquant_instance(dtype, m, n, yscaleType):
dim = (m, n)
input = torch.randn(dim, dtype=dtype, device="cuda")
weight = torch.randn(n, dtype=dtype, device="cuda")
bias = torch.randn(n, dtype=dtype, device="cuda")
(a, _, yscale_a), avg_a = run_torch(
input, weight, bias, 1e-5, y_scale_dtype=yscaleType
)
(b, _, yscale_b), avg_b = run_ck(
input, weight, bias, 1e-5, y_scale_dtype=yscaleType
)
print(
f"[perf] dim: {dim}, dtype: {dtype}, torch avg: {avg_a:<8.2f} us, ck avg: {avg_b:<8.2f} us, uplift: {avg_a/avg_b-1:<5.1%}"
)
checkAllclose(a, b, rtol=0, atol=1)
checkAllclose(yscale_a, yscale_b)
print(" [passed~]")
def test_layernorm2d_fuseAdd_Dynamicquant_instance(dtype, m, n, yscaleType):
dim = (m, n)
input = torch.randn(dim, dtype=dtype, device="cuda")
weight = torch.randn(n, dtype=dtype, device="cuda")
bias = torch.randn(n, dtype=dtype, device="cuda")
res = torch.randn(dim, dtype=dtype, device="cuda")
(a, res_a, yscale_a), avg_a = run_torch(
input, weight, bias, 1e-5, residual=res, y_scale_dtype=yscaleType
)
(b, res_b, yscale_b), avg_b = run_ck(
input, weight, bias, 1e-5, residual=res, y_scale_dtype=yscaleType
)
print(
f"[perf] dim: {dim}, dtype: {dtype}, torch avg: {avg_a:<8.2f} us, ck avg: {avg_b:<8.2f} us, uplift: {avg_a/avg_b-1:<5.1%}"
)
checkAllclose(a, b, rtol=0, atol=1)
checkAllclose(res_a, res_b)
checkAllclose(yscale_a, yscale_b)
print(" [passed~]")
def test_layernorm2d():
print("\nstart layernorm2d test")
for dtype in [dtypes.fp16, dtypes.bf16]:
for m in [1, 2, 4, 8, 16, 32, 64, 128, 256]:
for n in [4096, 8192, 16384, 32768, 65536]:
test_layernorm2d_instance(dtype, m, n)
def test_layernorm2d_fuseAdd():
print("\nstart layernorm2d fuse add test")
for dtype in [dtypes.fp16, dtypes.bf16]:
for m in [1, 2, 4, 8, 16, 32, 64, 128, 256]:
for n in [4096, 8192, 16384, 32768, 65536]:
test_layernorm2d_fuseAdd_instance(dtype, m, n)
def test_layernorm2d_fuseSmoothquant():
print("\nstart layernorm2d fuse Smoothquant test")
for scaleType in [dtypes.fp32]:
for dtype in [dtypes.fp16, dtypes.bf16]:
for m in [1, 2, 4, 8, 16, 32, 64, 128, 256]:
for n in [10, 4096, 8192]:
test_layernorm2d_fuseSmoothquant_instance(
dtype, m, n, xscaleType=scaleType, yscaleType=scaleType
)
def test_layernorm2d_fuseAdd_Smoothquant():
print("\nstart layernorm2d fuse add Smoothquant test")
for scaleType in [dtypes.fp32]:
for dtype in [dtypes.bf16]:
for m in [2, 4, 8, 16, 32, 64, 128, 256]:
for n in [8192]:
test_layernorm2d_fuseAdd_Smoothquant_instance(
dtype, m, n, xscaleType=scaleType, yscaleType=scaleType
)
def test_layernorm2d_fuseDynamicquant():
print("\nstart layernorm2d fuse Smoothquant test")
for scaleType in [dtypes.fp32]:
for dtype in [dtypes.fp16, dtypes.bf16]:
for m in [1, 2, 4, 8, 16, 32, 64, 128, 256]:
for n in [1024, 2048]:
test_layernorm2d_fuseDynamicquant_instance(
dtype, m, n, yscaleType=scaleType
)
def test_layernorm2d_fuseAdd_Dynamicquant():
print("\nstart layernorm2d fuse add Smoothquant test")
for scaleType in [dtypes.fp32]:
for dtype in [dtypes.fp16, dtypes.bf16]:
for m in [1, 2, 4, 8, 16, 32, 64, 128, 256]:
for n in [1024, 2048]:
test_layernorm2d_fuseAdd_Dynamicquant_instance(
dtype, m, n, yscaleType=scaleType
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
prog="test_layernorm2dFusedSQuant",
description="Test ck layernorm2d Fused add and SmoothQuant",
)
parser.add_argument(
"--mode",
type=int,
choices=[1, 2, 3, 4, 5, 6],
help="1: test_layernorm2d, \n2:test_layernorm2d_fuseAdd, \n"
+ "3:test_layernorm2d_fuseSmoothquant, \n4:test_layernorm2d_fuseAdd_Smoothquant"
+ "5:test_layernorm2d_fuseDynamicquant, \n6:test_layernorm2d_fuseAdd_Dynamicquant\n"
+ " e.g.: --mode 1",
default=1,
)
# parser.add_argument(
# "--GPUID",
# type=str,
# help="This script uses single GPU. Specify the GPU to use for tuning",
# default="0",
# )
args = parser.parse_args()
if args.mode == 1:
test_layernorm2d()
elif args.mode == 2:
test_layernorm2d_fuseAdd()
elif args.mode == 3:
test_layernorm2d_fuseSmoothquant()
elif args.mode == 4:
test_layernorm2d_fuseAdd_Smoothquant()
elif args.mode == 5:
test_layernorm2d_fuseDynamicquant()
elif args.mode == 6:
test_layernorm2d_fuseAdd_Dynamicquant()