| 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_instancenorm.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_Finstancenorm_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_weight 0 1 weight @data=(%num_features)f32 |
| 33 | pnnx.Attribute op_bias 0 1 bias @data=(%num_features)f32 |
| 34 | F.instance_norm op_0 3 1 input weight bias out running_mean=None running_var=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.InstanceNorm1d in 1 1 input out num_features=%num_features eps=%eps affine=True track_running_stats=False @weight=%op_weight.data @bias=%op_bias.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_Finstancenorm_pass_2d : public GraphRewriterPass |
| 57 | { |
| 58 | public: |
| 59 | const char* match_pattern_graph() const |
| 60 | { |
| 61 | return R"PNNXIR(7767517 |
| 62 | 5 4 |
| 63 | pnnx.Input input 0 1 input |
| 64 | pnnx.Attribute op_weight 0 1 weight @data=(%num_features)f32 |
| 65 | pnnx.Attribute op_bias 0 1 bias @data=(%num_features)f32 |
| 66 | F.instance_norm op_0 3 1 input weight bias out running_mean=None running_var=None eps=%eps #input=(?,?,?,?)f32 |
| 67 | pnnx.Output output 1 0 out |
| 68 | )PNNXIR"; |
| 69 | } |
| 70 | |
| 71 | const char* replace_pattern_graph() const |
| 72 | { |
| 73 | return R"PNNXIR(7767517 |
| 74 | 3 2 |
| 75 | pnnx.Input input 0 1 input |
| 76 | nn.InstanceNorm2d in 1 1 input out num_features=%num_features eps=%eps affine=True track_running_stats=False @weight=%op_weight.data @bias=%op_bias.data |
| 77 | pnnx.Output output 1 0 out |
| 78 | )PNNXIR"; |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | class fuse_static_Finstancenorm_pass_3d : public GraphRewriterPass |
| 83 | { |
| 84 | public: |
| 85 | const char* match_pattern_graph() const |
| 86 | { |
| 87 | return R"PNNXIR(7767517 |
| 88 | 5 4 |
| 89 | pnnx.Input input 0 1 input |
| 90 | pnnx.Attribute op_weight 0 1 weight @data=(%num_features)f32 |
| 91 | pnnx.Attribute op_bias 0 1 bias @data=(%num_features)f32 |
| 92 | F.instance_norm op_0 3 1 input weight bias out running_mean=None running_var=None eps=%eps #input=(?,?,?,?,?)f32 |
| 93 | pnnx.Output output 1 0 out |
| 94 | )PNNXIR"; |
| 95 | } |
| 96 | |
| 97 | const char* replace_pattern_graph() const |
| 98 | { |
| 99 | return R"PNNXIR(7767517 |
| 100 | 3 2 |
| 101 | pnnx.Input input 0 1 input |
| 102 | nn.InstanceNorm3d in 1 1 input out num_features=%num_features eps=%eps affine=True track_running_stats=False @weight=%op_weight.data @bias=%op_bias.data |
| 103 | pnnx.Output output 1 0 out |
| 104 | )PNNXIR"; |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | void fuse_static_instancenorm(Graph& graph) |
| 109 | { |
| 110 | fuse_static_Finstancenorm_pass_1d a; |
| 111 | fuse_static_Finstancenorm_pass_2d b; |
| 112 | fuse_static_Finstancenorm_pass_3d c; |
| 113 | int opindex = 0; |
| 114 | |
| 115 | pnnx_graph_rewrite(graph, &a, opindex); |
| 116 | pnnx_graph_rewrite(graph, &b, opindex); |
| 117 | pnnx_graph_rewrite(graph, &c, opindex); |
| 118 | } |
| 119 | |
| 120 | } // namespace pnnx |