parent
fe5db33358
commit
2898b2d83c
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,106 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 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 "remove_same_const_pass.h"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
|
#include "common/base64.h"
|
||||||
|
#include "ge_local_engine/engine/host_cpu_engine.h"
|
||||||
|
#include "graph/utils/node_utils.h"
|
||||||
|
|
||||||
|
namespace ge {
|
||||||
|
namespace {
|
||||||
|
std::string GetCseKey(const NodePtr &node) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << node->GetType() << "control-inputs-";
|
||||||
|
std::set<std::string> control_in_node_names;
|
||||||
|
for (auto &src_node : node->GetInControlNodes()) {
|
||||||
|
control_in_node_names.insert(src_node->GetName());
|
||||||
|
}
|
||||||
|
for (auto &name : control_in_node_names) {
|
||||||
|
ss << name << "-";
|
||||||
|
}
|
||||||
|
|
||||||
|
ss << "attrs-" << AttrUtils::GetAllAttrsStr(node->GetOpDesc());
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsConstType(const NodePtr &node) { return (node->GetType() == CONSTANT || node->GetType() == CONSTANTOP); }
|
||||||
|
} // namespace
|
||||||
|
Status RemoveSameConstPass::Run(ComputeGraphPtr graph) {
|
||||||
|
GELOGD("Begin to run RemoveSameConstPass on the graph");
|
||||||
|
GE_CHECK_NOTNULL(graph);
|
||||||
|
std::map<std::string, NodePtr> keys_to_node;
|
||||||
|
for (const auto &node : graph->GetDirectNode()) {
|
||||||
|
GE_CHECK_NOTNULL(node);
|
||||||
|
if (!IsConstType(node)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
bool is_unknown = false;
|
||||||
|
auto ret = NodeUtils::GetNodeUnknownShapeStatus(*node, is_unknown);
|
||||||
|
if (ret != GRAPH_SUCCESS) {
|
||||||
|
GELOGW("Get node unknown status failed, node name:%s, type:%s.",
|
||||||
|
node->GetName().c_str(), node->GetType().c_str());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (is_unknown) {
|
||||||
|
GELOGI("Current node %s, type %s is unknown shape which should be skip.",
|
||||||
|
node->GetName().c_str(), node->GetType().c_str());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto key = GetCseKey(node);
|
||||||
|
GELOGD("The const node %s cse key %s", node->GetName().c_str(), ge::base64::EncodeToBase64(key).c_str());
|
||||||
|
auto iter = keys_to_node.find(key);
|
||||||
|
if (iter == keys_to_node.end()) {
|
||||||
|
keys_to_node[key] = node;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node->GetAllOutDataAnchorsSize() != iter->second->GetAllOutDataAnchorsSize()) {
|
||||||
|
GELOGW("The const node %s and %s have the same CSE key, but different output anchor count, skip to fusion them",
|
||||||
|
iter->second->GetName().c_str(), node->GetName().c_str());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int> output_map(node->GetAllOutDataAnchorsSize());
|
||||||
|
for (size_t i = 0; i < node->GetAllOutDataAnchorsSize(); ++i) {
|
||||||
|
output_map[i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = GraphUtils::ReplaceNodeAnchors(iter->second, node, {}, output_map);
|
||||||
|
if (ret != GRAPH_SUCCESS) {
|
||||||
|
GELOGE(INTERNAL_ERROR, "Failed to replace node %s by node %s", node->GetName().c_str(),
|
||||||
|
iter->second->GetName().c_str(), ret);
|
||||||
|
return INTERNAL_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeUtils::UnlinkAll(*node);
|
||||||
|
|
||||||
|
ret = GraphUtils::RemoveNodeWithoutRelink(graph, node);
|
||||||
|
if (ret != GRAPH_SUCCESS) {
|
||||||
|
GELOGE(INTERNAL_ERROR, "Failed to remove node %s from graph", node->GetName().c_str());
|
||||||
|
return INTERNAL_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
GELOGI("Remove const node %s by RemoveSameConstPass, replace it with node %s", node->GetName().c_str(),
|
||||||
|
iter->second->GetName().c_str());
|
||||||
|
}
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
} // namespace ge
|
@ -0,0 +1,28 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 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.
|
||||||
|
*/
|
||||||
|
#ifndef GE_GRAPH_PASSES_REMOVE_SAME_CONST_PASS_H_
|
||||||
|
#define GE_GRAPH_PASSES_REMOVE_SAME_CONST_PASS_H_
|
||||||
|
|
||||||
|
#include "graph/types.h"
|
||||||
|
#include "inc/graph_pass.h"
|
||||||
|
|
||||||
|
namespace ge {
|
||||||
|
class RemoveSameConstPass : public GraphPass {
|
||||||
|
public:
|
||||||
|
Status Run(ge::ComputeGraphPtr graph) override ;
|
||||||
|
};
|
||||||
|
} // namespace ge
|
||||||
|
#endif //GE_GRAPH_PASSES_REMOVE_SAME_CONST_PASS_H_
|
@ -0,0 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 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/useless_control_out_remove_pass.h"
|
||||||
|
|
||||||
|
#include "graph/debug/ge_attr_define.h"
|
||||||
|
#include "graph/utils/graph_utils.h"
|
||||||
|
#include "framework/common/debug/ge_log.h"
|
||||||
|
#include "framework/common/debug/log.h"
|
||||||
|
|
||||||
|
namespace ge {
|
||||||
|
Status UselessControlOutRemovePass::Run(NodePtr &node) {
|
||||||
|
GE_CHECK_NOTNULL(node);
|
||||||
|
|
||||||
|
if ((node->GetType() != CONSTANT) && (node->GetType() != CONSTANTOP)) {
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
GELOGD("UselessControlOutRemovePass running, node: %s.", node->GetName().c_str());
|
||||||
|
|
||||||
|
// const has no control input
|
||||||
|
if (node->GetInControlNodes().empty()) {
|
||||||
|
if (node->GetOutDataNodes().empty()) {
|
||||||
|
// It is an isolated const, just remove it.
|
||||||
|
GELOGI("Delete isolated const: %s.", node->GetName().c_str());
|
||||||
|
GE_CHK_STATUS_RET(IsolateAndDeleteNode(node, {}))
|
||||||
|
AddNodeDeleted(node);
|
||||||
|
} else {
|
||||||
|
auto out_ctrl_anchor = node->GetOutControlAnchor();
|
||||||
|
if (out_ctrl_anchor != nullptr && !out_ctrl_anchor->GetPeerAnchors().empty()) {
|
||||||
|
GELOGI("Node: %s unlink all out control edge.", node->GetName().c_str());
|
||||||
|
out_ctrl_anchor->UnlinkAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
} // namespace ge
|
@ -0,0 +1,29 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GE_GRAPH_PASSES_USELESS_CONTROL_OUT_REMOVE_PASS_H_
|
||||||
|
#define GE_GRAPH_PASSES_USELESS_CONTROL_OUT_REMOVE_PASS_H_
|
||||||
|
|
||||||
|
#include "graph/passes/base_pass.h"
|
||||||
|
|
||||||
|
namespace ge {
|
||||||
|
class UselessControlOutRemovePass : public BaseNodePass {
|
||||||
|
public:
|
||||||
|
Status Run(NodePtr &node) override;
|
||||||
|
};
|
||||||
|
} // namespace ge
|
||||||
|
|
||||||
|
#endif // GE_GRAPH_PASSES_USELESS_CONTROL_OUT_REMOVE_PASS_H_
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue