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