• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "frontend/optimizer/graph_transform.h"
18 #include <vector>
19 #include <algorithm>
20 #include <string>
21 #include "ir/graph_utils.h"
22 
23 namespace mindspore {
24 /* namespace to support opt */
25 namespace opt {
26 // check cnode input values, whether it is tuple input
CNodeHasTupleInput(const CNodePtr & cnode)27 bool CNodeHasTupleInput(const CNodePtr &cnode) {
28   auto &inputs = cnode->inputs();
29   for (size_t i = 1; i < inputs.size(); i++) {
30     if (IsValueNode<FuncGraph>(inputs[i])) {
31       continue;
32     }
33     if (IsValueNode<Primitive>(inputs[i])) {
34       // unexpected high order primitvie  as cnode input when transform graph
35       MS_LOG(WARNING) << "CheckTupleInput, got unexpected primitive as input" << cnode->DebugString();
36       return false;
37     }
38     auto abs = inputs[i]->abstract();
39     if (abs == nullptr) {
40       MS_LOG(WARNING) << "CheckTupleInput, got abstract nullptr for node:" << cnode->DebugString();
41       return false;
42     }
43     if (abs->isa<abstract::AbstractTuple>()) {
44       return true;
45     }
46   }
47   return false;
48 }
49 
FuncGraphHasTupleInput(const FuncGraphPtr & fg)50 bool FuncGraphHasTupleInput(const FuncGraphPtr &fg) {
51   auto &params = fg->parameters();
52   for (auto &param : params) {
53     if (param->abstract()->isa<abstract::AbstractTuple>()) {
54       return true;
55     }
56   }
57   return false;
58 }
59 
TransformTupleArgument(const FuncGraphPtr & fg,const AnfNodePtr & node,const abstract::AbstractTuplePtr & abs)60 std::vector<AnfNodePtr> TransformTupleArgument(const FuncGraphPtr &fg, const AnfNodePtr &node,
61                                                const abstract::AbstractTuplePtr &abs) {
62   auto &elements = abs->elements();
63   std::vector<AnfNodePtr> tuple_node_expanded;
64   for (size_t i = 0; i < elements.size(); i++) {
65     auto elem_node = fg->NewCNode({NewValueNode(prim::kPrimTupleGetItem), node, NewValueNode(SizeToLong(i))});
66     elem_node->set_abstract(elements[i]);
67     if (elements[i]->isa<abstract::AbstractTuple>()) {
68       auto nodes = TransformTupleArgument(fg, elem_node, elements[i]->cast<abstract::AbstractTuplePtr>());
69       tuple_node_expanded.insert(tuple_node_expanded.end(), nodes.begin(), nodes.end());
70     } else {
71       tuple_node_expanded.push_back(elem_node);
72     }
73   }
74   return tuple_node_expanded;
75 }
76 
TransformCallGraph(const FuncGraphPtr & trans_fg,const CNodePtr & cnode)77 AnfNodePtr TransformCallGraph(const FuncGraphPtr &trans_fg, const CNodePtr &cnode) {
78   auto &cinputs = cnode->inputs();
79   auto fg = cnode->func_graph();
80   std::vector<AnfNodePtr> inputs;
81   inputs.push_back(NewValueNode(trans_fg));
82   for (size_t i = 1; i < cinputs.size(); i++) {
83     auto abs = cinputs[i]->abstract();
84     if (abs == nullptr) {
85       MS_LOG(EXCEPTION) << "TransformCallGraph:Node abstract should not be nullptr" << cinputs[i]->DebugString();
86     }
87     if (abs->isa<abstract::AbstractTuple>()) {
88       auto nodes = TransformTupleArgument(fg, cinputs[i], abs->cast<abstract::AbstractTuplePtr>());
89       inputs.insert(inputs.end(), nodes.begin(), nodes.end());
90     } else {
91       inputs.push_back(cinputs[i]);
92     }
93   }
94   auto new_node = fg->NewCNode(inputs);
95   new_node->set_abstract(cnode->abstract());
96   return new_node;
97 }
98 
TransformPartial(const FuncGraphPtr & trans_fg,const CNodePtr & cnode)99 AnfNodePtr TransformPartial(const FuncGraphPtr &trans_fg, const CNodePtr &cnode) {
100   auto &cinputs = cnode->inputs();
101   auto fg = cnode->func_graph();
102   std::vector<AnfNodePtr> inputs;
103   inputs.push_back(NewValueNode(prim::kPrimPartial));
104   inputs.push_back(NewValueNode(trans_fg));
105   for (size_t i = 2; i < cinputs.size(); i++) {
106     auto abs = cinputs[i]->abstract();
107     if (abs == nullptr) {
108       MS_LOG(EXCEPTION) << "TransformPartial:Node abstract should not be nullptr" << cinputs[i]->DebugString();
109     }
110     if (abs->isa<abstract::AbstractTuple>()) {
111       auto nodes = TransformTupleArgument(fg, cinputs[i], abs->cast<abstract::AbstractTuplePtr>());
112       inputs.insert(inputs.end(), nodes.begin(), nodes.end());
113     } else {
114       inputs.push_back(cinputs[i]);
115     }
116   }
117   auto new_node = fg->NewCNode(inputs);
118   new_node->set_abstract(cnode->abstract());
119   return new_node;
120 }
121 
TransformSwitchCall(const AnfNodePtr & swtich_node,const CNodePtr & cnode)122 AnfNodePtr TransformSwitchCall(const AnfNodePtr &swtich_node, const CNodePtr &cnode) {
123   auto &cinputs = cnode->inputs();
124   auto fg = cnode->func_graph();
125   std::vector<AnfNodePtr> inputs;
126   inputs.push_back(swtich_node);
127   for (size_t i = 1; i < cinputs.size(); i++) {
128     auto abs = cinputs[i]->abstract();
129     if (abs == nullptr) {
130       MS_LOG(EXCEPTION) << "TransformSwitchCall:Node abstract should not be nullptr" << cinputs[i]->DebugString();
131     }
132     if (abs->isa<abstract::AbstractTuple>()) {
133       auto nodes = TransformTupleArgument(fg, cinputs[i], abs->cast<abstract::AbstractTuplePtr>());
134       inputs.insert(inputs.end(), nodes.begin(), nodes.end());
135     } else {
136       inputs.push_back(cinputs[i]);
137     }
138   }
139   auto new_node = fg->NewCNode(inputs);
140   new_node->set_abstract(cnode->abstract());
141   return new_node;
142 }
143 }  // namespace opt
144 }  // namespace mindspore
145