You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
graphengine/ge/graph/passes/enter_pass.cc

100 lines
3.2 KiB

5 years ago
/**
* Copyright 2020 Huawei Technologies Co., Ltd
5 years ago
*
* 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/enter_pass.h"
#include "graph/debug/ge_attr_define.h"
5 years ago
#include "framework/common/debug/ge_log.h"
#include "framework/common/debug/log.h"
#include "graph/utils/graph_utils.h"
namespace {
const size_t kOutNodesNum = 1;
}
5 years ago
namespace ge {
Status EnterPass::Run(NodePtr &node) {
GELOGD("EnterPass running");
GE_CHECK_NOTNULL(node);
5 years ago
if ((node->GetType() != ENTER) && (node->GetType() != REFENTER)) {
return SUCCESS;
}
// enter node has only one input
if (node->GetInDataNodes().empty()) {
GELOGE(PARAM_INVALID, "enter_node %s has no input", node->GetName().c_str());
return PARAM_INVALID;
}
NodePtr in_node = node->GetInDataNodes().at(0);
GE_CHECK_NOTNULL(in_node);
5 years ago
if ((in_node->GetType() != CONSTANT) && (in_node->GetType() != CONSTANTOP)) {
return SUCCESS;
}
bool need_remove_flag = in_node->GetInControlNodes().empty() && node->GetInControlNodes().empty();
if (!need_remove_flag) {
return SUCCESS;
}
if (node->GetOutDataNodes().empty()) {
5 years ago
for (auto &out_ctrl_node : node->GetOutControlNodes()) {
if (out_ctrl_node == nullptr) {
continue;
}
if (GraphUtils::RemoveEdge(node->GetOutControlAnchor(), out_ctrl_node->GetInControlAnchor()) != GRAPH_SUCCESS) {
GELOGE(FAILED, "Remove Enter ctrl output fail, %s->%s", node->GetName().c_str(),
out_ctrl_node->GetName().c_str());
5 years ago
return FAILED;
}
}
} else {
4 years ago
if (OptimizeEnter(node, in_node) != SUCCESS) {
GELOGE(FAILED, "Optimize enter node[%s] failed.", node->GetName().c_str());
return FAILED;
}
5 years ago
}
GELOGD("EnterPass success");
return SUCCESS;
}
4 years ago
Status EnterPass::OptimizeEnter(NodePtr &node, NodePtr &in_node) {
if ((in_node->GetOutAllNodes().size() != kOutNodesNum) || !node->GetOutControlNodes().empty()) {
return SUCCESS;
}
bool is_constant_flag = true;
(void)AttrUtils::GetBool(node->GetOpDesc(), ENTER_ATTR_CONSTANT_FLAG, is_constant_flag);
if (!is_constant_flag) {
return SUCCESS;
}
GE_CHECK_NOTNULL(in_node->GetOutDataAnchor(0));
4 years ago
GE_CHK_STATUS_RET(in_node->GetOutDataAnchor(0)->Unlink(node->GetInDataAnchor(0)));
const auto &out_data_anchor = node->GetOutDataAnchor(0);
GE_CHECK_NOTNULL(out_data_anchor);
for (const auto &peer_in_data_anchor : out_data_anchor->GetPeerInDataAnchors()) {
4 years ago
GE_CHK_STATUS_RET(out_data_anchor->Unlink(peer_in_data_anchor));
GE_CHK_STATUS_RET(in_node->GetOutDataAnchor(0)->LinkTo(peer_in_data_anchor));
}
4 years ago
GE_CHK_STATUS_RET(GraphUtils::RemoveNodeWithoutRelink(node->GetOwnerComputeGraph(), node));
AddNodeDeleted(node);
AddRePassNodesWithInOut(in_node);
return SUCCESS;
}
5 years ago
} // namespace ge