Skip to content

Commit

Permalink
fix config saving when config initialization without parametes is not…
Browse files Browse the repository at this point in the history
… implemented
  • Loading branch information
eaidova committed Oct 23, 2024
1 parent b9bfb6d commit 9a83fe5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
10 changes: 8 additions & 2 deletions optimum/exporters/openvino/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
_get_open_clip_submodels_fn_and_export_configs,
clear_class_registry,
remove_none_from_dummy_inputs,
save_config,
)


Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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: "
Expand All @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions optimum/exporters/openvino/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
5 changes: 4 additions & 1 deletion optimum/intel/openvino/modeling_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: "
Expand Down
8 changes: 8 additions & 0 deletions optimum/intel/openvino/modeling_visual_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9a83fe5

Please sign in to comment.