[Python API] Remove old api class from the new api (#10470)

* [Python API] Remove old api class from the new api

* start working on refactoring of OVAny

* fix tests

* fix code-style

* remove tuple test

* fix test

* fix omz hash

* one more overload

* fix pyfloat

* move from_ov_any to utils

* code-style

* move function from common to utils
This commit is contained in:
Anastasia Kuporosova 2022-03-31 21:57:05 +03:00 committed by GitHub
parent d3060d4bcc
commit dd54cb9c17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 243 additions and 289 deletions

View File

@ -32,15 +32,13 @@ from openvino.pyopenvino import Coordinate
from openvino.pyopenvino import Layout from openvino.pyopenvino import Layout
from openvino.pyopenvino import ConstOutput from openvino.pyopenvino import ConstOutput
from openvino.pyopenvino import layout_helpers from openvino.pyopenvino import layout_helpers
from openvino.pyopenvino import OVAny
from openvino.pyopenvino import RTMap from openvino.pyopenvino import RTMap
from openvino.runtime.ie_api import Core from openvino.runtime.ie_api import Core
from openvino.runtime.ie_api import CompiledModel from openvino.runtime.ie_api import CompiledModel
from openvino.runtime.ie_api import InferRequest from openvino.runtime.ie_api import InferRequest
from openvino.runtime.ie_api import AsyncInferQueue from openvino.runtime.ie_api import AsyncInferQueue
from openvino.runtime.ie_api import OVAny
from openvino.pyopenvino import Version from openvino.pyopenvino import Version
from openvino.pyopenvino import Parameter
from openvino.pyopenvino import Tensor from openvino.pyopenvino import Tensor
from openvino.pyopenvino import Extension from openvino.pyopenvino import Extension
from openvino.pyopenvino import ProfilingInfo from openvino.pyopenvino import ProfilingInfo

View File

@ -11,7 +11,6 @@ from openvino.pyopenvino import InferRequest as InferRequestBase
from openvino.pyopenvino import AsyncInferQueue as AsyncInferQueueBase from openvino.pyopenvino import AsyncInferQueue as AsyncInferQueueBase
from openvino.pyopenvino import ConstOutput from openvino.pyopenvino import ConstOutput
from openvino.pyopenvino import Tensor from openvino.pyopenvino import Tensor
from openvino.pyopenvino import OVAny as OVAnyBase
from openvino.runtime.utils.types import get_dtype from openvino.runtime.utils.types import get_dtype
@ -345,48 +344,3 @@ def compile_model(model_path: str) -> CompiledModel:
""" """
core = Core() core = Core()
return core.compile_model(model_path, "AUTO") return core.compile_model(model_path, "AUTO")
class OVAny(OVAnyBase):
"""OVAny wrapper.
Wrapper provides some useful overloads for simple built-in Python types.
Access to the `OVAny` value is direct if it is a built-in Python data type.
:Example:
.. code-block:: ipython
any = OVAny([1, 2])
print(any[0])
Output: 2
Otherwise if `OVAny` value is a custom data type (for example user class),
access to the value is possible by `get()` method or property 'value'.
:Example:
.. code-block:: python
class Test:
def __init__(self):
self.data = "test"
any = OVAny(Test())
print(any.value.data)
"""
def __getitem__(self, key: Union[str, int]) -> Any:
return self.value[key]
def __get__(self) -> Any:
return self.value
def __setitem__(self, key: Union[str, int], val: Any) -> None:
self.value[key] = val
def __set__(self, val: Any) -> None:
self.value = val
def __len__(self) -> int:
return len(self.value)

View File

@ -293,138 +293,6 @@ void set_request_tensors(ov::InferRequest& request, const py::dict& inputs) {
} }
} }
PyAny from_ov_any(const ov::Any& any) {
// Check for py::object
if (any.is<py::object>()) {
return any.as<py::object>();
}
// Check for std::string
else if (any.is<std::string>()) {
return PyUnicode_FromString(any.as<std::string>().c_str());
}
// Check for int
else if (any.is<int>()) {
auto val = any.as<int>();
return PyLong_FromLong((long)val);
} else if (any.is<int64_t>()) {
auto val = any.as<int64_t>();
return PyLong_FromLong((long)val);
}
// Check for unsinged int
else if (any.is<unsigned int>()) {
auto val = any.as<unsigned int>();
return PyLong_FromLong((unsigned long)val);
}
// Check for float
else if (any.is<float>()) {
auto val = any.as<float>();
return PyFloat_FromDouble((double)val);
} else if (any.is<double>()) {
auto val = any.as<double>();
return PyFloat_FromDouble(val);
}
// Check for bool
else if (any.is<bool>()) {
auto val = any.as<bool>();
return val ? Py_True : Py_False;
}
// Check for std::vector<std::string>
else if (any.is<std::vector<std::string>>()) {
auto val = any.as<std::vector<std::string>>();
PyObject* list = PyList_New(0);
for (const auto& it : val) {
PyObject* str_val = PyUnicode_FromString(it.c_str());
PyList_Append(list, str_val);
}
return list;
}
// Check for std::vector<int>
else if (any.is<std::vector<int>>()) {
auto val = any.as<std::vector<int>>();
PyObject* list = PyList_New(0);
for (const auto& it : val) {
PyList_Append(list, PyLong_FromLong(it));
}
return list;
}
// Check for std::vector<int64_t>
else if (any.is<std::vector<int64_t>>()) {
auto val = any.as<std::vector<int64_t>>();
PyObject* list = PyList_New(0);
for (const auto& it : val) {
PyList_Append(list, PyLong_FromLong(it));
}
return list;
}
// Check for std::vector<unsigned int>
else if (any.is<std::vector<unsigned int>>()) {
auto val = any.as<std::vector<unsigned int>>();
PyObject* list = PyList_New(0);
for (const auto& it : val) {
PyList_Append(list, PyLong_FromLong(it));
}
return list;
}
// Check for std::vector<float>
else if (any.is<std::vector<float>>()) {
auto val = any.as<std::vector<float>>();
PyObject* list = PyList_New(0);
for (const auto& it : val) {
PyList_Append(list, PyFloat_FromDouble((double)it));
}
return list;
}
// Check for std::tuple<unsigned int, unsigned int>
else if (any.is<std::tuple<unsigned int, unsigned int>>()) {
auto val = any.as<std::tuple<unsigned int, unsigned int>>();
PyObject* tuple = PyTuple_New(2);
PyTuple_SetItem(tuple, 0, PyLong_FromUnsignedLong((unsigned long)std::get<0>(val)));
PyTuple_SetItem(tuple, 1, PyLong_FromUnsignedLong((unsigned long)std::get<1>(val)));
return tuple;
}
// Check for std::tuple<unsigned int, unsigned int, unsigned int>
else if (any.is<std::tuple<unsigned int, unsigned int, unsigned int>>()) {
auto val = any.as<std::tuple<unsigned int, unsigned int, unsigned int>>();
PyObject* tuple = PyTuple_New(3);
PyTuple_SetItem(tuple, 0, PyLong_FromUnsignedLong((unsigned long)std::get<0>(val)));
PyTuple_SetItem(tuple, 1, PyLong_FromUnsignedLong((unsigned long)std::get<1>(val)));
PyTuple_SetItem(tuple, 2, PyLong_FromUnsignedLong((unsigned long)std::get<2>(val)));
return tuple;
}
// Check for std::map<std::string, std::string>
else if (any.is<std::map<std::string, std::string>>()) {
auto val = any.as<std::map<std::string, std::string>>();
PyObject* dict = PyDict_New();
for (const auto& it : val) {
PyDict_SetItemString(dict, it.first.c_str(), PyUnicode_FromString(it.second.c_str()));
}
return dict;
}
// Check for std::map<std::string, int>
else if (any.is<std::map<std::string, int>>()) {
auto val = any.as<std::map<std::string, int>>();
PyObject* dict = PyDict_New();
for (const auto& it : val) {
PyDict_SetItemString(dict, it.first.c_str(), PyLong_FromLong((long)it.second));
}
return dict;
}
// Check for std::vector<ov::PropertyName>
else if (any.is<std::vector<ov::PropertyName>>()) {
auto val = any.as<std::vector<ov::PropertyName>>();
PyObject* dict = PyDict_New();
for (const auto& it : val) {
std::string property_name = it;
std::string mutability = it.is_mutable() ? "RW" : "RO";
PyDict_SetItemString(dict, property_name.c_str(), PyUnicode_FromString(mutability.c_str()));
}
return dict;
} else {
PyErr_SetString(PyExc_TypeError, "Failed to convert parameter to Python representation!");
return (PyObject*)NULL;
}
}
uint32_t get_optimal_number_of_requests(const ov::CompiledModel& actual) { uint32_t get_optimal_number_of_requests(const ov::CompiledModel& actual) {
try { try {
auto supported_properties = actual.get_property(ov::supported_properties); auto supported_properties = actual.get_property(ov::supported_properties);

View File

@ -14,11 +14,9 @@
#include <iterator> #include <iterator>
#include "Python.h" #include "Python.h"
#include "ie_common.h"
#include "openvino/runtime/compiled_model.hpp" #include "openvino/runtime/compiled_model.hpp"
#include "openvino/runtime/infer_request.hpp" #include "openvino/runtime/infer_request.hpp"
#include "openvino/runtime/tensor.hpp" #include "openvino/runtime/tensor.hpp"
#include "openvino/runtime/properties.hpp"
#include "openvino/pass/serialize.hpp" #include "openvino/pass/serialize.hpp"
#include "pyopenvino/core/containers.hpp" #include "pyopenvino/core/containers.hpp"
#include "pyopenvino/graph/any.hpp" #include "pyopenvino/graph/any.hpp"
@ -50,8 +48,6 @@ const Containers::TensorIndexMap cast_to_tensor_index_map(const py::dict& inputs
void set_request_tensors(ov::InferRequest& request, const py::dict& inputs); void set_request_tensors(ov::InferRequest& request, const py::dict& inputs);
PyAny from_ov_any(const ov::Any& any);
uint32_t get_optimal_number_of_requests(const ov::CompiledModel& actual); uint32_t get_optimal_number_of_requests(const ov::CompiledModel& actual);
py::dict outputs_to_dict(const std::vector<ov::Output<const ov::Node>>& outputs, ov::InferRequest& request); py::dict outputs_to_dict(const std::vector<ov::Output<const ov::Node>>& outputs, ov::InferRequest& request);

View File

@ -7,11 +7,10 @@
#include <pybind11/iostream.h> #include <pybind11/iostream.h>
#include <pybind11/stl.h> #include <pybind11/stl.h>
#include <pyopenvino/graph/any.hpp>
#include "common.hpp" #include "common.hpp"
#include "pyopenvino/core/containers.hpp" #include "pyopenvino/core/containers.hpp"
#include "pyopenvino/core/infer_request.hpp" #include "pyopenvino/core/infer_request.hpp"
#include "pyopenvino/utils/utils.hpp"
PYBIND11_MAKE_OPAQUE(Containers::TensorIndexMap); PYBIND11_MAKE_OPAQUE(Containers::TensorIndexMap);
PYBIND11_MAKE_OPAQUE(Containers::TensorNameMap); PYBIND11_MAKE_OPAQUE(Containers::TensorNameMap);
@ -148,9 +147,9 @@ void regclass_CompiledModel(py::module m) {
cls.def( cls.def(
"set_property", "set_property",
[](ov::CompiledModel& self, const std::map<std::string, py::object>& properties) { [](ov::CompiledModel& self, const std::map<std::string, py::object>& properties) {
std::map<std::string, PyAny> properties_to_cpp; std::map<std::string, ov::Any> properties_to_cpp;
for (const auto& property : properties) { for (const auto& property : properties) {
properties_to_cpp[property.first] = PyAny(property.second); properties_to_cpp[property.first] = ov::Any(py_object_to_any(property.second));
} }
self.set_property({properties_to_cpp.begin(), properties_to_cpp.end()}); self.set_property({properties_to_cpp.begin(), properties_to_cpp.end()});
}, },
@ -166,7 +165,7 @@ void regclass_CompiledModel(py::module m) {
cls.def( cls.def(
"get_property", "get_property",
[](ov::CompiledModel& self, const std::string& name) -> py::object { [](ov::CompiledModel& self, const std::string& name) -> py::object {
return Common::from_ov_any(self.get_property(name)).as<py::object>(); return Common::utils::from_ov_any(self.get_property(name));
}, },
py::arg("name"), py::arg("name"),
R"( R"(

View File

@ -10,9 +10,9 @@
#include <openvino/core/any.hpp> #include <openvino/core/any.hpp>
#include <openvino/runtime/core.hpp> #include <openvino/runtime/core.hpp>
#include <pyopenvino/core/tensor.hpp> #include <pyopenvino/core/tensor.hpp>
#include <pyopenvino/graph/any.hpp>
#include "common.hpp" #include "common.hpp"
#include "pyopenvino/utils/utils.hpp"
namespace py = pybind11; namespace py = pybind11;
@ -35,9 +35,9 @@ void regclass_Core(py::module m) {
cls.def( cls.def(
"set_property", "set_property",
[](ov::Core& self, const std::map<std::string, py::object>& properties) { [](ov::Core& self, const std::map<std::string, py::object>& properties) {
std::map<std::string, PyAny> properties_to_cpp; std::map<std::string, ov::Any> properties_to_cpp;
for (const auto& property : properties) { for (const auto& property : properties) {
properties_to_cpp[property.first] = PyAny(property.second); properties_to_cpp[property.first] = ov::Any(py_object_to_any(property.second));
} }
self.set_property({properties_to_cpp.begin(), properties_to_cpp.end()}); self.set_property({properties_to_cpp.begin(), properties_to_cpp.end()});
}, },
@ -52,9 +52,9 @@ void regclass_Core(py::module m) {
cls.def( cls.def(
"set_property", "set_property",
[](ov::Core& self, const std::string& device_name, const std::map<std::string, py::object>& properties) { [](ov::Core& self, const std::string& device_name, const std::map<std::string, py::object>& properties) {
std::map<std::string, PyAny> properties_to_cpp; std::map<std::string, ov::Any> properties_to_cpp;
for (const auto& property : properties) { for (const auto& property : properties) {
properties_to_cpp[property.first] = PyAny(property.second); properties_to_cpp[property.first] = ov::Any(py_object_to_any(property.second));
} }
self.set_property(device_name, {properties_to_cpp.begin(), properties_to_cpp.end()}); self.set_property(device_name, {properties_to_cpp.begin(), properties_to_cpp.end()});
}, },
@ -388,7 +388,7 @@ void regclass_Core(py::module m) {
cls.def( cls.def(
"get_property", "get_property",
[](ov::Core& self, const std::string& device_name, const std::string& name) -> py::object { [](ov::Core& self, const std::string& device_name, const std::string& name) -> py::object {
return Common::from_ov_any(self.get_property(device_name, name)).as<py::object>(); return Common::utils::from_ov_any(self.get_property(device_name, name));
}, },
py::arg("device_name"), py::arg("device_name"),
py::arg("name"), py::arg("name"),

View File

@ -1,16 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "pyopenvino/core/ie_parameter.hpp"
#include <ie_parameter.hpp>
namespace py = pybind11;
void regclass_Parameter(py::module m) {
py::class_<InferenceEngine::Parameter, std::shared_ptr<InferenceEngine::Parameter>> cls(m, "Parameter");
cls.def(py::init<const char*>());
cls.def(py::init<std::string>());
}

View File

@ -1,11 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <pybind11/pybind11.h>
namespace py = pybind11;
void regclass_Parameter(py::module m);

View File

@ -6,37 +6,64 @@
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include "pyopenvino/core/common.hpp"
#include "pyopenvino/graph/any.hpp" #include "pyopenvino/graph/any.hpp"
#include "pyopenvino/utils/utils.hpp"
namespace py = pybind11; namespace py = pybind11;
void regclass_graph_Any(py::module m) { void regclass_graph_Any(py::module m) {
py::class_<PyAny, std::shared_ptr<PyAny>> ov_any(m, "OVAny", py::module_local()); py::class_<ov::Any, std::shared_ptr<ov::Any>> ov_any(m, "OVAny");
ov_any.doc() = "openvino.runtime.OVAny provides object wrapper for OpenVINO" ov_any.doc() = "openvino.runtime.OVAny provides object wrapper for OpenVINO"
"ov::Any class. It allows to pass different types of objects" "ov::Any class. It allows to pass different types of objects"
"into C++ based core of the project."; "into C++ based core of the project.";
ov_any.def(py::init<py::object>()); ov_any.def(py::init([](py::object& input_value) {
return ov::Any(py_object_to_any(input_value));
}));
ov_any.def("__repr__", [](const PyAny& self) { ov_any.def("__repr__", [](const ov::Any& self) {
std::stringstream ret; std::stringstream ret;
self.print(ret); self.print(ret);
return ret.str(); return ret.str();
}); });
ov_any.def("__eq__", [](const PyAny& a, const PyAny& b) -> bool {
ov_any.def("__getitem__", [](const ov::Any& self, py::object& k) {
return Common::utils::from_ov_any(self)[k];
});
ov_any.def("__setitem__", [](const ov::Any& self, py::object& k, const std::string& v) {
Common::utils::from_ov_any(self)[k] = v;
});
ov_any.def("__setitem__", [](const ov::Any& self, py::object& k, const int64_t& v) {
Common::utils::from_ov_any(self)[k] = v;
});
ov_any.def("__get__", [](const ov::Any& self) {
return Common::utils::from_ov_any(self);
});
ov_any.def("__set__", [](const ov::Any& self, const ov::Any& val) {
Common::utils::from_ov_any(self) = Common::utils::from_ov_any(val);
});
ov_any.def("__len__", [](const ov::Any& self) {
py::handle some_object = Common::utils::from_ov_any(self);
PyObject* source = some_object.ptr();
return PyObject_Length(source);
});
ov_any.def("__eq__", [](const ov::Any& a, const ov::Any& b) -> bool {
return a == b; return a == b;
}); });
ov_any.def("__eq__", [](const PyAny& a, const ov::Any& b) -> bool { ov_any.def("__eq__", [](const ov::Any& a, py::object& b) -> bool {
return a == b; return a == ov::Any(py_object_to_any(b));
});
ov_any.def("__eq__", [](const PyAny& a, py::object b) -> bool {
return a == PyAny(b);
}); });
ov_any.def( ov_any.def(
"get", "get",
[](const PyAny& self) -> py::object { [](const ov::Any& self) -> py::object {
return self.as<py::object>(); return Common::utils::from_ov_any(self);
}, },
R"( R"(
:return: Value of this OVAny. :return: Value of this OVAny.
@ -44,8 +71,8 @@ void regclass_graph_Any(py::module m) {
)"); )");
ov_any.def( ov_any.def(
"set", "set",
[](PyAny& self, py::object value) { [](ov::Any& self, py::object& value) {
self = PyAny(value); self = ov::Any(py_object_to_any(value));
}, },
R"( R"(
:param: Value to be set in OVAny. :param: Value to be set in OVAny.
@ -53,8 +80,8 @@ void regclass_graph_Any(py::module m) {
)"); )");
ov_any.def_property_readonly( ov_any.def_property_readonly(
"value", "value",
[](const PyAny& self) { [](const ov::Any& self) {
return self.as<py::object>(); return Common::utils::from_ov_any(self);
}, },
R"( R"(
:return: Value of this OVAny. :return: Value of this OVAny.

View File

@ -6,21 +6,6 @@
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include <sstream>
#include <string>
#include "Python.h"
#include "openvino/core/any.hpp" // ov::RuntimeAttribute
namespace py = pybind11; namespace py = pybind11;
void regclass_graph_Any(py::module m); void regclass_graph_Any(py::module m);
class PyAny : public ov::Any {
public:
using ov::Any::Any;
PyAny() = default;
PyAny(py::object object) : ov::Any(object) {}
PyAny(PyObject* object) : ov::Any(py::reinterpret_borrow<py::object>(object)) {}
PyAny(const ov::Any& any) : ov::Any(any) {}
};

View File

@ -4,6 +4,7 @@
#include "pyopenvino/graph/rt_map.hpp" #include "pyopenvino/graph/rt_map.hpp"
#include <pybind11/operators.h>
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include <pybind11/stl.h> #include <pybind11/stl.h>
#include <pybind11/stl_bind.h> #include <pybind11/stl_bind.h>
@ -18,6 +19,7 @@
#include "pyopenvino/core/common.hpp" #include "pyopenvino/core/common.hpp"
#include "pyopenvino/graph/any.hpp" #include "pyopenvino/graph/any.hpp"
#include "pyopenvino/graph/node.hpp" #include "pyopenvino/graph/node.hpp"
#include "pyopenvino/utils/utils.hpp"
namespace py = pybind11; namespace py = pybind11;
@ -37,7 +39,7 @@ void regclass_graph_PyRTMap(py::module m) {
m[k] = v; m[k] = v;
}); });
py_map.def("__getitem__", [](PyRTMap& m, const std::string& k) -> py::object { py_map.def("__getitem__", [](PyRTMap& m, const std::string& k) -> py::object {
return Common::from_ov_any(m[k]).as<py::object>(); return Common::utils::from_ov_any(m[k]);
}); });
py_map.def( py_map.def(
"__bool__", "__bool__",

View File

@ -26,7 +26,6 @@
#include "pyopenvino/core/containers.hpp" #include "pyopenvino/core/containers.hpp"
#include "pyopenvino/core/core.hpp" #include "pyopenvino/core/core.hpp"
#include "pyopenvino/core/extension.hpp" #include "pyopenvino/core/extension.hpp"
#include "pyopenvino/core/ie_parameter.hpp"
#include "pyopenvino/core/infer_request.hpp" #include "pyopenvino/core/infer_request.hpp"
#include "pyopenvino/core/offline_transformations.hpp" #include "pyopenvino/core/offline_transformations.hpp"
#include "pyopenvino/core/profiling_info.hpp" #include "pyopenvino/core/profiling_info.hpp"
@ -199,7 +198,6 @@ PYBIND11_MODULE(pyopenvino, m) {
regclass_InferRequest(m); regclass_InferRequest(m);
regclass_VariableState(m); regclass_VariableState(m);
regclass_Version(m); regclass_Version(m);
regclass_Parameter(m);
regclass_AsyncInferQueue(m); regclass_AsyncInferQueue(m);
regclass_ProfilingInfo(m); regclass_ProfilingInfo(m);
regclass_Extension(m); regclass_Extension(m);

View File

@ -0,0 +1,155 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "pyopenvino/utils/utils.hpp"
#include <pybind11/stl.h>
#include <map>
#include <string>
#include <tuple>
#include <vector>
#include "Python.h"
#include "openvino/runtime/properties.hpp"
namespace Common {
namespace utils {
py::object from_ov_any(const ov::Any& any) {
// Check for py::object
if (any.is<py::object>()) {
return any.as<py::object>();
}
// Check for std::string
else if (any.is<std::string>()) {
return py::cast<py::object>(PyUnicode_FromString(any.as<std::string>().c_str()));
}
// Check for int
else if (any.is<int>()) {
return py::cast<py::object>(PyLong_FromLong(any.as<int>()));
} else if (any.is<int64_t>()) {
return py::cast<py::object>(PyLong_FromLong(any.as<int64_t>()));
}
// Check for unsigned int
else if (any.is<unsigned int>()) {
return py::cast<py::object>(PyLong_FromLong(any.as<unsigned int>()));
}
// Check for float
else if (any.is<float>()) {
return py::cast<py::object>(PyFloat_FromDouble(any.as<float>()));
} else if (any.is<double>()) {
return py::cast<py::object>(PyFloat_FromDouble(any.as<double>()));
}
// Check for bool
else if (any.is<bool>()) {
return py::cast<py::object>(any.as<bool>() ? Py_True : Py_False);
}
// Check for std::vector<std::string>
else if (any.is<std::vector<std::string>>()) {
auto val = any.as<std::vector<std::string>>();
PyObject* list = PyList_New(0);
for (const auto& it : val) {
PyObject* str_val = PyUnicode_FromString(it.c_str());
PyList_Append(list, str_val);
}
return py::cast<py::object>(list);
}
// Check for std::vector<int>
else if (any.is<std::vector<int>>()) {
auto val = any.as<std::vector<int>>();
PyObject* list = PyList_New(0);
for (const auto& it : val) {
PyList_Append(list, PyLong_FromLong(it));
}
return py::cast<py::object>(list);
}
// Check for std::vector<int64_t>
else if (any.is<std::vector<int64_t>>()) {
auto val = any.as<std::vector<int64_t>>();
PyObject* list = PyList_New(0);
for (const auto& it : val) {
PyList_Append(list, PyLong_FromLong(it));
}
return py::cast<py::object>(list);
}
// Check for std::vector<unsigned int>
else if (any.is<std::vector<unsigned int>>()) {
auto val = any.as<std::vector<unsigned int>>();
PyObject* list = PyList_New(0);
for (const auto& it : val) {
PyList_Append(list, PyLong_FromLong(it));
}
return py::cast<py::object>(list);
}
// Check for std::vector<float>
else if (any.is<std::vector<float>>()) {
auto val = any.as<std::vector<float>>();
PyObject* list = PyList_New(0);
for (const auto& it : val) {
PyList_Append(list, PyFloat_FromDouble((double)it));
}
return py::cast<py::object>(list);
}
// Check for std::vector<double>
else if (any.is<std::vector<double>>()) {
auto val = any.as<std::vector<double>>();
PyObject* list = PyList_New(0);
for (const auto& it : val) {
PyList_Append(list, PyFloat_FromDouble(it));
}
return py::cast<py::object>(list);
}
// Check for std::tuple<unsigned int, unsigned int>
else if (any.is<std::tuple<unsigned int, unsigned int>>()) {
auto val = any.as<std::tuple<unsigned int, unsigned int>>();
PyObject* tuple = PyTuple_New(2);
PyTuple_SetItem(tuple, 0, PyLong_FromUnsignedLong((unsigned long)std::get<0>(val)));
PyTuple_SetItem(tuple, 1, PyLong_FromUnsignedLong((unsigned long)std::get<1>(val)));
return py::cast<py::object>(tuple);
}
// Check for std::tuple<unsigned int, unsigned int, unsigned int>
else if (any.is<std::tuple<unsigned int, unsigned int, unsigned int>>()) {
auto val = any.as<std::tuple<unsigned int, unsigned int, unsigned int>>();
PyObject* tuple = PyTuple_New(3);
PyTuple_SetItem(tuple, 0, PyLong_FromUnsignedLong((unsigned long)std::get<0>(val)));
PyTuple_SetItem(tuple, 1, PyLong_FromUnsignedLong((unsigned long)std::get<1>(val)));
PyTuple_SetItem(tuple, 2, PyLong_FromUnsignedLong((unsigned long)std::get<2>(val)));
return py::cast<py::object>(tuple);
}
// Check for std::map<std::string, std::string>
else if (any.is<std::map<std::string, std::string>>()) {
auto val = any.as<std::map<std::string, std::string>>();
PyObject* dict = PyDict_New();
for (const auto& it : val) {
PyDict_SetItemString(dict, it.first.c_str(), PyUnicode_FromString(it.second.c_str()));
}
return py::cast<py::object>(dict);
}
// Check for std::map<std::string, int>
else if (any.is<std::map<std::string, int>>()) {
auto val = any.as<std::map<std::string, int>>();
PyObject* dict = PyDict_New();
for (const auto& it : val) {
PyDict_SetItemString(dict, it.first.c_str(), PyLong_FromLong((long)it.second));
}
return py::cast<py::object>(dict);
}
// Check for std::vector<ov::PropertyName>
else if (any.is<std::vector<ov::PropertyName>>()) {
auto val = any.as<std::vector<ov::PropertyName>>();
PyObject* dict = PyDict_New();
for (const auto& it : val) {
std::string property_name = it;
std::string mutability = it.is_mutable() ? "RW" : "RO";
PyDict_SetItemString(dict, property_name.c_str(), PyUnicode_FromString(mutability.c_str()));
}
return py::cast<py::object>(dict);
} else {
PyErr_SetString(PyExc_TypeError, "Failed to convert parameter to Python representation!");
return py::cast<py::object>((PyObject*)NULL);
}
}
}; // namespace utils
}; // namespace Common

View File

@ -7,26 +7,29 @@
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include <openvino/core/any.hpp> #include <openvino/core/any.hpp>
ov::Any py_object_to_any(const pybind11::object& py_obj) { namespace py = pybind11;
if (pybind11::isinstance<pybind11::str>(py_obj)) {
namespace Common {
namespace utils {
py::object from_ov_any(const ov::Any &any);
}; // namespace utils
}; // namespace Common
inline ov::Any py_object_to_any(const py::object& py_obj) {
if (py::isinstance<py::str>(py_obj)) {
return py_obj.cast<std::string>(); return py_obj.cast<std::string>();
} else if (pybind11::isinstance<pybind11::bool_>(py_obj)) { } else if (py::isinstance<py::bool_>(py_obj)) {
return py_obj.cast<bool>(); return py_obj.cast<bool>();
} else if (pybind11::isinstance<pybind11::float_>(py_obj)) { } else if (py::isinstance<py::float_>(py_obj)) {
return py_obj.cast<double>(); return py_obj.cast<double>();
} else if (pybind11::isinstance<pybind11::int_>(py_obj)) { } else if (py::isinstance<py::int_>(py_obj)) {
return py_obj.cast<int64_t>(); return py_obj.cast<int64_t>();
} else if (pybind11::isinstance<pybind11::list>(py_obj)) { } else if (py::isinstance<py::list>(py_obj)) {
auto _list = py_obj.cast<pybind11::list>(); auto _list = py_obj.cast<py::list>();
enum class PY_TYPE : int { enum class PY_TYPE : int { UNKNOWN = 0, STR, INT, FLOAT, BOOL };
UNKNOWN = 0,
STR,
INT,
FLOAT,
BOOL
};
PY_TYPE detected_type = PY_TYPE::UNKNOWN; PY_TYPE detected_type = PY_TYPE::UNKNOWN;
for (const auto &it: _list) { for (const auto& it : _list) {
auto check_type = [&](PY_TYPE type) { auto check_type = [&](PY_TYPE type) {
if (detected_type == PY_TYPE::UNKNOWN || detected_type == type) { if (detected_type == PY_TYPE::UNKNOWN || detected_type == type) {
detected_type = type; detected_type = type;
@ -34,13 +37,13 @@ ov::Any py_object_to_any(const pybind11::object& py_obj) {
} }
OPENVINO_ASSERT("Incorrect attribute. Mixed types in the list are not allowed."); OPENVINO_ASSERT("Incorrect attribute. Mixed types in the list are not allowed.");
}; };
if (pybind11::isinstance<pybind11::str>(it)) { if (py::isinstance<py::str>(it)) {
check_type(PY_TYPE::STR); check_type(PY_TYPE::STR);
} else if (pybind11::isinstance<pybind11::int_>(it)) { } else if (py::isinstance<py::int_>(it)) {
check_type(PY_TYPE::INT); check_type(PY_TYPE::INT);
} else if (pybind11::isinstance<pybind11::float_>(it)) { } else if (py::isinstance<py::float_>(it)) {
check_type(PY_TYPE::FLOAT); check_type(PY_TYPE::FLOAT);
} else if (pybind11::isinstance<pybind11::bool_>(it)) { } else if (py::isinstance<py::bool_>(it)) {
check_type(PY_TYPE::BOOL); check_type(PY_TYPE::BOOL);
} }
} }
@ -57,6 +60,9 @@ ov::Any py_object_to_any(const pybind11::object& py_obj) {
default: default:
OPENVINO_ASSERT(false, "Unsupported attribute type."); OPENVINO_ASSERT(false, "Unsupported attribute type.");
} }
} else if (py::isinstance<py::object>(py_obj)) {
return py_obj;
} }
OPENVINO_ASSERT(false, "Unsupported attribute type."); OPENVINO_ASSERT(false, "Unsupported attribute type.");
} }

View File

@ -4,12 +4,12 @@
import os import os
from ..conftest import model_path from ..conftest import model_path
from openvino.runtime import Input, Shape, PartialShape, Type, Parameter, \ from openvino.runtime import Input, Shape, PartialShape, Type, \
RTMap RTMap
from openvino.pyopenvino import DescriptorTensor from openvino.pyopenvino import DescriptorTensor
import openvino.runtime.opset8 as ops import openvino.runtime.opset8 as ops
from openvino.runtime import Core from openvino.runtime import Core, OVAny
is_myriad = os.environ.get("TEST_DEVICE") == "MYRIAD" is_myriad = os.environ.get("TEST_DEVICE") == "MYRIAD"
test_net_xml, test_net_bin = model_path(is_myriad) test_net_xml, test_net_bin = model_path(is_myriad)
@ -146,4 +146,4 @@ def test_input_update_rt_info(device):
rt["test12345"] = "test" rt["test12345"] = "test"
for k, v in input_node.get_rt_info().items(): for k, v in input_node.get_rt_info().items():
assert k == "test12345" assert k == "test12345"
assert isinstance(v, Parameter) assert isinstance(v, OVAny)

View File

@ -6,7 +6,7 @@ import os
from ..conftest import model_path from ..conftest import model_path
import openvino.runtime.opset8 as ops import openvino.runtime.opset8 as ops
from openvino.runtime import ConstOutput, Shape, PartialShape, Type, \ from openvino.runtime import ConstOutput, Shape, PartialShape, Type, \
Output, Parameter, RTMap Output, RTMap, OVAny
from openvino.runtime import Core from openvino.runtime import Core
@ -130,7 +130,7 @@ def test_update_rt_info(device):
rt["test12345"] = "test" rt["test12345"] = "test"
for k, v in output_node.get_rt_info().items(): for k, v in output_node.get_rt_info().items():
assert k == "test12345" assert k == "test12345"
assert isinstance(v, Parameter) assert isinstance(v, OVAny)
def test_operations(): def test_operations():

View File

@ -42,11 +42,6 @@ def test_any_float_list():
assert isinstance(v[0], float) assert isinstance(v[0], float)
def test_any_tuple():
v = OVAny((2, 1))
assert isinstance(v.value, tuple)
def test_any_bool(): def test_any_bool():
v = OVAny(False) v = OVAny(False)
assert isinstance(v.value, bool) assert isinstance(v.value, bool)

View File

@ -9,12 +9,10 @@ import pytest
import openvino.runtime.opset8 as ops import openvino.runtime.opset8 as ops
import openvino.runtime as ov import openvino.runtime as ov
from openvino.pyopenvino import OVAny
from openvino.runtime.exceptions import UserInputError from openvino.runtime.exceptions import UserInputError
from openvino.runtime import Model, PartialShape, Shape, Type, layout_helpers from openvino.runtime import Model, PartialShape, Shape, Type, layout_helpers
from openvino.runtime import Strides, AxisVector, Coordinate, CoordinateDiff from openvino.runtime import Strides, AxisVector, Coordinate, CoordinateDiff
from openvino.runtime import Tensor from openvino.runtime import Tensor, OVAny
from openvino.pyopenvino import DescriptorTensor from openvino.pyopenvino import DescriptorTensor
from openvino.runtime.op import Parameter from openvino.runtime.op import Parameter
from tests.runtime import get_runtime from tests.runtime import get_runtime