ncnn

Форк
0
/
unaryop.cpp 
285 строк · 5.8 Кб
1
// Tencent is pleased to support the open source community by making ncnn available.
2
//
3
// Copyright (C) 2017 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 "unaryop.h"
16

17
// #include <fenv.h>
18
#include <float.h>
19

20
namespace ncnn {
21

22
UnaryOp::UnaryOp()
23
{
24
    one_blob_only = true;
25
    support_inplace = true;
26
}
27

28
int UnaryOp::load_param(const ParamDict& pd)
29
{
30
    op_type = pd.get(0, 0);
31

32
    return 0;
33
}
34

35
template<typename Op>
36
static int unary_op_inplace(Mat& a, const Option& opt)
37
{
38
    Op op;
39

40
    int size = static_cast<int>(a.total());
41

42
    #pragma omp parallel for num_threads(opt.num_threads)
43
    for (int i = 0; i < size; i++)
44
    {
45
        a[i] = op(a[i]);
46
    }
47

48
    return 0;
49
}
50

51
struct unary_op_abs
52
{
53
    float operator()(const float& x) const
54
    {
55
        return (float)fabsf(x);
56
    }
57
};
58

59
struct unary_op_neg
60
{
61
    float operator()(const float& x) const
62
    {
63
        return -x;
64
    }
65
};
66

67
struct unary_op_floor
68
{
69
    float operator()(const float& x) const
70
    {
71
        return (float)floorf(x);
72
    }
73
};
74

75
struct unary_op_ceil
76
{
77
    float operator()(const float& x) const
78
    {
79
        return (float)ceilf(x);
80
    }
81
};
82

83
struct unary_op_square
84
{
85
    float operator()(const float& x) const
86
    {
87
        return x * x;
88
    }
89
};
90

91
struct unary_op_sqrt
92
{
93
    float operator()(const float& x) const
94
    {
95
        return (float)sqrtf(x);
96
    }
97
};
98

99
struct unary_op_rsqrt
100
{
101
    float operator()(const float& x) const
102
    {
103
        return 1.f / sqrtf(x);
104
    }
105
};
106

107
struct unary_op_exp
108
{
109
    float operator()(const float& x) const
110
    {
111
        return (float)expf(x);
112
    }
113
};
114

115
struct unary_op_log
116
{
117
    float operator()(const float& x) const
118
    {
119
        return (float)logf(x);
120
    }
121
};
122

123
struct unary_op_sin
124
{
125
    float operator()(const float& x) const
126
    {
127
        return (float)sinf(x);
128
    }
129
};
130

131
struct unary_op_cos
132
{
133
    float operator()(const float& x) const
134
    {
135
        return (float)cosf(x);
136
    }
137
};
138

139
struct unary_op_tan
140
{
141
    float operator()(const float& x) const
142
    {
143
        return (float)tanf(x);
144
    }
145
};
146

147
struct unary_op_asin
148
{
149
    float operator()(const float& x) const
150
    {
151
        return (float)asinf(x);
152
    }
153
};
154

155
struct unary_op_acos
156
{
157
    float operator()(const float& x) const
158
    {
159
        return (float)acosf(x);
160
    }
161
};
162

163
struct unary_op_atan
164
{
165
    float operator()(const float& x) const
166
    {
167
        return (float)atanf(x);
168
    }
169
};
170

171
struct unary_op_reciprocal
172
{
173
    float operator()(const float& x) const
174
    {
175
        return 1.f / x;
176
    }
177
};
178

179
struct unary_op_tanh
180
{
181
    float operator()(const float& x) const
182
    {
183
        return (float)tanhf(x);
184
    }
185
};
186

187
struct unary_op_log10
188
{
189
    float operator()(const float& x) const
190
    {
191
        return (float)log10f(x);
192
    }
193
};
194

195
struct unary_op_round
196
{
197
    float operator()(const float& x) const
198
    {
199
        // round to nearest even
200
#ifdef FE_TONEAREST
201
        int old_rm = fegetround();
202
        fesetround(FE_TONEAREST);
203
#endif
204
        float y = nearbyintf(x);
205
#ifdef FE_TONEAREST
206
        fesetround(old_rm);
207
#endif
208
        return y;
209
    }
210
};
211

212
struct unary_op_trunc
213
{
214
    float operator()(const float& x) const
215
    {
216
        return (float)truncf(x);
217
    }
218
};
219

220
int UnaryOp::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
221
{
222
    if (op_type == Operation_ABS)
223
        return unary_op_inplace<unary_op_abs>(bottom_top_blob, opt);
224

225
    if (op_type == Operation_NEG)
226
        return unary_op_inplace<unary_op_neg>(bottom_top_blob, opt);
227

228
    if (op_type == Operation_FLOOR)
229
        return unary_op_inplace<unary_op_floor>(bottom_top_blob, opt);
230

231
    if (op_type == Operation_CEIL)
232
        return unary_op_inplace<unary_op_ceil>(bottom_top_blob, opt);
233

234
    if (op_type == Operation_SQUARE)
235
        return unary_op_inplace<unary_op_square>(bottom_top_blob, opt);
236

237
    if (op_type == Operation_SQRT)
238
        return unary_op_inplace<unary_op_sqrt>(bottom_top_blob, opt);
239

240
    if (op_type == Operation_RSQRT)
241
        return unary_op_inplace<unary_op_rsqrt>(bottom_top_blob, opt);
242

243
    if (op_type == Operation_EXP)
244
        return unary_op_inplace<unary_op_exp>(bottom_top_blob, opt);
245

246
    if (op_type == Operation_LOG)
247
        return unary_op_inplace<unary_op_log>(bottom_top_blob, opt);
248

249
    if (op_type == Operation_SIN)
250
        return unary_op_inplace<unary_op_sin>(bottom_top_blob, opt);
251

252
    if (op_type == Operation_COS)
253
        return unary_op_inplace<unary_op_cos>(bottom_top_blob, opt);
254

255
    if (op_type == Operation_TAN)
256
        return unary_op_inplace<unary_op_tan>(bottom_top_blob, opt);
257

258
    if (op_type == Operation_ASIN)
259
        return unary_op_inplace<unary_op_asin>(bottom_top_blob, opt);
260

261
    if (op_type == Operation_ACOS)
262
        return unary_op_inplace<unary_op_acos>(bottom_top_blob, opt);
263

264
    if (op_type == Operation_ATAN)
265
        return unary_op_inplace<unary_op_atan>(bottom_top_blob, opt);
266

267
    if (op_type == Operation_RECIPROCAL)
268
        return unary_op_inplace<unary_op_reciprocal>(bottom_top_blob, opt);
269

270
    if (op_type == Operation_TANH)
271
        return unary_op_inplace<unary_op_tanh>(bottom_top_blob, opt);
272

273
    if (op_type == Operation_LOG10)
274
        return unary_op_inplace<unary_op_log10>(bottom_top_blob, opt);
275

276
    if (op_type == Operation_ROUND)
277
        return unary_op_inplace<unary_op_round>(bottom_top_blob, opt);
278

279
    if (op_type == Operation_TRUNC)
280
        return unary_op_inplace<unary_op_trunc>(bottom_top_blob, opt);
281

282
    return 0;
283
}
284

285
} // namespace ncnn
286

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.