Commit
•
0847403
1
Parent(s):
a50b299
Fixing naming
Browse files- calc_params.py +43 -0
calc_params.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
|
3 |
+
# Helper function to pretty-print message sizes
|
4 |
+
def convert_params(params):
|
5 |
+
if params == 0:
|
6 |
+
return "0"
|
7 |
+
size_name = ("", "K", "M", "B", "T", "P", "E", "Z", "Y")
|
8 |
+
i = int(math.floor(math.log(params, 1000)))
|
9 |
+
p = math.pow(1000, i)
|
10 |
+
s = round(params / p, 2)
|
11 |
+
return "%s %s" % (s, size_name[i])
|
12 |
+
|
13 |
+
# Parameter Calculation function
|
14 |
+
def calc_params(vocab_size, tied_embeddings, hidden_size, sequence_length, num_layers, moe, num_experts, expert_interval, topk, ffn_expansion_factor, num_mlp_linears, kv_size_ratio):
|
15 |
+
if tied_embeddings:
|
16 |
+
embedding_params = hidden_size * vocab_size
|
17 |
+
else:
|
18 |
+
embedding_params = 2 * hidden_size * vocab_size
|
19 |
+
position_embedding_params = hidden_size * sequence_length
|
20 |
+
attention_params = int(2 * (1 + kv_size_ratio) * num_layers * hidden_size * hidden_size)
|
21 |
+
layernorm_params = 13 * num_layers * hidden_size
|
22 |
+
|
23 |
+
if moe:
|
24 |
+
num_expert_layers = num_layers / expert_interval
|
25 |
+
ffn_expert_params = num_mlp_linears * ffn_expansion_factor * num_expert_layers * num_experts * hidden_size * hidden_size
|
26 |
+
ffn_dense_params = num_mlp_linears * ffn_expansion_factor * (num_layers - num_expert_layers) * hidden_size * hidden_size
|
27 |
+
ffn_params = ffn_expert_params + ffn_dense_params
|
28 |
+
gating_params = num_expert_layers * hidden_size * num_experts
|
29 |
+
else:
|
30 |
+
ffn_params = num_mlp_linears * ffn_expansion_factor * num_layers * hidden_size * hidden_size
|
31 |
+
|
32 |
+
total_params = embedding_params + attention_params + ffn_params + position_embedding_params + layernorm_params
|
33 |
+
|
34 |
+
if moe:
|
35 |
+
total_params += gating_params
|
36 |
+
|
37 |
+
return f"""
|
38 |
+
Embedding parameters: {convert_params(embedding_params)}
|
39 |
+
Attention parameters: {convert_params(attention_params)}
|
40 |
+
FFN parameters: {convert_params(ffn_params)}
|
41 |
+
{'Gating parameters: ' + convert_params(gating_params) if moe else ''}
|
42 |
+
Total Params in the Model: {convert_params(total_params)}
|
43 |
+
"""
|