OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Py OV] Core().compile_model(tokenizer, "CPU") fails (#29420)
Alicja Miloszewska154fb5bfda2f
diff --git a/src/bindings/python/src/pyopenvino/utils/utils.cpp b/src/bindings/python/src/pyopenvino/utils/utils.cppindex 0abf4339629..f19405df814 100644--- a/src/bindings/python/src/pyopenvino/utils/utils.cpp+++ b/src/bindings/python/src/pyopenvino/utils/utils.cpp@@ -101,8 +101,8 @@ py::object from_ov_any_map(const ov::AnyMap& map) {py::object from_ov_any(const ov::Any& any) {// Check for py::object- if (any.is<py::object>()) {- return any.as<py::object>();+ if (any.is<std::shared_ptr<py::object>>()) {+ return *any.as<std::shared_ptr<py::object>>();} // Check for std::stringelse if (any.is<std::string>()) {return py::cast(any.as<std::string>().c_str());@@ -408,6 +408,18 @@ ov::AnyMap py_object_to_any_map(const py::object& py_obj) {return return_value;}+template <typename... Args, std::size_t... I>+std::tuple<Args...> tuple_from_py_tuple_impl(const py::tuple& py_tuple, std::index_sequence<I...>) {+ return std::make_tuple(py_tuple[I].cast<Args>()...);+}++template <typename... Args>+std::tuple<Args...> tuple_from_py_tuple(const py::tuple& py_tuple) {+ OPENVINO_ASSERT(py_tuple.size() == sizeof...(Args), "Size of py::tuple does not match size of std::tuple");++ return tuple_from_py_tuple_impl<Args...>(py_tuple, std::index_sequence_for<Args...>{});+}+ov::Any py_object_to_any(const py::object& py_obj) {// Python typespy::object float_32_type = py::module_::import("numpy").attr("float32");@@ -425,6 +437,15 @@ ov::Any py_object_to_any(const py::object& py_obj) {return py_obj.cast<int64_t>();} else if (py::isinstance<py::none>(py_obj)) {return {};+ } else if (py::isinstance(py_obj, py::module_::import("enum").attr("Enum"))) {+ const auto value = py::cast<py::object>(py_obj).attr("value");+ if (py::isinstance<py::int_>(value)) {+ return value.cast<int64_t>();+ } else if (py::isinstance<py::str>(value)) {+ return value.cast<std::string>();+ } else {+ OPENVINO_THROW("Unsupported enum type.");+ }} else if (py::isinstance<py::list>(py_obj)) {auto _list = py_obj.cast<py::list>();@@ -447,6 +468,15 @@ ov::Any py_object_to_any(const py::object& py_obj) {default:OPENVINO_ASSERT(false, "Unsupported attribute type.");}+ } else if (py::isinstance<py::tuple>(py_obj)) {+ const auto _tuple = py::cast<py::tuple>(py_obj);+ if (_tuple.size() == 2) {+ return tuple_from_py_tuple<unsigned int, unsigned int>(_tuple);+ } else if (_tuple.size() == 3) {+ return tuple_from_py_tuple<unsigned int, unsigned int, unsigned int>(_tuple);+ } else {+ OPENVINO_THROW("Unsupported tuple size");+ }// OV types} else if (py_object_is_any_map(py_obj)) {return py_object_to_any_map(py_obj);@@ -501,7 +531,10 @@ ov::Any py_object_to_any(const py::object& py_obj) {return py::cast<ov::frontend::type::PyScalar>(py_obj);// If there is no match fallback to py::object} else if (py::isinstance<py::object>(py_obj)) {- return py_obj;+ return std::shared_ptr<py::object>(new py::object(py_obj), [](py::object* py_obj_reference) {+ py::gil_scoped_acquire acquire;+ delete py_obj_reference;+ });}OPENVINO_ASSERT(false, "Unsupported attribute type.");}diff --git a/src/bindings/python/tests/test_runtime/test_model.py b/src/bindings/python/tests/test_runtime/test_model.pyindex ccdfb1e5685..8e359a64f2a 100644--- a/src/bindings/python/tests/test_runtime/test_model.py+++ b/src/bindings/python/tests/test_runtime/test_model.py@@ -25,6 +25,7 @@ from openvino import (get_batch,serialize,save_model,+ OVAny,)from openvino import Outputfrom openvino.op.util import VariableInfo, Variable@@ -663,6 +664,16 @@ def test_serialize_rt_info(request, tmp_path):os.remove(bin_path)+def make_enum_info():+ from enum import Enum++ class EnumInfo(Enum):+ INFO_INT = 1+ INFO_STR = "info_str"++ return EnumInfo++# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#requestdef test_serialize_complex_rt_info(request, tmp_path):def check_rt_info(model):@@ -677,6 +688,8 @@ def test_serialize_complex_rt_info(request, tmp_path):assert model.has_rt_info(["config", "model_parameters", "labels", "label_tree", "nodes"]) is Trueassert model.has_rt_info(["config", "model_parameters", "labels", "label_groups", "ids"]) is Trueassert model.has_rt_info(["config", "model_parameters", "mean_values"]) is True+ assert model.has_rt_info("enum_info_int") is True+ assert model.has_rt_info("enum_info_str") is Trueassert model.get_rt_info(["config", "type_of_model"]).astype(str) == "classification"assert model.get_rt_info(["config", "converter_type"]).astype(str) == "classification"@@ -690,6 +703,8 @@ def test_serialize_complex_rt_info(request, tmp_path):assert model.get_rt_info(["config", "model_parameters", "labels", "label_tree", "nodes"]).aslist() == []assert model.get_rt_info(["config", "model_parameters", "labels", "label_groups", "ids"]).aslist(str) == ["sasd", "fdfdfsdf"]assert model.get_rt_info(["config", "model_parameters", "mean_values"]).aslist(float) == [22.3, 33.11, 44.0]+ assert model.get_rt_info("enum_info_int").astype(int) == 1+ assert model.get_rt_info("enum_info_str").astype(str) == "info_str"rt_info = model.get_rt_info()assert isinstance(rt_info["config"], dict)@@ -725,6 +740,8 @@ def test_serialize_complex_rt_info(request, tmp_path):model.set_rt_info([], ["config", "model_parameters", "labels", "label_tree", "nodes"])model.set_rt_info(["sasd", "fdfdfsdf"], ["config", "model_parameters", "labels", "label_groups", "ids"])model.set_rt_info([22.3, 33.11, 44.0], ["config", "model_parameters", "mean_values"])+ model.set_rt_info(make_enum_info().INFO_INT, "enum_info_int")+ model.set_rt_info(make_enum_info().INFO_STR, "enum_info_str")check_rt_info(model)@@ -738,6 +755,41 @@ def test_serialize_complex_rt_info(request, tmp_path):os.remove(bin_path)+def test_rt_info_enum():+ model = generate_add_model()+ enum_info = make_enum_info()++ core = Core()+ model.set_rt_info(enum_info.INFO_INT, "enum_info_int")+ model.set_rt_info(enum_info.INFO_STR, "enum_info_str")+ core.compile_model(model, "CPU")++ prop_int = model.get_rt_info("enum_info_int")+ assert isinstance(prop_int, OVAny)+ assert isinstance(prop_int.value, int)+ assert prop_int.value == 1+ assert enum_info.INFO_INT == enum_info(prop_int.value)++ prop_str = model.get_rt_info("enum_info_str")+ assert isinstance(prop_str, OVAny)+ assert isinstance(prop_str.value, str)+ assert prop_str.value == "info_str"+ assert enum_info.INFO_STR == enum_info(prop_str.value)+++def test_rt_info_gil():+ model = generate_add_model()++ class TestClass:+ def __init__(self):+ self.text = "test"++ core = Core()+ model.set_rt_info(TestClass(), "object_property")+ core.compile_model(model, "CPU")+ assert model.get_rt_info("object_property").value.text == "test"++def test_model_add_remove_result_parameter_sink():param = ops.parameter(PartialShape([1]), dtype=np.float32, name="param")relu1 = ops.relu(param, name="relu1")