Elffuss BraidHub

kikocis/ncnn public

master · 4.1 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_static_instancenorm.h"
16
17#include "pass_level2.h"
18
19#include <math.h>
20#include <string.h>
21
22namespace pnnx {
23
24class fuse_static_Finstancenorm_pass_1d : public GraphRewriterPass
25{
26public:
27 const char* match_pattern_graph() const
28 {
29 return R"PNNXIR(7767517
305 4
31pnnx.Input input 0 1 input
32pnnx.Attribute op_weight 0 1 weight @data=(%num_features)f32
33pnnx.Attribute op_bias 0 1 bias @data=(%num_features)f32
34F.instance_norm op_0 3 1 input weight bias out running_mean=None running_var=None eps=%eps
35pnnx.Output output 1 0 out
36)PNNXIR";
37 }
38
39 const char* replace_pattern_graph() const
40 {
41 return R"PNNXIR(7767517
423 2
43pnnx.Input input 0 1 input
44nn.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
45pnnx.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
56class fuse_static_Finstancenorm_pass_2d : public GraphRewriterPass
57{
58public:
59 const char* match_pattern_graph() const
60 {
61 return R"PNNXIR(7767517
625 4
63pnnx.Input input 0 1 input
64pnnx.Attribute op_weight 0 1 weight @data=(%num_features)f32
65pnnx.Attribute op_bias 0 1 bias @data=(%num_features)f32
66F.instance_norm op_0 3 1 input weight bias out running_mean=None running_var=None eps=%eps #input=(?,?,?,?)f32
67pnnx.Output output 1 0 out
68)PNNXIR";
69 }
70
71 const char* replace_pattern_graph() const
72 {
73 return R"PNNXIR(7767517
743 2
75pnnx.Input input 0 1 input
76nn.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
77pnnx.Output output 1 0 out
78)PNNXIR";
79 }
80};
81
82class fuse_static_Finstancenorm_pass_3d : public GraphRewriterPass
83{
84public:
85 const char* match_pattern_graph() const
86 {
87 return R"PNNXIR(7767517
885 4
89pnnx.Input input 0 1 input
90pnnx.Attribute op_weight 0 1 weight @data=(%num_features)f32
91pnnx.Attribute op_bias 0 1 bias @data=(%num_features)f32
92F.instance_norm op_0 3 1 input weight bias out running_mean=None running_var=None eps=%eps #input=(?,?,?,?,?)f32
93pnnx.Output output 1 0 out
94)PNNXIR";
95 }
96
97 const char* replace_pattern_graph() const
98 {
99 return R"PNNXIR(7767517
1003 2
101pnnx.Input input 0 1 input
102nn.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
103pnnx.Output output 1 0 out
104)PNNXIR";
105 }
106};
107
108void 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