Skip to content

Commit ee96c82

Browse files
authored
fix imports in openvino cli command (#1020)
1 parent cf5b951 commit ee96c82

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

optimum/commands/export/openvino.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
2222

2323
from ...exporters import TasksManager
24-
from ...exporters.openvino.convert import save_preprocessors
2524
from ...intel.utils.import_utils import DIFFUSERS_IMPORT_ERROR, is_diffusers_available
2625
from ...intel.utils.modeling_utils import _infer_library_from_model_name_or_path
2726
from ...utils.save_utils import maybe_load_preprocessors
@@ -244,6 +243,7 @@ def parse_args(parser: "ArgumentParser"):
244243

245244
def run(self):
246245
from ...exporters.openvino.__main__ import infer_task, main_export, maybe_convert_tokenizers
246+
from ...exporters.openvino.utils import save_preprocessors
247247
from ...intel.openvino.configuration import _DEFAULT_4BIT_CONFIG, OVConfig, get_default_int4_config
248248

249249
if self.args.library is None:

optimum/exporters/openvino/convert.py

+1-24
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
2222

2323
import onnx
24-
from transformers import PretrainedConfig
2524
from transformers.generation import GenerationMixin
2625
from transformers.utils import is_tf_available, is_torch_available
2726

@@ -54,7 +53,6 @@
5453
is_transformers_version,
5554
)
5655
from optimum.utils import DEFAULT_DUMMY_SHAPES, is_diffusers_available
57-
from optimum.utils.save_utils import maybe_save_preprocessors
5856

5957
from ...intel.utils.import_utils import is_nncf_available
6058
from ...intel.utils.modeling_utils import _infer_library_from_model_or_model_class
@@ -73,6 +71,7 @@
7371
clear_class_registry,
7472
remove_none_from_dummy_inputs,
7573
save_config,
74+
save_preprocessors,
7675
)
7776

7877

@@ -827,28 +826,6 @@ def export_tokenizer(
827826
save_model(model, output / file_name.format(suffix))
828827

829828

830-
def save_preprocessors(
831-
preprocessors: List, config: PretrainedConfig, output: Union[str, Path], trust_remote_code: bool
832-
):
833-
model_name_or_path = config._name_or_path
834-
if hasattr(config, "export_model_type"):
835-
model_type = config.export_model_type.replace("_", "-")
836-
else:
837-
model_type = config.model_type.replace("_", "-")
838-
if preprocessors is not None:
839-
# phi3-vision processor does not have chat_template attribute that breaks Processor saving on disk
840-
if is_transformers_version(">=", "4.45") and model_type == "phi3-v" and len(preprocessors) > 1:
841-
if not hasattr(preprocessors[1], "chat_template"):
842-
preprocessors[1].chat_template = getattr(preprocessors[0], "chat_template", None)
843-
for processor in preprocessors:
844-
try:
845-
processor.save_pretrained(output)
846-
except Exception as ex:
847-
logger.error(f"Saving {type(processor)} failed with {ex}")
848-
else:
849-
maybe_save_preprocessors(model_name_or_path, output, trust_remote_code=trust_remote_code)
850-
851-
852829
def _add_runtime_options_to_rt_info(model: Model, options: Dict):
853830
"""
854831
Add runtime optinos

optimum/exporters/openvino/utils.py

+25
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
from pathlib import Path
1919
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
2020

21+
from transformers import PretrainedConfig
2122
from transformers.utils import is_torch_available
2223

2324
from openvino.runtime import Dimension, PartialShape, Symbol
2425
from openvino.runtime.utils.types import get_element_type
2526
from optimum.exporters import TasksManager
2627
from optimum.exporters.onnx.base import OnnxConfig
28+
from optimum.intel.utils import is_transformers_version
2729
from optimum.utils import is_diffusers_available
30+
from optimum.utils.save_utils import maybe_save_preprocessors
2831

2932

3033
logger = logging.getLogger(__name__)
@@ -227,3 +230,25 @@ def save_config(config, save_dir):
227230
save_dir.mkdir(exist_ok=True, parents=True)
228231
output_config_file = Path(save_dir / "config.json")
229232
config.to_json_file(output_config_file, use_diff=True)
233+
234+
235+
def save_preprocessors(
236+
preprocessors: List, config: PretrainedConfig, output: Union[str, Path], trust_remote_code: bool
237+
):
238+
model_name_or_path = config._name_or_path
239+
if hasattr(config, "export_model_type"):
240+
model_type = config.export_model_type.replace("_", "-")
241+
else:
242+
model_type = config.model_type.replace("_", "-")
243+
if preprocessors is not None:
244+
# phi3-vision processor does not have chat_template attribute that breaks Processor saving on disk
245+
if is_transformers_version(">=", "4.45") and model_type == "phi3-v" and len(preprocessors) > 1:
246+
if not hasattr(preprocessors[1], "chat_template"):
247+
preprocessors[1].chat_template = getattr(preprocessors[0], "chat_template", None)
248+
for processor in preprocessors:
249+
try:
250+
processor.save_pretrained(output)
251+
except Exception as ex:
252+
logger.error(f"Saving {type(processor)} failed with {ex}")
253+
else:
254+
maybe_save_preprocessors(model_name_or_path, output, trust_remote_code=trust_remote_code)

0 commit comments

Comments
 (0)