Skip to content

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

[PY OV] Implement setter for rt_info for Node (#31133)

Alicja Miloszewska5b611748ac7c
diff --git a/src/bindings/python/src/pyopenvino/graph/node.cpp b/src/bindings/python/src/pyopenvino/graph/node.cpp
index 56af307c4a3..481028bd280 100644
--- a/src/bindings/python/src/pyopenvino/graph/node.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/node.cpp
@@ -501,11 +501,26 @@ void regclass_graph_Node(py::module m) {
(PyRTMap & (ov::Node::*)()) & ov::Node::get_rt_info,
py::return_value_policy::reference_internal,
R"(
- Returns PyRTMap which is a dictionary of user defined runtime info.
+ Returns RTMap which is a dictionary of user defined runtime info.
:return: A dictionary of user defined data.
:rtype: openvino.RTMap
)");
+ node.def(
+ "set_rt_info",
+ [](ov::Node& self, const py::object& value, const py::str& key) -> void {
+ self.get_rt_info()[key.cast<std::string>()] = Common::utils::py_object_to_any(value);
+ },
+ py::arg("value"),
+ py::arg("key"),
+ R"(
+ Add a value to the runtime info.
+
+ :param value: Value for the runtime info.
+ :type value: Any
+ :param key: String that defines a key in the runtime info dictionary.
+ :type key: str
+ )");
node.def("set_argument", &ov::Node::set_argument);
node.def("set_arguments", [](const std::shared_ptr<ov::Node>& self, const ov::NodeVector& args) {
diff --git a/src/bindings/python/src/pyopenvino/graph/node_input.cpp b/src/bindings/python/src/pyopenvino/graph/node_input.cpp
index 21ca2cc7181..2459258d316 100644
--- a/src/bindings/python/src/pyopenvino/graph/node_input.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/node_input.cpp
@@ -9,6 +9,7 @@
#include "dict_attribute_visitor.hpp"
#include "pyopenvino/core/common.hpp"
#include "pyopenvino/graph/node_input.hpp"
+#include "pyopenvino/utils/utils.hpp"
namespace py = pybind11;
@@ -86,6 +87,21 @@ void regclass_graph_Input(py::module m) {
:return: A dictionary of user defined data.
:rtype: openvino.RTMap
)");
+ input.def(
+ "set_rt_info",
+ [](ov::Input<ov::Node>& self, const py::object& value, const py::str& key) -> void {
+ self.get_rt_info()[key.cast<std::string>()] = Common::utils::py_object_to_any(value);
+ },
+ py::arg("value"),
+ py::arg("key"),
+ R"(
+ Add a value to the runtime info.
+
+ :param value: Value for the runtime info.
+ :type value: Any
+ :param key: String that defines a key in the runtime info dictionary.
+ :type key: str
+ )");
input.def("replace_source_output",
&ov::Input<ov::Node>::replace_source_output,
py::arg("new_source_output"),
diff --git a/src/bindings/python/src/pyopenvino/graph/node_output.cpp b/src/bindings/python/src/pyopenvino/graph/node_output.cpp
index b2bd472b484..8b1f7567e0b 100644
--- a/src/bindings/python/src/pyopenvino/graph/node_output.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/node_output.cpp
@@ -8,6 +8,7 @@
#include "dict_attribute_visitor.hpp"
#include "pyopenvino/graph/node_output.hpp"
+#include "pyopenvino/utils/utils.hpp"
namespace py = pybind11;
@@ -60,4 +61,19 @@ void def_type_dependent_functions<ov::Node>(
:param replacement: The node that is a replacement.
:type replacement: openvino.Output
)");
+ output.def(
+ "set_rt_info",
+ [](ov::Output<ov::Node>& self, const py::object& value, const py::str& key) -> void {
+ self.get_rt_info()[key.cast<std::string>()] = Common::utils::py_object_to_any(value);
+ },
+ py::arg("value"),
+ py::arg("key"),
+ R"(
+ Add a value to the runtime info.
+
+ :param value: Value for the runtime info.
+ :type value: Any
+ :param key: String that defines a key in the runtime info dictionary.
+ :type key: str
+ )");
}
diff --git a/src/bindings/python/tests/test_graph/test_basic.py b/src/bindings/python/tests/test_graph/test_basic.py
index e9abe820a70..eaeddb29138 100644
--- a/src/bindings/python/tests/test_graph/test_basic.py
+++ b/src/bindings/python/tests/test_graph/test_basic.py
@@ -517,6 +517,11 @@ def test_runtime_info():
runtime_info_after = relu_node.get_rt_info()
assert runtime_info_after["affinity"] == "test_affinity"
+ relu_node.set_rt_info("test_value", "test_key")
+ assert relu_node.get_rt_info()["test_key"] == "test_value"
+ relu_node.rt_info["new_key"] = "new_value"
+ assert relu_node.rt_info["new_key"] == "new_value"
+
def test_multiple_outputs():
input_shape = [4, 4]
diff --git a/src/bindings/python/tests/test_runtime/test_input_node.py b/src/bindings/python/tests/test_runtime/test_input_node.py
index ffc3aebc3b7..3e37b87abd6 100644
--- a/src/bindings/python/tests/test_runtime/test_input_node.py
+++ b/src/bindings/python/tests/test_runtime/test_input_node.py
@@ -135,6 +135,11 @@ def test_input_update_rt_info(device):
assert key == "test12345"
assert isinstance(value, OVAny)
+ input_node.set_rt_info("value", "key")
+ assert input_node.get_rt_info()["key"] == "value"
+ input_node.rt_info["test_key"] = "test_value"
+ assert input_node.rt_info["test_key"] == "test_value"
+
def test_tensor_bounds_in_model(device):
core = Core()
diff --git a/src/bindings/python/tests/test_runtime/test_output_const_node.py b/src/bindings/python/tests/test_runtime/test_output_const_node.py
index 5c33647c1d7..9de60769ae3 100644
--- a/src/bindings/python/tests/test_runtime/test_output_const_node.py
+++ b/src/bindings/python/tests/test_runtime/test_output_const_node.py
@@ -108,6 +108,10 @@ def test_const_get_rf_info(device):
rt_info = output_node.get_rt_info()
assert isinstance(rt_info, RTMap)
+ # ConstOutput ~ Output<const Node>. RTMap is not modifiable.
+ with pytest.raises(AttributeError, match="openvino._pyopenvino.ConstOutput' object has no attribute 'set_rt_info"):
+ output_node.set_rt_info("test_value", "test_key")
+
def test_const_output_runtime_info(device):
core = Core()
@@ -128,6 +132,11 @@ def test_update_rt_info(device):
assert key == "test12345"
assert isinstance(value, OVAny)
+ output_node.rt_info["test_key"] = "test_value"
+ assert output_node.rt_info["test_key"] == "test_value"
+ output_node.set_rt_info("test_value2", "test_key2")
+ assert output_node.get_rt_info()["test_key2"] == "test_value2"
+
def test_operations():
data = ops.parameter([2])