Elffuss BraidHub

kikocis/ncnn public

master · 4.3 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 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_reshape_linear.h"
16#include "pass_ncnn.h"
17
18namespace pnnx {
19
20namespace ncnn {
21
22void insert_reshape_linear(Graph& graph)
23{
24 while (1)
25 {
26 bool matched = false;
27
28 for (size_t i = 0; i < graph.ops.size(); i++)
29 {
30 Operator* op = graph.ops[i];
31
32 if (op->type != "nn.Linear")
33 continue;
34
35 int input_rank = op->inputs[0]->shape.size();
36 if (input_rank == 0)
37 continue;
38
39 // nn.Linear 4d-2d-4d
40 // nn.Linear 5d-2d-5d
41 bool insert_reshape = false;
42 if (op->type == "nn.Linear" && (input_rank == 4 || input_rank == 5))
43 {
44 insert_reshape = true;
45 }
46
47 if (!insert_reshape)
48 continue;
49
50 fprintf(stderr, "insert_reshape_linear %d\n", input_rank);
51
52 matched = true;
53
54 Operand* linear_in = op->inputs[0];
55 Operand* linear_out = op->outputs[0];
56
57 const int batch_index = linear_in->params["__batch_index"].i;
58
59 Operator* reshape0 = graph.new_operator_before("Tensor.reshape", op->name + "_ncnnreshape0", op);
60 Operator* reshape1 = graph.new_operator_after("Tensor.reshape", op->name + "_ncnnreshape1", op);
61
62 Operand* reshape0_out = graph.new_operand(op->name + "_ncnnreshape0_out");
63 Operand* reshape1_in = graph.new_operand(op->name + "_ncnnreshape1_in");
64
65 reshape0->inputs.push_back(linear_in);
66 reshape0->outputs.push_back(reshape0_out);
67 reshape1->inputs.push_back(reshape1_in);
68 reshape1->outputs.push_back(linear_out);
69
70 for (size_t j = 0; j < linear_in->consumers.size(); j++)
71 {
72 if (linear_in->consumers[j] == op)
73 {
74 linear_in->consumers[j] = reshape0;
75 break;
76 }
77 }
78 linear_out->producer = reshape1;
79
80 op->inputs[0] = reshape0_out;
81 op->outputs[0] = reshape1_in;
82
83 reshape0_out->producer = reshape0;
84 reshape0_out->consumers.push_back(op);
85 reshape1_in->producer = op;
86 reshape1_in->consumers.push_back(reshape1);
87
88 reshape0_out->params["__batch_index"] = batch_index;
89 reshape1_in->params["__batch_index"] = batch_index;
90
91 int reshape_h = 1;
92 for (size_t j = 0; j < linear_in->shape.size() - 1; j++)
93 {
94 reshape_h *= linear_in->shape[j];
95 }
96
97 std::vector<int> reshape0_out_shape;
98 std::vector<int> reshape1_in_shape;
99 if (batch_index == 0 && batch_index != 233)
100 {
101 reshape0_out_shape = {1, reshape_h, linear_in->shape[input_rank - 1]};
102 reshape1_in_shape = {1, reshape_h, linear_out->shape[input_rank - 1]};
103 }
104 else
105 {
106 reshape0_out_shape = {reshape_h, linear_in->shape[input_rank - 1]};
107 reshape1_in_shape = {reshape_h, linear_out->shape[input_rank - 1]};
108 }
109 std::vector<int> reshape1_out_shape = linear_out->shape;
110
111 reshape0->params["shape"] = reshape0_out_shape;
112 reshape1->params["shape"] = reshape1_out_shape;
113 reshape0_out->type = linear_in->type;
114 reshape0_out->shape = reshape0_out_shape;
115 reshape1_in->type = linear_out->type;
116 reshape1_in->shape = reshape1_in_shape;
117
118 break;
119 }
120
121 if (!matched)
122 break;
123 }
124}
125
126} // namespace ncnn
127
128} // namespace pnnx