| 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_static_batchnorm.h" |
| 16 | |
| 17 | #include "pass_level2.h" |
| 18 | |
| 19 | #include <math.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | namespace pnnx { |
| 23 | |
| 24 | class fuse_static_Fbatchnorm_pass_1d : public GraphRewriterPass |
| 25 | { |
| 26 | public: |
| 27 | const char* match_pattern_graph() const |
| 28 | { |
| 29 | return R"PNNXIR(7767517 |
| 30 | 5 4 |
| 31 | pnnx.Input input 0 1 input |
| 32 | pnnx.Attribute op_mean 0 1 running_mean @data=(%num_features)f32 |
| 33 | pnnx.Attribute op_var 0 1 running_var @data=(%num_features)f32 |
| 34 | F.batchnorm op_0 3 1 input running_mean running_var out weight=None bias=None eps=%eps |
| 35 | pnnx.Output output 1 0 out |
| 36 | )PNNXIR"; |
| 37 | } |
| 38 | |
| 39 | const char* replace_pattern_graph() const |
| 40 | { |
| 41 | return R"PNNXIR(7767517 |
| 42 | 3 2 |
| 43 | pnnx.Input input 0 1 input |
| 44 | nn.BatchNorm1d batchnorm 1 1 input out num_features=%num_features eps=%eps affine=False @running_mean=%op_mean.data @running_var=%op_var.data |
| 45 | pnnx.Output output 1 0 out |
| 46 | )PNNXIR"; |
| 47 | } |
| 48 | |
| 49 | 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 |
| 50 | { |
| 51 | size_t input_rank = matched_operators.at("op_0")->inputs[0]->shape.size(); |
| 52 | return input_rank == 2 || input_rank == 3; |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | class fuse_static_Fbatchnorm_pass_1d_1 : public GraphRewriterPass |
| 57 | { |
| 58 | public: |
| 59 | const char* match_pattern_graph() const |
| 60 | { |
| 61 | return R"PNNXIR(7767517 |
| 62 | 7 6 |
| 63 | pnnx.Input input 0 1 input |
| 64 | pnnx.Attribute op_mean 0 1 running_mean @data=(%num_features)f32 |
| 65 | pnnx.Attribute op_var 0 1 running_var @data=(%num_features)f32 |
| 66 | pnnx.Attribute op_weight 0 1 weight @data |
| 67 | pnnx.Attribute op_bias 0 1 bias @data |
| 68 | F.batch_norm op_0 5 1 input running_mean running_var weight bias out eps=%eps |
| 69 | pnnx.Output output 1 0 out |
| 70 | )PNNXIR"; |
| 71 | } |
| 72 | |
| 73 | const char* replace_pattern_graph() const |
| 74 | { |
| 75 | return R"PNNXIR(7767517 |
| 76 | 3 2 |
| 77 | pnnx.Input input 0 1 input |
| 78 | nn.BatchNorm1d batchnorm 1 1 input out num_features=%num_features eps=%eps affine=True @running_mean=%op_mean.data @running_var=%op_var.data @weight=%op_weight.data @bias=%op_bias.data |
| 79 | pnnx.Output output 1 0 out |
| 80 | )PNNXIR"; |
| 81 | } |
| 82 | |
| 83 | 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 |
| 84 | { |
| 85 | size_t input_rank = matched_operators.at("op_0")->inputs[0]->shape.size(); |
| 86 | return input_rank == 2 || input_rank == 3; |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | class fuse_static_Fbatchnorm_pass_2d : public GraphRewriterPass |
| 91 | { |
| 92 | public: |
| 93 | const char* match_pattern_graph() const |
| 94 | { |
| 95 | return R"PNNXIR(7767517 |
| 96 | 5 4 |
| 97 | pnnx.Input input 0 1 input |
| 98 | pnnx.Attribute op_mean 0 1 running_mean @data=(%num_features)f32 |
| 99 | pnnx.Attribute op_var 0 1 running_var @data=(%num_features)f32 |
| 100 | F.batchnorm op_0 3 1 input running_mean running_var out weight=None bias=None eps=%eps #input=(?,?,?,?)f32 |
| 101 | pnnx.Output output 1 0 out |
| 102 | )PNNXIR"; |
| 103 | } |
| 104 | |
| 105 | const char* replace_pattern_graph() const |
| 106 | { |
| 107 | return R"PNNXIR(7767517 |
| 108 | 3 2 |
| 109 | pnnx.Input input 0 1 input |
| 110 | nn.BatchNorm2d batchnorm 1 1 input out num_features=%num_features eps=%eps affine=False @running_mean=%op_mean.data @running_var=%op_var.data |
| 111 | pnnx.Output output 1 0 out |
| 112 | )PNNXIR"; |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | class fuse_static_Fbatchnorm_pass_2d_1 : public GraphRewriterPass |
| 117 | { |
| 118 | public: |
| 119 | const char* match_pattern_graph() const |
| 120 | { |
| 121 | return R"PNNXIR(7767517 |
| 122 | 7 6 |
| 123 | pnnx.Input input 0 1 input |
| 124 | pnnx.Attribute op_mean 0 1 running_mean @data=(%num_features)f32 |
| 125 | pnnx.Attribute op_var 0 1 running_var @data=(%num_features)f32 |
| 126 | pnnx.Attribute op_weight 0 1 weight @data |
| 127 | pnnx.Attribute op_bias 0 1 bias @data |
| 128 | F.batch_norm op_0 5 1 input running_mean running_var weight bias out eps=%eps #input=(?,?,?,?)f32 |
| 129 | pnnx.Output output 1 0 out |
| 130 | )PNNXIR"; |
| 131 | } |
| 132 | |
| 133 | const char* replace_pattern_graph() const |
| 134 | { |
| 135 | return R"PNNXIR(7767517 |
| 136 | 3 2 |
| 137 | pnnx.Input input 0 1 input |
| 138 | nn.BatchNorm2d batchnorm 1 1 input out num_features=%num_features eps=%eps affine=True @running_mean=%op_mean.data @running_var=%op_var.data @weight=%op_weight.data @bias=%op_bias.data |
| 139 | pnnx.Output output 1 0 out |
| 140 | )PNNXIR"; |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | class fuse_static_Fbatchnorm_pass_3d : public GraphRewriterPass |
| 145 | { |
| 146 | public: |
| 147 | const char* match_pattern_graph() const |
| 148 | { |
| 149 | return R"PNNXIR(7767517 |
| 150 | 5 4 |
| 151 | pnnx.Input input 0 1 input |
| 152 | pnnx.Attribute op_mean 0 1 running_mean @data=(%num_features)f32 |
| 153 | pnnx.Attribute op_var 0 1 running_var @data=(%num_features)f32 |
| 154 | F.batchnorm op_0 3 1 input running_mean running_var out weight=None bias=None eps=%eps #input=(?,?,?,?,?)f32 |
| 155 | pnnx.Output output 1 0 out |
| 156 | )PNNXIR"; |
| 157 | } |
| 158 | |
| 159 | const char* replace_pattern_graph() const |
| 160 | { |
| 161 | return R"PNNXIR(7767517 |
| 162 | 3 2 |
| 163 | pnnx.Input input 0 1 input |
| 164 | nn.BatchNorm3d batchnorm 1 1 input out num_features=%num_features eps=%eps affine=False @running_mean=%op_mean.data @running_var=%op_var.data |
| 165 | pnnx.Output output 1 0 out |
| 166 | )PNNXIR"; |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | class fuse_static_Fbatchnorm_pass_3d_1 : public GraphRewriterPass |
| 171 | { |
| 172 | public: |
| 173 | const char* match_pattern_graph() const |
| 174 | { |
| 175 | return R"PNNXIR(7767517 |
| 176 | 7 6 |
| 177 | pnnx.Input input 0 1 input |
| 178 | pnnx.Attribute op_mean 0 1 running_mean @data=(%num_features)f32 |
| 179 | pnnx.Attribute op_var 0 1 running_var @data=(%num_features)f32 |
| 180 | pnnx.Attribute op_weight 0 1 weight @data |
| 181 | pnnx.Attribute op_bias 0 1 bias @data |
| 182 | F.batch_norm op_0 5 1 input running_mean running_var weight bias out eps=%eps #input=(?,?,?,?,?)f32 |
| 183 | pnnx.Output output 1 0 out |
| 184 | )PNNXIR"; |
| 185 | } |
| 186 | |
| 187 | const char* replace_pattern_graph() const |
| 188 | { |
| 189 | return R"PNNXIR(7767517 |
| 190 | 3 2 |
| 191 | pnnx.Input input 0 1 input |
| 192 | nn.BatchNorm3d batchnorm 1 1 input out num_features=%num_features eps=%eps affine=True @running_mean=%op_mean.data @running_var=%op_var.data @weight=%op_weight.data @bias=%op_bias.data |
| 193 | pnnx.Output output 1 0 out |
| 194 | )PNNXIR"; |
| 195 | } |
| 196 | }; |
| 197 | |
| 198 | void fuse_static_batchnorm(Graph& graph) |
| 199 | { |
| 200 | fuse_static_Fbatchnorm_pass_1d a; |
| 201 | fuse_static_Fbatchnorm_pass_2d b; |
| 202 | fuse_static_Fbatchnorm_pass_3d c; |
| 203 | fuse_static_Fbatchnorm_pass_1d_1 a1; |
| 204 | fuse_static_Fbatchnorm_pass_2d_1 b1; |
| 205 | fuse_static_Fbatchnorm_pass_3d_1 c1; |
| 206 | int opindex = 0; |
| 207 | |
| 208 | pnnx_graph_rewrite(graph, &a, opindex); |
| 209 | pnnx_graph_rewrite(graph, &b, opindex); |
| 210 | pnnx_graph_rewrite(graph, &c, opindex); |
| 211 | pnnx_graph_rewrite(graph, &a1, opindex); |
| 212 | pnnx_graph_rewrite(graph, &b1, opindex); |
| 213 | pnnx_graph_rewrite(graph, &c1, opindex); |
| 214 | } |
| 215 | |
| 216 | } // namespace pnnx |