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/no_use_reshape_remove_pass.cc

122 lines
4.3 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/no_use_reshape_remove_pass.h"
#include <string>
#include <vector>
#include "common/op/ge_op_utils.h"
5 years ago
#include "external/graph/types.h"
#include "framework/common/debug/ge_log.h"
#include "framework/common/ge_inner_error_codes.h"
#include "graph/passes/pass_utils.h"
5 years ago
#include "graph/utils/graph_utils.h"
#include "graph/utils/op_desc_utils.h"
#include "graph/utils/tensor_utils.h"
namespace ge {
namespace {
const int kReshapeDataIndex = 0;
const int kReshapeShapeIndex = 1;
} // namespace
Status NoUseReshapeRemovePass::Run(ge::NodePtr &node) {
GE_CHECK_NOTNULL(node);
OpDescPtr op_desc_ptr = node->GetOpDesc();
if (op_desc_ptr == nullptr) {
GELOGE(PARAM_INVALID, "NoUseReshapeRemovePass enter. OpDesc is null.");
return PARAM_INVALID;
}
if (op_desc_ptr->GetType() != RESHAPE) {
5 years ago
return SUCCESS;
}
GELOGI("NoUseReshapeRemovePass enter.");
bool to_be_deleted = true;
// compare input and output dims
if (op_desc_ptr->GetAllInputsDesc().empty() || op_desc_ptr->GetAllOutputsDesc().empty()) {
GELOGE(INTERNAL_ERROR, "Input or output num is zero. node name:%s, input size:%zu, output size:%zu",
op_desc_ptr->GetName().c_str(), op_desc_ptr->GetAllInputsDesc().size(),
op_desc_ptr->GetAllOutputsDesc().size());
return INTERNAL_ERROR;
}
const auto &input_desc = op_desc_ptr->MutableInputDesc(0);
const auto &output_desc = op_desc_ptr->MutableOutputDesc(0);
GE_CHECK_NOTNULL(input_desc);
GE_CHECK_NOTNULL(output_desc);
std::vector<int64_t> input_4dims = input_desc->GetShape().GetDims();
std::vector<int64_t> output_4dims = output_desc->GetShape().GetDims();
5 years ago
if (input_desc->GetShape().IsUnknownShape() || output_desc->GetShape().IsUnknownShape()) {
GELOGI("Current Reshape %s is unknown shape which should be kept.", op_desc_ptr->GetName().c_str());
return SUCCESS;
}
5 years ago
if (input_4dims.size() != output_4dims.size()) {
GELOGI("Input and output dim size is not equal.Keep this reshape op.");
return SUCCESS;
}
size_t vec_size = input_4dims.size();
for (size_t i = 0; i < vec_size; i++) {
if (input_4dims[i] < 0) {
GELOGI("Input shape is unknown.Keep this reshape op.");
return SUCCESS;
}
if (input_4dims[i] != output_4dims[i]) {
to_be_deleted = false;
break;
}
}
if (to_be_deleted) {
auto ret = TryRemoveConstShapeInput(node);
GE_CHK_STATUS_RET_NOLOG(ret);
GELOGI("NoUseReshapeRemovePass remove useless reshape node:%s", node->GetName().c_str());
5 years ago
return IsolateAndDeleteNode(node, {kReshapeDataIndex});
}
return SUCCESS;
}
Status NoUseReshapeRemovePass::TryRemoveConstShapeInput(ge::NodePtr &reshape_node) {
auto shape_input_anchor = reshape_node->GetInDataAnchor(kReshapeShapeIndex);
if (shape_input_anchor == nullptr) {
return SUCCESS;
}
auto shape_input = shape_input_anchor->GetOwnerNode();
GE_CHECK_NOTNULL(shape_input);
if (shape_input->GetType() != CONSTANT && shape_input->GetType() != CONSTANTOP) {
return SUCCESS;
}
// op(x) const(shape)
// \ /
// reshape
// const input can unlink but should copy control_dependency
auto ret = PassUtils::UnlinkNodeWithControlCopy(reshape_node, kReshapeShapeIndex);
if (ret != SUCCESS) {
GELOGE(ret, "Unlink node %s with control copy failed.", shape_input->GetName().c_str());
return ret;
}
// remove const without any data_output
if (shape_input->GetOutDataNodesSize() == 0) {
auto ret = IsolateAndDeleteNode(shape_input, {});
GE_CHK_GRAPH_STATUS_RET(ret, "Fail to remove node %s", shape_input->GetName().c_str());
GELOGI("Remove useless shape input const %s.", shape_input->GetName().c_str());
}
return SUCCESS;
}
5 years ago
} // namespace ge