Commit
•
ef8c30b
1
Parent(s):
befd20b
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
import math
|
|
|
3 |
|
4 |
-
# Helper
|
5 |
def convert_params(params):
|
6 |
if params == 0:
|
7 |
return "0"
|
@@ -11,57 +12,78 @@ def convert_params(params):
|
|
11 |
s = round(params / p, 2)
|
12 |
return "%s %s" % (s, size_name[i])
|
13 |
|
14 |
-
#
|
15 |
-
def
|
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 |
dp_degree = args.num_gpus / (args.tensor_parallel_size * args.pipeline_parallel_size)
|
50 |
embed_params = 2 * args.vocab_size * args.hidden_size
|
51 |
positional_params = args.hidden_size * args.sequence_length
|
52 |
ln_params = 8 * args.hidden_size * args.num_layers + (2 * args.hidden_size)
|
53 |
-
attention_params = int(2 * (1 + args.
|
54 |
-
mlp_params = args.
|
55 |
total_params = embed_params + positional_params + ln_params + attention_params + mlp_params
|
56 |
|
57 |
-
bytes_per_param =
|
58 |
model_mem = total_params * bytes_per_param
|
59 |
-
|
60 |
-
per_gpu_mem_gib = per_gpu_model_mem / 1024**3 + args.misc_mem_gib
|
61 |
|
62 |
return f"Per-GPU Memory Required for Training: {per_gpu_mem_gib:.2f} GiB"
|
63 |
|
64 |
-
# Gradio Interface
|
65 |
with gr.Blocks() as demo:
|
66 |
with gr.Tabs():
|
67 |
with gr.TabItem("Parameter Calculation"):
|
@@ -101,6 +123,6 @@ with gr.Blocks() as demo:
|
|
101 |
|
102 |
memory_result = gr.Textbox(label="Memory Calculation Result", interactive=False)
|
103 |
calc_memory_button = gr.Button("Calculate Memory")
|
104 |
-
calc_memory_button.click(calc_mem, inputs=[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], outputs=memory_result)
|
105 |
|
106 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import math
|
3 |
+
from transformers import AutoConfig # Required for Hugging Face integration
|
4 |
|
5 |
+
# ---- Helper Functions ---- #
|
6 |
def convert_params(params):
|
7 |
if params == 0:
|
8 |
return "0"
|
|
|
12 |
s = round(params / p, 2)
|
13 |
return "%s %s" % (s, size_name[i])
|
14 |
|
15 |
+
# Set defaults for missing arguments
|
16 |
+
def set_defaults(args, defaults):
|
17 |
+
for key, value in defaults.items():
|
18 |
+
if getattr(args, key) is None:
|
19 |
+
setattr(args, key, value)
|
20 |
+
return args
|
21 |
+
|
22 |
+
# Set value if it's None, else use the config value
|
23 |
+
def set_if_none(args, key, config, config_key, defaults):
|
24 |
+
if getattr(args, key) is None:
|
25 |
+
setattr(args, key, config.get(config_key, defaults[key]))
|
26 |
+
return args
|
27 |
+
|
28 |
+
# Get Hugging Face model arguments
|
29 |
+
def get_hf_model_args(args, defaults):
|
30 |
+
if args.hf_model_name_or_path:
|
31 |
+
try:
|
32 |
+
config = AutoConfig.from_pretrained(args.hf_model_name_or_path, trust_remote_code=True).to_dict()
|
33 |
+
except Exception as e:
|
34 |
+
raise gr.Error(f"Error fetching Hugging Face model: {str(e)}")
|
35 |
+
|
36 |
+
# Update arguments with Hugging Face model config values
|
37 |
+
args.num_layers = config.get("num_hidden_layers", defaults["num_layers"])
|
38 |
+
args.hidden_size = config.get("hidden_size", defaults["hidden_size"])
|
39 |
+
args.num_attention_heads = config.get("num_attention_heads", defaults["num_attention_heads"])
|
40 |
+
args.vocab_size = config.get("vocab_size", defaults["vocab_size"])
|
41 |
+
args.sequence_length = config.get("max_position_embeddings", defaults["sequence_length"])
|
42 |
+
|
43 |
+
return set_defaults(args, defaults)
|
44 |
+
|
45 |
+
# ---- Memory Calculation ---- #
|
46 |
+
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):
|
47 |
+
|
48 |
+
# Define defaults
|
49 |
+
defaults = {
|
50 |
+
"num_layers": 44,
|
51 |
+
"hidden_size": 6144,
|
52 |
+
"num_attention_heads": 64,
|
53 |
+
"vocab_size": 51200,
|
54 |
+
"sequence_length": 2048,
|
55 |
+
"ffn_expansion_factor": 4,
|
56 |
+
}
|
57 |
+
|
58 |
+
# Create a simple args object to simulate parsed arguments
|
59 |
+
class Args:
|
60 |
+
def __init__(self, **kwargs):
|
61 |
+
for key, value in kwargs.items():
|
62 |
+
setattr(self, key, value)
|
63 |
+
|
64 |
+
args = Args(hf_model_name_or_path=hf_model_name_or_path, num_gpus=num_gpus, tensor_parallel_size=tensor_parallel_size,
|
65 |
+
pipeline_parallel_size=pipeline_parallel_size, batch_size_per_gpu=batch_size_per_gpu, sequence_length=sequence_length,
|
66 |
+
vocab_size=vocab_size, hidden_size=hidden_size, num_attention_heads=num_attention_heads, num_layers=num_layers,
|
67 |
+
ffn_expansion_factor=ffn_expansion_factor, is_mixed_precision=is_mixed_precision, misc_mem_gib=misc_mem_gib)
|
68 |
+
|
69 |
+
# Fetch Hugging Face model args if a model is provided
|
70 |
+
args = get_hf_model_args(args, defaults)
|
71 |
+
|
72 |
dp_degree = args.num_gpus / (args.tensor_parallel_size * args.pipeline_parallel_size)
|
73 |
embed_params = 2 * args.vocab_size * args.hidden_size
|
74 |
positional_params = args.hidden_size * args.sequence_length
|
75 |
ln_params = 8 * args.hidden_size * args.num_layers + (2 * args.hidden_size)
|
76 |
+
attention_params = int(2 * (1 + args.ffn_expansion_factor) * args.num_layers * args.hidden_size * args.hidden_size)
|
77 |
+
mlp_params = args.ffn_expansion_factor * args.num_layers * args.hidden_size * args.hidden_size
|
78 |
total_params = embed_params + positional_params + ln_params + attention_params + mlp_params
|
79 |
|
80 |
+
bytes_per_param = 2 if args.is_mixed_precision else 4
|
81 |
model_mem = total_params * bytes_per_param
|
82 |
+
per_gpu_mem_gib = (model_mem / (args.tensor_parallel_size * args.pipeline_parallel_size)) / 1024**3 + args.misc_mem_gib
|
|
|
83 |
|
84 |
return f"Per-GPU Memory Required for Training: {per_gpu_mem_gib:.2f} GiB"
|
85 |
|
86 |
+
# ---- Gradio Interface ---- #
|
87 |
with gr.Blocks() as demo:
|
88 |
with gr.Tabs():
|
89 |
with gr.TabItem("Parameter Calculation"):
|
|
|
123 |
|
124 |
memory_result = gr.Textbox(label="Memory Calculation Result", interactive=False)
|
125 |
calc_memory_button = gr.Button("Calculate Memory")
|
126 |
+
calc_memory_button.click(calc_mem, 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], outputs=memory_result)
|
127 |
|
128 |
demo.launch()
|