Elffuss BraidHub

kikocis/ncnn public

master · 2.0 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
17#include <float.h>
18
19namespace pnnx {
20
21namespace ncnn {
22
23class F_hardtanh : public GraphRewriterPass
24{
25public:
26 const char* match_pattern_graph() const
27 {
28 return R"PNNXIR(7767517
293 2
30pnnx.Input input 0 1 input
31F.hardtanh op_0 1 1 input out min_val=%min_val max_val=%max_val
32pnnx.Output output 1 0 out
33)PNNXIR";
34 }
35
36 const char* type_str() const
37 {
38 return "Clip";
39 }
40
41 const char* name_str() const
42 {
43 return "htanh";
44 }
45
46 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
47 {
48 float min = -FLT_MAX;
49 float max = FLT_MAX;
50
51 if (captured_params.at("min_val").type == 2)
52 {
53 min = captured_params.at("min_val").i;
54 }
55 if (captured_params.at("min_val").type == 3)
56 {
57 min = captured_params.at("min_val").f;
58 }
59
60 if (captured_params.at("max_val").type == 2)
61 {
62 max = captured_params.at("max_val").i;
63 }
64 if (captured_params.at("max_val").type == 3)
65 {
66 max = captured_params.at("max_val").f;
67 }
68
69 op->params["0"] = min;
70 op->params["1"] = max;
71 }
72};
73
74REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_hardtanh, 20)
75
76} // namespace ncnn
77
78} // namespace pnnx