echarlaix HF staff commited on
Commit
872b151
·
1 Parent(s): 63d1c27

fix quantized model saving

Browse files
Files changed (2) hide show
  1. app.py +31 -18
  2. requirements.txt +7 -3
app.py CHANGED
@@ -32,7 +32,7 @@ from optimum.intel import (
32
  OVWeightQuantizationConfig,
33
  )
34
 
35
- def process_model(
36
  model_id: str,
37
  dtype: str,
38
  calibration_dataset: str,
@@ -49,6 +49,8 @@ def process_model(
49
  new_repo_id = f"{username}/{model_name}-openvino-{dtype}"
50
  task = TasksManager.infer_task_from_model(model_id)
51
  library_name = TasksManager.infer_library_from_model(model_id)
 
 
52
 
53
  if task not in _HEAD_TO_AUTOMODELS:
54
  raise ValueError(
@@ -82,41 +84,52 @@ def process_model(
82
  )
83
 
84
  api = HfApi(token=oauth_token.token)
85
-
86
  if api.repo_exists(new_repo_id) and not overwritte:
87
  raise Exception(f"Model {new_repo_id} already exist, please set overwritte=True to push on an existing repo")
88
 
89
  with TemporaryDirectory() as d:
90
  folder = os.path.join(d, repo_folder_name(repo_id=model_id, repo_type="models"))
91
  os.makedirs(folder)
 
92
  try:
93
  api.snapshot_download(repo_id=model_id, local_dir=folder, allow_patterns=["*.json"])
94
-
95
  ov_model = eval(auto_model_class).from_pretrained(
96
  model_id,
97
  export=export,
98
- quantization_config=quantization_config,
 
 
99
  )
100
  ov_model.save_pretrained(folder)
101
-
102
  new_repo_url = api.create_repo(repo_id=new_repo_id, exist_ok=True, private=private_repo)
103
  new_repo_id = new_repo_url.repo_id
104
  print("Repo created successfully!", new_repo_url)
105
 
106
  folder = Path(folder)
107
- folder_parts = len(folder.parts)
108
- for file_path in folder.glob("**/*"):
109
- name = Path(*file_path.parts[folder_parts:])
110
- if not file_path.is_file() or any(part_name.startswith(".") for part_name in name.parts):
 
 
 
 
 
 
 
 
 
111
  continue
112
- try:
113
- api.upload_file(
114
- path_or_fileobj=file_path,
115
- path_in_repo=str(name),
116
- repo_id=new_repo_id,
117
- )
118
- except Exception as e:
119
- raise Exception(f"Error uploading file {file_path}: {e}")
 
 
120
 
121
  try:
122
  card = ModelCard.load(model_id, token=oauth_token.token)
@@ -220,7 +233,7 @@ overwritte = gr.Checkbox(
220
  info="Push files on existing repo potentially overwriting existing files",
221
  )
222
  interface = gr.Interface(
223
- fn=process_model,
224
  inputs=[
225
  model_id,
226
  dtype,
 
32
  OVWeightQuantizationConfig,
33
  )
34
 
35
+ def quantize_model(
36
  model_id: str,
37
  dtype: str,
38
  calibration_dataset: str,
 
49
  new_repo_id = f"{username}/{model_name}-openvino-{dtype}"
50
  task = TasksManager.infer_task_from_model(model_id)
51
  library_name = TasksManager.infer_library_from_model(model_id)
52
+ # task = TasksManager.infer_task_from_model(model_id, token=oauth_token.token)
53
+ # library_name = TasksManager.infer_library_from_model(model_id, token=oauth_token.token)
54
 
55
  if task not in _HEAD_TO_AUTOMODELS:
56
  raise ValueError(
 
84
  )
85
 
86
  api = HfApi(token=oauth_token.token)
 
87
  if api.repo_exists(new_repo_id) and not overwritte:
88
  raise Exception(f"Model {new_repo_id} already exist, please set overwritte=True to push on an existing repo")
89
 
90
  with TemporaryDirectory() as d:
91
  folder = os.path.join(d, repo_folder_name(repo_id=model_id, repo_type="models"))
92
  os.makedirs(folder)
93
+
94
  try:
95
  api.snapshot_download(repo_id=model_id, local_dir=folder, allow_patterns=["*.json"])
 
96
  ov_model = eval(auto_model_class).from_pretrained(
97
  model_id,
98
  export=export,
99
+ cache_dir=folder,
100
+ token=oauth_token.token,
101
+ quantization_config=quantization_config
102
  )
103
  ov_model.save_pretrained(folder)
 
104
  new_repo_url = api.create_repo(repo_id=new_repo_id, exist_ok=True, private=private_repo)
105
  new_repo_id = new_repo_url.repo_id
106
  print("Repo created successfully!", new_repo_url)
107
 
108
  folder = Path(folder)
109
+ for dir_name in (
110
+ "",
111
+ "vae_encoder",
112
+ "vae_decoder",
113
+ "text_encoder",
114
+ "text_encoder_2",
115
+ "unet",
116
+ "tokenizer",
117
+ "tokenizer_2",
118
+ "scheduler",
119
+ "feature_extractor",
120
+ ):
121
+ if not (folder / dir_name).is_dir():
122
  continue
123
+ for file_path in (folder / dir_name).iterdir():
124
+ if file_path.is_file():
125
+ try:
126
+ api.upload_file(
127
+ path_or_fileobj=file_path,
128
+ path_in_repo=os.path.join(dir_name, file_path.name),
129
+ repo_id=new_repo_id,
130
+ )
131
+ except Exception as e:
132
+ raise Exception(f"Error uploading file {file_path}: {e}")
133
 
134
  try:
135
  card = ModelCard.load(model_id, token=oauth_token.token)
 
233
  info="Push files on existing repo potentially overwriting existing files",
234
  )
235
  interface = gr.Interface(
236
+ fn=quantize_model,
237
  inputs=[
238
  model_id,
239
  dtype,
requirements.txt CHANGED
@@ -1,5 +1,9 @@
1
  huggingface_hub==0.23.4
2
- optimum[diffusers]==1.21.2
3
- optimum-intel[openvino]==1.18.1
4
- gradio[oauth]>=4.28.0
5
  gradio_huggingfacehub_search==0.0.6
 
 
 
 
 
 
 
1
  huggingface_hub==0.23.4
2
+ gradio[oauth]>=4.37.2
 
 
3
  gradio_huggingfacehub_search==0.0.6
4
+ transformers==4.42.4
5
+ diffusers==0.29.1
6
+ optimum==1.21.2
7
+ optimum-intel==1.18.1
8
+ openvino
9
+ nncf