| 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 | static void conv3x3s1_winograd43_transform_kernel_pack8to1_int8_msa(const Mat& kernel, Mat& kernel_tm_pack8to1, int inch, int outch, const Option& opt) |
| 16 | { |
| 17 | // winograd43 transform kernel |
| 18 | Mat kernel_tm(6 * 6, inch, outch, (size_t)2u); |
| 19 | |
| 20 | const short ktm[6][3] = { |
| 21 | {6, 0, 0}, |
| 22 | {-4, -4, -4}, |
| 23 | {-4, 4, -4}, |
| 24 | {1, 2, 4}, |
| 25 | {1, -2, 4}, |
| 26 | {0, 0, 6} |
| 27 | }; |
| 28 | |
| 29 | #pragma omp parallel for num_threads(opt.num_threads) |
| 30 | for (int p = 0; p < outch; p++) |
| 31 | { |
| 32 | for (int q = 0; q < inch; q++) |
| 33 | { |
| 34 | const signed char* kernel0 = (const signed char*)kernel + p * inch * 9 + q * 9; |
| 35 | short* kernel_tm0 = kernel_tm.channel(p).row<short>(q); |
| 36 | |
| 37 | // transform kernel |
| 38 | const signed char* k0 = kernel0; |
| 39 | const signed char* k1 = kernel0 + 3; |
| 40 | const signed char* k2 = kernel0 + 6; |
| 41 | |
| 42 | // h |
| 43 | short tmp[6][3]; |
| 44 | for (int i = 0; i < 6; i++) |
| 45 | { |
| 46 | tmp[i][0] = k0[0] * ktm[i][0] + k0[1] * ktm[i][1] + k0[2] * ktm[i][2]; |
| 47 | tmp[i][1] = k1[0] * ktm[i][0] + k1[1] * ktm[i][1] + k1[2] * ktm[i][2]; |
| 48 | tmp[i][2] = k2[0] * ktm[i][0] + k2[1] * ktm[i][1] + k2[2] * ktm[i][2]; |
| 49 | } |
| 50 | |
| 51 | // U |
| 52 | for (int j = 0; j < 6; j++) |
| 53 | { |
| 54 | short* tmpp = &tmp[j][0]; |
| 55 | |
| 56 | for (int i = 0; i < 6; i++) |
| 57 | { |
| 58 | kernel_tm0[j * 6 + i] = tmpp[0] * ktm[i][0] + tmpp[1] * ktm[i][1] + tmpp[2] * ktm[i][2]; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // interleave |
| 65 | // src = 36-inch-outch |
| 66 | // dst = 4b-8a-inch/8a-36-outch/4b |
| 67 | kernel_tm_pack8to1.create(8 * inch / 8, 36, outch / 4 + outch % 4, (size_t)2u * 4, 4); |
| 68 | |
| 69 | int p = 0; |
| 70 | for (; p + 3 < outch; p += 4) |
| 71 | { |
| 72 | const Mat k0 = kernel_tm.channel(p); |
| 73 | const Mat k1 = kernel_tm.channel(p + 1); |
| 74 | const Mat k2 = kernel_tm.channel(p + 2); |
| 75 | const Mat k3 = kernel_tm.channel(p + 3); |
| 76 | |
| 77 | Mat g0 = kernel_tm_pack8to1.channel(p / 4); |
| 78 | |
| 79 | for (int k = 0; k < 36; k++) |
| 80 | { |
| 81 | short* g00 = g0.row<short>(k); |
| 82 | |
| 83 | for (int q = 0; q + 7 < inch; q += 8) |
| 84 | { |
| 85 | for (int i = 0; i < 8; i++) |
| 86 | { |
| 87 | g00[0] = k0.row<const short>(q + i)[k]; |
| 88 | g00[1] = k1.row<const short>(q + i)[k]; |
| 89 | g00[2] = k2.row<const short>(q + i)[k]; |
| 90 | g00[3] = k3.row<const short>(q + i)[k]; |
| 91 | |
| 92 | g00 += 4; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | for (; p < outch; p++) |
| 98 | { |
| 99 | const Mat k0 = kernel_tm.channel(p); |
| 100 | |
| 101 | Mat g0 = kernel_tm_pack8to1.channel(p / 4 + p % 4); |
| 102 | |
| 103 | for (int k = 0; k < 36; k++) |
| 104 | { |
| 105 | short* g00 = g0.row<short>(k); |
| 106 | |
| 107 | for (int q = 0; q + 7 < inch; q += 8) |
| 108 | { |
| 109 | for (int i = 0; i < 8; i++) |
| 110 | { |
| 111 | g00[0] = k0.row<const short>(q + i)[k]; |
| 112 | |
| 113 | g00 += 1; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static void conv3x3s1_winograd43_pack8to1_int8_msa(const Mat& bottom_blob, Mat& top_blob, const Mat& kernel_tm, const Option& opt) |
| 121 | { |
| 122 | int w = bottom_blob.w; |
| 123 | int h = bottom_blob.h; |
| 124 | int inch = bottom_blob.c; |
| 125 | // size_t elemsize = bottom_blob.elemsize; |
| 126 | int elempack = bottom_blob.elempack; |
| 127 | |
| 128 | int outw = top_blob.w; |
| 129 | int outh = top_blob.h; |
| 130 | int outch = top_blob.c; |
| 131 | |
| 132 | // pad to 4n+2 |
| 133 | Mat bottom_blob_bordered = bottom_blob; |
| 134 | |
| 135 | outw = (outw + 3) / 4 * 4; |
| 136 | outh = (outh + 3) / 4 * 4; |
| 137 | |
| 138 | w = outw + 2; |
| 139 | h = outh + 2; |
| 140 | copy_make_border(bottom_blob, bottom_blob_bordered, 0, h - bottom_blob.h, 0, w - bottom_blob.w, BORDER_CONSTANT, 0.f, opt); |
| 141 | |
| 142 | // BEGIN transform input |
| 143 | Mat bottom_blob_tm; |
| 144 | { |
| 145 | int w_tiles = outw / 4; |
| 146 | int h_tiles = outh / 4; |
| 147 | const int tiles = w_tiles * h_tiles; |
| 148 | |
| 149 | bottom_blob_tm.create(tiles, 36, inch, 2u * elempack, elempack, opt.workspace_allocator); |
| 150 | conv3x3s1_winograd43_transform_input_pack8_int8_msa(bottom_blob_bordered, bottom_blob_tm, opt); |
| 151 | } |
| 152 | bottom_blob_bordered = Mat(); |
| 153 | // END transform input |
| 154 | |
| 155 | // BEGIN dot |
| 156 | Mat top_blob_tm; |
| 157 | convolution_winograd_dot_pack8to1_int8_msa(bottom_blob_tm, outch, kernel_tm, top_blob_tm, opt); |
| 158 | // END dot |
| 159 | |
| 160 | // BEGIN transform output |
| 161 | Mat top_blob_bordered; |
| 162 | if (outw == top_blob.w && outh == top_blob.h) |
| 163 | { |
| 164 | top_blob_bordered = top_blob; |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | top_blob_bordered.create(outw, outh, outch, 4u, 1, opt.workspace_allocator); |
| 169 | } |
| 170 | { |
| 171 | conv3x3s1_winograd43_transform_output_int8_msa(top_blob_tm, top_blob_bordered, opt); |
| 172 | } |
| 173 | // END transform output |
| 174 | |
| 175 | // cut result pad |
| 176 | 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); |
| 177 | } |