Elffuss BraidHub

kikocis/ncnn public

master · 7.3 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 "batchnorm_x86.h"
16
17#if __SSE2__
18#include <emmintrin.h>
19#if __AVX__
20#include <immintrin.h>
21#endif // __AVX__
22#endif // __SSE2__
23#include "x86_usability.h"
24
25namespace ncnn {
26
27BatchNorm_x86::BatchNorm_x86()
28{
29#if __SSE2__
30 support_packing = true;
31#endif // __SSE2__
32}
33
34int BatchNorm_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
35{
36 int dims = bottom_top_blob.dims;
37 int w = bottom_top_blob.w;
38 int h = bottom_top_blob.h;
39 int d = bottom_top_blob.d;
40 int c = bottom_top_blob.c;
41 int elempack = bottom_top_blob.elempack;
42
43 if (dims == 1)
44 {
45 float* ptr = bottom_top_blob;
46 const float* aptr = a_data;
47 const float* bptr = b_data;
48
49 const int size = w * elempack;
50
51 int i = 0;
52#if __SSE2__
53#if __AVX__
54#if __AVX512F__
55 for (; i + 15 < size; i += 16)
56 {
57 __m512 _p512 = _mm512_loadu_ps(ptr);
58 __m512 _a512 = _mm512_loadu_ps(aptr);
59 __m512 _b512 = _mm512_loadu_ps(bptr);
60 _p512 = _mm512_fmadd_ps(_p512, _b512, _a512);
61 _mm512_storeu_ps(ptr, _p512);
62 ptr += 16;
63 aptr += 16;
64 bptr += 16;
65 }
66#endif // __AVX512F__
67 for (; i + 7 < size; i += 8)
68 {
69 __m256 _p256 = _mm256_loadu_ps(ptr);
70 __m256 _a256 = _mm256_loadu_ps(aptr);
71 __m256 _b256 = _mm256_loadu_ps(bptr);
72 _p256 = _mm256_comp_fmadd_ps(_p256, _b256, _a256);
73 _mm256_storeu_ps(ptr, _p256);
74 ptr += 8;
75 aptr += 8;
76 bptr += 8;
77 }
78#endif // __AVX__
79 for (; i + 3 < size; i += 4)
80 {
81 __m128 _p128 = _mm_loadu_ps(ptr);
82 __m128 _a128 = _mm_loadu_ps(aptr);
83 __m128 _b128 = _mm_loadu_ps(bptr);
84 _p128 = _mm_comp_fmadd_ps(_p128, _b128, _a128);
85 _mm_storeu_ps(ptr, _p128);
86 ptr += 4;
87 aptr += 4;
88 bptr += 4;
89 }
90#endif // __SSE__
91 for (; i < size; i++)
92 {
93 *ptr = *bptr * *ptr + *aptr;
94 ptr++;
95 aptr++;
96 bptr++;
97 }
98 }
99
100 if (dims == 2)
101 {
102 const int size = w * elempack;
103
104 #pragma omp parallel for num_threads(opt.num_threads)
105 for (int i = 0; i < h; i++)
106 {
107 float* ptr = bottom_top_blob.row(i);
108 float a = a_data[i];
109 float b = b_data[i];
110
111#if __SSE2__
112 __m128 _a128 = (elempack == 4) ? _mm_loadu_ps((const float*)a_data + i * 4) : _mm_set1_ps(a);
113 __m128 _b128 = (elempack == 4) ? _mm_loadu_ps((const float*)b_data + i * 4) : _mm_set1_ps(b);
114#if __AVX__
115 __m256 _a256 = (elempack == 8) ? _mm256_loadu_ps((const float*)a_data + i * 8) : _mm256_insertf128_ps(_mm256_castps128_ps256(_a128), _a128, 1);
116 __m256 _b256 = (elempack == 8) ? _mm256_loadu_ps((const float*)b_data + i * 8) : _mm256_insertf128_ps(_mm256_castps128_ps256(_b128), _b128, 1);
117#if __AVX512F__
118 __m512 _a512 = (elempack == 16) ? _mm512_loadu_ps((const float*)a_data + i * 16) : _mm512_insertf32x8(_mm512_castps256_ps512(_a256), _a256, 1);
119 __m512 _b512 = (elempack == 16) ? _mm512_loadu_ps((const float*)b_data + i * 16) : _mm512_insertf32x8(_mm512_castps256_ps512(_b256), _b256, 1);
120#endif // __AVX512F__
121#endif // __AVX__
122#endif // __SSE2__
123
124 int j = 0;
125#if __SSE2__
126#if __AVX__
127#if __AVX512F__
128 for (; j + 15 < size; j += 16)
129 {
130 __m512 _p512 = _mm512_loadu_ps(ptr);
131 _p512 = _mm512_fmadd_ps(_p512, _b512, _a512);
132 _mm512_storeu_ps(ptr, _p512);
133 ptr += 16;
134 }
135#endif // __AVX512F__
136 for (; j + 7 < size; j += 8)
137 {
138 __m256 _p256 = _mm256_loadu_ps(ptr);
139 _p256 = _mm256_comp_fmadd_ps(_p256, _b256, _a256);
140 _mm256_storeu_ps(ptr, _p256);
141 ptr += 8;
142 }
143#endif // __AVX__
144 for (; j + 3 < size; j += 4)
145 {
146 __m128 _p128 = _mm_loadu_ps(ptr);
147 _p128 = _mm_comp_fmadd_ps(_p128, _b128, _a128);
148 _mm_storeu_ps(ptr, _p128);
149 ptr += 4;
150 }
151#endif // __SSE__
152 for (; j < size; j++)
153 {
154 *ptr = b * *ptr + a;
155 ptr++;
156 }
157 }
158 }
159
160 if (dims == 3 || dims == 4)
161 {
162 const int size = w * h * d * elempack;
163
164 #pragma omp parallel for num_threads(opt.num_threads)
165 for (int q = 0; q < c; q++)
166 {
167 float* ptr = bottom_top_blob.channel(q);
168 float a = a_data[q];
169 float b = b_data[q];
170
171#if __SSE2__
172 __m128 _a128 = (elempack == 4) ? _mm_loadu_ps((const float*)a_data + q * 4) : _mm_set1_ps(a);
173 __m128 _b128 = (elempack == 4) ? _mm_loadu_ps((const float*)b_data + q * 4) : _mm_set1_ps(b);
174#if __AVX__
175 __m256 _a256 = (elempack == 8) ? _mm256_loadu_ps((const float*)a_data + q * 8) : _mm256_insertf128_ps(_mm256_castps128_ps256(_a128), _a128, 1);
176 __m256 _b256 = (elempack == 8) ? _mm256_loadu_ps((const float*)b_data + q * 8) : _mm256_insertf128_ps(_mm256_castps128_ps256(_b128), _b128, 1);
177#if __AVX512F__
178 __m512 _a512 = (elempack == 16) ? _mm512_loadu_ps((const float*)a_data + q * 16) : _mm512_insertf32x8(_mm512_castps256_ps512(_a256), _a256, 1);
179 __m512 _b512 = (elempack == 16) ? _mm512_loadu_ps((const float*)b_data + q * 16) : _mm512_insertf32x8(_mm512_castps256_ps512(_b256), _b256, 1);
180#endif // __AVX512F__
181#endif // __AVX__
182#endif // __SSE2__
183
184 int i = 0;
185#if __SSE2__
186#if __AVX__
187#if __AVX512F__
188 for (; i + 15 < size; i += 16)
189 {
190 __m512 _p512 = _mm512_loadu_ps(ptr);
191 _p512 = _mm512_fmadd_ps(_p512, _b512, _a512);
192 _mm512_storeu_ps(ptr, _p512);
193 ptr += 16;
194 }
195#endif // __AVX512F__
196 for (; i + 7 < size; i += 8)
197 {
198 __m256 _p256 = _mm256_loadu_ps(ptr);
199 _p256 = _mm256_comp_fmadd_ps(_p256, _b256, _a256);
200 _mm256_storeu_ps(ptr, _p256);
201 ptr += 8;
202 }
203#endif // __AVX__
204 for (; i + 3 < size; i += 4)
205 {
206 __m128 _p128 = _mm_loadu_ps(ptr);
207 _p128 = _mm_comp_fmadd_ps(_p128, _b128, _a128);
208 _mm_storeu_ps(ptr, _p128);
209 ptr += 4;
210 }
211#endif // __SSE__
212 for (; i < size; i++)
213 {
214 *ptr = b * *ptr + a;
215 ptr++;
216 }
217 }
218 }
219
220 return 0;
221}
222
223} // namespace ncnn