blob: 7e82dc4ecd4c911f74194159eb9d24a5a86b88f2 [file] [log] [blame]
Pablo Tello89519332017-11-17 11:52:36 +00001/*
Pablo Tello9ceebbe2018-01-10 16:44:13 +00002 * Copyright (c) 2017-2018 ARM Limited.
Pablo Tello89519332017-11-17 11:52:36 +00003 *
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 */
Georgios Pinitas9fb11592018-04-26 20:34:58 +010024#include "arm_compute/core/NEON/kernels/NEWinogradConvolutionLayerKernel.h"
Pablo Tello89519332017-11-17 11:52:36 +000025
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +010026#include "arm_compute/core/AccessWindowStatic.h"
Pablo Tello89519332017-11-17 11:52:36 +000027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +010029#include "arm_compute/core/IAccessWindow.h"
Pablo Tello89519332017-11-17 11:52:36 +000030#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/TensorInfo.h"
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +010032#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Pablo Tello3d4968a2017-12-04 15:03:35 +000035#include "support/ToolchainSupport.h"
36
Pablo Tello89519332017-11-17 11:52:36 +000037namespace arm_compute
38{
Pablo Tello52140b42018-01-30 14:48:11 +000039//Batched Gemms
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +010040
41namespace
42{
43Status validate_arguments_winograd_gemm(const ITensorInfo *a, const ITensorInfo *b, const ITensor *c, const ITensorInfo *output, const float alpha, const float beta,
44 const GEMMInfo &gemm_info = GEMMInfo())
45{
46 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(a);
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(b);
48 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
49
50 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F32);
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
52 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
53 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
54
55 if(c != nullptr)
56 {
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, c->info());
58 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->dimension(1) != c->info()->dimension(1), "The matrix C must have the same number of rows as the matrix A");
59 ARM_COMPUTE_RETURN_ERROR_ON_MSG(b->dimension(0) != c->info()->dimension(0), "The matrix C must have the same number of columns as the matrix B");
60 }
61
62 if(output->total_size() != 0)
63 {
64 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, output);
65 ARM_COMPUTE_RETURN_ERROR_ON_MSG(b->dimension(0) != output->dimension(0), "The output matrix must have the same number of columns as the matrix B");
66 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->dimension(1) != output->dimension(1), "The output matrix must have the same number of rows as the matrix A");
67 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() != a->num_dimensions());
68 }
69
70 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->dimension(0) != b->dimension(1), "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
71 ARM_COMPUTE_UNUSED(alpha, beta);
72 return Status{};
73}
74
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +010075Status validate_arguments_winograd_weight_trans(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info)
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +010076{
77 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
78 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
79 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
80
81 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
82 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
83 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(idx_width) != 3 && input->dimension(idx_width) != 5);
84 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(idx_width) != input->dimension(idx_height));
85 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +010086 const Size2D &output_tile = winograd_info.output_tile_size;
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +010087 ARM_COMPUTE_RETURN_ERROR_ON(output_tile != Size2D(2U, 2U) && output_tile != Size2D(4U, 4U));
88
89 // Checks performed when output is configured
90 if(output->total_size() != 0)
91 {
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +010092 const TensorInfo tensor_info_output = input->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_winograd_filter_transform_shape(*input, winograd_info));
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +010093
94 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
95 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
96 }
97
98 return Status{};
99}
100
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100101std::pair<Status, Window> validate_and_configure_window_winograd_weight_trans(ITensorInfo *input, ITensorInfo *output, const WinogradInfo &winograd_info)
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100102{
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100103 const Size2D kernel_dims = winograd_info.kernel_size;
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100104 // Output tensor auto inizialitation if not yet initialized
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100105 auto_init_if_empty(*output, input->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_winograd_filter_transform_shape(*input, winograd_info)));
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100106
107 unsigned int num_elems_processed_per_iteration_x = kernel_dims.width;
108 unsigned int num_elems_processed_per_iteration_y = kernel_dims.height;
109
110 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
111 bool window_changed = false;
112
113 AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
114 AccessWindowStatic output_access(output, 0, 0, output->dimension(0), output->dimension(1));
115 window_changed = update_window_and_padding(win, input_access, output_access);
116 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
117
118 Window win_collapsed = win.collapse(win, Window::DimZ);
119
120 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
121
122 return std::make_pair(err, win_collapsed);
123}
124
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100125Status validate_arguments_winograd_input_trans(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info)
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100126{
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100127 const Size2D &kernel_dims = winograd_info.kernel_size;
128 const PadStrideInfo &conv_info = winograd_info.convolution_info;
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100129 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
130 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
131 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
132 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.stride().first != 1 || conv_info.stride().second != 1, "Winograd input transform only supports unit strides");
133 ARM_COMPUTE_RETURN_ERROR_ON_MSG((kernel_dims.width != 3U && kernel_dims.width != 5U), "Winograd input transform only supports 3x3 and 5x5 kernels");
134 ARM_COMPUTE_RETURN_ERROR_ON_MSG((kernel_dims.width != kernel_dims.height), "Winograd input transform only supports 3x3 and 5x5 kernels");
135
136 // Validate configured output
137 if(output->total_size() != 0)
138 {
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100139 const TensorShape output_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100140
141 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
142 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
143 }
144
145 return Status{};
146}
147
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100148std::pair<Status, Window> validate_and_configure_window_winograd_input_trans(ITensorInfo *input, ITensorInfo *output, const WinogradInfo &winograd_info)
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100149{
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100150 const PadStrideInfo conv_info = winograd_info.convolution_info;
151 const Size2D output_tile_size = winograd_info.output_tile_size;
152 const Size2D kernel_dims = winograd_info.kernel_size;
153 const TensorShape output_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100154 // Output auto inizialitation if not yet initialized
155 auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape));
156
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100157 unsigned int num_elems_read_per_iteration_x = (output_tile_size.width + kernel_dims.width - 1);
158 unsigned int num_elems_read_per_iteration_y = (output_tile_size.height + kernel_dims.height - 1);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100159
160 Window win = calculate_max_window(*input, Steps(1, 1));
161
162 AccessWindowRectangle input_access(input, -conv_info.pad_left(), -conv_info.pad_top(), num_elems_read_per_iteration_x, num_elems_read_per_iteration_y);
163
164 bool window_changed = update_window_and_padding(win, input_access);
165
166 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
167 return std::make_pair(err, win);
168}
169
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100170Status validate_arguments_winograd_output_trans(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const WinogradInfo &winograd_info)
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100171{
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100172 const PadStrideInfo &conv_info = winograd_info.convolution_info;
173 const Size2D kernel_dims = winograd_info.kernel_size;
174
175 // Number of tiles along the X and Y direction
176 const unsigned int num_tiles_x = std::ceil((winograd_info.input_dimensions.x() - (kernel_dims.width - 1) + conv_info.pad_left() + conv_info.pad_right()) / 2.f);
177 const unsigned int num_tiles_y = std::ceil((winograd_info.input_dimensions.y() - (kernel_dims.height - 1) + conv_info.pad_top() + conv_info.pad_bottom()) / 2.f);
178 const Size2D num_tiles = Size2D(num_tiles_x, num_tiles_y);
179
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100180 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
181 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
182 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100183 ARM_COMPUTE_RETURN_ERROR_ON(winograd_info.output_data_layout != DataLayout::NCHW);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100184 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != num_tiles.area());
185 ARM_COMPUTE_RETURN_ERROR_ON_MSG((kernel_dims.width != 3U && kernel_dims.width != 5U), "Winograd output transform only supports 3x3 and 5x5 kernels");
186 ARM_COMPUTE_RETURN_ERROR_ON_MSG((kernel_dims.width != kernel_dims.height), "Winograd output transform only supports 3x3 and 5x5 kernels");
187 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((input->dimension(2) != size_t(16U)) && (input->dimension(2) != size_t(36U))), "Only 2x2 and 4x4 output tile is supported");
188 ARM_COMPUTE_UNUSED(kernel_dims);
189 if(bias != nullptr)
190 {
191 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
192 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != bias->dimension(0));
193 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() != size_t(1));
194 }
195
196 // Checks performed when output is configured
197 if(output->total_size() != 0)
198 {
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100199 const TensorInfo tensor_info_output = input->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_winograd_output_transform_shape(*input, winograd_info));
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100200 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
201 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
202 }
203 return Status{};
204}
205
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100206std::pair<Status, Window> validate_and_configure_window_winograd_output_trans(ITensorInfo *input, ITensorInfo *bias, ITensorInfo *output, const WinogradInfo &winograd_info)
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100207{
208 // Output tensor auto initialization if not yet initialized
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100209 auto_init_if_empty(*output, input->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_winograd_output_transform_shape(*input, winograd_info)));
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100210
211 constexpr unsigned int num_elems_processed_per_iteration = 1;
212
213 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
214 bool window_changed = false;
215
216 AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration, num_elems_processed_per_iteration);
217 AccessWindowStatic output_access(output, 0, 0, ceil_to_multiple(output->dimension(0), 2), ceil_to_multiple(output->dimension(1), 2));
218
219 if(bias != nullptr)
220 {
221 AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1));
222 window_changed = update_window_and_padding(win, input_access, bias_access, output_access);
223 }
224 else
225 {
226 window_changed = update_window_and_padding(win, input_access, output_access);
227 }
228 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
229
230 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
231 return std::make_pair(err, win);
232}
233} // namespace
Pablo Tellof6c572c2018-02-14 12:47:30 +0000234template <typename TIn, typename TOut, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
235NEWinogradLayerBatchedGEMMKernel<TIn, TOut, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::NEWinogradLayerBatchedGEMMKernel()
Pablo Tello52140b42018-01-30 14:48:11 +0000236 : _gemms()
Pablo Tello3d4968a2017-12-04 15:03:35 +0000237{
238}
239
Pablo Tellof6c572c2018-02-14 12:47:30 +0000240template <typename TIn, typename TOut, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
241void NEWinogradLayerBatchedGEMMKernel<TIn, TOut, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::configure(
Pablo Tello52140b42018-01-30 14:48:11 +0000242 const unsigned int n_gemms,
243 const int M, const int K, const int N,
Pablo Tellof6c572c2018-02-14 12:47:30 +0000244 const int a_matrix_stride,
245 const int a_row_stride,
246 const int b_matrix_stride,
247 const int b_row_stride,
248 const int c_matrix_stride,
249 const int c_row_stride,
250 const TIn *const a_ptr,
251 const TIn *const b_ptr,
252 TOut *const c_ptr)
Pablo Tello3d4968a2017-12-04 15:03:35 +0000253{
Pablo Tello52140b42018-01-30 14:48:11 +0000254 _gemms = support::cpp14::make_unique<MultiGEMM>(n_gemms, M, K, N, a_matrix_stride, a_row_stride, b_matrix_stride, b_row_stride, c_matrix_stride, c_row_stride, a_ptr, b_ptr, c_ptr);
Pablo Tello02541fb2017-12-15 09:48:59 +0000255 Window win;
Pablo Tello52140b42018-01-30 14:48:11 +0000256 auto win_last = _gemms->get_window();
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000257 win.set(Window::DimX, Window::Dimension(0, win_last, 1));
Pablo Tello89519332017-11-17 11:52:36 +0000258 INEKernel::configure(win);
259}
260
Pablo Tellof6c572c2018-02-14 12:47:30 +0000261template <typename TIn, typename TOut, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
262void NEWinogradLayerBatchedGEMMKernel<TIn, TOut, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::run(const Window &window, const ThreadInfo &info)
Pablo Tello89519332017-11-17 11:52:36 +0000263{
Pablo Tello89519332017-11-17 11:52:36 +0000264 ARM_COMPUTE_UNUSED(info);
265 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Pablo Tello02541fb2017-12-15 09:48:59 +0000266 const size_t first_gemm = window.x().start();
267 const size_t last_gemm = window.x().end();
Pablo Tello52140b42018-01-30 14:48:11 +0000268 _gemms->run(first_gemm, last_gemm);
Pablo Tello89519332017-11-17 11:52:36 +0000269}
Pablo Tellod6ca4782018-01-23 09:36:04 +0000270
Pablo Tellof6c572c2018-02-14 12:47:30 +0000271template <typename TIn, typename TOut, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
272unsigned int NEWinogradLayerBatchedGEMMKernel<TIn, TOut, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_number_gemms() const
273{
274 return WinogradBase::N_GEMMS;
275}
276
277template <typename TIn, typename TOut, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
278int NEWinogradLayerBatchedGEMMKernel<TIn, TOut, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_output_tile_rows() const
279{
280 return _output_tile_rows;
281}
282
283template <typename TIn, typename TOut, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
284int NEWinogradLayerBatchedGEMMKernel<TIn, TOut, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_output_tile_cols() const
285{
286 return _output_tile_cols;
287}
288
289template <typename TIn, typename TOut, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
290int NEWinogradLayerBatchedGEMMKernel<TIn, TOut, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_number_blocks() const
291{
292 return WinogradConv::N_BLOCK;
293}
294
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100295template <typename TIn, typename TOut, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
296Status NEWinogradLayerBatchedGEMMKernel<TIn, TOut, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensor *c,
297 const ITensorInfo *output, const float alpha, const float beta, const GEMMInfo &gemm_info)
298{
299 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_winograd_gemm(a, b, c, output, alpha, beta, gemm_info));
300 return Status{};
301}
302
Pablo Tellof6c572c2018-02-14 12:47:30 +0000303template class NEWinogradLayerBatchedGEMMKernel<float, float, 2, 2, 3, 3>;
304template class NEWinogradLayerBatchedGEMMKernel<float, float, 2, 2, 5, 5>;
Pablo Tellod6ca4782018-01-23 09:36:04 +0000305
306// Weights transform
307
Pablo Tellof6c572c2018-02-14 12:47:30 +0000308template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
309unsigned int NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_weight_storage_size(int n_output_channels, int n_input_channels) const
Pablo Tellod6ca4782018-01-23 09:36:04 +0000310{
Pablo Tello52140b42018-01-30 14:48:11 +0000311 const KernelShape shape(n_output_channels, KernelRows, KernelCols, n_input_channels);
312 return static_cast<unsigned int>(
Pablo Tellof6c572c2018-02-14 12:47:30 +0000313 // WinogradConv returns the size in bytes, we divide by `sizeof(T)` to express that in units of T
314 WinogradConv::get_kernel_storage_size(shape) / sizeof(T));
Pablo Tello52140b42018-01-30 14:48:11 +0000315}
316
Pablo Tellof6c572c2018-02-14 12:47:30 +0000317template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
318NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::NEWinogradLayerTransformWeightsKernel()
Pablo Tello52140b42018-01-30 14:48:11 +0000319 : _transform()
320{
321}
322
Pablo Tellof6c572c2018-02-14 12:47:30 +0000323template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
324int NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_matrix_stride(const KernelShape &kernel_shape) const
325{
326 return WinogradConv::get_kernel_matrix_stride(kernel_shape);
327}
328
329template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
330void NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::configure(
Pablo Tello52140b42018-01-30 14:48:11 +0000331 const ITensor *weights_hwio,
Pablo Tellof6c572c2018-02-14 12:47:30 +0000332 T *const output,
Pablo Tello52140b42018-01-30 14:48:11 +0000333 const int matrix_stride, /** Stride across matrices in the output. */
334 const int n_output_channels, /** Number of filters. */
335 const int n_input_channels) /** Number of channels in each filter. */
336{
337 const int matrix_row_stride = roundup(n_output_channels, WinogradConv::N_BLOCK);
Pablo Tellof6c572c2018-02-14 12:47:30 +0000338 _transform = support::cpp14::make_unique<WeightsTransform>(reinterpret_cast<T *>(weights_hwio->buffer()), output, matrix_stride, matrix_row_stride, n_output_channels,
Pablo Tello52140b42018-01-30 14:48:11 +0000339 n_input_channels);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000340 Window win;
Pablo Tello52140b42018-01-30 14:48:11 +0000341 auto win_last = _transform->get_window();
Pablo Tellod6ca4782018-01-23 09:36:04 +0000342 win.set(Window::DimX, Window::Dimension(0, win_last, 1));
343 INEKernel::configure(win);
344}
345
Pablo Tellof6c572c2018-02-14 12:47:30 +0000346template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
347void NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::run(const Window &window, const ThreadInfo &info)
Pablo Tellod6ca4782018-01-23 09:36:04 +0000348{
349 ARM_COMPUTE_UNUSED(info);
350 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
351 const size_t fst = window.x().start();
352 const size_t lst = window.x().end();
Pablo Tello52140b42018-01-30 14:48:11 +0000353 _transform->run(fst, lst);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000354}
355
Pablo Tellof6c572c2018-02-14 12:47:30 +0000356template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
357bool NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::is_parallelisable() const
Pablo Tellod6ca4782018-01-23 09:36:04 +0000358{
359 return false;
360}
361
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100362template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100363Status NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::validate(const ITensorInfo *input, const ITensorInfo *output,
364 const WinogradInfo &winograd_info)
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100365{
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100366 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_winograd_weight_trans(input, output, winograd_info));
367 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_winograd_weight_trans(input->clone().get(), output->clone().get(), winograd_info).first);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100368 return Status{};
369}
370
Pablo Tellof6c572c2018-02-14 12:47:30 +0000371template class NEWinogradLayerTransformWeightsKernel<float, 2, 2, 3, 3>;
372template class NEWinogradLayerTransformWeightsKernel<float, 2, 2, 5, 5>;
Pablo Tello52140b42018-01-30 14:48:11 +0000373
Pablo Tellod6ca4782018-01-23 09:36:04 +0000374// Input transform
375
Pablo Tellof6c572c2018-02-14 12:47:30 +0000376template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
377unsigned int NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_input_storage_size(
Pablo Tello52140b42018-01-30 14:48:11 +0000378 int n_batches, /** Number of batches in the input tensor. */
379 int n_channels, /** Number of feature maps in the input tensor. */
380 int n_rows, /** Number of rows in each feature map. */
381 int n_cols, /** Number of columns in each feature map. */
382 bool same_padding /** Use "SAME" padding, otherwise use "VALID". */
Pablo Tellof6c572c2018-02-14 12:47:30 +0000383) const
Pablo Tellod6ca4782018-01-23 09:36:04 +0000384{
Pablo Tello52140b42018-01-30 14:48:11 +0000385 // Construct shapes for the input and kernel tensors.
386 const Tensor4DShape input_shape(n_batches, n_rows, n_cols, n_channels);
387 const KernelShape kern_shape(1, KernelRows, KernelCols, n_channels);
388 const PaddingType padding = (same_padding) ? PADDING_SAME : PADDING_VALID;
389 // Return the size, converted into units of TIn
Pablo Tellof6c572c2018-02-14 12:47:30 +0000390 return static_cast<unsigned int>(WinogradConv::get_input_storage_size(kern_shape, input_shape, padding) / sizeof(T));
Pablo Tello52140b42018-01-30 14:48:11 +0000391}
392
Pablo Tellof6c572c2018-02-14 12:47:30 +0000393template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
394int NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_matrix_stride(
395 const KernelShape &kernel_shape, const Tensor4DShape &input_shape, const PaddingType padding_type) const
396{
397 return WinogradConv::get_input_matrix_stride(kernel_shape, input_shape, padding_type);
398}
399
400template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
401NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::NEWinogradLayerTransformInputKernel()
Pablo Tello52140b42018-01-30 14:48:11 +0000402 : _transform()
403{
404}
405
Pablo Tellof6c572c2018-02-14 12:47:30 +0000406template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
407void NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::configure(
408 const T *const input, /** Input tensor data */
409 const int n_batches, /** Number of batches in input tensor. */
410 const int n_rows, /** Number of rows in input tensor. */
411 const int n_cols, /** Number of columns in input tensor. */
412 const int n_channels, /** Number of channels in input tensor. */
413 const PaddingType padding, /** Padding type. */
414 T *const output, /** Base of output matrices. */
415 const int matrix_stride) /** Stride between output matrices. */
Pablo Tello52140b42018-01-30 14:48:11 +0000416{
417 // _input_matrix_row_stride(n_input_channels),
418 _transform = support::cpp14::make_unique<InputTransform>(input, n_batches, n_rows, n_cols, n_channels, padding, output, matrix_stride, n_channels);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000419 Window win;
Pablo Tello52140b42018-01-30 14:48:11 +0000420 auto win_last = _transform->get_window();
Pablo Tellod6ca4782018-01-23 09:36:04 +0000421 win.set(Window::DimX, Window::Dimension(0, win_last, 1));
422 INEKernel::configure(win);
423}
424
Pablo Tellof6c572c2018-02-14 12:47:30 +0000425template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
426void NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::run(const Window &window, const ThreadInfo &info)
Pablo Tellod6ca4782018-01-23 09:36:04 +0000427{
428 ARM_COMPUTE_UNUSED(info);
429 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
430 const size_t fst = window.x().start();
431 const size_t lst = window.x().end();
Pablo Tello52140b42018-01-30 14:48:11 +0000432 _transform->run(fst, lst);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000433}
Pablo Tello52140b42018-01-30 14:48:11 +0000434
Pablo Tellof6c572c2018-02-14 12:47:30 +0000435template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100436Status NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::validate(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info)
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100437{
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100438 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_winograd_input_trans(input, output, winograd_info));
439 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_winograd_input_trans(input->clone().get(), output->clone().get(), winograd_info).first);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100440
441 return Status{};
442}
443
Pablo Tellof6c572c2018-02-14 12:47:30 +0000444template class NEWinogradLayerTransformInputKernel<float, 2, 2, 3, 3>;
445template class NEWinogradLayerTransformInputKernel<float, 2, 2, 5, 5>;
Pablo Tello52140b42018-01-30 14:48:11 +0000446
Pablo Tellod6ca4782018-01-23 09:36:04 +0000447// Output transform
Pablo Tello52140b42018-01-30 14:48:11 +0000448
Pablo Tellof6c572c2018-02-14 12:47:30 +0000449template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
450unsigned int NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_output_storage_size(
Pablo Tello52140b42018-01-30 14:48:11 +0000451 int n_batches, /** Number of batches in the output tensor. */
452 int n_rows, /** Number of rows in each feature map of the input tensor. */
453 int n_cols, /** Number of columns in each feature map of the input tensor. */
454 int n_output_channels, /** Number of feature maps in the output tensor. */
455 bool same_padding /** Use "SAME" padding, otherwise use "VALID". */
Pablo Tellof6c572c2018-02-14 12:47:30 +0000456) const
Pablo Tello52140b42018-01-30 14:48:11 +0000457{
458 // Construct shapes for the input and kernel tensors.
459 const Tensor4DShape input_shape(n_batches, n_rows, n_cols, 1);
460 const KernelShape kern_shape(n_output_channels, KernelRows, KernelCols, 1);
461 const PaddingType padding = (same_padding) ? PADDING_SAME : PADDING_VALID;
462
463 // Return the size, converted into units of TOut
464 return static_cast<unsigned int>(
Pablo Tellof6c572c2018-02-14 12:47:30 +0000465 WinogradConv::get_output_storage_size(kern_shape, input_shape, padding) / sizeof(T));
Pablo Tello52140b42018-01-30 14:48:11 +0000466}
467
Pablo Tellof6c572c2018-02-14 12:47:30 +0000468template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
469NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::NEWinogradLayerTransformOutputKernel()
Pablo Tellod6ca4782018-01-23 09:36:04 +0000470 : _biases(nullptr), _output_workspace(nullptr), _matrix_stride(0), _matrix_row_stride(0), _output(nullptr), _n_batches(0), _n_rows(0), _n_cols(0), _n_channels(0)
471{
472}
473
Pablo Tellof6c572c2018-02-14 12:47:30 +0000474template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
475int NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_matrix_stride(
476 const KernelShape &kernel_shape, const Tensor4DShape &input_shape, const PaddingType padding_type) const
477{
478 return WinogradConv::get_output_matrix_stride(kernel_shape, input_shape, padding_type);
479}
480template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
481Tensor4DShape NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_output_shape(
482 const KernelShape &kernel_shape, const Tensor4DShape &in_shape, const PaddingType padding) const
483{
484 return WinogradConv::get_output_shape(kernel_shape, in_shape, padding);
485}
486
487template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
488void NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::configure(
489 const ITensor *biases,
490 const T *const output_workingspace,
491 const int matrix_stride,
492 T *const output,
493 const int n_batches,
494 const int n_rows,
495 const int n_cols,
496 const int n_channels)
Pablo Tellod6ca4782018-01-23 09:36:04 +0000497{
Pablo Tellod6ca4782018-01-23 09:36:04 +0000498 _biases = biases;
499 _output_workspace = output_workingspace;
500 _matrix_stride = matrix_stride;
Pablo Tello52140b42018-01-30 14:48:11 +0000501 _matrix_row_stride = roundup(n_channels, WinogradConv::N_BLOCK);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000502 _output = output;
503 _n_batches = n_batches;
504 _n_rows = n_rows;
505 _n_cols = n_cols;
506 _n_channels = n_channels;
507
508 // We don't have the biases buffer at this stage as it hasn't been allocated, we pass in nullptr OutputTransform is only used here to compute the window
509 OutputTransform output_transform(_output_workspace, _matrix_stride, _matrix_row_stride, nullptr, _output, _n_batches, _n_rows, _n_cols, _n_channels);
510 Window win;
511 auto win_last = output_transform.get_window();
512 win.set(Window::DimX, Window::Dimension(0, win_last, 1));
513 INEKernel::configure(win);
514}
515
Pablo Tellof6c572c2018-02-14 12:47:30 +0000516template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
517void NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::run(const Window &window, const ThreadInfo &info)
Pablo Tellod6ca4782018-01-23 09:36:04 +0000518{
519 ARM_COMPUTE_UNUSED(info);
520 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000521 ARM_COMPUTE_ERROR_ON_NULLPTR(_output_workspace);
522 ARM_COMPUTE_ERROR_ON_NULLPTR(_output);
523
Pablo Tellod6ca4782018-01-23 09:36:04 +0000524 OutputTransform output_transform(_output_workspace, _matrix_stride, _matrix_row_stride,
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000525 (_biases ? reinterpret_cast<T *>(_biases->buffer()) : nullptr), _output,
Pablo Tellod6ca4782018-01-23 09:36:04 +0000526 _n_batches, _n_rows, _n_cols, _n_channels);
527
528 // The code below cannot be moved to configure because biases hasn't been allocated at that point
529 const size_t fst = window.x().start();
530 const size_t lst = window.x().end();
531 output_transform.run(fst, lst);
532}
533
Pablo Tellof6c572c2018-02-14 12:47:30 +0000534template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols>
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100535Status NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output,
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100536 const WinogradInfo &winograd_info)
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100537{
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100538 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_winograd_output_trans(input, (bias != nullptr ? bias->clone().get() : nullptr), output, winograd_info));
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100539 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_winograd_output_trans(input->clone().get(), (bias != nullptr ? bias->clone().get() : nullptr), output->clone().get(),
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100540 winograd_info)
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100541 .first);
542
543 return Status{};
544}
545
Pablo Tellof6c572c2018-02-14 12:47:30 +0000546template class NEWinogradLayerTransformOutputKernel<float, 2, 2, 3, 3>;
547template class NEWinogradLayerTransformOutputKernel<float, 2, 2, 5, 5>;
Pablo Tello52140b42018-01-30 14:48:11 +0000548
Pablo Tello89519332017-11-17 11:52:36 +0000549} // namespace arm_compute