| 1 | // Xavier Hsinyuan is pleased to support the open source community by making |
| 2 | // ncnn available. |
| 3 | // |
| 4 | // Copyright (C) 2021 Xavier Hsinyuan <me@lstlx.com>. All rights reserved. |
| 5 | // |
| 6 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this |
| 7 | // file except in compliance with the License. You may obtain a copy of the |
| 8 | // License at |
| 9 | // |
| 10 | // https://opensource.org/licenses/BSD-3-Clause |
| 11 | // |
| 12 | // Unless required by applicable law or agreed to in writing, software |
| 13 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | // License for the specific language governing permissions and limitations under |
| 16 | // the License. |
| 17 | |
| 18 | #include "hardsigmoid_riscv.h" |
| 19 | |
| 20 | #if __riscv_vector |
| 21 | #include <riscv_vector.h> |
| 22 | #endif // __riscv_vector |
| 23 | |
| 24 | namespace ncnn { |
| 25 | |
| 26 | HardSigmoid_riscv::HardSigmoid_riscv() |
| 27 | { |
| 28 | #if __riscv_vector |
| 29 | support_packing = true; |
| 30 | #if __riscv_zfh |
| 31 | support_fp16_storage = true; |
| 32 | #endif |
| 33 | #endif // __riscv_vector |
| 34 | } |
| 35 | |
| 36 | int HardSigmoid_riscv::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 37 | { |
| 38 | #if __riscv_vector && __riscv_zfh |
| 39 | int elembits = bottom_top_blob.elembits(); |
| 40 | |
| 41 | if (opt.use_fp16_storage && elembits == 16) |
| 42 | { |
| 43 | return forward_inplace_fp16s(bottom_top_blob, opt); |
| 44 | } |
| 45 | #endif |
| 46 | |
| 47 | int w = bottom_top_blob.w; |
| 48 | int h = bottom_top_blob.h; |
| 49 | int d = bottom_top_blob.d; |
| 50 | int channels = bottom_top_blob.c; |
| 51 | int elempack = bottom_top_blob.elempack; |
| 52 | int size = w * h * d * elempack; |
| 53 | |
| 54 | #pragma omp parallel for num_threads(opt.num_threads) |
| 55 | for (int q = 0; q < channels; q++) |
| 56 | { |
| 57 | float* ptr = bottom_top_blob.channel(q); |
| 58 | |
| 59 | #if __riscv_vector |
| 60 | int n = size; |
| 61 | while (n > 0) |
| 62 | { |
| 63 | size_t vl = vsetvl_e32m8(n); |
| 64 | vfloat32m8_t _p = vle32_v_f32m8(ptr, vl); |
| 65 | |
| 66 | vbool4_t _lower = vmflt_vf_f32m8_b4(_p, lower, vl); |
| 67 | vbool4_t _higher = vmfgt_vf_f32m8_b4(_p, upper, vl); |
| 68 | vbool4_t _apply = vmnor_mm_b4(_lower, _higher, vl); |
| 69 | _p = vfmerge_vfm_f32m8(_lower, _p, .0f, vl); |
| 70 | _p = vfmerge_vfm_f32m8(_higher, _p, 1.f, vl); |
| 71 | |
| 72 | _p = vfadd_vf_f32m8_m(_apply, _p, |
| 73 | /*op1*/ vfmul_vf_f32m8_m(_apply, _p, _p, alpha, vl), |
| 74 | beta, vl); |
| 75 | |
| 76 | vse32_v_f32m8(ptr, _p, vl); |
| 77 | ptr += vl; |
| 78 | n -= vl; |
| 79 | } |
| 80 | #else // __riscv_vector |
| 81 | for (int i = 0; i < size; i++) |
| 82 | { |
| 83 | if (ptr[i] < lower) |
| 84 | ptr[i] = 0.f; |
| 85 | else if (ptr[i] > upper) |
| 86 | ptr[i] = 1.f; |
| 87 | else |
| 88 | ptr[i] = ptr[i] * alpha + beta; |
| 89 | } |
| 90 | #endif // __riscv_vector |
| 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | #if __riscv_vector && __riscv_zfh |
| 97 | int HardSigmoid_riscv::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const |
| 98 | { |
| 99 | int w = bottom_top_blob.w; |
| 100 | int h = bottom_top_blob.h; |
| 101 | int d = bottom_top_blob.d; |
| 102 | int channels = bottom_top_blob.c; |
| 103 | int elempack = bottom_top_blob.elempack; |
| 104 | int size = w * h * d * elempack; |
| 105 | |
| 106 | #pragma omp parallel for num_threads(opt.num_threads) |
| 107 | for (int q = 0; q < channels; q++) |
| 108 | { |
| 109 | __fp16* ptr = bottom_top_blob.channel(q); |
| 110 | |
| 111 | int n = size; |
| 112 | while (n > 0) |
| 113 | { |
| 114 | size_t vl = vsetvl_e16m8(n); |
| 115 | vfloat16m8_t _p = vle16_v_f16m8(ptr, vl); |
| 116 | |
| 117 | vbool2_t _lower = vmflt_vf_f16m8_b2(_p, lower, vl); |
| 118 | vbool2_t _higher = vmfgt_vf_f16m8_b2(_p, upper, vl); |
| 119 | vbool2_t _apply = vmnor_mm_b2(_lower, _higher, vl); |
| 120 | _p = vfmerge_vfm_f16m8(_lower, _p, .0f, vl); |
| 121 | _p = vfmerge_vfm_f16m8(_higher, _p, 1.f, vl); |
| 122 | |
| 123 | _p = vfadd_vf_f16m8_m( |
| 124 | _apply, _p, |
| 125 | /*op1*/ vfmul_vf_f16m8_m(_apply, _p, /*op1*/ _p, alpha, vl), beta, |
| 126 | vl); |
| 127 | vse16_v_f16m8(ptr, _p, vl); |
| 128 | ptr += vl; |
| 129 | n -= vl; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | } // namespace ncnn |