muellerzr HF staff commited on
Commit
a924cd8
1 Parent(s): e44403a

Finish refactor

Browse files
Files changed (4) hide show
  1. .gitignore +144 -0
  2. src/app.py +2 -3
  3. src/hub_utils.py +6 -24
  4. src/model_utils.py +18 -2
.gitignore ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ pip-wheel-metadata/
24
+ share/python-wheels/
25
+ *.egg-info/
26
+ .installed.cfg
27
+ *.egg
28
+ MANIFEST
29
+
30
+ # PyInstaller
31
+ # Usually these files are written by a python script from a template
32
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
33
+ *.manifest
34
+ *.spec
35
+
36
+ # Installer logs
37
+ pip-log.txt
38
+ pip-delete-this-directory.txt
39
+
40
+ # Unit test / coverage reports
41
+ htmlcov/
42
+ .tox/
43
+ .nox/
44
+ .coverage
45
+ .coverage.*
46
+ .cache
47
+ nosetests.xml
48
+ coverage.xml
49
+ *.cover
50
+ *.py,cover
51
+ .hypothesis/
52
+ .pytest_cache/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ target/
76
+
77
+ # Jupyter Notebook
78
+ .ipynb_checkpoints
79
+
80
+ # IPython
81
+ profile_default/
82
+ ipython_config.py
83
+
84
+ # pyenv
85
+ .python-version
86
+
87
+ # pipenv
88
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
90
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
91
+ # install all needed dependencies.
92
+ #Pipfile.lock
93
+
94
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95
+ __pypackages__/
96
+
97
+ # Celery stuff
98
+ celerybeat-schedule
99
+ celerybeat.pid
100
+
101
+ # SageMath parsed files
102
+ *.sage.py
103
+
104
+ # Environments
105
+ .env
106
+ .venv
107
+ env/
108
+ venv/
109
+ ENV/
110
+ env.bak/
111
+ venv.bak/
112
+
113
+ # Spyder project settings
114
+ .spyderproject
115
+ .spyproject
116
+
117
+ # Rope project settings
118
+ .ropeproject
119
+
120
+ # mkdocs documentation
121
+ /site
122
+
123
+ # mypy
124
+ .mypy_cache/
125
+ .dmypy.json
126
+ dmypy.json
127
+
128
+ # Pyre type checker
129
+ .pyre/
130
+
131
+ # VSCode
132
+ .vscode
133
+
134
+ # IntelliJ
135
+ .idea
136
+
137
+ # Mac .DS_Store
138
+ .DS_Store
139
+
140
+ # More test things
141
+ wandb
142
+
143
+ # ruff
144
+ .ruff_cache
src/app.py CHANGED
@@ -1,8 +1,7 @@
1
  import gradio as gr
2
  import pandas as pd
3
-
4
- from .hub_utils import check_for_discussion, report_results
5
- from .model_utils import calculate_memory, get_model
6
 
7
 
8
  # We need to store them as globals because gradio doesn't have a way for us to pass them in to the button
 
1
  import gradio as gr
2
  import pandas as pd
3
+ from hub_utils import check_for_discussion, report_results
4
+ from model_utils import calculate_memory, get_model
 
5
 
6
 
7
  # We need to store them as globals because gradio doesn't have a way for us to pass them in to the button
src/hub_utils.py CHANGED
@@ -1,33 +1,16 @@
1
  # Utilities related to searching and posting on the Hub
2
  import os
3
  import webbrowser
4
- from urllib.parse import urlparse
5
 
6
  import pandas as pd
7
  from huggingface_hub import HfApi
8
-
9
- from .model_utils import calculate_memory, get_model
10
-
11
-
12
- def extract_from_url(name: str):
13
- "Checks if `name` is a URL, and if so converts it to a model name"
14
- is_url = False
15
- try:
16
- result = urlparse(name)
17
- is_url = all([result.scheme, result.netloc])
18
- except Exception:
19
- is_url = False
20
- # Pass through if not a URL
21
- if not is_url:
22
- return name
23
- else:
24
- path = result.path
25
- return path[1:]
26
 
27
 
28
  def check_for_discussion(model_name: str):
29
  "Checks if an automated discussion has been opened on the model by `model-sizer-bot`"
30
  api = HfApi(token=os.environ.get("HUGGINGFACE_API_LOGIN", None))
 
31
  discussions = list(api.get_repo_discussions(model_name))
