diff --git a/optimum/exporters/openvino/convert.py b/optimum/exporters/openvino/convert.py index 59cc2bb7c3..86fc0ab9a1 100644 --- a/optimum/exporters/openvino/convert.py +++ b/optimum/exporters/openvino/convert.py @@ -63,6 +63,7 @@ _get_open_clip_submodels_fn_and_export_configs, clear_class_registry, remove_none_from_dummy_inputs, + save_config, ) @@ -379,7 +380,9 @@ def ts_patched_forward(*args, **kwargs): if patch_16bit_model: from openvino.frontend.pytorch.patch_model import __make_16bit_traceable + logging.disable(logging.warning) __make_16bit_traceable(model) + logging.disable(logging.NOTSET) check_dummy_inputs_are_allowed(model, dummy_inputs) input_info = _get_input_info(model, config, dummy_inputs) ov_model = convert_model( @@ -644,7 +647,10 @@ 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() + 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: " @@ -656,7 +662,7 @@ def export_from_model( setattr(model.config, param_name, None) # Saving the model config and preprocessor as this is needed sometimes. - model.config.save_pretrained(output) + save_config(output, model.config) generation_config = getattr(model, "generation_config", None) if generation_config is not None: try: diff --git a/optimum/exporters/openvino/utils.py b/optimum/exporters/openvino/utils.py index 9286a37f78..a6303a9856 100644 --- a/optimum/exporters/openvino/utils.py +++ b/optimum/exporters/openvino/utils.py @@ -14,6 +14,7 @@ import inspect from collections import namedtuple +from pathlib import Path from typing import Any, Callable, Dict, List, Optional, Tuple, Union from transformers.utils import is_torch_available @@ -209,3 +210,13 @@ def get_submodels(model): MULTI_MODAL_TEXT_GENERATION_MODELS = ["llava", "llava-next", "llava-qwen2", "internvl-chat", "minicpmv"] + + +def save_config(save_dir, config): + 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 353416ff5e..7561634d9a 100644 --- a/optimum/intel/openvino/modeling_base.py +++ b/optimum/intel/openvino/modeling_base.py @@ -135,7 +135,10 @@ 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() + 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 3ebfb04ca7..61541fe882 100644 --- a/optimum/intel/openvino/modeling_visual_language.py +++ b/optimum/intel/openvino/modeling_visual_language.py @@ -16,6 +16,7 @@ from ...exporters.openvino import main_export from ...exporters.openvino.stateful import ensure_stateful_is_available, model_has_input_output_name +from ...exporters.openvino.utils import save_config from .configuration import OVConfig, OVWeightQuantizationConfig from .modeling_base import OVBaseModel, OVModelPart from .modeling_decoder import CausalLMOutputWithPast, OVModelForCausalLM @@ -346,6 +347,13 @@ def _save_pretrained(self, save_directory: Union[str, Path]): f"The generation config will not be saved, saving failed with following error:\n{exception}" ) + 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(save_directory, self.config) + @classmethod def _from_pretrained( cls,