| 1 | // Xavier Hsinyuan is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 Xavier Hsinyuan <me@lstlx.com>. 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 "selu_riscv.h" |
| 16 | |
| 17 | #if __riscv_vector |
| 18 | #include <riscv_vector.h> |
| 19 | #include "rvv_mathfun.h" |
| 20 | #endif // __riscv_vector |
| 21 | |
| 22 | namespace ncnn { |
| 23 | |
| 24 | int SELU_riscv::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 d = bottom_top_blob.d; |
| 29 | int channels = bottom_top_blob.c; |
| 30 | int elempack = bottom_top_blob.elempack; |
| 31 | int size = w * h * d * elempack; |
| 32 | |
| 33 | float alphaxlambda = alpha * lambda; |
| 34 | #pragma omp parallel for num_threads(opt.num_threads) |
| 35 | for (int q = 0; q < channels; q++) |
| 36 | { |
| 37 | float* ptr = bottom_top_blob.channel(q); |
| 38 | #if __riscv_vector |
| 39 | int n = size; |
| 40 | while (n > 0) |
| 41 | { |
| 42 | size_t vl = vsetvl_e32m8(n); |
| 43 | vfloat32m8_t _p = vle32_v_f32m8(ptr, vl); |
| 44 | vbool4_t _lower = vmflt_vf_f32m8_b4(_p, 0.f, vl); |
| 45 | vbool4_t _higher = vmnot_m_b4(_lower, vl); |
| 46 | |
| 47 | _p = vfmul_vf_f32m8_m(_higher, _p, /*op1*/ _p, lambda, vl); |
| 48 | vfloat32m8_t _nps = exp_ps(_p, vl); |
| 49 | _nps = vfsub_vf_f32m8_m(_lower, _p, /*op1*/ _nps, 1.f, vl); |
| 50 | _nps = vfmul_vf_f32m8_m(_lower, _p, /*op1*/ _nps, alphaxlambda, vl); |
| 51 | |
| 52 | vse32_v_f32m8(ptr, _nps, vl); |
| 53 | ptr += vl; |
| 54 | n -= vl; |
| 55 | } |
| 56 | #else |
| 57 | for (int i = 0; i < size; i++) |
| 58 | { |
| 59 | if (ptr[i] < 0.f) |
| 60 | ptr[i] = (expf(ptr[i]) - 1.f) * alphaxlambda; |
| 61 | else |
| 62 | ptr[i] *= lambda; |
| 63 | } |
| 64 | #endif // __riscv_vector |
| 65 | } |
| 66 | return 0; |
| 67 | }; |
| 68 | |
| 69 | } // namespace ncnn |