| 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 deconvolution_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 h = bottom_blob.h; |
| 22 | int channels = bottom_blob.c; |
| 23 | |
| 24 | int outw = top_blob.w; |
| 25 | int outh = top_blob.h; |
| 26 | int outch = top_blob.c; |
| 27 | |
| 28 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 29 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 30 | |
| 31 | const int maxk = kernel_w * kernel_h; |
| 32 | |
| 33 | const float* bias_data_ptr = bias_data; |
| 34 | |
| 35 | // num_output |
| 36 | #pragma omp parallel for num_threads(opt.num_threads) |
| 37 | for (int p = 0; p < outch; p++) |
| 38 | { |
| 39 | __fp16* outptr = top_blob.channel(p); |
| 40 | |
| 41 | for (int i = 0; i < outh; i++) |
| 42 | { |
| 43 | for (int j = 0; j < outw; j++) |
| 44 | { |
| 45 | vfloat32m2_t _sum = vfmv_v_f_f32m2(0.f, vl); |
| 46 | |
| 47 | if (bias_data_ptr) |
| 48 | { |
| 49 | _sum = vle32_v_f32m2(bias_data_ptr + p * packn, vl); |
| 50 | } |
| 51 | |
| 52 | const __fp16* kptr = (const __fp16*)weight_data_fp16.channel(p); |
| 53 | |
| 54 | // channels |
| 55 | for (int q = 0; q < channels; q++) |
| 56 | { |
| 57 | const Mat m = bottom_blob.channel(q); |
| 58 | |
| 59 | for (int y = 0; y < kernel_h; y++) |
| 60 | { |
| 61 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 62 | if (sys < 0 || sys % stride_h != 0) |
| 63 | continue; |
| 64 | |
| 65 | int sy = sys / stride_h; |
| 66 | if (sy >= h) |
| 67 | continue; |
| 68 | |
| 69 | for (int x = 0; x < kernel_w; x++) |
| 70 | { |
| 71 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 72 | if (sxs < 0 || sxs % stride_w != 0) |
| 73 | continue; |
| 74 | |
| 75 | int sx = sxs / stride_w; |
| 76 | if (sx >= w) |
| 77 | continue; |
| 78 | |
| 79 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * packn; |
| 80 | |
| 81 | int k = y * kernel_w + x; |
| 82 | |
| 83 | for (int l = 0; l < packn; l++) |
| 84 | { |
| 85 | __fp16 val = *sptr++; |
| 86 | vfloat16m1_t _w0 = vle16_v_f16m1(kptr + k * packn * packn + packn * l, vl); |
| 87 | _sum = vfwmacc_vf_f32m2(_sum, val, _w0, vl); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | kptr += maxk * packn * packn; |
| 93 | } |
| 94 | |
| 95 | _sum = activation_ps(_sum, activation_type, activation_params, vl); |
| 96 | |
| 97 | vse16_v_f16m1(outptr + j * packn, vfncvt_f_f_w_f16m1(_sum, vl), vl); |
| 98 | } |
| 99 | |
| 100 | outptr += outw * packn; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static void deconvolution_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) |
| 106 | { |
| 107 | const int packn = csrr_vlenb() / 2; |
| 108 | const size_t vl = vsetvl_e16m1(packn); |
| 109 | |
| 110 | int w = bottom_blob.w; |
| 111 | int h = bottom_blob.h; |
| 112 | int channels = bottom_blob.c; |
| 113 | |
| 114 | int outw = top_blob.w; |
| 115 | int outh = top_blob.h; |
| 116 | int outch = top_blob.c; |
| 117 | |
| 118 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 119 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 120 | |
| 121 | const int maxk = kernel_w * kernel_h; |
| 122 | |
| 123 | const __fp16* bias_data_ptr = bias_data_fp16; |
| 124 | |
| 125 | // num_output |
| 126 | #pragma omp parallel for num_threads(opt.num_threads) |
| 127 | for (int p = 0; p < outch; p++) |
| 128 | { |
| 129 | __fp16* outptr = top_blob.channel(p); |
| 130 | |
| 131 | for (int i = 0; i < outh; i++) |
| 132 | { |
| 133 | for (int j = 0; j < outw; j++) |
| 134 | { |
| 135 | vfloat16m1_t _sum = vfmv_v_f_f16m1(0.f, vl); |
| 136 | |
| 137 | if (bias_data_ptr) |
| 138 | { |
| 139 | _sum = vle16_v_f16m1(bias_data_ptr + p * packn, vl); |
| 140 | } |
| 141 | |
| 142 | const __fp16* kptr = (const __fp16*)weight_data_fp16.channel(p); |
| 143 | |
| 144 | // channels |
| 145 | for (int q = 0; q < channels; q++) |
| 146 | { |
| 147 | const Mat m = bottom_blob.channel(q); |
| 148 | |
| 149 | for (int y = 0; y < kernel_h; y++) |
| 150 | { |
| 151 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 152 | if (sys < 0 || sys % stride_h != 0) |
| 153 | continue; |
| 154 | |
| 155 | int sy = sys / stride_h; |
| 156 | if (sy >= h) |
| 157 | continue; |
| 158 | |
| 159 | for (int x = 0; x < kernel_w; x++) |
| 160 | { |
| 161 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 162 | if (sxs < 0 || sxs % stride_w != 0) |
| 163 | continue; |
| 164 | |
| 165 | int sx = sxs / stride_w; |
| 166 | if (sx >= w) |
| 167 | continue; |
| 168 | |
| 169 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * packn; |
| 170 | |
| 171 | int k = y * kernel_w + x; |
| 172 | |
| 173 | for (int l = 0; l < packn; l++) |
| 174 | { |
| 175 | __fp16 val = *sptr++; |
| 176 | vfloat16m1_t _w0 = vle16_v_f16m1(kptr + k * packn * packn + packn * l, vl); |
| 177 | _sum = vfmacc_vf_f16m1(_sum, val, _w0, vl); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | kptr += maxk * packn * packn; |
| 183 | } |
| 184 | |
| 185 | _sum = activation_ps(_sum, activation_type, activation_params, vl); |
| 186 | |
| 187 | vse16_v_f16m1(outptr + j * packn, _sum, vl); |
| 188 | } |
| 189 | |
| 190 | outptr += outw * packn; |
| 191 | } |
| 192 | } |
| 193 | } |