derek-thomas HF staff commited on
Commit
dcb01bb
1 Parent(s): 5d70faf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -47
app.py CHANGED
@@ -35,6 +35,33 @@ def get_hf_model_args(hf_model_name_or_path, num_layers, hidden_size, num_attent
35
  "sequence_length": sequence_length,
36
  }, None
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # ---- Parameter Calculation ---- #
39
  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):
40
  if tied_embeddings:
@@ -67,50 +94,38 @@ def calc_params(vocab_size, tied_embeddings, hidden_size, sequence_length, num_l
67
  Total Params in the Model: {convert_params(total_params)}
68
  """
69
 
70
- # ---- Memory Calculation ---- #
71
- def calc_mem(hf_model_name_or_path, num_gpus, tensor_parallel_size, pipeline_parallel_size, batch_size_per_gpu, sequence_length, vocab_size, hidden_size, num_attention_heads, num_layers, ffn_expansion_factor, is_mixed_precision, misc_mem_gib):
72
- model_params, hf_error = get_hf_model_args(hf_model_name_or_path, num_layers, hidden_size, num_attention_heads, vocab_size, sequence_length)
73
-
74
- if hf_error:
75
- return hf_error
76
-
77
- num_layers = model_params["num_layers"]
78
- hidden_size = model_params["hidden_size"]
79
- num_attention_heads = model_params["num_attention_heads"]
80
- vocab_size = model_params["vocab_size"]
81
- sequence_length = model_params["sequence_length"]
82
-
83
- dp_degree = num_gpus / (tensor_parallel_size * pipeline_parallel_size)
84
- embed_params = 2 * vocab_size * hidden_size
85
- positional_params = hidden_size * sequence_length
86
- ln_params = 8 * hidden_size * num_layers + (2 * hidden_size)
87
- attention_params = int(2 * (1 + ffn_expansion_factor) * num_layers * hidden_size * hidden_size)
88
- mlp_params = ffn_expansion_factor * num_layers * hidden_size * hidden_size
89
- total_params = embed_params + positional_params + ln_params + attention_params + mlp_params
90
-
91
- bytes_per_param = 2 if is_mixed_precision else 4
92
- model_mem = total_params * bytes_per_param
93
- per_gpu_mem_gib = (model_mem / (tensor_parallel_size * pipeline_parallel_size)) / 1024**3 + misc_mem_gib
94
-
95
- return f"Per-GPU Memory Required for Training: {per_gpu_mem_gib:.2f} GiB"
96
-
97
- # Combine param calculation and memory calculation in the result
98
- def calculate_model(hf_model_name_or_path, tied_embeddings, num_gpus, tensor_parallel_size, pipeline_parallel_size, batch_size_per_gpu, sequence_length, vocab_size, hidden_size, num_attention_heads, num_layers, ffn_expansion_factor, num_mlp_linears, kv_size_ratio, moe, num_experts, expert_interval, topk, is_mixed_precision, misc_mem_gib):
99
- param_result = 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)
100
- mem_result = calc_mem(hf_model_name_or_path, num_gpus, tensor_parallel_size, pipeline_parallel_size, batch_size_per_gpu, sequence_length, vocab_size, hidden_size, num_attention_heads, num_layers, ffn_expansion_factor, is_mixed_precision, misc_mem_gib)
101
- return param_result + "\n" + mem_result
102
-
103
  # ---- Gradio Interface ---- #
104
  with gr.Blocks() as demo:
105
  with gr.Tabs():
106
- with gr.TabItem("Model Calculation"):
 
107
  hf_model_name_or_path = gr.Textbox(label="HuggingFace Model Name or Path (optional)", value="")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  vocab_size = gr.Number(label="Vocab Size", value=51200)
109
  tied_embeddings = gr.Checkbox(label="Tied Embeddings", value=False)
110
  hidden_size = gr.Number(label="Hidden Size", value=6144)
111
  sequence_length = gr.Number(label="Sequence Length", value=2048)
112
  num_layers = gr.Number(label="Number of Layers", value=44)
113
- num_attention_heads = gr.Number(label="Number of Attention Heads", value=64)
114
  ffn_expansion_factor = gr.Number(label="FFN Expansion Factor", value=4)
115
  num_mlp_linears = gr.Number(label="Number of Linear Layers per MLP Block", value=2)
116
  kv_size_ratio = gr.Number(label="KV Size Ratio", value=1.0)
@@ -121,17 +136,10 @@ with gr.Blocks() as demo:
121
  expert_interval = gr.Number(label="Expert Interval", value=1)
122
  topk = gr.Number(label="Top k Routing", value=1)
123
 
124
- num_gpus = gr.Number(label="Number of GPUs", value=1)
125
- tensor_parallel_size = gr.Number(label="Tensor Parallel Size", value=1)
126
- pipeline_parallel_size = gr.Number(label="Pipeline Parallel Size", value=1)
127
- batch_size_per_gpu = gr.Number(label="Batch Size per GPU", value=8)
128
- is_mixed_precision = gr.Checkbox(label="Mixed Precision", value=True)
129
- misc_mem_gib = gr.Number(label="Misc Memory Overhead (GiB)", value=5)
130
-
131
- result = gr.Textbox(label="Output", interactive=False)
132
- calculate_button = gr.Button("Calculate")
133
- calculate_button.click(calculate_model,
134
- inputs=[hf_model_name_or_path, tied_embeddings, num_gpus, tensor_parallel_size, pipeline_parallel_size, batch_size_per_gpu, sequence_length, vocab_size, hidden_size, num_attention_heads, num_layers, ffn_expansion_factor, num_mlp_linears, kv_size_ratio, moe, num_experts, expert_interval, topk, is_mixed_precision, misc_mem_gib],
135
- outputs=result)
136
 
137
  demo.launch()
 
35
  "sequence_length": sequence_length,
36
  }, None
37
 
38
+ # ---- Memory Calculation ---- #
39
+ def calc_mem(hf_model_name_or_path, num_gpus, tensor_parallel_size, pipeline_parallel_size, batch_size_per_gpu, sequence_length, vocab_size, hidden_size, num_attention_heads, num_layers, ffn_expansion_factor, is_mixed_precision, misc_mem_gib):
40
+ model_params, hf_error = get_hf_model_args(hf_model_name_or_path, num_layers, hidden_size, num_attention_heads, vocab_size, sequence_length)
41
+
42
+ if hf_error:
43
+ return hf_error
44
+
45
+ num_layers = model_params["num_layers"]
46
+ hidden_size = model_params["hidden_size"]
47
+ num_attention_heads = model_params["num_attention_heads"]
48
+ vocab_size = model_params["vocab_size"]
49
+ sequence_length = model_params["sequence_length"]
50
+
51
+ dp_degree = num_gpus / (tensor_parallel_size * pipeline_parallel_size)
52
+ embed_params = 2 * vocab_size * hidden_size
53
+ positional_params = hidden_size * sequence_length
54
+ ln_params = 8 * hidden_size * num_layers + (2 * hidden_size)
55
+ attention_params = int(2 * (1 + ffn_expansion_factor) * num_layers * hidden_size * hidden_size)
56
+ mlp_params = ffn_expansion_factor * num_layers * hidden_size * hidden_size
57
+ total_params = embed_params + positional_params + ln_params + attention_params + mlp_params
58
+
59
+ bytes_per_param = 2 if is_mixed_precision else 4
60
+ model_mem = total_params * bytes_per_param
61
+ per_gpu_mem_gib = (model_mem / (tensor_parallel_size * pipeline_parallel_size)) / 1024**3 + misc_mem_gib
62
+
63
+ return f"Per-GPU Memory Required for Training: {per_gpu_mem_gib:.2f} GiB"
64
+
65
  # ---- Parameter Calculation ---- #
66
  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):
67
  if tied_embeddings:
 
94
  Total Params in the Model: {convert_params(total_params)}
95
  """
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  # ---- Gradio Interface ---- #
98
  with gr.Blocks() as demo:
99
  with gr.Tabs():
100
+ # Memory Calculation Tab
101
+ with gr.TabItem("Memory Calculation"):
102
  hf_model_name_or_path = gr.Textbox(label="HuggingFace Model Name or Path (optional)", value="")
103
+ num_gpus = gr.Number(label="Number of GPUs", value=1)
104
+ tensor_parallel_size = gr.Number(label="Tensor Parallel Size", value=1)
105
+ pipeline_parallel_size = gr.Number(label="Pipeline Parallel Size", value=1)
106
+ batch_size_per_gpu = gr.Number(label="Batch Size per GPU", value=8)
107
+ sequence_length = gr.Number(label="Sequence Length", value=2048)
108
+ vocab_size = gr.Number(label="Vocab Size", value=51200)
109
+ hidden_size = gr.Number(label="Hidden Size", value=6144)
110
+ num_attention_heads = gr.Number(label="Number of Attention Heads", value=64)
111
+ num_layers = gr.Number(label="Number of Layers", value=44)
112
+ ffn_expansion_factor = gr.Number(label="FFN Expansion Factor", value=4)
113
+ is_mixed_precision = gr.Checkbox(label="Mixed Precision", value=True)
114
+ misc_mem_gib = gr.Number(label="Misc Memory Overhead (GiB)", value=5)
115
+
116
+ memory_result = gr.Textbox(label="Memory Calculation Result", interactive=False)
117
+ calc_memory_button = gr.Button("Calculate Memory")
118
+ calc_memory_button.click(calc_mem,
119
+ inputs=[hf_model_name_or_path, num_gpus, tensor_parallel_size, pipeline_parallel_size, batch_size_per_gpu, sequence_length, vocab_size, hidden_size, num_attention_heads, num_layers, ffn_expansion_factor, is_mixed_precision, misc_mem_gib],
120
+ outputs=memory_result)
121
+
122
+ # Parameter Calculation Tab
123
+ with gr.TabItem("Parameter Calculation"):
124
  vocab_size = gr.Number(label="Vocab Size", value=51200)
125
  tied_embeddings = gr.Checkbox(label="Tied Embeddings", value=False)
126
  hidden_size = gr.Number(label="Hidden Size", value=6144)
127
  sequence_length = gr.Number(label="Sequence Length", value=2048)
128
  num_layers = gr.Number(label="Number of Layers", value=44)
 
129
  ffn_expansion_factor = gr.Number(label="FFN Expansion Factor", value=4)
130
  num_mlp_linears = gr.Number(label="Number of Linear Layers per MLP Block", value=2)
131
  kv_size_ratio = gr.Number(label="KV Size Ratio", value=1.0)
 
136
  expert_interval = gr.Number(label="Expert Interval", value=1)
137
  topk = gr.Number(label="Top k Routing", value=1)
138
 
139
+ param_result = gr.Textbox(label="Parameter Calculation Result", interactive=False)
140
+ calc_param_button = gr.Button("Calculate Parameters")
141
+ calc_param_button.click(calc_params,
142
+ 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],
143
+ outputs=param_result)
 
 
 
 
 
 
 
144
 
145
  demo.launch()