Elffuss BraidHub

kikocis/ncnn public

master · 5.7 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2023 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 "eliminate_reshape_shape_expression.h"
16
17#include <iostream>
18#include <sstream>
19#include <algorithm>
20#include <stack>
21#include <vector>
22#include <string>
23
24namespace pnnx {
25
26static bool token_is_interger_literal(const std::string& t)
27{
28 std::istringstream iss(t);
29 int f;
30 iss >> std::noskipws >> f;
31 return iss.eof() && !iss.fail();
32}
33
34static void build_shape(const std::string& expr, std::vector<int>& shape, std::vector<std::string>& expr_tokens)
35{
36 std::string listexpr = expr.substr(1, expr.size() - 2);
37
38 std::string t;
39 std::string et;
40 int level = 0;
41 for (size_t i = 0; i < listexpr.size(); i++)
42 {
43 char ch = listexpr[i];
44
45 if (ch == '(' || ch == '[')
46 {
47 level += 1;
48 t = "-1";
49 et += ch;
50 }
51 else if (ch == ')' || ch == ']')
52 {
53 level -= 1;
54 t = "-1";
55 et += ch;
56 }
57 else if (level == 0 && ch == ',')
58 {
59 int dimsize = token_is_interger_literal(t) ? std::stoi(t) : -1;
60 shape.push_back(dimsize);
61 expr_tokens.push_back(et);
62 t.clear();
63 et.clear();
64 }
65 else
66 {
67 t += ch;
68 et += ch;
69 }
70 }
71
72 if (level == 0 && !t.empty())
73 {
74 int dimsize = token_is_interger_literal(t) ? std::stoi(t) : -1;
75 shape.push_back(dimsize);
76 }
77
78 if (level == 0 && !et.empty())
79 {
80 expr_tokens.push_back(et);
81 }
82}
83
84static std::string build_expr(const std::vector<std::string>& expr_tokens)
85{
86 std::string expr;
87
88 expr += '[';
89 for (int i = 0; i < (int)expr_tokens.size(); i++)
90 {
91 expr += expr_tokens[i];
92 if (i != (int)expr_tokens.size() - 1)
93 expr += ',';
94 }
95 expr += ']';
96
97 return expr;
98}
99
100void eliminate_reshape_shape_expression(Graph& graph)
101{
102 while (1)
103 {
104 bool matched = false;
105
106 for (size_t i = 0; i < graph.ops.size(); i++)
107 {
108 Operator* op = graph.ops[i];
109
110 if (op->type != "Tensor.view" && op->type != "Tensor.reshape")
111 continue;
112
113 if (op->inputs.size() != 2)
114 continue;
115
116 Operator* op_expr = op->inputs[1]->producer;
117 if (op_expr->type != "pnnx.Expression")
118 continue;
119
120 std::string expr = op_expr->params.at("expr").s;
121 if (expr.empty() || expr[0] != '[')
122 continue;
123
124 std::vector<int> outshape = op->outputs[0]->shape;
125 if (outshape.empty())
126 continue;
127
128 std::vector<int> shape;
129 std::vector<std::string> expr_tokens;
130 build_shape(expr, shape, expr_tokens);
131
132 // replace -1 with static dim-size
133 for (size_t j = 0; j < outshape.size(); j++)
134 {
135 if (outshape[j] != -1)
136 {
137 shape[j] = outshape[j];
138 expr_tokens[j] = std::to_string(outshape[j]);
139 }
140 }
141
142 // if only one dynamic dim-size, drop expression
143 int dynamic_dim_count = 0;
144 for (size_t j = 0; j < shape.size(); j++)
145 {
146 if (shape[j] == -1)
147 {
148 dynamic_dim_count += 1;
149 }
150 }
151
152 if (dynamic_dim_count > 1)
153 {
154 op_expr->params["expr"] = build_expr(expr_tokens);
155 continue;
156 }
157
158 matched = true;
159
160 op->params["shape"] = shape;
161
162 op->inputs.resize(1);
163 op_expr->outputs[0]->remove_consumer(op);
164
165 if (op_expr->outputs[0]->consumers.size() == 0)
166 {
167 // remove expression operator
168 for (auto x : op_expr->inputs)
169 {
170 x->remove_consumer(op_expr);
171 }
172
173 Operand* op_expr_out = op_expr->outputs[0];
174
175 graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), op_expr_out));
176 delete op_expr_out;
177
178 op_expr->inputs.clear();
179 op_expr->outputs.clear();
180
181 graph.ops.erase(std::find(graph.ops.begin(), graph.ops.end(), op_expr));
182 delete op_expr;
183 }
184
185 break;
186 }
187
188 if (!matched)
189 break;
190 }
191
192 for (size_t i = 0; i < graph.ops.size(); i++)
193 {
194 Operator* op = graph.ops[i];
195
196 if (op->type != "Tensor.view" && op->type != "Tensor.reshape")
197 continue;
198
199 if (op->inputs.size() != 1)
200 continue;
201
202 std::vector<int> outshape = op->outputs[0]->shape;
203 if (outshape.empty())
204 continue;
205
206 std::vector<int> shape = op->params.at("shape").ai;
207
208 // replace -1 with static dim-size
209 for (size_t j = 0; j < outshape.size(); j++)
210 {
211 if (outshape[j] != -1)
212 {
213 shape[j] = outshape[j];
214 }
215 }
216
217 op->params["shape"] = shape;
218 }
219}
220
221} // namespace pnnx