Elffuss BraidHub

kikocis/ncnn public

master · 6.1 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 "lrn_x86.h"
16
17#if __AVX__
18#include "avx_mathfun.h"
19#endif // __AVX__
20
21namespace ncnn {
22
23int LRN_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
24{
25 int w = bottom_top_blob.w;
26 int h = bottom_top_blob.h;
27 int channels = bottom_top_blob.c;
28 size_t elemsize = bottom_top_blob.elemsize;
29 int size = w * h;
30
31 // squared values with local_size padding
32 Mat square_blob;
33 square_blob.create(w, h, channels, elemsize, opt.workspace_allocator);
34 if (square_blob.empty())
35 return -100;
36
37 #pragma omp parallel for num_threads(opt.num_threads)
38 for (int q = 0; q < channels; q++)
39 {
40 const float* ptr = bottom_top_blob.channel(q);
41 float* outptr = square_blob.channel(q);
42
43 int i = 0;
44#if __AVX__
45 for (; i + 7 < size; i += 8)
46 {
47 __m256 _p = _mm256_loadu_ps(ptr);
48 __m256 _outp = _mm256_mul_ps(_p, _p);
49 _mm256_storeu_ps(outptr, _outp);
50
51 ptr += 8;
52 outptr += 8;
53 }
54#endif // __AVX__
55 for (; i < size; i++)
56 {
57 *outptr = *ptr * *ptr;
58
59 ptr++;
60 outptr++;
61 }
62 }
63
64 if (region_type == NormRegion_ACROSS_CHANNELS)
65 {
66 Mat square_sum;
67 square_sum.create(w, h, channels, elemsize, opt.workspace_allocator);
68 if (square_sum.empty())
69 return -100;
70 square_sum.fill(0.f);
71
72 const float alpha_div_size = alpha / local_size;
73
74 #pragma omp parallel for num_threads(opt.num_threads)
75 for (int q = 0; q < channels; q++)
76 {
77 // square sum
78 for (int p = q - local_size / 2; p <= q + local_size / 2; p++)
79 {
80 if (p < 0 || p >= channels)
81 continue;
82
83 const float* sptr = square_blob.channel(p);
84 float* ssptr = square_sum.channel(q);
85
86 int i = 0;
87#if __AVX__
88 for (; i + 7 < size; i += 8)
89 {
90 __m256 _sp = _mm256_loadu_ps(sptr);
91 __m256 _ssp = _mm256_loadu_ps(ssptr);
92 _ssp = _mm256_add_ps(_ssp, _sp);
93 _mm256_storeu_ps(ssptr, _ssp);
94
95 sptr += 8;
96 ssptr += 8;
97 }
98#endif // __AVX__
99 for (; i < size; i++)
100 {
101 *ssptr += *sptr;
102 sptr++;
103 ssptr++;
104 }
105 }
106
107 float* ptr = bottom_top_blob.channel(q);
108 float* ssptr = square_sum.channel(q);
109
110 int i = 0;
111#if __AVX__
112 __m256 _bias = _mm256_set1_ps(bias);
113 __m256 _ads = _mm256_set1_ps(alpha_div_size);
114 __m256 _mb = _mm256_set1_ps(-beta);
115 for (; i + 7 < size; i += 8)
116 {
117 __m256 _p = _mm256_loadu_ps(ptr);
118 __m256 _ssp = _mm256_loadu_ps(ssptr);
119 _ssp = _mm256_mul_ps(_ssp, _ads);
120 _ssp = _mm256_add_ps(_ssp, _bias);
121 _ssp = pow256_ps(_ssp, _mb);
122 _p = _mm256_mul_ps(_p, _ssp);
123 _mm256_storeu_ps(ptr, _p);
124
125 ssptr += 8;
126 ptr += 8;
127 }
128#endif // __AVX__
129 for (; i < size; i++)
130 {
131 *ptr = *ptr * powf(bias + alpha_div_size * *ssptr, -beta);
132
133 ssptr++;
134 ptr++;
135 }
136 }
137 }
138 else if (region_type == NormRegion_WITHIN_CHANNEL)
139 {
140 int outw = w;
141 int outh = h;
142
143 Mat square_blob_bordered = square_blob;
144 int pad = local_size / 2;
145 if (pad > 0)
146 {
147 Option opt_b = opt;
148 opt_b.blob_allocator = opt.workspace_allocator;
149 copy_make_border(square_blob, square_blob_bordered, pad, local_size - pad - 1, pad, local_size - pad - 1, BORDER_CONSTANT, 0.f, opt_b);
150 if (square_blob_bordered.empty())
151 return -100;
152
153 w = square_blob_bordered.w;
154 h = square_blob_bordered.h;
155 }
156
157 const int maxk = local_size * local_size;
158
159 const float alpha_div_size = alpha / maxk;
160
161 // norm window offsets
162 std::vector<int> _space_ofs(maxk);
163 int* space_ofs = &_space_ofs[0];
164 {
165 int p1 = 0;
166 int p2 = 0;
167 int gap = w - local_size;
168 for (int i = 0; i < local_size; i++)
169 {
170 for (int j = 0; j < local_size; j++)
171 {
172 space_ofs[p1] = p2;
173 p1++;
174 p2++;
175 }
176 p2 += gap;
177 }
178 }
179
180 #pragma omp parallel for num_threads(opt.num_threads)
181 for (int q = 0; q < channels; q++)
182 {
183 float* ptr = bottom_top_blob.channel(q);
184 const Mat m = square_blob_bordered.channel(q);
185
186 for (int i = 0; i < outh; i++)
187 {
188 for (int j = 0; j < outw; j++)
189 {
190 const float* sptr = m.row(i) + j;
191
192 float ss = 0.f;
193
194 for (int k = 0; k < maxk; k++)
195 {
196 float val = sptr[space_ofs[k]];
197 ss += val;
198 }
199
200 ptr[j] = ptr[j] * powf(bias + alpha_div_size * ss, -beta);
201 }
202
203 ptr += outw;
204 }
205 }
206 }
207
208 return 0;
209}
210
211} // namespace ncnn