Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[Py OV] Bind ov::TensorVector as TensorVector to enable pass-by-reference (#31424)

Alicja Miloszewska190f50d6b6d3
diff --git a/src/bindings/python/src/openvino/__init__.py b/src/bindings/python/src/openvino/__init__.py
index d5594ee4227..572d4532661 100644
--- a/src/bindings/python/src/openvino/__init__.py
+++ b/src/bindings/python/src/openvino/__init__.py
@@ -40,6 +40,7 @@ from openvino._pyopenvino import PartialShape
from openvino._pyopenvino import Shape
from openvino._pyopenvino import Layout
from openvino._pyopenvino import Tensor
+from openvino._pyopenvino import TensorVector
from openvino._pyopenvino import Type
from openvino._pyopenvino import OVAny
from openvino._pyopenvino import get_batch
diff --git a/src/bindings/python/src/openvino/_ov_api.py b/src/bindings/python/src/openvino/_ov_api.py
index 7ab9d2521d2..7133f180dee 100644
--- a/src/bindings/python/src/openvino/_ov_api.py
+++ b/src/bindings/python/src/openvino/_ov_api.py
@@ -14,7 +14,7 @@ from openvino._pyopenvino import Model as ModelBase
from openvino._pyopenvino import Core as CoreBase
from openvino._pyopenvino import CompiledModel as CompiledModelBase
from openvino._pyopenvino import AsyncInferQueue as AsyncInferQueueBase
-from openvino._pyopenvino import Node, Tensor, Type
+from openvino._pyopenvino import Node, Tensor, Type, RTMap, TensorVector
from openvino.utils.data_helpers import (
OVDict,
@@ -89,6 +89,17 @@ class Model(object, metaclass=ModelMeta):
wrapper_methods = ["__copy__", "__deepcopy__", "__dict__", "__enter__", "__exit__", "__getattr__", "__weakref__"]
return dir(self.__model) + wrapper_methods
+ def evaluate(
+ self,
+ output_tensors: Union[list[Tensor], TensorVector],
+ input_tensors: Union[list[Tensor], TensorVector],
+ evaluation_context: Optional[RTMap] = None,
+ ) -> bool:
+ outputs = output_tensors if isinstance(output_tensors, TensorVector) else TensorVector(output_tensors)
+ inputs = input_tensors if isinstance(input_tensors, TensorVector) else TensorVector(input_tensors)
+ context = evaluation_context if evaluation_context is not None else RTMap()
+ return self.__model.evaluate(outputs, inputs, context)
+
class InferRequest(_InferRequestWrapper):
"""InferRequest class represents infer request which can be run in asynchronous or synchronous manners."""
diff --git a/src/bindings/python/src/openvino/utils/postponed_constant.py b/src/bindings/python/src/openvino/utils/postponed_constant.py
index d1041d5fc8c..6c7db5b8e76 100644
--- a/src/bindings/python/src/openvino/utils/postponed_constant.py
+++ b/src/bindings/python/src/openvino/utils/postponed_constant.py
@@ -3,14 +3,43 @@
# SPDX-License-Identifier: Apache-2.0
-from typing import Optional
+from typing import Optional, cast, overload
from collections.abc import Callable
-from openvino import Op, Type, Shape, Tensor, PartialShape
+from openvino import Op, Type, Shape, Tensor, PartialShape, TensorVector
class PostponedConstant(Op):
"""Postponed Constant is a way to materialize a big constant only when it is going to be serialized to IR and then immediately dispose."""
+ @overload
+ def __init__(self, element_type: Type, shape: Shape, maker: Callable[[], Tensor], name: Optional[str] = None) -> None:
+ ...
+
+ @overload
def __init__(self, element_type: Type, shape: Shape, maker: Callable[[Tensor], None], name: Optional[str] = None) -> None:
+ ...
+
+ def __init__(self, element_type: Type, shape: Shape, maker: Callable, name: Optional[str] = None) -> None:
+ """Creates a PostponedConstant.
+
+ :param element_type: Element type of the constant.
+ :type element_type: openvino.Type
+ :param shape: Shape of the constant.
+ :type shape: openvino.Shape
+ :param maker: A callable that returns a Tensor or modifies the provided Tensor to represent the constant.
+ Note: It's recommended to use a callable without arguments (returns Tensor) to avoid unnecessary tensor data copies.
+ :type maker: Union[Callable[[], Tensor], Callable[[Tensor], None]]
+ :param name: Optional name for the constant.
+ :type name: Optional[str]
+
+ :Example of a maker that returns a Tensor:
+
+ .. code-block:: python
+
+ class Maker:
+ def __call__(self) -> ov.Tensor:
+ tensor_data = np.array([2, 2, 2, 2], dtype=np.float32)
+ return ov.Tensor(tensor_data)
+ """
super().__init__(self)
self.get_rt_info()["postponed_constant"] = True # value doesn't matter
self.m_element_type = element_type
@@ -20,8 +49,12 @@ class PostponedConstant(Op):
self.friendly_name = name
self.constructor_validate_and_infer_types()
- def evaluate(self, outputs: list[Tensor], _: list[Tensor]) -> bool: # type: ignore
- self.m_maker(outputs[0])
+ def evaluate(self, outputs: TensorVector, _: list[Tensor]) -> bool: # type: ignore
+ num_args = self.m_maker.__call__.__code__.co_argcount
+ if num_args == 1:
+ outputs[0] = cast(Callable[[], Tensor], self.m_maker)()
+ else:
+ cast(Callable[[Tensor], None], self.m_maker)(outputs[0])
return True
def validate_and_infer_types(self) -> None:
@@ -35,5 +68,5 @@ class PostponedConstant(Op):
# `maker` is a function that returns ov.Tensor that represents a target Constant
-def make_postponed_constant(element_type: Type, shape: Shape, maker: Callable[[Tensor], None], name: Optional[str] = None) -> Op:
+def make_postponed_constant(element_type: Type, shape: Shape, maker: Callable, name: Optional[str] = None) -> Op:
return PostponedConstant(element_type, shape, maker, name)
diff --git a/src/bindings/python/src/pyopenvino/core/infer_request.cpp b/src/bindings/python/src/pyopenvino/core/infer_request.cpp
index 39650d18d81..d79381e4602 100644
--- a/src/bindings/python/src/pyopenvino/core/infer_request.cpp
+++ b/src/bindings/python/src/pyopenvino/core/infer_request.cpp
@@ -15,6 +15,8 @@
namespace py = pybind11;
+PYBIND11_MAKE_OPAQUE(ov::TensorVector);
+
inline py::object run_sync_infer(InferRequestWrapper& self, bool share_outputs, bool decode_strings) {
{
ConditionalGILScopedRelease release;
@@ -51,7 +53,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_tensors",
- [](InferRequestWrapper& self, const std::string& tensor_name, const std::vector<ov::Tensor>& tensors) {
+ [](InferRequestWrapper& self, const std::string& tensor_name, const ov::TensorVector& tensors) {
self.m_request->set_tensors(tensor_name, tensors);
},
py::arg("tensor_name"),
@@ -63,6 +65,28 @@ void regclass_InferRequest(py::module m) {
In case if `tensor_name` is associated with output (or any other non-input node),
an exception will be thrown.
+ :param tensor_name: Name of input tensor.
+ :type tensor_name: str
+ :param tensors: Input tensors for batched infer request. The type of each tensor
+ must match the model input element type and shape (except batch dimension).
+ Total size of tensors needs to match with input's size.
+ :type tensors: openvino.TensorVector
+ )");
+ cls.def(
+ "set_tensors",
+ [](InferRequestWrapper& self, const std::string& tensor_name, const py::list& tensors) {
+ py::object pyTensorVector = py::module_::import("openvino").attr("TensorVector");
+ self.m_request->set_tensors(tensor_name, pyTensorVector(tensors).cast<ov::TensorVector>());
+ },
+ py::arg("tensor_name"),
+ py::arg("tensors"),
+ R"(
+ Sets batch of tensors for input data to infer by tensor name.
+ Model input needs to have batch dimension and the number of tensors needs to be
+ matched with batch size. Current version supports set tensors to model inputs only.
+ In case if `tensor_name` is associated with output (or any other non-input node),
+ an exception will be thrown.
+
:param tensor_name: Name of input tensor.
:type tensor_name: str
:param tensors: Input tensors for batched infer request. The type of each tensor
@@ -73,13 +97,38 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_tensors",
- [](InferRequestWrapper& self, const ov::Output<const ov::Node>& port, const std::vector<ov::Tensor>& tensors) {
+ [](InferRequestWrapper& self, const ov::Output<const ov::Node>& port, const ov::TensorVector& tensors) {
self.m_request->set_tensors(port, tensors);
},
py::arg("port"),
py::arg("tensors"),
R"(
- Sets batch of tensors for input data to infer by tensor name.
+ Sets a batch of tensors for input data to infer by input port.
+ Model input needs to have batch dimension and the number of tensors needs to be
+ matched with batch size. Current version supports set tensors to model inputs only.
+ In case if `port` is associated with output (or any other non-input node),
+ an exception will be thrown.
+
+
+ :param port: Port of input tensor.
+ :type port: openvino.ConstOutput
+ :param tensors: Input tensors for batched infer request. The type of each tensor
+ must match the model input element type and shape (except batch dimension).
+ Total size of tensors needs to match with input's size.
+ :type tensors: openvino.TensorVector
+ :rtype: None
+ )");
+
+ cls.def(
+ "set_tensors",
+ [](InferRequestWrapper& self, const ov::Output<const ov::Node>& port, const py::list& tensors) {
+ py::object pyTensorVector = py::module_::import("openvino").attr("TensorVector");
+ self.m_request->set_tensors(port, pyTensorVector(tensors).cast<ov::TensorVector>());
+ },
+ py::arg("port"),
+ py::arg("tensors"),
+ R"(
+ Sets a batch of tensors for input data to infer by input port.
Model input needs to have batch dimension and the number of tensors needs to be
matched with batch size. Current version supports set tensors to model inputs only.
In case if `port` is associated with output (or any other non-input node),
@@ -131,7 +180,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_input_tensors",
- [](InferRequestWrapper& self, const std::vector<ov::Tensor>& tensors) {
+ [](InferRequestWrapper& self, const ov::TensorVector& tensors) {
self.m_request->set_input_tensors(tensors);
},
py::arg("tensors"),
@@ -140,6 +189,24 @@ void regclass_InferRequest(py::module m) {
Model input needs to have batch dimension and the number of `tensors`
needs to match with batch size.
+ :param tensors: Input tensors for batched infer request. The type of each tensor
+ must match the model input element type and shape (except batch dimension).
+ Total size of tensors needs to match with input's size.
+ :type tensors: openvino.TensorVector
+ )");
+
+ cls.def(
+ "set_input_tensors",
+ [](InferRequestWrapper& self, const py::list& tensors) {
+ py::object pyTensorVector = py::module_::import("openvino").attr("TensorVector");
+ self.m_request->set_input_tensors(pyTensorVector(tensors).cast<ov::TensorVector>());
+ },
+ py::arg("tensors"),
+ R"(
+ Sets batch of tensors for single input data.
+ Model input needs to have batch dimension and the number of `tensors`
+ needs to match with batch size.
+
:param tensors: Input tensors for batched infer request. The type of each tensor
must match the model input element type and shape (except batch dimension).
Total size of tensors needs to match with input's size.
@@ -148,7 +215,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_input_tensors",
- [](InferRequestWrapper& self, size_t idx, const std::vector<ov::Tensor>& tensors) {
+ [](InferRequestWrapper& self, size_t idx, const ov::TensorVector& tensors) {
self.m_request->set_input_tensors(idx, tensors);
},
py::arg("idx"),
@@ -163,6 +230,28 @@ void regclass_InferRequest(py::module m) {
:param tensors: Input tensors for batched infer request. The type of each tensor
must match the model input element type and shape (except batch dimension).
Total size of tensors needs to match with input's size.
+ :type tensors: openvino.TensorVector
+ )");
+
+ cls.def(
+ "set_input_tensors",
+ [](InferRequestWrapper& self, size_t idx, const py::list& tensors) {
+ py::object pyTensorVector = py::module_::import("openvino").attr("TensorVector");
+ self.m_request->set_input_tensors(idx, pyTensorVector(tensors).cast<ov::TensorVector>());
+ },
+ py::arg("idx"),
+ py::arg("tensors"),
+ R"(
+ Sets batch of tensors for single input data to infer by index.
+ Model input needs to have batch dimension and the number of `tensors`
+ needs to match with batch size.
+
+ :param idx: Index of input tensor.
+ :type idx: int
+ :param tensors: Input tensors for batched infer request. The type of each tensor
+ must match the model input element type and shape (except batch dimension).
+ Total size of tensors needs to match with input's size.
+ :type tensors: list[openvino.Tensor]
)");
// Overload for single input, it will throw error if a model has more than one input.
@@ -691,22 +780,28 @@ void regclass_InferRequest(py::module m) {
:rtype: list[openvino.ConstOutput]
)");
- cls.def_property_readonly("input_tensors",
- &InferRequestWrapper::get_input_tensors,
- R"(
- Gets all input tensors of this InferRequest.
+ cls.def_property_readonly(
+ "input_tensors",
+ [](InferRequestWrapper& self) {
+ return py::list(py::cast(self.get_input_tensors()));
+ },
+ R"(
+ Gets all input tensors of this InferRequest.
- :rtype: list[openvino.Tensor]
- )");
+ :rtype: list[openvino.Tensor]
+ )");
- cls.def_property_readonly("output_tensors",
- &InferRequestWrapper::get_output_tensors,
- R"(
+ cls.def_property_readonly(
+ "output_tensors",
+ [](InferRequestWrapper& self) {
+ return py::list(py::cast(self.get_output_tensors()));
+ },
+ R"(
- Gets all output tensors of this InferRequest.
+ Gets all output tensors of this InferRequest.
- :rtype: list[openvino.Tensor]
- )");
+ :rtype: list[openvino.Tensor]
+ )");
cls.def_property_readonly(
"latency",
diff --git a/src/bindings/python/src/pyopenvino/core/infer_request.hpp b/src/bindings/python/src/pyopenvino/core/infer_request.hpp
index 17254975896..a8d71ba9629 100644
--- a/src/bindings/python/src/pyopenvino/core/infer_request.hpp
+++ b/src/bindings/python/src/pyopenvino/core/infer_request.hpp
@@ -59,11 +59,11 @@ public:
// ~InferRequestWrapper() = default;
- std::vector<ov::Tensor> get_input_tensors() {
+ ov::TensorVector get_input_tensors() {
return get_tensors_from(m_inputs);
}
- std::vector<ov::Tensor> get_output_tensors() {
+ ov::TensorVector get_output_tensors() {
return get_tensors_from(m_outputs);
}
@@ -86,8 +86,8 @@ public:
std::shared_ptr<Time::time_point> m_end_time;
private:
- inline std::vector<ov::Tensor> get_tensors_from(const std::vector<ov::Output<const ov::Node>>& v) {
- std::vector<ov::Tensor> tensors;
+ inline ov::TensorVector get_tensors_from(const std::vector<ov::Output<const ov::Node>>& v) {
+ ov::TensorVector tensors;
tensors.reserve(v.size());
for (auto&& node : v) {
diff --git a/src/bindings/python/src/pyopenvino/graph/model.cpp b/src/bindings/python/src/pyopenvino/graph/model.cpp
index 35c6f20da18..a4eef9fc1cd 100644
--- a/src/bindings/python/src/pyopenvino/graph/model.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/model.cpp
@@ -30,6 +30,7 @@ namespace py = pybind11;
using PyRTMap = ov::RTMap;
PYBIND11_MAKE_OPAQUE(PyRTMap);
+PYBIND11_MAKE_OPAQUE(ov::TensorVector);
namespace {
template <class... Args>
@@ -1301,9 +1302,9 @@ void regclass_graph_Model(py::module m) {
Evaluate the model on inputs, putting results in outputs
:param output_tensors: Tensors for the outputs to compute. One for each result
- :type output_tensors: list[openvino.Tensor]
+ :type output_tensors: Union[list[openvino.Tensor], TensorVector]
:param input_tensors: Tensors for the inputs. One for each inputs.
- :type input_tensors: list[openvino.Tensor]
+ :type input_tensors: Union[list[openvino.Tensor], TensorVector]
:param evaluation_context: Storage of additional settings and attributes that can be used
when evaluating the model. This additional information can be
shared across nodes.
diff --git a/src/bindings/python/src/pyopenvino/graph/node.cpp b/src/bindings/python/src/pyopenvino/graph/node.cpp
index 5b4ba58bb66..17e35849c5e 100644
--- a/src/bindings/python/src/pyopenvino/graph/node.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/node.cpp
@@ -39,6 +39,7 @@ namespace py = pybind11;
using PyRTMap = ov::Node::RTMap;
PYBIND11_MAKE_OPAQUE(PyRTMap);
+PYBIND11_MAKE_OPAQUE(ov::TensorVector);
void regclass_graph_Node(py::module m) {
py::class_<ov::Node, std::shared_ptr<ov::Node>, PyNode> node(m, "Node", py::dynamic_attr());
@@ -225,35 +226,48 @@ void regclass_graph_Node(py::module m) {
},
py::arg("output_values"),
py::arg("input_values"),
- py::arg("evaluationContext"),
+ py::arg("evaluationContext") = PyRTMap(),
R"(
Evaluate the node on inputs, putting results in outputs
:param output_tensors: Tensors for the outputs to compute. One for each result.
- :type output_tensors: list[openvino.Tensor]
+ :type output_tensors: openvino.TensorVector
:param input_tensors: Tensors for the inputs. One for each inputs.
- :type input_tensors: list[openvino.Tensor]
+ :type input_tensors: openvino.TensorVector
:param evaluation_context: Storage of additional settings and attributes that can be used
when evaluating the function. This additional information can be shared across nodes.
:type evaluation_context: openvino.RTMap
:rtype: bool
)");
+
node.def(
"evaluate",
- [](const ov::Node& self, ov::TensorVector& output_values, const ov::TensorVector& input_values) -> bool {
- return self.evaluate(output_values, input_values);
+ [](const ov::Node& self,
+ py::list& output_values,
+ const py::list& input_values,
+ const ov::EvaluationContext& evaluationContext) -> bool {
+ py::object pyTensorVector = py::module_::import("openvino").attr("TensorVector");
+ auto casted_output_values = pyTensorVector(output_values).cast<ov::TensorVector>();
+ const auto casted_input_values = pyTensorVector(input_values).cast<ov::TensorVector>();
+
+ return self.evaluate(casted_output_values, casted_input_values, evaluationContext);
},
py::arg("output_values"),
py::arg("input_values"),
+ py::arg("evaluationContext") = PyRTMap(),
R"(
- Evaluate the function on inputs, putting results in outputs
-
+ Evaluate the node on inputs, putting results in outputs
+
:param output_tensors: Tensors for the outputs to compute. One for each result.
- :type output_tensors: list[openvino.Tensor]
+ :type output_tensors: openvino.TensorVector
:param input_tensors: Tensors for the inputs. One for each inputs.
- :type input_tensors: list[openvino.Tensor]
+ :type input_tensors: openvino.TensorVector
+ :param evaluation_context: Storage of additional settings and attributes that can be used
+ when evaluating the function. This additional information can be shared across nodes.
+ :type evaluation_context: openvino.RTMap
:rtype: bool
- )");
+ )");
+
node.def("get_instance_id",
&ov::Node::get_instance_id,
R"(
diff --git a/src/bindings/python/src/pyopenvino/graph/op.cpp b/src/bindings/python/src/pyopenvino/graph/op.cpp
index 37c9dae233e..300c1e1f066 100644
--- a/src/bindings/python/src/pyopenvino/graph/op.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/op.cpp
@@ -17,6 +17,8 @@
namespace py = pybind11;
+PYBIND11_MAKE_OPAQUE(ov::TensorVector);
+
void PyOp::validate_and_infer_types() {
PYBIND11_OVERRIDE(void, ov::op::Op, validate_and_infer_types);
}
@@ -50,7 +52,13 @@ const ov::op::Op::type_info_t& PyOp::get_type_info() const {
}
bool PyOp::evaluate(ov::TensorVector& output_values, const ov::TensorVector& input_values) const {
- PYBIND11_OVERRIDE(bool, ov::op::Op, evaluate, output_values, input_values);
+ py::gil_scoped_acquire gil; // Acquire the GIL while in this scope.
+ py::function overrided_py_method = pybind11::get_override(this, "evaluate");
+
+ if (overrided_py_method) {
+ return static_cast<py::bool_>(overrided_py_method(&output_values, &input_values)); // Call the Python function.
+ }
+ return true;
}
bool PyOp::has_evaluate() const {
diff --git a/src/bindings/python/src/pyopenvino/graph/rt_map.cpp b/src/bindings/python/src/pyopenvino/graph/rt_map.cpp
index 07efb1877cc..2e9451be32c 100644
--- a/src/bindings/python/src/pyopenvino/graph/rt_map.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/rt_map.cpp
@@ -70,6 +70,10 @@ void regclass_graph_PyRTMap(py::module m) {
})
.def("__next__", &PyRTMapIterator::next);
+ py_map.def(py::init([]() {
+ return PyRTMap();
+ }));
+
py_map.def("__setitem__", [](PyRTMap& m, const std::string& k, const std::string v) {
m[k] = v;
});
diff --git a/src/bindings/python/src/pyopenvino/pyopenvino.cpp b/src/bindings/python/src/pyopenvino/pyopenvino.cpp
index 9bb7b530bb2..e8052347c51 100644
--- a/src/bindings/python/src/pyopenvino/pyopenvino.cpp
+++ b/src/bindings/python/src/pyopenvino/pyopenvino.cpp
@@ -80,6 +80,8 @@ inline std::string get_version() {
return version.buildNumber;
}
+PYBIND11_MAKE_OPAQUE(ov::TensorVector);
+
#ifdef Py_GIL_DISABLED
PYBIND11_MODULE(_pyopenvino, m, py::mod_gil_not_used()) {
#else
@@ -101,6 +103,9 @@ PYBIND11_MODULE(_pyopenvino, m) {
"Please ensure that environment variables (e.g. PATH, PYTHONPATH) are set correctly so that "
"OpenVINO Runtime and Python libraries point to same release.");
+ // https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#making-opaque-types
+ py::bind_vector<ov::TensorVector>(m, "TensorVector");
+
m.def("get_version", &get_version);
m.def(
"serialize",
diff --git a/src/bindings/python/tests/test_runtime/test_sync_infer_request.py b/src/bindings/python/tests/test_runtime/test_sync_infer_request.py
index ead3c61aba1..995401a95cd 100644
--- a/src/bindings/python/tests/test_runtime/test_sync_infer_request.py
+++ b/src/bindings/python/tests/test_runtime/test_sync_infer_request.py
@@ -226,6 +226,10 @@ def test_batched_tensors(device):
for idx in range(0, batch):
assert np.array_equal(actual[idx], _tmp)
+ with does_not_raise():
+ req.set_tensors("tensor_input0", tensors)
+ req.set_tensors(req.model_inputs[0], tensors)
+
def test_inputs_outputs_property_and_method(device):
num_inputs = 10
@@ -816,3 +820,20 @@ def test_output_result_to_input():
with does_not_raise():
result_2 = compiled_2(result_1, share_inputs=False)
assert np.array_equal(result_2[0], [[8]])
+
+
+def test_infer_request_tensors_prop(device):
+ compiled_model = generate_add_compiled_model(device, input_shape=[2, 2])
+ request = compiled_model.create_infer_request()
+ inputs = request.input_tensors
+ outputs = request.output_tensors
+
+ assert len(inputs) == 2
+ for input_tensor in inputs:
+ assert input_tensor.get_shape() == Shape([2, 2])
+ assert input_tensor.get_element_type() == Type.f32
+ assert inputs[0].shape == Shape([2, 2])
+ assert isinstance(inputs, list)
+ assert isinstance(outputs, list)
+ assert str(inputs) == "[<Tensor: shape[2,2] type: f32>, <Tensor: shape[2,2] type: f32>]"
+ assert str(outputs) == "[<Tensor: shape[2,2] type: f32>]" # Check representation
diff --git a/src/bindings/python/tests/test_utils/test_utils.py b/src/bindings/python/tests/test_utils/test_utils.py
index 678e5f7e7e1..a4db87c1fbb 100644
--- a/src/bindings/python/tests/test_utils/test_utils.py
+++ b/src/bindings/python/tests/test_utils/test_utils.py
@@ -98,7 +98,8 @@ def test_cmake_file_not_found(monkeypatch):
assert result == ""
-class Maker:
+# For backward compatibility with old maker signatures
+class MakerTensorCopy:
def __init__(self):
self.calls_count = 0
@@ -111,6 +112,19 @@ class Maker:
return self.calls_count
+class Maker:
+ def __init__(self):
+ self.calls_count = 0
+
+ def __call__(self) -> ov.Tensor:
+ self.calls_count += 1
+ tensor_data = np.array([2, 2, 2, 2], dtype=np.float32).reshape(1, 1, 2, 2)
+ return ov.Tensor(tensor_data)
+
+ def called_times(self):
+ return self.calls_count
+
+
def create_model(maker):
input_shape = ov.Shape([1, 2, 1, 2])
param_node = ov.opset13.parameter(input_shape, ov.Type.f32, name="data")
@@ -171,6 +185,16 @@ def test_serialize_postponned_constant(prepare_ir_paths):
assert maker.called_times() == 1
+def test_serialize_postponned_constant_maker_tensor_copy(prepare_ir_paths):
+ maker = MakerTensorCopy()
+ model = create_model(maker)
+ assert maker.called_times() == 0
+
+ model_export_file_name, weights_export_file_name = prepare_ir_paths
+ ov.serialize(model, model_export_file_name, weights_export_file_name)
+ assert maker.called_times() == 1
+
+
def test_infer_postponned_constant():
maker = Maker()
model = create_model(maker)
diff --git a/tools/benchmark_tool/openvino/__init__.py b/tools/benchmark_tool/openvino/__init__.py
index d5594ee4227..572d4532661 100644
--- a/tools/benchmark_tool/openvino/__init__.py
+++ b/tools/benchmark_tool/openvino/__init__.py
@@ -40,6 +40,7 @@ from openvino._pyopenvino import PartialShape
from openvino._pyopenvino import Shape
from openvino._pyopenvino import Layout
from openvino._pyopenvino import Tensor
+from openvino._pyopenvino import TensorVector
from openvino._pyopenvino import Type
from openvino._pyopenvino import OVAny
from openvino._pyopenvino import get_batch
diff --git a/tools/ovc/openvino/__init__.py b/tools/ovc/openvino/__init__.py
index d5594ee4227..572d4532661 100644
--- a/tools/ovc/openvino/__init__.py
+++ b/tools/ovc/openvino/__init__.py
@@ -40,6 +40,7 @@ from openvino._pyopenvino import PartialShape
from openvino._pyopenvino import Shape
from openvino._pyopenvino import Layout
from openvino._pyopenvino import Tensor
+from openvino._pyopenvino import TensorVector
from openvino._pyopenvino import Type
from openvino._pyopenvino import OVAny
from openvino._pyopenvino import get_batch