Skip to content

Commit e3031f0

Browse files
authored
[fix] Extend when a model repository/directory already has an exported OV model (#1000)
* Also accept e.g. "openvino_model_qint8_quantized.xml" * Add test case * Add missing subfolder call to test
1 parent c7d6227 commit e3031f0

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

optimum/intel/openvino/modeling_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def from_pretrained(
444444

445445
ov_files = _find_files_matching_pattern(
446446
model_dir,
447-
pattern=r"(.*)?openvino(.*)?\_model.xml$",
447+
pattern=r"(.*)?openvino(.*)?\_model(.*)?.xml$",
448448
subfolder=subfolder,
449449
use_auth_token=token,
450450
revision=revision,

tests/openvino/test_modeling.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ def test_infer_export_when_loading(self):
431431

432432
def test_find_files_matching_pattern(self):
433433
model_id = "echarlaix/tiny-random-PhiForCausalLM"
434-
pattern = r"(.*)?openvino(.*)?\_model.xml$"
434+
pattern = r"(.*)?openvino(.*)?\_model(.*)?.xml$"
435435
# hub model
436436
for revision in ("main", "ov", "itrex"):
437437
ov_files = _find_files_matching_pattern(
@@ -452,7 +452,7 @@ def test_find_files_matching_pattern(self):
452452

453453
@parameterized.expand(("stable-diffusion", "stable-diffusion-openvino"))
454454
def test_find_files_matching_pattern_sd(self, model_arch):
455-
pattern = r"(.*)?openvino(.*)?\_model.xml$"
455+
pattern = r"(.*)?openvino(.*)?\_model(.*)?.xml$"
456456
model_id = MODEL_NAMES[model_arch]
457457
# hub model
458458
ov_files = _find_files_matching_pattern(model_id, pattern=pattern)
@@ -470,7 +470,7 @@ def test_find_files_matching_pattern_sd(self, model_arch):
470470
def test_find_files_matching_pattern_with_config_in_root(self, subfolder):
471471
# Notably, the model has a config.json file in the root directory and not in the subfolder
472472
model_id = "sentence-transformers-testing/stsb-bert-tiny-openvino"
473-
pattern = r"(.*)?openvino(.*)?\_model.xml$"
473+
pattern = r"(.*)?openvino(.*)?\_model(.*)?.xml$"
474474
# hub model
475475
ov_files = _find_files_matching_pattern(model_id, pattern=pattern, subfolder=subfolder)
476476
self.assertTrue(len(ov_files) == 1 if subfolder == "openvino" else len(ov_files) == 0)
@@ -483,6 +483,24 @@ def test_find_files_matching_pattern_with_config_in_root(self, subfolder):
483483
ov_files = _find_files_matching_pattern(local_dir, pattern=pattern, subfolder=subfolder)
484484
self.assertTrue(len(ov_files) == 1 if subfolder == "openvino" else len(ov_files) == 0)
485485

486+
def test_find_files_matching_pattern_with_quantized_ov_model(self):
487+
# This model only has "openvino/openvino_model_qint8_quantized.xml" and "openvino/openvino_model_qint8_quantized.bin"
488+
# We want to ensure that this model is found, so the `export` isn't forced to True
489+
model_id = "sentence-transformers-testing/stsb-bert-tiny-openvino-quantized-only"
490+
subfolder = "openvino"
491+
pattern = r"(.*)?openvino(.*)?\_model(.*)?.xml$"
492+
# hub model
493+
ov_files = _find_files_matching_pattern(model_id, pattern=pattern, subfolder=subfolder)
494+
self.assertTrue(len(ov_files) == 1)
495+
496+
# local model
497+
api = HfApi()
498+
with tempfile.TemporaryDirectory() as tmpdirname:
499+
local_dir = Path(tmpdirname) / "model"
500+
api.snapshot_download(repo_id=model_id, local_dir=local_dir)
501+
ov_files = _find_files_matching_pattern(local_dir, pattern=pattern, subfolder=subfolder)
502+
self.assertTrue(len(ov_files) == 1)
503+
486504

487505
class PipelineTest(unittest.TestCase):
488506
def test_load_model_from_hub(self):

0 commit comments

Comments
 (0)