Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 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 | */ |
Georgios Pinitas | 9fb1159 | 2018-04-26 20:34:58 +0100 | [diff] [blame] | 24 | #include "arm_compute/core/NEON/kernels/NEWinogradConvolutionLayerKernel.h" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 25 | |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/IAccessWindow.h" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/ITensor.h" |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/NEON/kernels/convolution/common/utils.hpp" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/TensorInfo.h" |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 36 | #include "support/MemorySupport.h" |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 37 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 40 | //Batched Gemms |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 41 | |
| 42 | namespace |
| 43 | { |
Pablo Tello | bda6e4b | 2018-08-22 11:40:33 +0100 | [diff] [blame] | 44 | inline bool is_kernel_size_supported(Size2D size) |
| 45 | { |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 46 | const std::array<Size2D, 8> supported_input_sizes = { { Size2D(1, 3), Size2D(3, 1), Size2D(5, 5), Size2D(3, 3), Size2D(1, 5), Size2D(5, 1), Size2D(7, 1), Size2D(1, 7) } }; |
Pablo Tello | bda6e4b | 2018-08-22 11:40:33 +0100 | [diff] [blame] | 47 | return std::end(supported_input_sizes) != std::find(std::begin(supported_input_sizes), std::end(supported_input_sizes), size); |
| 48 | } |
| 49 | |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 50 | Status validate_arguments_winograd_weight_trans(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info) |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 51 | { |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 55 | |
Pablo Tello | bda6e4b | 2018-08-22 11:40:33 +0100 | [diff] [blame] | 56 | const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 57 | const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
| 58 | const auto input_width = input->dimension(idx_width); |
| 59 | const auto input_height = input->dimension(idx_height); |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_kernel_size_supported(Size2D(input_width, input_height)), "Only 1x3, 3x1, 1x5, 5x1, 7x1, 1x7, 3x3 and 5x5 kernels are supported"); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 62 | const Size2D &output_tile = winograd_info.output_tile_size; |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 63 | const std::array<Size2D, 8> supported_tile_sizes = { { Size2D(2U, 2U), Size2D(4U, 4U), Size2D(1U, 6U), Size2D(6U, 1U), Size2D(4, 1), Size2D(1, 4), Size2D(2, 1), Size2D(1, 2) } }; |
Pablo Tello | bda6e4b | 2018-08-22 11:40:33 +0100 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON(std::end(supported_tile_sizes) == std::find(std::begin(supported_tile_sizes), std::end(supported_tile_sizes), output_tile)); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 65 | |
| 66 | // Checks performed when output is configured |
| 67 | if(output->total_size() != 0) |
| 68 | { |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 69 | 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 Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 70 | |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output); |
| 72 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 73 | } |
| 74 | |
| 75 | return Status{}; |
| 76 | } |
| 77 | |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 78 | std::pair<Status, Window> validate_and_configure_window_winograd_weight_trans(ITensorInfo *input, ITensorInfo *output, const WinogradInfo &winograd_info) |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 79 | { |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 80 | // Output tensor auto inizialitation if not yet initialized |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 81 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_winograd_filter_transform_shape(*input, winograd_info))); |
morgolock | c6d9a8b | 2019-12-23 10:45:59 +0000 | [diff] [blame] | 82 | const Window win = calculate_max_window(*input, Steps(), true /* skip border*/); |
| 83 | return std::make_pair(Status{}, win); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 86 | Status validate_arguments_winograd_input_trans(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info) |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 87 | { |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 88 | const Size2D &kernel_dims = winograd_info.kernel_size; |
| 89 | const PadStrideInfo &conv_info = winograd_info.convolution_info; |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 90 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 91 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 92 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 93 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.stride().first != 1 || conv_info.stride().second != 1, "Winograd input transform only supports unit strides"); |
Pablo Tello | bda6e4b | 2018-08-22 11:40:33 +0100 | [diff] [blame] | 94 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_kernel_size_supported(Size2D(kernel_dims.width, kernel_dims.height)), |
| 95 | "Only 1x3, 3x1, 3x3 and 5x5 kernels are supported"); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 96 | |
| 97 | // Validate configured output |
| 98 | if(output->total_size() != 0) |
| 99 | { |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 100 | const TensorShape output_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 101 | |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 104 | } |
| 105 | |
| 106 | return Status{}; |
| 107 | } |
| 108 | |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 109 | std::pair<Status, Window> validate_and_configure_window_winograd_input_trans(ITensorInfo *input, ITensorInfo *output, const WinogradInfo &winograd_info) |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 110 | { |
morgolock | c6d9a8b | 2019-12-23 10:45:59 +0000 | [diff] [blame] | 111 | const TensorShape output_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 112 | // Output auto inizialitation if not yet initialized |
| 113 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape)); |
morgolock | c6d9a8b | 2019-12-23 10:45:59 +0000 | [diff] [blame] | 114 | return std::make_pair(Status{}, calculate_max_window(*input, Steps(), true)); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 117 | Status validate_arguments_winograd_output_trans(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const WinogradInfo &winograd_info) |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 118 | { |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 119 | const PadStrideInfo &conv_info = winograd_info.convolution_info; |
| 120 | const Size2D kernel_dims = winograd_info.kernel_size; |
| 121 | |
| 122 | // Number of tiles along the X and Y direction |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame] | 123 | 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()) / static_cast<float> |
| 124 | (winograd_info.output_tile_size.width)); |
| 125 | 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()) / static_cast<float> |
| 126 | (winograd_info.output_tile_size.height)); |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 127 | const Size2D num_tiles = Size2D(num_tiles_x, num_tiles_y); |
| 128 | |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 129 | 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(input->dimension(1) != num_tiles.area()); |
Pablo Tello | bda6e4b | 2018-08-22 11:40:33 +0100 | [diff] [blame] | 133 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_kernel_size_supported(Size2D(kernel_dims.width, kernel_dims.height)), |
| 134 | "Only 1x3, 3x1, 3x3 and 5x5 kernels are supported"); |
| 135 | |
| 136 | const std::array<unsigned int, 3> supported_gemm_sizes = { { 8U, 16U, 36U } }; |
| 137 | ARM_COMPUTE_RETURN_ERROR_ON(std::end(supported_gemm_sizes) == std::find(std::begin(supported_gemm_sizes), std::end(supported_gemm_sizes), input->dimension(2))); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 138 | ARM_COMPUTE_UNUSED(kernel_dims); |
| 139 | if(bias != nullptr) |
| 140 | { |
| 141 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias); |
| 142 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != bias->dimension(0)); |
| 143 | ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() != size_t(1)); |
| 144 | } |
| 145 | |
| 146 | // Checks performed when output is configured |
| 147 | if(output->total_size() != 0) |
| 148 | { |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 149 | 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 Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 150 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output); |
| 151 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 152 | } |
| 153 | return Status{}; |
| 154 | } |
| 155 | |
morgolock | c6d9a8b | 2019-12-23 10:45:59 +0000 | [diff] [blame] | 156 | std::pair<Status, Window> validate_and_configure_window_winograd_output_trans(ITensorInfo *input, ITensorInfo *output, const WinogradInfo &winograd_info) |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 157 | { |
| 158 | // Output tensor auto initialization if not yet initialized |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 159 | 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 Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 160 | |
morgolock | c6d9a8b | 2019-12-23 10:45:59 +0000 | [diff] [blame] | 161 | return std::make_pair(Status{}, calculate_max_window(*input, Steps(), true)); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 162 | } |
| 163 | } // namespace |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 164 | |
Pablo Tello | bda6e4b | 2018-08-22 11:40:33 +0100 | [diff] [blame] | 165 | template <typename T> |
| 166 | Status INEWinogradLayerTransformWeightsKernel<T>::validate(const ITensorInfo *input, const ITensorInfo *weights) |
| 167 | { |
| 168 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 169 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 170 | const DataLayout data_layout = input->data_layout(); |
| 171 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 172 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 173 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_kernel_size_supported(Size2D(weights->dimension(width_idx), weights->dimension(height_idx))), |
| 174 | "Only 1x3, 3x1, 3x3 and 5x5 kernels are supported"); |
| 175 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 176 | return Status{}; |
| 177 | } |
| 178 | |
| 179 | template class INEWinogradLayerTransformWeightsKernel<float>; |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 180 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 181 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 182 | unsigned int NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_weight_storage_size(int num_output_channels, int num_input_channels) const |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 183 | { |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 184 | const KernelShape shape(num_output_channels, KernelRows, KernelCols, num_input_channels); |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 185 | return static_cast<unsigned int>( |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 186 | // WinogradConv returns the size in bytes, we divide by `sizeof(T)` to express that in units of T |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 187 | WinogradConv::get_kernel_storage_size(num_input_channels, num_output_channels) / sizeof(T)); |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 190 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
| 191 | NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::NEWinogradLayerTransformWeightsKernel() |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 192 | : _transform(nullptr), _weights_hwio(nullptr), _output(nullptr), _matrix_stride(0), _num_output_channels(0), _num_input_channels(0) |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 193 | { |
| 194 | } |
| 195 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 196 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 197 | int NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_matrix_stride(int num_output_channels, int num_input_channels) const |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 198 | { |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 199 | return WinogradConv::get_kernel_matrix_stride(num_input_channels, num_output_channels); |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Vidhya Sudhan Loganathan | d646ae1 | 2018-11-19 15:18:20 +0000 | [diff] [blame] | 202 | #ifndef DOXYGEN_SKIP_THIS |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 203 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
| 204 | void NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::configure( |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 205 | const ITensor *weights_hwio, |
Anthony Barbier | e155337 | 2018-07-16 18:53:52 +0100 | [diff] [blame] | 206 | ITensor *output, |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 207 | const int matrix_stride, /** Stride across matrices in the output. */ |
| 208 | const int num_output_channels, /** Number of filters. */ |
| 209 | const int num_input_channels) /** Number of channels in each filter. */ |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 210 | { |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 211 | _weights_hwio = weights_hwio; |
| 212 | _output = output; |
| 213 | _matrix_stride = matrix_stride; |
| 214 | _num_output_channels = num_output_channels; |
| 215 | _num_input_channels = num_input_channels; |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 216 | _transform = arm_compute::support::cpp14::make_unique<WeightsTransform>(num_output_channels, num_input_channels); |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 217 | |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 218 | Window win; |
| 219 | auto win_last = _transform->get_window(); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 220 | win.set(Window::DimX, Window::Dimension(0, win_last, 1)); |
| 221 | INEKernel::configure(win); |
| 222 | } |
Vidhya Sudhan Loganathan | d646ae1 | 2018-11-19 15:18:20 +0000 | [diff] [blame] | 223 | #endif /* DOXYGEN_SKIP_THIS */ |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 224 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 225 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
| 226 | void NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::run(const Window &window, const ThreadInfo &info) |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 227 | { |
| 228 | ARM_COMPUTE_UNUSED(info); |
| 229 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 230 | const size_t fst = window.x().start(); |
| 231 | const size_t lst = window.x().end(); |
| 232 | _transform->set_weight_tensor(_weights_hwio->buffer()); |
| 233 | const int matrix_row_stride = roundup(_num_output_channels, WinogradConv::N_BLOCK); |
| 234 | _transform->set_output_matrices(_output->buffer(), _matrix_stride, matrix_row_stride); |
| 235 | _transform->set_working_space(_output->buffer()); |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 236 | |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 237 | _transform->run(fst, lst); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 240 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
| 241 | bool NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::is_parallelisable() const |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 242 | { |
| 243 | return false; |
| 244 | } |
| 245 | |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 246 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 247 | Status NEWinogradLayerTransformWeightsKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::validate(const ITensorInfo *input, const ITensorInfo *output, |
| 248 | const WinogradInfo &winograd_info) |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 249 | { |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 250 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_winograd_weight_trans(input, output, winograd_info)); |
| 251 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_winograd_weight_trans(input->clone().get(), output->clone().get(), winograd_info).first); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 252 | return Status{}; |
| 253 | } |
| 254 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 255 | template class NEWinogradLayerTransformWeightsKernel<float, 2, 2, 3, 3>; |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame] | 256 | template class NEWinogradLayerTransformWeightsKernel<float, 4, 4, 3, 3>; |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 257 | template class NEWinogradLayerTransformWeightsKernel<float, 2, 2, 5, 5>; |
Pablo Tello | bda6e4b | 2018-08-22 11:40:33 +0100 | [diff] [blame] | 258 | template class NEWinogradLayerTransformWeightsKernel<float, 1, 6, 1, 3>; |
| 259 | template class NEWinogradLayerTransformWeightsKernel<float, 6, 1, 3, 1>; |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 260 | |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 261 | template class NEWinogradLayerTransformWeightsKernel<float, 1, 4, 1, 5>; |
| 262 | template class NEWinogradLayerTransformWeightsKernel<float, 4, 1, 5, 1>; |
| 263 | template class NEWinogradLayerTransformWeightsKernel<float, 1, 2, 1, 7>; |
| 264 | template class NEWinogradLayerTransformWeightsKernel<float, 2, 1, 7, 1>; |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 265 | // Input transform |
| 266 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 267 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
| 268 | unsigned int NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_input_storage_size( |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 269 | int num_batches, /* Number of batches in the input tensor. */ |
| 270 | int num_channels, /* Number of feature maps in the input tensor. */ |
| 271 | int num_rows, /* Number of rows in each feature map. */ |
| 272 | int num_cols, /* Number of columns in each feature map. */ |
| 273 | bool same_padding /* Use "SAME" padding, otherwise use "VALID". */ |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 274 | ) const |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 275 | { |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 276 | // Construct shapes for the input and kernel tensors. |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 277 | const Tensor4DShape input_shape(num_batches, num_rows, num_cols, num_channels); |
| 278 | const KernelShape kern_shape(1, KernelRows, KernelCols, num_channels); |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 279 | // Return the size, converted into units of TIn |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 280 | return static_cast<unsigned int>(WinogradConv::get_input_storage_size(num_batches, num_rows, num_cols, num_channels, same_padding) / sizeof(T)); |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 283 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 284 | unsigned int NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_working_space_size(unsigned int num_threads) const |
| 285 | { |
| 286 | return _transform->get_working_space_size(num_threads) / sizeof(T); |
| 287 | } |
| 288 | |
| 289 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 290 | int NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_matrix_stride( |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 291 | int num_batches, /* Number of batches in the input tensor. */ |
| 292 | int num_channels, /* Number of feature maps in the input tensor. */ |
| 293 | int num_rows, /* Number of rows in each feature map. */ |
| 294 | int num_cols, /* Number of columns in each feature map. */ |
| 295 | bool same_padding /* Use "SAME" padding, otherwise use "VALID". */) const |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 296 | { |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 297 | return WinogradConv::get_input_matrix_stride(num_batches, num_rows, num_cols, num_channels, same_padding); |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
| 301 | NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::NEWinogradLayerTransformInputKernel() |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 302 | : _transform(nullptr), _input_nhwc(nullptr), _num_batches(0), _num_rows(0), _num_cols(0), _num_channels(0), _padding(), _output(nullptr), _matrix_stride(0), _padding_top(), _padding_left(), |
| 303 | _padding_right(), _padding_bottom(), _workspace(nullptr) |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 304 | { |
| 305 | } |
| 306 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 307 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
| 308 | void NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::configure( |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 309 | const ITensor *input_nhwc, |
| 310 | const int num_batches, /* Number of batches in input tensor. */ |
| 311 | const int num_rows, /* Number of rows in input tensor. */ |
| 312 | const int num_cols, /* Number of columns in input tensor. */ |
| 313 | const int num_channels, /* Number of channels in input tensor. */ |
| 314 | const PaddingType padding, /* Padding type. */ |
Anthony Barbier | e155337 | 2018-07-16 18:53:52 +0100 | [diff] [blame] | 315 | ITensor *output, /* Base of output matrices. */ |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 316 | const int matrix_stride, /* Stride between output matrices. */ |
| 317 | ITensor *workspace) |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 318 | { |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 319 | _input_nhwc = input_nhwc; |
| 320 | _num_batches = num_batches; |
| 321 | _num_rows = num_rows; |
| 322 | _num_cols = num_cols; |
| 323 | _num_channels = num_channels; |
| 324 | _padding = padding; |
| 325 | _output = output; |
| 326 | _matrix_stride = matrix_stride; |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 327 | _workspace = workspace; |
| 328 | |
| 329 | _padding_top = (padding == PADDING_SAME) ? (KernelRows - 1) / 2 : 0; |
| 330 | _padding_left = (padding == PADDING_SAME) ? (KernelCols - 1) / 2 : 0; |
| 331 | _padding_bottom = (padding == PADDING_SAME) ? iceildiv(KernelRows - 1, 2) : 0; |
| 332 | _padding_right = (padding == PADDING_SAME) ? iceildiv(KernelCols - 1, 2) : 0; |
| 333 | |
| 334 | _transform = arm_compute::support::cpp14::make_unique<InputTransform>( |
| 335 | KernelRows, |
| 336 | KernelCols, |
| 337 | num_batches, |
| 338 | num_rows, |
| 339 | num_cols, |
| 340 | num_channels, |
| 341 | _padding_top, /**< Padding to apply to the top of the image. */ |
| 342 | _padding_left, /**< Padding to apply to the left of the image. */ |
| 343 | _padding_bottom, /**< Padding to apply to the bottom of the image. */ |
| 344 | _padding_right /**< Padding to apply to the right of the image. */ |
| 345 | ); |
| 346 | |
| 347 | Window win; |
| 348 | auto win_last = _transform->get_window(); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 349 | win.set(Window::DimX, Window::Dimension(0, win_last, 1)); |
| 350 | INEKernel::configure(win); |
| 351 | } |
| 352 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 353 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
| 354 | void NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::run(const Window &window, const ThreadInfo &info) |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 355 | { |
| 356 | ARM_COMPUTE_UNUSED(info); |
| 357 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 358 | ARM_COMPUTE_ERROR_ON_NULLPTR(_workspace); |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 359 | |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 360 | const int element_size_in_bytes = _input_nhwc->info()->element_size(); |
| 361 | const int input_col_stride = _input_nhwc->info()->strides_in_bytes().y() / element_size_in_bytes; |
| 362 | const int input_row_stride = _input_nhwc->info()->strides_in_bytes().z() / element_size_in_bytes; |
| 363 | const int input_batch_stride = _input_nhwc->info()->strides_in_bytes()[3] / element_size_in_bytes; |
| 364 | const auto input_nhwc_ptr = reinterpret_cast<const T *>(_input_nhwc->buffer() + _input_nhwc->info()->offset_first_element_in_bytes()); |
| 365 | auto output_ptr = reinterpret_cast<T *>(_output->buffer() + _output->info()->offset_first_element_in_bytes()); |
| 366 | ARM_COMPUTE_ERROR_ON_NULLPTR(output_ptr); |
| 367 | |
| 368 | _transform->set_input_tensor(input_nhwc_ptr, input_batch_stride, input_row_stride, input_col_stride); |
| 369 | _transform->set_output_matrices(output_ptr, _matrix_stride, _num_channels); |
| 370 | |
| 371 | _transform->set_working_space(_workspace->buffer()); |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 372 | |
| 373 | // The code below cannot be moved to configure because biases hasn't been allocated at that point |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 374 | const size_t fst = window.x().start(); |
| 375 | const size_t lst = window.x().end(); |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 376 | _transform->run(fst, lst, info.thread_id); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 377 | } |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 378 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 379 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 380 | Status NEWinogradLayerTransformInputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::validate(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info) |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 381 | { |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 382 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_winograd_input_trans(input, output, winograd_info)); |
| 383 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_winograd_input_trans(input->clone().get(), output->clone().get(), winograd_info).first); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 384 | |
| 385 | return Status{}; |
| 386 | } |
| 387 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 388 | template class NEWinogradLayerTransformInputKernel<float, 2, 2, 3, 3>; |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame] | 389 | template class NEWinogradLayerTransformInputKernel<float, 4, 4, 3, 3>; |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 390 | template class NEWinogradLayerTransformInputKernel<float, 2, 2, 5, 5>; |
Pablo Tello | bda6e4b | 2018-08-22 11:40:33 +0100 | [diff] [blame] | 391 | template class NEWinogradLayerTransformInputKernel<float, 1, 6, 1, 3>; |
| 392 | template class NEWinogradLayerTransformInputKernel<float, 6, 1, 3, 1>; |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 393 | |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 394 | template class NEWinogradLayerTransformInputKernel<float, 1, 4, 1, 5>; |
| 395 | template class NEWinogradLayerTransformInputKernel<float, 4, 1, 5, 1>; |
| 396 | template class NEWinogradLayerTransformInputKernel<float, 1, 2, 1, 7>; |
| 397 | template class NEWinogradLayerTransformInputKernel<float, 2, 1, 7, 1>; |
| 398 | |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 399 | // Output transform |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 400 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 401 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
| 402 | unsigned int NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_output_storage_size( |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 403 | int num_batches, /* Number of batches in the output tensor. */ |
| 404 | int num_rows, /* Number of rows in each feature map of the input tensor. */ |
| 405 | int num_cols, /* Number of columns in each feature map of the input tensor. */ |
| 406 | int num_output_channels /* Number of feature maps in the output tensor. */ |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 407 | ) const |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 408 | { |
| 409 | // Construct shapes for the input and kernel tensors. |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 410 | const Tensor4DShape input_shape(num_batches, num_rows, num_cols, 1); |
| 411 | const KernelShape kern_shape(num_output_channels, KernelRows, KernelCols, 1); |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 412 | // Return the size, converted into units of TOut |
| 413 | return static_cast<unsigned int>( |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 414 | WinogradConv::get_output_storage_size(num_batches, num_rows, num_cols, num_output_channels) / sizeof(T)); |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 417 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
| 418 | NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::NEWinogradLayerTransformOutputKernel() |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 419 | : _transform(nullptr), _biases(nullptr), _transformed_output(nullptr), _workspace(nullptr), _matrix_stride(0), _matrix_row_stride(0), _output_nhwc(nullptr), _num_batches(0), _num_rows(0), |
| 420 | _num_cols(0), _num_channels(0) |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 421 | { |
| 422 | } |
| 423 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 424 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 425 | unsigned int NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_working_space_size(unsigned int num_threads) const |
| 426 | { |
| 427 | return _transform->get_working_space_size(num_threads) / sizeof(T); |
| 428 | } |
| 429 | |
| 430 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 431 | int NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_matrix_stride( |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 432 | int num_batches, /* Number of batches in the output tensor. */ |
| 433 | int num_rows, /* Number of rows in each feature map of the input tensor. */ |
| 434 | int num_cols, /* Number of columns in each feature map of the input tensor. */ |
| 435 | int num_output_channels /* Number of feature maps in the output tensor. */ |
| 436 | ) const |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 437 | { |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 438 | return WinogradConv::get_output_matrix_stride(num_batches, num_rows, num_cols, num_output_channels); |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 439 | } |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 440 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 441 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 442 | std::pair<unsigned int, unsigned int> NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::get_output_shape( |
| 443 | int num_rows, /* Number of rows in each feature map of the input tensor. */ |
| 444 | int num_cols, /* Number of columns in each feature map of the input tensor. */ |
| 445 | bool padding_same) const |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 446 | { |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 447 | return WinogradConv::get_output_shape(std::make_pair<unsigned int, unsigned int>(num_rows, num_cols), padding_same); |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
| 451 | void NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::configure( |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 452 | const ITensor *biases, |
| 453 | const ITensor *transformed_output, |
| 454 | const int matrix_stride, |
| 455 | ITensor *output_nhwc, |
| 456 | const int num_batches, |
| 457 | const int num_rows, |
| 458 | const int num_cols, |
| 459 | const int num_channels, |
| 460 | ITensor *workspace, |
| 461 | const arm_gemm::Activation &activation) |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 462 | { |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 463 | _biases = biases; |
| 464 | _workspace = workspace; |
| 465 | _transformed_output = transformed_output; |
| 466 | _matrix_stride = matrix_stride; |
| 467 | _matrix_row_stride = roundup(num_channels, WinogradConv::N_BLOCK); |
| 468 | _output_nhwc = output_nhwc; |
| 469 | _num_batches = num_batches; |
| 470 | _num_rows = num_rows; |
| 471 | _num_cols = num_cols; |
| 472 | _num_channels = num_channels; |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 473 | // 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 |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 474 | _transform = arm_compute::support::cpp14::make_unique<OutputTransform>(num_batches, num_rows, num_cols, num_channels, activation); |
Pablo Tello | 7282d56 | 2018-06-14 15:35:49 +0100 | [diff] [blame] | 475 | Window win; |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 476 | auto win_last = _transform->get_window(); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 477 | win.set(Window::DimX, Window::Dimension(0, win_last, 1)); |
Pablo Tello | 7282d56 | 2018-06-14 15:35:49 +0100 | [diff] [blame] | 478 | _output_nhwc->info()->set_valid_region(ValidRegion(Coordinates(), _output_nhwc->info()->tensor_shape())); |
| 479 | |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 480 | INEKernel::configure(win); |
| 481 | } |
| 482 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 483 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
| 484 | void NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::run(const Window &window, const ThreadInfo &info) |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 485 | { |
| 486 | ARM_COMPUTE_UNUSED(info); |
| 487 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 488 | ARM_COMPUTE_ERROR_ON_NULLPTR(_workspace); |
| 489 | ARM_COMPUTE_ERROR_ON_NULLPTR(_transformed_output); |
Pablo Tello | 7df2786 | 2018-05-30 11:44:26 +0100 | [diff] [blame] | 490 | ARM_COMPUTE_ERROR_ON_NULLPTR(_output_nhwc); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 491 | |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 492 | const int out_batch_stride = _output_nhwc->info()->strides_in_bytes()[3] / sizeof(T); |
Pablo Tello | bda6e4b | 2018-08-22 11:40:33 +0100 | [diff] [blame] | 493 | const int out_row_stride = _output_nhwc->info()->strides_in_bytes()[2] / sizeof(T); |
| 494 | const int out_col_stride = _output_nhwc->info()->strides_in_bytes()[1] / sizeof(T); |
| 495 | |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 496 | _transform->set_input_matrices(_transformed_output->buffer(), _matrix_stride, _matrix_row_stride); |
| 497 | _transform->set_bias((_biases ? reinterpret_cast<T *>(_biases->buffer() + _biases->info()->offset_first_element_in_bytes()) : nullptr)); |
| 498 | _transform->set_output_tensor(_output_nhwc->buffer() + _output_nhwc->info()->offset_first_element_in_bytes(), out_batch_stride, out_row_stride, out_col_stride); |
| 499 | _transform->set_working_space(_workspace->buffer()); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 500 | // The code below cannot be moved to configure because biases hasn't been allocated at that point |
| 501 | const size_t fst = window.x().start(); |
| 502 | const size_t lst = window.x().end(); |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 503 | _transform->run(fst, lst, info.thread_id); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 506 | template <typename T, int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols> |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 507 | Status NEWinogradLayerTransformOutputKernel<T, OutputTileRows, OutputTileCols, KernelRows, KernelCols>::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 508 | const WinogradInfo &winograd_info) |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 509 | { |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 510 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_winograd_output_trans(input, (bias != nullptr ? bias->clone().get() : nullptr), output, winograd_info)); |
morgolock | c6d9a8b | 2019-12-23 10:45:59 +0000 | [diff] [blame] | 511 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_winograd_output_trans(input->clone().get(), output->clone().get(), winograd_info).first); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 512 | |
| 513 | return Status{}; |
| 514 | } |
| 515 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 516 | template class NEWinogradLayerTransformOutputKernel<float, 2, 2, 3, 3>; |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame] | 517 | template class NEWinogradLayerTransformOutputKernel<float, 4, 4, 3, 3>; |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 518 | template class NEWinogradLayerTransformOutputKernel<float, 2, 2, 5, 5>; |
Pablo Tello | bda6e4b | 2018-08-22 11:40:33 +0100 | [diff] [blame] | 519 | template class NEWinogradLayerTransformOutputKernel<float, 1, 6, 1, 3>; |
| 520 | template class NEWinogradLayerTransformOutputKernel<float, 6, 1, 3, 1>; |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 521 | |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 522 | template class NEWinogradLayerTransformOutputKernel<float, 1, 4, 1, 5>; |
| 523 | template class NEWinogradLayerTransformOutputKernel<float, 4, 1, 5, 1>; |
| 524 | template class NEWinogradLayerTransformOutputKernel<float, 1, 2, 1, 7>; |
| 525 | template class NEWinogradLayerTransformOutputKernel<float, 2, 1, 7, 1>; |
| 526 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 527 | } // namespace arm_compute |