Skip to content

Commit 4e1b57d

Browse files
committed
support offline mode
1 parent ebe5c0f commit 4e1b57d

File tree

3 files changed

+127
-53
lines changed

3 files changed

+127
-53
lines changed

optimum/intel/openvino/modeling_base.py

+40-18
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
from typing import Dict, Optional, Union
2121

2222
import openvino
23-
from huggingface_hub import HfApi, hf_hub_download
24-
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
23+
from huggingface_hub import HfApi, hf_hub_download, try_to_load_from_cache
24+
from huggingface_hub.constants import HF_HUB_OFFLINE, HUGGINGFACE_HUB_CACHE
2525
from openvino import Core, convert_model
2626
from openvino._offline_transformations import apply_moc_transformations, compress_model_transformation
2727
from transformers import AutoConfig, GenerationConfig, PretrainedConfig
@@ -608,7 +608,7 @@ def from_pretrained(
608608
)
609609

610610
if export is None:
611-
export = cls._check_export_status(model_id, revision, subfolder)
611+
export = cls._check_export_status(model_id, revision, subfolder, local_files_only, cache_dir)
612612

613613
if not export and trust_remote_code:
614614
logger.warning(
@@ -633,7 +633,14 @@ def from_pretrained(
633633
)
634634

635635
@classmethod
636-
def _check_export_status(cls, model_id: Union[str, Path], revision: Optional[str] = None, subfolder: str = ""):
636+
def _check_export_status(
637+
cls,
638+
model_id: Union[str, Path],
639+
revision: Optional[str] = None,
640+
subfolder: str = "",
641+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
642+
local_files_only: bool = HF_HUB_OFFLINE,
643+
):
637644
model_dir = Path(model_id)
638645
if subfolder is not None:
639646
model_dir = model_dir / subfolder
@@ -642,17 +649,32 @@ def _check_export_status(cls, model_id: Union[str, Path], revision: Optional[str
642649
not (model_dir / OV_XML_FILE_NAME).exists()
643650
or not (model_dir / OV_XML_FILE_NAME.replace(".xml", ".bin")).exists()
644651
)
645-
646-
hf_api = HfApi()
647-
try:
648-
model_info = hf_api.model_info(model_id, revision=revision or "main")
649-
normalized_subfolder = None if subfolder is None else Path(subfolder).as_posix()
650-
model_files = [
651-
file.rfilename
652-
for file in model_info.siblings
653-
if normalized_subfolder is None or file.rfilename.startswith(normalized_subfolder)
654-
]
655-
ov_model_path = OV_XML_FILE_NAME if subfolder is None else f"{normalized_subfolder}/{OV_XML_FILE_NAME}"
656-
return ov_model_path not in model_files or ov_model_path.replace(".xml", ".bin") not in model_files
657-
except Exception:
658-
return True
652+
normalized_subfolder = None if subfolder is None else Path(subfolder).as_posix()
653+
ov_model_path = OV_XML_FILE_NAME if subfolder is None else f"{normalized_subfolder}/{OV_XML_FILE_NAME}"
654+
cache_model_path = try_to_load_from_cache(
655+
model_id, ov_model_path, cache_dir=cache_dir, revision=revision or "main", repo_type="model"
656+
)
657+
cache_bin_path = try_to_load_from_cache(
658+
model_id,
659+
ov_model_path.replace(".xml", ".bin"),
660+
cache_dir=cache_dir,
661+
revision=revision or "main",
662+
repo_type="model",
663+
)
664+
cache_status = cache_bin_path is not None and cache_model_path is not None
665+
666+
if not cache_status and not local_files_only:
667+
hf_api = HfApi()
668+
try:
669+
model_info = hf_api.model_info(model_id, revision=revision or "main")
670+
model_files = [
671+
file.rfilename
672+
for file in model_info.siblings
673+
if normalized_subfolder is None or file.rfilename.startswith(normalized_subfolder)
674+
]
675+
ov_model_path = OV_XML_FILE_NAME if subfolder is None else f"{normalized_subfolder}/{OV_XML_FILE_NAME}"
676+
return ov_model_path not in model_files or ov_model_path.replace(".xml", ".bin") not in model_files
677+
except Exception:
678+
return True
679+
680+
return not cache_status

optimum/intel/openvino/modeling_base_seq2seq.py

+47-19
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
from typing import Dict, Optional, Union
2121

2222
import openvino
23-
from huggingface_hub import HfApi, hf_hub_download
24-
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
23+
from huggingface_hub import HfApi, hf_hub_download, try_to_load_from_cache
24+
from huggingface_hub.constants import HF_HUB_OFFLINE, HUGGINGFACE_HUB_CACHE
2525
from openvino._offline_transformations import apply_moc_transformations, compress_model_transformation
2626
from transformers import GenerationConfig, PretrainedConfig
2727
from transformers.file_utils import add_start_docstrings
@@ -364,7 +364,14 @@ def forward(self, *args, **kwargs):
364364
raise NotImplementedError
365365

366366
@classmethod
367-
def _check_export_status(cls, model_id: Union[str, Path], revision: Optional[str] = None, subfolder: str = ""):
367+
def _check_export_status(
368+
cls,
369+
model_id: Union[str, Path],
370+
revision: Optional[str] = None,
371+
subfolder: str = "",
372+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
373+
local_files_only: bool = HF_HUB_OFFLINE,
374+
):
368375
model_dir = Path(model_id)
369376
if subfolder is not None:
370377
model_dir = model_dir / subfolder
@@ -377,19 +384,40 @@ def _check_export_status(cls, model_id: Union[str, Path], revision: Optional[str
377384
).exists()
378385
return not encoder_exists or not decoder_exists
379386

380-
hf_api = HfApi()
381-
try:
382-
model_info = hf_api.model_info(model_id, revision=revision or "main")
383-
normalized_subfolder = None if subfolder is None else Path(subfolder).as_posix()
384-
model_files = [
385-
file.rfilename
386-
for file in model_info.siblings
387-
if normalized_subfolder is None or file.rfilename.startswith(normalized_subfolder)
388-
]
389-
for model_name in [OV_ENCODER_NAME, OV_DECODER_NAME]:
390-
ov_model_path = model_name if subfolder is None else f"{normalized_subfolder}/{model_name}"
391-
if ov_model_path not in model_files or ov_model_path.replace(".xml", ".bin") not in model_files:
392-
return True
393-
return False
394-
except Exception:
395-
return True
387+
cache_status = []
388+
normalized_subfolder = None if subfolder is None else Path(subfolder).as_posix()
389+
390+
for model_name in [OV_ENCODER_NAME, OV_DECODER_NAME]:
391+
ov_model_path = model_name if subfolder is None else f"{normalized_subfolder}/{model_name}"
392+
cache_model_path = try_to_load_from_cache(
393+
model_id, ov_model_path, cache_dir=cache_dir, revision=revision or "main", repo_type="model"
394+
)
395+
cache_bin_path = try_to_load_from_cache(
396+
model_id,
397+
ov_model_path.replace(".xml", ".bin"),
398+
cache_dir=cache_dir,
399+
revision=revision or "main",
400+
repo_type="model",
401+
)
402+
403+
cache_status.append(cache_model_path is not None and cache_bin_path is not None)
404+
405+
if not all(cache_status) and not local_files_only:
406+
hf_api = HfApi()
407+
try:
408+
model_info = hf_api.model_info(model_id, revision=revision or "main")
409+
normalized_subfolder = None if subfolder is None else Path(subfolder).as_posix()
410+
model_files = [
411+
file.rfilename
412+
for file in model_info.siblings
413+
if normalized_subfolder is None or file.rfilename.startswith(normalized_subfolder)
414+
]
415+
for model_name in [OV_ENCODER_NAME, OV_DECODER_NAME]:
416+
ov_model_path = model_name if subfolder is None else f"{normalized_subfolder}/{model_name}"
417+
if ov_model_path not in model_files or ov_model_path.replace(".xml", ".bin") not in model_files:
418+
return True
419+
return False
420+
except Exception:
421+
return True
422+
423+
return not all(cache_status)

optimum/intel/openvino/modeling_diffusion.py

+40-16
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
)
3636
from diffusers.schedulers.scheduling_utils import SCHEDULER_CONFIG_NAME
3737
from diffusers.utils import CONFIG_NAME, is_invisible_watermark_available
38-
from huggingface_hub import HfApi, snapshot_download
39-
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
38+
from huggingface_hub import HfApi, snapshot_download, try_to_load_from_cache
39+
from huggingface_hub.constants import HF_HUB_OFFLINE, HUGGINGFACE_HUB_CACHE
4040
from openvino._offline_transformations import compress_model_transformation
4141
from openvino.runtime import Core
4242
from transformers import CLIPFeatureExtractor, CLIPTokenizer
@@ -412,7 +412,14 @@ def _from_transformers(
412412
)
413413

414414
@classmethod
415-
def _check_export_status(cls, model_id: Union[str, Path], revision: Optional[str] = None, subfolder: str = ""):
415+
def _check_export_status(
416+
cls,
417+
model_id: Union[str, Path],
418+
revision: Optional[str] = None,
419+
subfolder: str = "",
420+
cache_dir: str = HUGGINGFACE_HUB_CACHE,
421+
local_files_only: bool = HF_HUB_OFFLINE,
422+
):
416423
sub_model_dirs = [DIFFUSION_MODEL_UNET_SUBFOLDER, DIFFUSION_MODEL_VAE_DECODER_SUBFOLDER]
417424

418425
def check_model_part_status(model_id, subfolder, revision):
@@ -425,19 +432,36 @@ def check_model_part_status(model_id, subfolder, revision):
425432
or not (model_dir / OV_XML_FILE_NAME.replace(".xml", ".bin")).exists()
426433
)
427434

428-
hf_api = HfApi()
429-
try:
430-
model_info = hf_api.model_info(model_id, revision=revision or "main")
431-
normalized_subfolder = None if subfolder is None else Path(subfolder).as_posix()
432-
model_files = [
433-
file.rfilename
434-
for file in model_info.siblings
435-
if normalized_subfolder is None or file.rfilename.startswith(normalized_subfolder)
436-
]
437-
ov_model_path = OV_XML_FILE_NAME if subfolder is None else f"{normalized_subfolder}/{OV_XML_FILE_NAME}"
438-
return ov_model_path not in model_files or ov_model_path.replace(".xml", ".bin") not in model_files
439-
except Exception:
440-
return True
435+
normalized_subfolder = None if subfolder is None else Path(subfolder).as_posix()
436+
ov_model_path = OV_XML_FILE_NAME if subfolder is None else f"{normalized_subfolder}/{OV_XML_FILE_NAME}"
437+
cache_model_path = try_to_load_from_cache(
438+
model_id, ov_model_path, cache_dir=cache_dir, revision=revision or "main", repo_type="model"
439+
)
440+
cache_bin_path = try_to_load_from_cache(
441+
model_id,
442+
ov_model_path.replace(".xml", ".bin"),
443+
cache_dir=cache_dir,
444+
revision=revision or "main",
445+
repo_type="model",
446+
)
447+
cache_status = cache_bin_path is not None and cache_model_path is not None
448+
449+
if not cache_status and not local_files_only:
450+
hf_api = HfApi()
451+
try:
452+
model_info = hf_api.model_info(model_id, revision=revision or "main")
453+
model_files = [
454+
file.rfilename
455+
for file in model_info.siblings
456+
if normalized_subfolder is None or file.rfilename.startswith(normalized_subfolder)
457+
]
458+
ov_model_path = (
459+
OV_XML_FILE_NAME if subfolder is None else f"{normalized_subfolder}/{OV_XML_FILE_NAME}"
460+
)
461+
return ov_model_path not in model_files or ov_model_path.replace(".xml", ".bin") not in model_files
462+
except Exception:
463+
return True
464+
return not cache_status
441465

442466
for sub_model_dir in sub_model_dirs:
443467
sub_model = sub_model_dir if not subfolder else f"{Path(subfolder).as_posix()}/{sub_model_dir}"

0 commit comments

Comments
 (0)