28
28
from transformers import GenerationConfig , PretrainedConfig
29
29
from transformers .file_utils import add_start_docstrings
30
30
from transformers .generation import GenerationMixin
31
+ from transformers .utils import is_offline_mode
31
32
32
33
from optimum .exporters .onnx import OnnxConfig
33
- from optimum .modeling_base import OptimizedModel
34
+ from optimum .modeling_base import FROM_PRETRAINED_START_DOCSTRING , OptimizedModel
34
35
35
36
from ...exporters .openvino import export , main_export
36
37
from ..utils .import_utils import is_nncf_available
38
+ from ..utils .modeling_utils import _find_files_matching_pattern
37
39
from .configuration import OVConfig , OVDynamicQuantizationConfig , OVWeightQuantizationConfig
38
40
from .utils import ONNX_WEIGHTS_NAME , OV_TO_PT_TYPE , OV_XML_FILE_NAME , _print_compiled_model_properties
39
41
@@ -220,7 +222,6 @@ def _from_pretrained(
220
222
cls ,
221
223
model_id : Union [str , Path ],
222
224
config : PretrainedConfig ,
223
- use_auth_token : Optional [Union [bool , str ]] = None ,
224
225
token : Optional [Union [bool , str ]] = None ,
225
226
revision : Optional [str ] = None ,
226
227
force_download : bool = False ,
@@ -242,8 +243,6 @@ def _from_pretrained(
242
243
Can be either:
243
244
- The model id of a pretrained model hosted inside a model repo on huggingface.co.
244
245
- 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.
247
246
token (Optional[Union[bool, str]], defaults to `None`):
248
247
The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
249
248
when running `huggingface-cli login` (stored in `~/.huggingface`).
@@ -263,15 +262,6 @@ def _from_pretrained(
263
262
load_in_8bit (`bool`, *optional*, defaults to `False`):
264
263
Whether or not to apply 8-bit weight quantization.
265
264
"""
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
-
275
265
model_path = Path (model_id )
276
266
default_file_name = ONNX_WEIGHTS_NAME if from_onnx else OV_XML_FILE_NAME
277
267
file_name = file_name or default_file_name
@@ -312,6 +302,87 @@ def _from_pretrained(
312
302
** kwargs ,
313
303
)
314
304
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
+
315
386
@staticmethod
316
387
def _prepare_weight_quantization_config (
317
388
quantization_config : Optional [Union [OVWeightQuantizationConfig , Dict ]] = None , load_in_8bit : bool = False
@@ -337,7 +408,6 @@ def _set_ov_config_parameters(self):
337
408
@staticmethod
338
409
def _cached_file (
339
410
model_path : Union [Path , str ],
340
- use_auth_token : Optional [Union [bool , str ]] = None ,
341
411
token : Optional [Union [bool , str ]] = None ,
342
412
revision : Optional [str ] = None ,
343
413
force_download : bool = False ,
@@ -346,15 +416,6 @@ def _cached_file(
346
416
subfolder : str = "" ,
347
417
local_files_only : bool = False ,
348
418
):
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
-
358
419
# locates a file in a local folder and repo, downloads and cache it if necessary.
359
420
model_path = Path (model_path )
360
421
if model_path .is_dir ():
@@ -385,7 +446,6 @@ def _from_transformers(
385
446
cls ,
386
447
model_id : str ,
387
448
config : PretrainedConfig ,
388
- use_auth_token : Optional [Union [bool , str ]] = None ,
389
449
token : Optional [Union [bool , str ]] = None ,
390
450
revision : Optional [str ] = None ,
391
451
force_download : bool = False ,
@@ -409,8 +469,6 @@ def _from_transformers(
409
469
- The path to a directory containing the model weights. save_dir (`str` or `Path`):
410
470
The directory where the exported ONNX model should be saved, default to
411
471
`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.
414
472
token (Optional[Union[bool, str]], defaults to `None`):
415
473
The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
416
474
when running `huggingface-cli login` (stored in `~/.huggingface`).
@@ -419,15 +477,6 @@ def _from_transformers(
419
477
kwargs (`Dict`, *optional*):
420
478
kwargs will be passed to the model during initialization
421
479
"""
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
-
431
480
save_dir = TemporaryDirectory ()
432
481
save_dir_path = Path (save_dir .name )
433
482
# This attribute is needed to keep one reference on the temporary directory, since garbage collecting
@@ -469,7 +518,6 @@ def _to_load(
469
518
model ,
470
519
config : PretrainedConfig ,
471
520
onnx_config : OnnxConfig ,
472
- use_auth_token : Optional [Union [bool , str ]] = None ,
473
521
token : Optional [Union [bool , str ]] = None ,
474
522
revision : Optional [str ] = None ,
475
523
force_download : bool = False ,
@@ -478,15 +526,6 @@ def _to_load(
478
526
stateful : bool = False ,
479
527
** kwargs ,
480
528
):
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
-
490
529
save_dir = TemporaryDirectory ()
491
530
save_dir_path = Path (save_dir .name )
492
531
0 commit comments