justinblalock87 commited on
Commit
08328de
1 Parent(s): dbde0cb
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
__pycache__/app.cpython-38.pyc DELETED
Binary file (2.21 kB)
 
__pycache__/quantize.cpython-38.pyc DELETED
Binary file (3.64 kB)
 
app.py CHANGED
@@ -1,25 +1,14 @@
1
- import os
2
  from typing import Optional
3
  import gradio as gr
4
 
5
  import quantize
6
  from huggingface_hub import HfApi, login
7
 
8
- HF_TOKEN = os.environ.get("HF_TOKEN")
9
-
10
- def run(model_id: str, model_version: str, additional_args: str, is_private: bool, token: Optional[str] = None) -> str:
11
  if model_id == "":
12
- return "Please fill a token and model_id."
13
  login(token=token)
14
- if is_private:
15
- api = HfApi(token=token)
16
- else:
17
- api = HfApi(token=HF_TOKEN)
18
- hf_is_private = api.model_info(repo_id=model_id).private
19
- if is_private and not hf_is_private:
20
- api = HfApi(token=HF_TOKEN)
21
-
22
- print("is_private", is_private)
23
 
24
  quantize.quantize(api=api, model_id=model_id, model_version=model_version, additional_args=additional_args)
25
 
@@ -29,10 +18,6 @@ Simple utility tool to quantize diffusion models and convert them to CoreML.
29
  """
30
 
31
  title="Quantize model and convert to CoreML"
32
- allow_flagging="never"
33
-
34
- def token_text(visible=False):
35
- return gr.Text(max_lines=1, label="your_hf_token", visible=True)
36
 
37
  with gr.Blocks(title=title) as demo:
38
  description = gr.Markdown(f"""# {title}""")
@@ -40,11 +25,10 @@ with gr.Blocks(title=title) as demo:
40
 
41
  with gr.Row() as r:
42
  with gr.Column() as c:
43
- model_id = gr.Text(max_lines=1, label="model_id", value="jblalock30/coreml")
44
- model_version = gr.Text(max_lines=1, label="model_version", value="stabilityai/sd-turbo")
45
- additional_args = gr.Text(max_lines=1, label="additional_args", value="--quantize-nbits 2 --convert-unet --convert-text-encoder --convert-vae-decoder --chunk-unet --attention-implementation ORIGINAL")
46
- is_private = gr.Checkbox(label="Private model")
47
- token = token_text()
48
  with gr.Row() as c:
49
  clean = gr.ClearButton()
50
  submit = gr.Button("Submit", variant="primary")
@@ -52,7 +36,6 @@ with gr.Blocks(title=title) as demo:
52
  with gr.Column() as d:
53
  output = gr.Markdown()
54
 
55
- is_private.change(lambda s: token_text(s), inputs=is_private, outputs=token)
56
- submit.click(run, inputs=[model_id, model_version, additional_args, is_private, token], outputs=output, concurrency_limit=1)
57
 
58
  demo.queue(max_size=10).launch(show_api=True)
 
 
1
  from typing import Optional
2
  import gradio as gr
3
 
4
  import quantize
5
  from huggingface_hub import HfApi, login
6
 
7
+ def run(model_id: str, model_version: str, additional_args: str, token: Optional[str] = None) -> str:
 
 
8
  if model_id == "":
9
+ return "Please enter model_id."
10
  login(token=token)
11
+ api = HfApi(token=token)
 
 
 
 
 
 
 
 
12
 
13
  quantize.quantize(api=api, model_id=model_id, model_version=model_version, additional_args=additional_args)
14
 
 
18
  """
19
 
20
  title="Quantize model and convert to CoreML"
 
 
 
 
21
 
22
  with gr.Blocks(title=title) as demo:
23
  description = gr.Markdown(f"""# {title}""")
 
25
 
26
  with gr.Row() as r:
27
  with gr.Column() as c:
28
+ model_id = gr.Text(max_lines=1, label="ID of output repo")
29
+ model_version = gr.Text(max_lines=1, label="Version of model to convert", value="stabilityai/sd-turbo")
30
+ additional_args = gr.Text(max_lines=1, label="Additional Args (optional)")
31
+ token = gr.Text(max_lines=1, label="Your HuggingFace write token")
 
32
  with gr.Row() as c:
33
  clean = gr.ClearButton()
34
  submit = gr.Button("Submit", variant="primary")
 
36
  with gr.Column() as d:
37
  output = gr.Markdown()
38
 
39
+ submit.click(run, inputs=[model_id, model_version, additional_args, token], outputs=output, concurrency_limit=1)
 
40
 
41
  demo.queue(max_size=10).launch(show_api=True)
quantize.py CHANGED
@@ -1,28 +1,26 @@
1
- import argparse
2
- import json
3
  import os
4
  import shutil
5
- from collections import defaultdict
6
  from tempfile import TemporaryDirectory
7
- from typing import Dict, List, Optional, Set, Tuple
8
  import subprocess
