Elffuss BraidHub

kikocis/ncnn public

master · 4.5 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 "gelu_x86.h"
16
17#if __SSE2__
18#include <emmintrin.h>
19#include "sse_mathfun.h"
20#if __AVX__
21#include <immintrin.h>
22#include "avx_mathfun.h"
23#if __AVX512F__
24#include "avx512_mathfun.h"
25#endif // __AVX512F__
26#endif // __AVX__
27#endif // __SSE2__
28
29namespace ncnn {
30
31GELU_x86::GELU_x86()
32{
33#if __SSE2__
34 support_packing = true;
35#endif // __SSE2__
36}
37
38int GELU_x86::create_pipeline(const Option& /*opt*/)
39{
40 if (!fast_gelu)
41 {
42 support_packing = false;
43 }
44 return 0;
45}
46
47int GELU_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
48{
49 if (!fast_gelu)
50 {
51 return GELU::forward_inplace(bottom_top_blob, opt);
52 }
53
54 int w = bottom_top_blob.w;
55 int h = bottom_top_blob.h;
56 int d = bottom_top_blob.d;
57 int elempack = bottom_top_blob.elempack;
58 int channels = bottom_top_blob.c;
59 int size = w * h * d * elempack;
60
61 #pragma omp parallel for num_threads(opt.num_threads)
62 for (int q = 0; q < channels; q++)
63 {
64 float* ptr = bottom_top_blob.channel(q);
65
66 int i = 0;
67
68#if __SSE2__
69#if __AVX__
70#if __AVX512F__
71 __m512 _half512 = _mm512_set1_ps(0.5f);
72 __m512 _one512 = _mm512_set1_ps(1.f);
73 __m512 _fast1c512 = _mm512_set1_ps(0.79788452f);
74 __m512 _fast2c512 = _mm512_set1_ps(0.044715f);
75 for (; i + 15 < size; i += 16)
76 {
77 __m512 _pLoad = _mm512_loadu_ps(ptr);
78
79 __m512 _cube = _mm512_mul_ps(_pLoad, _pLoad);
80 _cube = _mm512_mul_ps(_pLoad, _cube);
81
82 __m512 _blob = _mm512_mul_ps(_fast2c512, _cube);
83 _blob = _mm512_add_ps(_pLoad, _blob);
84 _blob = _mm512_mul_ps(_fast1c512, _blob);
85 _blob = tanh512_ps(_blob);
86 _blob = _mm512_add_ps(_one512, _blob);
87
88 _blob = _mm512_mul_ps(_half512, _mm512_mul_ps(_blob, _pLoad));
89
90 _mm512_storeu_ps(ptr, _blob);
91
92 ptr += 16;
93 }
94#endif // __AVX512F__
95 __m256 _half256 = _mm256_set1_ps(0.5f);
96 __m256 _one256 = _mm256_set1_ps(1.f);
97 __m256 _fast1c256 = _mm256_set1_ps(0.79788452f);
98 __m256 _fast2c256 = _mm256_set1_ps(0.044715f);
99 for (; i + 7 < size; i += 8)
100 {
101 __m256 _pLoad = _mm256_loadu_ps(ptr);
102
103 __m256 _cube = _mm256_mul_ps(_pLoad, _pLoad);
104 _cube = _mm256_mul_ps(_pLoad, _cube);
105
106 __m256 _blob = _mm256_mul_ps(_fast2c256, _cube);
107 _blob = _mm256_add_ps(_pLoad, _blob);
108 _blob = _mm256_mul_ps(_fast1c256, _blob);
109 _blob = tanh256_ps(_blob);
110 _blob = _mm256_add_ps(_one256, _blob);
111
112 _blob = _mm256_mul_ps(_half256, _mm256_mul_ps(_blob, _pLoad));
113
114 _mm256_storeu_ps(ptr, _blob);
115
116 ptr += 8;
117 }
118#endif // __AVX__
119 __m128 _half128 = _mm_set1_ps(0.5f);
120 __m128 _one128 = _mm_set1_ps(1.f);
121 __m128 _fast1c128 = _mm_set1_ps(0.79788452f);
122 __m128 _fast2c128 = _mm_set1_ps(0.044715f);
123 for (; i + 3 < size; i += 4)
124 {
125 __m128 _pLoad = _mm_loadu_ps(ptr);
126
127 __m128 _cube = _mm_mul_ps(_pLoad, _pLoad);
128 _cube = _mm_mul_ps(_pLoad, _cube);
129
130 __m128 _blob = _mm_mul_ps(_fast2c128, _cube);
131 _blob = _mm_add_ps(_pLoad, _blob);
132 _blob = _mm_mul_ps(_fast1c128, _blob);
133 _blob = tanh_ps(_blob);
134 _blob = _mm_add_ps(_one128, _blob);
135
136 _blob = _mm_mul_ps(_half128, _mm_mul_ps(_blob, _pLoad));
137
138 _mm_storeu_ps(ptr, _blob);
139
140 ptr += 4;
141 }
142#endif // __SSE2__
143 for (; i < size; i++)
144 {
145 // y = 0.5x * (1 + tanh(sqrt(2/Pi) * (x + 0.044715x^3)))
146 *ptr = 0.5f * *ptr * (1.0f + tanhf(0.79788452f * (*ptr + 0.044715f * *ptr * *ptr * *ptr)));
147
148 ptr++;
149 }
150 }
151
152 return 0;
153}
154
155} // namespace ncnn