1 /*
2 * Copyright (c) 2022 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <cstddef>
26 #include <arm_neon.h>
27
28 namespace arm_conv {
29 namespace winograd {
30 namespace weight_transform {
31
arm_fp32_2x2_3x3(unsigned int n_channels,const float * inptr,size_t ld_weight_row,size_t ld_weight_col,float * outptr,size_t matrix_stride)32 void arm_fp32_2x2_3x3(
33 unsigned int n_channels,
34 const float *inptr, size_t ld_weight_row, size_t ld_weight_col,
35 float *outptr, size_t matrix_stride
36 )
37 {
38 constexpr auto inner_tile_i = 4u;
39 constexpr auto inner_tile_j = 4u;
40
41 #ifdef __aarch64__
42 // For each output channel
43 for (; n_channels >= 4u; n_channels -= 4)
44 {
45 // Matrices used and computed in this kernel
46 float32x4_t w[3][3], Ww[inner_tile_i][3], V[inner_tile_i][inner_tile_j];
47
48 // Read weights
49 for (int i = 0; i < 3; i++)
50 {
51 for (int j = 0; j < 3; j++)
52 {
53 w[i][j] = vld1q_f32(inptr + i*ld_weight_row + j*ld_weight_col);
54 }
55 }
56
57 // Compute the matrix W w
58 for (int j = 0; j < 3; j++)
59 {
60 Ww[0][j] = w[0][j];
61
62 // Ww[1][j] = 0.5*(w[0][j] + w[1][j] + w[2][j]);
63 Ww[1][j] = vmulq_n_f32(vaddq_f32(vaddq_f32(w[0][j], w[1][j]), w[2][j]), 0.5f);
64
65 // Ww[2][j] = 0.5*(w[0][j] - w[1][j] + w[2][j]);
66 Ww[2][j] = vmulq_n_f32(vaddq_f32(vsubq_f32(w[0][j], w[1][j]), w[2][j]), 0.5f);
67
68 Ww[3][j] = w[2][j];
69 }
70
71 // Compute V = W w WT
72 for (auto i = 0u; i < inner_tile_i; i++)
73 {
74 V[i][0] = Ww[i][0];
75
76 // V[i][1] = 0.5*(Ww[i][0] + Ww[i][1] + Ww[i][2]);
77 V[i][1] = vmulq_n_f32(vaddq_f32(vaddq_f32(Ww[i][0], Ww[i][1]), Ww[i][2]), 0.5f);
78
79 // V[i][2] = 0.5*(Ww[i][0] - Ww[i][1] + Ww[i][2]);
80 V[i][2] = vmulq_n_f32(vaddq_f32(vsubq_f32(Ww[i][0], Ww[i][1]), Ww[i][2]), 0.5f);
81
82 V[i][3] = Ww[i][2];
83 }
84
85 // Store the transformed weights
86 for (auto i = 0u, m = 0u; i < inner_tile_i; i++)
87 {
88 for (auto j = 0u; j < inner_tile_j; j++, m++)
89 {
90 vst1q_f32(outptr + m*matrix_stride, V[i][j]);
91 }
92 }
93
94 inptr += 4;
95 outptr += 4;
96 }
97 #endif // __aarch64__
98 for (; n_channels >= 2u; n_channels -= 2)
99 {
100 // Matrices used and computed in this kernel
101 float32x2_t w[3][3], Ww[inner_tile_i][3], V[inner_tile_i][inner_tile_j];
102
103 // Read weights
104 for (int i = 0; i < 3; i++)
105 {
106 for (int j = 0; j < 3; j++)
107 {
108 w[i][j] = vld1_f32(inptr + i*ld_weight_row + j*ld_weight_col);
109 }
110 }
111
112 // Compute the matrix W w
113 for (int j = 0; j < 3; j++)
114 {
115 Ww[0][j] = w[0][j];
116
117 // Ww[1][j] = 0.5*(w[0][j] + w[1][j] + w[2][j]);
118 Ww[1][j] = vmul_n_f32(vadd_f32(vadd_f32(w[0][j], w[1][j]), w[2][j]), 0.5f);
119
120 // Ww[2][j] = 0.5*(w[0][j] - w[1][j] + w[2][j]);
121 Ww[2][j] = vmul_n_f32(vadd_f32(vsub_f32(w[0][j], w[1][j]), w[2][j]), 0.5f);
122
123 Ww[3][j] = w[2][j];
124 }
125
126 // Compute V = W w WT
127 for (auto i = 0u; i < inner_tile_i; i++)
128 {
129 V[i][0] = Ww[i][0];
130
131 // V[i][1] = 0.5*(Ww[i][0] + Ww[i][1] + Ww[i][2]);
132 V[i][1] = vmul_n_f32(vadd_f32(vadd_f32(Ww[i][0], Ww[i][1]), Ww[i][2]), 0.5f);
133
134 // V[i][2] = 0.5*(Ww[i][0] - Ww[i][1] + Ww[i][2]);
135 V[i][2] = vmul_n_f32(vadd_f32(vsub_f32(Ww[i][0], Ww[i][1]), Ww[i][2]), 0.5f);
136
137 V[i][3] = Ww[i][2];
138 }
139
140 // Store the transformed weights
141 for (auto i = 0u, m = 0u; i < inner_tile_i; i++)
142 {
143 for (auto j = 0u; j < inner_tile_j; j++, m++)
144 {
145 vst1_f32(outptr + m*matrix_stride, V[i][j]);
146 }
147 }
148
149 inptr += 2;
150 outptr += 2;
151 }
152 for (; n_channels; n_channels--)
153 {
154 // Matrices used and computed in this kernel
155 float w[3][3], Ww[inner_tile_i][3], V[inner_tile_i][inner_tile_j];
156
157 // Read weights
158 for (int i = 0; i < 3; i++)
159 {
160 for (int j = 0; j < 3; j++)
161 {
162 w[i][j] = *(inptr + i*ld_weight_row + j*ld_weight_col);
163 }
164 }
165
166 // Compute the matrix W w
167 for (int j = 0; j < 3; j++)
168 {
169 Ww[0][j] = w[0][j];
170 Ww[1][j] = 0.5*(w[0][j] + w[1][j] + w[2][j]);
171 Ww[2][j] = 0.5*(w[0][j] - w[1][j] + w[2][j]);
172 Ww[3][j] = w[2][j];
173 }
174
175 // Compute V = W w WT
176 for (auto i = 0u; i < inner_tile_i; i++)
177 {
178 V[i][0] = Ww[i][0];
179 V[i][1] = 0.5*(Ww[i][0] + Ww[i][1] + Ww[i][2]);
180 V[i][2] = 0.5*(Ww[i][0] - Ww[i][1] + Ww[i][2]);
181 V[i][3] = Ww[i][2];
182 }
183
184 // Store the transformed weights
185 for (auto i = 0u, m = 0u; i < inner_tile_i; i++)
186 {
187 for (auto j = 0u; j < inner_tile_j; j++, m++)
188 {
189 *(outptr + m*matrix_stride) = V[i][j];
190 }
191 }
192
193 inptr++;
194 outptr++;
195 }
196 }
197
198 } // namespace weight_transform
199 } // namespace winograd
200 } // namespace arm_conv
201