Commit
•
8abcf2d
1
Parent(s):
7ff7edf
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import math
|
3 |
+
|
4 |
+
# Helper function to pretty-print message sizes
|
5 |
+
def convert_params(params):
|
6 |
+
if params == 0:
|
7 |
+
return "0"
|
8 |
+
size_name = ("", "K", "M", "B", "T", "P", "E", "Z", "Y")
|
9 |
+
i = int(math.floor(math.log(params, 1000)))
|
10 |
+
p = math.pow(1000, i)
|
11 |
+
s = round(params / p, 2)
|
12 |
+
return "%s %s" % (s, size_name[i])
|
13 |
+
|
14 |
+
# calculates the params of a model given their hparams
|
15 |
+
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):
|
16 |
+
# Calculate embedding and unembedding params. If tied, re-use the same params
|
17 |
+
if tied_embeddings:
|
18 |
+
embedding_params = hidden_size * vocab_size
|
19 |
+
else:
|
20 |
+
embedding_params = 2 * hidden_size * vocab_size
|
21 |
+
position_embedding_params = hidden_size * sequence_length
|
22 |
+
# Each QKVO matrix is (hxh)
|
23 |
+
# Unless using GQA/MQA which makes K/V smaller
|
24 |
+
attention_params = int(2 * (1 + kv_size_ratio) * num_layers * hidden_size * hidden_size)
|
25 |
+
# (4*2)lh from the layernorm weights and biases for each of the QKV and mlp_in layernorms, 1h for the final layernorm.
|
26 |
+
# the extra 4lh is a mystery but we include it here
|
27 |
+
layernorm_params = 13 * num_layers * hidden_size
|
28 |
+
#ffn_params = 12 * num_layers * hidden_size * hidden_size
|
29 |
+
|
30 |
+
if moe:
|
31 |
+
# the number of layers that are MoE. (e.g. interval is 2 for GShard)
|
32 |
+
num_expert_layers = num_layers / expert_interval
|
33 |
+
# the number of FFN params for each MoE layer
|
34 |
+
ffn_expert_params = num_mlp_linears * ffn_expansion_factor * num_expert_layers * num_experts * hidden_size * hidden_size
|
35 |
+
# the number of FFN params for every dense layer
|
36 |
+
ffn_dense_params = num_mlp_linears * ffn_expansion_factor * (num_layers - num_expert_layers) * hidden_size * hidden_size
|
37 |
+
ffn_params = ffn_expert_params + ffn_dense_params
|
38 |
+
# the number of gating layer params assuming it's implemented as a simple linear layer
|
39 |
+
gating_params = num_expert_layers * hidden_size * num_experts
|
40 |
+
else:
|
41 |
+
# num_mlp_layers * (h x [ffn_expansion_factor * h]) FFN matrices
|
42 |
+
ffn_params = num_mlp_linears * ffn_expansion_factor * num_layers * hidden_size * hidden_size
|
43 |
+
|
44 |
+
total_params = embedding_params + attention_params + ffn_params + position_embedding_params + layernorm_params
|
45 |
+
|
46 |
+
if moe:
|
47 |
+
total_params += gating_params
|
48 |
+
|
49 |
+
result = f"""
|
50 |
+
Embedding parameters: {convert_params(embedding_params)}
|
51 |
+
Attention parameters: {convert_params(attention_params)}
|
52 |
+
FFN parameters: {convert_params(ffn_params)}
|
53 |
+
{'Gating parameters: ' + convert_params(gating_params) if moe else ''}
|
54 |
+
Total Params in the Model: {convert_params(total_params)}
|
55 |
+
"""
|
56 |
+
return result
|
57 |
+
|
58 |
+
# Gradio interface
|
59 |
+
with gr.Blocks() as demo:
|
60 |
+
gr.Markdown("# Transformer Model Parameter Calculator")
|
61 |
+
|
62 |
+
vocab_size = gr.Number(label="Vocab Size", value=51200)
|
63 |
+
tied_embeddings = gr.Checkbox(label="Tied Embeddings", value=False)
|
64 |
+
hidden_size = gr.Number(label="Hidden Size", value=6144)
|
65 |
+
sequence_length = gr.Number(label="Sequence Length", value=2048)
|
66 |
+
num_layers = gr.Number(label="Number of Layers", value=44)
|
67 |
+
moe = gr.Checkbox(label="MoE", value=False)
|
68 |
+
num_experts = gr.Number(label="Number of Experts", value=8)
|
69 |
+
expert_interval = gr.Number(label="Expert Interval", value=1)
|
70 |
+
topk = gr.Number(label="Top k Routing", value=1)
|
71 |
+
ffn_expansion_factor = gr.Number(label="FFN Expansion Factor", value=4)
|
72 |
+
num_mlp_linears = gr.Number(label="Number of Linear Layers per MLP Block", value=2)
|
73 |
+
kv_size_ratio = gr.Number(label="KV Size Ratio", value=1.0)
|
74 |
+
|
75 |
+
result = gr.Textbox(label="Output", interactive=False)
|
76 |
+
|
77 |
+
def run_calculation(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):
|
78 |
+
return 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)
|
79 |
+
|
80 |
+
calculate_button = gr.Button("Calculate")
|
81 |
+
calculate_button.click(run_calculation,
|
82 |
+
inputs=[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],
|
83 |
+
outputs=[result])
|
84 |
+
|
85 |
+
demo.launch()
|