32
  return any(
33
  discussion.title == "[AUTOMATED] Model Memory Requirements" and discussion.author == "model-sizer-bot"
@@ -38,13 +21,12 @@ def check_for_discussion(model_name: str):
38
  def report_results(model_name, library, access_token):
39
  "Reports the results of a memory calculation to the model's discussion page, and opens a new tab to it afterwards"
40
  model = get_model(model_name, library, access_token)
41
- data = calculate_memory(model, ["fp32", "fp16", "int8", "int4"])
42
- minimum = data[0]
43
- data = pd.DataFrame(data).to_markdown(index=False)
44
 
45
  post = f"""# Model Memory Requirements\n
46
 
47
- You will need about {minimum[1]} VRAM to load this model for inference, and {minimum[3]} VRAM to train it using Adam.
48
 
49
  These calculations were measured from the [Model Memory Utility Space](https://hf.co/spaces/hf-accelerate/model-memory-utility) on the Hub.
50
 
@@ -55,7 +37,7 @@ When training with `Adam`, you can expect roughly 4x the reported results to be
55
 
56
  ## Results:
57
 
58
- {data}
59
  """
60
  api = HfApi(token=os.environ.get("HUGGINGFACE_API_LOGIN", None))
61
  discussion = api.create_discussion(model_name, "[AUTOMATED] Model Memory Requirements", description=post)
 
1
  # Utilities related to searching and posting on the Hub
2
  import os
3
  import webbrowser
 
4
 
5
  import pandas as pd
6
  from huggingface_hub import HfApi
7
+ from model_utils import calculate_memory, extract_from_url, get_model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
 
10
  def check_for_discussion(model_name: str):
11
  "Checks if an automated discussion has been opened on the model by `model-sizer-bot`"
12
  api = HfApi(token=os.environ.get("HUGGINGFACE_API_LOGIN", None))
13
+ model_name = extract_from_url(model_name)
14
  discussions = list(api.get_repo_discussions(model_name))
15
  return any(
16
  discussion.title == "[AUTOMATED] Model Memory Requirements" and discussion.author == "model-sizer-bot"
 
21
  def report_results(model_name, library, access_token):
22
  "Reports the results of a memory calculation to the model's discussion page, and opens a new tab to it afterwards"
23
  model = get_model(model_name, library, access_token)
24
+ data = calculate_memory(model, ["float32", "float16/bfloat16", "int8", "int4"])
25
+ df = pd.DataFrame(data).to_markdown(index=False)
 
26
 
27
  post = f"""# Model Memory Requirements\n
28
 
29
+ You will need about {data[1]} VRAM to load this model for inference, and {data[3]} VRAM to train it using Adam.
30
 
31
  These calculations were measured from the [Model Memory Utility Space](https://hf.co/spaces/hf-accelerate/model-memory-utility) on the Hub.
32
 
 
37
 
38
  ## Results:
39
 
40
+ {df}
41
  """
42
  api = HfApi(token=os.environ.get("HUGGINGFACE_API_LOGIN", None))
43
  discussion = api.create_discussion(model_name, "[AUTOMATED] Model Memory Requirements", description=post)
src/model_utils.py CHANGED
@@ -1,16 +1,32 @@
1
  # Utilities related to loading in and working with models/specific models
 
 
2
  import gradio as gr
3
  import torch
4
  from accelerate.commands.estimate import check_has_model, create_empty_model
5
  from accelerate.utils import calculate_maximum_sizes, convert_bytes
6
  from huggingface_hub.utils import GatedRepoError, RepositoryNotFoundError
7
 
8
- from .hub_utils import extract_from_url
9
-
10
 
11
  DTYPE_MODIFIER = {"float32": 1, "float16/bfloat16": 2, "int8": 4, "int4": 8}
12
 
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def translate_llama2(text):
15
  "Translates llama-2 to its hf counterpart"
16
  if not text.endswith("-hf"):
 
1
  # Utilities related to loading in and working with models/specific models
2
+ from urllib.parse import urlparse
3
+
4
  import gradio as gr
5
  import torch
6
  from accelerate.commands.estimate import check_has_model, create_empty_model
7
  from accelerate.utils import calculate_maximum_sizes, convert_bytes
8
  from huggingface_hub.utils import GatedRepoError, RepositoryNotFoundError
9
 
 
 
10
 
11
  DTYPE_MODIFIER = {"float32": 1, "float16/bfloat16": 2, "int8": 4, "int4": 8}
12
 
13
 
14
+ def extract_from_url(name: str):
15
+ "Checks if `name` is a URL, and if so converts it to a model name"
16
+ is_url = False
17
+ try:
18
+ result = urlparse(name)
19
+ is_url = all([result.scheme, result.netloc])
20
+ except Exception:
21
+ is_url = False
22
+ # Pass through if not a URL
23
+ if not is_url:
24
+ return name
25
+ else:
26
+ path = result.path
27
+ return path[1:]
28
+
29
+
30
  def translate_llama2(text):
31
  "Translates llama-2 to its hf counterpart"
32
  if not text.endswith("-hf"):