| 1 | // Xavier Hsinyuan is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 Xavier Hsinyuan <thelastlinex@hotmail.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 "relu_riscv.h" |
| 16 | |
| 17 | #if __riscv_vector |
| 18 | #include <riscv_vector.h> |
| 19 | #endif // __riscv_vector |
| 20 | |
| 21 | namespace ncnn { |
| 22 | |
| 23 | ReLU_riscv::ReLU_riscv() |
| 24 | { |
| 25 | #if __riscv_vector |
| 26 | support_packing = true; |
| 27 | #if __riscv_zfh |
| 28 | support_fp16_storage = true; |
| 29 | #endif |
| 30 | #endif |
| 31 | } |
| 32 | |
| 33 | int ReLU_riscv::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 34 | { |
| 35 | #if __riscv_vector && __riscv_zfh |
| 36 | int elembits = bottom_top_blob.elembits(); |
| 37 | |
| 38 | if (opt.use_fp16_storage && elembits == 16) |
| 39 | { |
| 40 | return forward_inplace_fp16s(bottom_top_blob, opt); |
| 41 | } |
| 42 | #endif |
| 43 | |
| 44 | int w = bottom_top_blob.w; |
| 45 | int h = bottom_top_blob.h; |
| 46 | int d = bottom_top_blob.d; |
| 47 | int channels = bottom_top_blob.c; |
| 48 | int elempack = bottom_top_blob.elempack; |
| 49 | int size = w * h * d * elempack; |
| 50 | |
| 51 | #pragma omp parallel for num_threads(opt.num_threads) |
| 52 | for (int q = 0; q < channels; q++) |
| 53 | { |
| 54 | float* ptr = bottom_top_blob.channel(q); |
| 55 | if (slope == 0.f) |
| 56 | { |
| 57 | #if __riscv_vector |
| 58 | int n = size; |
| 59 | while (n > 0) |
| 60 | { |
| 61 | size_t vl = vsetvl_e32m8(n); |
| 62 | |
| 63 | vfloat32m8_t _p = vle32_v_f32m8(ptr, vl); |
| 64 | _p = vfmax_vf_f32m8(_p, 0.f, vl); |
| 65 | vse32_v_f32m8(ptr, _p, vl); |
| 66 | |
| 67 | ptr += vl; |
| 68 | n -= vl; |
| 69 | } |
| 70 | #else // __riscv_vector |
| 71 | for (int i = 0; i < size; i++) |
| 72 | { |
| 73 | if (*ptr < 0) |
| 74 | *ptr = 0; |
| 75 | ptr++; |
| 76 | } |
| 77 | #endif // __riscv_vector |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | #if __riscv_vector |
| 82 | int n = size; |
| 83 | while (n > 0) |
| 84 | { |
| 85 | size_t vl = vsetvl_e32m8(n); |
| 86 | |
| 87 | vfloat32m8_t _p = vle32_v_f32m8(ptr, vl); |
| 88 | _p = vfmul_vf_f32m8_m(vmflt_vf_f32m8_b4(_p, .0f, vl), _p, _p, slope, vl); //slope: float(float32_t) |
| 89 | vse32_v_f32m8(ptr, _p, vl); |
| 90 | |
| 91 | ptr += vl; |
| 92 | n -= vl; |
| 93 | } |
| 94 | #else // __riscv_vector |
| 95 | for (int i = 0; i < size; i++) |
| 96 | { |
| 97 | if (*ptr < 0) |
| 98 | *ptr *= slope; |
| 99 | ptr++; |
| 100 | } |
| 101 | #endif // __riscv_vector |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | #if __riscv_vector && __riscv_zfh |
| 109 | int ReLU_riscv::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const |
| 110 | { |
| 111 | int w = bottom_top_blob.w; |
| 112 | int h = bottom_top_blob.h; |
| 113 | int d = bottom_top_blob.d; |
| 114 | int channels = bottom_top_blob.c; |
| 115 | int elempack = bottom_top_blob.elempack; |
| 116 | int size = w * h * d * elempack; |
| 117 | |
| 118 | #pragma omp parallel for num_threads(opt.num_threads) |
| 119 | for (int q = 0; q < channels; q++) |
| 120 | { |
| 121 | __fp16* ptr = bottom_top_blob.channel(q); |
| 122 | if (slope == 0.f) |
| 123 | { |
| 124 | int n = size; |
| 125 | while (n > 0) |
| 126 | { |
| 127 | size_t vl = vsetvl_e16m8(n); |
| 128 | |
| 129 | vfloat16m8_t _p = vle16_v_f16m8(ptr, vl); |
| 130 | _p = vfmax_vf_f16m8(_p, (__fp16)0.f, vl); |
| 131 | vse16_v_f16m8(ptr, _p, vl); |
| 132 | |
| 133 | ptr += vl; |
| 134 | n -= vl; |
| 135 | } |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | int n = size; |
| 140 | __fp16 _slope = (__fp16)slope; |
| 141 | while (n > 0) |
| 142 | { |
| 143 | size_t vl = vsetvl_e16m8(n); |
| 144 | |
| 145 | vfloat16m8_t _p = vle16_v_f16m8(ptr, vl); |
| 146 | _p = vfmul_vf_f16m8_m(vmflt_vf_f16m8_b2(_p, .0f, vl), _p, _p, _slope, vl); |
| 147 | vse16_v_f16m8(ptr, _p, vl); |
| 148 | |
| 149 | ptr += vl; |
| 150 | n -= vl; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | #endif |
| 159 | } // namespace ncnn |