| 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_fp16s_rvv(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data_fp16, 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() / 2; |
| 18 | const size_t vl = vsetvl_e16m1(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 | // num_output |
| 51 | #pragma omp parallel for num_threads(opt.num_threads) |
| 52 | for (int p = 0; p < outch; p++) |
| 53 | { |
| 54 | __fp16* outptr = top_blob.channel(p); |
| 55 | |
| 56 | for (int i = 0; i < outh; i++) |
| 57 | { |
| 58 | for (int j = 0; j < outw; j++) |
| 59 | { |
| 60 | vfloat32m2_t _sum = vfmv_v_f_f32m2(0.f, vl); |
| 61 | |
| 62 | if (bias_data_ptr) |
| 63 | { |
| 64 | _sum = vle32_v_f32m2(bias_data_ptr + p * packn, vl); |
| 65 | } |
| 66 | |
| 67 | const __fp16* kptr = weight_data_fp16.channel(p); |
| 68 | |
| 69 | // channels |
| 70 | for (int q = 0; q < channels; q++) |
| 71 | { |
| 72 | const Mat m = bottom_blob.channel(q); |
| 73 | const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w * packn; |
| 74 | |
| 75 | for (int k = 0; k < maxk; k++) |
| 76 | { |
| 77 | const __fp16* slptr = sptr + space_ofs[k] * packn; |
| 78 | |
| 79 | for (int l = 0; l < packn; l++) |
| 80 | { |
| 81 | float val = (float)*slptr++; |
| 82 | vfloat16m1_t _w0 = vle16_v_f16m1(kptr, vl); |
| 83 | // _sum = vfwmacc_vf_f32m2(_sum, val, _w0, vl); |
| 84 | |
| 85 | vfloat32m2_t _qwq = vfwmul_vf_f32m2(_w0, val, vl); |
| 86 | _sum = vfadd_vv_f32m2(_sum, _qwq, vl); |
| 87 | |
| 88 | kptr += packn; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | _sum = activation_ps(_sum, activation_type, activation_params, vl); |
| 94 | |
| 95 | vse16_v_f16m1(outptr + j * packn, vfncvt_f_f_w_f16m1(_sum, vl), vl); |
| 96 | } |
| 97 | |
| 98 | outptr += outw * packn; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static void convolution_packn_fp16sa_rvv(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data_fp16, const Mat& bias_data_fp16, 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) |
| 104 | { |
| 105 | const int packn = csrr_vlenb() / 2; |
| 106 | const size_t vl = vsetvl_e16m1(packn); |
| 107 | |
| 108 | int w = bottom_blob.w; |
| 109 | int channels = bottom_blob.c; |
| 110 | |
| 111 | int outw = top_blob.w; |
| 112 | int outh = top_blob.h; |
| 113 | int outch = top_blob.c; |
| 114 | |
| 115 | const int maxk = kernel_w * kernel_h; |
| 116 | |
| 117 | // kernel offsets |
| 118 | std::vector<int> _space_ofs(maxk); |
| 119 | int* space_ofs = &_space_ofs[0]; |
| 120 | { |
| 121 | int p1 = 0; |
| 122 | int p2 = 0; |
| 123 | int gap = w * dilation_h - kernel_w * dilation_w; |
| 124 | for (int i = 0; i < kernel_h; i++) |
| 125 | { |
| 126 | for (int j = 0; j < kernel_w; j++) |
| 127 | { |
| 128 | space_ofs[p1] = p2; |
| 129 | p1++; |
| 130 | p2 += dilation_w; |
| 131 | } |
| 132 | p2 += gap; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | const __fp16* bias_data_ptr = bias_data_fp16; |
| 137 | |
| 138 | // num_output |
| 139 | #pragma omp parallel for num_threads(opt.num_threads) |
| 140 | for (int p = 0; p < outch; p++) |
| 141 | { |
| 142 | __fp16* outptr = top_blob.channel(p); |
| 143 | |
| 144 | for (int i = 0; i < outh; i++) |
| 145 | { |
| 146 | for (int j = 0; j < outw; j++) |
| 147 | { |
| 148 | vfloat16m1_t _sum = vfmv_v_f_f16m1(0.f, vl); |
| 149 | |
| 150 | if (bias_data_ptr) |
| 151 | { |
| 152 | _sum = vle16_v_f16m1(bias_data_ptr + p * packn, vl); |
| 153 | } |
| 154 | |
| 155 | const __fp16* kptr = weight_data_fp16.channel(p); |
| 156 | |
| 157 | // channels |
| 158 | for (int q = 0; q < channels; q++) |
| 159 | { |
| 160 | const Mat m = bottom_blob.channel(q); |
| 161 | const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w * packn; |
| 162 | |
| 163 | for (int k = 0; k < maxk; k++) |
| 164 | { |
| 165 | const __fp16* slptr = sptr + space_ofs[k] * packn; |
| 166 | |
| 167 | for (int l = 0; l < packn; l++) |
| 168 | { |
| 169 | __fp16 val = *slptr++; |
| 170 | vfloat16m1_t _w0 = vle16_v_f16m1(kptr, vl); |
| 171 | _sum = vfmacc_vf_f16m1(_sum, val, _w0, vl); |
| 172 | |
| 173 | kptr += packn; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | _sum = activation_ps(_sum, activation_type, activation_params, vl); |
| 179 | |
| 180 | vse16_v_f16m1(outptr + j * packn, _sum, vl); |
| 181 | } |
| 182 | |
| 183 | outptr += outw * packn; |
| 184 | } |
| 185 | } |
| 186 | } |