From aa06a0e93d52738bd6fe1c63ad869ee9d36004e6 Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Fri, 26 Feb 2021 09:38:40 +0800 Subject: [PATCH 1/8] modified: ge/graph/passes/replace_with_empty_const_pass.cc --- ge/graph/passes/replace_with_empty_const_pass.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ge/graph/passes/replace_with_empty_const_pass.cc b/ge/graph/passes/replace_with_empty_const_pass.cc index f3887867..e44aee09 100644 --- a/ge/graph/passes/replace_with_empty_const_pass.cc +++ b/ge/graph/passes/replace_with_empty_const_pass.cc @@ -33,8 +33,8 @@ Status ReplaceWithEmptyConstPass::Run(NodePtr &node) { GELOGE(PARAM_INVALID, "Param [opDesc] must not be null."); return PARAM_INVALID; } - if (node->GetType() == CONSTANT || node->GetType() == CONSTANTOP) { - GELOGI("Node %s is const. Ignore current pass.", node->GetName().c_str()); + if (node->GetType() == CONSTANT || node->GetType() == CONSTANTOP || node->GetType() == DATA) { + GELOGD("Node %s is const or data. Ignore current pass.", node->GetName().c_str()); return SUCCESS; } // Node like no op, it has no output From ad7b6cadf1f0e41d1586b8eeb7b7fed36768928f Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Mon, 1 Mar 2021 20:58:47 +0800 Subject: [PATCH 2/8] modified: replace_with_empty_const_pass.cc --- ge/graph/passes/replace_with_empty_const_pass.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ge/graph/passes/replace_with_empty_const_pass.cc b/ge/graph/passes/replace_with_empty_const_pass.cc index e44aee09..223ee7ea 100644 --- a/ge/graph/passes/replace_with_empty_const_pass.cc +++ b/ge/graph/passes/replace_with_empty_const_pass.cc @@ -34,7 +34,7 @@ Status ReplaceWithEmptyConstPass::Run(NodePtr &node) { return PARAM_INVALID; } if (node->GetType() == CONSTANT || node->GetType() == CONSTANTOP || node->GetType() == DATA) { - GELOGD("Node %s is const or data. Ignore current pass.", node->GetName().c_str()); + GELOGD("Node %s is const. Ignore current pass.", node->GetName().c_str()); return SUCCESS; } // Node like no op, it has no output From 1c733582ef0fc498c98c9e478a99c3a1ed90fd5a Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Mon, 1 Mar 2021 20:59:37 +0800 Subject: [PATCH 3/8] modified: replace_with_empty_const_pass.cc --- ge/graph/passes/replace_with_empty_const_pass.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ge/graph/passes/replace_with_empty_const_pass.cc b/ge/graph/passes/replace_with_empty_const_pass.cc index 223ee7ea..5962fe0e 100644 --- a/ge/graph/passes/replace_with_empty_const_pass.cc +++ b/ge/graph/passes/replace_with_empty_const_pass.cc @@ -34,7 +34,7 @@ Status ReplaceWithEmptyConstPass::Run(NodePtr &node) { return PARAM_INVALID; } if (node->GetType() == CONSTANT || node->GetType() == CONSTANTOP || node->GetType() == DATA) { - GELOGD("Node %s is const. Ignore current pass.", node->GetName().c_str()); + GELOGI("Node %s is const. Ignore current pass.", node->GetName().c_str()); return SUCCESS; } // Node like no op, it has no output From ce359a3c325f1ca10d5991ecc4681a2fe0d75680 Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Thu, 4 Mar 2021 14:54:37 +0800 Subject: [PATCH 4/8] modified: tests/ut/ge/CMakeLists.txt new file: tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc --- tests/ut/ge/CMakeLists.txt | 1 + .../replace_with_empty_const_pass_unittest.cc | 83 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc diff --git a/tests/ut/ge/CMakeLists.txt b/tests/ut/ge/CMakeLists.txt index a09d5789..5456e151 100755 --- a/tests/ut/ge/CMakeLists.txt +++ b/tests/ut/ge/CMakeLists.txt @@ -688,6 +688,7 @@ set(PASS_TEST_FILES "graph/passes/no_use_reshape_remove_pass_unittest.cc" "graph/passes/infershape_pass_unittest.cc" "graph/passes/multi_batch_clone_pass_unittest.cc" + "graph/passes/replace_with_empty_const_pass_unittest.cc" ) set(KERNEL_TEST_FILES diff --git a/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc b/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc new file mode 100644 index 00000000..078d8dbc --- /dev/null +++ b/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc @@ -0,0 +1,83 @@ +/** + * Copyright 2019-2020 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "graph/passes/replace_with_empty_const_pass.h" + +#include +#include +#include + +#include "graph_builder_utils.h" + +namespace ge { +class UtestReplaceWithEmptyConstPass : public testing::Test { + protected: + void SetUp() {} + void TearDown() {} +}; + +namespace { +/// data1 const1 +/// \ / +/// add1 +/// | +/// cast1(empty) +/// | +/// conv2d +ut::GraphBuilder Graph1Builder() { + ut::GraphBuilder builder = ut::GraphBuilder("g1"); + auto data1 = builder.AddNode("data1", "Data", 0, 1); + auto const1 = builder.AddNode("const1", "Const", 0, 1); + auto add1 = builder.AddNode("add1", "Add", 2, 1); + auto cast1 = builder.AddNode("cast1", "Cast", 1, 1); + auto conv2d = builder.AddNode("conv2d", "Conv2D", 1, 0); + + add1->GetOpDesc()->AddInputDesc(GeTensorDesc(GeShape({1,1,8,8}),FORMAT_NCHW)); + add1->GetOpDesc()->AddInputDesc(GeTensorDesc(GeShape({1,1,8,8}),FORMAT_NCHW)); + add1->GetOpDesc()->AddOutputDesc(GeTensorDesc(GeShape({1,1,8,8}),FORMAT_NCHW)); + cast1->GetOpDesc()->AddOutputDesc(GeTensorDesc(GeShape({1,1,8,8}),FORMAT_NCHW)); + GeTensorDesc empty_tensor(GeShape({1,0,8,8}),FORMAT_NCHW); + cast1->GetOpDesc()->UpdateOutputDesc(0,empty_tensor); + + builder.AddDataEdge(data1, 0, add1, 0); + builder.AddDataEdge(const1, 0, add1, 1); + builder.AddDataEdge(add1, 0, cast1, 0); + builder.AddDataEdge(cast1, 0, conv2d, 0); + return builder; +} +} // namespace + + +TEST_F(UtestReplaceWithEmptyConstPass, replace_whith_empty_const_success) { + auto builder = Graph1Builder(); + auto graph = builder.GetGraph(); + graph->SetSessionID(0); + ReplaceWithEmptyConstPass replace_with_empty_const_pass; + + EXPECT_EQ(graph->GetDirectNodesSize(),5); + // run pass on add1, graph still has 5 nodes + auto add1 = graph->FindNode("add1"); + Status ret = replace_with_empty_const_pass.Run(add1); + EXPECT_EQ(ret, SUCCESS); + EXPECT_EQ(graph->GetDirectNodesSize(),5); + + auto cast1 = graph->FindNode("cast1"); + ret = replace_with_empty_const_pass.Run(cast1) + EXPECT_EQ(cast1->GetOutAllNodes().size(),0); + auto conv2d = graph->FindNode("conv2d"); + EXPECT_EQ(conv2d->GetInDataNodes().at(0)->GetType(),"Const"); +} +} // namespace ge From 554d08f277d9a51cd377c8aa2782f1bd1ed4f33c Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Thu, 4 Mar 2021 15:21:27 +0800 Subject: [PATCH 5/8] modified: tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc --- .../ge/graph/passes/replace_with_empty_const_pass_unittest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc b/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc index 078d8dbc..348543d3 100644 --- a/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc +++ b/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc @@ -75,7 +75,7 @@ TEST_F(UtestReplaceWithEmptyConstPass, replace_whith_empty_const_success) { EXPECT_EQ(graph->GetDirectNodesSize(),5); auto cast1 = graph->FindNode("cast1"); - ret = replace_with_empty_const_pass.Run(cast1) + ret = replace_with_empty_const_pass.Run(cast1); EXPECT_EQ(cast1->GetOutAllNodes().size(),0); auto conv2d = graph->FindNode("conv2d"); EXPECT_EQ(conv2d->GetInDataNodes().at(0)->GetType(),"Const"); From 0646b4b6efd78c87b19ed9fff4683d32bc1ab767 Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Thu, 4 Mar 2021 15:32:42 +0800 Subject: [PATCH 6/8] modified: tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc --- .../graph/passes/replace_with_empty_const_pass_unittest.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc b/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc index 348543d3..d7984fb3 100644 --- a/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc +++ b/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc @@ -74,6 +74,12 @@ TEST_F(UtestReplaceWithEmptyConstPass, replace_whith_empty_const_success) { EXPECT_EQ(ret, SUCCESS); EXPECT_EQ(graph->GetDirectNodesSize(),5); + // run pass on const1, graph still has 5 nodes + auto const1 = graph->FindNode("const1"); + Status ret = replace_with_empty_const_pass.Run(const1); + EXPECT_EQ(ret, SUCCESS); + EXPECT_EQ(graph->GetDirectNodesSize(),5); + auto cast1 = graph->FindNode("cast1"); ret = replace_with_empty_const_pass.Run(cast1); EXPECT_EQ(cast1->GetOutAllNodes().size(),0); From d07364e44489a0abb21dcac25a4eb27dfb9295d7 Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Thu, 4 Mar 2021 15:33:46 +0800 Subject: [PATCH 7/8] modified: tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc --- .../ge/graph/passes/replace_with_empty_const_pass_unittest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc b/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc index d7984fb3..e52f1237 100644 --- a/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc +++ b/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc @@ -76,7 +76,7 @@ TEST_F(UtestReplaceWithEmptyConstPass, replace_whith_empty_const_success) { // run pass on const1, graph still has 5 nodes auto const1 = graph->FindNode("const1"); - Status ret = replace_with_empty_const_pass.Run(const1); + ret = replace_with_empty_const_pass.Run(const1); EXPECT_EQ(ret, SUCCESS); EXPECT_EQ(graph->GetDirectNodesSize(),5); From 42b72d74b550cd613b697ad8ed7e713130b897aa Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Thu, 4 Mar 2021 16:05:52 +0800 Subject: [PATCH 8/8] modified: tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc --- .../ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc b/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc index e52f1237..6711b0d3 100644 --- a/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc +++ b/tests/ut/ge/graph/passes/replace_with_empty_const_pass_unittest.cc @@ -48,7 +48,6 @@ ut::GraphBuilder Graph1Builder() { add1->GetOpDesc()->AddInputDesc(GeTensorDesc(GeShape({1,1,8,8}),FORMAT_NCHW)); add1->GetOpDesc()->AddInputDesc(GeTensorDesc(GeShape({1,1,8,8}),FORMAT_NCHW)); add1->GetOpDesc()->AddOutputDesc(GeTensorDesc(GeShape({1,1,8,8}),FORMAT_NCHW)); - cast1->GetOpDesc()->AddOutputDesc(GeTensorDesc(GeShape({1,1,8,8}),FORMAT_NCHW)); GeTensorDesc empty_tensor(GeShape({1,0,8,8}),FORMAT_NCHW); cast1->GetOpDesc()->UpdateOutputDesc(0,empty_tensor);