Skip to content

Commit 3586b5b

Browse files
authored
Infer if the model needs to be exported (huggingface#825)
* Infer if the model needs to be exported * add test * fix test revision * remove infer export in pipeline * apply comments * fix offline mode
1 parent 40194a0 commit 3586b5b

12 files changed

+176
-181
lines changed

docs/source/openvino/reference.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ limitations under the License.
1919
## Generic model classes
2020

2121
[[autodoc]] openvino.modeling_base.OVBaseModel
22-
- _from_pretrained
22+
- from_pretrained
2323
- reshape
2424

2525
## Natural Language Processing

docs/source/openvino/tutorials/diffusers.mdx

+3-7
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,14 @@ To further speed up inference, the model can be statically reshaped :
5050

5151
```python
5252
# Define the shapes related to the inputs and desired outputs
53-
batch_size = 1
54-
num_images_per_prompt = 1
55-
height = 512
56-
width = 512
57-
53+
batch_size, num_images, height, width = 1, 1, 512, 512
5854
# Statically reshape the model
59-
pipeline.reshape(batch_size=batch_size, height=height, width=width, num_images_per_prompt=num_images_per_prompt)
55+
pipeline.reshape(batch_size=batch_size, height=height, width=width, num_images_per_prompt=num_images)
6056
# Compile the model before the first inference
6157
pipeline.compile()
6258

6359
# Run inference
64-
images = pipeline(prompt, height=height, width=width, num_images_per_prompt=num_images_per_prompt).images
60+
images = pipeline(prompt, height=height, width=width, num_images_per_prompt=num_images).images
6561
```
6662

6763
In case you want to change any parameters such as the outputs height or width, you'll need to statically reshape your model once again.

optimum/intel/openvino/modeling.py

-21
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import logging
1616
import os
17-
import warnings
1817
from pathlib import Path
1918
from tempfile import TemporaryDirectory
2019
from typing import Dict, Optional, Union
@@ -417,7 +416,6 @@ def _from_transformers(
417416
cls,
418417
model_id: str,
419418
config: PretrainedConfig,
420-
use_auth_token: Optional[Union[bool, str]] = None,
421419
token: Optional[Union[bool, str]] = None,
422420
revision: Optional[str] = None,
423421
force_download: bool = False,
@@ -430,15 +428,6 @@ def _from_transformers(
430428
quantization_config: Union[OVWeightQuantizationConfig, Dict] = None,
431429
**kwargs,
432430
):
433-
if use_auth_token is not None:
434-
warnings.warn(
435-
"The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.",
436-
FutureWarning,
437-
)
438-
if token is not None:
439-
raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.")
440-
token = use_auth_token
441-
442431
save_dir = TemporaryDirectory()
443432
save_dir_path = Path(save_dir.name)
444433
# This attribute is needed to keep one reference on the temporary directory, since garbage collecting
@@ -591,7 +580,6 @@ def from_pretrained(
591580
model_id: Union[str, Path],
592581
export: bool = False,
593582
config: Optional["PretrainedConfig"] = None,
594-
use_auth_token: Optional[Union[bool, str]] = None,
595583
token: Optional[Union[bool, str]] = None,
596584
revision: Optional[str] = None,
597585
force_download: bool = False,
@@ -602,15 +590,6 @@ def from_pretrained(
602590
trust_remote_code: bool = False,
603591
**kwargs,
604592
):
605-
if use_auth_token is not None:
606-
warnings.warn(
607-
"The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.",
608-
FutureWarning,
609-
)
610-
if token is not None:
611-
raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.")
612-
token = use_auth_token
613-
614593
# Fix the mismatch between timm_config and huggingface_config
615594
local_timm_model = _is_timm_ov_dir(model_id)
616595
if local_timm_model or (not os.path.isdir(model_id) and model_info(model_id).library_name == "timm"):

optimum/intel/openvino/modeling_base.py

+84-45
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
from transformers import GenerationConfig, PretrainedConfig
2929
from transformers.file_utils import add_start_docstrings
3030
from transformers.generation import GenerationMixin
31+
from transformers.utils import is_offline_mode
3132

3233
from optimum.exporters.onnx import OnnxConfig
33-
from optimum.modeling_base import OptimizedModel
34+
from optimum.modeling_base import FROM_PRETRAINED_START_DOCSTRING, OptimizedModel
3435

3536
from ...exporters.openvino import export, main_export
3637
from ..utils.import_utils import is_nncf_available
38+
from ..utils.modeling_utils import _find_files_matching_pattern
3739
from .configuration import OVConfig, OVDynamicQuantizationConfig, OVWeightQuantizationConfig
3840
from .utils import ONNX_WEIGHTS_NAME, OV_TO_PT_TYPE, OV_XML_FILE_NAME, _print_compiled_model_properties
3941

@@ -220,7 +222,6 @@ def _from_pretrained(
220222
cls,
221223
model_id: Union[str, Path],
222224
config: PretrainedConfig,
223-
use_auth_token: Optional[Union[bool, str]] = None,
224225
token: Optional[Union[bool, str]] = None,
225226
revision: Optional[str] = None,
226227
force_download: bool = False,
@@ -242,8 +243,6 @@ def _from_pretrained(
242243
Can be either:
243244
- The model id of a pretrained model hosted inside a model repo on huggingface.co.
244245
- The path to a directory containing the model weights.
245-
use_auth_token (Optional[Union[bool, str]], defaults to `None`):
246-
Deprecated. Please use `token` instead.
247246
token (Optional[Union[bool, str]], defaults to `None`):
248247
The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
249248
when running `huggingface-cli login` (stored in `~/.huggingface`).
@@ -263,15 +262,6 @@ def _from_pretrained(
263262
load_in_8bit (`bool`, *optional*, defaults to `False`):
264263
Whether or not to apply 8-bit weight quantization.
265264
"""
266-
if use_auth_token is not None:
267-
warnings.warn(
268-
"The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.",
269-
FutureWarning,
270-
)
271-
if token is not None:
272-
raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.")
273-
token = use_auth_token
274-
275265
model_path = Path(model_id)
276266
default_file_name = ONNX_WEIGHTS_NAME if from_onnx else OV_XML_FILE_NAME
277267
file_name = file_name or default_file_name
@@ -312,6 +302,87 @@ def _from_pretrained(
312302
**kwargs,
313303
)
314304

305+
@classmethod
306+
@add_start_docstrings(FROM_PRETRAINED_START_DOCSTRING)
307+
def from_pretrained(
308+
cls,
309+
model_id: Union[str, Path],
310+
export: bool = False,
311+
force_download: bool = False,
312+
use_auth_token: Optional[Union[bool, str]] = None,
313+
token: Optional[Union[bool, str]] = None,
314+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
315+
subfolder: str = "",
316+
config: Optional[PretrainedConfig] = None,
317+
local_files_only: bool = False,
318+
trust_remote_code: bool = False,
319+
revision: Optional[str] = None,
320+
**kwargs,
321+
):
322+
if use_auth_token is not None:
323+
warnings.warn(
324+
"The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.",
325+
FutureWarning,
326+
)
327+
if token is not None:
328+
raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.")
329+
token = use_auth_token
330+
331+
if is_offline_mode() and not local_files_only:
332+
logger.info("Offline mode: forcing local_files_only=True")
333+
local_files_only = True
334+
335+
_export = export
336+
try:
337+
if local_files_only:
338+
object_id = model_id.replace("/", "--")
339+
cached_model_dir = os.path.join(cache_dir, f"models--{object_id}")
340+
refs_file = os.path.join(os.path.join(cached_model_dir, "refs"), revision or "main")
341+
with open(refs_file) as f:
342+
revision = f.read()
343+
model_dir = os.path.join(cached_model_dir, "snapshots", revision)
344+
else:
345+
model_dir = model_id
346+
347+
ov_files = _find_files_matching_pattern(
348+
model_dir,
349+
pattern=r"(.*)?openvino(.*)?\_model.xml",
350+
subfolder=subfolder,
351+
use_auth_token=token,
352+
revision=revision,
353+
)
354+
_export = len(ov_files) == 0
355+
if _export ^ export:
356+
if export:
357+
logger.warning(
358+
f"The model {model_id} was already converted to the OpenVINO IR but got `export=True`, the model will be converted to OpenVINO once again. "
359+
"Don't forget to save the resulting model with `.save_pretrained()`"
360+
)
361+
_export = True
362+
else:
363+
logger.warning(
364+
f"No OpenVINO files were found for {model_id}, setting `export=True` to convert the model to the OpenVINO IR. "
365+
"Don't forget to save the resulting model with `.save_pretrained()`"
366+
)
367+
except Exception as exception:
368+
logger.warning(
369+
f"Could not infer whether the model was already converted or not to the OpenVINO IR, keeping `export={export}`.\n{exception}"
370+
)
371+
372+
return super().from_pretrained(
373+
model_id,
374+
export=_export,
375+
force_download=force_download,
376+
token=token,
377+
cache_dir=cache_dir,
378+
subfolder=subfolder,
379+
config=config,
380+
local_files_only=local_files_only,
381+
trust_remote_code=trust_remote_code,
382+
revision=revision,
383+
**kwargs,
384+
)
385+
315386
@staticmethod
316387
def _prepare_weight_quantization_config(
317388
quantization_config: Optional[Union[OVWeightQuantizationConfig, Dict]] = None, load_in_8bit: bool = False
@@ -337,7 +408,6 @@ def _set_ov_config_parameters(self):
337408
@staticmethod
338409
def _cached_file(
339410
model_path: Union[Path, str],
340-
use_auth_token: Optional[Union[bool, str]] = None,
341411
token: Optional[Union[bool, str]] = None,
342412
revision: Optional[str] = None,
343413
force_download: bool = False,
@@ -346,15 +416,6 @@ def _cached_file(
346416
subfolder: str = "",
347417
local_files_only: bool = False,
348418
):
349-
if use_auth_token is not None:
350-
warnings.warn(
351-
"The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.",
352-
FutureWarning,
353-
)
354-
if token is not None:
355-
raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.")
356-
token = use_auth_token
357-
358419
# locates a file in a local folder and repo, downloads and cache it if necessary.
359420
model_path = Path(model_path)
360421
if model_path.is_dir():
@@ -385,7 +446,6 @@ def _from_transformers(
385446
cls,
386447
model_id: str,
387448
config: PretrainedConfig,
388-
use_auth_token: Optional[Union[bool, str]] = None,
389449
token: Optional[Union[bool, str]] = None,
390450
revision: Optional[str] = None,
391451
force_download: bool = False,
@@ -409,8 +469,6 @@ def _from_transformers(
409469
- The path to a directory containing the model weights. save_dir (`str` or `Path`):
410470
The directory where the exported ONNX model should be saved, default to
411471
`transformers.file_utils.default_cache_path`, which is the cache directory for transformers.
412-
use_auth_token (`Optional[str]`, defaults to `None`):
413-
Deprecated. Please use `token` instead.
414472
token (Optional[Union[bool, str]], defaults to `None`):
415473
The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
416474
when running `huggingface-cli login` (stored in `~/.huggingface`).
@@ -419,15 +477,6 @@ def _from_transformers(
419477
kwargs (`Dict`, *optional*):
420478
kwargs will be passed to the model during initialization
421479
"""
422-
if use_auth_token is not None:
423-
warnings.warn(
424-
"The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.",
425-
FutureWarning,
426-
)
427-
if token is not None:
428-
raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.")
429-
token = use_auth_token
430-
431480
save_dir = TemporaryDirectory()
432481
save_dir_path = Path(save_dir.name)
433482
# This attribute is needed to keep one reference on the temporary directory, since garbage collecting
@@ -469,7 +518,6 @@ def _to_load(
469518
model,
470519
config: PretrainedConfig,
471520
onnx_config: OnnxConfig,
472-
use_auth_token: Optional[Union[bool, str]] = None,
473521
token: Optional[Union[bool, str]] = None,
474522
revision: Optional[str] = None,
475523
force_download: bool = False,
@@ -478,15 +526,6 @@ def _to_load(
478526
stateful: bool = False,
479527
**kwargs,
480528
):
481-
if use_auth_token is not None:
482-
warnings.warn(
483-
"The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.",
484-
FutureWarning,
485-
)
486-
if token is not None:
487-
raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.")
488-
token = use_auth_token
489-
490529
save_dir = TemporaryDirectory()
491530
save_dir_path = Path(save_dir.name)
492531

optimum/intel/openvino/modeling_base_seq2seq.py

-25
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import logging
1616
import os
17-
import warnings
1817
from pathlib import Path
1918
from tempfile import TemporaryDirectory
2019
from typing import Dict, Optional, Union
@@ -120,7 +119,6 @@ def _from_pretrained(
120119
cls,
121120
model_id: Union[str, Path],
122121
config: PretrainedConfig,
123-
use_auth_token: Optional[Union[bool, str]] = None,
124122
token: Optional[Union[bool, str]] = None,
125123
revision: Optional[str] = None,
126124
force_download: bool = False,
@@ -144,8 +142,6 @@ def _from_pretrained(
144142
Can be either:
145143
- The model id of a pretrained model hosted inside a model repo on huggingface.co.
146144
- The path to a directory containing the model weights.
147-
use_auth_token (Optional[Union[bool, str]], defaults to `None`):
148-
Deprecated. Please use `token` instead.
149145
token (Optional[Union[bool, str]], defaults to `None`):
150146
The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
151147
when running `huggingface-cli login` (stored in `~/.huggingface`).
@@ -169,15 +165,6 @@ def _from_pretrained(
169165
local_files_only(`bool`, *optional*, defaults to `False`):
170166
Whether or not to only look at local files (i.e., do not try to download the model).
171167
"""
172-
if use_auth_token is not None:
173-
warnings.warn(
174-
"The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.",
175-
FutureWarning,
176-
)
177-
if token is not None:
178-
raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.")
179-
token = use_auth_token
180-
181168
default_encoder_file_name = ONNX_ENCODER_NAME if from_onnx else OV_ENCODER_NAME
182169
default_decoder_file_name = ONNX_DECODER_NAME if from_onnx else OV_DECODER_NAME
183170
default_decoder_with_past_file_name = ONNX_DECODER_WITH_PAST_NAME if from_onnx else OV_DECODER_WITH_PAST_NAME
@@ -256,7 +243,6 @@ def _from_transformers(
256243
cls,
257244
model_id: str,
258245
config: PretrainedConfig,
259-
use_auth_token: Optional[Union[bool, str]] = None,
260246
token: Optional[Union[bool, str]] = None,
261247
revision: Optional[str] = None,
262248
force_download: bool = False,
@@ -282,8 +268,6 @@ def _from_transformers(
282268
save_dir (`str` or `Path`):
283269
The directory where the exported ONNX model should be saved, defaults to
284270
`transformers.file_utils.default_cache_path`, which is the cache directory for transformers.
285-
use_auth_token (`Optional[str]`, defaults to `None`):
286-
Deprecated. Please use `token` instead.
287271
token (Optional[Union[bool, str]], defaults to `None`):
288272
The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
289273
when running `huggingface-cli login` (stored in `~/.huggingface`).
@@ -292,15 +276,6 @@ def _from_transformers(
292276
kwargs (`Dict`, *optional*):
293277
kwargs will be passed to the model during initialization
294278
"""
295-
if use_auth_token is not None:
296-
warnings.warn(
297-
"The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.",
298-
FutureWarning,
299-
)
300-
if token is not None:
301-
raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.")
302-
token = use_auth_token
303-
304279
save_dir = TemporaryDirectory()
305280
save_dir_path = Path(save_dir.name)
306281

0 commit comments

Comments
 (0)