forked from ROCm/aiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_moe.py
More file actions
executable file
·593 lines (555 loc) · 18.1 KB
/
Copy pathtest_moe.py
File metadata and controls
executable file
·593 lines (555 loc) · 18.1 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# SPDX-License-Identifier: MIT
# Copyright (C) 2024-2026, Advanced Micro Devices, Inc. All rights reserved.
import torch
from aiter.test_common import checkAllclose, perftest
from aiter import dtypes, get_gfx
from aiter.fused_moe import torch_moe, fused_topk
from aiter.fused_moe_bf16_asm import asm_moe
from aiter.ops.shuffle import shuffle_weight
from aiter import pertoken_quant
from aiter.int4_utils import *
from aiter import ActivationType
import argparse
BLOCK_SIZE_M = 32
def permute_weight_a(x: torch.Tensor) -> torch.Tensor:
# Hardcode BLOCK_K and BLOCK_N
BK = 128
BN = 128
x_ = x
x_ = x_.view(
x.shape[0], x.shape[1] // BN, BN // 16, 16, x.shape[2] // BK, BK // 32, 4, 8
)
x_ = x_.permute(0, 1, 5, 2, 6, 4, 3, 7)
x_ = x_.contiguous()
x_ = x_.view(x.shape[0], x.shape[1], x.shape[2])
return x_
@perftest(num_warmup=1, num_iters=2)
def torch_moe_test(
hidden_states,
w1,
w2,
topk_weight,
topk_ids,
# following for int8 quant
fc1_scale=None, # [expert, inter_dim, 1]
fc2_scale=None, # [expert, model_dim, 1]
fc1_smooth_scale=None, # [expert, 1, model_dim]
fc2_smooth_scale=None, # [expert, 1, inter_dim]
activation=ActivationType.Silu,
):
return torch_moe(
hidden_states,
w1,
w2,
topk_weight,
topk_ids,
fc1_scale,
fc2_scale,
fc1_smooth_scale,
fc2_smooth_scale,
None,
activation,
)
@perftest()
def asm_moe_test(
hidden_states,
w1,
w2,
topk_weight,
topk_ids,
# following for int8 quant
fc1_scale=None, # [expert, inter_dim, 1]
fc2_scale=None, # [expert, model_dim, 1]
fc1_smooth_scale=None, # [expert, 1, model_dim]
fc2_smooth_scale=None, # [expert, 1, inter_dim]
a16=False,
activation=ActivationType.Silu,
):
return asm_moe(
hidden_states,
w1,
w2,
topk_weight,
topk_ids,
fc1_scale,
fc2_scale,
fc1_smooth_scale,
fc2_smooth_scale,
a16,
None,
None,
None,
activation,
)
@perftest()
def vllm_moe(hidden_states, w1, w2, topk_weight, topk_ids):
return fused_experts(hidden_states, w1, w2, topk_weight, topk_ids, inplace=False)
quant_algo = [
"No", # g1u0/ck(g1ux) support
"int8quant", # g1u1 support
"fp8quant", # g1u1 support
"int8smoothquant", # g1u1/g1u0 support
"fp8smoothquant", # g1u1 support
"wint4afp8smoothquant", # g1u1 support
]
def test_fmoe(
dtype,
token,
model_dim,
inter_dim,
E,
topk,
quant="No",
use_g1u1=False,
shared_E=0,
activation=ActivationType.Silu,
):
quantAlgoId = quant_algo.index(quant)
if quantAlgoId not in [0, 3] and not use_g1u1:
print("g1u0 only could test no quant and int8smoothquant")
return
quantstr = quant_algo[quantAlgoId]
use_int4 = "wint4" in quantstr
quant_dtype = dtypes.i8 if use_int4 or quantstr.startswith("int8") else dtypes.fp8
use_smooth = "smooth" in quantstr
input = torch.randn((token, model_dim), dtype=dtype, device="cuda")
if use_g1u1:
w1 = (
torch.randn(
(E + shared_E, inter_dim * 2, model_dim), dtype=dtype, device="cuda"
)
/ 10.0
)
else:
w1 = torch.randn(
(E + shared_E, inter_dim, model_dim), dtype=dtype, device="cuda"
)
w2 = torch.randn((E + shared_E, model_dim, inter_dim), dtype=dtype, device="cuda")
score = torch.randn((token, E), device="cuda", dtype=dtype)
topk_weights, topk_ids = fused_topk(input, score, topk, True)
if shared_E > 0:
shared_E_score = 0.5
s_topk_weights = torch.tensor(
[
[shared_E_score, shared_E_score],
]
* token,
dtype=dtypes.fp32,
device=input.device,
)
topk_weights = torch.cat((topk_weights, s_topk_weights), dim=1)
s_topk_ids = torch.tensor(
[
[E, E + 1],
]
* token,
dtype=dtypes.i32,
device=input.device,
)
topk_ids = torch.cat((topk_ids, s_topk_ids), dim=1)
# ref implement
# w1a = permute_weight_a(w1)
# w2a = permute_weight_a(w2)
w1a = w1
w2a = w2
avg_a = 1
# ref1, avg_a = vllm_moe(input,
# w1a,
# w2a,
# topk_weights,
# topk_ids)
# print(f'{ref1=}')
if quantAlgoId == 0:
# ref2 implement
ref2, avg_c = torch_moe_test(input, w1, w2, topk_weights, topk_ids)
# b implement
w1b = shuffle_weight(w1)
w2b = shuffle_weight(w2)
if use_g1u1:
out_b = ref2
avg_b = 9999
print("asm g1u1 only support quant/smoothquant Now")
elif get_gfx() != "gfx942":
out_b = ref2
avg_b = 9999
print(f"skip asm g1u0 no-quant on {get_gfx()}: only runs on gfx942")
else:
out_b, avg_b = asm_moe_test(
input, w1b, w2b, topk_weights, topk_ids, activation=activation
)
msg = f"[perf] {token=}, quant={quantstr}, {model_dim=}, {inter_dim=}, {E=}, {topk=}, dtype: {dtype}, torch_avg: {avg_c:<8.2f} us, asm_avg: {avg_b:>8.2f} us, uplift: {avg_c/avg_b-1:.1%}"
checkAllclose(ref2, out_b, rtol=0.01, atol=100, msg=msg)
else:
dtypeMax = 7 if use_int4 else None
w1, fc1_scale = pertoken_quant(w1, quant_dtype=quant_dtype, dtypeMax=dtypeMax)
w2, fc2_scale = pertoken_quant(w2, quant_dtype=quant_dtype, dtypeMax=dtypeMax)
sp1 = (E + shared_E, inter_dim)
sp2 = (E + shared_E, model_dim)
if not use_smooth:
fc1_smooth_scale = None
fc2_smooth_scale = None
else:
if use_int4:
# fixme @felix: hack here, int4 kernel need this buffer but not used, so ones.
# [expert, 1, model_dim]
fc1_smooth_scale = torch.ones(sp2, dtype=dtypes.fp32, device="cuda")
# [expert, 1, inter_dim]
fc2_smooth_scale = torch.ones(sp1, dtype=dtypes.fp32, device="cuda")
else:
# [expert, 1, model_dim]
fc1_smooth_scale = torch.randn(sp2, dtype=dtypes.fp32, device="cuda")
# [expert, 1, inter_dim]
fc2_smooth_scale = torch.randn(sp1, dtype=dtypes.fp32, device="cuda")
# ref2 implement
ref2, avg_c = torch_moe_test(
input,
w1,
w2,
topk_weights,
topk_ids,
fc1_scale,
fc2_scale,
fc1_smooth_scale,
fc2_smooth_scale,
activation,
)
# b implement
if use_int4:
w1 = rearrange_4bit_elements(convert_int8_to_uint32_int4(w1))
w2 = rearrange_4bit_elements(convert_int8_to_uint32_int4(w2))
w1b = shuffle_weight(w1)
w2b = shuffle_weight(w2)
out_b, avg_b = asm_moe_test(
input,
w1b,
w2b,
topk_weights,
topk_ids,
fc1_scale,
fc2_scale,
fc1_smooth_scale,
fc2_smooth_scale,
a16=False,
activation=activation,
)
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
num_tb = calculateTensorsSize(
input,
input,
w1b,
w2b,
topk_weights,
topk_ids,
fc1_scale,
fc2_scale,
fc1_smooth_scale,
fc2_smooth_scale,
) / (1024 * 1024 * 1024 * 1024.0)
bw = num_tb * 1e6 / avg_b
print(
f"[BW ] {token=}, quant={quantstr}, {model_dim=}, {inter_dim=}, {E=}, {shared_E=}, {topk=}, dtype: {dtype}, asm_bandwidth: {bw:>8.2f}TB/s"
)
if use_smooth and (
(
(inter_dim % 512 == 0 or inter_dim % 320 == 0)
and (w1b.dtype == dtypes.fp8 and inter_dim * 2 == w1b.shape[1])
)
or (
(inter_dim % 320 == 0 or inter_dim % 256 == 0)
and (w1b.dtype == dtypes.i8 and inter_dim * 2 == w1b.shape[1])
)
or (
(inter_dim % 512 == 0)
and (w1b.dtype == dtypes.i8 and inter_dim == w1b.shape[1])
)
):
if input.dtype == dtypes.bf16:
out_b2, avg_b2 = asm_moe_test(
input,
w1b,
w2b,
topk_weights,
topk_ids,
fc1_scale,
fc2_scale,
fc1_smooth_scale,
fc2_smooth_scale,
a16=True,
activation=activation,
)
msg = f"[perf] a8w8 asm: {avg_b:>8.2f} vs a16w8 asm: {avg_b2:>8.2f} ......"
checkAllclose(ref2, out_b2, atol=100, msg=msg)
msg = f"[perf] {use_g1u1=} {token=}, quant={quantstr}, {model_dim=}, {inter_dim=}, {E=}, {shared_E=}, {topk=}, dtype: {dtype}, torch_avg: {avg_c:<8.2f} us, asm_avg: {avg_b:>8.2f} us ...... uplift: {avg_c/avg_b-1:.1%}"
checkAllclose(ref2, out_b, rtol=0.01, atol=100, msg=msg)
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description="select test",
)
parser.add_argument(
"-t",
"--test",
type=str,
choices=[
"test_fmoe_16_bit",
"g1u1_no_quant",
"g1u1_int8quant",
"g1u1_fp8quant",
"g1u0_int8smoothquant",
"g1u1_int8smoothquant",
"g1u1_fp8smoothquant",
"g1u1_int4",
],
default=[
"test_fmoe_16_bit",
"g1u1_no_quant",
"g1u1_int8quant",
"g1u1_fp8quant",
"g1u0_int8smoothquant",
"g1u1_int8smoothquant",
"g1u1_fp8smoothquant",
"g1u1_int4",
],
nargs="*",
help="""Select test to run.
e.g.: -t test_fmoe_16_bit
or -t test_fmoe_16_bit
or -t g1u1_no_quant
or -t g1u1_int8quant
or -t g1u1_fp8quant
or -t g1u0_int8smoothquant
or -t g1u1_int8smoothquant
or -t g1u1_fp8smoothquant
or -t g1u1_int4""",
)
parser.add_argument(
"-d",
"--dtype",
type=dtypes.str2Dtype,
nargs="*",
default=[dtypes.d_dtypes["bf16"]],
help="""Data type.
e.g.: -d bf16""",
)
parser.add_argument(
"-m",
"--token",
type=int,
nargs="*",
default=[128],
help="""Token Num.
e.g.: -m 128""",
)
parser.add_argument(
"-hd",
"--hidden_dim",
type=int,
nargs="*",
default=[4096],
help="""Hidden states dim.
e.g.: -hd 4096""",
)
parser.add_argument(
"-id",
"--inter_dim",
type=int,
nargs="*",
default=[1024],
help="""Intermediate dim.
e.g.: -id 1024""",
)
parser.add_argument(
"-e",
"--expert",
type=int,
nargs="?",
default=None,
help="""Number of experts.
e.g.: -e 32""",
)
parser.add_argument(
"-k",
"--topk",
type=int,
nargs="?",
default=None,
help="""Top-k value.
e.g.: -k 5""",
)
parser.add_argument(
"-a",
"--activation",
type=dtypes.str2ActivationType,
choices=[
"silu",
"gelu",
],
default="silu",
help="""Activation function.
e.g.: -a silu
or -a gelu
""",
)
args = parser.parse_args()
for test in args.test:
print(f"\nRunning test: {test}")
if test == "test_fmoe_16_bit":
print("test test_fmoe 16 bit")
print("\ng1u0 no quant")
for dtype in args.dtype:
for m in args.token:
for hdim in args.hidden_dim:
for idim in args.inter_dim:
expert = 32 if args.expert is None else args.expert
topk = 5 if args.topk is None else args.topk
test_fmoe(
dtype,
m,
hdim,
idim,
expert,
topk,
quant="No",
activation=args.activation,
)
elif test == "g1u1_no_quant":
for dtype in args.dtype:
for m in args.token:
for hdim in args.hidden_dim:
for idim in args.inter_dim:
expert = 32 if args.expert is None else args.expert
topk = 5 if args.topk is None else args.topk
test_fmoe(
dtype,
m,
hdim,
idim,
expert,
topk,
quant="No",
use_g1u1=True,
activation=args.activation,
)
elif test == "g1u1_int8quant":
for dtype in args.dtype:
for m in args.token:
for hdim in args.hidden_dim:
for idim in args.inter_dim:
expert = 32 if args.expert is None else args.expert
topk = 5 if args.topk is None else args.topk
test_fmoe(
dtype,
m,
hdim,
idim,
expert,
topk,
# quant='int8quant', use_g1u1=True, shared_E=0, activation=ActivationType.Gelu)
quant="int8quant",
use_g1u1=True,
activation=args.activation,
)
elif test == "g1u1_fp8quant":
for dtype in args.dtype:
for m in args.token:
for hdim in args.hidden_dim:
for idim in args.inter_dim:
expert = 32 if args.expert is None else args.expert
topk = 5 if args.topk is None else args.topk
test_fmoe(
dtype,
m,
hdim,
idim,
expert,
topk,
quant="fp8quant",
use_g1u1=True,
shared_E=0,
activation=args.activation,
)
# quant='fp8quant', use_g1u1=True)
elif test == "g1u0_int8smoothquant":
if get_gfx() != "gfx942":
print(f"skip {test} on {get_gfx()}: only runs on gfx942")
continue
for dtype in args.dtype:
for m in args.token:
for hdim in args.hidden_dim:
for idim in args.inter_dim:
expert = 32 if args.expert is None else args.expert
topk = 5 if args.topk is None else args.topk
test_fmoe(
dtype,
m,
hdim,
idim,
expert,
topk,
quant="int8smoothquant",
use_g1u1=False,
activation=args.activation,
)
elif test == "g1u1_int8smoothquant":
for dtype in args.dtype:
for m in args.token:
for hdim in args.hidden_dim:
for idim in args.inter_dim:
expert = 32 if args.expert is None else args.expert
topk = 5 if args.topk is None else args.topk
test_fmoe(
dtype,
m,
hdim,
idim,
expert,
topk,
quant="int8smoothquant",
use_g1u1=True,
activation=args.activation,
)
elif test == "g1u1_fp8smoothquant":
for dtype in args.dtype:
for m in args.token:
for hdim in args.hidden_dim:
for idim in args.inter_dim:
expert = 32 if args.expert is None else args.expert
topk = 5 if args.topk is None else args.topk
test_fmoe(
dtype,
m,
hdim,
idim,
expert,
topk,
quant="fp8smoothquant",
use_g1u1=True,
activation=args.activation,
)
elif test == "g1u1_int4":
if get_gfx() != "gfx942":
print(f"skip {test} on {get_gfx()}: only runs on gfx942")
continue
for dtype in args.dtype:
for m in args.token:
for hdim in args.hidden_dim:
for idim in args.inter_dim:
expert = 8 if args.expert is None else args.expert
topk = 3 if args.topk is None else args.topk
test_fmoe(
dtype,
m,
hdim,
idim,
expert,
topk,
quant="wint4afp8smoothquant",
use_g1u1=True,
activation=args.activation,
)
else:
raise ValueError(f"Unknown test: {test}")