Elffuss BraidHub

kikocis/ncnn public

master · 5.5 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "inline_containers.h"
16
17#include <map>
18#include <string>
19
20namespace pnnx {
21
22namespace onnx2pnnx {
23
24int inline_if_graph(onnx::ModelProto& model)
25{
26 int inlined = 0;
27
28 // collect initializers
29 std::unordered_map<std::string, int> initializers;
30 {
31 const onnx::GraphProto& graph = model.graph();
32 for (int i = 0; i < graph.initializer_size(); i++)
33 {
34 initializers.insert(std::make_pair(graph.initializer(i).name(), i));
35 }
36 }
37
38 onnx::GraphProto* graph = model.mutable_graph();
39
40 for (int i = 0; i < graph->node_size(); i++)
41 {
42 onnx::NodeProto* node = graph->mutable_node(i);
43
44 const std::string& op_type = node->op_type();
45
46 if (op_type != "If")
47 continue;
48
49 // find constant cond
50 if (initializers.find(node->input(0)) == initializers.end())
51 continue;
52
53 bool cond;
54 {
55 const onnx::TensorProto& tensor = graph->initializer(initializers.at(node->input(0)));
56 if (tensor.has_raw_data())
57 {
58 // assert tensor.raw_data().size() == 2
59 cond = ((uint16_t*)tensor.raw_data().data())[0] ? true : false;
60 }
61 else
62 {
63 // assert tensor.int32_data().size() == 1
64 cond = tensor.int32_data().at(0) ? true : false;
65 }
66 }
67
68 onnx::GraphProto* sg = 0;
69 for (int j = 0; j < node->attribute_size(); j++)
70 {
71 if (cond == true && node->attribute(j).name() == "then_branch")
72 {
73 sg = node->mutable_attribute(j)->mutable_g();
74 break;
75 }
76 if (cond == false && node->attribute(j).name() == "else_branch")
77 {
78 sg = node->mutable_attribute(j)->mutable_g();
79 break;
80 }
81 }
82
83 if (!sg)
84 continue;
85
86 // build subgraph output name remap
87 std::map<std::string, std::string> output_remap;
88 {
89 for (int j = 0; j < node->output_size(); j++)
90 {
91 const std::string& node_output = node->output(j);
92 const std::string& sg_output = sg->output(j).name();
93
94 output_remap[sg_output] = node_output;
95 }
96 }
97
98 // append subgraph nodes to graph
99 {
100 std::map<std::string, std::string> input_remap;
101
102 graph->mutable_node()->Reserve(graph->node_size() + sg->node_size());
103 for (int j = 0; j < sg->node_size(); j++)
104 {
105 onnx::NodeProto* inlined_node = graph->add_node();
106 inlined_node->CopyFrom(sg->node(j));
107
108 // prefix with caller node name
109 inlined_node->set_name(node->name() + "/" + inlined_node->name());
110
111 // reset input output
112 for (int j = 0; j < inlined_node->input_size(); j++)
113 {
114 const std::string& node_input = inlined_node->input(j);
115 if (input_remap.find(node_input) != input_remap.end())
116 {
117 std::string new_name = input_remap.at(node_input);
118 inlined_node->set_input(j, new_name);
119 }
120 }
121 for (int j = 0; j < inlined_node->output_size(); j++)
122 {
123 const std::string& node_output = inlined_node->output(j);
124 if (output_remap.find(node_output) != output_remap.end())
125 {
126 inlined_node->set_output(j, output_remap.at(node_output));
127 }
128 else
129 {
130 std::string new_name = node->name() + "/" + node_output;
131 input_remap[node_output] = new_name;
132 inlined_node->set_output(j, new_name);
133 }
134 }
135 }
136 }
137
138 // swap inlined subgraph nodes to caller
139 {
140 // ..... cni ....... 0 1 2 3 4
141 const int graph_node_size = graph->node_size();
142 for (int j = 0; j < sg->node_size(); j++)
143 {
144 for (int k = graph_node_size - 1; k > i; k--)
145 {
146 graph->mutable_node()->SwapElements(k, k - 1);
147 }
148 }
149
150 // ..... 0 1 2 3 4 cni .......
151 for (int j = i + sg->node_size(); j < graph_node_size - 1; j++)
152 {
153 graph->mutable_node()->SwapElements(j, j + 1);
154 }
155
156 // ..... 0 1 2 3 4 ....... cni
157 graph->mutable_node()->RemoveLast();
158 }
159
160 inlined = 1;
161
162 // inlined node may be another subgraph
163 i -= 1;
164 }
165
166 return inlined;
167}
168
169} // namespace onnx2pnnx
170
171} // namespace pnnx