Skip to content

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

[core] Util to set (auto) names on model's input, output tensors (#28975)

Pawel Raasz015fe97c41b0
diff --git a/src/bindings/python/src/pyopenvino/graph/model.cpp b/src/bindings/python/src/pyopenvino/graph/model.cpp
index 337eca6ee9a..404b720c69a 100644
--- a/src/bindings/python/src/pyopenvino/graph/model.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/model.cpp
@@ -13,6 +13,7 @@
#include "openvino/core/except.hpp"
#include "openvino/core/graph_util.hpp"
#include "openvino/core/model.hpp" // ov::Model
+#include "openvino/core/model_util.hpp"
#include "openvino/core/partial_shape.hpp"
#include "openvino/op/assign.hpp"
#include "openvino/op/parameter.hpp" // ov::op::v0::Parameter
@@ -30,15 +31,14 @@ using PyRTMap = ov::RTMap;
PYBIND11_MAKE_OPAQUE(PyRTMap);
-static void set_tensor_names(const ov::ParameterVector& parameters) {
- for (const auto& param : parameters) {
- ov::Output<ov::Node> p = param;
- if (p.get_node()->output(0).get_names().empty()) {
- std::unordered_set<std::string> p_names({p.get_node()->get_friendly_name()});
- p.get_node()->output(0).set_names(p_names);
- }
- }
+namespace {
+template <class... Args>
+std::shared_ptr<ov::Model> make_model_with_tensor_names(Args&&... args) {
+ auto model = std::make_shared<ov::Model>(std::forward<Args>(args)...);
+ ov::util::set_tensors_names(ov::AUTO, *model);
+ return model;
}
+} // namespace
static std::shared_ptr<ov::Node> get_node_ptr(std::shared_ptr<ov::Node> node) {
return node;
@@ -163,9 +163,8 @@ void regclass_graph_Model(py::module m) {
const std::vector<std::shared_ptr<ov::Node>>& nodes,
const ov::ParameterVector& params,
const std::string& name) {
- set_tensor_names(params);
const auto sinks = cast_to_sink_vector(nodes);
- auto model = std::make_shared<ov::Model>(res, sinks, params, name);
+ auto model = make_model_with_tensor_names(res, sinks, params, name);
set_correct_variables_for_assign_ops(model, sinks);
return model;
}),
@@ -189,8 +188,7 @@ void regclass_graph_Model(py::module m) {
model.def(py::init([](const std::vector<std::shared_ptr<ov::Node>>& results,
const ov::ParameterVector& parameters,
const std::string& name) {
- set_tensor_names(parameters);
- return std::make_shared<ov::Model>(results, parameters, name);
+ return make_model_with_tensor_names(results, parameters, name);
}),
py::arg("results"),
py::arg("parameters"),
@@ -209,8 +207,7 @@ void regclass_graph_Model(py::module m) {
model.def(py::init([](const std::shared_ptr<ov::Node>& result,
const ov::ParameterVector& parameters,
const std::string& name) {
- set_tensor_names(parameters);
- return std::make_shared<ov::Model>(result, parameters, name);
+ return make_model_with_tensor_names(result, parameters, name);
}),
py::arg("result"),
py::arg("parameters"),
@@ -228,8 +225,7 @@ void regclass_graph_Model(py::module m) {
model.def(
py::init([](const ov::OutputVector& results, const ov::ParameterVector& parameters, const std::string& name) {
- set_tensor_names(parameters);
- return std::make_shared<ov::Model>(results, parameters, name);
+ return make_model_with_tensor_names(results, parameters, name);
}),
py::arg("results"),
py::arg("parameters"),
@@ -249,9 +245,8 @@ void regclass_graph_Model(py::module m) {
const std::vector<std::shared_ptr<ov::Node>>& nodes,
const ov::ParameterVector& parameters,
const std::string& name) {
- set_tensor_names(parameters);
const auto sinks = cast_to_sink_vector(nodes);
- auto model = std::make_shared<ov::Model>(results, sinks, parameters, name);
+ auto model = make_model_with_tensor_names(results, sinks, parameters, name);
set_correct_variables_for_assign_ops(model, sinks);
return model;
}),
@@ -276,9 +271,8 @@ void regclass_graph_Model(py::module m) {
const ov::OutputVector& nodes,
const ov::ParameterVector& parameters,
const std::string& name) {
- set_tensor_names(parameters);
const auto sinks = cast_to_sink_vector(nodes);
- auto model = std::make_shared<ov::Model>(results, sinks, parameters, name);
+ auto model = make_model_with_tensor_names(results, sinks, parameters, name);
set_correct_variables_for_assign_ops(model, sinks);
return model;
}),
@@ -304,9 +298,7 @@ void regclass_graph_Model(py::module m) {
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
- set_tensor_names(parameters);
- const auto sinks = cast_to_sink_vector(nodes);
- return std::make_shared<ov::Model>(results, sinks, parameters, variables, name);
+ return make_model_with_tensor_names(results, cast_to_sink_vector(nodes), parameters, variables, name);
}),
py::arg("results"),
py::arg("sinks"),
@@ -332,9 +324,8 @@ void regclass_graph_Model(py::module m) {
const ov::OutputVector& nodes,
const ov::ParameterVector& parameters,
const std::string& name) {
- set_tensor_names(parameters);
const auto sinks = cast_to_sink_vector(nodes);
- auto model = std::make_shared<ov::Model>(results, sinks, parameters, name);
+ auto model = make_model_with_tensor_names(results, sinks, parameters, name);
set_correct_variables_for_assign_ops(model, sinks);
return model;
}),
@@ -360,9 +351,7 @@ void regclass_graph_Model(py::module m) {
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
- set_tensor_names(parameters);
- const auto sinks = cast_to_sink_vector(nodes);
- return std::make_shared<ov::Model>(results, sinks, parameters, variables, name);
+ return make_model_with_tensor_names(results, cast_to_sink_vector(nodes), parameters, variables, name);
}),
py::arg("results"),
py::arg("sinks"),
@@ -389,9 +378,7 @@ void regclass_graph_Model(py::module m) {
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
- set_tensor_names(parameters);
- const auto sinks = cast_to_sink_vector(nodes);
- return std::make_shared<ov::Model>(results, sinks, parameters, variables, name);
+ return make_model_with_tensor_names(results, cast_to_sink_vector(nodes), parameters, variables, name);
}),
py::arg("results"),
py::arg("sinks"),
@@ -418,9 +405,7 @@ void regclass_graph_Model(py::module m) {
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
- set_tensor_names(parameters);
- const auto sinks = cast_to_sink_vector(nodes);
- return std::make_shared<ov::Model>(results, sinks, parameters, variables, name);
+ return make_model_with_tensor_names(results, cast_to_sink_vector(nodes), parameters, variables, name);
}),
py::arg("results"),
py::arg("sinks"),
@@ -444,8 +429,7 @@ void regclass_graph_Model(py::module m) {
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
- set_tensor_names(parameters);
- return std::make_shared<ov::Model>(results, parameters, variables, name);
+ return make_model_with_tensor_names(results, parameters, variables, name);
}),
py::arg("results"),
py::arg("parameters"),
@@ -468,8 +452,7 @@ void regclass_graph_Model(py::module m) {
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
- set_tensor_names(parameters);
- return std::make_shared<ov::Model>(results, parameters, variables, name);
+ return make_model_with_tensor_names(results, parameters, variables, name);
}),
py::arg("results"),
py::arg("parameters"),
diff --git a/src/bindings/python/tests/test_graph/test_core.py b/src/bindings/python/tests/test_graph/test_core.py
index a75d6aa4059..94f1c0e09d2 100644
--- a/src/bindings/python/tests/test_graph/test_core.py
+++ b/src/bindings/python/tests/test_graph/test_core.py
@@ -9,7 +9,7 @@ import pytest
from openvino import Dimension, Model, PartialShape, Shape
-import openvino.opset8 as ov
+import openvino.opset13 as ov
def test_dimension():
@@ -408,6 +408,8 @@ def test_repr_dynamic_shape():
parameter_a = ov.parameter(shape, dtype=np.float32, name="A")
parameter_b = ov.parameter(shape, dtype=np.float32, name="B")
param_sum = parameter_a + parameter_b
+ # set tensor name to have deterministic output name of model (default use unique node name)
+ param_sum.output(0).set_names({"sum"})
model = Model(param_sum, [parameter_a, parameter_b], "simple_dyn_shapes_graph")
assert (
@@ -415,7 +417,7 @@ def test_repr_dynamic_shape():
== "<Model: 'simple_dyn_shapes_graph'\ninputs["
+ "\n<ConstOutput: names[A] shape[?,2] type: f32>,"
+ "\n<ConstOutput: names[B] shape[?,2] type: f32>\n]"
- + "\noutputs[\n<ConstOutput: names[] shape[?,2] type: f32>\n]>"
+ + "\noutputs[\n<ConstOutput: names[sum] shape[?,2] type: f32>\n]>"
)
ops = model.get_ordered_ops()
diff --git a/src/bindings/python/tests/test_runtime/test_ovdict.py b/src/bindings/python/tests/test_runtime/test_ovdict.py
index 772c73b3407..d2209b75871 100644
--- a/src/bindings/python/tests/test_runtime/test_ovdict.py
+++ b/src/bindings/python/tests/test_runtime/test_ovdict.py
@@ -154,9 +154,9 @@ def test_ovdict_single_output_noname(device, is_direct):
_ = result["some_name"]
assert "some_name" in str(e0.value)
- # Check if returned names are tuple with one empty set
+ # Check if returned names are tuple with one default name set
assert len(result.names()) == 1
- assert result.names()[0] == set()
+ assert result.names()[0] != set()
@pytest.mark.parametrize("is_direct", [True, False])
diff --git a/src/core/dev_api/openvino/core/descriptor_tensor.hpp b/src/core/dev_api/openvino/core/descriptor_tensor.hpp
index bc07abd98c4..1960d682d3c 100644
--- a/src/core/dev_api/openvino/core/descriptor_tensor.hpp
+++ b/src/core/dev_api/openvino/core/descriptor_tensor.hpp
@@ -12,6 +12,9 @@
namespace ov {
namespace descriptor {
+/// @brief Defines tensor name port separator.
+inline constexpr auto port_separator = ':';
+
class Tensor;
class Input;
class Output;
diff --git a/src/core/dev_api/openvino/core/model_util.hpp b/src/core/dev_api/openvino/core/model_util.hpp
new file mode 100644
index 00000000000..fd03af3ac63
--- /dev/null
+++ b/src/core/dev_api/openvino/core/model_util.hpp
@@ -0,0 +1,98 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <unordered_map>
+
+#include "openvino/core/core_visibility.hpp"
+#include "openvino/core/descriptor/tensor.hpp"
+#include "openvino/core/model.hpp"
+
+namespace ov {
+
+/** @brief Generic atg suggests automated functionality.
+ *
+ * Can be use for template specialization or function overloading
+ */
+struct AutoTag {};
+inline constexpr AutoTag AUTO{};
+
+/** @brief Alias to map of port number and tensor names */
+using TensorNamesMap = std::unordered_map<size_t, TensorNames>;
+} // namespace ov
+
+namespace ov::util {
+
+/** @brief Set input tensors names for the model
+ *
+ * Sets only tensors defined in the input map.
+ * The tensors defined in map but not existing in the model will be ignored.
+ *
+ * @param model Model to set its input tensors names.
+ * @param inputs_names Map of input tensor names.
+ */
+OPENVINO_API void set_input_tensors_names(Model& model, const TensorNamesMap& inputs_names);
+
+/** @brief Set input tensors names for the model
+ *
+ * Sets tensors defined in the input map.
+ * Tensors not defined in the map are set to default names if the tensor hasn't got names.
+ * The tensors defined in the map but not existing in the model will be ignored.
+ *
+ * @param model Model to set its input tensors names.
+ * @param inputs_names Map of input tensor names. Default empty.
+ */
+OPENVINO_API void set_input_tensors_names(const AutoTag&, Model& model, const TensorNamesMap& inputs_names = {});
+
+/** @brief Set output tensors names for the model
+ *
+ * Sets only tensors defined in the output map.
+ * The tensors defined in map but not existing in the model will be ignored.
+ *
+ * @param model Model to set its output tensors names.
+ * @param outputs_names Map of output tensor names.AnyMap
+ */
+OPENVINO_API void set_output_tensor_names(Model& model, const TensorNamesMap& outputs_names);
+
+/** @brief Set output tensors names for the model.
+ *
+ * Sets tensors defined in the output map.
+ * Tensors not defined in the map are set to default names if the tensor hasn't got names.
+ * The tensors defined in the map but not existing in the model will be ignored.
+ *
+ * @param model Model to set its output tensors names.
+ * @param outputs_names Map of output tensor names. Default empty.
+ */
+OPENVINO_API void set_output_tensor_names(const AutoTag&, Model& model, const TensorNamesMap& outputs_names = {});
+
+/** @brief Set input and output tensors names for the model
+ *
+ * Sets only tensors defined in the input and output maps.
+ * The tensors defined in maps but not existing in the model will be ignored.
+ *
+ * @param model Model to set its input and output tensors names.
+ * @param inputs_names Map of input tensor names.
+ * @param outputs_names Map of output tensor names.
+ */
+OPENVINO_API void set_tensors_names(Model& model,
+ const TensorNamesMap& inputs_names,
+ const TensorNamesMap& outputs_names);
+
+/** @brief Set input and output tensors names for the model.
+ *
+ * Sets tensors defined in the input and output maps.
+ * Tensors not defined in the maps are set to default names if the tensor hasn't got names.
+ * The tensors defined in the maps but not existing in the model will be ignored.
+ *
+ * @param model Model to set its input and output tensors names.
+ * @param inputs_names Map of input tensor names. Default empty.
+ * @param outputs_names Map of output tensor names. Default empty.
+ */
+OPENVINO_API void set_tensors_names(const AutoTag&,
+ Model& model,
+ const TensorNamesMap& inputs_names = {},
+ const TensorNamesMap& outputs_names = {});
+
+} // namespace ov::util
diff --git a/src/core/dev_api/openvino/op/util/node_util.hpp b/src/core/dev_api/openvino/op/util/node_util.hpp
index a99523ab4f8..92dd4d0dc8e 100644
--- a/src/core/dev_api/openvino/op/util/node_util.hpp
+++ b/src/core/dev_api/openvino/op/util/node_util.hpp
@@ -4,11 +4,23 @@
#pragma once
+#include <string>
+
#include "openvino/core/node.hpp"
namespace ov {
-namespace op {
namespace util {
+/**
+ * @brief Creates default tensor name for given Node's output.
+ * The name format is "node_name:output_port".
+ *
+ * @param output - Node's output to create name for tensor.
+ * @return Default tensor name.
+ */
+OPENVINO_API std::string make_default_tensor_name(const Output<const Node>& output);
+} // namespace util
+
+namespace op::util {
/**
* @brief Set name for both node and output tensor. Any other names will be overriden by a given single name
* @param node - node to rename
@@ -16,6 +28,5 @@ namespace util {
* @param output_port - output port to rename
*/
void OPENVINO_API set_name(ov::Node& node, const std::string& name, size_t output_port = 0);
-} // namespace util
-} // namespace op
+} // namespace op::util
} // namespace ov
diff --git a/src/core/include/openvino/core/descriptor/tensor.hpp b/src/core/include/openvino/core/descriptor/tensor.hpp
index 4eddee39a8b..b734fa33cc1 100644
--- a/src/core/include/openvino/core/descriptor/tensor.hpp
+++ b/src/core/include/openvino/core/descriptor/tensor.hpp
@@ -26,6 +26,8 @@ using TensorSymbol = std::vector<std::shared_ptr<Symbol>>;
/// \brief Alias for vector of symbol tensors.
using TensorSymbolVector = std::vector<TensorSymbol>;
+/// \brief Alias for set of tensor names.
+using TensorNames = std::unordered_set<std::string>;
namespace descriptor {
class ITensorDescriptor;
diff --git a/src/core/src/op/util/model_util.cpp b/src/core/src/op/util/model_util.cpp
new file mode 100644
index 00000000000..ae9e88d3747
--- /dev/null
+++ b/src/core/src/op/util/model_util.cpp
@@ -0,0 +1,61 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/core/model_util.hpp"
+
+#include "openvino/core/model.hpp"
+#include "openvino/op/util/node_util.hpp"
+
+namespace ov::util {
+namespace {
+
+void set_default_tensor_names(OutputVector&& outputs) {
+ for (auto& output : outputs) {
+ if (output.get_tensor().get_names().empty()) {
+ output.get_tensor().set_names({make_default_tensor_name(output)});
+ }
+ }
+}
+
+void set_tensor_names(OutputVector&& outputs, const TensorNamesMap& tensor_names) {
+ for (const auto& [port, names] : tensor_names) {
+ if (port < outputs.size()) {
+ outputs[port].get_tensor().set_names(names);
+ }
+ }
+}
+} // namespace
+
+void set_input_tensors_names(Model& model, const TensorNamesMap& inputs_names) {
+ set_tensor_names(model.inputs(), inputs_names);
+}
+
+void set_input_tensors_names(const AutoTag&, Model& model, const TensorNamesMap& inputs_names) {
+ set_input_tensors_names(model, inputs_names);
+ set_default_tensor_names(model.inputs());
+}
+
+void set_output_tensor_names(Model& model, const TensorNamesMap& outputs_names) {
+ set_tensor_names(model.outputs(), outputs_names);
+}
+
+void set_output_tensor_names(const AutoTag&, Model& model, const TensorNamesMap& outputs_names) {
+ set_output_tensor_names(model, outputs_names);
+ set_default_tensor_names(model.outputs());
+}
+
+void set_tensors_names(Model& model, const TensorNamesMap& inputs_names, const TensorNamesMap& outputs_names) {
+ set_input_tensors_names(model, inputs_names);
+ set_output_tensor_names(model, outputs_names);
+}
+
+void set_tensors_names(const AutoTag&,
+ Model& model,
+ const TensorNamesMap& inputs_names,
+ const TensorNamesMap& outputs_names) {
+ set_input_tensors_names(AUTO, model, inputs_names);
+ set_output_tensor_names(AUTO, model, outputs_names);
+}
+
+} // namespace ov::util
diff --git a/src/core/src/op/util/node_util.cpp b/src/core/src/op/util/node_util.cpp
index e3dc1e6bb58..e98735f4560 100644
--- a/src/core/src/op/util/node_util.cpp
+++ b/src/core/src/op/util/node_util.cpp
@@ -4,13 +4,23 @@
#include "openvino/op/util/node_util.hpp"
-namespace ov {
-namespace op {
-namespace util {
+#include "openvino/core/descriptor_tensor.hpp"
+
+namespace ov::op::util {
+
void set_name(ov::Node& node, const std::string& name, size_t output_port) {
node.set_friendly_name(name);
node.get_output_tensor(output_port).set_names({name});
}
-} // namespace util
-} // namespace op
-} // namespace ov
+} // namespace ov::op::util
+
+namespace ov::util {
+
+std::string make_default_tensor_name(const Output<const Node>& output) {
+ auto default_name = output.get_node()->get_friendly_name();
+ if (output.get_node()->get_output_size() > 1) {
+ default_name += descriptor::port_separator + std::to_string(output.get_index());
+ }
+ return default_name;
+}
+} // namespace ov::util
diff --git a/src/core/tests/model_util_tests.cpp b/src/core/tests/model_util_tests.cpp
new file mode 100644
index 00000000000..a1c9da42d42
--- /dev/null
+++ b/src/core/tests/model_util_tests.cpp
@@ -0,0 +1,219 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gmock/gmock.h>
+
+#include <optional>
+
+#include "openvino/core/model.hpp"
+#include "openvino/core/model_util.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/multiply.hpp"
+
+namespace ov::test {
+
+using op::v0::Parameter, op::v1::Add, op::v1::Multiply, op::v0::Result;
+
+class ModelUtilTest : public testing::Test {
+protected:
+ static auto make_model_without_tensor_names() {
+ auto input_1 = std::make_shared<Parameter>(element::f32, Shape{1, 3});
+ auto input_2 = std::make_shared<Parameter>(element::f32, Shape{3, 3});
+ auto input_3 = std::make_shared<Parameter>(element::f32, Shape{3, 1});
+
+ auto add = std::make_shared<Add>(input_1, input_2);
+ auto mul = std::make_shared<Multiply>(add, input_3);
+ auto add_final = std::make_shared<Add>(mul, add);
+
+ auto add_result = std::make_shared<Result>(add);
+ auto mul_result = std::make_shared<Result>(mul);
+ auto final_result = std::make_shared<Result>(add_final);
+
+ auto model = std::make_shared<Model>(ResultVector{add_result, mul_result, final_result},
+ ParameterVector{input_1, input_2, input_3},
+ "ModelWithoutTensorNames");
+ return model;
+ }
+
+ static auto make_model_with_named_nodes() {
+ auto model = make_model_without_tensor_names();
+
+ const auto& inputs = model->inputs();
+ for (size_t i = 0; i < inputs.size(); ++i) {
+ inputs[i].get_node()->set_friendly_name("input_" + std::to_string(i + 1));
+ }
+
+ auto results = model->get_results();
+ results[0]->set_friendly_name("add_result");
+ results[1]->set_friendly_name("mul_result");
+ results[2]->set_friendly_name("final_result");
+ model->set_friendly_name("ModelWithNamedNodes");
+ return model;
+ }
+
+ static auto compare_tensor_names(const OutputVector& outputs, const TensorNamesMap& expected) {
+ std::optional<std::string> mismatch_err;
+
+ for (const auto& [port, expected_names] : expected) {
+ if (const auto& names = outputs[port].get_names(); expected_names != names) {
+ using testing::PrintToString;
+ mismatch_err.emplace("Tensor names mismatch on port " + PrintToString(port) + "\n Expected: " +
+ PrintToString(expected_names) + "\n Actual: " + PrintToString(names));
+ }
+ }
+ return mismatch_err;
+ }
+};
+
+TEST_F(ModelUtilTest, manual_set_all_input_tensors_names) {
+ const auto inputs_names = TensorNamesMap{{0, {"input_1"}}, {1, {"input_2"}}, {2, {"input_3"}}};
+
+ auto model = make_model_without_tensor_names();
+ util::set_input_tensors_names(*model, inputs_names);
+
+ ASSERT_EQ(model->inputs().size(), inputs_names.size());
+ for (const auto& [port, names] : inputs_names) {
+ EXPECT_EQ(model->input(port).get_names(), names) << "Names not match for input port " << port;
+ }
+}
+TEST_F(ModelUtilTest, manual_set_some_input_tensors_names) {
+ const auto inputs_names = TensorNamesMap{{2, {"input_2", "mul_input"}}, {0, {"add_input"}}};
+ auto expected_names = inputs_names;
+ expected_names.emplace(1, TensorNames{});
+
+ auto model = make_model_without_tensor_names();
+ util::set_input_tensors_names(*model, inputs_names);
+
+ ASSERT_EQ(model->inputs().size(), expected_names.size());
+ const auto mismatch_error = compare_tensor_names(model->inputs(), expected_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+}
+
+TEST_F(ModelUtilTest, auto_set_all_input_tensors_names) {
+ const auto expected_names = TensorNamesMap{{0, {"input_1"}}, {1, {"input_2"}}, {2, {"input_3"}}};
+
+ auto model = make_model_with_named_nodes();
+ util::set_input_tensors_names(AUTO, *model);
+
+ ASSERT_EQ(model->inputs().size(), expected_names.size());
+ const auto mismatch_error = compare_tensor_names(model->inputs(), expected_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+}
+
+TEST_F(ModelUtilTest, auto_set_missing_input_tensors_names) {
+ const auto inputs_names = TensorNamesMap{{2, {"input_2", "mul_input"}}, {0, {"add_input"}}};
+ auto expected_names = inputs_names;
+ expected_names.emplace(1, TensorNames{"input_2"});
+
+ auto model = make_model_with_named_nodes();
+ util::set_input_tensors_names(AUTO, *model, inputs_names);
+
+ ASSERT_EQ(model->inputs().size(), expected_names.size());
+ const auto mismatch_error = compare_tensor_names(model->inputs(), expected_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+}
+
+TEST_F(ModelUtilTest, auto_set_all_io_tensors_names) {
+ const auto exp_inputs_names = TensorNamesMap{{2, {"input_3"}}, {0, {"input_1"}}, {1, {"input_2"}}};
+ const auto exp_outputs_names = TensorNamesMap{{0, {"add_result"}}, {1, {"mul_result"}}, {2, {"final_result"}}};
+
+ auto model = make_model_with_named_nodes();
+ util::set_tensors_names(AUTO, *model);
+
+ ASSERT_EQ(model->inputs().size(), exp_inputs_names.size());
+ auto mismatch_error = compare_tensor_names(model->inputs(), exp_inputs_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+
+ ASSERT_EQ(model->outputs().size(), exp_outputs_names.size());
+ mismatch_error = compare_tensor_names(model->outputs(), exp_outputs_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+}
+
+TEST_F(ModelUtilTest, manual_set_all_io_tensors_names) {
+ const auto inputs_names = TensorNamesMap{{0, {"input_1"}}, {1, {"input_2"}}, {2, {"input_3"}}};
+ const auto outputs_names = TensorNamesMap{{0, {"add_result"}}, {1, {"mul_result"}}, {2, {"final_result"}}};
+
+ auto model = make_model_without_tensor_names();
+ util::set_tensors_names(*model, inputs_names, outputs_names);
+
+ ASSERT_EQ(model->inputs().size(), inputs_names.size());
+ auto mismatch_error = compare_tensor_names(model->inputs(), inputs_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+
+ ASSERT_EQ(model->outputs().size(), outputs_names.size());
+ mismatch_error = compare_tensor_names(model->outputs(), outputs_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+}
+
+TEST_F(ModelUtilTest, manual_set_some_io_tensors_names) {
+ const auto inputs_names = TensorNamesMap{{0, {"input_1"}}, {2, {"input_3"}}};
+ auto expected_input_names = inputs_names;
+ expected_input_names.emplace(1, TensorNames{});
+
+ const auto outputs_names = TensorNamesMap{{1, {"mul_result"}}};
+ auto expected_output_names = outputs_names;
+ expected_output_names.emplace(0, TensorNames{});
+ expected_output_names.emplace(2, TensorNames{});
+
+ auto model = make_model_without_tensor_names();
+ util::set_tensors_names(*model, inputs_names, outputs_names);
+
+ ASSERT_EQ(model->inputs().size(), expected_input_names.size());
+ auto mismatch_error = compare_tensor_names(model->inputs(), expected_input_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+
+ ASSERT_EQ(model->outputs().size(), expected_output_names.size());
+ mismatch_error = compare_tensor_names(model->outputs(), expected_output_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+}
+
+TEST_F(ModelUtilTest, manual_set_all_output_tensors_names) {
+ const auto outputs_names = TensorNamesMap{{0, {"add_result"}}, {1, {"mul_result"}}, {2, {"final_result"}}};
+
+ auto model = make_model_with_named_nodes();
+ util::set_output_tensor_names(*model, outputs_names);
+
+ ASSERT_EQ(model->outputs().size(), outputs_names.size());
+ const auto mismatch_error = compare_tensor_names(model->outputs(), outputs_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+}
+
+TEST_F(ModelUtilTest, manual_set_some_output_tensors_names) {
+ const auto outputs_names = TensorNamesMap{{1, {"mul_result"}}, {2, {"final_result"}}};
+ auto expected_names = outputs_names;
+ expected_names.emplace(0, TensorNames{});
+
+ auto model = make_model_with_named_nodes();
+ util::set_output_tensor_names(*model, outputs_names);
+
+ ASSERT_EQ(model->outputs().size(), expected_names.size());
+ const auto mismatch_error = compare_tensor_names(model->outputs(), expected_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+}
+
+TEST_F(ModelUtilTest, auto_set_all_output_tensors_names) {
+ const auto expected_names = TensorNamesMap{{0, {"add_result"}}, {1, {"mul_result"}}, {2, {"final_result"}}};
+
+ auto model = make_model_with_named_nodes();
+ util::set_output_tensor_names(AUTO, *model);
+
+ ASSERT_EQ(model->outputs().size(), expected_names.size());
+ const auto mismatch_error = compare_tensor_names(model->outputs(), expected_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+}
+
+TEST_F(ModelUtilTest, auto_set_missing_output_tensors_names) {
+ const auto outputs_names = TensorNamesMap{{1, {"mul_result"}}, {2, {"final_result"}}};
+ auto expected_names = outputs_names;
+ expected_names.emplace(0, TensorNames{"add_result"});
+
+ auto model = make_model_with_named_nodes();
+ util::set_output_tensor_names(AUTO, *model, outputs_names);
+
+ ASSERT_EQ(model->outputs().size(), expected_names.size());
+ const auto mismatch_error = compare_tensor_names(model->outputs(), expected_names);
+ EXPECT_FALSE(mismatch_error) << *mismatch_error;
+}
+
+} // namespace ov::test