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