| 1 | // Xavier Hsinyuan is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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 "gelu_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 | GELU_riscv::GELU_riscv() |
| 25 | { |
| 26 | #if __riscv_vector |
| 27 | support_packing = true; |
| 28 | #endif |
| 29 | } |
| 30 | |
| 31 | int GELU_riscv::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 32 | { |
| 33 | int w = bottom_top_blob.w; |
| 34 | int h = bottom_top_blob.h; |
| 35 | int d = bottom_top_blob.d; |
| 36 | int channels = bottom_top_blob.c; |
| 37 | int elempack = bottom_top_blob.elempack; |
| 38 | int size = w * h * d * elempack; |
| 39 | |
| 40 | #if __riscv_vector |
| 41 | if (fast_gelu) |
| 42 | { |
| 43 | #pragma omp parallel for num_threads(opt.num_threads) |
| 44 | for (int q = 0; q < channels; q++) |
| 45 | { |
| 46 | float* ptr = bottom_top_blob.channel(q); |
| 47 | |
| 48 | int n = size; |
| 49 | while (n > 0) |
| 50 | { |
| 51 | size_t vl = vsetvl_e32m4(n); |
| 52 | |
| 53 | vfloat32m4_t _p = vle32_v_f32m4(ptr, vl); |
| 54 | |
| 55 | vfloat32m4_t _arg = vfmul_vf_f32m4( |
| 56 | vfmul_vv_f32m4(vfmul_vv_f32m4(_p, _p, vl), _p, vl), 0.044715f, vl); |
| 57 | |
| 58 | _arg = vfadd_vv_f32m4(_p, _arg, vl); |
| 59 | _arg = vfmul_vf_f32m4(_arg, 0.79788452f, vl); |
| 60 | vfloat32m4_t _tanharg = tanh_ps(_arg, vl); |
| 61 | _p = vfmul_vf_f32m4( |
| 62 | vfmul_vv_f32m4(_p, vfadd_vf_f32m4(_tanharg, 1.f, vl), vl), .5f, vl); |
| 63 | |
| 64 | vse32_v_f32m4(ptr, _p, vl); |
| 65 | n -= vl; |
| 66 | ptr += vl; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | #pragma omp parallel for num_threads(opt.num_threads) |
| 73 | for (int q = 0; q < channels; q++) |
| 74 | { |
| 75 | float* ptr = bottom_top_blob.channel(q); |
| 76 | |
| 77 | int n = size; |
| 78 | while (n > 0) |
| 79 | { |
| 80 | size_t vl = vsetvl_e32m8(n); |
| 81 | auto _p = vle32_v_f32m8(ptr, vl); |
| 82 | auto _perfc = vfmul_vf_f32m8(_p, -.70710678f, vl); |
| 83 | _p = vfmul_vf_f32m8(_p, .5f, vl); |
| 84 | // y = x * P(X <= x) where X ~ N(0, 1) |
| 85 | |
| 86 | _perfc = erfc_ps(_perfc, vl); |
| 87 | |
| 88 | _p = vfmul_vv_f32m8(_p, _perfc, vl); |
| 89 | vse32_v_f32m8(ptr, _p, vl); |
| 90 | |
| 91 | n -= vl; |
| 92 | ptr += vl; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | #endif |
| 99 | |
| 100 | return GELU::forward_inplace(bottom_top_blob, opt); |
| 101 | } |
| 102 | |
| 103 | } // namespace ncnn |