Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoConfig, AutoModelForCausalLM
|
4 |
+
from accelerate import init_empty_weights
|
5 |
+
|
6 |
+
def recommend_gpu_mem_util(
|
7 |
+
model_config_url,
|
8 |
+
batch_size,
|
9 |
+
max_prompt_length,
|
10 |
+
max_completion_length,
|
11 |
+
tp_size,
|
12 |
+
gpu_memory=79,
|
13 |
+
precision_in_bytes=2,
|
14 |
+
kv_multiplier=2
|
15 |
+
):
|
16 |
+
# Load model config from HF URL
|
17 |
+
try:
|
18 |
+
config = AutoConfig.from_pretrained(model_config_url)
|
19 |
+
except Exception as e:
|
20 |
+
msg = f"Failed to load model config from URL: {e}"
|
21 |
+
return msg, {"Error": msg}
|
22 |
+
|
23 |
+
# Extract model config params
|
24 |
+
try:
|
25 |
+
num_hidden_layers = getattr(config, "num_hidden_layers")
|
26 |
+
hidden_size = getattr(config, "hidden_size")
|
27 |
+
num_attention_heads = getattr(config, "num_attention_heads")
|
28 |
+
num_key_value_heads = getattr(config, "num_key_value_heads", num_attention_heads)
|
29 |
+
except Exception as e:
|
30 |
+
msg = f"Required field missing in model config: {e}"
|
31 |
+
return msg, {"Error": msg}
|
32 |
+
|
33 |
+
# Estimate model no. parameters
|
34 |
+
try:
|
35 |
+
with init_empty_weights():
|
36 |
+
model = AutoModelForCausalLM.from_config(config)
|
37 |
+
num_params = sum(p.numel() for p in model.parameters())
|
38 |
+
model_params = num_params / 1e9
|
39 |
+
est_msg = f"Estimated model_params from config: {model_params:.2f}B"
|
40 |
+
except Exception as e:
|
41 |
+
msg = f"Failed to estimate model parameters: {e}"
|
42 |
+
return msg, {"Error": msg}
|
43 |
+
|
44 |
+
# Calculate all memory and utilization values
|
45 |
+
try:
|
46 |
+
seq_len = max_prompt_length + max_completion_length
|
47 |
+
|
48 |
+
model_size = float(model_params) * 1024**3 * precision_in_bytes / tp_size
|
49 |
+
|
50 |
+
# KV_cache_per_token = kv_multiplier (K and V) * num_hidden_layers * (num_key_value_heads * hidden_size / num_attention_heads) * precision_in_bytes
|
51 |
+
kv_cache_per_token = (
|
52 |
+
kv_multiplier
|
53 |
+
* num_hidden_layers
|
54 |
+
* (num_key_value_heads * hidden_size / num_attention_heads)
|
55 |
+
* precision_in_bytes
|
56 |
+
)
|
57 |
+
# KV_cache_total = KV_cache_per_token * Batch_size * Seq_len (max_prompt_length + max_completion_length)
|
58 |
+
kv_cache_total = kv_cache_per_token * batch_size * seq_len
|
59 |
+
# Buffer = (Model + KV_cache) * 0.2 # generous 20% buffer
|
60 |
+
buffer_size = 0.2 * (model_size + kv_cache_total)
|
61 |
+
# Total = Model + KV_cache + Buffer
|
62 |
+
total_required = model_size + kv_cache_total + buffer_size
|
63 |
+
# GPU utilization = Total_reqd / Total_gpu
|
64 |
+
gpu_memory_bytes = float(gpu_memory) * 1024**3
|
65 |
+
gpu_utilization_ratio = total_required / gpu_memory_bytes
|
66 |
+
# Round up to nearest 0.05 - this generous estimate works much better than actual prediction!
|
67 |
+
rounded_utilization = math.ceil(gpu_utilization_ratio * 20) / 20 + 0.05
|
68 |
+
|
69 |
+
main_result = f"vllm_gpu_memory_utilization = {rounded_utilization:.2f}"
|
70 |
+
ans = {
|
71 |
+
"KV_cache_per_token_MB": kv_cache_per_token / 1024**2,
|
72 |
+
"KV_cache_total_GB": kv_cache_total / 1024**3,
|
73 |
+
"Model_size_GB": model_size / 1024**3,
|
74 |
+
"Buffer_GB": buffer_size / 1024**3,
|
75 |
+
"Total_required_GB": total_required / 1024**3,
|
76 |
+
"GPU_mem_util": gpu_utilization_ratio,
|
77 |
+
"GPU_mem_util_recommended": rounded_utilization,
|
78 |
+
"model_params": est_msg,
|
79 |
+
"num_hidden_layers": num_hidden_layers,
|
80 |
+
"hidden_size": hidden_size,
|
81 |
+
"num_attention_heads": num_attention_heads,
|
82 |
+
"num_key_value_heads": num_key_value_heads,
|
83 |
+
}
|
84 |
+
|
85 |
+
return main_result, ans
|
86 |
+
except Exception as e:
|
87 |
+
msg = f"Error during calculation: {e}"
|
88 |
+
return msg, {"Error": msg}
|
89 |
+
|
90 |
+
iface = gr.Interface(
|
91 |
+
fn=recommend_gpu_mem_util,
|
92 |
+
inputs=[
|
93 |
+
gr.Textbox(label="Model Config URL (HuggingFace)", value="https://huggingface.co/Qwen/Qwen2.5-Math-1.5B/resolve/main/config.json"),
|
94 |
+
gr.Number(label="per_device_train_batch_size", value=4),
|
95 |
+
gr.Number(label="max_prompt_length", value=512),
|
96 |
+
gr.Number(label="max_completion_length", value=512),
|
97 |
+
gr.Number(label="vllm_tensor_parallel_size (tp_size)", value=1),
|
98 |
+
gr.Number(label="GPU Memory (GB)", value=79),
|
99 |
+
gr.Number(label="Precision in Bytes (e.g., 2)", value=2),
|
100 |
+
gr.Number(label="KV Multiplier", value=2),
|
101 |
+
],
|
102 |
+
outputs=[
|
103 |
+
gr.Textbox(label="Recommended vLLM GPU Memory Utilization"),
|
104 |
+
gr.JSON(label="Calculation Details"),
|
105 |
+
],
|
106 |
+
title="vLLM GRPO GPU Memory Utilization Estimator",
|
107 |
+
description = """
|
108 |
+
Paste your HuggingFace model config URL (ending in config.json), and enter experiment details.
|
109 |
+
Model parameters are automatically extracted and estimated from the config.
|
110 |
+
|
111 |
+
Note: This is a general recommendation and may not be optimal for your specific environment.
|
112 |
+
Always verify your actual training GPU requirements. For example, if you're using DeepSpeed, consider utilizing their memory estimation tool:
|
113 |
+
https://deepspeed.readthedocs.io/en/latest/memory.html
|
114 |
+
|
115 |
+
If you encounter "not enough memory" errors, try increasing the GPU memory utilization setting.
|
116 |
+
If you experience out-of-memory (OOM) errors, lower the utilization value and/or reduce your batch size.
|
117 |
+
""",
|
118 |
+
allow_flagging="never"
|
119 |
+
)
|
120 |
+
|
121 |
+
if __name__ == "__main__":
|
122 |
+
iface.launch()
|