9
-
10
- import torch
11
-
12
- from huggingface_hub import CommitInfo, CommitOperationAdd, Discussion, HfApi, hf_hub_download
13
  from huggingface_hub.file_download import repo_folder_name
14
- from safetensors.torch import _find_shared_tensors, _is_complete, load_file, save_file
15
 
16
  ConversionResult = Tuple[List["CommitOperationAdd"], List[Tuple[str, "Exception"]]]
17
 
18
- def convert_generic(
19
  model_id: str, folder: str, token: Optional[str], model_version: str, additional_args: str
20
  ) -> ConversionResult:
21
 
22
  command = ["python3", "-m" , "python_coreml_stable_diffusion.torch2coreml", "--model-version", model_version, "-o", folder]
 
 
 
 
 
23
  command.extend(additional_args.split(" "))
24
 
25
- print("Starting conversion")
26
 
27
  subprocess.run(command)
28
 
@@ -44,62 +42,6 @@ def quantize(
44
  folder = os.path.join(d, repo_folder_name(repo_id=model_id, repo_type="models"))
45
  os.makedirs(folder)
46
  try:
47
- convert_generic(model_id, folder, token=api.token, model_version=model_version, additional_args=additional_args)
48
  finally:
49
- shutil.rmtree(folder)
50
-
51
-
52
- if __name__ == "__main__":
53
- DESCRIPTION = """
54
- Simple utility tool to convert automatically some weights on the hub to `safetensors` format.
55
- It is PyTorch exclusive for now.
56
- It works by downloading the weights (PT), converting them locally, and uploading them back
57
- as a PR on the hub.
58
- """
59
- parser = argparse.ArgumentParser(description=DESCRIPTION)
60
- parser.add_argument(
61
- "model_id",
62
- type=str,
63
- help="The name of the model on the hub to convert. E.g. `gpt2` or `facebook/wav2vec2-base-960h`",
64
- )
65
- parser.add_argument(
66
- "--revision",
67
- type=str,
68
- help="The revision to convert",
69
- )
70
- parser.add_argument(
71
- "--force",
72
- action="store_true",
73
- help="Create the PR even if it already exists of if the model was already converted.",
74
- )
75
- parser.add_argument(
76
- "-y",
77
- action="store_true",
78
- help="Ignore safety prompt",
79
- )
80
- args = parser.parse_args()
81
- model_id = args.model_id
82
- api = HfApi()
83
- if args.y:
84
- txt = "y"
85
- else:
86
- txt = input(
87
- "This conversion script will unpickle a pickled file, which is inherently unsafe. If you do not trust this file, we invite you to use"
88
- " https://huggingface.co/spaces/safetensors/convert or google colab or other hosted solution to avoid potential issues with this file."
89
- " Continue [Y/n] ?"
90
- )
91
- if txt.lower() in {"", "y"}:
92
- commit_info, errors = convert(api, model_id, revision=args.revision, force=args.force)
93
- string = f"""
94
- ### Success 🔥
95
- Yay! This model was successfully converted and a PR was open using your token, here:
96
- [{commit_info.pr_url}]({commit_info.pr_url})
97
- """
98
- if errors:
99
- string += "\nErrors during conversion:\n"
100
- string += "\n".join(
101
- f"Error while converting {filename}: {e}, skipped conversion" for filename, e in errors
102
- )
103
- print(string)
104
- else:
105
- print(f"Answer was `{txt}` aborting.")
 
 
 
1
  import os
2
  import shutil
 
3
  from tempfile import TemporaryDirectory
4
+ from typing import List, Optional, Tuple
5
  import subprocess
6
+ from huggingface_hub import CommitOperationAdd, HfApi
 
 
 
7
  from huggingface_hub.file_download import repo_folder_name
 
8
 
9
  ConversionResult = Tuple[List["CommitOperationAdd"], List[Tuple[str, "Exception"]]]
10
 
11
+ def convert_to_core_ml(
12
  model_id: str, folder: str, token: Optional[str], model_version: str, additional_args: str
13
  ) -> ConversionResult:
14
 
15
  command = ["python3", "-m" , "python_coreml_stable_diffusion.torch2coreml", "--model-version", model_version, "-o", folder]
16
+ additional_args = additional_args
17
+ if additional_args == "":
18
+ # Set default args
19
+ additional_args = f"--convert-unet --convert-text-encoder --convert-vae-decoder --attention-implementation SPLIT_EINSUM --quantize-nbits 6"
20
+
21
  command.extend(additional_args.split(" "))
22
 
23
+ print("Starting conversion: ", command)
24
 
25
  subprocess.run(command)
26
 
 
42
  folder = os.path.join(d, repo_folder_name(repo_id=model_id, repo_type="models"))
43
  os.makedirs(folder)
44
  try:
45
+ convert_to_core_ml(model_id, folder, token=api.token, model_version=model_version, additional_args=additional_args)
46
  finally:
47
+ shutil.rmtree(folder)