2828from transformers import GenerationConfig , PretrainedConfig
2929from transformers .file_utils import add_start_docstrings
3030from transformers .generation import GenerationMixin
31+ from transformers .utils import is_offline_mode
3132
3233from optimum .exporters .onnx import OnnxConfig
33- from optimum .modeling_base import OptimizedModel
34+ from optimum .modeling_base import FROM_PRETRAINED_START_DOCSTRING , OptimizedModel
3435
3536from ...exporters .openvino import export , main_export
3637from ..utils .import_utils import is_nncf_available
38+ from ..utils .modeling_utils import _find_files_matching_pattern
3739from .configuration import OVConfig , OVDynamicQuantizationConfig , OVWeightQuantizationConfig
3840from .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
0 commit comments