Zekun Wu commited on
Commit
ae7b140
·
1 Parent(s): 55101ef
Files changed (3) hide show
  1. app.py +0 -0
  2. model_util.py +136 -0
  3. requirements.txt +0 -0
app.py ADDED
File without changes
model_util.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+
4
+ from accelerate.commands.estimate import check_has_model
5
+ from urllib.parse import urlparse
6
+ from huggingface_hub.utils import GatedRepoError
7
+ from huggingface_hub.utils import RepositoryNotFoundError
8
+
9
+ from calflops import create_empty_model
10
+ from calflops import calculate_flops_hf
11
+ from calflops import flops_to_string
12
+ from calflops import macs_to_string
13
+ from calflops import params_to_string
14
+
15
+
16
+ def calculate_flops_in_hugging_space(model_name: str,
17
+ empty_model: torch.nn.modules,
18
+ access_token: str,
19
+ input_shape: tuple,
20
+ bp_factor: float,
21
+ output_unit: str):
22
+ "Calculates the FLOPs and Params usage for a model init on `meta` device"
23
+
24
+ try:
25
+ flops, macs, params, return_print = calculate_flops_hf(model_name=model_name,
26
+ empty_model=empty_model,
27
+ access_token=access_token,
28
+ input_shape=input_shape,
29
+ return_results=True,
30
+ output_as_string=False)
31
+ except Exception as e:
32
+ print("Error info:", e)
33
+ raise gr.Error(
34
+ f"Model `{model_name}` does not support inference on the meta device, You can download the complete model parameters to your local and using the python package calflops to calculate FLOPs and Params of model `{model_name}`."
35
+ )
36
+
37
+ fw_bp_flops = flops * (1.0 + bp_factor)
38
+ fw_bp_macs = macs * (1.0 + bp_factor)
39
+
40
+ if output_unit == "":
41
+ pass
42
+ elif output_unit == "auto":
43
+ params = params_to_string(params, units=None, precision=3)
44
+ flops = flops_to_string(flops, units=None, precision=3)
45
+ macs = macs_to_string(macs, units=None, precision=3)
46
+ fw_bp_flops = flops_to_string(fw_bp_flops, units=None, precision=3)
47
+ fw_bp_macs = macs_to_string(fw_bp_macs, units=None, precision=3)
48
+ elif output_unit == "T" or output_unit == "G" or output_unit == "M" or output_unit == "K" or output_unit == "m" or output_unit == "u":
49
+ params = params_to_string(params, units=output_unit, precision=3)
50
+ flops = flops_to_string(flops, units=output_unit, precision=3)
51
+ macs = macs_to_string(macs, units=output_unit, precision=3)
52
+ fw_bp_flops = flops_to_string(fw_bp_flops, units=output_unit, precision=3)
53
+ fw_bp_macs = macs_to_string(fw_bp_macs, units=output_unit, precision=3)
54
+
55
+ return_lines = return_print.split("\n")
56
+ return_start = False
57
+ return_print = ""
58
+ for line in return_lines[:-2]:
59
+ if return_start:
60
+ return_print += line + "\n"
61
+ if "Detailed" in line:
62
+ return_start = True
63
+
64
+ data = []
65
+ data.append(
66
+ {"Total Training Params": params,
67
+ "Forward FLOPs": flops,
68
+ "Forward MACs": macs,
69
+ "Forward+Backward FLOPs": fw_bp_flops,
70
+ "Forward+Backward MACs": fw_bp_macs
71
+ }
72
+ )
73
+ return data, return_print
74
+
75
+
76
+ def extract_from_url(name: str):
77
+ "Checks if `name` is a URL, and if so converts it to a model name"
78
+ is_url = False
79
+ try:
80
+ result = urlparse(name)
81
+ is_url = all([result.scheme, result.netloc])
82
+ except Exception:
83
+ is_url = False
84
+ # Pass through if not a URL
85
+ if not is_url:
86
+ return name
87
+ else:
88
+ path = result.path
89
+ return path[1:]
90
+
91
+
92
+ def translate_llama2(text):
93
+ "Translates llama-2 to its hf counterpart"
94
+ if not text.endswith("-hf"):
95
+ return text + "-hf"
96
+ return text
97
+
98
+
99
+ def get_mode_from_hf(model_name: str, library: str, access_token: str):
100
+ "Finds and grabs model from the Hub, and initializes on `meta`"
101
+ if "meta-llama" in model_name:
102
+ model_name = translate_llama2(model_name)
103
+ if library == "auto":
104
+ library = None
105
+ model_name = extract_from_url(model_name)
106
+ try:
107
+ model = create_empty_model(model_name, library_name=library, trust_remote_code=True, access_token=access_token)
108
+ except GatedRepoError:
109
+ raise gr.Error(
110
+ f"Model `{model_name}` is a gated model, please ensure to pass in your access token and try again if you have access. You can find your access token here : https://huggingface.co/settings/tokens. "
111
+ )
112
+ except RepositoryNotFoundError:
113
+ raise gr.Error(f"Model `{model_name}` was not found on the Hub, please try another model name.")
114
+ except ValueError:
115
+ raise gr.Error(
116
+ f"Model `{model_name}` does not have any library metadata on the Hub, please manually select a library_name to use (such as `transformers`)"
117
+ )
118
+ except (RuntimeError, OSError) as e:
119
+ library = check_has_model(e)
120
+ if library != "unknown":
121
+ raise gr.Error(
122
+ f"Tried to load `{model_name}` with `{library}` but a possible model to load was not found inside the repo."
123
+ )
124
+ raise gr.Error(
125
+ f"Model `{model_name}` had an error, please open a discussion on the model's page with the error message and name: `{e}`"
126
+ )
127
+ except ImportError:
128
+ # hacky way to check if it works with `trust_remote_code=False`
129
+ model = create_empty_model(
130
+ model_name, library_name=library, trust_remote_code=False, access_token=access_token
131
+ )
132
+ except Exception as e:
133
+ raise gr.Error(
134
+ f"Model `{model_name}` had an error, please open a discussion on the model's page with the error message and name: `{e}`"
135
+ )
136
+ return model
requirements.txt ADDED
File without changes