1
// Leo is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2020 Leo <leo@nullptr.com.cn>. 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_mips.h"
21
#include "msa_mathfun.h"
26
int Softmax_mips::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);
76
v4f32 _p = (v4f32)__msa_ld_w(ptr, 0);
77
v4f32 _max = (v4f32)__msa_ld_w(maxptr, 0);
79
_p = exp_ps(__msa_fsub_w(_p, _max));
81
__msa_st_w((v4i32)_p, ptr, 0);
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);
117
v4f32 _p = (v4f32)__msa_ld_w(ptr, 0);
118
v4f32 _sum = (v4f32)__msa_ld_w(sumptr, 0);
119
_sum = __msa_fadd_w(_sum, _p);
120
__msa_st_w((v4i32)_sum, sumptr, 0);
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);
152
v4f32 _p = (v4f32)__msa_ld_w(ptr, 0);
153
v4f32 _sum = (v4f32)__msa_ld_w(sumptr, 0);
154
_p = __msa_fdiv_w(_p, _sum);
155
__msa_st_w((v4i32)_p, ptr, 0);
162
for (; remain > 0; remain--)