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) 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_level2.h"
16
17namespace pnnx {
18
19class F_group_norm : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
258 7
26pnnx.Input input_1 0 1 input
27pnnx.Input input_2 0 1 weight
28pnnx.Input input_3 0 1 bias
29prim::Constant op_0 0 1 num_groups value=%num_groups
30prim::Constant op_1 0 1 eps value=%eps
31prim::Constant op_2 0 1 cudnn_enabled value=*
32aten::group_norm op_3 6 1 input num_groups weight bias eps cudnn_enabled out
33pnnx.Output output 1 0 out
34)PNNXIR";
35 }
36
37 const char* type_str() const
38 {
39 return "F.group_norm";
40 }
41};
42
43REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_group_norm, 10)
44
45class F_group_norm_onnx : public GraphRewriterPass
46{
47public:
48 const char* match_pattern_graph() const
49 {
50 return R"PNNXIR(7767517
517 6
52pnnx.Input input_0 0 1 input
53Reshape op_0 1 1 input r1 allowzero=0 shape=(0,%num_groups,-1)
54pnnx.Attribute op_1 0 1 ones @data
55pnnx.Attribute op_2 0 1 zeros @data
56InstanceNormalization op_3 3 1 r1 ones zeros in epsilon=%epsilon
57Reshape op_4 1 1 in out allowzero=0 shape=%shape
58pnnx.Output output 1 0 out
59)PNNXIR";
60 }
61
62 const char* type_str() const
63 {
64 return "F.group_norm";
65 }
66
67 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
68 {
69 const Operator* op_reshape = matched_operators.at("op_0");
70 const std::vector<int>& inputshape = op_reshape->inputs[0]->shape;
71 if (inputshape != captured_params.at("shape").ai)
72 return false;
73
74 const int num_groups = captured_params.at("num_groups").i;
75
76 const Attribute& ones = captured_attrs.at("op_1.data");
77 const Attribute& zeros = captured_attrs.at("op_2.data");
78
79 if (ones.shape.size() != 1 || ones.shape[0] != num_groups)
80 return false;
81 if (zeros.shape.size() != 1 || zeros.shape[0] != num_groups)
82 return false;
83
84 for (auto x : ones.get_float32_data())
85 {
86 if (x != 1.f)
87 return false;
88 }
89
90 for (auto x : zeros.get_float32_data())
91 {
92 if (x != 0.f)
93 return false;
94 }
95
96 return true;
97 }
98
99 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
100 {
101 op->params["num_groups"] = captured_params.at("num_groups");
102 op->params["eps"] = captured_params.at("epsilon");
103 }
104};
105
106REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_group_norm_onnx, 9)
107
108class F_group_norm_onnx_1 : public F_group_norm_onnx
109{
110public:
111 const char* match_pattern_graph() const
112 {
113 return R"PNNXIR(7767517
1147 6
115pnnx.Input input_0 0 1 input
116Reshape op_0 1 1 input r1 shape=(0,%num_groups,-1)
117pnnx.Attribute op_1 0 1 ones @data
118pnnx.Attribute op_2 0 1 zeros @data
119InstanceNormalization op_3 3 1 r1 ones zeros in epsilon=%epsilon
120Reshape op_4 1 1 in out shape=%shape
121pnnx.Output output 1 0 out
122)PNNXIR";
123 }
124};
125
126REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_group_norm_onnx_1, 9)
127
128} // namespace pnnx