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) 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 "fuse_binaryop_eltwise.h"
16
17#include "pass_level2.h"
18
19#include <float.h>
20
21namespace pnnx {
22
23namespace ncnn {
24
25class fuse_binaryop_eltwise_pass : public GraphRewriterPass
26{
27public:
28 const char* match_pattern_graph() const
29 {
30 return R"PNNXIR(7767517
316 5
32pnnx.Input input_a 0 1 a
33pnnx.Input input_b 0 1 b
34BinaryOp op_0 1 1 a a2 0=2 1=1 2=%c0
35BinaryOp op_1 1 1 b b2 0=2 1=1 2=%c1
36BinaryOp op_2 2 1 a2 b2 out 0=0
37pnnx.Output output 1 0 out
38)PNNXIR";
39 }
40
41 const char* type_str() const
42 {
43 return "Eltwise";
44 }
45
46 const char* name_str() const
47 {
48 return "weighted_sum";
49 }
50
51 bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& /*captured_params*/, const std::map<std::string, Attribute>& /*captured_attrs*/) const
52 {
53 auto a_shape = matched_operators.at("op_0")->inputs[0]->shape;
54 auto b_shape = matched_operators.at("op_1")->inputs[0]->shape;
55 return !a_shape.empty() && a_shape == b_shape;
56 }
57
58 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const
59 {
60 float c0 = 1.f;
61 float c1 = 1.f;
62
63 if (captured_params.at("c0").type == 2)
64 c0 = (float)captured_params.at("c0").i;
65 if (captured_params.at("c0").type == 3)
66 c0 = captured_params.at("c0").f;
67
68 if (captured_params.at("c1").type == 2)
69 c1 = (float)captured_params.at("c1").i;
70 if (captured_params.at("c1").type == 3)
71 c1 = captured_params.at("c1").f;
72
73 op->params["0"] = 1;
74 op->params["1"] = std::vector<float>{c0, c1};
75 }
76};
77
78class fuse_binaryop_eltwise_pass_1 : public GraphRewriterPass
79{
80public:
81 const char* match_pattern_graph() const
82 {
83 return R"PNNXIR(7767517
845 4
85pnnx.Input input_a 0 1 a
86pnnx.Input input_b 0 1 b
87BinaryOp op_0 1 1 a a2 0=2 1=1 2=%c0
88BinaryOp op_1 2 1 a2 b out 0=0
89pnnx.Output output 1 0 out
90)PNNXIR";
91 }
92
93 const char* type_str() const
94 {
95 return "Eltwise";
96 }
97
98 const char* name_str() const
99 {
100 return "weighted_sum";
101 }
102
103 bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& /*captured_params*/, const std::map<std::string, Attribute>& /*captured_attrs*/) const
104 {
105 auto a_shape = matched_operators.at("op_0")->inputs[0]->shape;
106 auto b_shape = matched_operators.at("op_1")->inputs[1]->shape;
107 return !a_shape.empty() && a_shape == b_shape;
108 }
109
110 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const
111 {
112 float c0 = 1.f;
113 float c1 = 1.f;
114
115 if (captured_params.at("c0").type == 2)
116 c0 = (float)captured_params.at("c0").i;
117 if (captured_params.at("c0").type == 3)
118 c0 = captured_params.at("c0").f;
119
120 op->params["0"] = 1;
121 op->params["1"] = std::vector<float>{c0, c1};
122 }
123};
124
125class fuse_binaryop_eltwise_pass_2 : public GraphRewriterPass
126{
127public:
128 const char* match_pattern_graph() const
129 {
130 return R"PNNXIR(7767517
1316 5
132pnnx.Input input_a 0 1 a
133pnnx.Input input_b 0 1 b
134BinaryOp op_0 1 1 b b2 0=2 1=1 2=%c1
135BinaryOp op_1 2 1 a b2 out 0=0
136pnnx.Output output 1 0 out
137)PNNXIR";
138 }
139
140 const char* type_str() const
141 {
142 return "Eltwise";
143 }
144
145 const char* name_str() const
146 {
147 return "weighted_sum";
148 }
149
150 bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& /*captured_params*/, const std::map<std::string, Attribute>& /*captured_attrs*/) const
151 {
152 auto a_shape = matched_operators.at("op_1")->inputs[0]->shape;
153 auto b_shape = matched_operators.at("op_0")->inputs[0]->shape;
154 return !a_shape.empty() && a_shape == b_shape;
155 }
156
157 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const
158 {
159 float c0 = 1.f;
160 float c1 = 1.f;
161
162 if (captured_params.at("c1").type == 2)
163 c1 = (float)captured_params.at("c1").i;
164 if (captured_params.at("c1").type == 3)
165 c1 = captured_params.at("c1").f;
166
167 op->params["0"] = 1;
168 op->params["1"] = std::vector<float>{c0, c1};
169 }
170};
171
172void fuse_binaryop_eltwise(Graph& graph)
173{
174 fuse_binaryop_eltwise_pass a;
175 fuse_binaryop_eltwise_pass_1 b;
176 fuse_binaryop_eltwise_pass_2 c;
177 int opindex = 0;
178
179 pnnx_graph_rewrite(graph, &a, opindex);
180 pnnx_graph_rewrite(graph, &b, opindex);
181 pnnx_graph_rewrite(graph, &c, opindex);
182}
183
184} // namespace ncnn
185
186} // namespace pnnx