| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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 | #if NCNN_RUNTIME_CPU && NCNN_MMI && !__mips_msa && !__mips_loongson_mmi |
| 16 | void conv3x3s1_winograd43_transform_kernel_int8_loongson_mmi(const Mat& kernel, Mat& kernel_tm_packed, int inch, int outch, const Option& opt); |
| 17 | #endif |
| 18 | |
| 19 | static void conv3x3s1_winograd43_transform_kernel_int8_msa(const Mat& kernel, Mat& kernel_tm_packed, int inch, int outch, const Option& opt) |
| 20 | { |
| 21 | #if NCNN_RUNTIME_CPU && NCNN_MMI && !__mips_msa && !__mips_loongson_mmi |
| 22 | if (ncnn::cpu_support_loongson_mmi()) |
| 23 | { |
| 24 | conv3x3s1_winograd43_transform_kernel_int8_loongson_mmi(kernel, kernel_tm_packed, inch, outch, opt); |
| 25 | return; |
| 26 | } |
| 27 | #endif |
| 28 | |
| 29 | // winograd43 transform kernel |
| 30 | Mat kernel_tm(6 * 6, inch, outch, (size_t)2u); |
| 31 | |
| 32 | const short ktm[6][3] = { |
| 33 | {6, 0, 0}, |
| 34 | {-4, -4, -4}, |
| 35 | {-4, 4, -4}, |
| 36 | {1, 2, 4}, |
| 37 | {1, -2, 4}, |
| 38 | {0, 0, 6} |
| 39 | }; |
| 40 | |
| 41 | #pragma omp parallel for num_threads(opt.num_threads) |
| 42 | for (int p = 0; p < outch; p++) |
| 43 | { |
| 44 | for (int q = 0; q < inch; q++) |
| 45 | { |
| 46 | const signed char* kernel0 = (const signed char*)kernel + p * inch * 9 + q * 9; |
| 47 | short* kernel_tm0 = kernel_tm.channel(p).row<short>(q); |
| 48 | |
| 49 | // transform kernel |
| 50 | const signed char* k0 = kernel0; |
| 51 | const signed char* k1 = kernel0 + 3; |
| 52 | const signed char* k2 = kernel0 + 6; |
| 53 | |
| 54 | // h |
| 55 | short tmp[6][3]; |
| 56 | for (int i = 0; i < 6; i++) |
| 57 | { |
| 58 | tmp[i][0] = k0[0] * ktm[i][0] + k0[1] * ktm[i][1] + k0[2] * ktm[i][2]; |
| 59 | tmp[i][1] = k1[0] * ktm[i][0] + k1[1] * ktm[i][1] + k1[2] * ktm[i][2]; |
| 60 | tmp[i][2] = k2[0] * ktm[i][0] + k2[1] * ktm[i][1] + k2[2] * ktm[i][2]; |
| 61 | } |
| 62 | |
| 63 | // U |
| 64 | for (int j = 0; j < 6; j++) |
| 65 | { |
| 66 | short* tmpp = &tmp[j][0]; |
| 67 | |
| 68 | for (int i = 0; i < 6; i++) |
| 69 | { |
| 70 | kernel_tm0[j * 6 + i] = tmpp[0] * ktm[i][0] + tmpp[1] * ktm[i][1] + tmpp[2] * ktm[i][2]; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // interleave |
| 77 | // src = 36-inch-outch |
| 78 | // dst = 2b-inch-36-outch/2b |
| 79 | #if __mips_msa |
| 80 | if (outch >= 4) |
| 81 | { |
| 82 | if (inch >= 4) |
| 83 | kernel_tm_packed.create(inch / 4 + inch % 4, 36, outch / 4 + outch % 4, (size_t)2u * 16, 16); |
| 84 | else |
| 85 | kernel_tm_packed.create(inch, 36, outch / 4 + outch % 4, (size_t)2u * 4, 4); |
| 86 | } |
| 87 | #else // __mips_msa |
| 88 | if (outch >= 2) |
| 89 | { |
| 90 | #if __mips_loongson_mmi |
| 91 | if (inch >= 4) |
| 92 | kernel_tm_packed.create(inch / 4 + inch % 4, 36, outch / 2 + outch % 2, (size_t)2u * 8, 8); |
| 93 | else |
| 94 | #endif // __mips_loongson_mmi |
| 95 | { |
| 96 | kernel_tm_packed.create(inch, 36, outch / 2 + outch % 2, (size_t)2u * 2, 2); |
| 97 | } |
| 98 | } |
| 99 | #endif // __mips_msa |
| 100 | else |
| 101 | { |
| 102 | #if __mips_msa || __mips_loongson_mmi |
| 103 | if (inch >= 4) |
| 104 | kernel_tm_packed.create(inch / 4 + inch % 4, 36, outch, (size_t)2u * 4, 4); |
| 105 | else |
| 106 | #endif // __mips_msa || __mips_loongson_mmi |
| 107 | { |
| 108 | kernel_tm_packed.create(inch, 36, outch, (size_t)2u, 1); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | int p = 0; |
| 113 | #if __mips_msa |
| 114 | for (; p + 3 < outch; p += 4) |
| 115 | { |
| 116 | const Mat k0 = kernel_tm.channel(p); |
| 117 | const Mat k1 = kernel_tm.channel(p + 1); |
| 118 | const Mat k2 = kernel_tm.channel(p + 2); |
| 119 | const Mat k3 = kernel_tm.channel(p + 3); |
| 120 | |
| 121 | Mat g0 = kernel_tm_packed.channel(p / 4); |
| 122 | |
| 123 | for (int k = 0; k < 36; k++) |
| 124 | { |
| 125 | short* g00 = g0.row<short>(k); |
| 126 | |
| 127 | int q = 0; |
| 128 | for (; q + 3 < inch; q += 4) |
| 129 | { |
| 130 | g00[0] = k0.row<const short>(q)[k]; |
| 131 | g00[1] = k0.row<const short>(q + 1)[k]; |
| 132 | g00[2] = k0.row<const short>(q + 2)[k]; |
| 133 | g00[3] = k0.row<const short>(q + 3)[k]; |
| 134 | g00[4] = k1.row<const short>(q)[k]; |
| 135 | g00[5] = k1.row<const short>(q + 1)[k]; |
| 136 | g00[6] = k1.row<const short>(q + 2)[k]; |
| 137 | g00[7] = k1.row<const short>(q + 3)[k]; |
| 138 | g00[8] = k2.row<const short>(q)[k]; |
| 139 | g00[9] = k2.row<const short>(q + 1)[k]; |
| 140 | g00[10] = k2.row<const short>(q + 2)[k]; |
| 141 | g00[11] = k2.row<const short>(q + 3)[k]; |
| 142 | g00[12] = k3.row<const short>(q)[k]; |
| 143 | g00[13] = k3.row<const short>(q + 1)[k]; |
| 144 | g00[14] = k3.row<const short>(q + 2)[k]; |
| 145 | g00[15] = k3.row<const short>(q + 3)[k]; |
| 146 | g00 += 16; |
| 147 | } |
| 148 | for (; q < inch; q++) |
| 149 | { |
| 150 | g00[0] = k0.row<const short>(q)[k]; |
| 151 | g00[1] = k1.row<const short>(q)[k]; |
| 152 | g00[2] = k2.row<const short>(q)[k]; |
| 153 | g00[3] = k3.row<const short>(q)[k]; |
| 154 | g00 += 4; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | #else // __mips_msa |
| 159 | for (; p + 1 < outch; p += 2) |
| 160 | { |
| 161 | const Mat k0 = kernel_tm.channel(p); |
| 162 | const Mat k1 = kernel_tm.channel(p + 1); |
| 163 | |
| 164 | Mat g0 = kernel_tm_packed.channel(p / 2); |
| 165 | |
| 166 | for (int k = 0; k < 36; k++) |
| 167 | { |
| 168 | short* g00 = g0.row<short>(k); |
| 169 | |
| 170 | int q = 0; |
| 171 | #if __mips_loongson_mmi |
| 172 | for (; q + 3 < inch; q += 4) |
| 173 | { |
| 174 | g00[0] = k0.row<const short>(q)[k]; |
| 175 | g00[1] = k0.row<const short>(q + 1)[k]; |
| 176 | g00[2] = k1.row<const short>(q)[k]; |
| 177 | g00[3] = k1.row<const short>(q + 1)[k]; |
| 178 | g00[4] = k0.row<const short>(q + 2)[k]; |
| 179 | g00[5] = k0.row<const short>(q + 3)[k]; |
| 180 | g00[6] = k1.row<const short>(q + 2)[k]; |
| 181 | g00[7] = k1.row<const short>(q + 3)[k]; |
| 182 | g00 += 8; |
| 183 | } |
| 184 | #endif // __mips_loongson_mmi |
| 185 | for (; q < inch; q++) |
| 186 | { |
| 187 | g00[0] = k0.row<const short>(q)[k]; |
| 188 | g00[1] = k1.row<const short>(q)[k]; |
| 189 | g00 += 2; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | #endif // __mips_msa |
| 194 | for (; p < outch; p++) |
| 195 | { |
| 196 | const Mat k0 = kernel_tm.channel(p); |
| 197 | |
| 198 | #if __mips_msa |
| 199 | Mat g0 = kernel_tm_packed.channel(p / 4 + p % 4); |
| 200 | #else |
| 201 | Mat g0 = kernel_tm_packed.channel(p / 2 + p % 2); |
| 202 | #endif |
| 203 | |
| 204 | for (int k = 0; k < 36; k++) |
| 205 | { |
| 206 | short* g00 = g0.row<short>(k); |
| 207 | |
| 208 | int q = 0; |
| 209 | #if __mips_msa || __mips_loongson_mmi |
| 210 | for (; q + 3 < inch; q += 4) |
| 211 | { |
| 212 | g00[0] = k0.row<const short>(q)[k]; |
| 213 | g00[1] = k0.row<const short>(q + 1)[k]; |
| 214 | g00[2] = k0.row<const short>(q + 2)[k]; |
| 215 | g00[3] = k0.row<const short>(q + 3)[k]; |
| 216 | g00 += 4; |
| 217 | } |
| 218 | #endif // __mips_msa || __mips_loongson_mmi |
| 219 | for (; q < inch; q++) |
| 220 | { |
| 221 | g00[0] = k0.row<const short>(q)[k]; |
| 222 | g00 += 1; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static void conv3x3s1_winograd43_int8_msa(const Mat& bottom_blob, Mat& top_blob, const Mat& kernel_tm, const Option& opt) |
| 229 | { |
| 230 | int w = bottom_blob.w; |
| 231 | int h = bottom_blob.h; |
| 232 | int inch = bottom_blob.c; |
| 233 | // size_t elemsize = bottom_blob.elemsize; |
| 234 | int elempack = bottom_blob.elempack; |
| 235 | |
| 236 | int outw = top_blob.w; |
| 237 | int outh = top_blob.h; |
| 238 | int outch = top_blob.c; |
| 239 | |
| 240 | // pad to 4n+2 |
| 241 | Mat bottom_blob_bordered = bottom_blob; |
| 242 | |
| 243 | outw = (outw + 3) / 4 * 4; |
| 244 | outh = (outh + 3) / 4 * 4; |
| 245 | |
| 246 | w = outw + 2; |
| 247 | h = outh + 2; |
| 248 | copy_make_border(bottom_blob, bottom_blob_bordered, 0, h - bottom_blob.h, 0, w - bottom_blob.w, BORDER_CONSTANT, 0.f, opt); |
| 249 | |
| 250 | // BEGIN transform input |
| 251 | Mat bottom_blob_tm; |
| 252 | { |
| 253 | int w_tiles = outw / 4; |
| 254 | int h_tiles = outh / 4; |
| 255 | const int tiles = w_tiles * h_tiles; |
| 256 | |
| 257 | bottom_blob_tm.create(tiles, 36, inch, 2u * elempack, elempack, opt.workspace_allocator); |
| 258 | conv3x3s1_winograd43_transform_input_int8_msa(bottom_blob_bordered, bottom_blob_tm, opt); |
| 259 | } |
| 260 | bottom_blob_bordered = Mat(); |
| 261 | // END transform input |
| 262 | |
| 263 | // BEGIN dot |
| 264 | Mat top_blob_tm; |
| 265 | convolution_winograd_dot_int8_msa(bottom_blob_tm, outch, kernel_tm, top_blob_tm, opt); |
| 266 | // END dot |
| 267 | |
| 268 | // BEGIN transform output |
| 269 | Mat top_blob_bordered; |
| 270 | if (outw == top_blob.w && outh == top_blob.h) |
| 271 | { |
| 272 | top_blob_bordered = top_blob; |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | top_blob_bordered.create(outw, outh, outch, 4u, 1, opt.workspace_allocator); |
| 277 | } |
| 278 | { |
| 279 | conv3x3s1_winograd43_transform_output_int8_msa(top_blob_tm, top_blob_bordered, opt); |
| 280 | } |
| 281 | // END transform output |
| 282 | |
| 283 | // cut result pad |
| 284 | copy_cut_border(top_blob_bordered, top_blob, 0, top_blob_bordered.h - top_blob.h, 0, top_blob_bordered.w - top_blob.w, opt); |
| 285 | } |