diff --git a/optimum/exporters/openvino/convert.py b/optimum/exporters/openvino/convert.py index 2c076827d2..0f203bd04b 100644 --- a/optimum/exporters/openvino/convert.py +++ b/optimum/exporters/openvino/convert.py @@ -70,6 +70,7 @@ _get_open_clip_submodels_fn_and_export_configs, clear_class_registry, remove_none_from_dummy_inputs, + save_config, ) @@ -658,7 +659,11 @@ def export_from_model( files_subpaths = ["openvino_" + model_name + ".xml" for model_name in models_and_export_configs.keys()] elif library_name != "diffusers": if is_transformers_version(">=", "4.44.99"): - misplaced_generation_parameters = model.config._get_non_default_generation_parameters() + # some model configs may have issues with loading without parameters initialization + try: + misplaced_generation_parameters = model.config._get_non_default_generation_parameters() + except Exception: + misplaced_generation_parameters = {} if isinstance(model, GenerationMixin) and len(misplaced_generation_parameters) > 0: logger.warning( "Moving the following attributes in the config to the generation config: " diff --git a/optimum/exporters/openvino/utils.py b/optimum/exporters/openvino/utils.py index 75106fc2b5..04c8f3babf 100644 --- a/optimum/exporters/openvino/utils.py +++ b/optimum/exporters/openvino/utils.py @@ -15,6 +15,7 @@ import inspect from collections import namedtuple from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from pathlib import Path from transformers.utils import is_torch_available @@ -209,3 +210,13 @@ def get_submodels(model): MULTI_MODAL_TEXT_GENERATION_MODELS = ["llava", "llava-next", "internvl-chat"] + + +def save_config(config, save_dir): + try: + config.save_pretrained(save_dir) + except Exception: + save_dir = Path(save_dir) + save_dir.mkdir(exist_ok=True) + output_config_file = Path(save_dir / "config.json") + config.to_json_file(output_config_file, use_diff=True) diff --git a/optimum/intel/openvino/modeling_base.py b/optimum/intel/openvino/modeling_base.py index ed3cdadb51..5dd0dbc231 100644 --- a/optimum/intel/openvino/modeling_base.py +++ b/optimum/intel/openvino/modeling_base.py @@ -136,7 +136,11 @@ def __init__( self.generation_config = generation_config or GenerationConfig.from_model_config(config) if is_transformers_version(">=", "4.44.99"): - misplaced_generation_parameters = self.config._get_non_default_generation_parameters() + # some model configs may have issues with loading without parameters initialization + try: + misplaced_generation_parameters = self.config._get_non_default_generation_parameters() + except Exception: + misplaced_generation_parameters = {} if len(misplaced_generation_parameters) > 0: logger.warning( "Moving the following attributes in the config to the generation config: " diff --git a/optimum/intel/openvino/modeling_base_seq2seq.py b/optimum/intel/openvino/modeling_base_seq2seq.py index 06c6011488..20a5afdca7 100644 --- a/optimum/intel/openvino/modeling_base_seq2seq.py +++ b/optimum/intel/openvino/modeling_base_seq2seq.py @@ -84,7 +84,11 @@ def __init__( self.generation_config = generation_config or GenerationConfig.from_model_config(config) if is_transformers_version(">=", "4.44.99"): - misplaced_generation_parameters = self.config._get_non_default_generation_parameters() + # some model configs may have issues with loading without parameters initialization + try: + misplaced_generation_parameters = self.config._get_non_default_generation_parameters() + except Exception: + misplaced_generation_parameters = {} if len(misplaced_generation_parameters) > 0: logger.warning( "Moving the following attributes in the config to the generation config: " diff --git a/optimum/intel/openvino/modeling_visual_language.py b/optimum/intel/openvino/modeling_visual_language.py index 141abeb87f..32cda33861 100644 --- a/optimum/intel/openvino/modeling_visual_language.py +++ b/optimum/intel/openvino/modeling_visual_language.py @@ -14,6 +14,7 @@ from transformers.modeling_outputs import BaseModelOutputWithPooling from ...exporters.openvino import main_export +from ...exporters.openvino.utils import save_config from ...exporters.openvino.stateful import ensure_stateful_is_available from .configuration import OVConfig, OVWeightQuantizationConfig from .modeling_base import OVBaseModel, OVModelPart @@ -272,6 +273,13 @@ def compile(self): if part_model is not None: part_model._compile() + def _save_config(self, save_directory): + """ + Saves a model configuration into a directory, so that it can be re-loaded using the + [`from_pretrained`] class method. + """ + save_config(self.config, save_directory) + def _save_pretrained(self, save_directory: Union[str, Path]): """ Saves the model to the OpenVINO IR format so that it can be re-loaded using the