2 * Copyright (c) 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
25 #include "transforms/output.hpp"
26 #include "winograd_gemm.hpp"
32 using Transform = WinogradGEMM<4, 4, 3, 3>::OutputTransform<float>;
36 int Transform::ops_performed(const Tensor4DShape &shape)
38 // NOTE: Cost in FLOPs rather than instructions or uops.
39 const int tile_M = iceildiv(shape.n_rows, 4);
40 const int tile_N = iceildiv(shape.n_cols, 4);
41 return 170 * tile_M * tile_N * shape.n_channels;
44 /* F(4x4, 3x3) constructs 4x4 output tiles from a 3x3 convolution. Since we use
45 * enough tiles to cover the output space each output tile may contain up to 3
46 * padded values to the right and bottom columns or rows of the tile, e.g.:
48 * ________ ________ ________ ________
49 * | | | X| | X X| | X X X|
50 * | | | X| | X X| | X X X|
51 * | | | X| | X X| | X X X|
52 * |_______| |______X| |____X_X| |__X_X_X|
54 * ________ ________ ________ ________
55 * | | | X| | X X| | X X X|
56 * | | | X| | X X| | X X X|
57 * | | | X| | X X| | X X X|
58 * |X_X_X_X| |X_X_X_X| |X_X_X_X| |X_X_X_X|
60 * ________ ________ ________ ________
61 * | | | X| | X X| | X X X|
62 * | | | X| | X X| | X X X|
63 * |X X X X| |X X X X| |X X X X| |X X X X|
64 * |X_X_X_X| |X_X_X_X| |X_X_X_X| |X_X_X_X|
66 * ________ ________ ________ ________
67 * | | | X| | X X| | X X X|
68 * |X X X X| |X X X X| |X X X X| |X X X X|
69 * |X X X X| |X X X X| |X X X X| |X X X X|
70 * |X_X_X_X| |X_X_X_X| |X_X_X_X| |X_X_X_X|
73 * We provide a specialised output transform for each of these instances.
77 template <int pad_bottom, int pad_right>
78 void Transform::process_tile(
80 const float* const matrix_base,
81 const int matrix_stride,
83 const int output_row_stride,
84 const int output_col_stride
87 constexpr int cells_i = 4 - pad_bottom;
88 constexpr int cells_j = 4 - pad_right;
90 // Construct a map to the output cells
91 float *outptrs[cells_i][cells_j];
92 for (int i = 0; i < cells_i; i++)
94 for (int j = 0; j < cells_j; j++)
96 outptrs[i][j] = output + i*output_row_stride + j*output_col_stride;
99 const float *inptr = matrix_base;
101 // For each channel of the output
102 int channels_remaining = n_channels;
104 for (; channels_remaining >= 4; channels_remaining -= 4)
106 // Matrices used and computed during this transform
107 float32x4_t F[6][6], FZ[6][4], f[4][4];
109 // Read a 6x6 tile in the Winograd domain
110 for (int i = 0, m = 0; i < 6; i++)
112 for (int j = 0; j < 6; j++, m++)
114 F[i][j] = vld1q_f32(inptr + m*matrix_stride);
119 // Compute the matrix F Z
120 for (int i = 0; i < 6; i++)
122 // FZ[i][0] = 1*F[i][0] + 1*F[i][1] + 1*F[i][2] + 1*F[i][3] + 1*F[i][4];
123 FZ[i][0] = vaddq_f32(vaddq_f32(vaddq_f32(F[i][0], F[i][1]), vaddq_f32(F[i][2], F[i][3])), F[i][4]);
125 // FZ[i][1] = 1*F[i][1] + -1*F[i][2] + 2*F[i][3] + -2*F[i][4];
126 FZ[i][1] = vmlaq_n_f32(vsubq_f32(F[i][1], F[i][2]), vsubq_f32(F[i][3], F[i][4]), 2.0f);
128 // FZ[i][2] = 1*F[i][1] + 1*F[i][2] + 4*F[i][3] + 4*F[i][4];
129 FZ[i][2] = vmlaq_n_f32(vaddq_f32(F[i][1], F[i][2]), vaddq_f32(F[i][3], F[i][4]), 4.0f);
131 // FZ[i][3] = 1*F[i][1] + -1*F[i][2] + 8*F[i][3] + -8*F[i][4] + 1*F[i][5];
132 FZ[i][3] = vaddq_f32(vmlaq_n_f32(vsubq_f32(F[i][1], F[i][2]), vsubq_f32(F[i][3], F[i][4]), 8.0f), F[i][5]);
135 // Compute the output tile f = ZT F Z
136 for (int j = 0; j < 4; j++)
138 // f[0][j] = 1*FZ[0][j] + 1*FZ[1][j] + 1*FZ[2][j] + 1*FZ[3][j] + 1*FZ[4][j];
139 f[0][j] = vaddq_f32(vaddq_f32(vaddq_f32(FZ[0][j], FZ[1][j]), vaddq_f32(FZ[2][j], FZ[3][j])), FZ[4][j]);
141 // f[1][j] = 1*FZ[1][j] + -1*FZ[2][j] + 2*FZ[3][j] + -2*FZ[4][j];
142 f[1][j] = vmlaq_n_f32(vsubq_f32(FZ[1][j], FZ[2][j]), vsubq_f32(FZ[3][j], FZ[4][j]), 2.0f);
144 // f[2][j] = 1*FZ[1][j] + 1*FZ[2][j] + 4*FZ[3][j] + 4*FZ[4][j];
145 f[2][j] = vmlaq_n_f32(vaddq_f32(FZ[1][j], FZ[2][j]), vaddq_f32(FZ[3][j], FZ[4][j]), 4.0f);
147 // f[3][j] = 1*FZ[1][j] + -1*FZ[2][j] + 8*FZ[3][j] + -8*FZ[4][j] + 1*FZ[5][j];
148 f[3][j] = vaddq_f32(vmlaq_n_f32(vsubq_f32(FZ[1][j], FZ[2][j]), vsubq_f32(FZ[3][j], FZ[4][j]), 8.0f), FZ[5][j]);
151 // Write out the output tile
152 for (int i = 0; i < cells_i; i++)
154 for (int j = 0; j < cells_j; j++)
156 vst1q_f32(outptrs[i][j], f[i][j]);
161 #endif // __aarch64__
163 for (; channels_remaining >= 2; channels_remaining -= 2)
165 // Matrices used and computed during this transform
166 float32x2_t F[6][6], FZ[6][4], f[4][4];
168 // Read a 6x6 tile in the Winograd domain
169 for (int i = 0, m = 0; i < 6; i++)
171 for (int j = 0; j < 6; j++, m++)
173 F[i][j] = vld1_f32(inptr + m*matrix_stride);
178 // Compute the matrix F Z
179 for (int i = 0; i < 6; i++)
181 // FZ[i][0] = 1*F[i][0] + 1*F[i][1] + 1*F[i][2] + 1*F[i][3] + 1*F[i][4];
182 FZ[i][0] = vadd_f32(vadd_f32(vadd_f32(F[i][0], F[i][1]), vadd_f32(F[i][2], F[i][3])), F[i][4]);
184 // FZ[i][1] = 1*F[i][1] + -1*F[i][2] + 2*F[i][3] + -2*F[i][4];
185 FZ[i][1] = vmla_n_f32(vsub_f32(F[i][1], F[i][2]), vsub_f32(F[i][3], F[i][4]), 2.0f);
187 // FZ[i][2] = 1*F[i][1] + 1*F[i][2] + 4*F[i][3] + 4*F[i][4];
188 FZ[i][2] = vmla_n_f32(vadd_f32(F[i][1], F[i][2]), vadd_f32(F[i][3], F[i][4]), 4.0f);
190 // FZ[i][3] = 1*F[i][1] + -1*F[i][2] + 8*F[i][3] + -8*F[i][4] + 1*F[i][5];
191 FZ[i][3] = vadd_f32(vmla_n_f32(vsub_f32(F[i][1], F[i][2]), vsub_f32(F[i][3], F[i][4]), 8.0f), F[i][5]);
194 // Compute the output tile f = ZT F Z
195 for (int j = 0; j < 4; j++)
197 // f[0][j] = 1*FZ[0][j] + 1*FZ[1][j] + 1*FZ[2][j] + 1*FZ[3][j] + 1*FZ[4][j];
198 f[0][j] = vadd_f32(vadd_f32(vadd_f32(FZ[0][j], FZ[1][j]), vadd_f32(FZ[2][j], FZ[3][j])), FZ[4][j]);
200 // f[1][j] = 1*FZ[1][j] + -1*FZ[2][j] + 2*FZ[3][j] + -2*FZ[4][j];
201 f[1][j] = vmla_n_f32(vsub_f32(FZ[1][j], FZ[2][j]), vsub_f32(FZ[3][j], FZ[4][j]), 2.0f);
203 // f[2][j] = 1*FZ[1][j] + 1*FZ[2][j] + 4*FZ[3][j] + 4*FZ[4][j];
204 f[2][j] = vmla_n_f32(vadd_f32(FZ[1][j], FZ[2][j]), vadd_f32(FZ[3][j], FZ[4][j]), 4.0f);
206 // f[3][j] = 1*FZ[1][j] + -1*FZ[2][j] + 8*FZ[3][j] + -8*FZ[4][j] + 1*FZ[5][j];
207 f[3][j] = vadd_f32(vmla_n_f32(vsub_f32(FZ[1][j], FZ[2][j]), vsub_f32(FZ[3][j], FZ[4][j]), 8.0f), FZ[5][j]);
210 // Write out the output tile
211 for (int i = 0; i < cells_i; i++)
213 for (int j = 0; j < cells_j; j++)
215 vst1_f32(outptrs[i][j], f[i][j]);
221 for (; channels_remaining; channels_remaining--)
223 // Matrices used and computed during this transform
224 float F[6][6], FZ[6][4], f[4][4];
226 // Read a 6x6 tile in the Winograd domain
227 for (int i = 0, m = 0; i < 6; i++)
229 for (int j = 0; j < 6; j++, m++)
231 F[i][j] = *(inptr + m*matrix_stride);
236 // Compute the matrix F Z
237 for (int i = 0; i < 6; i++)
239 FZ[i][0] = 1*F[i][0] + 1*F[i][1] + 1*F[i][2] + 1*F[i][3] + 1*F[i][4];
240 FZ[i][1] = 1*F[i][1] + -1*F[i][2] + 2*F[i][3] + -2*F[i][4];
241 FZ[i][2] = 1*F[i][1] + 1*F[i][2] + 4*F[i][3] + 4*F[i][4];
242 FZ[i][3] = 1*F[i][1] + -1*F[i][2] + 8*F[i][3] + -8*F[i][4] + 1*F[i][5];
245 // Compute the output tile f = ZT F Z
246 for (int j = 0; j < 4; j++)
248 f[0][j] = 1*FZ[0][j] + 1*FZ[1][j] + 1*FZ[2][j] + 1*FZ[3][j] + 1*FZ[4][j];
249 f[1][j] = 1*FZ[1][j] + -1*FZ[2][j] + 2*FZ[3][j] + -2*FZ[4][j];
250 f[2][j] = 1*FZ[1][j] + 1*FZ[2][j] + 4*FZ[3][j] + 4*FZ[4][j];
251 f[3][j] = 1*FZ[1][j] + -1*FZ[2][j] + 8*FZ[3][j] + -8*FZ[4][j] + 1*FZ[5][j];
254 // Write out the output tile
255 for (int i = 0; i < cells_i; i++)
257 for (int j = 0; j < cells_j; j++)
259 *(outptrs[i][j]++) = f[i][j];
267 const Transform::TileFn Transform::tile_fns[max_pad_bottom][max_pad_right] =
270 Transform::template process_tile<0, 0>,
271 Transform::template process_tile<0, 1>,
272 Transform::template process_tile<0, 2>,
273 Transform::template process_tile<0, 3>,
276 Transform::template process_tile<1, 0>,
277 Transform::template process_tile<1, 1>,
278 Transform::template process_tile<1, 2>,
279 Transform::template process_tile<1, 3>,
282 Transform::template process_tile<2, 0>,
283 Transform::template process_tile<2, 1>,
284 Transform::template process_tile<2, 2>,
285 Transform::template process_tile<2, 3>,
288 Transform::template process_tile<3, 0>,
289 Transform::template process_tile<3, 1>,
290 Transform::template process_tile<3, 2>,
291 Transform::template process_tile<3, 3>,
295 template struct WinogradGEMM<4, 4, 3, 3>::OutputTransform<float>;
296 } // namespace winograd