| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 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 | #ifndef RISCV_ACTIVATION_H |
| 16 | #define RISCV_ACTIVATION_H |
| 17 | |
| 18 | #include "fused_activation.h" |
| 19 | |
| 20 | #if __riscv_vector |
| 21 | #include <riscv_vector.h> |
| 22 | #include "rvv_mathfun.h" |
| 23 | #include "rvv_mathfun_fp16s.h" |
| 24 | |
| 25 | #define _RVV_FLOAT_ACTIVATION_PS(SEW, LMUL, MLEN) \ |
| 26 | static inline vfloat##SEW##m##LMUL##_t activation_ps(vfloat##SEW##m##LMUL##_t _v, int activation_type, const ncnn::Mat& activation_params, size_t vl) \ |
| 27 | { \ |
| 28 | if (activation_type == 1) \ |
| 29 | { \ |
| 30 | _v = vfmax_vf_f##SEW##m##LMUL(_v, 0.f, vl); \ |
| 31 | } \ |
| 32 | else if (activation_type == 2) \ |
| 33 | { \ |
| 34 | vbool##MLEN##_t _lemask = vmfle_vf_f##SEW##m##LMUL##_b##MLEN(_v, 0.f, vl); \ |
| 35 | _v = vfmul_vf_f##SEW##m##LMUL##_m(_lemask, _v, _v, activation_params[0], vl); \ |
| 36 | } \ |
| 37 | else if (activation_type == 3) \ |
| 38 | { \ |
| 39 | _v = vfmax_vf_f##SEW##m##LMUL(_v, activation_params[0], vl); \ |
| 40 | _v = vfmin_vf_f##SEW##m##LMUL(_v, activation_params[1], vl); \ |
| 41 | } \ |
| 42 | else if (activation_type == 4) \ |
| 43 | { \ |
| 44 | _v = sigmoid_ps(_v, vl); \ |
| 45 | } \ |
| 46 | else if (activation_type == 5) \ |
| 47 | { \ |
| 48 | _v = vfmul_vv_f##SEW##m##LMUL(_v, tanh_ps(log_ps(vfadd_vf_f##SEW##m##LMUL(exp_ps(_v, vl), 1.f, vl), vl), vl), vl); \ |
| 49 | } \ |
| 50 | else if (activation_type == 6) \ |
| 51 | { \ |
| 52 | const float alpha = activation_params[0]; \ |
| 53 | const float beta = activation_params[1]; \ |
| 54 | const float lower = -beta / alpha; \ |
| 55 | const float upper = (1.f / alpha) + lower; \ |
| 56 | vbool##MLEN##_t _lower = vmflt_vf_f##SEW##m##LMUL##_b##MLEN(_v, lower, vl); \ |
| 57 | vbool##MLEN##_t _higher = vmfgt_vf_f##SEW##m##LMUL##_b##MLEN(_v, upper, vl); \ |
| 58 | vbool##MLEN##_t _apply = vmnor_mm_b##MLEN(_lower, _higher, vl); \ |
| 59 | _v = vfmerge_vfm_f##SEW##m##LMUL(_lower, _v, .0f, vl); \ |
| 60 | \ |
| 61 | vfloat##SEW##m##LMUL##_t _p0 = vfadd_vf_f##SEW##m##LMUL##_m( \ |
| 62 | _apply, _v, /*op1*/ vfmul_vf_f##SEW##m##LMUL##_m(_apply, _v, _v, alpha, vl), beta, \ |
| 63 | vl); \ |
| 64 | _v = vfmul_vv_f##SEW##m##LMUL##_m(_apply, _v, /*op1*/ _v, _p0, vl); \ |
| 65 | } \ |
| 66 | \ |
| 67 | return _v; \ |
| 68 | } |
| 69 | |
| 70 | _RVV_FLOAT_ACTIVATION_PS(16, 1, 16) |
| 71 | _RVV_FLOAT_ACTIVATION_PS(16, 2, 8) |
| 72 | _RVV_FLOAT_ACTIVATION_PS(16, 4, 4) |
| 73 | _RVV_FLOAT_ACTIVATION_PS(16, 8, 2) |
| 74 | _RVV_FLOAT_ACTIVATION_PS(32, 1, 32) |
| 75 | _RVV_FLOAT_ACTIVATION_PS(32, 2, 16) |
| 76 | _RVV_FLOAT_ACTIVATION_PS(32, 4, 8) |
| 77 | _RVV_FLOAT_ACTIVATION_PS(32, 8, 4) |
| 78 | |
| 79 | #endif // __riscv_vector |
| 80 | |
| 81 | #endif // RISCV_ACTIVATION_H |