1 /**
2 * Copyright 2020 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 <fstream>
18 #include <sstream>
19 #include <utility>
20 #include <deque>
21 #include "src/common/graph_util.h"
22 #include "src/common/utils.h"
23 #include "src/common/log_adapter.h"
24 #include "src/common/version_manager.h"
25 #include "include/errorcode.h"
26 #include "nnacl/op_base.h"
27
28 namespace mindspore {
29 namespace lite {
GetGraphInputNodes(const lite::Model * model)30 std::vector<size_t> GetGraphInputNodes(const lite::Model *model) {
31 MS_ASSERT(model != nullptr);
32 MS_ASSERT(!(model->graph_.sub_graphs_.empty()));
33 std::vector<size_t> ret;
34 for (auto graph_in_index : model->graph_.input_indices_) {
35 auto node_size = model->graph_.all_nodes_.size();
36 for (size_t j = 0; j < node_size; ++j) {
37 auto node = model->graph_.all_nodes_[j];
38 MS_ASSERT(node != nullptr);
39 if (std::any_of(node->input_indices_.begin(), node->input_indices_.end(),
40 [&](const uint32_t &node_in_index) { return node_in_index == graph_in_index; })) {
41 if (!IsContain<size_t>(ret, j)) {
42 ret.emplace_back(j);
43 }
44 }
45 }
46 }
47 return ret;
48 }
49
GetGraphOutputNodes(const lite::Model * model)50 std::vector<size_t> GetGraphOutputNodes(const lite::Model *model) {
51 MS_ASSERT(model != nullptr);
52 std::vector<size_t> ret;
53 for (auto graph_out_index : model->graph_.output_indices_) {
54 auto node_size = model->graph_.all_nodes_.size();
55 for (size_t j = 0; j < node_size; ++j) {
56 auto node = model->graph_.all_nodes_[j];
57 MS_ASSERT(node != nullptr);
58 if (std::any_of(node->output_indices_.begin(), node->output_indices_.end(),
59 [&](const uint32_t &node_out_index) { return node_out_index == graph_out_index; })) {
60 if (!IsContain<size_t>(ret, j)) {
61 ret.emplace_back(j);
62 }
63 }
64 }
65 }
66 return ret;
67 }
68
GetLinkedPostNodeIdx(const lite::Model * model,size_t tensor_idx)69 std::vector<size_t> GetLinkedPostNodeIdx(const lite::Model *model, size_t tensor_idx) {
70 MS_ASSERT(model != nullptr);
71 std::vector<size_t> post_node_idxes;
72 auto nodes_size = model->graph_.all_nodes_.size();
73 for (size_t i = 0; i < nodes_size; ++i) {
74 auto node = model->graph_.all_nodes_[i];
75 if (node == nullptr) {
76 continue;
77 }
78
79 auto is_contain = std::any_of(node->input_indices_.begin(), node->input_indices_.end(),
80 [&](const uint32_t &node_input_idx) { return node_input_idx == tensor_idx; });
81 if (is_contain) {
82 post_node_idxes.emplace_back(i);
83 }
84 }
85 return post_node_idxes;
86 }
87
88 // only support op_type from current schema
IsPackedOp(int op_type)89 bool IsPackedOp(int op_type) {
90 static const std::vector<int> packed_ops = {
91 schema::PrimitiveType_Conv2DFusion, schema::PrimitiveType_Conv2dTransposeFusion,
92 schema::PrimitiveType_FullConnection, schema::PrimitiveType_MatMulFusion, PrimType::PrimType_Inner_CustomGru};
93 return IsContain(packed_ops, op_type);
94 }
95
IsShareConstOp(int op_type)96 bool IsShareConstOp(int op_type) {
97 static const std::vector<int> packed_ops = {schema::PrimitiveType_Gather};
98 return IsContain(packed_ops, op_type);
99 }
100 } // namespace lite
101 } // namespace mindspore
102