diff --git a/optimum/exporters/openvino/__main__.py b/optimum/exporters/openvino/__main__.py index 67d7cd53bb..8908c430b3 100644 --- a/optimum/exporters/openvino/__main__.py +++ b/optimum/exporters/openvino/__main__.py @@ -13,6 +13,7 @@ # limitations under the License. import logging +import warnings from pathlib import Path from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union @@ -57,6 +58,7 @@ def main_export( force_download: bool = False, local_files_only: bool = False, use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, model_kwargs: Optional[Dict[str, Any]] = None, custom_export_configs: Optional[Dict[str, "OnnxConfig"]] = None, fn_get_submodels: Optional[Callable] = None, @@ -107,9 +109,11 @@ def main_export( cached versions if they exist. local_files_only (`Optional[bool]`, defaults to `False`): Whether or not to only look at local files (i.e., do not try to download the model). - use_auth_token (`Optional[str]`, defaults to `None`): + use_auth_token (Optional[Union[bool, str]], defaults to `None`): + Deprecated. Please use `token` instead. + token (Optional[Union[bool, str]], defaults to `None`): The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated - when running `transformers-cli login` (stored in `~/.huggingface`). + when running `huggingface-cli login` (stored in `~/.huggingface`). model_kwargs (`Optional[Dict[str, Any]]`, defaults to `None`): Experimental usage: keyword arguments to pass to the model during the export. This argument should be used along the `custom_export_configs` argument @@ -138,6 +142,15 @@ def main_export( ``` """ + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + if compression_option is not None: logger.warning( "The `compression_option` argument is deprecated and will be removed in optimum-intel v1.17.0. " @@ -196,7 +209,7 @@ def main_export( subfolder=subfolder, revision=revision, cache_dir=cache_dir, - use_auth_token=use_auth_token, + token=token, local_files_only=local_files_only, force_download=force_download, trust_remote_code=trust_remote_code, @@ -268,7 +281,7 @@ class StoreAttr(object): subfolder=subfolder, revision=revision, cache_dir=cache_dir, - use_auth_token=use_auth_token, + token=token, local_files_only=local_files_only, force_download=force_download, trust_remote_code=trust_remote_code, diff --git a/optimum/intel/generation/modeling.py b/optimum/intel/generation/modeling.py index 76ef245f33..d17e046c3b 100644 --- a/optimum/intel/generation/modeling.py +++ b/optimum/intel/generation/modeling.py @@ -15,6 +15,7 @@ import inspect import logging import os +import warnings from pathlib import Path from tempfile import TemporaryDirectory from typing import Optional, Tuple, Union @@ -354,8 +355,9 @@ 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, + use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, + revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, file_name: Optional[str] = WEIGHTS_NAME, @@ -363,6 +365,15 @@ def _from_pretrained( use_cache: bool = True, **kwargs, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + if not getattr(config, "torchscript", False): raise ValueError("`torchscript` should be set to True to load TorchScript model") @@ -376,7 +387,7 @@ def _from_pretrained( model_cache_path = hf_hub_download( repo_id=model_id, filename=file_name, - use_auth_token=use_auth_token, + token=token, revision=revision, cache_dir=cache_dir, force_download=force_download, @@ -399,6 +410,7 @@ def _from_transformers( model_id: str, config: PretrainedConfig, use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, @@ -408,13 +420,22 @@ def _from_transformers( torch_dtype: Optional[Union[str, "torch.dtype"]] = None, **kwargs, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + if is_torch_version("<", "2.1.0"): raise ImportError("`torch>=2.0.0` is needed to trace your model") task = cls.export_feature model_kwargs = { "revision": revision, - "use_auth_token": use_auth_token, + "token": token, "cache_dir": cache_dir, "subfolder": subfolder, "local_files_only": local_files_only, @@ -436,7 +457,7 @@ def _from_transformers( model_id=save_dir_path, config=config, use_cache=use_cache, - use_auth_token=use_auth_token, + token=token, revision=revision, force_download=force_download, cache_dir=cache_dir, diff --git a/optimum/intel/ipex/modeling_base.py b/optimum/intel/ipex/modeling_base.py index 3961c1f3af..2b739ea502 100644 --- a/optimum/intel/ipex/modeling_base.py +++ b/optimum/intel/ipex/modeling_base.py @@ -15,6 +15,7 @@ import logging import os +import warnings from pathlib import Path from tempfile import TemporaryDirectory from typing import Optional, Tuple, Union @@ -152,6 +153,7 @@ def _from_transformers( config: PretrainedConfig, use_cache: bool = True, use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, @@ -160,13 +162,24 @@ def _from_transformers( torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: bool = False, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.", + FutureWarning, + ) + if token is not None: + raise ValueError( + "Both the arguments `use_auth_token` and `token` were specified, which is not supported. Please specify only `token`." + ) + token = use_auth_token + if is_torch_version("<", "2.1.0"): raise ImportError("`torch>=2.0.0` is needed to trace your model") task = cls.export_feature model_kwargs = { "revision": revision, - "use_auth_token": use_auth_token, + "token": token, "cache_dir": cache_dir, "subfolder": subfolder, "local_files_only": local_files_only, @@ -188,8 +201,9 @@ 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, + use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, + revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, file_name: Optional[str] = WEIGHTS_NAME, @@ -197,6 +211,17 @@ def _from_pretrained( subfolder: str = "", **kwargs, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.", + FutureWarning, + ) + if token is not None: + raise ValueError( + "Both the arguments `use_auth_token` and `token` were specified, which is not supported. Please specify only `token`." + ) + token = use_auth_token + if not getattr(config, "torchscript", False): raise ValueError( "`config.torchscript` should be set to `True`, if your model is not a TorchScript model and needs to be traced please set `export=True` when loading it with `.from_pretrained()`" @@ -211,7 +236,7 @@ def _from_pretrained( model_cache_path = hf_hub_download( repo_id=model_id, filename=file_name, - use_auth_token=use_auth_token, + token=token, revision=revision, cache_dir=cache_dir, force_download=force_download, diff --git a/optimum/intel/neural_compressor/modeling_base.py b/optimum/intel/neural_compressor/modeling_base.py index cee506f690..2556a6048e 100644 --- a/optimum/intel/neural_compressor/modeling_base.py +++ b/optimum/intel/neural_compressor/modeling_base.py @@ -14,6 +14,7 @@ import logging import os +import warnings from pathlib import Path from tempfile import TemporaryDirectory from typing import Dict, Optional, Union @@ -98,8 +99,9 @@ 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, + use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, + revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, file_name: str = WEIGHTS_NAME, @@ -108,6 +110,15 @@ def _from_pretrained( trust_remote_code: bool = False, **kwargs, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + model_name_or_path = kwargs.pop("model_name_or_path", None) if model_name_or_path is not None: logger.warning("`model_name_or_path` is deprecated please use `model_id`") @@ -122,7 +133,7 @@ def _from_pretrained( repo_id=model_id, filename=file_name, subfolder=subfolder, - use_auth_token=use_auth_token, + token=token, revision=revision, cache_dir=cache_dir, force_download=force_download, @@ -145,7 +156,7 @@ def _from_pretrained( return _BaseQBitsAutoModelClass.from_pretrained( pretrained_model_name_or_path=model_id, - use_auth_token=use_auth_token, + token=token, revision=revision, force_download=force_download, cache_dir=cache_dir, diff --git a/optimum/intel/neural_compressor/quantization.py b/optimum/intel/neural_compressor/quantization.py index 090d26e772..9ee4365930 100644 --- a/optimum/intel/neural_compressor/quantization.py +++ b/optimum/intel/neural_compressor/quantization.py @@ -16,6 +16,7 @@ import inspect import logging import types +import warnings from enum import Enum from itertools import chain from pathlib import Path @@ -446,7 +447,8 @@ def get_calibration_dataset( dataset_split: str = "train", preprocess_function: Optional[Callable] = None, preprocess_batch: bool = True, - use_auth_token: bool = False, + use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, ) -> Dataset: """ Create the calibration `datasets.Dataset` to use for the post-training static quantization calibration step. @@ -465,16 +467,28 @@ def get_calibration_dataset( Processing function to apply to each example after loading dataset. preprocess_batch (`bool`, defaults to `True`): Whether the `preprocess_function` should be batched. - use_auth_token (`bool`, defaults to `False`): - Whether to use the token generated when running `transformers-cli login`. + use_auth_token (Optional[Union[bool, str]], defaults to `None`): + Deprecated. Please use `token` instead. + token (Optional[Union[bool, str]], defaults to `None`): + The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated + when running `huggingface-cli login` (stored in `~/.huggingface`). Returns: The calibration `datasets.Dataset` to use for the post-training static quantization calibration step. """ + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + calibration_dataset = load_dataset( dataset_name, name=dataset_config_name, split=dataset_split, - use_auth_token=use_auth_token, + token=token, ) if num_samples is not None: diff --git a/optimum/intel/openvino/loaders.py b/optimum/intel/openvino/loaders.py index 9234036854..fc5ae97495 100644 --- a/optimum/intel/openvino/loaders.py +++ b/optimum/intel/openvino/loaders.py @@ -13,6 +13,7 @@ # limitations under the License. import logging +import warnings from typing import Dict, List, Optional, Union import torch @@ -188,9 +189,11 @@ def load_textual_inversion( local_files_only (`bool`, *optional*, defaults to `False`): Whether to only load local model weights and configuration files or not. If set to `True`, the model won't be downloaded from the Hub. - use_auth_token (`str` or *bool*, *optional*): - The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from - `diffusers-cli login` (stored in `~/.huggingface`) is used. + use_auth_token (Optional[Union[bool, str]], defaults to `None`): + Deprecated. Please use `token` instead. + token (Optional[Union[bool, str]], defaults to `None`): + The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated + when running `huggingface-cli login` (stored in `~/.huggingface`). revision (`str`, *optional*, defaults to `"main"`): The specific model version to use. It can be a branch name, a tag name, a commit id, or any identifier allowed by Git. @@ -258,11 +261,21 @@ def load_textual_inversion( proxies = kwargs.pop("proxies", None) local_files_only = kwargs.pop("local_files_only", HF_HUB_OFFLINE) use_auth_token = kwargs.pop("use_auth_token", None) + token = kwargs.pop("token", None) revision = kwargs.pop("revision", None) subfolder = kwargs.pop("subfolder", None) weight_name = kwargs.pop("weight_name", None) use_safetensors = kwargs.pop("use_safetensors", None) + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + if use_safetensors and not is_safetensors_available(): raise ValueError( "`use_safetensors`=True but safetensors is not installed. Please install safetensors with `pip install safetensors" @@ -319,7 +332,7 @@ def load_textual_inversion( resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, - use_auth_token=use_auth_token, + use_auth_token=token, # still uses use_auth_token revision=revision, subfolder=subfolder, user_agent=user_agent, @@ -340,7 +353,7 @@ def load_textual_inversion( resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, - use_auth_token=use_auth_token, + use_auth_token=token, # still uses use_auth_token revision=revision, subfolder=subfolder, user_agent=user_agent, diff --git a/optimum/intel/openvino/modeling.py b/optimum/intel/openvino/modeling.py index e61148dc3d..1c907f2135 100644 --- a/optimum/intel/openvino/modeling.py +++ b/optimum/intel/openvino/modeling.py @@ -14,6 +14,7 @@ import logging import os +import warnings from pathlib import Path from tempfile import TemporaryDirectory from typing import Dict, Optional, Union @@ -422,6 +423,7 @@ def _from_transformers( model_id: str, config: PretrainedConfig, use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, @@ -433,6 +435,15 @@ def _from_transformers( quantization_config: Union[OVWeightQuantizationConfig, Dict] = None, **kwargs, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + save_dir = TemporaryDirectory() save_dir_path = Path(save_dir.name) @@ -450,7 +461,7 @@ def _from_transformers( subfolder=subfolder, revision=revision, cache_dir=cache_dir, - use_auth_token=use_auth_token, + token=token, local_files_only=local_files_only, force_download=force_download, trust_remote_code=trust_remote_code, @@ -584,6 +595,7 @@ def from_pretrained( export: bool = False, config: Optional["PretrainedConfig"] = None, use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, @@ -593,6 +605,15 @@ def from_pretrained( trust_remote_code: bool = False, **kwargs, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + # Fix the mismatch between timm_config and huggingface_config local_timm_model = _is_timm_ov_dir(model_id) if local_timm_model or (not os.path.isdir(model_id) and model_info(model_id).library_name == "timm"): @@ -621,7 +642,7 @@ def from_pretrained( model_id=model_id, config=config, export=export, - use_auth_token=use_auth_token, + token=token, revision=revision, force_download=force_download, cache_dir=cache_dir, diff --git a/optimum/intel/openvino/modeling_base.py b/optimum/intel/openvino/modeling_base.py index 830db11933..7937deea52 100644 --- a/optimum/intel/openvino/modeling_base.py +++ b/optimum/intel/openvino/modeling_base.py @@ -14,6 +14,7 @@ import logging import os +import warnings from pathlib import Path from tempfile import TemporaryDirectory, gettempdir from typing import Dict, Optional, Union @@ -169,8 +170,9 @@ 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, + use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, + revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, file_name: Optional[str] = None, @@ -190,9 +192,11 @@ def _from_pretrained( 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. + use_auth_token (Optional[Union[bool, str]], defaults to `None`): + Deprecated. Please use `token` instead. + token (Optional[Union[bool, str]], defaults to `None`): + The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated + when running `huggingface-cli login` (stored in `~/.huggingface`). 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*): @@ -209,13 +213,22 @@ def _from_pretrained( load_in_8bit (`bool`, *optional*, defaults to `False`): Whether or not to apply 8-bit weight quantization. """ + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + 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, + token=token, revision=revision, force_download=force_download, cache_dir=cache_dir, @@ -261,6 +274,7 @@ def _set_ov_config_parameters(self): def _cached_file( model_path: Union[Path, str], use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, cache_dir: Optional[str] = None, @@ -268,6 +282,15 @@ def _cached_file( subfolder: str = "", local_files_only: bool = False, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + # 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(): @@ -283,7 +306,7 @@ def _cached_file( repo_id=model_path.as_posix(), filename=file_name.as_posix(), subfolder=subfolder, - use_auth_token=use_auth_token, + token=token, revision=revision, cache_dir=cache_dir, force_download=force_download, @@ -299,6 +322,7 @@ def _from_transformers( model_id: str, config: PretrainedConfig, use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, @@ -321,13 +345,25 @@ def _from_transformers( - 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 + use_auth_token (`Optional[str]`, defaults to `None`): + Deprecated. Please use `token` instead. + token (Optional[Union[bool, str]], defaults to `None`): + The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated + when running `huggingface-cli login` (stored in `~/.huggingface`). 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 """ + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + save_dir = TemporaryDirectory() save_dir_path = Path(save_dir.name) @@ -344,7 +380,7 @@ def _from_transformers( subfolder=subfolder, revision=revision, cache_dir=cache_dir, - use_auth_token=use_auth_token, + token=token, local_files_only=local_files_only, force_download=force_download, trust_remote_code=trust_remote_code, @@ -367,6 +403,7 @@ def _to_load( config: PretrainedConfig, onnx_config: OnnxConfig, use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, @@ -374,6 +411,15 @@ def _to_load( stateful: bool = False, **kwargs, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + save_dir = TemporaryDirectory() save_dir_path = Path(save_dir.name) @@ -390,7 +436,7 @@ def _to_load( model_id=save_dir_path, config=config, from_onnx=False, - use_auth_token=use_auth_token, + token=token, revision=revision, force_download=force_download, cache_dir=cache_dir, diff --git a/optimum/intel/openvino/modeling_base_seq2seq.py b/optimum/intel/openvino/modeling_base_seq2seq.py index 99f6c79286..fb53f9b2e2 100644 --- a/optimum/intel/openvino/modeling_base_seq2seq.py +++ b/optimum/intel/openvino/modeling_base_seq2seq.py @@ -14,6 +14,7 @@ import logging import os +import warnings from pathlib import Path from tempfile import TemporaryDirectory from typing import Dict, Optional, Union @@ -110,6 +111,7 @@ def _from_pretrained( model_id: Union[str, Path], config: PretrainedConfig, use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, @@ -132,9 +134,11 @@ def _from_pretrained( 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. + use_auth_token (Optional[Union[bool, str]], defaults to `None`): + Deprecated. Please use `token` instead. + token (Optional[Union[bool, str]], defaults to `None`): + The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated + when running `huggingface-cli login` (stored in `~/.huggingface`). revision (`str`): The specific model version to use. It can be a branch name, a tag name, or a commit id. force_download (`bool`, *optional*, defaults to `False`): @@ -155,6 +159,15 @@ def _from_pretrained( 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). """ + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + default_encoder_file_name = ONNX_ENCODER_NAME if from_onnx else OV_ENCODER_NAME default_decoder_file_name = ONNX_DECODER_NAME if from_onnx else OV_DECODER_NAME default_decoder_with_past_file_name = ONNX_DECODER_WITH_PAST_NAME if from_onnx else OV_DECODER_WITH_PAST_NAME @@ -191,7 +204,7 @@ def _from_pretrained( model_cache_path = hf_hub_download( repo_id=model_id, filename=file_name, - use_auth_token=use_auth_token, + token=token, revision=revision, cache_dir=cache_dir, force_download=force_download, @@ -221,6 +234,7 @@ def _from_transformers( model_id: str, config: PretrainedConfig, use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, @@ -245,13 +259,25 @@ def _from_transformers( save_dir (`str` or `Path`): The directory where the exported ONNX model should be saved, defaults 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 + use_auth_token (`Optional[str]`, defaults to `None`): + Deprecated. Please use `token` instead. + token (Optional[Union[bool, str]], defaults to `None`): + The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated + when running `huggingface-cli login` (stored in `~/.huggingface`). 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 """ + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + save_dir = TemporaryDirectory() save_dir_path = Path(save_dir.name) @@ -273,7 +299,7 @@ def _from_transformers( subfolder=subfolder, revision=revision, cache_dir=cache_dir, - use_auth_token=use_auth_token, + token=token, local_files_only=local_files_only, force_download=force_download, trust_remote_code=trust_remote_code, diff --git a/optimum/intel/openvino/modeling_decoder.py b/optimum/intel/openvino/modeling_decoder.py index e69792cec3..62a53f1c24 100644 --- a/optimum/intel/openvino/modeling_decoder.py +++ b/optimum/intel/openvino/modeling_decoder.py @@ -14,6 +14,7 @@ import logging import os +import warnings from pathlib import Path from tempfile import TemporaryDirectory from typing import Dict, Optional, Tuple, Union @@ -220,6 +221,7 @@ def _from_transformers( model_id: str, config: PretrainedConfig, use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, @@ -232,6 +234,15 @@ def _from_transformers( quantization_config: Optional[Union[OVWeightQuantizationConfig, Dict]] = None, **kwargs, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + save_dir = TemporaryDirectory() save_dir_path = Path(save_dir.name) @@ -255,7 +266,7 @@ def _from_transformers( subfolder=subfolder, revision=revision, cache_dir=cache_dir, - use_auth_token=use_auth_token, + token=token, local_files_only=local_files_only, force_download=force_download, trust_remote_code=trust_remote_code, @@ -563,7 +574,8 @@ def _from_pretrained( cls, model_id: Union[str, Path], config: PretrainedConfig, - use_auth_token: Optional[Union[bool, str, None]] = None, + use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[Union[str, None]] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, @@ -575,13 +587,22 @@ def _from_pretrained( quantization_config: Optional[Union[OVWeightQuantizationConfig, Dict]] = None, **kwargs, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + 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, + token=token, revision=revision, force_download=force_download, cache_dir=cache_dir, diff --git a/optimum/intel/openvino/modeling_diffusion.py b/optimum/intel/openvino/modeling_diffusion.py index aaf02079d1..2de7cb8154 100644 --- a/optimum/intel/openvino/modeling_diffusion.py +++ b/optimum/intel/openvino/modeling_diffusion.py @@ -16,6 +16,7 @@ import logging import os import shutil +import warnings from copy import deepcopy from pathlib import Path from tempfile import TemporaryDirectory, gettempdir @@ -208,6 +209,7 @@ def _from_pretrained( model_id: Union[str, Path], config: Dict[str, Any], use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, cache_dir: str = HUGGINGFACE_HUB_CACHE, vae_decoder_file_name: Optional[str] = None, @@ -222,6 +224,15 @@ def _from_pretrained( quantization_config: Union[OVWeightQuantizationConfig, Dict] = None, **kwargs, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + default_file_name = ONNX_WEIGHTS_NAME if from_onnx else OV_XML_FILE_NAME vae_decoder_file_name = vae_decoder_file_name or default_file_name text_encoder_file_name = text_encoder_file_name or default_file_name @@ -260,7 +271,7 @@ def _from_pretrained( model_id, cache_dir=cache_dir, local_files_only=local_files_only, - use_auth_token=use_auth_token, + token=token, revision=revision, allow_patterns=allow_patterns, ignore_patterns=ignore_patterns, @@ -399,6 +410,7 @@ def _from_transformers( model_id: str, config: Dict[str, Any], use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, cache_dir: str = HUGGINGFACE_HUB_CACHE, @@ -411,6 +423,15 @@ def _from_transformers( quantization_config: Union[OVWeightQuantizationConfig, Dict] = None, **kwargs, ): + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + save_dir = TemporaryDirectory() save_dir_path = Path(save_dir.name) @@ -428,7 +449,7 @@ def _from_transformers( no_post_process=True, revision=revision, cache_dir=cache_dir, - use_auth_token=use_auth_token, + token=token, local_files_only=local_files_only, force_download=force_download, ov_config=ov_config, @@ -438,7 +459,7 @@ def _from_transformers( model_id=save_dir_path, config=config, from_onnx=False, - use_auth_token=use_auth_token, + token=token, revision=revision, force_download=force_download, cache_dir=cache_dir, diff --git a/optimum/intel/openvino/quantization.py b/optimum/intel/openvino/quantization.py index 124b0366c1..66b30cb045 100644 --- a/optimum/intel/openvino/quantization.py +++ b/optimum/intel/openvino/quantization.py @@ -16,6 +16,7 @@ import inspect import logging import os +import warnings from collections import deque from pathlib import Path from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union @@ -477,9 +478,9 @@ def _quantize_torchmodel( subset_size=quantization_config.num_samples, ignored_scope=quantization_config.get_ignored_scope_instance(), model_type=nncf.ModelType(quantization_config.model_type), - preset=nncf.QuantizationPreset.PERFORMANCE - if quantization_config.sym - else nncf.QuantizationPreset.MIXED, + preset=( + nncf.QuantizationPreset.PERFORMANCE if quantization_config.sym else nncf.QuantizationPreset.MIXED + ), fast_bias_correction=quantization_config.fast_bias_correction, advanced_parameters=nncf.AdvancedQuantizationParameters( overflow_fix=OverflowFix(quantization_config.overflow_fix) @@ -541,7 +542,8 @@ def get_calibration_dataset( dataset_split: str = "train", preprocess_function: Optional[Callable] = None, preprocess_batch: bool = True, - use_auth_token: bool = False, + use_auth_token: Optional[Union[bool, str]] = None, + token: Optional[Union[bool, str]] = None, cache_dir: str = HUGGINGFACE_HUB_CACHE, ) -> datasets.Dataset: """ @@ -561,13 +563,25 @@ def get_calibration_dataset( Processing function to apply to each example after loading dataset. preprocess_batch (`bool`, defaults to `True`): Whether the `preprocess_function` should be batched. - use_auth_token (`bool`, defaults to `False`): - Whether to use the token generated when running `transformers-cli login`. + use_auth_token (Optional[Union[bool, str]], defaults to `None`): + Deprecated. Please use `token` instead. + token (Optional[Union[bool, str]], defaults to `None`): + The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated + when running `huggingface-cli login` (stored in `~/.huggingface`). cache_dir (`str`, *optional*): Caching directory for a calibration dataset. Returns: The calibration `datasets.Dataset` to use for the post-training static quantization calibration step. """ + if use_auth_token is not None: + warnings.warn( + "The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.", + FutureWarning, + ) + if token is not None: + raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.") + token = use_auth_token + if not is_datasets_available(): raise ValueError(DATASETS_IMPORT_ERROR.format("OVQuantizer.get_calibration_dataset")) from datasets import load_dataset @@ -576,7 +590,7 @@ def get_calibration_dataset( dataset_name, name=dataset_config_name, split=dataset_split, - use_auth_token=use_auth_token, + token=token, cache_dir=cache_dir, ) diff --git a/tests/openvino/test_modeling.py b/tests/openvino/test_modeling.py index f84cac8161..a463f40c0e 100644 --- a/tests/openvino/test_modeling.py +++ b/tests/openvino/test_modeling.py @@ -14,6 +14,7 @@ import gc import os +import subprocess import tempfile import time import unittest @@ -247,6 +248,15 @@ def test_load_from_hub_and_save_stable_diffusion_model(self): del pipeline gc.collect() + def test_load_model_from_hub_private_with_token(self): + subprocess.run("huggingface-cli logout", shell=True) + + # a fine-grained read-only token of private repo "IlyasMoutawwakil/test-hub-bert" + token = "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" + + loaded_model = OVModelForMaskedLM.from_pretrained("IlyasMoutawwakil/test-hub-bert", use_auth_token=token) + self.assertIsInstance(loaded_model.config, PretrainedConfig) + class OVModelForSequenceClassificationIntegrationTest(unittest.TestCase): SUPPORTED_ARCHITECTURES = (