Spaces:
Runtime error
Runtime error
First version
Browse files- app.py +70 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import pandas as pd
|
3 |
+
import gradio as gr
|
4 |
+
from py_markdown_table.markdown_table import markdown_table
|
5 |
+
from model_sizer.utils import get_sizes, create_empty_model, convert_bytes
|
6 |
+
|
7 |
+
|
8 |
+
def convert_url_to_name(url:str):
|
9 |
+
"Converts a model URL to its name on the Hub"
|
10 |
+
results = re.findall(r"huggingface.co\/(.*?)#", url)
|
11 |
+
if len(results) < 1:
|
12 |
+
raise ValueError(f"URL {url} is not a valid model URL to the Hugging Face Hub")
|
13 |
+
return results[0]
|
14 |
+
|
15 |
+
def calculate_memory(model_name:str, library:str, options:list):
|
16 |
+
"Calculates the memory usage for a model"
|
17 |
+
if library == "auto":
|
18 |
+
library = None
|
19 |
+
if "huggingface.co" in model_name:
|
20 |
+
model_name = convert_url_to_name(model_name)
|
21 |
+
model = create_empty_model(model_name, library_name=library)
|
22 |
+
total_size, largest_layer = get_sizes(model)
|
23 |
+
|
24 |
+
data = []
|
25 |
+
|
26 |
+
title = f"Memory Usage for `{model_name}`"
|
27 |
+
for dtype in options:
|
28 |
+
dtype_total_size = total_size
|
29 |
+
dtype_largest_layer = largest_layer[0]
|
30 |
+
if dtype == "float16":
|
31 |
+
dtype_total_size /= 2
|
32 |
+
dtype_largest_layer /= 2
|
33 |
+
elif dtype == "int8":
|
34 |
+
dtype_total_size /= 4
|
35 |
+
dtype_largest_layer /= 4
|
36 |
+
elif dtype == "int4":
|
37 |
+
dtype_total_size /= 8
|
38 |
+
dtype_largest_layer /= 8
|
39 |
+
dtype_training_size = convert_bytes(dtype_total_size * 4)
|
40 |
+
dtype_total_size = convert_bytes(dtype_total_size)
|
41 |
+
dtype_largest_layer = convert_bytes(dtype_largest_layer)
|
42 |
+
data.append({
|
43 |
+
"dtype": dtype,
|
44 |
+
"Largest Layer": dtype_largest_layer,
|
45 |
+
"Total Size": dtype_total_size,
|
46 |
+
"Training using Adam": dtype_training_size
|
47 |
+
})
|
48 |
+
return pd.DataFrame(data)
|
49 |
+
# return f"## {title}\n\n" + markdown_table(data).set_params(
|
50 |
+
# row_sep="markdown", quote=False,
|
51 |
+
# ).get_markdown()
|
52 |
+
|
53 |
+
|
54 |
+
options = gr.CheckboxGroup(
|
55 |
+
["float32", "float16", "int8", "int4"],
|
56 |
+
)
|
57 |
+
|
58 |
+
library = gr.Radio(["auto", "transformers", "timm"], label="Library", value="auto")
|
59 |
+
|
60 |
+
iface = gr.Interface(
|
61 |
+
fn=calculate_memory,
|
62 |
+
inputs=[
|
63 |
+
"text",
|
64 |
+
library,
|
65 |
+
options,
|
66 |
+
],
|
67 |
+
outputs="dataframe"
|
68 |
+
)
|
69 |
+
|
70 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
model-sizer @ git+https://github.com/muellerzr/model-sizer@main
|
2 |
+
transformers
|
3 |
+
timm
|
4 |
+
huggingface_hub
|
5 |
+
py-markdown-table
|