!260 refactor padding strategy
Merge pull request !260 from lianliguang/refactor-padding-strategypull/260/MERGE
commit
9bda080bb5
File diff suppressed because it is too large
Load Diff
@ -1,99 +1,99 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
#include "pre_activate/ascend/ir_fusion/transdata_split.h"
|
#include "pre_activate/ascend/ir_fusion/transdata_split.h"
|
||||||
#include <set>
|
#include <set>
|
||||||
#include "pre_activate/ascend/ascend_helper.h"
|
#include "pre_activate/ascend/ascend_helper.h"
|
||||||
#include "session/anf_runtime_algorithm.h"
|
#include "session/anf_runtime_algorithm.h"
|
||||||
#include "debug/anf_ir_dump.h"
|
#include "debug/anf_ir_dump.h"
|
||||||
|
|
||||||
namespace mindspore {
|
namespace mindspore {
|
||||||
namespace opt {
|
namespace opt {
|
||||||
const std::set<std::pair<string, string>> invalid_formats_pair = {{kOpFormat_C1HWNCoC0, kOpFormat_NCHW},
|
const std::set<std::pair<string, string>> invalid_formats_pair = {{kOpFormat_C1HWNCoC0, kOpFormat_NCHW},
|
||||||
{kOpFormat_NCHW, kOpFormat_C1HWNCoC0},
|
{kOpFormat_NCHW, kOpFormat_C1HWNCoC0},
|
||||||
{kOpFormat_C1HWNCoC0, kOpFormat_DEFAULT},
|
{kOpFormat_C1HWNCoC0, kOpFormat_DEFAULT},
|
||||||
{kOpFormat_DEFAULT, kOpFormat_C1HWNCoC0}};
|
{kOpFormat_DEFAULT, kOpFormat_C1HWNCoC0}};
|
||||||
|
|
||||||
bool TransDataSplit::Run(const FuncGraphPtr &func_graph) {
|
bool TransDataSplit::Run(const FuncGraphPtr &func_graph) {
|
||||||
MS_EXCEPTION_IF_NULL(func_graph);
|
MS_EXCEPTION_IF_NULL(func_graph);
|
||||||
bool changed = false;
|
bool changed = false;
|
||||||
std::vector<AnfNodePtr> node_list = TopoSort(func_graph->get_return());
|
std::vector<AnfNodePtr> node_list = TopoSort(func_graph->get_return());
|
||||||
for (auto &node : node_list) {
|
for (auto &node : node_list) {
|
||||||
if (node != nullptr && node->isa<CNode>() && AnfAlgo::GetCNodeName(node) == kTransDataOpName) {
|
if (node != nullptr && node->isa<CNode>() && AnfAlgo::GetCNodeName(node) == kTransDataOpName) {
|
||||||
CheckCNodeInputSize(node->cast<CNodePtr>(), kBackendTransDataInputNum);
|
CheckCNodeInputSize(node->cast<CNodePtr>(), kBackendTransDataInputNum);
|
||||||
if (IsFormatInvaild(node)) {
|
if (IsFormatInvaild(node)) {
|
||||||
changed = DoSplit(func_graph, node);
|
changed = DoSplit(func_graph, node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
bool TransDataSplit::IsFormatInvaild(const AnfNodePtr &node) {
|
bool TransDataSplit::IsFormatInvaild(const AnfNodePtr &node) {
|
||||||
MS_EXCEPTION_IF_NULL(node);
|
MS_EXCEPTION_IF_NULL(node);
|
||||||
auto cnode = node->cast<CNodePtr>();
|
auto cnode = node->cast<CNodePtr>();
|
||||||
MS_EXCEPTION_IF_NULL(cnode);
|
MS_EXCEPTION_IF_NULL(cnode);
|
||||||
auto input_format = AnfAlgo::GetInputFormat(node, 0);
|
auto input_format = AnfAlgo::GetInputFormat(node, 0);
|
||||||
auto output_format = AnfAlgo::GetOutputFormat(node, 0);
|
auto output_format = AnfAlgo::GetOutputFormat(node, 0);
|
||||||
auto format_pair = std::make_pair(input_format, output_format);
|
auto format_pair = std::make_pair(input_format, output_format);
|
||||||
|
|
||||||
return invalid_formats_pair.find(format_pair) != invalid_formats_pair.end();
|
return invalid_formats_pair.find(format_pair) != invalid_formats_pair.end();
|
||||||
}
|
}
|
||||||
// transdata cannot support frac_z to nchw need split transdata(frac_z-HWCN) and transpose(HWCN-NCHW)
|
// transdata cannot support frac_z to nchw need split transdata(frac_z-HWCN) and transpose(HWCN-NCHW)
|
||||||
bool TransDataSplit::DoSplit(const FuncGraphPtr &func_graph, const AnfNodePtr &node) {
|
bool TransDataSplit::DoSplit(const FuncGraphPtr &func_graph, const AnfNodePtr &node) {
|
||||||
MS_EXCEPTION_IF_NULL(func_graph);
|
MS_EXCEPTION_IF_NULL(func_graph);
|
||||||
MS_EXCEPTION_IF_NULL(node);
|
MS_EXCEPTION_IF_NULL(node);
|
||||||
auto cnode = node->cast<CNodePtr>();
|
auto cnode = node->cast<CNodePtr>();
|
||||||
MS_EXCEPTION_IF_NULL(cnode);
|
MS_EXCEPTION_IF_NULL(cnode);
|
||||||
auto input_node = node->cast<CNodePtr>()->input(1);
|
auto input_node = node->cast<CNodePtr>()->input(1);
|
||||||
MS_EXCEPTION_IF_NULL(input_node);
|
MS_EXCEPTION_IF_NULL(input_node);
|
||||||
|
|
||||||
auto input_format = AnfAlgo::GetInputFormat(node, 0);
|
auto input_format = AnfAlgo::GetInputFormat(node, 0);
|
||||||
auto output_format = AnfAlgo::GetOutputFormat(node, 0);
|
auto output_format = AnfAlgo::GetOutputFormat(node, 0);
|
||||||
AnfNodePtr new_transdata_node = nullptr;
|
AnfNodePtr new_transdata_node = nullptr;
|
||||||
AnfNodePtr new_transpose_node = nullptr;
|
AnfNodePtr new_transpose_node = nullptr;
|
||||||
AnfNodePtr new_replace_node = nullptr;
|
AnfNodePtr new_replace_node = nullptr;
|
||||||
// if output_format=default transdata need split transdata->transpose else transpose->transdata
|
// if output_format=default transdata need split transdata->transpose else transpose->transdata
|
||||||
if (output_format == kOpFormat_DEFAULT || output_format == kOpFormat_NCHW) {
|
if (output_format == kOpFormat_DEFAULT || output_format == kOpFormat_NCHW) {
|
||||||
// trans input_format to hwcn
|
// trans input_format to hwcn
|
||||||
new_transdata_node = AddTransOpNodeToGraph(func_graph, node, kernel_select_, 0, false, input_format, kOpFormat_HWCN,
|
new_transdata_node =
|
||||||
kTransDataOpName, true);
|
AddTransOpNodeToGraph(func_graph, node, kernel_select_, 0, input_format, kOpFormat_HWCN, kTransDataOpName, true);
|
||||||
// trans hwcn to default_format
|
// trans hwcn to default_format
|
||||||
new_transpose_node = AddTransOpNodeToGraph(func_graph, new_transdata_node, kernel_select_, 0, false, kOpFormat_HWCN,
|
new_transpose_node = AddTransOpNodeToGraph(func_graph, new_transdata_node, kernel_select_, 0, kOpFormat_HWCN,
|
||||||
output_format, prim::kPrimTranspose->name(), false);
|
output_format, prim::kPrimTranspose->name(), false);
|
||||||
AnfAlgo::SetNodeAttr(kAttrPerm, MakeValue(std::vector<int>{3, 2, 0, 1}), new_transpose_node);
|
AnfAlgo::SetNodeAttr(kAttrPerm, MakeValue(std::vector<int>{3, 2, 0, 1}), new_transpose_node);
|
||||||
new_replace_node = new_transpose_node;
|
new_replace_node = new_transpose_node;
|
||||||
} else {
|
} else {
|
||||||
// trans default to hwcn
|
// trans default to hwcn
|
||||||
new_transpose_node = AddTransOpNodeToGraph(func_graph, node, kernel_select_, 0, false, input_format, kOpFormat_HWCN,
|
new_transpose_node = AddTransOpNodeToGraph(func_graph, node, kernel_select_, 0, input_format, kOpFormat_HWCN,
|
||||||
prim::kPrimTranspose->name(), true);
|
prim::kPrimTranspose->name(), true);
|
||||||
AnfAlgo::SetNodeAttr(kAttrPerm, MakeValue(std::vector<int>{2, 3, 1, 0}), new_transpose_node);
|
AnfAlgo::SetNodeAttr(kAttrPerm, MakeValue(std::vector<int>{2, 3, 1, 0}), new_transpose_node);
|
||||||
|
|
||||||
// trans hwcn to output_format
|
// trans hwcn to output_format
|
||||||
new_transdata_node = AddTransOpNodeToGraph(func_graph, new_transpose_node, kernel_select_, 0, false, kOpFormat_HWCN,
|
new_transdata_node = AddTransOpNodeToGraph(func_graph, new_transpose_node, kernel_select_, 0, kOpFormat_HWCN,
|
||||||
output_format, kTransDataOpName, false);
|
output_format, kTransDataOpName, false);
|
||||||
new_replace_node = new_transdata_node;
|
new_replace_node = new_transdata_node;
|
||||||
}
|
}
|
||||||
FuncGraphManagerPtr manager = func_graph->manager();
|
FuncGraphManagerPtr manager = func_graph->manager();
|
||||||
MS_EXCEPTION_IF_NULL(manager);
|
MS_EXCEPTION_IF_NULL(manager);
|
||||||
manager->AddFuncGraph(func_graph);
|
manager->AddFuncGraph(func_graph);
|
||||||
|
|
||||||
if (!manager->Replace(node, new_replace_node)) {
|
if (!manager->Replace(node, new_replace_node)) {
|
||||||
MS_LOG(EXCEPTION) << "manager replace node failed";
|
MS_LOG(EXCEPTION) << "Manager replace node failed";
|
||||||
}
|
}
|
||||||
MS_LOG(INFO) << "transdata node:" << cnode->DebugString() << "split success.";
|
MS_LOG(INFO) << "Transdata node:" << cnode->DebugString() << "split success.";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} // namespace opt
|
} // namespace opt
|
||||||
} // namespace mindspore
|
} // namespace mindspore
|
||||||
|
Loading…
Reference in new issue