parent
c0c0b0985e
commit
00e4306518
@ -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
|
@ -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
|
@ -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
|
@ -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]
|
Loading…
Reference in new issue