diff --git a/cmake/options.cmake b/cmake/options.cmake index 3677418a98e..3e03ed33395 100644 --- a/cmake/options.cmake +++ b/cmake/options.cmake @@ -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() diff --git a/mindspore/ccsrc/common/trans.cc b/mindspore/ccsrc/common/trans.cc index a2b9f7ef241..5eb21f09bd1 100644 --- a/mindspore/ccsrc/common/trans.cc +++ b/mindspore/ccsrc/common/trans.cc @@ -205,8 +205,8 @@ std::vector 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); } diff --git a/mindspore/ccsrc/pre_activate/ascend/ascend_helper.cc b/mindspore/ccsrc/pre_activate/ascend/ascend_helper.cc index 745ed4460fa..fbb3e345dfa 100644 --- a/mindspore/ccsrc/pre_activate/ascend/ascend_helper.cc +++ b/mindspore/ccsrc/pre_activate/ascend/ascend_helper.cc @@ -18,6 +18,7 @@ #include #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(SizeToInt(output_idx)); - auto abstract_scalar = std::make_shared(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 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); diff --git a/mindspore/ccsrc/pre_activate/ascend/ascend_helper.h b/mindspore/ccsrc/pre_activate/ascend/ascend_helper.h index 7f5e86d726d..a8fd7dc5144 100644 --- a/mindspore/ccsrc/pre_activate/ascend/ascend_helper.h +++ b/mindspore/ccsrc/pre_activate/ascend/ascend_helper.h @@ -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 diff --git a/mindspore/ccsrc/pre_activate/ascend/enhancer/insert_memcpy_async_for_getnext.cc b/mindspore/ccsrc/pre_activate/ascend/enhancer/insert_memcpy_async_for_getnext.cc index 5065bab0f2d..fb8b19047cb 100644 --- a/mindspore/ccsrc/pre_activate/ascend/enhancer/insert_memcpy_async_for_getnext.cc +++ b/mindspore/ccsrc/pre_activate/ascend/enhancer/insert_memcpy_async_for_getnext.cc @@ -17,6 +17,7 @@ #include #include #include "pre_activate/ascend/ascend_helper.h" +#include "pre_activate/common/helper.h" #include "session/anf_runtime_algorithm.h" namespace mindspore { diff --git a/mindspore/ccsrc/pre_activate/common/common_backend_optimization.cc b/mindspore/ccsrc/pre_activate/common/common_backend_optimization.cc index c3fd292aa9f..f622f2f06f0 100644 --- a/mindspore/ccsrc/pre_activate/common/common_backend_optimization.cc +++ b/mindspore/ccsrc/pre_activate/common/common_backend_optimization.cc @@ -18,6 +18,7 @@ #include #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 &kern common_pm->AddPass(std::make_shared()); common_pm->AddPass(std::make_shared()); common_pm->AddPass(std::make_shared()); + common_pm->AddPass(std::make_shared()); optimizer->AddPassManager(common_pm); (void)optimizer->Optimize(kernel_graph); kernel_graph->SetExecOrderByDefault(); diff --git a/mindspore/ccsrc/pre_activate/common/helper.cc b/mindspore/ccsrc/pre_activate/common/helper.cc index 3f157d6a647..de452392683 100644 --- a/mindspore/ccsrc/pre_activate/common/helper.cc +++ b/mindspore/ccsrc/pre_activate/common/helper.cc @@ -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(SizeToInt(output_idx)); + auto abstract_scalar = std::make_shared(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 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 diff --git a/mindspore/ccsrc/pre_activate/common/helper.h b/mindspore/ccsrc/pre_activate/common/helper.h index 8d174a1ad03..2b95bec824d 100644 --- a/mindspore/ccsrc/pre_activate/common/helper.h +++ b/mindspore/ccsrc/pre_activate/common/helper.h @@ -146,6 +146,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 diff --git a/mindspore/ccsrc/pre_activate/pass/convert_tuple_input_to_dynamic_input.cc b/mindspore/ccsrc/pre_activate/pass/convert_tuple_input_to_dynamic_input.cc index 92579111f69..ccc4fd52650 100644 --- a/mindspore/ccsrc/pre_activate/pass/convert_tuple_input_to_dynamic_input.cc +++ b/mindspore/ccsrc/pre_activate/pass/convert_tuple_input_to_dynamic_input.cc @@ -19,6 +19,7 @@ #include #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(SizeToInt(index)); - auto abstract_scalar = std::make_shared(imm); - idx->set_abstract(abstract_scalar); - auto tuple_get_item = - graph->NewCNode(std::vector{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); diff --git a/mindspore/ccsrc/pre_activate/pass/convert_tuple_output_to_maketuple.cc b/mindspore/ccsrc/pre_activate/pass/convert_tuple_output_to_maketuple.cc new file mode 100644 index 00000000000..3f283e5d24e --- /dev/null +++ b/mindspore/ccsrc/pre_activate/pass/convert_tuple_output_to_maketuple.cc @@ -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 +#include + +#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 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 types; + std::vector> shapes; + std::vector 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(); + VarPtr Xs = std::make_shared(); + return VectorRef({V, Xs}); +} + +const AnfNodePtr ConvertTupleOutputToMaketuple::Process(const FuncGraphPtr &func_graph, const AnfNodePtr &node, + const EquivPtr &) const { + if (node == nullptr || !node->isa()) { + return nullptr; + } + auto cnode = node->cast(); + 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 diff --git a/mindspore/ccsrc/pre_activate/pass/convert_tuple_output_to_maketuple.h b/mindspore/ccsrc/pre_activate/pass/convert_tuple_output_to_maketuple.h new file mode 100644 index 00000000000..a16ffaf674c --- /dev/null +++ b/mindspore/ccsrc/pre_activate/pass/convert_tuple_output_to_maketuple.h @@ -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 +#include + +#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 diff --git a/mindspore/ccsrc/session/kernel_graph.cc b/mindspore/ccsrc/session/kernel_graph.cc index bbcc04e14b3..3c647bf21d9 100755 --- a/mindspore/ccsrc/session/kernel_graph.cc +++ b/mindspore/ccsrc/session/kernel_graph.cc @@ -239,7 +239,7 @@ std::vector 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; diff --git a/tests/ut/cpp/pre_activate/pass/convert_tuple_output_to_maketuple_test.cc b/tests/ut/cpp/pre_activate/pass/convert_tuple_output_to_maketuple_test.cc new file mode 100644 index 00000000000..da01a74d760 --- /dev/null +++ b/tests/ut/cpp/pre_activate/pass/convert_tuple_output_to_maketuple_test.cc @@ -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 shp_x{5, 2, 10}; + std::vector shp_h{1, 2, 2}; + std::vector shp_c{1, 2, 2}; + std::vector shp_w{112, 1, 1}; + auto x_abstract = std::make_shared(kFloat32, shp_x); + auto h_abstract = std::make_shared(kFloat32, shp_h); + auto c_abstract = std::make_shared(kFloat32, shp_c); + auto w_abstract = std::make_shared(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(); + auto pm = std::make_shared(); + pm->AddPass(std::make_shared()); + 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 diff --git a/tests/ut/cpp/python_input/gtest_input/pre_activate/convert_tuple_output_to_maketuple_test.py b/tests/ut/cpp/python_input/gtest_input/pre_activate/convert_tuple_output_to_maketuple_test.py new file mode 100644 index 00000000000..961f7b62329 --- /dev/null +++ b/tests/ut/cpp/python_input/gtest_input/pre_activate/convert_tuple_output_to_maketuple_test.py @@ -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] diff --git a/tests/ut/cpp/python_input/gtest_input/pre_activate/insert_memcpy_async_for_getnext.py b/tests/ut/cpp/python_input/gtest_input/pre_activate/insert_memcpy_async_for_getnext.py index 902fd636dea..f0320fef79a 100644 --- a/tests/ut/cpp/python_input/gtest_input/pre_activate/insert_memcpy_async_for_getnext.py +++ b/tests/ut/cpp/python_input/gtest_input/pre_activate/insert_memcpy_async_for_getnext.py @@ -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]