Elffuss BraidHub

kikocis/ncnn public

master · 4.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 "fuse_linear_batchnorm1d.h"
16
17#include "pass_level2.h"
18
19#include <math.h>
20#include <string.h>
21
22namespace pnnx {
23
24class fuse_linear_batchnorm1d_pass : public GraphRewriterPass
25{
26public:
27 const char* match_pattern_graph() const
28 {
29 return R"PNNXIR(7767517
304 3
31pnnx.Input input 0 1 input
32nn.Linear op_0 1 1 input a in_features=%in_features out_features=%out_features bias=%bias @weight @bias
33nn.BatchNorm1d op_1 1 1 a out num_features=%num_features eps=%eps affine=%affine @running_mean @running_var @weight @bias
34pnnx.Output output 1 0 out
35)PNNXIR";
36 }
37
38 const char* type_str() const
39 {
40 return "nn.Linear";
41 }
42
43 const char* name_str() const
44 {
45 return "linearbn1d";
46 }
47
48 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
49 {
50 op->params["in_features"] = captured_params.at("in_features");
51 op->params["out_features"] = captured_params.at("out_features");
52 op->params["bias"] = true;
53
54 // resolve merged linear weight and bias
55 int channels = captured_params.at("out_features").i;
56 float bn_eps = captured_params.at("eps").f;
57 bool has_bn_affine = captured_params.at("affine").b;
58 bool has_conv_bias = captured_params.at("bias").b;
59
60 auto bn_running_mean = captured_attrs.at("op_1.running_mean").get_float32_data();
61 auto bn_running_var = captured_attrs.at("op_1.running_var").get_float32_data();
62 auto bn_weight = has_bn_affine ? captured_attrs.at("op_1.weight").get_float32_data() : std::vector<float>();
63 auto bn_bias = has_bn_affine ? captured_attrs.at("op_1.bias").get_float32_data() : std::vector<float>();
64
65 // a = bias - slope * mean / sqrt(var + eps)
66 // b = slope / sqrt(var + eps)
67 // value = value * b + a
68
69 std::vector<float> a(channels);
70 std::vector<float> b(channels);
71 for (int i = 0; i < channels; i++)
72 {
73 double sqrt_var = sqrt(bn_running_var[i] + bn_eps);
74
75 if (has_bn_affine)
76 {
77 a[i] = (float)(bn_bias[i] - bn_weight[i] * bn_running_mean[i] / sqrt_var);
78 b[i] = (float)(bn_weight[i] / sqrt_var);
79 }
80 else
81 {
82 a[i] = (float)(-bn_running_mean[i] / sqrt_var);
83 b[i] = (float)(1.f / sqrt_var);
84 }
85 }
86
87 op->attrs["weight"] = captured_attrs.at("op_0.weight");
88
89 if (has_conv_bias)
90 {
91 op->attrs["bias"] = captured_attrs.at("op_0.bias");
92 }
93 else
94 {
95 // init bias as zero
96 op->attrs["bias"] = Attribute();
97 op->attrs["bias"].type = op->attrs["weight"].type;
98 op->attrs["bias"].shape = {channels};
99 op->attrs["bias"].set_float32_data(std::vector<float>(channels, 0.f));
100 }
101
102 auto conv_weight = op->attrs["weight"].get_float32_data();
103 auto conv_bias = op->attrs["bias"].get_float32_data();
104
105 const int weight_per_outch = op->params["in_features"].i;
106
107 for (int i = 0; i < channels; i++)
108 {
109 float* conv_weight_outch = (float*)conv_weight.data() + weight_per_outch * i;
110 for (int j = 0; j < weight_per_outch; j++)
111 {
112 conv_weight_outch[j] *= b[i];
113 }
114
115 conv_bias[i] = conv_bias[i] * b[i] + a[i];
116 }
117
118 op->attrs["weight"].set_float32_data(conv_weight);
119 op->attrs["bias"].set_float32_data(conv_bias);
120 }
121};
122
123void fuse_linear_batchnorm1d(Graph& graph)
124{
125 fuse_linear_batchnorm1d_pass a;
126 int opindex = 0;
127
128 pnnx_graph_rewrite(graph, &a, opindex);
129}
130
131} // namespace pnnx