!350 change tuple output to make tuple

Merge pull request !350 from lianliguang/chang-tuple-output-to-make-tuple
This commit is contained in:
mindspore-ci-bot 2020-04-17 19:22:41 +08:00 committed by Gitee
commit f69a668d98
15 changed files with 270 additions and 30 deletions

View File

@ -45,8 +45,8 @@ endif()
if (DEBUG_MODE)
set(CMAKE_BUILD_TYPE "Debug")
else()
add_compile_definitions(MEM_REUSE_DEBUG)
else()
set(CMAKE_BUILD_TYPE "Release")
endif()

View File

@ -205,8 +205,8 @@ std::vector<int> GetRuntimePaddingShape(const AnfNodePtr &node, size_t index) {
if (tensor == nullptr) {
MS_LOG(EXCEPTION) << " the node[ " << node->DebugString() << "]'s cannot convert ";
}
shape = tensor->shape();
(void)std::transform(shape.begin(), shape.end(), std::back_inserter(host_shape), IntToSize);
auto shape_temp = tensor->shape();
(void)std::transform(shape_temp.begin(), shape_temp.end(), std::back_inserter(host_shape), IntToSize);
if (host_shape.empty()) {
host_shape.push_back(1);
}

View File

@ -18,6 +18,7 @@
#include <set>
#include "common/trans.h"
#include "common/utils.h"
#include "pre_activate/common/helper.h"
#include "utils/utils.h"
#include "device/kernel_info.h"
#include "kernel/oplib/oplib.h"
@ -346,21 +347,6 @@ CNodePtr InsertCastForInput(const FuncGraphPtr &func_graph, const CNodePtr &cnod
return new_node;
}
AnfNodePtr CreatTupleGetItemNode(const FuncGraphPtr &func_graph, const AnfNodePtr &node, size_t output_idx) {
auto idx = NewValueNode(SizeToInt(output_idx));
MS_EXCEPTION_IF_NULL(idx);
auto imm = std::make_shared<Int32Imm>(SizeToInt(output_idx));
auto abstract_scalar = std::make_shared<abstract::AbstractScalar>(imm);
idx->set_abstract(abstract_scalar);
AnfNodePtr tuple_getitem = func_graph->NewCNode({NewValueNode(prim::kPrimTupleGetItem), node, idx});
MS_EXCEPTION_IF_NULL(tuple_getitem);
tuple_getitem->set_scope(node->scope());
std::vector<size_t> origin_shape = AnfAlgo::GetOutputInferShape(node, output_idx);
TypeId origin_type = AnfAlgo::GetOutputInferDataType(node, output_idx);
AnfAlgo::SetOutputInferTypeAndShape({origin_type}, {origin_shape}, tuple_getitem.get());
return tuple_getitem;
}
AnfNodePtr CreateMemcpyAsyncOp(const FuncGraphPtr &graph, const AnfNodePtr &node) {
MS_EXCEPTION_IF_NULL(graph);
MS_EXCEPTION_IF_NULL(node);

View File

@ -64,8 +64,6 @@ AnfNodePtr InsertTransOpForOutput(const FuncGraphPtr &func_graph, const AnfNodeP
CNodePtr InsertCastForInput(const FuncGraphPtr &func_graph, const CNodePtr &cnode);
AnfNodePtr CreatTupleGetItemNode(const FuncGraphPtr &func_graph, const AnfNodePtr &node, size_t output_idx);
AnfNodePtr CreateMemcpyAsyncOp(const FuncGraphPtr &graph, const AnfNodePtr &node);
} // namespace opt
} // namespace mindspore

View File

@ -17,6 +17,7 @@
#include <vector>
#include <memory>
#include "pre_activate/ascend/ascend_helper.h"
#include "pre_activate/common/helper.h"
#include "session/anf_runtime_algorithm.h"
namespace mindspore {

View File

@ -18,6 +18,7 @@
#include <string>
#include "pre_activate/common/optimizer.h"
#include "pre_activate/pass/convert_const_input_to_attr.h"
#include "pre_activate/pass/convert_tuple_output_to_maketuple.h"
#include "pre_activate/pass/convert_const_input_to_tensor_input.h"
#include "pre_activate/pass/convert_tuple_input_to_dynamic_input.h"
#include "utils/context/ms_context.h"
@ -42,6 +43,7 @@ void BackendCommonOptimization(const std::shared_ptr<session::KernelGraph> &kern
common_pm->AddPass(std::make_shared<ConvertConstInputToAttr>());
common_pm->AddPass(std::make_shared<ConvertConstInputToTensorInput>());
common_pm->AddPass(std::make_shared<ConvertTupleInputToDynamicInput>());
common_pm->AddPass(std::make_shared<ConvertTupleOutputToMaketuple>());
optimizer->AddPassManager(common_pm);
(void)optimizer->Optimize(kernel_graph);
kernel_graph->SetExecOrderByDefault();

View File

@ -407,5 +407,20 @@ bool IsUsedByOthers(const FuncGraphPtr &graph, const AnfNodePtr &node) {
}
return manager->node_users()[node].size() > 1;
}
AnfNodePtr CreatTupleGetItemNode(const FuncGraphPtr &func_graph, const AnfNodePtr &node, size_t output_idx) {
auto idx = NewValueNode(SizeToInt(output_idx));
MS_EXCEPTION_IF_NULL(idx);
auto imm = std::make_shared<Int32Imm>(SizeToInt(output_idx));
auto abstract_scalar = std::make_shared<abstract::AbstractScalar>(imm);
idx->set_abstract(abstract_scalar);
AnfNodePtr tuple_getitem = func_graph->NewCNode({NewValueNode(prim::kPrimTupleGetItem), node, idx});
MS_EXCEPTION_IF_NULL(tuple_getitem);
tuple_getitem->set_scope(node->scope());
std::vector<size_t> origin_shape = AnfAlgo::GetOutputInferShape(node, output_idx);
TypeId origin_type = AnfAlgo::GetOutputInferDataType(node, output_idx);
AnfAlgo::SetOutputInferTypeAndShape({origin_type}, {origin_shape}, tuple_getitem.get());
return tuple_getitem;
}
} // namespace opt
} // namespace mindspore

