-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathmodeling_base.py
474 lines (419 loc) · 18.4 KB
/
modeling_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os
from pathlib import Path
from tempfile import TemporaryDirectory, gettempdir
from typing import Dict, Optional, Union
import openvino
from huggingface_hub import hf_hub_download
from openvino import Core, convert_model
from openvino._offline_transformations import apply_moc_transformations, compress_model_transformation
from transformers import GenerationConfig, PretrainedConfig
from transformers.file_utils import add_start_docstrings
from transformers.generation import GenerationMixin
from optimum.exporters.onnx import OnnxConfig
from optimum.modeling_base import OptimizedModel
from ...exporters.openvino import export, main_export
from ..utils.import_utils import is_nncf_available
from .configuration import OVConfig, OVWeightQuantizationConfig
from .utils import ONNX_WEIGHTS_NAME, OV_XML_FILE_NAME, _print_compiled_model_properties
core = Core()
logger = logging.getLogger(__name__)
@add_start_docstrings(
"""
Base OVModel class.
""",
)
class OVBaseModel(OptimizedModel):
auto_model_class = None
export_feature = None
def __init__(
self,
model: openvino.runtime.Model,
config: PretrainedConfig = None,
device: str = "CPU",
dynamic_shapes: bool = True,
ov_config: Optional[Dict[str, str]] = None,
model_save_dir: Optional[Union[str, Path, TemporaryDirectory]] = None,
quantization_config: Optional[Union[OVWeightQuantizationConfig, Dict]] = None,
**kwargs,
):
self.config = config
self.model_save_dir = model_save_dir
self._device = device.upper()
self.is_dynamic = dynamic_shapes
self.ov_config = ov_config if ov_config is not None else {}
if self.ov_config.get("PERFORMANCE_HINT") is None:
self.ov_config["PERFORMANCE_HINT"] = "LATENCY"
self.preprocessors = kwargs.get("preprocessors", [])
enable_compilation = kwargs.get("compile", True)
if self.is_dynamic:
height = -1 if self.export_feature == "image-classification" else None
width = -1 if self.export_feature == "image-classification" else None
model = self._reshape(model, -1, -1, height, width)
input_names = {}
for idx, key in enumerate(model.inputs):
names = tuple(key.get_names())
input_names[next((name for name in names if "/" not in name), names[0])] = idx
self.input_names = input_names
output_names = {}
for idx, key in enumerate(model.outputs):
names = tuple(key.get_names())
output_names[next((name for name in names if "/" not in name), names[0])] = idx
self.output_names = output_names
self.model = model
self.request = None
if enable_compilation:
self.compile()
self.generation_config = GenerationConfig.from_model_config(config) if self.can_generate() else None
self._openvino_config = None
if quantization_config:
self._openvino_config = OVConfig(quantization_config=quantization_config)
@staticmethod
def load_model(
file_name: Union[str, Path],
quantization_config: Union[OVWeightQuantizationConfig, Dict] = None,
calibration_dataset: Optional = None,
):
"""
Loads the model.
Arguments:
file_name (`str` or `Path`):
The path of the model ONNX or XML file.
quantization_config (`OVWeightQuantizationConfig` or `Dict`, *optional*):
Quantization config to apply after model is loaded.
calibration_dataset (`nncf.Dataset`, *optional*):
Optional nncf.Dataset to feed to model weight compression when quantization config is provided.
"""
def fix_op_names_duplicates(model: openvino.runtime.Model):
names = set()
for op in model.get_ops():
friendly_name = op.get_friendly_name()
while True:
if friendly_name not in names:
break
friendly_name += "_"
names.add(friendly_name)
op.set_friendly_name(friendly_name)
return model
if isinstance(file_name, str):
file_name = Path(file_name)
model = core.read_model(file_name) if not file_name.suffix == ".onnx" else convert_model(file_name)
if file_name.suffix == ".onnx":
model = fix_op_names_duplicates(model) # should be called during model conversion to IR
if quantization_config:
if not is_nncf_available():
raise ImportError(
"Quantization of the weights to int8 requires nncf, please install it with `pip install nncf`"
)
from optimum.intel.openvino.quantization import _weight_only_quantization
model = _weight_only_quantization(model, quantization_config, calibration_dataset=calibration_dataset)
return model
def _save_pretrained(self, save_directory: Union[str, Path]):
"""
Saves the model to the OpenVINO IR format so that it can be re-loaded using the
[`~optimum.intel.openvino.modeling.OVModel.from_pretrained`] class method.
Arguments:
save_directory (`str` or `Path`):
The directory where to save the model files.
"""
dst_path = os.path.join(save_directory, OV_XML_FILE_NAME)
openvino.save_model(self.model, dst_path, compress_to_fp16=False)
self._save_openvino_config(save_directory)
def _save_openvino_config(self, save_directory: Union[str, Path]):
if self._openvino_config is not None:
if not isinstance(self._openvino_config.quantization_config.dataset, (str, type(None))):
self._openvino_config.quantization_config.dataset = None
self._openvino_config.save_pretrained(save_directory)
@classmethod
def _from_pretrained(
cls,
model_id: Union[str, Path],
config: PretrainedConfig,
use_auth_token: Optional[Union[bool, str, None]] = None,
revision: Optional[Union[str, None]] = None,
force_download: bool = False,
cache_dir: Optional[str] = None,
file_name: Optional[str] = None,
subfolder: str = "",
from_onnx: bool = False,
local_files_only: bool = False,
load_in_8bit: bool = False,
quantization_config: Union[OVWeightQuantizationConfig, Dict] = None,
**kwargs,
):
"""
Loads a model and its configuration file from a directory or the HF Hub.
Arguments:
model_id (`str` or `Path`):
The directory from which to load the model.
Can be either:
- The model id of a pretrained model hosted inside a model repo on huggingface.co.
- The path to a directory containing the model weights.
use_auth_token (`str` or `bool`):
The token to use as HTTP bearer authorization for remote files. Needed to load models from a private
repository.
revision (`str`, *optional*):
The specific model version to use. It can be a branch name, a tag name, or a commit id.
cache_dir (`Union[str, Path]`, *optional*):
The path to a directory in which a downloaded pretrained model configuration should be cached if the
standard cache should not be used.
force_download (`bool`, defaults to `False`):
Whether or not to force the (re-)download of the model weights and configuration files, overriding the
cached versions if they exist.
file_name (`str`, *optional*):
The file name of the model to load. Overwrites the default file name and allows one to load the model
with a different name.
local_files_only (`bool`, *optional*, defaults to `False`):
Whether or not to only look at local files (i.e., do not try to download the model).
load_in_8bit (`bool`, *optional*, defaults to `False`):
Whether or not to apply 8-bit weight quantization.
"""
model_path = Path(model_id)
default_file_name = ONNX_WEIGHTS_NAME if from_onnx else OV_XML_FILE_NAME
file_name = file_name or default_file_name
model_cache_path = cls._cached_file(
model_path=model_path,
use_auth_token=use_auth_token,
revision=revision,
force_download=force_download,
cache_dir=cache_dir,
file_name=file_name,
subfolder=subfolder,
local_files_only=local_files_only,
)
quantization_config = cls._prepare_weight_quantization_config(quantization_config, load_in_8bit)
model = cls.load_model(model_cache_path, quantization_config=quantization_config)
return cls(
model,
config=config,
model_save_dir=model_cache_path.parent,
quantization_config=quantization_config,
**kwargs,
)
@staticmethod
def _prepare_weight_quantization_config(
quantization_config: Optional[Union[OVWeightQuantizationConfig, Dict]] = None, load_in_8bit: bool = False
):
# Give default quantization config if not provided and load_in_8bit=True
if not quantization_config and load_in_8bit:
quantization_config = OVWeightQuantizationConfig(bits=8)
elif isinstance(quantization_config, dict):
quantization_config = OVWeightQuantizationConfig.from_dict(quantization_config)
return quantization_config
@staticmethod
def _cached_file(
model_path: Union[Path, str],
use_auth_token: Optional[Union[bool, str]] = None,
revision: Optional[str] = None,
force_download: bool = False,
cache_dir: Optional[str] = None,
file_name: Optional[str] = None,
subfolder: str = "",
local_files_only: bool = False,
):
# locates a file in a local folder and repo, downloads and cache it if necessary.
model_path = Path(model_path)
if model_path.is_dir():
model_cache_path = model_path / file_name
else:
file_name = Path(file_name)
if file_name.suffix != ".onnx":
model_file_names = [file_name.with_suffix(".bin"), file_name]
else:
model_file_names = [file_name]
for file_name in model_file_names:
model_cache_path = hf_hub_download(
repo_id=model_path.as_posix(),
filename=file_name.as_posix(),
subfolder=subfolder,
use_auth_token=use_auth_token,
revision=revision,
cache_dir=cache_dir,
force_download=force_download,
local_files_only=local_files_only,
)
model_cache_path = Path(model_cache_path)
return model_cache_path
@classmethod
def _from_transformers(
cls,
model_id: str,
config: PretrainedConfig,
use_auth_token: Optional[Union[bool, str]] = None,
revision: Optional[str] = None,
force_download: bool = False,
cache_dir: Optional[str] = None,
subfolder: str = "",
local_files_only: bool = False,
task: Optional[str] = None,
trust_remote_code: bool = False,
load_in_8bit: Optional[bool] = None,
quantization_config: Union[OVWeightQuantizationConfig, Dict] = None,
**kwargs,
):
"""
Export a vanilla Transformers model into an ONNX model using `transformers.onnx.export_onnx`.
Arguments:
model_id (`str` or `Path`):
The directory from which to load the model.
Can be either:
- The model id of a pretrained model hosted inside a model repo on huggingface.co.
- The path to a directory containing the model weights. save_dir (`str` or `Path`):
The directory where the exported ONNX model should be saved, default to
`transformers.file_utils.default_cache_path`, which is the cache directory for transformers.
use_auth_token (`str` or `bool`):
Is needed to load models from a private repository
revision (`str`):
Revision is the specific model version to use. It can be a branch name, a tag name, or a commit id
kwargs (`Dict`, *optional*):
kwargs will be passed to the model during initialization
"""
save_dir = TemporaryDirectory()
save_dir_path = Path(save_dir.name)
# If load_in_8bit and quantization_config not specified then ov_config is set to None and will be set by default in convert depending on the model size
if load_in_8bit is None and not quantization_config:
ov_config = None
else:
ov_config = OVConfig(dtype="fp32")
main_export(
model_name_or_path=model_id,
output=save_dir_path,
task=task or cls.export_feature,
subfolder=subfolder,
revision=revision,
cache_dir=cache_dir,
use_auth_token=use_auth_token,
local_files_only=local_files_only,
force_download=force_download,
trust_remote_code=trust_remote_code,
ov_config=ov_config,
)
config.save_pretrained(save_dir_path)
return cls._from_pretrained(
model_id=save_dir_path,
config=config,
load_in_8bit=load_in_8bit,
quantization_config=quantization_config,
**kwargs,
)
@classmethod
def _to_load(
cls,
model,
config: PretrainedConfig,
onnx_config: OnnxConfig,
use_auth_token: Optional[Union[bool, str]] = None,
revision: Optional[str] = None,
force_download: bool = False,
cache_dir: Optional[str] = None,
local_files_only: bool = False,
stateful: bool = False,
**kwargs,
):
save_dir = TemporaryDirectory()
save_dir_path = Path(save_dir.name)
# Export the model to the ONNX format
export(
model=model,
config=onnx_config,
opset=onnx_config.DEFAULT_ONNX_OPSET,
output=save_dir_path / OV_XML_FILE_NAME,
stateful=stateful,
)
return cls._from_pretrained(
model_id=save_dir_path,
config=config,
from_onnx=False,
use_auth_token=use_auth_token,
revision=revision,
force_download=force_download,
cache_dir=cache_dir,
local_files_only=local_files_only,
**kwargs,
)
def compile(self):
if self.request is None:
logger.info(f"Compiling the model to {self._device} ...")
ov_config = {**self.ov_config}
if (
"CACHE_DIR" not in self.ov_config.keys()
and not str(self.model_save_dir).startswith(gettempdir())
and "gpu" in self._device.lower()
):
# Set default CACHE_DIR only if it is not set, if the model is not in a temporary directory, and device is GPU
cache_dir = Path(self.model_save_dir).joinpath("model_cache")
ov_config["CACHE_DIR"] = str(cache_dir)
logger.info(f"Setting OpenVINO CACHE_DIR to {str(cache_dir)}")
self.request = core.compile_model(self.model, self._device, ov_config)
# OPENVINO_LOG_LEVEL can be found in https://docs.openvino.ai/2023.2/openvino_docs_OV_UG_supported_plugins_AUTO_debugging.html
if "OPENVINO_LOG_LEVEL" in os.environ and int(os.environ["OPENVINO_LOG_LEVEL"]) > 2:
logger.info(f"{self._device} SUPPORTED_PROPERTIES:")
_print_compiled_model_properties(self.request)
def _reshape(
self,
model: openvino.runtime.Model,
batch_size: int,
sequence_length: int,
height: int = None,
width: int = None,
):
shapes = {}
for inputs in model.inputs:
shapes[inputs] = inputs.get_partial_shape()
shapes[inputs][0] = batch_size
shapes[inputs][1] = sequence_length
if height is not None:
shapes[inputs][2] = height
if width is not None:
shapes[inputs][3] = width
model.reshape(shapes)
return model
def reshape(self, batch_size: int, sequence_length: int, height: int = None, width: int = None):
"""
Propagates the given input shapes on the model's layers, fixing the inputs shapes of the model.
Arguments:
batch_size (`int`):
The batch size.
sequence_length (`int`):
The sequence length or number of channels.
height (`int`, *optional*):
The image height.
width (`int`, *optional*):
The image width.
"""
self.is_dynamic = True if batch_size == -1 and sequence_length == -1 else False
self.model = self._reshape(self.model, batch_size, sequence_length, height, width)
self.request = None
return self
def half(self):
"""
Converts all the model weights to FP16
"""
apply_moc_transformations(self.model, cf=False)
compress_model_transformation(self.model)
self.request = None
return self
def eval(self):
return self
def forward(self, *args, **kwargs):
raise NotImplementedError
def can_generate(self) -> bool:
"""
Returns whether this model can generate sequences with `.generate()`.
"""
if isinstance(self, GenerationMixin):
return True
return False