diff --git a/optimum/exporters/openvino/convert.py b/optimum/exporters/openvino/convert.py index d46bf7e807..228e11c972 100644 --- a/optimum/exporters/openvino/convert.py +++ b/optimum/exporters/openvino/convert.py @@ -432,10 +432,7 @@ def ts_patched_forward(*args, **kwargs): ov_model.validate_nodes_and_infer_types() if stateful: - # Patching model according to stateful parameters - model.key_value_input_names = [name for name in input_names if name.startswith("past_key_values.")] - model.key_value_output_names = [name for name in output_names if name.startswith("present.")] - patch_stateful(model, ov_model) + patch_stateful(model.config, ov_model) _save_model(ov_model, output, compression_option=compression_option, compression_ratio=compression_ratio) clear_class_registry() diff --git a/optimum/exporters/openvino/stateful.py b/optimum/exporters/openvino/stateful.py index 2f7be147a5..d88ba57143 100644 --- a/optimum/exporters/openvino/stateful.py +++ b/optimum/exporters/openvino/stateful.py @@ -17,7 +17,7 @@ import openvino as ov from openvino.runtime import opset13 -from optimum.intel.utils.import_utils import is_openvino_version +from optimum.intel.utils.import_utils import _openvino_version, is_openvino_version from optimum.utils.normalized_config import NormalizedConfigManager @@ -121,9 +121,9 @@ def make_stateful( def raise_if_openvino_is_too_old(): - if is_openvino_version("<=", "2023.2"): + if is_openvino_version("<", "2023.3"): raise ValueError( - f"Could not create or use stateful model when using old version of openvino=={ov.__version__}. Install openvino>=2023.3.0." + f"Could not create or use stateful model when using old version of openvino=={_openvino_version}. Install openvino>=2023.3.0." ) @@ -131,10 +131,10 @@ def patch_stateful(config, ov_model): raise_if_openvino_is_too_old() key_value_input_names = [ - key.get_any_name() for key in ov_model.inputs if any("key_values" in key_name for key_name in key.names) + key.get_any_name() for key in ov_model.inputs if any("key_values" in key_name for key_name in key.get_names()) ] key_value_output_names = [ - key.get_any_name() for key in ov_model.output if any("present" in key_name for key_name in key.names) + key.get_any_name() for key in ov_model.outputs if any("present" in key_name for key_name in key.get_names()) ] not_kv_inputs = [ input for input in ov_model.inputs if not any(name in key_value_input_names for name in input.get_names()) diff --git a/optimum/intel/utils/import_utils.py b/optimum/intel/utils/import_utils.py index f778bbfcbd..3f3fa6c55b 100644 --- a/optimum/intel/utils/import_utils.py +++ b/optimum/intel/utils/import_utils.py @@ -70,12 +70,17 @@ _openvino_version = "N/A" if _openvino_available: try: - _openvino_version = importlib_metadata.version("openvino") - except importlib_metadata.PackageNotFoundError: - try: - _openvino_version = importlib_metadata.version("openvino-nightly") - except importlib_metadata.PackageNotFoundError: - _openvino_available = False + from openvino.runtime import get_version + + version = get_version() + # avoid invalid format + if "-" in version: + major_version, dev_info = version.split("-", 1) + commit_id = dev_info.split("-")[0] + version = f"{major_version}-{commit_id}" + _openvino_version = version + except ImportError: + _openvino_available = False _nncf_available = importlib.util.find_spec("nncf") is not None