Elffuss BraidHub

kikocis/ncnn public

master · 6.2 KiBRawPermalink
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 "relu_x86.h"
16
17#if __SSE2__
18#include <emmintrin.h>
19#if __AVX__
20#include <immintrin.h>
21#endif // __AVX__
22#endif // __SSE2__
23
24namespace ncnn {
25
26ReLU_x86::ReLU_x86()
27{
28#if __SSE2__
29 support_packing = true;
30#endif // __SSE2__
31}
32
33int ReLU_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
34{
35 int elembits = bottom_top_blob.elembits();
36
37 if (elembits == 8)
38 return forward_inplace_int8(bottom_top_blob, opt);
39
40 int w = bottom_top_blob.w;
41 int h = bottom_top_blob.h;
42 int d = bottom_top_blob.d;
43 int channels = bottom_top_blob.c;
44 int elempack = bottom_top_blob.elempack;
45 int size = w * h * d * elempack;
46
47 if (slope == 0.f)
48 {
49 #pragma omp parallel for num_threads(opt.num_threads)
50 for (int q = 0; q < channels; q++)
51 {
52 float* ptr = bottom_top_blob.channel(q);
53
54 int i = 0;
55#if __SSE2__
56#if __AVX__
57#if __AVX512F__
58 __m512 _zero_avx512 = _mm512_setzero_ps();
59 for (; i + 15 < size; i += 16)
60 {
61 __m512 _p = _mm512_loadu_ps(ptr);
62 _mm512_storeu_ps(ptr, _mm512_max_ps(_zero_avx512, _p));
63 ptr += 16;
64 }
65#endif // __AVX512F__
66 __m256 _zero_avx = _mm256_setzero_ps();
67 for (; i + 7 < size; i += 8)
68 {
69 __m256 _p = _mm256_loadu_ps(ptr);
70 _mm256_storeu_ps(ptr, _mm256_max_ps(_zero_avx, _p));
71 ptr += 8;
72 }
73#endif // __AVX__
74 __m128 _zero = _mm_setzero_ps();
75 for (; i + 3 < size; i += 4)
76 {
77 __m128 _p = _mm_load_ps(ptr);
78 _mm_store_ps(ptr, _mm_max_ps(_zero, _p));
79 ptr += 4;
80 }
81#endif // __SSE2__
82 for (; i < size; i++)
83 {
84 *ptr = std::max(*ptr, 0.f);
85 ptr++;
86 }
87 }
88 }
89 else
90 {
91 #pragma omp parallel for num_threads(opt.num_threads)
92 for (int q = 0; q < channels; q++)
93 {
94 float* ptr = bottom_top_blob.channel(q);
95
96 int i = 0;
97#if __SSE2__
98#if __AVX__
99#if __AVX512F__
100 __m512 _zero_avx512 = _mm512_setzero_ps();
101 __m512 _slope_avx512 = _mm512_set1_ps(slope);
102 for (; i + 15 < size; i += 16)
103 {
104 __m512 _p = _mm512_loadu_ps(ptr);
105 __mmask16 _is_negative = _mm512_cmp_ps_mask(_p, _zero_avx512, _CMP_LT_OQ);
106 _p = _mm512_mask_mul_ps(_p, _is_negative, _p, _slope_avx512);
107 _mm512_storeu_ps(ptr, _p);
108 ptr += 16;
109 }
110#endif // __AVX512F__
111 __m256 _zero_avx = _mm256_setzero_ps();
112 __m256 _slope_avx = _mm256_set1_ps(slope);
113 for (; i + 7 < size; i += 8)
114 {
115 __m256 _p = _mm256_loadu_ps(ptr);
116 __m256 _pos = _mm256_max_ps(_zero_avx, _p);
117 __m256 _neg = _mm256_min_ps(_zero_avx, _p);
118 _p = _mm256_add_ps(_pos, _mm256_mul_ps(_slope_avx, _neg));
119 _mm256_storeu_ps(ptr, _p);
120 ptr += 8;
121 }
122#endif // __AVX__
123 __m128 _zero = _mm_setzero_ps();
124 __m128 _slope = _mm_set1_ps(slope);
125 for (; i + 3 < size; i += 4)
126 {
127 __m128 _p = _mm_load_ps(ptr);
128 __m128 _pos = _mm_max_ps(_zero, _p);
129 __m128 _neg = _mm_min_ps(_zero, _p);
130 _p = _mm_add_ps(_pos, _mm_mul_ps(_slope, _neg));
131 _mm_store_ps(ptr, _p);
132 ptr += 4;
133 }
134#endif // __SSE2__
135 for (; i < size; i++)
136 {
137 if (*ptr < 0)
138 *ptr *= slope;
139 ptr++;
140 }
141 }
142 }
143
144 return 0;
145}
146
147int ReLU_x86::forward_inplace_int8(Mat& bottom_top_blob, const Option& opt) const
148{
149 int w = bottom_top_blob.w;
150 int h = bottom_top_blob.h;
151 int d = bottom_top_blob.d;
152 int channels = bottom_top_blob.c;
153 int size = w * h * d;
154 int elempack = bottom_top_blob.elempack;
155
156#if __SSE2__
157 if (elempack == 8)
158 {
159 if (slope == 0.f)
160 {
161 #pragma omp parallel for num_threads(opt.num_threads)
162 for (int q = 0; q < channels; q++)
163 {
164 signed char* ptr = bottom_top_blob.channel(q);
165
166 int i = 0;
167 for (; i < size; i++)
168 {
169 if (ptr[0] < 0)
170 ptr[0] = 0;
171 if (ptr[1] < 0)
172 ptr[1] = 0;
173 if (ptr[2] < 0)
174 ptr[2] = 0;
175 if (ptr[3] < 0)
176 ptr[3] = 0;
177 if (ptr[4] < 0)
178 ptr[4] = 0;
179 if (ptr[5] < 0)
180 ptr[5] = 0;
181 if (ptr[6] < 0)
182 ptr[6] = 0;
183 if (ptr[7] < 0)
184 ptr[7] = 0;
185
186 ptr += 8;
187 }
188 }
189 }
190 else
191 {
192 // TODO leakyrelu
193 }
194
195 return 0;
196 }
197#endif // __SSE2__
198
199 if (slope == 0.f)
200 {
201 #pragma omp parallel for num_threads(opt.num_threads)
202 for (int q = 0; q < channels; q++)
203 {
204 signed char* ptr = bottom_top_blob.channel(q);
205
206 int i = 0;
207 for (; i < size; i++)
208 {
209 if (*ptr < 0)
210 *ptr = 0;
211
212 ptr++;
213 }
214 }
215 }
216 else
217 {
218 // TODO leakyrelu
219 }
220
221 return 0;
222}
223
224} //namespace ncnn