Elffuss BraidHub

kikocis/ncnn public

master · 4.7 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 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 "dropout_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
26Dropout_x86::Dropout_x86()
27{
28#if __SSE2__
29 support_packing = true;
30#endif // __SSE2__
31}
32
33int Dropout_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
34{
35 if (scale == 1.f)
36 {
37 return 0;
38 }
39
40#if __SSE2__
41 int dims = bottom_top_blob.dims;
42 int elempack = bottom_top_blob.elempack;
43
44#if __AVX__
45#if __AVX512F__
46 if (elempack == 16)
47 {
48 Mat tmp;
49 convert_packing(bottom_top_blob, tmp, 8, opt);
50
51 forward_inplace(tmp, opt);
52
53 convert_packing(tmp, bottom_top_blob, 16, opt);
54
55 return 0;
56 }
57#endif // __AVX512F__
58
59 if (elempack == 8)
60 {
61 int w = bottom_top_blob.w;
62 int h = bottom_top_blob.h;
63 int channels = bottom_top_blob.c;
64 int size = w * h;
65
66 __m256 _scale = _mm256_set1_ps(scale);
67
68 if (dims == 1)
69 {
70 #pragma omp parallel for num_threads(opt.num_threads)
71 for (int i = 0; i < w; i++)
72 {
73 float* ptr = (float*)bottom_top_blob + i * 8;
74 __m256 _p = _mm256_loadu_ps(ptr);
75 _p = _mm256_mul_ps(_p, _scale);
76 _mm256_storeu_ps(ptr, _p);
77 }
78 }
79
80 if (dims == 2)
81 {
82 #pragma omp parallel for num_threads(opt.num_threads)
83 for (int i = 0; i < h; i++)
84 {
85 float* ptr = bottom_top_blob.row(i);
86
87 for (int j = 0; j < w; j++)
88 {
89 __m256 _p = _mm256_loadu_ps(ptr);
90 _p = _mm256_mul_ps(_p, _scale);
91 _mm256_storeu_ps(ptr, _p);
92 ptr += 8;
93 }
94 }
95 }
96
97 if (dims == 3)
98 {
99 #pragma omp parallel for num_threads(opt.num_threads)
100 for (int q = 0; q < channels; q++)
101 {
102 float* ptr = bottom_top_blob.channel(q);
103
104 for (int i = 0; i < size; i++)
105 {
106 __m256 _p = _mm256_loadu_ps(ptr);
107 _p = _mm256_mul_ps(_p, _scale);
108 _mm256_storeu_ps(ptr, _p);
109 ptr += 8;
110 }
111 }
112 }
113
114 return 0;
115 }
116#endif // __AVX__
117
118 if (elempack == 4)
119 {
120 int w = bottom_top_blob.w;
121 int h = bottom_top_blob.h;
122 int channels = bottom_top_blob.c;
123 int size = w * h;
124
125 __m128 _scale = _mm_set1_ps(scale);
126
127 if (dims == 1)
128 {
129 #pragma omp parallel for num_threads(opt.num_threads)
130 for (int i = 0; i < w; i++)
131 {
132 float* ptr = (float*)bottom_top_blob + i * 4;
133 __m128 _p = _mm_loadu_ps(ptr);
134 _p = _mm_mul_ps(_p, _scale);
135 _mm_storeu_ps(ptr, _p);
136 }
137 }
138
139 if (dims == 2)
140 {
141 #pragma omp parallel for num_threads(opt.num_threads)
142 for (int i = 0; i < h; i++)
143 {
144 float* ptr = bottom_top_blob.row(i);
145
146 for (int j = 0; j < w; j++)
147 {
148 __m128 _p = _mm_loadu_ps(ptr);
149 _p = _mm_mul_ps(_p, _scale);
150 _mm_storeu_ps(ptr, _p);
151 ptr += 4;
152 }
153 }
154 }
155
156 if (dims == 3)
157 {
158 #pragma omp parallel for num_threads(opt.num_threads)
159 for (int q = 0; q < channels; q++)
160 {
161 float* ptr = bottom_top_blob.channel(q);
162
163 for (int i = 0; i < size; i++)
164 {
165 __m128 _p = _mm_loadu_ps(ptr);
166 _p = _mm_mul_ps(_p, _scale);
167 _mm_storeu_ps(ptr, _p);
168 ptr += 4;
169 }
170 }
171 }
172
173 return 0;
174 }
175#endif // __SSE2__
176
177 return Dropout::forward_inplace(bottom_top_blob, opt);
178}
179
180} // namespace ncnn