Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Fin
Browse files
app.py
CHANGED
|
@@ -1,9 +1,44 @@
|
|
| 1 |
import re
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
-
from
|
| 5 |
-
from
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def convert_url_to_name(url:str):
|
| 9 |
"Converts a model URL to its name on the Hub"
|
|
@@ -12,14 +47,14 @@ def convert_url_to_name(url:str):
|
|
| 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 =
|
| 23 |
|
| 24 |
data = []
|
| 25 |
|
|
@@ -45,7 +80,15 @@ def calculate_memory(model_name:str, library:str, options:list):
|
|
| 45 |
"Total Size": dtype_total_size,
|
| 46 |
"Training using Adam": dtype_training_size
|
| 47 |
})
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
gr.Markdown(
|
|
@@ -75,10 +118,15 @@ with gr.Blocks() as demo:
|
|
| 75 |
["float32", "float16", "int8", "int4"],
|
| 76 |
value="float32"
|
| 77 |
)
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
btn.click(
|
| 81 |
-
calculate_memory, inputs=[inp, library, options], outputs=[out_text, out],
|
| 82 |
)
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
demo.launch()
|
|
|
|
| 1 |
import re
|
| 2 |
+
import webbrowser
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
| 5 |
+
from huggingface_hub import HfApi
|
| 6 |
+
from accelerate.commands.estimate import create_empty_model
|
| 7 |
+
from accelerate.utils import convert_bytes, calculate_maximum_sizes
|
| 8 |
|
| 9 |
+
# We need to store them as globals because gradio doesn't have a way for us to pass them in to the button
|
| 10 |
+
HAS_DISCUSSION = True
|
| 11 |
+
MODEL_NAME = None
|
| 12 |
+
LIBRARY = None
|
| 13 |
+
TRUST_REMOTE_CODE = False
|
| 14 |
+
|
| 15 |
+
# We use this class to check if a discussion has been opened on the model by `huggingface_model_memory_bot`
|
| 16 |
+
hf_api = HfApi()
|
| 17 |
+
|
| 18 |
+
def check_for_discussion(model_name:str):
|
| 19 |
+
"Checks if a discussion has been opened on the model"
|
| 20 |
+
global hf_api
|
| 21 |
+
discussions = list(hf_api.get_repo_discussions(model_name))
|
| 22 |
+
return any(discussion.title == "[AUTOMATED] Model Memory Requirements" for discussion in discussions)
|
| 23 |
+
|
| 24 |
+
def report_results():
|
| 25 |
+
"Reports the results of a memory calculation to the model's discussion"
|
| 26 |
+
global MODEL_NAME, LIBRARY, TRUST_REMOTE_CODE
|
| 27 |
+
_, results = calculate_memory(MODEL_NAME, LIBRARY, ["float32", "float16", "int8", "int4"], TRUST_REMOTE_CODE, raw=True)
|
| 28 |
+
post = f"""# Model Memory Requirements\n
|
| 29 |
+
|
| 30 |
+
These calculations were measured from the [Model Memory Utility Space](https://hf.co/spaces/muellerzr/model-memory-utility) on the Hub.
|
| 31 |
+
|
| 32 |
+
The minimum recommended vRAM needed for this model to perform inference via [Accelerate or `device_map="auto"`](https://huggingface.co/docs/accelerate/usage_guides/big_modeling) is denoted by the size of the "largest layer" and training of the model is roughly 4x its total size (for Adam).
|
| 33 |
+
|
| 34 |
+
## Results
|
| 35 |
+
|
| 36 |
+
"""
|
| 37 |
+
global hf_api
|
| 38 |
+
post += results.to_markdown(index=False)
|
| 39 |
+
discussion = hf_api.create_discussion(MODEL_NAME, "[AUTOMATED] Model Memory Requirements", description=post)
|
| 40 |
+
webbrowser.open_new_tab(discussion.url)
|
| 41 |
+
|
| 42 |
|
| 43 |
def convert_url_to_name(url:str):
|
| 44 |
"Converts a model URL to its name on the Hub"
|
|
|
|
| 47 |
raise ValueError(f"URL {url} is not a valid model URL to the Hugging Face Hub")
|
| 48 |
return results[0]
|
| 49 |
|
| 50 |
+
def calculate_memory(model_name:str, library:str, options:list, trust_remote_code:bool, raw=False):
|
| 51 |
"Calculates the memory usage for a model"
|
| 52 |
if library == "auto":
|
| 53 |
library = None
|
| 54 |
if "huggingface.co" in model_name:
|
| 55 |
model_name = convert_url_to_name(model_name)
|
| 56 |
+
model = create_empty_model(model_name, library_name=library, trust_remote_code=trust_remote_code)
|
| 57 |
+
total_size, largest_layer = calculate_maximum_sizes(model)
|
| 58 |
|
| 59 |
data = []
|
| 60 |
|
|
|
|
| 80 |
"Total Size": dtype_total_size,
|
| 81 |
"Training using Adam": dtype_training_size
|
| 82 |
})
|
| 83 |
+
global HAS_DISCUSSION, MODEL_NAME, LIBRARY, TRUST_REMOTE_CODE
|
| 84 |
+
HAS_DISCUSSION = check_for_discussion(model_name)
|
| 85 |
+
MODEL_NAME = model_name
|
| 86 |
+
LIBRARY = library
|
| 87 |
+
TRUST_REMOTE_CODE = trust_remote_code
|
| 88 |
+
results = [f'## {title}', pd.DataFrame(data)]
|
| 89 |
+
if not raw:
|
| 90 |
+
results += [gr.update(visible=not HAS_DISCUSSION)]
|
| 91 |
+
return results
|
| 92 |
|
| 93 |
with gr.Blocks() as demo:
|
| 94 |
gr.Markdown(
|
|
|
|
| 118 |
["float32", "float16", "int8", "int4"],
|
| 119 |
value="float32"
|
| 120 |
)
|
| 121 |
+
trust_remote_code = gr.Checkbox(label="Trust Remote Code", value=False)
|
| 122 |
+
btn = gr.Button("Calculate Memory Usage")
|
| 123 |
+
post_to_hub = gr.Button(value = "Report results in this model repo's discussions!", visible=False)
|
| 124 |
|
| 125 |
btn.click(
|
| 126 |
+
calculate_memory, inputs=[inp, library, options, trust_remote_code], outputs=[out_text, out, post_to_hub],
|
| 127 |
)
|
| 128 |
+
|
| 129 |
+
post_to_hub.click(report_results)
|
| 130 |
+
|
| 131 |
|
| 132 |
demo.launch()
|