| 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 | static void convolution_packn_rvv(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data_packn, const Mat& bias_data, int kernel_w, int kernel_h, int dilation_w, int dilation_h, int stride_w, int stride_h, int activation_type, const Mat& activation_params, const Option& opt) |
| 16 | { |
| 17 | const int packn = csrr_vlenb() / 4; |
| 18 | const size_t vl = vsetvl_e32m1(packn); |
| 19 | |
| 20 | int w = bottom_blob.w; |
| 21 | int channels = bottom_blob.c; |
| 22 | |
| 23 | int outw = top_blob.w; |
| 24 | int outh = top_blob.h; |
| 25 | int outch = top_blob.c; |
| 26 | |
| 27 | const int maxk = kernel_w * kernel_h; |
| 28 | |
| 29 | // kernel offsets |
| 30 | std::vector<int> _space_ofs(maxk); |
| 31 | int* space_ofs = &_space_ofs[0]; |
| 32 | { |
| 33 | int p1 = 0; |
| 34 | int p2 = 0; |
| 35 | int gap = w * dilation_h - kernel_w * dilation_w; |
| 36 | for (int i = 0; i < kernel_h; i++) |
| 37 | { |
| 38 | for (int j = 0; j < kernel_w; j++) |
| 39 | { |
| 40 | space_ofs[p1] = p2; |
| 41 | p1++; |
| 42 | p2 += dilation_w; |
| 43 | } |
| 44 | p2 += gap; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | const float* bias_data_ptr = bias_data; |
| 49 | |
| 50 | #pragma omp parallel for num_threads(opt.num_threads) |
| 51 | for (int p = 0; p < outch; p++) |
| 52 | { |
| 53 | float* outptr = top_blob.channel(p); |
| 54 | |
| 55 | for (int i = 0; i < outh; i++) |
| 56 | { |
| 57 | for (int j = 0; j < outw; j++) |
| 58 | { |
| 59 | vfloat32m1_t _sum = vfmv_v_f_f32m1(0.f, vl); |
| 60 | |
| 61 | if (bias_data_ptr) |
| 62 | { |
| 63 | _sum = vle32_v_f32m1(bias_data_ptr + p * packn, vl); |
| 64 | } |
| 65 | |
| 66 | const float* kptr = (const float*)weight_data_packn.channel(p); |
| 67 | |
| 68 | // channels |
| 69 | for (int q = 0; q < channels; q++) |
| 70 | { |
| 71 | const Mat m = bottom_blob.channel(q); |
| 72 | const float* sptr = m.row(i * stride_h) + j * stride_w * packn; |
| 73 | |
| 74 | for (int k = 0; k < maxk; k++) // 29.23 |
| 75 | { |
| 76 | const float* slptr = sptr + space_ofs[k] * packn; |
| 77 | |
| 78 | for (int l = 0; l < packn; l++) |
| 79 | { |
| 80 | float val = *slptr++; |
| 81 | vfloat32m1_t _w0 = vle32_v_f32m1(kptr, vl); |
| 82 | _sum = vfmacc_vf_f32m1(_sum, val, _w0, vl); |
| 83 | |
| 84 | kptr += packn; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | _sum = activation_ps(_sum, activation_type, activation_params, vl); |
| 90 | |
| 91 | vse32_v_f32m1(outptr + j * packn, _sum, vl); |
| 92 | } |
| 93 | |
| 94 | outptr += outw * packn; |
| 95 | } |
| 96 | } |
| 97 | } |