Elffuss BraidHub

kikocis/ncnn public

master · 3.5 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 "pass_ncnn.h"
16
17namespace pnnx {
18
19namespace ncnn {
20
21class F_batch_norm : public GraphRewriterPass
22{
23public:
24 const char* match_pattern_graph() const
25 {
26 return R"PNNXIR(7767517
275 4
28pnnx.Input input 0 1 input
29pnnx.Attribute op_mean 0 1 running_mean @data
30pnnx.Attribute op_var 0 1 running_var @data
31F.batch_norm op_0 3 1 input running_mean running_var out weight=None bias=None eps=%eps
32pnnx.Output output 1 0 out
33)PNNXIR";
34 }
35
36 const char* type_str() const
37 {
38 return "BatchNorm";
39 }
40
41 const char* name_str() const
42 {
43 return "bn";
44 }
45
46 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
47 {
48 Attribute running_mean = captured_attrs.at("op_mean.data");
49 Attribute running_var = captured_attrs.at("op_var.data");
50
51 op->params["0"] = running_mean.shape[0];
52 op->params["1"] = captured_params.at("eps");
53
54 const int channels = running_mean.shape[0];
55
56 op->attrs["0"] = Attribute({channels}, std::vector<float>(channels, 1.f));
57 op->attrs["1"] = running_mean;
58 op->attrs["2"] = running_var;
59 op->attrs["3"] = Attribute({channels}, std::vector<float>(channels, 0.f));
60 }
61};
62
63REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_batch_norm, 20)
64
65class F_batch_norm_1 : public GraphRewriterPass
66{
67public:
68 const char* match_pattern_graph() const
69 {
70 return R"PNNXIR(7767517
717 6
72pnnx.Input input 0 1 input
73pnnx.Attribute op_mean 0 1 running_mean @data
74pnnx.Attribute op_var 0 1 running_var @data
75pnnx.Attribute op_weight 0 1 weight @data
76pnnx.Attribute op_bias 0 1 bias @data
77F.batch_norm op_0 5 1 input running_mean running_var weight bias out eps=%eps
78pnnx.Output output 1 0 out
79)PNNXIR";
80 }
81
82 const char* type_str() const
83 {
84 return "BatchNorm";
85 }
86
87 const char* name_str() const
88 {
89 return "bn";
90 }
91
92 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
93 {
94 Attribute running_mean = captured_attrs.at("op_mean.data");
95 Attribute running_var = captured_attrs.at("op_var.data");
96 Attribute weight = captured_attrs.at("op_weight.data");
97 Attribute bias = captured_attrs.at("op_bias.data");
98
99 op->params["0"] = running_mean.shape[0];
100 op->params["1"] = captured_params.at("eps");
101
102 op->attrs["0"] = weight;
103 op->attrs["1"] = running_mean;
104 op->attrs["2"] = running_var;
105 op->attrs["3"] = bias;
106 }
107};
108
109REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_batch_norm_1, 20)
110
111} // namespace ncnn
112
113} // namespace pnnx