| 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 "dropout_riscv.h" |
| 19 | |
| 20 | #if __riscv_vector |
| 21 | #include <riscv_vector.h> |
| 22 | #endif // __riscv_vector |
| 23 | |
| 24 | namespace ncnn { |
| 25 | |
| 26 | Dropout_riscv::Dropout_riscv() |
| 27 | { |
| 28 | #if __riscv_vector |
| 29 | support_packing = true; |
| 30 | #endif |
| 31 | } |
| 32 | |
| 33 | int Dropout_riscv::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 34 | { |
| 35 | if (scale == 1.f) |
| 36 | { |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | int w = bottom_top_blob.w; |
| 41 | int h = bottom_top_blob.h; |
| 42 | int d = bottom_top_blob.d; |
| 43 | int channels = bottom_top_blob.c; |
| 44 | int elempack = bottom_top_blob.elempack; |
| 45 | int size = w * h * d * elempack; |
| 46 | |
| 47 | #pragma omp parallel for num_threads(opt.num_threads) |
| 48 | for (int q = 0; q < channels; q++) |
| 49 | { |
| 50 | float* ptr = bottom_top_blob.channel(q); |
| 51 | |
| 52 | #if __riscv_vector |
| 53 | int n = size; |
| 54 | while (n > 0) |
| 55 | { |
| 56 | size_t vl = vsetvl_e32m8(n); |
| 57 | |
| 58 | vfloat32m8_t _p = vle32_v_f32m8(ptr, vl); |
| 59 | _p = vfmul_vf_f32m8(_p, scale, vl); |
| 60 | vse32_v_f32m8(ptr, _p, vl); |
| 61 | |
| 62 | ptr += vl; |
| 63 | n -= vl; |
| 64 | } |
| 65 | #else // __riscv_vector |
| 66 | for (int i = 0; i < size; i++) |
| 67 | { |
| 68 | *ptr = *ptr * scale; |
| 69 | |
| 70 | ptr++; |
| 71 | } |
| 72 | #endif // __riscv_vector |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | } // namespace ncnn |