Elffuss BraidHub

kikocis/ncnn public

master · 2.3 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 "elu_x86.h"
16
17#include "x86_activation.h"
18
19namespace ncnn {
20
21ELU_x86::ELU_x86()
22{
23#if __SSE2__
24 support_packing = true;
25#endif // __SSE2__
26}
27
28int ELU_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
29{
30 int w = bottom_top_blob.w;
31 int h = bottom_top_blob.h;
32 int d = bottom_top_blob.d;
33 int channels = bottom_top_blob.c;
34 int elempack = bottom_top_blob.elempack;
35 int size = w * h * d * elempack;
36
37 #pragma omp parallel for num_threads(opt.num_threads)
38 for (int q = 0; q < channels; q++)
39 {
40 float* ptr = bottom_top_blob.channel(q);
41
42 int i = 0;
43
44#if __SSE2__
45#if __AVX__
46#if __AVX512F__
47 __m512 _alpha512 = _mm512_set1_ps(alpha);
48 for (; i + 15 < size; i += 16)
49 {
50 __m512 _p = _mm512_loadu_ps(ptr);
51 _mm512_storeu_ps(ptr, elu_avx512(_p, _alpha512));
52
53 ptr += 16;
54 }
55#endif // __AVX512F__
56 __m256 _alpha256 = _mm256_set1_ps(alpha);
57 for (; i + 7 < size; i += 8)
58 {
59 __m256 _p = _mm256_loadu_ps(ptr);
60 _mm256_storeu_ps(ptr, elu_avx(_p, _alpha256));
61
62 ptr += 8;
63 }
64#endif // __AVX__
65 __m128 _alpha128 = _mm_set1_ps(alpha);
66 for (; i + 3 < size; i += 4)
67 {
68 __m128 _p = _mm_load_ps(ptr);
69 _mm_store_ps(ptr, elu_sse(_p, _alpha128));
70
71 ptr += 4;
72 }
73#endif // __SSE2__
74 for (; i < size; i++)
75 {
76 if (*ptr < 0.f)
77 *ptr = alpha * (expf(*ptr) - 1.f);
78 ptr++;
79 }
80 }
81
82 return 0;
83}
84
85} // namespace ncnn