From 00581ab69a4e36fc712e8fa23d6e6def7ed69243 Mon Sep 17 00:00:00 2001 From: Ilyas Moutawwakil <57442720+IlyasMoutawwakil@users.noreply.github.com> Date: Tue, 23 Apr 2024 15:15:37 +0200 Subject: [PATCH] Fix hub offline (#680) * set default cache dir * added offline test * fix * finish --- .github/workflows/test_offline.yaml | 40 +++++++++++++++++++ optimum/commands/export/openvino.py | 6 ++- optimum/exporters/openvino/__main__.py | 3 +- optimum/intel/generation/modeling.py | 5 ++- optimum/intel/ipex/modeling_base.py | 5 ++- .../intel/neural_compressor/modeling_base.py | 3 +- optimum/intel/openvino/loaders.py | 4 +- optimum/intel/openvino/modeling.py | 5 ++- optimum/intel/openvino/modeling_base.py | 7 ++-- .../intel/openvino/modeling_base_seq2seq.py | 5 ++- optimum/intel/openvino/modeling_decoder.py | 5 ++- optimum/intel/openvino/modeling_diffusion.py | 5 ++- optimum/intel/openvino/modeling_timm.py | 3 +- optimum/intel/openvino/quantization.py | 3 +- 14 files changed, 77 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/test_offline.yaml diff --git a/.github/workflows/test_offline.yaml b/.github/workflows/test_offline.yaml new file mode 100644 index 0000000000..a54ba20766 --- /dev/null +++ b/.github/workflows/test_offline.yaml @@ -0,0 +1,40 @@ +name: Offline usage / Python - Test + +on: + push: + branches: [main] + pull_request: + branches: [main] + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +jobs: + build: + strategy: + fail-fast: false + matrix: + python-version: [3.9] + os: [ubuntu-latest] + + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - name: Setup Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + pip install .[tests,openvino] + - name: Test + run: | + HF_HOME=/tmp/ huggingface-cli download hf-internal-testing/tiny-random-gpt2 + HF_HOME=/tmp/ HF_HUB_OFFLINE=1 optimum-cli export openvino --model hf-internal-testing/tiny-random-gpt2 gpt2_openvino --task text-generation + + huggingface-cli download hf-internal-testing/tiny-random-gpt2 + HF_HUB_OFFLINE=1 optimum-cli export openvino --model hf-internal-testing/tiny-random-gpt2 gpt2_openvino --task text-generation + + pytest tests/openvino/test_modeling.py -k "test_load_from_hub" -s -vvvvv + HF_HUB_OFFLINE=1 pytest tests/openvino/test_modeling.py -k "test_load_from_hub" -s -vvvvv diff --git a/optimum/commands/export/openvino.py b/optimum/commands/export/openvino.py index cdae847468..4fed3f6f88 100644 --- a/optimum/commands/export/openvino.py +++ b/optimum/commands/export/openvino.py @@ -18,6 +18,8 @@ from pathlib import Path from typing import TYPE_CHECKING, Optional +from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE + from ...exporters import TasksManager from ...intel.utils.import_utils import DIFFUSERS_IMPORT_ERROR, is_diffusers_available from ..base import BaseOptimumCLICommand, CommandInfo @@ -47,7 +49,9 @@ def parse_args_openvino(parser: "ArgumentParser"): f" {str(TasksManager.get_all_tasks())}. For decoder models, use `xxx-with-past` to export the model using past key values in the decoder." ), ) - optional_group.add_argument("--cache_dir", type=str, default=None, help="Path indicating where to store cache.") + optional_group.add_argument( + "--cache_dir", type=str, default=HUGGINGFACE_HUB_CACHE, help="Path indicating where to store cache." + ) optional_group.add_argument( "--framework", type=str, diff --git a/optimum/exporters/openvino/__main__.py b/optimum/exporters/openvino/__main__.py index d7b29584d6..67d7cd53bb 100644 --- a/optimum/exporters/openvino/__main__.py +++ b/optimum/exporters/openvino/__main__.py @@ -16,6 +16,7 @@ from pathlib import Path from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union +from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE from requests.exceptions import ConnectionError as RequestsConnectionError from transformers import AutoConfig, AutoTokenizer, PreTrainedTokenizerBase @@ -48,7 +49,7 @@ def main_export( task: str = "auto", device: str = "cpu", framework: Optional[str] = None, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, trust_remote_code: bool = False, pad_token_id: Optional[int] = None, subfolder: str = "", diff --git a/optimum/intel/generation/modeling.py b/optimum/intel/generation/modeling.py index 3d9c657626..76ef245f33 100644 --- a/optimum/intel/generation/modeling.py +++ b/optimum/intel/generation/modeling.py @@ -21,6 +21,7 @@ import torch from huggingface_hub import hf_hub_download +from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE from transformers import AutoConfig, AutoModelForCausalLM, GenerationConfig, PretrainedConfig, PreTrainedModel from transformers.generation import GenerationMixin from transformers.modeling_outputs import CausalLMOutputWithPast @@ -356,7 +357,7 @@ def _from_pretrained( use_auth_token: Optional[Union[bool, str, None]] = None, revision: Optional[Union[str, None]] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, file_name: Optional[str] = WEIGHTS_NAME, local_files_only: bool = False, use_cache: bool = True, @@ -400,7 +401,7 @@ def _from_transformers( use_auth_token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, subfolder: str = "", local_files_only: bool = False, use_cache: bool = True, diff --git a/optimum/intel/ipex/modeling_base.py b/optimum/intel/ipex/modeling_base.py index 8a7a4f2028..3961c1f3af 100644 --- a/optimum/intel/ipex/modeling_base.py +++ b/optimum/intel/ipex/modeling_base.py @@ -22,6 +22,7 @@ import intel_extension_for_pytorch as ipex import torch from huggingface_hub import hf_hub_download +from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE from intel_extension_for_pytorch.cpu._auto_kernel_selection import _enable_tpp from intel_extension_for_pytorch.transformers.optimize import get_dummy_input from transformers import ( @@ -153,7 +154,7 @@ def _from_transformers( use_auth_token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, subfolder: str = "", local_files_only: bool = False, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, @@ -190,7 +191,7 @@ def _from_pretrained( use_auth_token: Optional[Union[bool, str, None]] = None, revision: Optional[Union[str, None]] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, file_name: Optional[str] = WEIGHTS_NAME, local_files_only: bool = False, subfolder: str = "", diff --git a/optimum/intel/neural_compressor/modeling_base.py b/optimum/intel/neural_compressor/modeling_base.py index c46e3f41c5..c27d329c06 100644 --- a/optimum/intel/neural_compressor/modeling_base.py +++ b/optimum/intel/neural_compressor/modeling_base.py @@ -20,6 +20,7 @@ import torch from huggingface_hub import hf_hub_download +from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE from neural_compressor.utils.pytorch import load from transformers import ( AutoConfig, @@ -104,7 +105,7 @@ def _from_pretrained( use_auth_token: Optional[Union[bool, str, None]] = None, revision: Optional[Union[str, None]] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, file_name: str = WEIGHTS_NAME, local_files_only: bool = False, subfolder: str = "", diff --git a/optimum/intel/openvino/loaders.py b/optimum/intel/openvino/loaders.py index 61d5755cfa..9234036854 100644 --- a/optimum/intel/openvino/loaders.py +++ b/optimum/intel/openvino/loaders.py @@ -25,7 +25,7 @@ import safetensors import openvino -from huggingface_hub.constants import HF_HUB_OFFLINE +from huggingface_hub.constants import HF_HUB_OFFLINE, HUGGINGFACE_HUB_CACHE from openvino.runtime import Type from openvino.runtime import opset11 as ops from openvino.runtime.passes import Manager, Matcher, MatcherPass, WrapType @@ -37,7 +37,7 @@ try: from diffusers.utils import DIFFUSERS_CACHE except ImportError: - DIFFUSERS_CACHE = None + DIFFUSERS_CACHE = HUGGINGFACE_HUB_CACHE logger = logging.getLogger(__name__) diff --git a/optimum/intel/openvino/modeling.py b/optimum/intel/openvino/modeling.py index 9c7c2b5258..e61148dc3d 100644 --- a/optimum/intel/openvino/modeling.py +++ b/optimum/intel/openvino/modeling.py @@ -23,6 +23,7 @@ import torch import transformers from huggingface_hub import model_info +from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE from transformers import ( AutoConfig, AutoModel, @@ -423,7 +424,7 @@ def _from_transformers( use_auth_token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, subfolder: str = "", local_files_only: bool = False, task: Optional[str] = None, @@ -585,7 +586,7 @@ def from_pretrained( use_auth_token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, subfolder: str = "", local_files_only: bool = False, task: Optional[str] = None, diff --git a/optimum/intel/openvino/modeling_base.py b/optimum/intel/openvino/modeling_base.py index a48cdf5c92..830db11933 100644 --- a/optimum/intel/openvino/modeling_base.py +++ b/optimum/intel/openvino/modeling_base.py @@ -20,6 +20,7 @@ import openvino from huggingface_hub import hf_hub_download +from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE from openvino import Core, convert_model from openvino._offline_transformations import apply_moc_transformations, compress_model_transformation from transformers import GenerationConfig, PretrainedConfig @@ -171,7 +172,7 @@ def _from_pretrained( use_auth_token: Optional[Union[bool, str, None]] = None, revision: Optional[Union[str, None]] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, file_name: Optional[str] = None, subfolder: str = "", from_onnx: bool = False, @@ -300,7 +301,7 @@ def _from_transformers( use_auth_token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, subfolder: str = "", local_files_only: bool = False, task: Optional[str] = None, @@ -368,7 +369,7 @@ def _to_load( use_auth_token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, local_files_only: bool = False, stateful: bool = False, **kwargs, diff --git a/optimum/intel/openvino/modeling_base_seq2seq.py b/optimum/intel/openvino/modeling_base_seq2seq.py index 78648e93d2..99f6c79286 100644 --- a/optimum/intel/openvino/modeling_base_seq2seq.py +++ b/optimum/intel/openvino/modeling_base_seq2seq.py @@ -20,6 +20,7 @@ import openvino from huggingface_hub import hf_hub_download +from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE from openvino._offline_transformations import apply_moc_transformations, compress_model_transformation from transformers import GenerationConfig, PretrainedConfig from transformers.file_utils import add_start_docstrings @@ -111,7 +112,7 @@ def _from_pretrained( use_auth_token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, encoder_file_name: Optional[str] = None, decoder_file_name: Optional[str] = None, decoder_with_past_file_name: Optional[str] = None, @@ -222,7 +223,7 @@ def _from_transformers( use_auth_token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, subfolder: str = "", local_files_only: bool = False, task: Optional[str] = None, diff --git a/optimum/intel/openvino/modeling_decoder.py b/optimum/intel/openvino/modeling_decoder.py index 39a7bee9a2..e69792cec3 100644 --- a/optimum/intel/openvino/modeling_decoder.py +++ b/optimum/intel/openvino/modeling_decoder.py @@ -21,6 +21,7 @@ import numpy as np import openvino import torch +from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE from openvino.preprocess import PrePostProcessor from openvino.runtime import Core, Tensor, Type from transformers import AutoModelForCausalLM, AutoTokenizer, PretrainedConfig @@ -221,7 +222,7 @@ def _from_transformers( use_auth_token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, subfolder: str = "", local_files_only: bool = False, task: Optional[str] = None, @@ -565,7 +566,7 @@ def _from_pretrained( use_auth_token: Optional[Union[bool, str, None]] = None, revision: Optional[Union[str, None]] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, file_name: Optional[str] = None, subfolder: str = "", from_onnx: bool = False, diff --git a/optimum/intel/openvino/modeling_diffusion.py b/optimum/intel/openvino/modeling_diffusion.py index fb9bec7a8e..aaf02079d1 100644 --- a/optimum/intel/openvino/modeling_diffusion.py +++ b/optimum/intel/openvino/modeling_diffusion.py @@ -35,6 +35,7 @@ from diffusers.schedulers.scheduling_utils import SCHEDULER_CONFIG_NAME from diffusers.utils import CONFIG_NAME, is_invisible_watermark_available from huggingface_hub import snapshot_download +from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE from openvino._offline_transformations import compress_model_transformation from openvino.runtime import Core from transformers import CLIPFeatureExtractor, CLIPTokenizer @@ -208,7 +209,7 @@ def _from_pretrained( config: Dict[str, Any], use_auth_token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, vae_decoder_file_name: Optional[str] = None, text_encoder_file_name: Optional[str] = None, unet_file_name: Optional[str] = None, @@ -400,7 +401,7 @@ def _from_transformers( use_auth_token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, force_download: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, local_files_only: bool = False, tokenizer: Optional["CLIPTokenizer"] = None, scheduler: Union["DDIMScheduler", "PNDMScheduler", "LMSDiscreteScheduler"] = None, diff --git a/optimum/intel/openvino/modeling_timm.py b/optimum/intel/openvino/modeling_timm.py index a84f80c9f7..f2566c8c4a 100644 --- a/optimum/intel/openvino/modeling_timm.py +++ b/optimum/intel/openvino/modeling_timm.py @@ -19,6 +19,7 @@ import numpy as np import timm import torch +from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE from packaging import version from timm.layers.config import set_fused_attn from timm.models._hub import load_model_config_from_hf @@ -55,7 +56,7 @@ class TimmConfig(PretrainedConfig): def from_pretrained( cls, pretrained_model_name_or_path: Union[str, os.PathLike], - cache_dir: Optional[Union[str, os.PathLike]] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, force_download: bool = False, local_files_only: bool = False, token: Optional[Union[str, bool]] = None, diff --git a/optimum/intel/openvino/quantization.py b/optimum/intel/openvino/quantization.py index 217e5e4056..124b0366c1 100644 --- a/optimum/intel/openvino/quantization.py +++ b/optimum/intel/openvino/quantization.py @@ -25,6 +25,7 @@ import openvino import torch import transformers +from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE from nncf import CompressWeightsMode, SensitivityMetric from nncf.quantization.advanced_parameters import AdvancedSmoothQuantParameters, OverflowFix from nncf.torch import register_module @@ -541,7 +542,7 @@ def get_calibration_dataset( preprocess_function: Optional[Callable] = None, preprocess_batch: bool = True, use_auth_token: bool = False, - cache_dir: Optional[str] = None, + cache_dir: str = HUGGINGFACE_HUB_CACHE, ) -> datasets.Dataset: """ Create the calibration `datasets.Dataset` to use for the post-training static quantization calibration step.