Elffuss BraidHub

kikocis/ncnn public

master · 6.1 KiBRawPermalink
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
15static void conv3x3s1_winograd43_transform_input_pack8_int8_msa(const Mat& bottom_blob, Mat& bottom_blob_tm, const Option& opt)
16{
17 const int w = bottom_blob.w;
18 const int h = bottom_blob.h;
19 const int inch = bottom_blob.c;
20
21 const int w_tiles = (w - 2) / 4;
22 const int h_tiles = (h - 2) / 4;
23 const int tiles = w_tiles * h_tiles;
24
25 // const float itm[6][6] = {
26 // {4.0f, 0.0f, -5.0f, 0.0f, 1.0f, 0.0f},
27 // {0.0f,-4.0f, -4.0f, 1.0f, 1.0f, 0.0f},
28 // {0.0f, 4.0f, -4.0f,-1.0f, 1.0f, 0.0f},
29 // {0.0f,-2.0f, -1.0f, 2.0f, 1.0f, 0.0f},
30 // {0.0f, 2.0f, -1.0f,-2.0f, 1.0f, 0.0f},
31 // {0.0f, 4.0f, 0.0f,-5.0f, 0.0f, 1.0f}
32 // };
33
34 // 0 = 4 * r00 - 5 * r02 + r04
35 // 1 = -4 * (r01 + r02) + r04 + r03
36 // 2 = 4 * (r01 - r02) + r04 - r03
37 // 3 = -2 * (r01 - r03) + r04 - r02
38 // 4 = 2 * (r01 - r03) + r04 - r02
39 // 5 = 4 * r01 - 5 * r03 + r05
40
41 #pragma omp parallel for num_threads(opt.num_threads)
42 for (int q = 0; q < inch; q++)
43 {
44 const Mat img0 = bottom_blob.channel(q);
45 Mat img0_tm = bottom_blob_tm.channel(q);
46
47 short tmp[6][6][8];
48
49 // tile
50 for (int i = 0; i < h_tiles; i++)
51 {
52 for (int j = 0; j < w_tiles; j++)
53 {
54 const signed char* r0 = img0.row<const signed char>(i * 4) + (j * 4) * 8;
55
56 for (int m = 0; m < 6; m++)
57 {
58 v16i8 _r00_01 = __msa_ld_b(r0, 0);
59 v16i8 _r02_03 = __msa_ld_b(r0 + 16, 0);
60 v16i8 _r04_05 = __msa_ld_b(r0 + 32, 0);
61 v16i8 _extr0001 = __msa_clti_s_b(_r00_01, 0);
62 v16i8 _extr0203 = __msa_clti_s_b(_r02_03, 0);
63 v16i8 _extr0405 = __msa_clti_s_b(_r04_05, 0);
64 v8i16 _r00 = (v8i16)__msa_ilvr_b(_extr0001, _r00_01);
65 v8i16 _r01 = (v8i16)__msa_ilvl_b(_extr0001, _r00_01);
66 v8i16 _r02 = (v8i16)__msa_ilvr_b(_extr0203, _r02_03);
67 v8i16 _r03 = (v8i16)__msa_ilvl_b(_extr0203, _r02_03);
68 v8i16 _r04 = (v8i16)__msa_ilvr_b(_extr0405, _r04_05);
69 v8i16 _r05 = (v8i16)__msa_ilvl_b(_extr0405, _r04_05);
70
71 v8i16 _v5 = __msa_fill_h(5);
72
73 v8i16 _tmp0m = __msa_subv_h(__msa_addv_h(__msa_slli_h(_r00, 2), _r04), __msa_mulv_h(_r02, _v5));
74 v8i16 _tmp1m = __msa_subv_h(__msa_addv_h(_r04, _r03), __msa_slli_h(__msa_addv_h(_r01, _r02), 2));
75 v8i16 _tmp2m = __msa_addv_h(__msa_subv_h(_r04, _r03), __msa_slli_h(__msa_subv_h(_r01, _r02), 2));
76 v8i16 _tmp3m = __msa_subv_h(__msa_subv_h(_r04, _r02), __msa_slli_h(__msa_subv_h(_r01, _r03), 1));
77 v8i16 _tmp4m = __msa_addv_h(__msa_subv_h(_r04, _r02), __msa_slli_h(__msa_subv_h(_r01, _r03), 1));
78 v8i16 _tmp5m = __msa_subv_h(__msa_addv_h(__msa_slli_h(_r01, 2), _r05), __msa_mulv_h(_r03, _v5));
79
80 __msa_st_h(_tmp0m, tmp[0][m], 0);
81 __msa_st_h(_tmp1m, tmp[1][m], 0);
82 __msa_st_h(_tmp2m, tmp[2][m], 0);
83 __msa_st_h(_tmp3m, tmp[3][m], 0);
84 __msa_st_h(_tmp4m, tmp[4][m], 0);
85 __msa_st_h(_tmp5m, tmp[5][m], 0);
86
87 r0 += w * 8;
88 }
89
90 short* r0_tm_0 = (short*)img0_tm + (i * w_tiles + j) * 8;
91 short* r0_tm_1 = r0_tm_0 + tiles * 8;
92 short* r0_tm_2 = r0_tm_0 + tiles * 16;
93 short* r0_tm_3 = r0_tm_0 + tiles * 24;
94 short* r0_tm_4 = r0_tm_0 + tiles * 32;
95 short* r0_tm_5 = r0_tm_0 + tiles * 40;
96
97 for (int m = 0; m < 6; m++)
98 {
99 v8i16 _tmp00 = __msa_ld_h(tmp[m][0], 0);
100 v8i16 _tmp01 = __msa_ld_h(tmp[m][1], 0);
101 v8i16 _tmp02 = __msa_ld_h(tmp[m][2], 0);
102 v8i16 _tmp03 = __msa_ld_h(tmp[m][3], 0);
103 v8i16 _tmp04 = __msa_ld_h(tmp[m][4], 0);
104 v8i16 _tmp05 = __msa_ld_h(tmp[m][5], 0);
105
106 v8i16 _v5 = __msa_fill_h(5);
107
108 v8i16 _r0tm0 = __msa_subv_h(__msa_addv_h(__msa_slli_h(_tmp00, 2), _tmp04), __msa_mulv_h(_tmp02, _v5));
109 v8i16 _r0tm1 = __msa_subv_h(__msa_addv_h(_tmp04, _tmp03), __msa_slli_h(__msa_addv_h(_tmp01, _tmp02), 2));
110 v8i16 _r0tm2 = __msa_addv_h(__msa_subv_h(_tmp04, _tmp03), __msa_slli_h(__msa_subv_h(_tmp01, _tmp02), 2));
111 v8i16 _r0tm3 = __msa_subv_h(__msa_subv_h(_tmp04, _tmp02), __msa_slli_h(__msa_subv_h(_tmp01, _tmp03), 1));
112 v8i16 _r0tm4 = __msa_addv_h(__msa_subv_h(_tmp04, _tmp02), __msa_slli_h(__msa_subv_h(_tmp01, _tmp03), 1));
113 v8i16 _r0tm5 = __msa_subv_h(__msa_addv_h(__msa_slli_h(_tmp01, 2), _tmp05), __msa_mulv_h(_tmp03, _v5));
114
115 __msa_st_h(_r0tm0, r0_tm_0, 0);
116 __msa_st_h(_r0tm1, r0_tm_1, 0);
117 __msa_st_h(_r0tm2, r0_tm_2, 0);
118 __msa_st_h(_r0tm3, r0_tm_3, 0);
119 __msa_st_h(_r0tm4, r0_tm_4, 0);
120 __msa_st_h(_r0tm5, r0_tm_5, 0);
121
122 r0_tm_0 += tiles * 48;
123 r0_tm_1 += tiles * 48;
124 r0_tm_2 += tiles * 48;
125 r0_tm_3 += tiles * 48;
126 r0_tm_4 += tiles * 48;
127 r0_tm_5 += tiles * 48;
128 }
129 }
130 }
131 }
132}