forked from ROCm/aiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_moe_blockscale.py
More file actions
363 lines (331 loc) · 9.64 KB
/
Copy pathtest_moe_blockscale.py
File metadata and controls
363 lines (331 loc) · 9.64 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
# SPDX-License-Identifier: MIT
# Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.
import torch
import torch.nn.functional as F
import aiter
from aiter.test_common import checkAllclose, run_perftest
from aiter.fused_moe import torch_moe, moe_sorting, fused_topk
from aiter.ops.shuffle import shuffle_weight
from aiter import pertoken_quant
from aiter import dtypes
from einops import rearrange
import argparse
BLOCK_SIZE_M = 32
def torch_moe_blockscale(
hidden_states,
w1, # [expert, inter_dim*2, model_dim]
w2, # [expert, model_dim, inter_dim]
topk_weight,
topk_ids,
dtype,
# following for quant
scale_blks=(128, 128),
a_scale=None,
# [expert, inter_dim/blk_m, model_dim/blk_k]
fc1_scale=None,
# [expert, model_dim/blk_m, inter_dim/blk_k]
fc2_scale=None,
expert_mask=None,
):
computeType = dtypes.fp32
hidden_states = hidden_states.to(computeType)
w1 = w1.to(computeType)
w2 = w2.to(computeType)
token_num, topk = topk_ids.shape
expert, model_dim, inter_dim = w2.shape
B, D = hidden_states.shape
topk = topk_weight.shape[1]
if expert_mask is not None:
local_expert_hash = expert_mask.cumsum(0, dtype=dtypes.i32) - 1
local_expert_hash[expert_mask == 0] = -1
topk_ids = local_expert_hash[topk_ids]
blk_n, blk_k = scale_blks
if a_scale is not None:
# print(f'{a_scale.unsqueeze(-1).shape=}, {hidden_states.view(token_num, -1, blk_k).shape=}')
hidden_states = hidden_states.view(token_num, -1, blk_k) * a_scale.unsqueeze(-1)
hidden_states = hidden_states.view(token_num, -1)
hidden_states = hidden_states.view(token_num, 1, model_dim).repeat(1, topk, 1)
out = torch.zeros(
(B, topk, D),
dtype=computeType,
device=hidden_states.device,
)
if w2.shape[2] * 2 == w1.shape[1]:
moeType = "g1u1"
else:
moeType = "g1u0"
nblk_n = inter_dim // blk_n
nblk_k = model_dim // blk_k
if fc1_scale is not None:
# gose to quant D_w8a8/w8a8
# blk_n, blk_k = scale_blks
# expert, nblk_n, nblk_k = fc1_scale.shape
fc1_scale = rearrange(
fc1_scale.view(-1, 1)
.repeat(1, blk_n * blk_k)
.view(expert, -1, nblk_k, blk_n, blk_k),
"e num_blk_n num_blk_k blk_n blk_k -> e (num_blk_n blk_n) (num_blk_k blk_k)",
)
fc2_scale = rearrange(
fc2_scale.view(-1, 1)
.repeat(1, blk_n * blk_k)
.view(expert, nblk_k, nblk_n, blk_k, blk_n),
"e num_blk_n num_blk_k blk_n blk_k -> e (num_blk_n blk_n) (num_blk_k blk_k)",
)
w1 = w1 * fc1_scale
w2 = w2 * fc2_scale
for E_id in range(w1.shape[0]):
mask = topk_ids == E_id
if mask.sum():
sub_tokens = hidden_states[mask]
act_input = sub_tokens @ (w1[E_id].transpose(0, 1))
if moeType == "g1u1":
gate, up = act_input.split([inter_dim, inter_dim], dim=-1)
act_out = F.silu(gate) * up
else:
act_out = F.gelu(act_input)
out[mask] = act_out @ (w2[E_id].transpose(0, 1))
return (out * topk_weight.view(B, -1, 1)).sum(dim=1).to(dtype)
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]
):
return torch_moe(
hidden_states,
w1,
w2,
topk_weight,
topk_ids,
fc1_scale,
fc2_scale,
fc1_smooth_scale,
fc2_smooth_scale,
)
def asm_moe_test(
hidden_states,
w1,
w2,
topk_weights,
topk_ids,
# following for int8 quant
fc1_scale=None,
fc2_scale=None,
a1_scale=None,
scale_blk=(128, 128),
):
model_dim = hidden_states.shape[-1]
topk = topk_ids.shape[-1]
E = w1.shape[0]
sorted_token_ids, sorted_weights, sorted_expert_ids, num_valid_ids, out_asm = (
moe_sorting(topk_ids, topk_weights, E, model_dim, dtype)
)
scale_blk_n, scale_blk_k = scale_blk
aiter.fmoe_fp8_blockscale_g1u1(
out_asm,
hidden_states,
w1,
w2,
sorted_token_ids,
sorted_weights,
sorted_expert_ids,
num_valid_ids,
topk,
a1_scale,
fc1_scale,
fc2_scale,
"",
scale_blk_n,
scale_blk_k,
None,
)
return out_asm
torch.set_default_device("cuda")
def test_fmoe(
dtype,
token,
model_dim,
inter_dim,
scale_blks,
E,
topk,
quant="No",
use_g1u1=False,
shared_E=0,
):
input = torch.randn((token, model_dim), dtype=dtype)
if use_g1u1:
w1 = torch.randn((E + shared_E, inter_dim * 2, model_dim), dtype=dtype) / 10
else:
w1 = torch.randn((E + shared_E, inter_dim, model_dim), dtype=dtype)
w2 = torch.randn((E + shared_E, model_dim, inter_dim), dtype=dtype) / 10
score = torch.randn((token, E), dtype=dtype)
topk_weights, topk_ids = fused_topk(input, score, topk, True)
scale_blk_n, scale_blk_k = scale_blks
quant_dtype = dtypes.fp8
# block quant w1
tmp = rearrange(
w1.view(
-1,
w1.shape[1] // scale_blk_n,
scale_blk_n,
w1.shape[2] // scale_blk_k,
scale_blk_k,
),
"e num_blk_n blk_n num_blk_k blk_k -> e num_blk_n num_blk_k (blk_n blk_k)",
).contiguous()
w1_q, w1_scale = pertoken_quant(tmp, quant_dtype=quant_dtype)
w1_q = rearrange(
w1_q.view(
-1,
w1.shape[1] // scale_blk_n,
w1.shape[2] // scale_blk_k,
scale_blk_n,
scale_blk_k,
),
"e num_blk_n num_blk_k blk_n blk_k -> e (num_blk_n blk_n) (num_blk_k blk_k)",
).contiguous()
w1_scale = w1_scale.view(E, -1)
# block quant w2
tmp = rearrange(
w2.view(
-1,
model_dim // scale_blk_n,
scale_blk_n,
inter_dim // scale_blk_k,
scale_blk_k,
),
"e num_blk_n blk_n num_blk_k blk_k -> e num_blk_n num_blk_k (blk_n blk_k)",
).contiguous()
w2_q, w2_scale = pertoken_quant(tmp, quant_dtype=quant_dtype)
w2_q = rearrange(
w2_q.view(
-1,
w2.shape[1] // scale_blk_n,
w2.shape[2] // scale_blk_k,
scale_blk_n,
scale_blk_k,
),
"e num_blk_n num_blk_k blk_n blk_k -> e (num_blk_n blk_n) (num_blk_k blk_k)",
).contiguous()
w2_scale = w2_scale.view(E, -1)
# block quant input
a1_q, a1_scale = pertoken_quant(
input.view(-1, model_dim // scale_blk_k, scale_blk_k), quant_dtype=quant_dtype
)
a1_q = a1_q.view(-1, model_dim)
a1_scale = a1_scale.squeeze(-1)
# w2, fc2_scale = pertoken_quant(w2, quant_dtype=quant_dtype)
out_ref, us_ref = run_perftest(
torch_moe_blockscale,
a1_q,
w1_q,
w2_q,
topk_weights,
topk_ids,
dtype,
scale_blks=scale_blks,
fc1_scale=w1_scale,
fc2_scale=w2_scale,
a_scale=a1_scale,
num_warmup=1,
num_iters=2,
)
# out_ref2, us_ref = run_perftest(torch_moe,
# input,
# w1,
# w2,
# topk_weights,
# topk_ids,
# num_warmup=1, num_iters=2)
# msg = '111'
# checkAllclose(out_ref, out_ref2, rtol=0.01, atol=100, msg=msg)
out_asm, us_ref = run_perftest(
asm_moe_test,
a1_q,
shuffle_weight(w1_q, (16, 16)),
shuffle_weight(w2_q, (16, 16)),
topk_weights,
topk_ids,
w1_scale,
w2_scale,
a1_scale.t().contiguous(),
(scale_blk_n, scale_blk_k),
)
msg = f"[perf] a8w8 asm: {us_ref:>8.2f} us ...... {m=}, {model_dim=}, {inter_dim=}, {E=}, {shared_E=}, {topk=}, dtype: {dtype}"
checkAllclose(out_ref, out_asm, rtol=0.05, atol=0.05, msg=msg)
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description="config input of test",
)
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",
type=int,
nargs="*",
default=[1, 2, 5, 16, 32, 163840],
help="""M of mnk.
e.g.: -m 32""",
)
parser.add_argument(
"-dim",
type=int,
default=7168,
help="""Hidden dimension. Default is 7168.
e.g.: -dim 7168""",
)
parser.add_argument(
"-idim",
type=int,
default=256,
help="""Intermediate dimension. Default is 256.
e.g.: -idim 256""",
)
parser.add_argument(
"-e",
"--expert",
type=int,
default=256,
help="""Number of experts. Default is 256.
e.g.: -e 256""",
)
parser.add_argument(
"-k",
"--topk",
type=int,
default=8,
help="""Top-k value. Default is 8.
e.g.: -k 8""",
)
args = parser.parse_args()
for dtype in args.dtype:
for m in args.m:
for dim in [args.dim]:
for idim in [args.idim]:
scale_blks = (128, 128)
test_fmoe(
dtype,
m,
dim,
idim,
scale_blks,
args.expert,
args.topk,
quant="No",
use_g1u1=True,
)