!10806 insert reformat op
From: @liubuyu Reviewed-by: @zhoufeng54,@kisnwang Signed-off-by: @kisnwangpull/10806/MERGE
commit
0212e19bc9
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* 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 "backend/optimizer/ascend/enhancer/add_attr_for_3d_graph.h"
|
||||
#include <memory>
|
||||
#include "backend/session/anf_runtime_algorithm.h"
|
||||
#include "utils/utils.h"
|
||||
#include "base/core_ops.h"
|
||||
#include "runtime/device/kernel_info.h"
|
||||
#include "backend/optimizer/common/helper.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace opt {
|
||||
const BaseRef AddIoFormatAttrFor3DGraph::DefinePattern() const {
|
||||
std::shared_ptr<Var> V = std::make_shared<CondVar>(UnVisited);
|
||||
std::shared_ptr<Var> Xs = std::make_shared<SeqVar>();
|
||||
return VectorRef({V, Xs});
|
||||
}
|
||||
|
||||
const AnfNodePtr AddIoFormatAttrFor3DGraph::Process(const FuncGraphPtr &func_graph, const AnfNodePtr &node,
|
||||
const EquivPtr &) const {
|
||||
MS_EXCEPTION_IF_NULL(node);
|
||||
MS_EXCEPTION_IF_NULL(func_graph);
|
||||
if (AnfAlgo::IsRealKernel(node)) {
|
||||
AnfAlgo::SetNodeAttr(kAttrVisited, MakeValue(true), node);
|
||||
auto formats = AnfAlgo::GetAllOutputFormats(node);
|
||||
if (std::any_of(formats.begin(), formats.end(),
|
||||
[](const std::string &format) { return k3DFormatSet.find(format) != k3DFormatSet.end(); })) {
|
||||
AnfAlgo::SetNodeAttr("io_format", MakeValue(kOpFormat_NCDHW), node);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace opt
|
||||
} // namespace mindspore
|
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* 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 MINDSPORE_CCSRC_PRE_ACTIVATE_ASCEND_ENHANCER_ADD_ATTR_FOR_3D_GRAPH_H
|
||||
#define MINDSPORE_CCSRC_PRE_ACTIVATE_ASCEND_ENHANCER_ADD_ATTR_FOR_3D_GRAPH_H
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
#include "ir/anf.h"
|
||||
#include "backend/optimizer/common/optimizer.h"
|
||||
#include "backend/optimizer/ascend/ascend_helper.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace opt {
|
||||
class AddIoFormatAttrFor3DGraph : public PatternProcessPass {
|
||||
public:
|
||||
explicit AddIoFormatAttrFor3DGraph(bool multigraph = true)
|
||||
: PatternProcessPass("add_attr_for_3d_graph", multigraph) {}
|
||||
~AddIoFormatAttrFor3DGraph() override = default;
|
||||
const BaseRef DefinePattern() const override;
|
||||
const AnfNodePtr Process(const FuncGraphPtr &, const AnfNodePtr &, const EquivPtr &) const override;
|
||||
};
|
||||
} // namespace opt
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_PRE_ACTIVATE_ASCEND_ENHANCER_ADD_ATTR_FOR_3D_GRAPH_H
|
@ -1,63 +0,0 @@
|
||||
/**
|
||||
* 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 "backend/optimizer/ascend/format_type/add_attr_for_3d_graph.h"
|
||||
#include <memory>
|
||||
#include "backend/session/anf_runtime_algorithm.h"
|
||||
#include "utils/utils.h"
|
||||
#include "base/core_ops.h"
|
||||
#include "runtime/device/kernel_info.h"
|
||||
#include "backend/optimizer/common/helper.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace opt {
|
||||
namespace {
|
||||
void AddAttrForAllCNode(const std::vector<AnfNodePtr> &node_list) {
|
||||
for (auto node : node_list) {
|
||||
if (node == nullptr || !node->isa<CNode>() || !AnfAlgo::IsRealKernel(node)) {
|
||||
continue;
|
||||
}
|
||||
AnfAlgo::SetNodeAttr("io_format", MakeValue(kOpFormat_NCDHW), node);
|
||||
}
|
||||
}
|
||||
|
||||
bool NodeHasAttrIoFormat(const AnfNodePtr &node) {
|
||||
MS_EXCEPTION_IF_NULL(node);
|
||||
if (node->isa<CNode>()) {
|
||||
auto cnode = node->cast<CNodePtr>();
|
||||
MS_EXCEPTION_IF_NULL(cnode);
|
||||
if (AnfAlgo::HasNodeAttr("io_format", cnode)) {
|
||||
auto attr = AnfAlgo::GetNodeAttr<std::string>(cnode, "io_format");
|
||||
return attr == kOpFormat_NCDHW;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool AddIoFormatAttrFor3DGraph::Run(const FuncGraphPtr &func_graph) {
|
||||
MS_EXCEPTION_IF_NULL(func_graph);
|
||||
std::vector<AnfNodePtr> node_list = TopoSort(func_graph->get_return());
|
||||
bool changed = false;
|
||||
if (std::any_of(node_list.begin(), node_list.end(),
|
||||
[](const AnfNodePtr &node) { return NodeHasAttrIoFormat(node); })) {
|
||||
AddAttrForAllCNode(node_list);
|
||||
changed = true;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
} // namespace opt
|
||||
} // namespace mindspore
|
@ -0,0 +1,136 @@
|
||||
/**
|
||||
* 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 "backend/optimizer/ascend/format_type/add_reformat_op.h"
|
||||
#include <memory>
|
||||
#include "backend/session/anf_runtime_algorithm.h"
|
||||
#include "utils/utils.h"
|
||||
#include "base/core_ops.h"
|
||||
#include "runtime/device/kernel_info.h"
|
||||
#include "backend/optimizer/common/helper.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace opt {
|
||||
using KernelWithIndex = std::pair<AnfNodePtr, size_t>;
|
||||
namespace {
|
||||
AnfNodePtr InsertReFormatOp(const FuncGraphPtr &func_graph, const AnfNodePtr &node, const AnfNodePtr &in_node,
|
||||
size_t idx) {
|
||||
MS_EXCEPTION_IF_NULL(node);
|
||||
MS_EXCEPTION_IF_NULL(in_node);
|
||||
MS_EXCEPTION_IF_NULL(func_graph);
|
||||
std::vector<AnfNodePtr> reformat_inputs;
|
||||
auto node_kernel_build_info = AnfAlgo::GetSelectKernelBuildInfo(node);
|
||||
MS_EXCEPTION_IF_NULL(node_kernel_build_info);
|
||||
auto reformat_prim = std::make_shared<Primitive>(prim::kPrimReformat->name());
|
||||
reformat_inputs.push_back(NewValueNode(reformat_prim));
|
||||
reformat_inputs.push_back(in_node);
|
||||
auto reformat = func_graph->NewCNode(reformat_inputs);
|
||||
auto reformat_builder = std::make_shared<kernel::KernelBuildInfo::KernelBuildInfoBuilder>();
|
||||
reformat_builder->SetInputsFormat({AnfAlgo::GetPrevNodeOutputFormat(node, idx)});
|
||||
reformat_builder->SetOutputsFormat({AnfAlgo::GetInputFormat(node, idx)});
|
||||
reformat_builder->SetInputsDeviceType({AnfAlgo::GetPrevNodeOutputDeviceDataType(node, idx)});
|
||||
reformat_builder->SetOutputsDeviceType({node_kernel_build_info->GetInputDeviceType(idx)});
|
||||
AnfAlgo::SetSelectKernelBuildInfo(reformat_builder->Build(), reformat.get());
|
||||
|
||||
reformat->set_abstract(in_node->abstract());
|
||||
AnfAlgo::SetNodeAttr("nop_op", MakeValue(true), reformat);
|
||||
return reformat;
|
||||
}
|
||||
|
||||
bool NeedInsert(const CNodePtr &cnode, const size_t input_index) {
|
||||
KernelWithIndex kernel_with_index = AnfAlgo::GetPrevNodeOutput(cnode, input_index);
|
||||
auto real_input_node = kernel_with_index.first;
|
||||
auto idx = kernel_with_index.second;
|
||||
auto input_format = AnfAlgo::GetInputFormat(cnode, input_index);
|
||||
auto prev_format = AnfAlgo::GetOutputFormat(real_input_node, idx);
|
||||
bool flag_format = (input_format != prev_format);
|
||||
if (!flag_format) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool flag_shape = true;
|
||||
auto input_origin_shape = AnfAlgo::GetOutputInferShape(real_input_node, idx);
|
||||
if (prev_format == kOpFormat_DEFAULT || input_format == kOpFormat_DEFAULT) {
|
||||
string checking_format = (prev_format == kOpFormat_DEFAULT) ? input_format : prev_format;
|
||||
// when input shape size is 1D, default format and NC1HWC0 are compatible
|
||||
if (input_origin_shape.size() == 1 && checking_format == kOpFormat_NC1HWC0) {
|
||||
flag_shape = false;
|
||||
}
|
||||
if (kDefaultCompatibleFormat.find(checking_format) != kDefaultCompatibleFormat.end()) {
|
||||
flag_shape = false;
|
||||
}
|
||||
}
|
||||
if (input_origin_shape.size() == 0) {
|
||||
flag_shape = false;
|
||||
}
|
||||
return flag_format && flag_shape;
|
||||
}
|
||||
|
||||
AnfNodePtr NeedInSertReformatOp(const FuncGraphPtr &func_graph, const AnfNodePtr &node) {
|
||||
MS_EXCEPTION_IF_NULL(node);
|
||||
MS_EXCEPTION_IF_NULL(func_graph);
|
||||
if (!node->isa<CNode>() || !AnfAlgo::IsRealKernel(node)) {
|
||||
return nullptr;
|
||||
}
|
||||
auto cnode = node->cast<CNodePtr>();
|
||||
MS_EXCEPTION_IF_NULL(cnode);
|
||||
auto in_nums = AnfAlgo::GetInputTensorNum(cnode);
|
||||
bool need_insert = false;
|
||||
std::vector<AnfNodePtr> new_inputs = {AnfAlgo::GetCNodePrimitiveNode(cnode)};
|
||||
for (size_t i = 0; i < in_nums; i++) {
|
||||
auto input_node = AnfAlgo::GetInputNode(cnode, i);
|
||||
if (NeedInsert(cnode, i)) {
|
||||
need_insert = true;
|
||||
auto re_format = InsertReFormatOp(func_graph, cnode, input_node, i);
|
||||
new_inputs.push_back(re_format);
|
||||
continue;
|
||||
}
|
||||
new_inputs.push_back(input_node);
|
||||
}
|
||||
if (need_insert) {
|
||||
auto kernel_graph = func_graph->cast<std::shared_ptr<session::KernelGraph>>();
|
||||
CNodePtr new_node = nullptr;
|
||||
if (kernel_graph == nullptr) {
|
||||
new_node = std::make_shared<CNode>(*cnode);
|
||||
} else {
|
||||
new_node = kernel_graph->NewCNode(cnode);
|
||||
}
|
||||
MS_EXCEPTION_IF_NULL(new_node);
|
||||
new_node->set_inputs(new_inputs);
|
||||
AnfAlgo::CopyNodeAttrs(cnode, new_node);
|
||||
return new_node;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool AddReFormatOp::Run(const FuncGraphPtr &func_graph) {
|
||||
MS_EXCEPTION_IF_NULL(func_graph);
|
||||
std::vector<AnfNodePtr> node_list = TopoSort(func_graph->get_return());
|
||||
bool changed = false;
|
||||
auto manager = func_graph->manager();
|
||||
MS_EXCEPTION_IF_NULL(manager);
|
||||
for (auto &node : node_list) {
|
||||
auto new_node = NeedInSertReformatOp(func_graph, node);
|
||||
if (new_node != nullptr) {
|
||||
manager->Replace(node, new_node);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
} // namespace opt
|
||||
} // namespace mindspore
|
Loading…
Reference in new issue