[PyOV] Move py_object_to_any under namespace (#15147)
* [PyOV] Move py_object_to_any under namespace * remove inline * codestyle * fix frontend
This commit is contained in:
parent
c29f111717
commit
fcd95f2169
@ -135,7 +135,7 @@ void regclass_CompiledModel(py::module m) {
|
||||
cls.def(
|
||||
"set_property",
|
||||
[](ov::CompiledModel& self, const std::pair<std::string, py::object>& property) {
|
||||
ov::AnyMap _properties{{property.first, py_object_to_any(property.second)}};
|
||||
ov::AnyMap _properties{{property.first, Common::utils::py_object_to_any(property.second)}};
|
||||
self.set_property(_properties);
|
||||
},
|
||||
py::arg("property"),
|
||||
|
@ -47,7 +47,7 @@ void regclass_Core(py::module m) {
|
||||
cls.def(
|
||||
"set_property",
|
||||
[](ov::Core& self, const std::pair<std::string, py::object>& property) {
|
||||
ov::AnyMap _properties{{property.first, py_object_to_any(property.second)}};
|
||||
ov::AnyMap _properties{{property.first, Common::utils::py_object_to_any(property.second)}};
|
||||
self.set_property(_properties);
|
||||
},
|
||||
py::arg("property"),
|
||||
@ -78,7 +78,7 @@ void regclass_Core(py::module m) {
|
||||
cls.def(
|
||||
"set_property",
|
||||
[](ov::Core& self, const std::string& device_name, const std::pair<std::string, py::object>& property) {
|
||||
ov::AnyMap _properties{{property.first, py_object_to_any(property.second)}};
|
||||
ov::AnyMap _properties{{property.first, Common::utils::py_object_to_any(property.second)}};
|
||||
self.set_property(device_name, _properties);
|
||||
},
|
||||
py::arg("device_name"),
|
||||
|
@ -117,7 +117,7 @@ void regclass_frontend_OpExtension(py::module m) {
|
||||
const std::map<std::string, py::object>& attr_values_map) {
|
||||
std::map<std::string, ov::Any> any_map;
|
||||
for (const auto& it : attr_values_map) {
|
||||
any_map[it.first] = py_object_to_any(it.second);
|
||||
any_map[it.first] = Common::utils::py_object_to_any(it.second);
|
||||
}
|
||||
return std::make_shared<OpExtension<void>>(fw_type_name, attr_names_map, any_map);
|
||||
}),
|
||||
@ -131,7 +131,7 @@ void regclass_frontend_OpExtension(py::module m) {
|
||||
const std::map<std::string, py::object>& attr_values_map) {
|
||||
std::map<std::string, ov::Any> any_map;
|
||||
for (const auto& it : attr_values_map) {
|
||||
any_map[it.first] = py_object_to_any(it.second);
|
||||
any_map[it.first] = Common::utils::py_object_to_any(it.second);
|
||||
}
|
||||
|
||||
return std::make_shared<OpExtension<void>>(ov_type_name, fw_type_name, attr_names_map, any_map);
|
||||
|
@ -14,7 +14,7 @@ function(frontend_module TARGET FRAMEWORK INSTALL_COMPONENT)
|
||||
set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY ${PYTHON_BRIDGE_OUTPUT_DIRECTORY}/frontend/${FRAMEWORK})
|
||||
set(CMAKE_PDB_OUTPUT_DIRECTORY ${PYTHON_BRIDGE_OUTPUT_DIRECTORY}/frontend/${FRAMEWORK})
|
||||
|
||||
file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
||||
file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${OpenVINOPython_SOURCE_DIR}/src/pyopenvino/utils/utils.cpp)
|
||||
|
||||
# create target
|
||||
|
||||
|
@ -62,7 +62,7 @@ void regclass_frontend_onnx_OpExtension(py::module m) {
|
||||
|
||||
std::map<std::string, ov::Any> any_map;
|
||||
for (const auto& it : attr_values_map) {
|
||||
any_map[it.first] = py_object_to_any(it.second);
|
||||
any_map[it.first] = Common::utils::py_object_to_any(it.second);
|
||||
}
|
||||
return std::make_shared<OpExtension<void>>(fw_type_name, attr_names_map, any_map);
|
||||
}), py::arg("fw_type_name"),
|
||||
@ -76,7 +76,7 @@ void regclass_frontend_onnx_OpExtension(py::module m) {
|
||||
|
||||
std::map<std::string, ov::Any> any_map;
|
||||
for (const auto& it : attr_values_map) {
|
||||
any_map[it.first] = py_object_to_any(it.second);
|
||||
any_map[it.first] = Common::utils::py_object_to_any(it.second);
|
||||
}
|
||||
return std::make_shared<OpExtension<void>>(ov_type_name, fw_type_name, attr_names_map, any_map);
|
||||
}),
|
||||
@ -93,7 +93,7 @@ void regclass_frontend_onnx_OpExtension(py::module m) {
|
||||
|
||||
std::map<std::string, ov::Any> any_map;
|
||||
for (const auto& it : attr_values_map) {
|
||||
any_map[it.first] = py_object_to_any(it.second);
|
||||
any_map[it.first] = Common::utils::py_object_to_any(it.second);
|
||||
}
|
||||
return std::make_shared<OpExtension<void>>(ov_type_name, fw_type_name, fw_domain, attr_names_map, any_map);
|
||||
}),
|
||||
|
@ -54,7 +54,7 @@ void regclass_frontend_tensorflow_OpExtension(py::module m) {
|
||||
const std::map<std::string, py::object>& attr_values_map) {
|
||||
std::map<std::string, ov::Any> any_map;
|
||||
for (const auto& it : attr_values_map) {
|
||||
any_map[it.first] = py_object_to_any(it.second);
|
||||
any_map[it.first] = Common::utils::py_object_to_any(it.second);
|
||||
}
|
||||
return std::make_shared<OpExtension<void>>(fw_type_name, attr_names_map, any_map);
|
||||
}), py::arg("fw_type_name"),
|
||||
@ -67,7 +67,7 @@ void regclass_frontend_tensorflow_OpExtension(py::module m) {
|
||||
const std::map<std::string, py::object>& attr_values_map) {
|
||||
std::map<std::string, ov::Any> any_map;
|
||||
for (const auto& it : attr_values_map) {
|
||||
any_map[it.first] = py_object_to_any(it.second);
|
||||
any_map[it.first] = Common::utils::py_object_to_any(it.second);
|
||||
}
|
||||
return std::make_shared<OpExtension<void>>(ov_type_name, fw_type_name, attr_names_map, any_map);
|
||||
}),
|
||||
|
@ -19,7 +19,7 @@ void regclass_graph_Any(py::module m) {
|
||||
"into C++ based core of the project.";
|
||||
|
||||
ov_any.def(py::init([](py::object& input_value) {
|
||||
return ov::Any(py_object_to_any(input_value));
|
||||
return ov::Any(Common::utils::py_object_to_any(input_value));
|
||||
}));
|
||||
|
||||
ov_any.def("__repr__", [](const ov::Any& self) {
|
||||
@ -60,7 +60,7 @@ void regclass_graph_Any(py::module m) {
|
||||
return a == b;
|
||||
});
|
||||
ov_any.def("__eq__", [](const ov::Any& a, py::object& b) -> bool {
|
||||
return a == ov::Any(py_object_to_any(b));
|
||||
return a == ov::Any(Common::utils::py_object_to_any(b));
|
||||
});
|
||||
ov_any.def(
|
||||
"get",
|
||||
@ -74,7 +74,7 @@ void regclass_graph_Any(py::module m) {
|
||||
ov_any.def(
|
||||
"set",
|
||||
[](ov::Any& self, py::object& value) {
|
||||
self = ov::Any(py_object_to_any(value));
|
||||
self = ov::Any(Common::utils::py_object_to_any(value));
|
||||
},
|
||||
R"(
|
||||
:param: Value to be set in OVAny.
|
||||
|
@ -796,7 +796,7 @@ void regclass_graph_Model(py::module m) {
|
||||
for (size_t i = 0; i < path.size(); i++) {
|
||||
cpp_args[i] = path[i].cast<std::string>();
|
||||
}
|
||||
self.set_rt_info<ov::Any>(py_object_to_any(obj), cpp_args);
|
||||
self.set_rt_info<ov::Any>(Common::utils::py_object_to_any(obj), cpp_args);
|
||||
},
|
||||
py::arg("obj"),
|
||||
py::arg("path"),
|
||||
@ -811,7 +811,7 @@ void regclass_graph_Model(py::module m) {
|
||||
model.def(
|
||||
"set_rt_info",
|
||||
[](ov::Model& self, const py::object& obj, const py::str& path) -> void {
|
||||
self.set_rt_info<ov::Any>(py_object_to_any(obj), path.cast<std::string>());
|
||||
self.set_rt_info<ov::Any>(Common::utils::py_object_to_any(obj), path.cast<std::string>());
|
||||
},
|
||||
py::arg("obj"),
|
||||
py::arg("path"),
|
||||
|
@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
#include "pyopenvino/utils/utils.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
#include <pybind11/stl.h>
|
||||
|
||||
@ -132,7 +132,7 @@ py::object from_ov_any(const ov::Any& any) {
|
||||
std::map<std::string, ov::Any> properties_to_any_map(const std::map<std::string, py::object>& properties) {
|
||||
std::map<std::string, ov::Any> properties_to_cpp;
|
||||
for (const auto& property : properties) {
|
||||
properties_to_cpp[property.first] = py_object_to_any(property.second);
|
||||
properties_to_cpp[property.first] = Common::utils::py_object_to_any(property.second);
|
||||
}
|
||||
return properties_to_cpp;
|
||||
}
|
||||
@ -166,5 +166,78 @@ void deprecation_warning(const std::string& function_name, const std::string& ve
|
||||
}
|
||||
PyErr_WarnEx(PyExc_DeprecationWarning, ss.str().data(), 2);
|
||||
}
|
||||
|
||||
ov::Any py_object_to_any(const py::object& py_obj) {
|
||||
// Python types
|
||||
if (py::isinstance<py::str>(py_obj)) {
|
||||
return py_obj.cast<std::string>();
|
||||
} else if (py::isinstance<py::bool_>(py_obj)) {
|
||||
return py_obj.cast<bool>();
|
||||
} else if (py::isinstance<py::float_>(py_obj)) {
|
||||
return py_obj.cast<double>();
|
||||
} else if (py::isinstance<py::int_>(py_obj)) {
|
||||
return py_obj.cast<int64_t>();
|
||||
} else if (py::isinstance<py::list>(py_obj)) {
|
||||
auto _list = py_obj.cast<py::list>();
|
||||
enum class PY_TYPE : int { UNKNOWN = 0, STR, INT, FLOAT, BOOL };
|
||||
PY_TYPE detected_type = PY_TYPE::UNKNOWN;
|
||||
for (const auto& it : _list) {
|
||||
auto check_type = [&](PY_TYPE type) {
|
||||
if (detected_type == PY_TYPE::UNKNOWN || detected_type == type) {
|
||||
detected_type = type;
|
||||
return;
|
||||
}
|
||||
OPENVINO_ASSERT("Incorrect attribute. Mixed types in the list are not allowed.");
|
||||
};
|
||||
if (py::isinstance<py::str>(it)) {
|
||||
check_type(PY_TYPE::STR);
|
||||
} else if (py::isinstance<py::int_>(it)) {
|
||||
check_type(PY_TYPE::INT);
|
||||
} else if (py::isinstance<py::float_>(it)) {
|
||||
check_type(PY_TYPE::FLOAT);
|
||||
} else if (py::isinstance<py::bool_>(it)) {
|
||||
check_type(PY_TYPE::BOOL);
|
||||
}
|
||||
}
|
||||
|
||||
// In case of empty vector works like with vector of strings
|
||||
if (_list.empty())
|
||||
return _list.cast<std::vector<std::string>>();
|
||||
|
||||
switch (detected_type) {
|
||||
case PY_TYPE::STR:
|
||||
return _list.cast<std::vector<std::string>>();
|
||||
case PY_TYPE::FLOAT:
|
||||
return _list.cast<std::vector<double>>();
|
||||
case PY_TYPE::INT:
|
||||
return _list.cast<std::vector<int64_t>>();
|
||||
case PY_TYPE::BOOL:
|
||||
return _list.cast<std::vector<bool>>();
|
||||
default:
|
||||
OPENVINO_ASSERT(false, "Unsupported attribute type.");
|
||||
}
|
||||
// OV types
|
||||
} else if (py::isinstance<ov::Any>(py_obj)) {
|
||||
return py::cast<ov::Any>(py_obj);
|
||||
} else if (py::isinstance<ov::element::Type>(py_obj)) {
|
||||
return py::cast<ov::element::Type>(py_obj);
|
||||
} else if (py::isinstance<ov::hint::Priority>(py_obj)) {
|
||||
return py::cast<ov::hint::Priority>(py_obj);
|
||||
} else if (py::isinstance<ov::hint::PerformanceMode>(py_obj)) {
|
||||
return py::cast<ov::hint::PerformanceMode>(py_obj);
|
||||
} else if (py::isinstance<ov::log::Level>(py_obj)) {
|
||||
return py::cast<ov::log::Level>(py_obj);
|
||||
} else if (py::isinstance<ov::device::Type>(py_obj)) {
|
||||
return py::cast<ov::device::Type>(py_obj);
|
||||
} else if (py::isinstance<ov::streams::Num>(py_obj)) {
|
||||
return py::cast<ov::streams::Num>(py_obj);
|
||||
} else if (py::isinstance<ov::Affinity>(py_obj)) {
|
||||
return py::cast<ov::Affinity>(py_obj);
|
||||
// If there is no match fallback to py::object
|
||||
} else if (py::isinstance<py::object>(py_obj)) {
|
||||
return py_obj;
|
||||
}
|
||||
OPENVINO_ASSERT(false, "Unsupported attribute type.");
|
||||
}
|
||||
}; // namespace utils
|
||||
}; // namespace Common
|
||||
|
@ -20,78 +20,8 @@ namespace utils {
|
||||
std::string convert_path_to_string(const py::object& path);
|
||||
|
||||
void deprecation_warning(const std::string& function_name, const std::string& version = std::string(), const std::string& message = std::string());
|
||||
|
||||
ov::Any py_object_to_any(const py::object& py_obj);
|
||||
|
||||
}; // namespace utils
|
||||
}; // namespace Common
|
||||
|
||||
inline ov::Any py_object_to_any(const py::object& py_obj) {
|
||||
// Python types
|
||||
if (py::isinstance<py::str>(py_obj)) {
|
||||
return py_obj.cast<std::string>();
|
||||
} else if (py::isinstance<py::bool_>(py_obj)) {
|
||||
return py_obj.cast<bool>();
|
||||
} else if (py::isinstance<py::float_>(py_obj)) {
|
||||
return py_obj.cast<double>();
|
||||
} else if (py::isinstance<py::int_>(py_obj)) {
|
||||
return py_obj.cast<int64_t>();
|
||||
} else if (py::isinstance<py::list>(py_obj)) {
|
||||
auto _list = py_obj.cast<py::list>();
|
||||
enum class PY_TYPE : int { UNKNOWN = 0, STR, INT, FLOAT, BOOL };
|
||||
PY_TYPE detected_type = PY_TYPE::UNKNOWN;
|
||||
for (const auto& it : _list) {
|
||||
auto check_type = [&](PY_TYPE type) {
|
||||
if (detected_type == PY_TYPE::UNKNOWN || detected_type == type) {
|
||||
detected_type = type;
|
||||
return;
|
||||
}
|
||||
OPENVINO_ASSERT("Incorrect attribute. Mixed types in the list are not allowed.");
|
||||
};
|
||||
if (py::isinstance<py::str>(it)) {
|
||||
check_type(PY_TYPE::STR);
|
||||
} else if (py::isinstance<py::int_>(it)) {
|
||||
check_type(PY_TYPE::INT);
|
||||
} else if (py::isinstance<py::float_>(it)) {
|
||||
check_type(PY_TYPE::FLOAT);
|
||||
} else if (py::isinstance<py::bool_>(it)) {
|
||||
check_type(PY_TYPE::BOOL);
|
||||
}
|
||||
}
|
||||
|
||||
// In case of empty vector works like with vector of strings
|
||||
if (_list.empty())
|
||||
return _list.cast<std::vector<std::string>>();
|
||||
|
||||
switch (detected_type) {
|
||||
case PY_TYPE::STR:
|
||||
return _list.cast<std::vector<std::string>>();
|
||||
case PY_TYPE::FLOAT:
|
||||
return _list.cast<std::vector<double>>();
|
||||
case PY_TYPE::INT:
|
||||
return _list.cast<std::vector<int64_t>>();
|
||||
case PY_TYPE::BOOL:
|
||||
return _list.cast<std::vector<bool>>();
|
||||
default:
|
||||
OPENVINO_ASSERT(false, "Unsupported attribute type.");
|
||||
}
|
||||
// OV types
|
||||
} else if (py::isinstance<ov::Any>(py_obj)) {
|
||||
return py::cast<ov::Any>(py_obj);
|
||||
} else if (py::isinstance<ov::element::Type>(py_obj)) {
|
||||
return py::cast<ov::element::Type>(py_obj);
|
||||
} else if (py::isinstance<ov::hint::Priority>(py_obj)) {
|
||||
return py::cast<ov::hint::Priority>(py_obj);
|
||||
} else if (py::isinstance<ov::hint::PerformanceMode>(py_obj)) {
|
||||
return py::cast<ov::hint::PerformanceMode>(py_obj);
|
||||
} else if (py::isinstance<ov::log::Level>(py_obj)) {
|
||||
return py::cast<ov::log::Level>(py_obj);
|
||||
} else if (py::isinstance<ov::device::Type>(py_obj)) {
|
||||
return py::cast<ov::device::Type>(py_obj);
|
||||
} else if (py::isinstance<ov::streams::Num>(py_obj)) {
|
||||
return py::cast<ov::streams::Num>(py_obj);
|
||||
} else if (py::isinstance<ov::Affinity>(py_obj)) {
|
||||
return py::cast<ov::Affinity>(py_obj);
|
||||
// If there is no match fallback to py::object
|
||||
} else if (py::isinstance<py::object>(py_obj)) {
|
||||
return py_obj;
|
||||
}
|
||||
OPENVINO_ASSERT(false, "Unsupported attribute type.");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user