Elffuss BraidHub

kikocis/ncnn public

master · 2.3 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 "attribute_unpooling.h"
16
17#include <algorithm>
18
19namespace pnnx {
20
21void attribute_unpooling(Graph& graph)
22{
23 while (1)
24 {
25 bool matched = false;
26
27 for (size_t i = 0; i < graph.ops.size(); i++)
28 {
29 Operator* op = graph.ops[i];
30
31 if (op->type != "pnnx.Attribute")
32 continue;
33
34 Operand* attr = op->outputs[0];
35
36 if (attr->consumers.size() < 2)
37 continue;
38
39 // multiple modules share same weight
40 matched = true;
41
42 for (int i = 1; i < (int)attr->consumers.size(); i++)
43 {
44 Operator* x = attr->consumers[i];
45
46 Operator* op2 = graph.new_operator_after("pnnx.Attribute", op->name + "_" + std::to_string(i), op);
47
48 op2->inputnames = op->inputnames;
49 op2->params = op->params;
50 op2->attrs = op->attrs;
51
52 Operand* attr2 = graph.new_operand(attr->name + "_" + std::to_string(i));
53
54 attr2->type = attr->type;
55 attr2->shape = attr->shape;
56 attr2->params = attr->params;
57
58 op2->outputs.push_back(attr2);
59
60 attr2->producer = op2;
61 attr2->consumers.push_back(x);
62
63 for (size_t j = 0; j < x->inputs.size(); j++)
64 {
65 if (x->inputs[j] == attr)
66 x->inputs[j] = attr2;
67 }
68 }
69
70 attr->consumers.resize(1);
71
72 break;
73 }
74
75 if (!matched)
76 break;
77 }
78}
79
80} // namespace pnnx