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