Elffuss BraidHub

kikocis/ncnn public

master · 2.4 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 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 "insert_split.h"
16#include "pass_ncnn.h"
17
18namespace pnnx {
19
20namespace ncnn {
21
22void insert_split(Graph& graph)
23{
24 int opindex = 0;
25 while (1)
26 {
27 bool matched = false;
28
29 for (size_t i = 0; i < graph.ops.size(); i++)
30 {
31 Operator* op = graph.ops[i];
32 for (auto& x : op->outputs)
33 {
34 if (x->consumers.size() <= 1)
35 continue;
36
37 matched = true;
38
39 // insert split
40 Operator* split = graph.new_operator_after("Split", std::string("splitncnn_") + std::to_string(opindex++), op);
41
42 split->inputs.push_back(x);
43
44 for (size_t j = 0; j < x->consumers.size(); j++)
45 {
46 Operator* op2 = x->consumers[j];
47
48 Operand* operand = graph.new_operand(x->name + "_" + std::to_string(j));
49 operand->producer = split;
50 operand->consumers.push_back(op2);
51
52 operand->params["__batch_index"] = x->params["__batch_index"];
53
54 split->outputs.push_back(operand);
55
56 for (size_t k = 0; k < op2->inputs.size(); k++)
57 {
58 if (op2->inputs[k] == x)
59 {
60 op2->inputs[k] = operand;
61 break;
62 }
63 }
64 }
65
66 x->consumers.clear();
67 x->consumers.push_back(split);
68
69 break;
70 }
71
72 if (matched)
73 break;
74 }
75
76 if (!matched)
77 break;
78 }
79}
80
81} // namespace ncnn
82
83} // namespace pnnx