OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GHA] [Sanitizers] Transformations func tests show leaks (#34758)
Pawel Raasz26bcbbb8d219
diff --git a/src/core/include/openvino/core/graph_util.hpp b/src/core/include/openvino/core/graph_util.hppindex c979b3c5bb2..f465c77fc57 100644--- a/src/core/include/openvino/core/graph_util.hpp+++ b/src/core/include/openvino/core/graph_util.hpp@@ -221,7 +221,21 @@ void replace_nodes(const std::shared_ptr<Model>& f,parameter_replacement_map,const std::unordered_map<std::shared_ptr<Node>, std::shared_ptr<Node>>& body_replacement_map);-/// Topological sort of nodes needed to compute root_nodes+/// \brief Performs a topological sort on a graph of nodes.+///+/// \tparam T A container type (e.g., std::vector, std::deque) of shared_ptr<Node> or Node*.+/// \param root_nodes The starting nodes from which to begin the topological sort traversal.+///+/// \return A vector of nodes sorted in topological order, where dependencies come before dependents.+///+/// \details+/// This function performs a depth-first traversal of the computation graph starting from the given root nodes.+/// It produces an ordering where each node appears after all of its input dependencies.+///+/// \note If a circular dependency is detected (a node is visited more than twice during traversal),+/// the function throws an OPENVINO_THROW exception with details about the loop source.+///+/// \warning The caller is responsible for clean-up the model connections when circular dependencies are present.template <typename T>std::vector<std::shared_ptr<Node>> topological_sort(T root_nodes) {std::stack<Node*, std::vector<Node*>> nodes_to_do;@@ -236,13 +250,14 @@ std::vector<std::shared_ptr<Node>> topological_sort(T root_nodes) {Node* node = nodes_to_do.top();if (nodes_done.count(node) == 0) {bool can_add = true;- if (++nodes_visited[node] > 2)+ if (++nodes_visited[node] > 2) {// Node may be at the top of `nodes_to_do` not more than twice before it's added to `nodes_done` -// when visited and placed in `nodes_to_do` and after the subtree traversal is finished.// Otherwise it's a loop.OPENVINO_THROW("Loop detected during topological sort starting from '",node->get_friendly_name(),"' node.");+ }size_t arg_count = node->get_input_size();for (size_t i = 0; i < arg_count; ++i) {diff --git a/src/core/src/descriptor/input.cpp b/src/core/src/descriptor/input.cppindex 2e7eac6f708..bbcaf72a3a8 100644--- a/src/core/src/descriptor/input.cpp+++ b/src/core/src/descriptor/input.cpp@@ -45,7 +45,7 @@ void ov::descriptor::Input::replace_output(Output& new_output) {}new_output.add_input(this);m_output = &new_output;- m_src_node = std::shared_ptr<ov::Node>(new_output.get_node());+ m_src_node = new_output.get_node();// Output replacement may change the topological order of nodes,// so we have to reset cache by setting a flag into shared node info.diff --git a/src/core/tests/conditional_compilation/ov_cc_collect.cpp b/src/core/tests/conditional_compilation/ov_cc_collect.cppindex 090fbfc8853..f8ece93347d 100644--- a/src/core/tests/conditional_compilation/ov_cc_collect.cpp+++ b/src/core/tests/conditional_compilation/ov_cc_collect.cpp@@ -43,12 +43,16 @@ TEST(conditional_compilation, collect_ops_in_opset) {#define ov_opset_test_opset1_Abs 1ov::OpSet opset("test_opset1");INSERT_OP(test_opset1, Abs, ov::op::v0);- EXPECT_NE(opset.create("Abs"), nullptr);- EXPECT_NE(opset.create_insensitive("Abs"), nullptr);+ auto node = std::shared_ptr<ov::Node>(opset.create("Abs"));+ EXPECT_NE(node.get(), nullptr);+ node = std::shared_ptr<ov::Node>(opset.create_insensitive("Abs"));+ EXPECT_NE(node.get(), nullptr);INSERT_OP(test_opset1, Constant, ov::op::v0);- EXPECT_NE(opset.create("Constant"), nullptr);- EXPECT_NE(opset.create_insensitive("Constant"), nullptr);+ node = std::shared_ptr<ov::Node>(opset.create("Constant"));+ EXPECT_NE(node.get(), nullptr);+ node = std::shared_ptr<ov::Node>(opset.create_insensitive("Constant"));+ EXPECT_NE(node.get(), nullptr);#undef ov_opset_test_opset1_Abs}diff --git a/src/core/tests/conditional_compilation/ov_cc_off.cpp b/src/core/tests/conditional_compilation/ov_cc_off.cppindex 961f172e812..0e6be6e7471 100644--- a/src/core/tests/conditional_compilation/ov_cc_off.cpp+++ b/src/core/tests/conditional_compilation/ov_cc_off.cpp@@ -38,12 +38,16 @@ TEST(conditional_compilation, op_scope_with_disabled_cc) {TEST(conditional_compilation, all_ops_enabled_in_opset) {ov::OpSet opset("test_opset2");INSERT_OP(test_opset2, Abs, ov::op::v0);- EXPECT_NE(opset.create("Abs"), nullptr);- EXPECT_NE(opset.create_insensitive("Abs"), nullptr);+ auto node = std::shared_ptr<ov::Node>(opset.create("Abs"));+ EXPECT_NE(node.get(), nullptr);+ node = std::shared_ptr<ov::Node>(opset.create_insensitive("Abs"));+ EXPECT_NE(node.get(), nullptr);INSERT_OP(test_opset2, Constant, ov::op::v0);- EXPECT_NE(opset.create("Constant"), nullptr);- EXPECT_NE(opset.create_insensitive("Constant"), nullptr);+ node = std::shared_ptr<ov::Node>(opset.create("Constant"));+ EXPECT_NE(node.get(), nullptr);+ node = std::shared_ptr<ov::Node>(opset.create_insensitive("Constant"));+ EXPECT_NE(node.get(), nullptr);}namespace {diff --git a/src/core/tests/conditional_compilation/ov_cc_on.cpp b/src/core/tests/conditional_compilation/ov_cc_on.cppindex 4550c964c5a..b0d6b5c0002 100644--- a/src/core/tests/conditional_compilation/ov_cc_on.cpp+++ b/src/core/tests/conditional_compilation/ov_cc_on.cpp@@ -41,12 +41,16 @@ TEST(conditional_compilation, disabled_Constant_in_opset) {#define ov_opset_test_opset3_Abs 1ov::OpSet opset("test_opset3");INSERT_OP(test_opset3, Abs, ov::op::v0);- EXPECT_NE(opset.create("Abs"), nullptr);- EXPECT_NE(opset.create_insensitive("Abs"), nullptr);+ auto node = std::shared_ptr<ov::Node>(opset.create("Abs"));+ EXPECT_NE(node.get(), nullptr);+ node = std::shared_ptr<ov::Node>(opset.create_insensitive("Abs"));+ EXPECT_NE(node.get(), nullptr);INSERT_OP(test_opset3, Constant, ov::op::v0);- EXPECT_EQ(opset.create("Constant"), nullptr);- EXPECT_EQ(opset.create_insensitive("Constant"), nullptr);+ node = std::shared_ptr<ov::Node>(opset.create("Constant"));+ EXPECT_EQ(node.get(), nullptr);+ node = std::shared_ptr<ov::Node>(opset.create_insensitive("Constant"));+ EXPECT_EQ(node.get(), nullptr);#undef ov_opset_test_opset3_Abs}diff --git a/src/core/tests/model.cpp b/src/core/tests/model.cppindex 6be95a69a1e..898079cfc8a 100644--- a/src/core/tests/model.cpp+++ b/src/core/tests/model.cpp@@ -22,10 +22,8 @@#include "openvino/op/subtract.hpp"#include "shared_node_info.hpp"-using ov::op::util::Variable;-using ov::op::util::VariableInfo;-using ov::op::v0::Parameter;-using ov::op::v0::Result;+using ov::op::util::Variable, ov::op::util::VariableInfo;+using ov::op::v0::Parameter, ov::op::v0::Result, ov::op::v0::Relu;TEST(model, get_input_by_tensor_name) {auto arg0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1});@@ -1529,60 +1527,108 @@ bool all_ops_have_same_info(const std::shared_ptr<ov::Model>& f) {} // namespaceTEST(model, topological_sort_throws_if_loop_with_one_node) {- auto arg0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1});- auto relu1 = std::make_shared<ov::op::v0::Relu>(arg0);-- // Loop relu1->relu1- relu1->input(0).replace_source_output(relu1->output(0));+ std::vector<std::weak_ptr<ov::Node>> nodes;+ {+ auto arg0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1});+ auto relu1 = std::make_shared<ov::op::v0::Relu>(arg0);++ // Loop relu1->relu1+ relu1->input(0).replace_source_output(relu1->output(0));+ nodes = {arg0, relu1};++ auto result = std::make_shared<ov::op::v0::Result>(relu1);+ ASSERT_THROW(std::ignore = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{arg0}),+ ov::Exception);+ // break circular dependency to release all nodes+ relu1->set_arguments(ov::OutputVector{});+ }+ EXPECT_THAT(nodes, testing::Each(testing::Property(&std::weak_ptr<ov::Node>::expired, true)));+}- auto result = std::make_shared<ov::op::v0::Result>(relu1);- ASSERT_THROW(std::ignore = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{arg0}),- ov::Exception);+TEST(model, topological_sort_throws_if_loop_with_two_node) {+ std::vector<std::weak_ptr<ov::Node>> nodes;+ {+ auto arg0 = std::make_shared<Parameter>(ov::element::f32, ov::PartialShape{1});+ auto relu1 = std::make_shared<Relu>(arg0);+ auto relu2 = std::make_shared<Relu>(relu1);+ nodes = {arg0, relu1, relu2};+ // Loop relu1->relu2->relu1+ relu1->input(0).replace_source_output(relu2->output(0));++ auto result = std::make_shared<Result>(relu2);+ ASSERT_THROW(std::ignore = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{arg0}),+ ov::Exception);+ // break circular dependency to release all nodes+ relu1->set_arguments(ov::OutputVector{});+ }+ EXPECT_THAT(nodes, testing::Each(testing::Property(&std::weak_ptr<ov::Node>::expired, true)));}TEST(model, topological_sort_throws_if_loop_with_several_nodes) {- auto arg0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1});- auto relu1 = std::make_shared<ov::op::v0::Relu>(arg0);- auto result = std::make_shared<ov::op::v0::Result>(relu1);-- // Loop relu2->relu3->relu2- auto relu2 = std::make_shared<ov::op::v0::Relu>(relu1->output(0));- auto relu3 = std::make_shared<ov::op::v0::Relu>(relu2);- ov::replace_node(relu1, relu3);-- ASSERT_THROW(std::ignore = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{arg0}),- ov::Exception);+ std::vector<std::weak_ptr<ov::Node>> nodes;+ {+ auto arg0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1});+ auto relu1 = std::make_shared<ov::op::v0::Relu>(arg0);+ auto result = std::make_shared<ov::op::v0::Result>(relu1);++ // Loop relu2->relu3->relu2+ auto relu2 = std::make_shared<ov::op::v0::Relu>(relu1->output(0));+ auto relu3 = std::make_shared<ov::op::v0::Relu>(relu2);+ nodes = {arg0, relu1, relu2, relu3};+ ov::replace_node(relu1, relu3);++ ASSERT_THROW(std::ignore = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{arg0}),+ ov::Exception);+ // break circular dependency to release all nodes+ relu2->set_arguments(ov::OutputVector{});+ }+ EXPECT_THAT(nodes, testing::Each(testing::Property(&std::weak_ptr<ov::Node>::expired, true)));}TEST(model, topological_sort_throws_if_loop_with_control_dependency) {- auto arg0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1});- auto relu1 = std::make_shared<ov::op::v0::Relu>(arg0);- auto relu2 = std::make_shared<ov::op::v0::Relu>(relu1);- auto result = std::make_shared<ov::op::v0::Result>(relu2);-- // Loop relu1->relu2->relu1- relu1->add_control_dependency(relu2);-- ASSERT_THROW(std::ignore = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{arg0}),- ov::Exception);+ std::vector<std::weak_ptr<ov::Node>> nodes;+ {+ auto arg0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1});+ auto relu1 = std::make_shared<ov::op::v0::Relu>(arg0);+ auto relu2 = std::make_shared<ov::op::v0::Relu>(relu1);+ auto result = std::make_shared<ov::op::v0::Result>(relu2);+ nodes = {arg0, relu1, relu2};++ // Loop relu1->relu2->relu1+ relu1->add_control_dependency(relu2);++ ASSERT_THROW(std::ignore = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{arg0}),+ ov::Exception);+ // break circular dependency to release all nodes+ relu1->remove_control_dependency(relu2);+ }+ EXPECT_THAT(nodes, testing::Each(testing::Property(&std::weak_ptr<ov::Node>::expired, true)));}TEST(model, topological_sort_throws_if_loop_with_control_dependency_only) {- auto arg0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1});- auto relu0 = std::make_shared<ov::op::v0::Relu>(arg0);- auto result0 = std::make_shared<ov::op::v0::Result>(relu0);-- auto arg1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1});- auto relu1 = std::make_shared<ov::op::v0::Relu>(arg1);- auto result1 = std::make_shared<ov::op::v0::Result>(relu1);-- // Loop relu0->relu1->relu0- relu0->add_control_dependency(relu1);- relu1->add_control_dependency(relu0);-- ASSERT_THROW(- std::ignore = std::make_shared<ov::Model>(ov::ResultVector{result0, result1}, ov::ParameterVector{arg0, arg1}),- ov::Exception);+ std::vector<std::weak_ptr<ov::Node>> nodes;+ {+ auto arg0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1});+ auto relu0 = std::make_shared<ov::op::v0::Relu>(arg0);+ auto result0 = std::make_shared<ov::op::v0::Result>(relu0);++ auto arg1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1});+ auto relu1 = std::make_shared<ov::op::v0::Relu>(arg1);+ auto result1 = std::make_shared<ov::op::v0::Result>(relu1);+ nodes = {arg0, relu0, arg1, relu1};++ // Loop relu0->relu1->relu0+ relu0->add_control_dependency(relu1);+ relu1->add_control_dependency(relu0);++ ASSERT_THROW(std::ignore = std::make_shared<ov::Model>(ov::ResultVector{result0, result1},+ ov::ParameterVector{arg0, arg1}),+ ov::Exception);+ // break circular dependency to release all nodes+ relu0->remove_control_dependency(relu1);+ relu1->remove_control_dependency(relu0);+ }+ EXPECT_THAT(nodes, testing::Each(testing::Property(&std::weak_ptr<ov::Node>::expired, true)));}TEST(model, topological_sort_caching_basic) {diff --git a/src/core/tests/node_input_output.cpp b/src/core/tests/node_input_output.cppindex 241cc76e34a..0c1e6b1643c 100644--- a/src/core/tests/node_input_output.cpp+++ b/src/core/tests/node_input_output.cpp@@ -10,6 +10,7 @@#include <string>#include <vector>+#include "common_test_utils/test_assertions.hpp"#include "openvino/core/graph_util.hpp"#include "openvino/op/add.hpp"#include "openvino/op/convert.hpp"@@ -18,9 +19,11 @@#include "openvino/op/relu.hpp"#include "transformations/utils/utils.hpp"-using namespace ov;-using namespace std;+namespace ov::test {+using ov::op::util::disconnect_output_from_consumers;+using std::make_shared;+using testing::_;TEST(node_input_output, input_create) {auto x = make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 2, 3, 4});@@ -430,6 +433,11 @@ TEST(node_input_output, output_replace_bidirectional_connection) {EXPECT_EQ(mul_count, 2) << "mul should have exactly 2 connections from add2";EXPECT_EQ(add1->output(0).get_target_inputs().size(), 0) << "add1 should have no targets";++ // create model from incorrect net (cycles) to release nodes without leaking memory+ OV_EXPECT_THROW(ov::Model(OutputVector{mul}, ParameterVector{param}), ov::Exception, _);+ // break circular dependency to release all nodes+ relu->set_arguments(ov::OutputVector{});}TEST(node_input_output, output_replace_empty_targets) {@@ -475,3 +483,4 @@ TEST(node_input_output, output_replace_cascade) {EXPECT_EQ(add2->output(0).get_target_inputs().size(), 0);EXPECT_EQ(add3->output(0).get_target_inputs().size(), 0);}+} // namespace ov::testdiff --git a/src/core/tests/ov_default_allocator_test.cpp b/src/core/tests/ov_default_allocator_test.cppindex 79d49904eaa..60747a63e7d 100644--- a/src/core/tests/ov_default_allocator_test.cpp+++ b/src/core/tests/ov_default_allocator_test.cpp@@ -30,7 +30,9 @@ TEST_F(OVDefaultAllocatorTest, canAllocateAndDeallocate) {TEST_F(OVDefaultAllocatorTest, alignedAllocationNotThrow) {ov::Allocator allocator;- OV_ASSERT_NO_THROW(allocator.allocate(64, 64));+ void* ptr = nullptr;+ OV_ASSERT_NO_THROW(ptr = allocator.allocate(64, 64));+ OV_ASSERT_NO_THROW(allocator.deallocate(ptr, 64, 64));}TEST_F(OVDefaultAllocatorTest, sizedAndAlignedDeallocationNotThrow) {