Elffuss BraidHub

kikocis/ncnn public

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