Skip to content

Commit 00581ab

Browse files
Fix hub offline (#680)
* set default cache dir * added offline test * fix * finish
1 parent 7db4c7e commit 00581ab

File tree

14 files changed

+77
-22
lines changed

14 files changed

+77
-22
lines changed

.github/workflows/test_offline.yaml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Offline usage / Python - Test
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
build:
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
python-version: [3.9]
19+
os: [ubuntu-latest]
20+
21+
runs-on: ${{ matrix.os }}
22+
steps:
23+
- uses: actions/checkout@v3
24+
- name: Setup Python ${{ matrix.python-version }}
25+
uses: actions/setup-python@v3
26+
with:
27+
python-version: ${{ matrix.python-version }}
28+
- name: Install dependencies
29+
run: |
30+
pip install .[tests,openvino]
31+
- name: Test
32+
run: |
33+
HF_HOME=/tmp/ huggingface-cli download hf-internal-testing/tiny-random-gpt2
34+
HF_HOME=/tmp/ HF_HUB_OFFLINE=1 optimum-cli export openvino --model hf-internal-testing/tiny-random-gpt2 gpt2_openvino --task text-generation
35+
36+
huggingface-cli download hf-internal-testing/tiny-random-gpt2
37+
HF_HUB_OFFLINE=1 optimum-cli export openvino --model hf-internal-testing/tiny-random-gpt2 gpt2_openvino --task text-generation
38+
39+
pytest tests/openvino/test_modeling.py -k "test_load_from_hub" -s -vvvvv
40+
HF_HUB_OFFLINE=1 pytest tests/openvino/test_modeling.py -k "test_load_from_hub" -s -vvvvv

optimum/commands/export/openvino.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from pathlib import Path
1919
from typing import TYPE_CHECKING, Optional
2020

21+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
22+
2123
from ...exporters import TasksManager
2224
from ...intel.utils.import_utils import DIFFUSERS_IMPORT_ERROR, is_diffusers_available
2325
from ..base import BaseOptimumCLICommand, CommandInfo
@@ -47,7 +49,9 @@ def parse_args_openvino(parser: "ArgumentParser"):
4749
f" {str(TasksManager.get_all_tasks())}. For decoder models, use `xxx-with-past` to export the model using past key values in the decoder."
4850
),
4951
)
50-
optional_group.add_argument("--cache_dir", type=str, default=None, help="Path indicating where to store cache.")
52+
optional_group.add_argument(
53+
"--cache_dir", type=str, default=HUGGINGFACE_HUB_CACHE, help="Path indicating where to store cache."
54+
)
5155
optional_group.add_argument(
5256
"--framework",
5357
type=str,

optimum/exporters/openvino/__main__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pathlib import Path
1717
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union
1818

19+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
1920
from requests.exceptions import ConnectionError as RequestsConnectionError
2021
from transformers import AutoConfig, AutoTokenizer, PreTrainedTokenizerBase
2122

@@ -48,7 +49,7 @@ def main_export(
4849
task: str = "auto",
4950
device: str = "cpu",
5051
framework: Optional[str] = None,
51-
cache_dir: Optional[str] = None,
52+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
5253
trust_remote_code: bool = False,
5354
pad_token_id: Optional[int] = None,
5455
subfolder: str = "",

optimum/intel/generation/modeling.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import torch
2323
from huggingface_hub import hf_hub_download
24+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
2425
from transformers import AutoConfig, AutoModelForCausalLM, GenerationConfig, PretrainedConfig, PreTrainedModel
2526
from transformers.generation import GenerationMixin
2627
from transformers.modeling_outputs import CausalLMOutputWithPast
@@ -356,7 +357,7 @@ def _from_pretrained(
356357
use_auth_token: Optional[Union[bool, str, None]] = None,
357358
revision: Optional[Union[str, None]] = None,
358359
force_download: bool = False,
359-
cache_dir: Optional[str] = None,
360+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
360361
file_name: Optional[str] = WEIGHTS_NAME,
361362
local_files_only: bool = False,
362363
use_cache: bool = True,
@@ -400,7 +401,7 @@ def _from_transformers(
400401
use_auth_token: Optional[Union[bool, str]] = None,
401402
revision: Optional[str] = None,
402403
force_download: bool = False,
403-
cache_dir: Optional[str] = None,
404+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
404405
subfolder: str = "",
405406
local_files_only: bool = False,
406407
use_cache: bool = True,

optimum/intel/ipex/modeling_base.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import intel_extension_for_pytorch as ipex
2323
import torch
2424
from huggingface_hub import hf_hub_download
25+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
2526
from intel_extension_for_pytorch.cpu._auto_kernel_selection import _enable_tpp
2627
from intel_extension_for_pytorch.transformers.optimize import get_dummy_input
2728
from transformers import (
@@ -153,7 +154,7 @@ def _from_transformers(
153154
use_auth_token: Optional[Union[bool, str]] = None,
154155
revision: Optional[str] = None,
155156
force_download: bool = False,
156-
cache_dir: Optional[str] = None,
157+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
157158
subfolder: str = "",
158159
local_files_only: bool = False,
159160
torch_dtype: Optional[Union[str, "torch.dtype"]] = None,
@@ -190,7 +191,7 @@ def _from_pretrained(
190191
use_auth_token: Optional[Union[bool, str, None]] = None,
191192
revision: Optional[Union[str, None]] = None,
192193
force_download: bool = False,
193-
cache_dir: Optional[str] = None,
194+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
194195
file_name: Optional[str] = WEIGHTS_NAME,
195196
local_files_only: bool = False,
196197
subfolder: str = "",

optimum/intel/neural_compressor/modeling_base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import torch
2222
from huggingface_hub import hf_hub_download
23+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
2324
from neural_compressor.utils.pytorch import load
2425
from transformers import (
2526
AutoConfig,
@@ -104,7 +105,7 @@ def _from_pretrained(
104105
use_auth_token: Optional[Union[bool, str, None]] = None,
105106
revision: Optional[Union[str, None]] = None,
106107
force_download: bool = False,
107-
cache_dir: Optional[str] = None,
108+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
108109
file_name: str = WEIGHTS_NAME,
109110
local_files_only: bool = False,
110111
subfolder: str = "",

optimum/intel/openvino/loaders.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import safetensors
2626

2727
import openvino
28-
from huggingface_hub.constants import HF_HUB_OFFLINE
28+
from huggingface_hub.constants import HF_HUB_OFFLINE, HUGGINGFACE_HUB_CACHE
2929
from openvino.runtime import Type
3030
from openvino.runtime import opset11 as ops
3131
from openvino.runtime.passes import Manager, Matcher, MatcherPass, WrapType
@@ -37,7 +37,7 @@
3737
try:
3838
from diffusers.utils import DIFFUSERS_CACHE
3939
except ImportError:
40-
DIFFUSERS_CACHE = None
40+
DIFFUSERS_CACHE = HUGGINGFACE_HUB_CACHE
4141

4242

4343
logger = logging.getLogger(__name__)

optimum/intel/openvino/modeling.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import torch
2424
import transformers
2525
from huggingface_hub import model_info
26+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
2627
from transformers import (
2728
AutoConfig,
2829
AutoModel,
@@ -423,7 +424,7 @@ def _from_transformers(
423424
use_auth_token: Optional[Union[bool, str]] = None,
424425
revision: Optional[str] = None,
425426
force_download: bool = False,
426-
cache_dir: Optional[str] = None,
427+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
427428
subfolder: str = "",
428429
local_files_only: bool = False,
429430
task: Optional[str] = None,
@@ -585,7 +586,7 @@ def from_pretrained(
585586
use_auth_token: Optional[Union[bool, str]] = None,
586587
revision: Optional[str] = None,
587588
force_download: bool = False,
588-
cache_dir: Optional[str] = None,
589+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
589590
subfolder: str = "",
590591
local_files_only: bool = False,
591592
task: Optional[str] = None,

optimum/intel/openvino/modeling_base.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import openvino
2222
from huggingface_hub import hf_hub_download
23+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
2324
from openvino import Core, convert_model
2425
from openvino._offline_transformations import apply_moc_transformations, compress_model_transformation
2526
from transformers import GenerationConfig, PretrainedConfig
@@ -171,7 +172,7 @@ def _from_pretrained(
171172
use_auth_token: Optional[Union[bool, str, None]] = None,
172173
revision: Optional[Union[str, None]] = None,
173174
force_download: bool = False,
174-
cache_dir: Optional[str] = None,
175+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
175176
file_name: Optional[str] = None,
176177
subfolder: str = "",
177178
from_onnx: bool = False,
@@ -300,7 +301,7 @@ def _from_transformers(
300301
use_auth_token: Optional[Union[bool, str]] = None,
301302
revision: Optional[str] = None,
302303
force_download: bool = False,
303-
cache_dir: Optional[str] = None,
304+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
304305
subfolder: str = "",
305306
local_files_only: bool = False,
306307
task: Optional[str] = None,
@@ -368,7 +369,7 @@ def _to_load(
368369
use_auth_token: Optional[Union[bool, str]] = None,
369370
revision: Optional[str] = None,
370371
force_download: bool = False,
371-
cache_dir: Optional[str] = None,
372+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
372373
local_files_only: bool = False,
373374
stateful: bool = False,
374375
**kwargs,

optimum/intel/openvino/modeling_base_seq2seq.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import openvino
2222
from huggingface_hub import hf_hub_download
23+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
2324
from openvino._offline_transformations import apply_moc_transformations, compress_model_transformation
2425
from transformers import GenerationConfig, PretrainedConfig
2526
from transformers.file_utils import add_start_docstrings
@@ -111,7 +112,7 @@ def _from_pretrained(
111112
use_auth_token: Optional[Union[bool, str]] = None,
112113
revision: Optional[str] = None,
113114
force_download: bool = False,
114-
cache_dir: Optional[str] = None,
115+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
115116
encoder_file_name: Optional[str] = None,
116117
decoder_file_name: Optional[str] = None,
117118
decoder_with_past_file_name: Optional[str] = None,
@@ -222,7 +223,7 @@ def _from_transformers(
222223
use_auth_token: Optional[Union[bool, str]] = None,
223224
revision: Optional[str] = None,
224225
force_download: bool = False,
225-
cache_dir: Optional[str] = None,
226+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
226227
subfolder: str = "",
227228
local_files_only: bool = False,
228229
task: Optional[str] = None,

optimum/intel/openvino/modeling_decoder.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import numpy as np
2222
import openvino
2323
import torch
24+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
2425
from openvino.preprocess import PrePostProcessor
2526
from openvino.runtime import Core, Tensor, Type
2627
from transformers import AutoModelForCausalLM, AutoTokenizer, PretrainedConfig
@@ -221,7 +222,7 @@ def _from_transformers(
221222
use_auth_token: Optional[Union[bool, str]] = None,
222223
revision: Optional[str] = None,
223224
force_download: bool = False,
224-
cache_dir: Optional[str] = None,
225+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
225226
subfolder: str = "",
226227
local_files_only: bool = False,
227228
task: Optional[str] = None,
@@ -565,7 +566,7 @@ def _from_pretrained(
565566
use_auth_token: Optional[Union[bool, str, None]] = None,
566567
revision: Optional[Union[str, None]] = None,
567568
force_download: bool = False,
568-
cache_dir: Optional[str] = None,
569+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
569570
file_name: Optional[str] = None,
570571
subfolder: str = "",
571572
from_onnx: bool = False,

optimum/intel/openvino/modeling_diffusion.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from diffusers.schedulers.scheduling_utils import SCHEDULER_CONFIG_NAME
3636
from diffusers.utils import CONFIG_NAME, is_invisible_watermark_available
3737
from huggingface_hub import snapshot_download
38+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
3839
from openvino._offline_transformations import compress_model_transformation
3940
from openvino.runtime import Core
4041
from transformers import CLIPFeatureExtractor, CLIPTokenizer
@@ -208,7 +209,7 @@ def _from_pretrained(
208209
config: Dict[str, Any],
209210
use_auth_token: Optional[Union[bool, str]] = None,
210211
revision: Optional[str] = None,
211-
cache_dir: Optional[str] = None,
212+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
212213
vae_decoder_file_name: Optional[str] = None,
213214
text_encoder_file_name: Optional[str] = None,
214215
unet_file_name: Optional[str] = None,
@@ -400,7 +401,7 @@ def _from_transformers(
400401
use_auth_token: Optional[Union[bool, str]] = None,
401402
revision: Optional[str] = None,
402403
force_download: bool = False,
403-
cache_dir: Optional[str] = None,
404+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
404405
local_files_only: bool = False,
405406
tokenizer: Optional["CLIPTokenizer"] = None,
406407
scheduler: Union["DDIMScheduler", "PNDMScheduler", "LMSDiscreteScheduler"] = None,

optimum/intel/openvino/modeling_timm.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import numpy as np
2020
import timm
2121
import torch
22+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
2223
from packaging import version
2324
from timm.layers.config import set_fused_attn
2425
from timm.models._hub import load_model_config_from_hf
@@ -55,7 +56,7 @@ class TimmConfig(PretrainedConfig):
5556
def from_pretrained(
5657
cls,
5758
pretrained_model_name_or_path: Union[str, os.PathLike],
58-
cache_dir: Optional[Union[str, os.PathLike]] = None,
59+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
5960
force_download: bool = False,
6061
local_files_only: bool = False,
6162
token: Optional[Union[str, bool]] = None,

optimum/intel/openvino/quantization.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import openvino
2626
import torch
2727
import transformers
28+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
2829
from nncf import CompressWeightsMode, SensitivityMetric
2930
from nncf.quantization.advanced_parameters import AdvancedSmoothQuantParameters, OverflowFix
3031
from nncf.torch import register_module
@@ -541,7 +542,7 @@ def get_calibration_dataset(
541542
preprocess_function: Optional[Callable] = None,
542543
preprocess_batch: bool = True,
543544
use_auth_token: bool = False,
544-
cache_dir: Optional[str] = None,
545+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
545546
) -> datasets.Dataset:
546547
"""
547548
Create the calibration `datasets.Dataset` to use for the post-training static quantization calibration step.

0 commit comments

Comments
 (0)