View File

@ -147,6 +147,8 @@ void HideNopNode(session::KernelGraph *const graph);
void RemoveNopNode(session::KernelGraph *const graph);
AnfNodePtr CreatTupleGetItemNode(const FuncGraphPtr &func_graph, const AnfNodePtr &node, size_t output_idx);
bool IsUsedByOthers(const FuncGraphPtr &graph, const AnfNodePtr &node);
} // namespace opt
} // namespace mindspore

View File

@ -19,6 +19,7 @@
#include <memory>
#include "session/anf_runtime_algorithm.h"
#include "pre_activate/common/helper.h"
#include "session/kernel_graph.h"
namespace mindspore {
@ -40,13 +41,7 @@ void ConvertTupleOuputToPlantInputs(const FuncGraphPtr &graph, const AnfNodePtr
convert_inputs = kernel_graph->SplitTupleValueNodeToNodeList(value_node);
} else {
for (size_t index = 0; index < output_size; ++index) {
auto idx = NewValueNode(SizeToInt(index));
MS_EXCEPTION_IF_NULL(idx);
auto imm = std::make_shared<Int32Imm>(SizeToInt(index));
auto abstract_scalar = std::make_shared<abstract::AbstractScalar>(imm);
idx->set_abstract(abstract_scalar);
auto tuple_get_item =
graph->NewCNode(std::vector<AnfNodePtr>{NewValueNode(prim::kPrimTupleGetItem), input_node, idx});
auto tuple_get_item = CreatTupleGetItemNode(graph, input_node, index);
AnfAlgo::SetOutputInferTypeAndShape({AnfAlgo::GetOutputInferDataType(input_node, index)},
{AnfAlgo::GetOutputInferShape(input_node, index)}, tuple_get_item.get());
convert_inputs.emplace_back(tuple_get_item);

View File

@ -0,0 +1,79 @@
/**
* 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 "pre_activate/pass/convert_tuple_output_to_maketuple.h"
#include <algorithm>
#include <memory>
#include "session/anf_runtime_algorithm.h"
#include "pre_activate/common/helper.h"
#include "session/kernel_graph.h"
namespace mindspore {
namespace opt {
namespace {
CNodePtr ConvertTupleInputToMakeTuple(const FuncGraphPtr &graph, const CNodePtr &cnode_ptr) {
MS_EXCEPTION_IF_NULL(cnode_ptr);
MS_EXCEPTION_IF_NULL(graph);
std::vector<AnfNodePtr> convert_inputs = {cnode_ptr->input(0)};
for (size_t index = 0; index < AnfAlgo::GetInputTensorNum(cnode_ptr); ++index) {
auto input_node = AnfAlgo::GetInputNode(cnode_ptr, index);
if (AnfAlgo::IsTupleOutput(input_node)) {
std::vector<TypeId> types;
std::vector<std::vector<size_t>> shapes;
std::vector<AnfNodePtr> make_tuple_inputs_list = {NewValueNode(prim::kPrimMakeTuple)};
for (size_t tuple_out_index = 0; tuple_out_index < AnfAlgo::GetOutputTensorNum(input_node); ++tuple_out_index) {
make_tuple_inputs_list.emplace_back(CreatTupleGetItemNode(graph, input_node, tuple_out_index));
types.push_back(AnfAlgo::GetOutputInferDataType(input_node, tuple_out_index));
shapes.emplace_back(AnfAlgo::GetOutputInferShape(input_node, tuple_out_index));
}
auto make_tuple = graph->NewCNode(make_tuple_inputs_list);
AnfAlgo::SetOutputInferTypeAndShape(types, shapes, make_tuple.get());
convert_inputs.emplace_back(make_tuple);
} else {
convert_inputs.push_back(input_node);
}
}
cnode_ptr->set_inputs(convert_inputs);
return cnode_ptr;
}
} // namespace
const BaseRef ConvertTupleOutputToMaketuple::DefinePattern() const {
VarPtr V = std::make_shared<Var>();
VarPtr Xs = std::make_shared<SeqVar>();
return VectorRef({V, Xs});
}
const AnfNodePtr ConvertTupleOutputToMaketuple::Process(const FuncGraphPtr &func_graph, const AnfNodePtr &node,
const EquivPtr &) const {
if (node == nullptr || !node->isa<CNode>()) {
return nullptr;
}
auto cnode = node->cast<CNodePtr>();
MS_EXCEPTION_IF_NULL(cnode);
if (AnfAlgo::GetCNodeName(cnode) == prim::kPrimTupleGetItem->name()) {
return nullptr;
}
if (std::any_of(cnode->inputs().begin() + 1, cnode->inputs().end(), [](const AnfNodePtr &node) {
return AnfAlgo::IsTupleOutput(node) && AnfAlgo::GetCNodeName(node) != prim::kPrimMakeTuple->name();
})) {
return ConvertTupleInputToMakeTuple(func_graph, cnode);
}
return nullptr;
}
} // namespace opt
} // namespace mindspore

View File

@ -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_CONVERT_TUPLE_OUTPUT_TO_MAKETUPLE_H
#define MINDSPORE_CONVERT_TUPLE_OUTPUT_TO_MAKETUPLE_H
#include <string>
#include <vector>
#include "ir/anf.h"
#include "pre_activate/common/optimizer.h"
namespace mindspore {
namespace opt {
class ConvertTupleOutputToMaketuple : public PatternProcessPass {
public:
explicit ConvertTupleOutputToMaketuple(bool multigraph = true)
: PatternProcessPass("convert_tuple_output_to_maketuple", multigraph) {}
~ConvertTupleOutputToMaketuple() override = default;
const BaseRef DefinePattern() const override;
const AnfNodePtr Process(const FuncGraphPtr &, const AnfNodePtr &, const EquivPtr &) const override;
};
} // namespace opt
} // namespace mindspore
#endif // MINDSPORE_CONVERT_TUPLE_OUTPUT_TO_MAKETUPLE_H

View File

@ -239,7 +239,7 @@ std::vector<AnfNodePtr> KernelGraph::SplitTupleValueNodeToNodeList(const ValueNo
AddValueNodeToGraph(new_value_node);
convert_inputs.emplace_back(new_value_node);
}
if (RemoveValueNodeFromGraph(value_node)) {
if (!RemoveValueNodeFromGraph(value_node)) {
MS_LOG(WARNING) << "failed to remove the value_node " << value_node->DebugString();
}
return convert_inputs;

View File

@ -0,0 +1,65 @@
/**
* 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 "common/backend_common_test.h"
#include "ir/anf.h"
#include "ir/meta_tensor.h"
#include "debug/anf_ir_dump.h"
#include "common/py_func_graph_fetcher.h"
#include "session/anf_runtime_algorithm.h"
#include "pre_activate/common/optimizer.h"
#include "pre_activate/common/pass_manager.h"
#include "pre_activate/pass/convert_tuple_output_to_maketuple.h"
#include "utils/utils.h"
namespace mindspore {
namespace opt {
class TestHWTupleOutputToMakeTuple : public BackendCommon {
public:
TestHWTupleOutputToMakeTuple()
: getPyFun_("gtest_input.pre_activate.convert_tuple_output_to_maketuple_test", true) {}
~TestHWTupleOutputToMakeTuple() override = default;
public:
UT::PyFuncGraphFetcher getPyFun_;
};
TEST_F(TestHWTupleOutputToMakeTuple, test_convert_tuple_output_to_maketuple) {
FuncGraphPtr g = getPyFun_.CallAndParseRet("test_convert_tuple_output_to_maketuple", "before");
ASSERT_TRUE(g != nullptr);
std::vector<int> shp_x{5, 2, 10};
std::vector<int> shp_h{1, 2, 2};
std::vector<int> shp_c{1, 2, 2};
std::vector<int> shp_w{112, 1, 1};
auto x_abstract = std::make_shared<abstract::AbstractTensor>(kFloat32, shp_x);
auto h_abstract = std::make_shared<abstract::AbstractTensor>(kFloat32, shp_h);
auto c_abstract = std::make_shared<abstract::AbstractTensor>(kFloat32, shp_c);
auto w_abstract = std::make_shared<abstract::AbstractTensor>(kFloat32, shp_w);
AbstractBasePtrList args_spec_list{x_abstract, h_abstract, c_abstract, w_abstract};
auto func_graph = GetKernelGraph(g, args_spec_list);
ASSERT_TRUE(func_graph != nullptr);
auto optimizer = std::make_shared<opt::GraphOptimizer>();
auto pm = std::make_shared<opt::PassManager>();
pm->AddPass(std::make_shared<opt::ConvertTupleOutputToMaketuple>());
optimizer->AddPassManager(pm);
optimizer->Optimize(func_graph);
FuncGraphPtr g_after = getPyFun_.CallAndParseRet("test_convert_tuple_output_to_maketuple", "after");
ASSERT_TRUE(g_after != nullptr);
EXPECT_TRUE(CheckEqualGraph(func_graph, g_after));
}
} // namespace opt
} // namespace mindspore

View File

@ -0,0 +1,54 @@
# 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.
# ============================================================================
from mindspore.ops import operations as P
from mindspore.ops import Primitive
import mindspore as ms
import mindspore.common.dtype as mstype
from mindspore.common.tensor import Tensor
import numpy as np
make_tuple = Primitive('make_tuple')
tuple_get_item = Primitive("tuple_getitem");
LSTM = P.LSTM(input_size=10,hidden_size=2,num_layers=1,has_bias=True,bidirectional=False,dropout=0.0)
add = P.TensorAdd()
class FnDict:
def __init__(self):
self.fnDict = {}
def __call__(self, fn):
self.fnDict[fn.__name__] = fn
def __getitem__(self, name):
return self.fnDict[name]
def test_convert_tuple_output_to_maketuple(tag):
fns = FnDict()
@fns
def before(x, h, c, w):
res = LSTM(x, h, c, w)
return res
@fns
def after(x, h, c, w):
res = LSTM(x, h, c, w)
res = make_tuple(
make_tuple(tuple_get_item(res, 0), tuple_get_item(res, 1), tuple_get_item(res, 2), tuple_get_item(res, 3),
tuple_get_item(res, 4)));
return res
return fns[tag]

View File

@ -49,7 +49,10 @@ def test_insert_memcpy_async_for_getnext(tag):
label = tuple_getitem(res, 1)
memcpy_async_data = memcpy_async(data)
memcpy_async_label = memcpy_async(label)
tuple = make_tuple(make_tuple(memcpy_async_data, memcpy_async_label))
return tuple
bind_tuple = make_tuple(memcpy_async_data, memcpy_async_label)
get_item0 = tuple_getitem(bind_tuple, 0)
get_item1 = tuple_getitem(bind_tuple, 1)
bind_tuple = make_tuple(make_tuple(get_item0, get_item1))
return bind_tuple
return fns[tag]