1
// yala is pleased to support the open source community by making ncnn available.
4
// Copyright (C) 2022 yala <zhaojunchao@loongson.cn>;<junchao82@qq.com>. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
15
#include "softmax_loongarch.h"
21
#include "lsx_mathfun.h"
22
#endif // __loongarch_sx
26
int Softmax_loongarch::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
28
int dims = bottom_top_blob.dims;
29
size_t elemsize = bottom_top_blob.elemsize;
30
int positive_axis = axis < 0 ? dims + axis : axis;
32
if (dims != 3 || positive_axis != 0)
33
return Softmax::forward_inplace(bottom_top_blob, opt);
35
// value = exp( value - global max value )
37
// value = value / sum
39
int w = bottom_top_blob.w;
40
int h = bottom_top_blob.h;
41
int channels = bottom_top_blob.c;
45
max.create(w, h, elemsize, opt.workspace_allocator);
49
for (int q = 0; q < channels; q++)
51
float* ptr = bottom_top_blob.channel(q);
54
for (int i = 0; i < size; i++)
56
maxptr[i] = std::max(maxptr[i], ptr[i]);
60
#pragma omp parallel for num_threads(opt.num_threads)
61
for (int q = 0; q < channels; q++)
63
float* ptr = bottom_top_blob.channel(q);
68
int remain = size - (nn << 2);
71
#endif // __loongarch_sx
76
__m128 _p = (__m128)__lsx_vld(ptr, 0);
77
__m128 _max = (__m128)__lsx_vld(maxptr, 0);
79
_p = exp_ps(__lsx_vfsub_s(_p, _max));
81
__lsx_vst(_p, ptr, 0);
86
#endif // __loongarch_sx
88
for (; remain > 0; remain--)
90
*ptr = exp(*ptr - *maxptr);
98
sum.create(w, h, elemsize, opt.workspace_allocator);
102
for (int q = 0; q < channels; q++)
104
float* ptr = bottom_top_blob.channel(q);
109
int remain = size - (nn << 2);
112
#endif // __loongarch_sx
117
__m128 _p = (__m128)__lsx_vld(ptr, 0);
118
__m128 _sum = (__m128)__lsx_vld(sumptr, 0);
119
_sum = __lsx_vfadd_s(_sum, _p);
120
__lsx_vst(_sum, sumptr, 0);
125
#endif // __loongarch_sx
127
for (; remain > 0; remain--)
136
#pragma omp parallel for num_threads(opt.num_threads)
137
for (int q = 0; q < channels; q++)
139
float* ptr = bottom_top_blob.channel(q);
144
int remain = size - (nn << 2);
147
#endif // __loongarch_sx
152
__m128 _p = (__m128)__lsx_vld(ptr, 0);
153
__m128 _sum = (__m128)__lsx_vld(sumptr, 0);
154
_p = __lsx_vfdiv_s(_p, _sum);
155
__lsx_vst(_p, ptr, 0);
160
#endif // __loongarch_sx
162
for (; remain > 0; remain--)