forked from jychen21/Habana-LLM-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
365 lines (305 loc) · 13.5 KB
/
base.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
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
# https://arxiv.org/pdf/2402.16363
from tabulate import tabulate
import matplotlib.pyplot as plt
device_bw_tops = {
"Gaudi2H_FP32": [2.24e12, 114e12, 11e12],
"Gaudi2H_FP16": [2.24e12, 420e12, 22e12],
"Gaudi2H_BF16": [2.24e12, 420e12, 22e12],
"Gaudi2H_FP8": [2.24e12, 840e12, 22e12],
"Gaudi2C_FP16": [2.24e12, 287e12, 22e12],
"Gaudi2C_BF16": [2.24e12, 287e12, 22e12],
"Gaudi2C_FP8": [2.24e12, 574e12, 22e12],
}
type2bytes = {
"fp32": 4,
"fp16": 2,
"bf16": 2,
"fp8": 1,
}
type2devices = {
"fp32": "Gaudi2H_FP32",
"fp16": "Gaudi2H_BF16",
"bf16": "Gaudi2C_BF16", # "Gaudi2H_BF16",
"fp8": "Gaudi2C_FP8", # "Gaudi2H_FP8",
}
item_list = ["Device", "HiddenSize", "HeadsQ", "HeadsKV", "InterSize", "Decoding", "Experts",
"Layers", "Input", "Output", "DType", "BS", "Latency(s)", "Throughput(tokens/sec)"]
layer_analysis_list = ["Input", "Output", "DataType", "BatchSize", "LayerName",
"NumOps(e9)", "Memory(GB)", "TopsRF(TFlops)", "AI", "Bound"]
class Config:
def __init__(self, batch_size, seq_len_q, seq_len_kv, hidden_size, num_heads_q, num_heads_kv,
intermediate_size, is_decoding, num_bytes, bw, tops, tops_tpc, with_gate, num_experts, num_layers):
self.batch_size = batch_size
self.seq_len_q = seq_len_q
self.seq_len_kv = seq_len_kv
self.hidden_size = hidden_size
self.num_heads_q = num_heads_q
self.num_heads_kv = num_heads_kv
self.intermediate_size = intermediate_size
self.is_decoding = is_decoding
self.num_bytes = num_bytes
self.bw = bw
self.tops = tops
self.tops_tpc = tops_tpc
self.with_gate = with_gate
self.num_experts = num_experts
self.num_layers = num_layers
self.kvcache_bucket = False
self.hardware_ai = tops / bw
self.hardware_ai_attn = tops / bw
if self.is_decoding:
self.hardware_ai_attn /= 128 # 128 for Gaudi2
def proj_qkvo_proj(model_config):
# memory (in & out)
params_in_input = model_config.batch_size * \
model_config.seq_len_q * model_config.hidden_size
params_in_weight = model_config.hidden_size * model_config.hidden_size
params_out = model_config.batch_size * \
model_config.seq_len_q * model_config.hidden_size
params_total = params_in_input + params_in_weight + params_out
params_total *= 4 # 4 for qkvo
bytes_total = params_total * model_config.num_bytes
runtime_memory = bytes_total / model_config.bw
# compute (2 for mul & add)
# [B, T_Q, H] @ [H, H]
num_ops = model_config.batch_size * model_config.seq_len_q * \
model_config.hidden_size * model_config.hidden_size * 2 * 4 # 4 for qkvo
tops = min(model_config.tops, model_config.tops *
(model_config.batch_size * model_config.seq_len_q / 128)) # 128 for Gaudi2
# arithmetic intensity (#flops / #bytes)
math_ai = num_ops / bytes_total
tops = min(tops, math_ai * model_config.bw)
runtime_compute = num_ops / tops # model_config.tops
proj_rst = {
"name": "qkvo_proj",
"#ops": num_ops,
"#mem": bytes_total,
"math_ai": math_ai,
"tops_roofline": tops,
"latency": runtime_memory if runtime_memory > runtime_compute else runtime_compute,
"bound": "memory" if math_ai < model_config.hardware_ai else "compute"
}
return proj_rst
def proj_attn_qk(model_config):
head_dim = model_config.hidden_size // model_config.num_heads_q
# memory (in & out)
params_in_q = model_config.batch_size * model_config.num_heads_q * \
model_config.seq_len_q * head_dim
params_in_k = model_config.batch_size * model_config.num_heads_kv * \
model_config.seq_len_kv * head_dim
params_out = model_config.batch_size * model_config.num_heads_q * \
model_config.seq_len_q * model_config.seq_len_kv
params_total = params_in_q + params_in_k + params_out
bytes_total = params_total * model_config.num_bytes
runtime_memory = bytes_total / model_config.bw
# compute (2 for mul & add)
# [B, M, T_Q, D] @ [B, M, D, T_KV]
num_ops = model_config.batch_size * model_config.num_heads_q * \
model_config.seq_len_q * head_dim * model_config.seq_len_kv * 2
tops = model_config.tops
if model_config.is_decoding:
# 128 for Gaudi2
# 128 for Gaudi2
tops = min(tops, tops * (model_config.batch_size / 128))
# arithmetic intensity (#flops / #bytes)
math_ai = num_ops / bytes_total
tops = min(tops, math_ai * model_config.bw)
runtime_compute = num_ops / tops
proj_rst = {
"name": "q@k_T",
"#ops": num_ops,
"#mem": bytes_total,
"math_ai": math_ai,
"tops_roofline": tops,
"latency": runtime_memory if runtime_memory > runtime_compute else runtime_compute,
"bound": "memory" if math_ai < model_config.hardware_ai_attn else "compute"
}
return proj_rst
def proj_attn_softmax(model_config):
head_dim = model_config.hidden_size // model_config.num_heads_q
# memory (in & out)
params_in = model_config.batch_size * model_config.num_heads_q * \
model_config.seq_len_q * model_config.seq_len_kv
params_out = model_config.batch_size * model_config.num_heads_q * \
model_config.seq_len_q * model_config.seq_len_kv
params_total = params_in + params_out
# 2 for tpc default dtype as bf16, model_config.num_bytes
bytes_total = params_total * 2
runtime_memory = bytes_total / model_config.bw
# compute (max, x-max, exp(x-max), sum(exp(x-max)), x/sum(exp(x-max)))
num_ops = model_config.batch_size * model_config.num_heads_q * \
model_config.seq_len_q * head_dim * \
model_config.seq_len_kv * 5 # 5 for traversal times
runtime_compute = num_ops / model_config.tops_tpc
# arithmetic intensity (#flops / #bytes)
math_ai = num_ops / bytes_total
proj_rst = {
"name": "softmax",
"#ops": num_ops,
"#mem": bytes_total,
"math_ai": math_ai,
"tops_roofline": min(model_config.tops_tpc, math_ai * model_config.bw),
"latency": runtime_memory if runtime_memory > runtime_compute else runtime_compute,
"bound": "memory" if math_ai < model_config.hardware_ai_attn else "compute"
}
return proj_rst
def proj_attn_scorev(model_config):
head_dim = model_config.hidden_size // model_config.num_heads_q
# memory (in & out)
params_in_score = model_config.batch_size * model_config.num_heads_q * \
model_config.seq_len_q * model_config.seq_len_kv
params_in_v = model_config.batch_size * \
model_config.num_heads_kv * model_config.seq_len_kv * head_dim
params_out = model_config.batch_size * \
model_config.num_heads_q * model_config.seq_len_q * head_dim
params_total = params_in_score + params_in_v + params_out
bytes_total = params_total * model_config.num_bytes
runtime_memory = bytes_total / model_config.bw
# compute (2 for mul & add)
# [B, M, T_Q, T_KV] @ [B, M, T_KV, D]
num_ops = model_config.batch_size * model_config.num_heads_q * \
model_config.seq_len_q * model_config.seq_len_kv * head_dim * 2
tops = model_config.tops
if model_config.is_decoding:
# 128 for Gaudi2
# 128 for Gaudi2
tops = min(tops, tops * (model_config.batch_size / 128))
# arithmetic intensity (#flops / #bytes)
math_ai = num_ops / bytes_total
tops = min(tops, math_ai * model_config.bw)
runtime_compute = num_ops / tops
proj_rst = {
"name": "score@v",
"#ops": num_ops,
"#mem": bytes_total,
"math_ai": math_ai,
"tops_roofline": tops,
"latency": runtime_memory if runtime_memory > runtime_compute else runtime_compute,
"bound": "memory" if math_ai < model_config.hardware_ai else "compute"
}
return proj_rst
def proj_mlp_gate_or_w3(model_config):
# memory (in & out)
params_in_input = model_config.batch_size * \
model_config.seq_len_q * model_config.hidden_size
params_in_weight = model_config.hidden_size * model_config.intermediate_size
params_out = model_config.batch_size * \
model_config.seq_len_q * model_config.intermediate_size
params_total = params_in_input + params_in_weight + params_out
bytes_total = params_total * model_config.num_bytes
runtime_memory = bytes_total / model_config.bw
# compute (2 for mul & add)
# [B, T_Q, H] @ [H, H_Inter]
num_ops = model_config.batch_size * model_config.seq_len_q * \
model_config.hidden_size * model_config.intermediate_size * 2
tops = min(model_config.tops, model_config.tops *
(model_config.batch_size * model_config.seq_len_q / 128)) # 128 for Gaudi2
# arithmetic intensity (#flops / #bytes)
math_ai = num_ops / bytes_total
tops = min(tops, math_ai * model_config.bw)
runtime_compute = num_ops / tops
proj_rst = {
"name": "mlp_gate(w3)",
"#ops": num_ops,
"#mem": bytes_total,
"math_ai": math_ai,
"tops_roofline": tops,
"latency": runtime_memory if runtime_memory > runtime_compute else runtime_compute,
"bound": "memory" if math_ai < model_config.hardware_ai else "compute"
}
return proj_rst
def proj_mlp_up_or_w1(model_config):
# memory (in & out)
params_in_input = model_config.batch_size * \
model_config.seq_len_q * model_config.hidden_size
params_in_weight = model_config.hidden_size * model_config.intermediate_size
params_out = model_config.batch_size * \
model_config.seq_len_q * model_config.intermediate_size
params_total = params_in_input + params_in_weight + params_out
bytes_total = params_total * model_config.num_bytes
runtime_memory = bytes_total / model_config.bw
# compute (2 for mul & add)
# [B, T_Q, H] @ [H, H_Inter]
num_ops = model_config.batch_size * model_config.seq_len_q * \
model_config.hidden_size * model_config.intermediate_size * 2
tops = min(model_config.tops, model_config.tops *
(model_config.batch_size * model_config.seq_len_q / 128)) # 128 for Gaudi2
# arithmetic intensity (#flops / #bytes)
math_ai = num_ops / bytes_total
tops = min(tops, math_ai * model_config.bw)
runtime_compute = num_ops / tops
proj_rst = {
"name": "mlp_up(w1)",
"#ops": num_ops,
"#mem": bytes_total,
"math_ai": math_ai,
"tops_roofline": tops,
"latency": runtime_memory if runtime_memory > runtime_compute else runtime_compute,
"bound": "memory" if math_ai < model_config.hardware_ai else "compute"
}
return proj_rst
def proj_mlp_down_or_w2(model_config):
# memory (in & out)
params_in_input = model_config.batch_size * \
model_config.seq_len_q * model_config.intermediate_size
params_in_weight = model_config.intermediate_size * model_config.hidden_size
params_out = model_config.batch_size * \
model_config.seq_len_q * model_config.hidden_size
params_total = params_in_input + params_in_weight + params_out
bytes_total = params_total * model_config.num_bytes
runtime_memory = bytes_total / model_config.bw
# compute (2 for mul & add)
# [B, T_Q, H_Inter] @ [H_Inter, H]
num_ops = model_config.batch_size * model_config.seq_len_q * \
model_config.hidden_size * model_config.intermediate_size * 2
tops = min(model_config.tops, model_config.tops *
(model_config.batch_size * model_config.seq_len_q / 128)) # 128 for Gaudi2
# arithmetic intensity (#flops / #bytes)
math_ai = num_ops / bytes_total
tops = min(tops, math_ai * model_config.bw)
runtime_compute = num_ops / tops
proj_rst = {
"name": "mlp_down(w2)",
"#ops": num_ops,
"#mem": bytes_total,
"math_ai": math_ai,
"tops_roofline": tops,
"latency": runtime_memory if runtime_memory > runtime_compute else runtime_compute,
"bound": "memory" if math_ai < model_config.hardware_ai else "compute"
}
return proj_rst
def print_projection(projection_dict):
for key, projection in projection_dict.items():
for _, proj in projection.items():
print(key.center(150))
for data in proj:
print(tabulate(data))
def print_analysis(analysis_dict, batchsize_list):
for key, analysis in analysis_dict.items():
for bs in batchsize_list:
for data in analysis:
print(key.center(100))
print(tabulate(data[bs]))
def plot_projection(projection_dict, batchsize_list):
for key, projection in projection_dict.items():
if key == "decode":
plt.figure(figsize=(20, 10))
for dtype, proj in projection.items():
for data in proj:
proj_list = []
for i in range(0, len(batchsize_list)):
proj_list.append(data[i+1][-1])
device, input, output = data[1][0], data[1][8], data[1][9]
plt.plot(batchsize_list, proj_list,
label=f"{device}_{dtype}_{input}_{output}")
for b, p in zip(batchsize_list, proj_list):
plt.text(b, p, p, ha='right', va='bottom', fontsize=9)
plt.xticks(batchsize_list, batchsize_list)
plt.tick_params(axis='x', rotation=70)
plt.xlabel("batch size")
plt.ylabel("tokens / s")
plt.title("throughput")
plt.grid(axis='x')
plt.legend()
plt.show()
plt.savefig("./figure/decode_projection.png")