Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 7.8 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 "softmax.h"
16
17#include <float.h>
18
19namespace ncnn {
20
21Softmax::Softmax()
22{
23 one_blob_only = true;
24 support_inplace = true;
25}
26
27int Softmax::load_param(const ParamDict& pd)
28{
29 axis = pd.get(0, 0);
30
31 // the original softmax handle axis on 3-dim blob incorrectly
32 // ask user to regenerate param instead of producing wrong result
33 int fixbug0 = pd.get(1, 0);
34 if (fixbug0 == 0 && axis != 0)
35 {
36 NCNN_LOGE("param is too old, please regenerate!");
37 return -1;
38 }
39
40 return 0;
41}
42
43int Softmax::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
44{
45 // value = expf( value - global max value )
46 // sum all value
47 // value = value / sum
48
49 int dims = bottom_top_blob.dims;
50 size_t elemsize = bottom_top_blob.elemsize;
51 int positive_axis = axis < 0 ? dims + axis : axis;
52
53 if (dims == 1) // positive_axis == 0
54 {
55 int w = bottom_top_blob.w;
56
57 float* ptr = bottom_top_blob;
58
59 float max = -FLT_MAX;
60 for (int i = 0; i < w; i++)
61 {
62 max = std::max(max, ptr[i]);
63 }
64
65 float sum = 0.f;
66 for (int i = 0; i < w; i++)
67 {
68 ptr[i] = expf(ptr[i] - max);
69 sum += ptr[i];
70 }
71
72 for (int i = 0; i < w; i++)
73 {
74 ptr[i] /= sum;
75 }
76 }
77
78 if (dims == 2 && positive_axis == 0)
79 {
80 int w = bottom_top_blob.w;
81 int h = bottom_top_blob.h;
82
83 Mat max;
84 max.create(w, elemsize, opt.workspace_allocator);
85 if (max.empty())
86 return -100;
87 max.fill(-FLT_MAX);
88
89 for (int i = 0; i < h; i++)
90 {
91 const float* ptr = bottom_top_blob.row(i);
92 for (int j = 0; j < w; j++)
93 {
94 max[j] = std::max(max[j], ptr[j]);
95 }
96 }
97
98 Mat sum;
99 sum.create(w, elemsize, opt.workspace_allocator);
100 if (sum.empty())
101 return -100;
102 sum.fill(0.f);
103
104 for (int i = 0; i < h; i++)
105 {
106 float* ptr = bottom_top_blob.row(i);
107 for (int j = 0; j < w; j++)
108 {
109 ptr[j] = expf(ptr[j] - max[j]);
110 sum[j] += ptr[j];
111 }
112 }
113
114 for (int i = 0; i < h; i++)
115 {
116 float* ptr = bottom_top_blob.row(i);
117 for (int j = 0; j < w; j++)
118 {
119 ptr[j] /= sum[j];
120 }
121 }
122 }
123
124 if (dims == 2 && positive_axis == 1)
125 {
126 int w = bottom_top_blob.w;
127 int h = bottom_top_blob.h;
128
129 for (int i = 0; i < h; i++)
130 {
131 float* ptr = bottom_top_blob.row(i);
132 float m = -FLT_MAX;
133 for (int j = 0; j < w; j++)
134 {
135 m = std::max(m, ptr[j]);
136 }
137
138 float s = 0.f;
139 for (int j = 0; j < w; j++)
140 {
141 ptr[j] = expf(ptr[j] - m);
142 s += ptr[j];
143 }
144
145 for (int j = 0; j < w; j++)
146 {
147 ptr[j] /= s;
148 }
149 }
150 }
151
152 if (dims == 3 && positive_axis == 0)
153 {
154 int w = bottom_top_blob.w;
155 int h = bottom_top_blob.h;
156 int channels = bottom_top_blob.c;
157 int size = w * h;
158
159 Mat max;
160 max.create(w, h, elemsize, opt.workspace_allocator);
161 if (max.empty())
162 return -100;
163 max.fill(-FLT_MAX);
164 for (int q = 0; q < channels; q++)
165 {
166 const float* ptr = bottom_top_blob.channel(q);
167
168 for (int i = 0; i < size; i++)
169 {
170 max[i] = std::max(max[i], ptr[i]);
171 }
172 }
173
174 Mat sum;
175 sum.create(w, h, elemsize, opt.workspace_allocator);
176 if (sum.empty())
177 return -100;
178 sum.fill(0.f);
179 for (int q = 0; q < channels; q++)
180 {
181 float* ptr = bottom_top_blob.channel(q);
182
183 for (int i = 0; i < size; i++)
184 {
185 ptr[i] = expf(ptr[i] - max[i]);
186 sum[i] += ptr[i];
187 }
188 }
189
190 #pragma omp parallel for num_threads(opt.num_threads)
191 for (int q = 0; q < channels; q++)
192 {
193 float* ptr = bottom_top_blob.channel(q);
194
195 for (int i = 0; i < size; i++)
196 {
197 ptr[i] /= sum[i];
198 }
199 }
200 }
201
202 if (dims == 3 && positive_axis == 1)
203 {
204 int w = bottom_top_blob.w;
205 int h = bottom_top_blob.h;
206 int channels = bottom_top_blob.c;
207
208 Mat max;
209 max.create(w, channels, elemsize, opt.workspace_allocator);
210 if (max.empty())
211 return -100;
212 max.fill(-FLT_MAX);
213 #pragma omp parallel for num_threads(opt.num_threads)
214 for (int q = 0; q < channels; q++)
215 {
216 const float* ptr = bottom_top_blob.channel(q);
217 float* maxptr = max.row(q);
218
219 for (int i = 0; i < h; i++)
220 {
221 for (int j = 0; j < w; j++)
222 {
223 maxptr[j] = std::max(maxptr[j], ptr[j]);
224 }
225
226 ptr += w;
227 }
228 }
229
230 Mat sum;
231 sum.create(w, channels, elemsize, opt.workspace_allocator);
232 if (sum.empty())
233 return -100;
234 sum.fill(0.f);
235 #pragma omp parallel for num_threads(opt.num_threads)
236 for (int q = 0; q < channels; q++)
237 {
238 float* ptr = bottom_top_blob.channel(q);
239 float* maxptr = max.row(q);
240 float* sumptr = sum.row(q);
241
242 for (int i = 0; i < h; i++)
243 {
244 for (int j = 0; j < w; j++)
245 {
246 ptr[j] = expf(ptr[j] - maxptr[j]);
247 sumptr[j] += ptr[j];
248 }
249
250 ptr += w;
251 }
252 }
253
254 #pragma omp parallel for num_threads(opt.num_threads)
255 for (int q = 0; q < channels; q++)
256 {
257 float* ptr = bottom_top_blob.channel(q);
258 float* sumptr = sum.row(q);
259
260 for (int i = 0; i < h; i++)
261 {
262 for (int j = 0; j < w; j++)
263 {
264 ptr[j] /= sumptr[j];
265 }
266
267 ptr += w;
268 }
269 }
270 }
271
272 if (dims == 3 && positive_axis == 2)
273 {
274 int w = bottom_top_blob.w;
275 int h = bottom_top_blob.h;
276 int channels = bottom_top_blob.c;
277
278 #pragma omp parallel for num_threads(opt.num_threads)
279 for (int q = 0; q < channels; q++)
280 {
281 float* ptr = bottom_top_blob.channel(q);
282
283 for (int i = 0; i < h; i++)
284 {
285 float max = -FLT_MAX;
286 for (int j = 0; j < w; j++)
287 {
288 max = std::max(max, ptr[j]);
289 }
290
291 float sum = 0.f;
292 for (int j = 0; j < w; j++)
293 {
294 ptr[j] = expf(ptr[j] - max);
295 sum += ptr[j];
296 }
297
298 for (int j = 0; j < w; j++)
299 {
300 ptr[j] /= sum;
301 }
302
303 ptr += w;
304 }
305 }
306 }
307
308 return 0;
309}
310
311} // namespace ncnn