| 1 | // yala is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // |
| 4 | // Copyright (C) 2022 yala <zhaojunchao@loongson.cn>;<junchao82@qq.com>. All rights reserved. |
| 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_output_pack4_int8_lsx(const Mat& top_blob_tm, Mat& top_blob, const Option& opt) |
| 16 | { |
| 17 | const int outw = top_blob.w; |
| 18 | const int outh = top_blob.h; |
| 19 | const int outch = top_blob.c; |
| 20 | |
| 21 | const int w_tiles = outw / 4; |
| 22 | const int h_tiles = outh / 4; |
| 23 | const int tiles = w_tiles * h_tiles; |
| 24 | |
| 25 | // const float otm[4][6] = { |
| 26 | // {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f}, |
| 27 | // {0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 0.0f}, |
| 28 | // {0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 0.0f}, |
| 29 | // {0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 1.0f} |
| 30 | // }; |
| 31 | |
| 32 | // 0 = r00 + (r01 + r02) + (r03 + r04) |
| 33 | // 1 = (r01 - r02) + (r03 - r04) * 2 |
| 34 | // 2 = (r01 + r02) + (r03 + r04) * 4 |
| 35 | // 3 = r05 + (r01 - r02) + (r03 - r04) * 8 |
| 36 | |
| 37 | #pragma omp parallel for num_threads(opt.num_threads) |
| 38 | for (int p = 0; p < outch; p++) |
| 39 | { |
| 40 | const Mat out0_tm = top_blob_tm.channel(p); |
| 41 | Mat out0 = top_blob.channel(p); |
| 42 | |
| 43 | int tmp[4][6][4]; |
| 44 | |
| 45 | // tile |
| 46 | for (int i = 0; i < h_tiles; i++) |
| 47 | { |
| 48 | for (int j = 0; j < w_tiles; j++) |
| 49 | { |
| 50 | const int* output0_tm_0 = (const int*)out0_tm + (i * w_tiles + j) * 4; |
| 51 | const int* output0_tm_1 = output0_tm_0 + tiles * 4; |
| 52 | const int* output0_tm_2 = output0_tm_0 + tiles * 8; |
| 53 | const int* output0_tm_3 = output0_tm_0 + tiles * 12; |
| 54 | const int* output0_tm_4 = output0_tm_0 + tiles * 16; |
| 55 | const int* output0_tm_5 = output0_tm_0 + tiles * 20; |
| 56 | |
| 57 | int* output0 = out0.row<int>(i * 4) + (j * 4) * 4; |
| 58 | |
| 59 | for (int m = 0; m < 5; m++) |
| 60 | { |
| 61 | __m128i _out0tm0 = __lsx_vld(output0_tm_0, 0); |
| 62 | __m128i _out0tm1 = __lsx_vld(output0_tm_1, 0); |
| 63 | __m128i _out0tm2 = __lsx_vld(output0_tm_2, 0); |
| 64 | __m128i _out0tm3 = __lsx_vld(output0_tm_3, 0); |
| 65 | __m128i _out0tm4 = __lsx_vld(output0_tm_4, 0); |
| 66 | __m128i _out0tm5 = __lsx_vld(output0_tm_5, 0); |
| 67 | |
| 68 | __m128i _tmp02a = __lsx_vadd_w(_out0tm1, _out0tm2); |
| 69 | __m128i _tmp13a = __lsx_vsub_w(_out0tm1, _out0tm2); |
| 70 | |
| 71 | __m128i _tmp02b = __lsx_vadd_w(_out0tm3, _out0tm4); |
| 72 | __m128i _tmp13b = __lsx_vsub_w(_out0tm3, _out0tm4); |
| 73 | |
| 74 | __m128i _tmp0m = __lsx_vadd_w(__lsx_vadd_w(_out0tm0, _tmp02a), _tmp02b); |
| 75 | __m128i _tmp1m = __lsx_vadd_w(_tmp13a, __lsx_vslli_w(_tmp13b, 1)); |
| 76 | __m128i _tmp2m = __lsx_vadd_w(_tmp02a, __lsx_vslli_w(_tmp02b, 2)); |
| 77 | __m128i _tmp3m = __lsx_vadd_w(__lsx_vadd_w(_tmp13a, __lsx_vslli_w(_out0tm5, 2)), __lsx_vslli_w(_tmp13b, 3)); |
| 78 | |
| 79 | __lsx_vst(_tmp0m, tmp[0][m], 0); |
| 80 | __lsx_vst(_tmp1m, tmp[1][m], 0); |
| 81 | __lsx_vst(_tmp2m, tmp[2][m], 0); |
| 82 | __lsx_vst(_tmp3m, tmp[3][m], 0); |
| 83 | |
| 84 | output0_tm_0 += tiles * 24; |
| 85 | output0_tm_1 += tiles * 24; |
| 86 | output0_tm_2 += tiles * 24; |
| 87 | output0_tm_3 += tiles * 24; |
| 88 | output0_tm_4 += tiles * 24; |
| 89 | output0_tm_5 += tiles * 24; |
| 90 | } |
| 91 | for (int m = 5; m < 6; m++) |
| 92 | { |
| 93 | __m128i _out0tm0 = __lsx_vld(output0_tm_0, 0); |
| 94 | __m128i _out0tm1 = __lsx_vld(output0_tm_1, 0); |
| 95 | __m128i _out0tm2 = __lsx_vld(output0_tm_2, 0); |
| 96 | __m128i _out0tm3 = __lsx_vld(output0_tm_3, 0); |
| 97 | __m128i _out0tm4 = __lsx_vld(output0_tm_4, 0); |
| 98 | __m128i _out0tm5 = __lsx_vld(output0_tm_5, 0); |
| 99 | |
| 100 | __m128i _tmp02a = __lsx_vadd_w(_out0tm1, _out0tm2); |
| 101 | __m128i _tmp13a = __lsx_vsub_w(_out0tm1, _out0tm2); |
| 102 | |
| 103 | __m128i _tmp02b = __lsx_vadd_w(_out0tm3, _out0tm4); |
| 104 | __m128i _tmp13b = __lsx_vsub_w(_out0tm3, _out0tm4); |
| 105 | |
| 106 | __m128i _tmp0m = __lsx_vadd_w(__lsx_vadd_w(_out0tm0, _tmp02a), _tmp02b); |
| 107 | __m128i _tmp1m = __lsx_vadd_w(_tmp13a, __lsx_vslli_w(_tmp13b, 1)); |
| 108 | __m128i _tmp2m = __lsx_vadd_w(_tmp02a, __lsx_vslli_w(_tmp02b, 2)); |
| 109 | __m128i _tmp3m = __lsx_vadd_w(__lsx_vadd_w(_tmp13a, __lsx_vslli_w(_out0tm5, 2)), __lsx_vslli_w(_tmp13b, 3)); |
| 110 | |
| 111 | _tmp0m = __lsx_vslli_w(_tmp0m, 2); |
| 112 | _tmp1m = __lsx_vslli_w(_tmp1m, 2); |
| 113 | _tmp2m = __lsx_vslli_w(_tmp2m, 2); |
| 114 | _tmp3m = __lsx_vslli_w(_tmp3m, 2); |
| 115 | |
| 116 | __lsx_vst(_tmp0m, tmp[0][m], 0); |
| 117 | __lsx_vst(_tmp1m, tmp[1][m], 0); |
| 118 | __lsx_vst(_tmp2m, tmp[2][m], 0); |
| 119 | __lsx_vst(_tmp3m, tmp[3][m], 0); |
| 120 | |
| 121 | output0_tm_0 += tiles * 24; |
| 122 | output0_tm_1 += tiles * 24; |
| 123 | output0_tm_2 += tiles * 24; |
| 124 | output0_tm_3 += tiles * 24; |
| 125 | output0_tm_4 += tiles * 24; |
| 126 | output0_tm_5 += tiles * 24; |
| 127 | } |
| 128 | |
| 129 | for (int m = 0; m < 4; m++) |
| 130 | { |
| 131 | __m128i _tmp00 = __lsx_vld(tmp[m][0], 0); |
| 132 | __m128i _tmp01 = __lsx_vld(tmp[m][1], 0); |
| 133 | __m128i _tmp02 = __lsx_vld(tmp[m][2], 0); |
| 134 | __m128i _tmp03 = __lsx_vld(tmp[m][3], 0); |
| 135 | __m128i _tmp04 = __lsx_vld(tmp[m][4], 0); |
| 136 | __m128i _tmp05 = __lsx_vld(tmp[m][5], 0); |
| 137 | |
| 138 | __m128i _tmp02a = __lsx_vadd_w(_tmp01, _tmp02); |
| 139 | __m128i _tmp13a = __lsx_vsub_w(_tmp01, _tmp02); |
| 140 | |
| 141 | __m128i _tmp02b = __lsx_vadd_w(_tmp03, _tmp04); |
| 142 | __m128i _tmp13b = __lsx_vsub_w(_tmp03, _tmp04); |
| 143 | |
| 144 | __m128i _out00 = __lsx_vadd_w(__lsx_vadd_w(_tmp00, _tmp02a), _tmp02b); |
| 145 | __m128i _out01 = __lsx_vadd_w(_tmp13a, __lsx_vslli_w(_tmp13b, 1)); |
| 146 | __m128i _out02 = __lsx_vadd_w(_tmp02a, __lsx_vslli_w(_tmp02b, 2)); |
| 147 | __m128i _out03 = __lsx_vadd_w(__lsx_vadd_w(_tmp05, _tmp13a), __lsx_vslli_w(_tmp13b, 3)); |
| 148 | |
| 149 | // TODO use integer trick for division by 576 |
| 150 | __m128 _v576 = __lsx_vreplfr2vr_s(1.0 / 576); |
| 151 | _out00 = __lsx_vftint_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(_out00), _v576)); |
| 152 | _out01 = __lsx_vftint_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(_out01), _v576)); |
| 153 | _out02 = __lsx_vftint_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(_out02), _v576)); |
| 154 | _out03 = __lsx_vftint_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(_out03), _v576)); |
| 155 | |
| 156 | __lsx_vst(_out00, output0, 0); |
| 157 | __lsx_vst(_out01, output0 + 4, 0); |
| 158 | __lsx_vst(_out02, output0 + 8, 0); |
| 159 | __lsx_vst(_out03, output0 + 12, 0); |
| 160 | |
| 161 | output0 += outw * 4; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |