Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 1 | /* |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +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 | */ |
| 24 | #include "arm_compute/runtime/CL/functions/CLFFTConvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
| 27 | #include "arm_compute/core/Utils.h" |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Validate.h" |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 31 | #include "arm_compute/runtime/CPP/CPPScheduler.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 32 | |
| 33 | #include "src/common/utils/Log.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 34 | #include "src/core/CL/kernels/CLFFTDigitReverseKernel.h" |
| 35 | #include "src/core/CL/kernels/CLFFTRadixStageKernel.h" |
| 36 | #include "src/core/CL/kernels/CLFFTScaleKernel.h" |
| 37 | #include "src/core/CL/kernels/CLFillBorderKernel.h" |
| 38 | #include "src/core/CL/kernels/CLPadLayerKernel.h" |
| 39 | #include "src/core/CL/kernels/CLReductionOperationKernel.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 40 | #include "src/core/helpers/AutoConfiguration.h" |
| 41 | #include "src/core/utils/helpers/fft.h" |
| 42 | |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 43 | namespace arm_compute |
| 44 | { |
| 45 | namespace |
| 46 | { |
| 47 | int pad_decomposable(int N) |
| 48 | { |
| 49 | const auto supported_radix = CLFFTRadixStageKernel::supported_radix(); |
| 50 | |
| 51 | int pad = 0; |
| 52 | bool is_decomposed = false; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 53 | while (!is_decomposed) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 54 | { |
| 55 | const auto decomposed_vector = arm_compute::helpers::fft::decompose_stages(N++, supported_radix); |
| 56 | is_decomposed = !decomposed_vector.empty(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 57 | if (!is_decomposed) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 58 | { |
| 59 | ++pad; |
| 60 | } |
| 61 | } |
| 62 | return pad; |
| 63 | } |
| 64 | } // namespace |
| 65 | CLFFTConvolutionLayer::CLFFTConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
| 66 | : _memory_group(memory_manager), |
| 67 | _flip_weights_func(), |
| 68 | _permute_input_func(), |
| 69 | _permute_output_func(), |
| 70 | _permute_weights_func(), |
| 71 | _permute_bias_func(), |
| 72 | _pad_input_func(), |
| 73 | _pad_weights_func(), |
| 74 | _transform_input_func(memory_manager), |
Georgios Pinitas | 098516b | 2019-04-25 18:25:06 +0100 | [diff] [blame] | 75 | _transform_weights_func(), |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 76 | _itransform_output_func(memory_manager), |
| 77 | _prod_func(), |
| 78 | _reduce_func(), |
| 79 | _extract_output_func(), |
| 80 | _bias_add_func(), |
| 81 | _activation_layer_func(), |
| 82 | _permuted_input(), |
| 83 | _permuted_weights(), |
| 84 | _permuted_bias(), |
| 85 | _permuted_output(), |
| 86 | _padded_input(), |
| 87 | _padded_weights(), |
| 88 | _flip_axis(), |
| 89 | _flipped_weights(), |
| 90 | _transformed_input(), |
| 91 | _transformed_weights(), |
| 92 | _input_weights_product(), |
| 93 | _output_product(), |
| 94 | _output_reduced(), |
| 95 | _itransformed_output(), |
| 96 | _reshaped_output(), |
| 97 | _bias_output(), |
| 98 | _original_weights(nullptr), |
| 99 | _original_bias(nullptr), |
| 100 | _is_activationlayer_enabled(false), |
| 101 | _needs_permute(false), |
| 102 | _has_bias(false), |
| 103 | _is_prepared(false) |
| 104 | { |
| 105 | } |
| 106 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | void CLFFTConvolutionLayer::configure(ICLTensor *input, |
| 108 | const ICLTensor *weights, |
| 109 | const ICLTensor *biases, |
| 110 | ICLTensor *output, |
| 111 | const PadStrideInfo &conv_info, |
| 112 | const ActivationLayerInfo &act_info, |
| 113 | bool enable_fast_math) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 114 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 115 | configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, act_info, |
| 116 | enable_fast_math); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 119 | void CLFFTConvolutionLayer::configure(const CLCompileContext &compile_context, |
| 120 | ICLTensor *input, |
| 121 | const ICLTensor *weights, |
| 122 | const ICLTensor *biases, |
| 123 | ICLTensor *output, |
| 124 | const PadStrideInfo &conv_info, |
| 125 | const ActivationLayerInfo &act_info, |
| 126 | bool enable_fast_math) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 127 | { |
Giorgio Arena | ea7de7b | 2020-12-10 16:49:39 +0000 | [diff] [blame] | 128 | ARM_COMPUTE_UNUSED(enable_fast_math); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 129 | ARM_COMPUTE_ERROR_THROW_ON(CLFFTConvolutionLayer::validate(input->info(), weights->info(), |
| 130 | biases != nullptr ? biases->info() : nullptr, |
| 131 | output->info(), conv_info, act_info, enable_fast_math)); |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 132 | ARM_COMPUTE_LOG_PARAMS(input, weights, biases, output, conv_info, act_info, enable_fast_math); |
Giorgio Arena | ea7de7b | 2020-12-10 16:49:39 +0000 | [diff] [blame] | 133 | |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 134 | _original_weights = weights; |
| 135 | _original_bias = biases; |
| 136 | |
| 137 | // Flat if bias addition is required |
| 138 | _has_bias = biases != nullptr; |
| 139 | |
| 140 | // Get indices for the width and height |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 141 | const size_t idx_width = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH); |
| 142 | const size_t idx_height = |
| 143 | get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 144 | |
| 145 | // Input shape, kernel size and output tile |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 146 | const Size2D input_dims = |
| 147 | Size2D(input->info()->tensor_shape()[idx_width], input->info()->tensor_shape()[idx_height]); |
| 148 | const Size2D kernel_size = |
| 149 | Size2D(weights->info()->tensor_shape()[idx_width], weights->info()->tensor_shape()[idx_height]); |
| 150 | const Size2D pad_valid = Size2D(pad_decomposable(input_dims.x() + kernel_size.x() - 1), |
| 151 | pad_decomposable(input_dims.y() + kernel_size.y() - 1)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 152 | // Tensors to use |
| 153 | ICLTensor *input_to_use = input; |
| 154 | const ICLTensor *weights_to_use = weights; |
| 155 | ICLTensor *output_to_use = _has_bias ? &_bias_output : output; |
| 156 | |
| 157 | // Permute bias |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 158 | if (biases != nullptr) |
Georgios Pinitas | 68c6a79 | 2019-05-15 13:24:00 +0100 | [diff] [blame] | 159 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 160 | _permute_bias_func.configure(compile_context, biases, &_permuted_bias, PermutationVector(1U, 2U, 0U)); |
Georgios Pinitas | 68c6a79 | 2019-05-15 13:24:00 +0100 | [diff] [blame] | 161 | _permuted_bias.info()->set_data_layout(DataLayout::NCHW); |
| 162 | } |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 163 | |
| 164 | // Permute input if needed |
| 165 | _needs_permute = input->info()->data_layout() == DataLayout::NHWC; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 166 | if (_needs_permute) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 167 | { |
| 168 | _memory_group.manage(&_permuted_input); |
| 169 | // Configure the function to transform the input tensor from NHWC -> NCHW |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 170 | _permute_input_func.configure(compile_context, input, &_permuted_input, PermutationVector(1U, 2U, 0U)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 171 | _permuted_input.info()->set_data_layout(DataLayout::NCHW); |
| 172 | |
| 173 | // Configure the function to transform the weights tensor from HWI -> IHW |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 174 | _permute_weights_func.configure(compile_context, weights, &_permuted_weights, PermutationVector(1U, 2U, 0U)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 175 | _permuted_weights.info()->set_data_layout(DataLayout::NCHW); |
| 176 | |
| 177 | input_to_use = &_permuted_input; |
| 178 | weights_to_use = &_permuted_weights; |
| 179 | } |
| 180 | |
| 181 | // Flip weights |
| 182 | _flipped_weights.allocator()->init(weights_to_use->info()->clone()->set_is_resizable(true).reset_padding()); |
| 183 | _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32)); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 184 | _flip_weights_func.configure(compile_context, weights_to_use, &_flipped_weights, &_flip_axis); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 185 | |
| 186 | // Pad weights |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 187 | const PaddingList padding_w = {{0, input_dims.x() + pad_valid.x() - 1}, {0, input_dims.y() + pad_valid.y() - 1}}; |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 188 | _pad_weights_func.configure(compile_context, &_flipped_weights, &_padded_weights, padding_w); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 189 | |
| 190 | // Transform weights |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 191 | _transform_weights_func = std::make_unique<CLFFT2D>(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 192 | _transform_weights_func->configure(compile_context, &_padded_weights, &_transformed_weights, FFT2DInfo()); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 193 | |
| 194 | // Pad input |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 195 | const PaddingList padding_in = {{0, kernel_size.x() + pad_valid.x() - 1}, {0, kernel_size.y() + pad_valid.y() - 1}}; |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 196 | _memory_group.manage(&_padded_input); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 197 | _pad_input_func.configure(compile_context, input_to_use, &_padded_input, padding_in); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 198 | if (_needs_permute) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 199 | { |
| 200 | _permuted_input.allocator()->allocate(); |
| 201 | } |
| 202 | |
| 203 | // Transform input |
| 204 | _memory_group.manage(&_transformed_input); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 205 | _transform_input_func.configure(compile_context, &_padded_input, &_transformed_input, FFT2DInfo()); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 206 | _padded_input.allocator()->allocate(); |
| 207 | |
| 208 | // Perform product |
| 209 | _memory_group.manage(&_output_product); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 210 | _prod_func.configure(compile_context, &_transformed_input, &_transformed_weights, &_output_product); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 211 | _transformed_input.allocator()->allocate(); |
| 212 | |
| 213 | // Perform reduction |
| 214 | _memory_group.manage(&_output_reduced); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 215 | _reduce_func.configure(compile_context, &_output_product, &_output_reduced, 2, ReductionOperation::SUM); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 216 | _output_product.allocator()->allocate(); |
| 217 | |
| 218 | // Transform output |
| 219 | _memory_group.manage(&_itransformed_output); |
| 220 | FFT2DInfo itranform_info; |
| 221 | itranform_info.direction = FFTDirection::Inverse; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 222 | _itransformed_output.allocator()->init( |
| 223 | _output_reduced.info()->clone()->set_is_resizable(true).set_num_channels(1).reset_padding()); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 224 | _itransform_output_func.configure(compile_context, &_output_reduced, &_itransformed_output, itranform_info); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 225 | _output_reduced.allocator()->allocate(); |
| 226 | |
| 227 | // Reshape output |
| 228 | TensorShape reshaped_shape = _itransformed_output.info()->tensor_shape(); |
| 229 | reshaped_shape.remove_dimension(2); |
| 230 | _reshaped_output.allocator()->init(_itransformed_output.info()->clone()->set_tensor_shape(reshaped_shape)); |
| 231 | |
| 232 | // Extract correct region |
| 233 | const int start_left = kernel_size.x() - conv_info.pad_left() - 1; |
| 234 | const int start_top = kernel_size.y() - conv_info.pad_top() - 1; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 235 | const int end_right = |
| 236 | _reshaped_output.info()->tensor_shape().x() - (kernel_size.x() - conv_info.pad_right() - 1) - pad_valid.x(); |
| 237 | const int end_botton = |
| 238 | _reshaped_output.info()->tensor_shape().y() - (kernel_size.y() - conv_info.pad_bottom() - 1) - pad_valid.y(); |
| 239 | if (_has_bias) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 240 | { |
| 241 | _memory_group.manage(&_bias_output); |
| 242 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 243 | else if (_needs_permute) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 244 | { |
| 245 | output_to_use = &_permuted_output; |
| 246 | _memory_group.manage(&_permuted_output); |
| 247 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 248 | _extract_output_func.configure(compile_context, &_reshaped_output, output_to_use, |
| 249 | Coordinates(start_left, start_top), Coordinates(end_right, end_botton)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 250 | _itransformed_output.allocator()->allocate(); |
| 251 | |
| 252 | // Add bias |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 253 | if (biases != nullptr) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 254 | { |
| 255 | output_to_use = output; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 256 | if (_needs_permute) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 257 | { |
| 258 | output_to_use = &_permuted_output; |
| 259 | _memory_group.manage(&_permuted_output); |
| 260 | } |
| 261 | auto_init_if_empty(*output_to_use->info(), *_bias_output.info()); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 262 | _bias_add_func.configure(compile_context, &_bias_output, &_permuted_bias, output_to_use, ConvertPolicy::WRAP); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 263 | _bias_output.allocator()->allocate(); |
| 264 | } |
| 265 | |
| 266 | // Permute output |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 267 | if (_needs_permute) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 268 | { |
| 269 | // Configure the function to transform the convoluted output to ACL's native ordering format NCHW |
| 270 | _permuted_output.info()->set_data_layout(DataLayout::NCHW); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 271 | _permute_output_func.configure(compile_context, &_permuted_output, output, PermutationVector(2U, 0U, 1U)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 272 | |
| 273 | // Allocate tensors |
| 274 | _permuted_output.allocator()->allocate(); |
| 275 | } |
| 276 | |
| 277 | // Configure Activation Layer |
| 278 | _is_activationlayer_enabled = act_info.enabled(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 279 | if (_is_activationlayer_enabled) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 280 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 281 | _activation_layer_func.configure(compile_context, output, nullptr, act_info); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // Setup flip axis data |
| 285 | _flip_axis.allocator()->allocate(); |
| 286 | _flip_axis.map(true); |
| 287 | auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer()); |
| 288 | axis_data[0] = 0; |
| 289 | axis_data[1] = 1; |
| 290 | _flip_axis.unmap(); |
| 291 | } |
| 292 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 293 | Status CLFFTConvolutionLayer::validate(const ITensorInfo *input, |
| 294 | const ITensorInfo *weights, |
| 295 | const ITensorInfo *biases, |
| 296 | const ITensorInfo *output, |
| 297 | const PadStrideInfo &conv_info, |
| 298 | const ActivationLayerInfo &act_info, |
| 299 | bool enable_fast_math) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 300 | { |
Giorgio Arena | ea7de7b | 2020-12-10 16:49:39 +0000 | [diff] [blame] | 301 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 302 | ARM_COMPUTE_RETURN_ERROR_ON((input->data_type() == DataType::F16) && !enable_fast_math); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 303 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 304 | |
| 305 | // Get indices for the width and height |
| 306 | const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 307 | const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
| 308 | |
| 309 | // Input shape, kernel size and output tile |
| 310 | const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]); |
| 311 | |
| 312 | // Strides |
| 313 | const auto strides = conv_info.stride(); |
| 314 | ARM_COMPUTE_RETURN_ERROR_ON(strides.first != strides.second && strides.first != 1); |
| 315 | ARM_COMPUTE_RETURN_ERROR_ON(kernel_size.x() != kernel_size.y()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 316 | ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_left() != (kernel_size.x() / 2) || |
| 317 | conv_info.pad_right() != (kernel_size.x() / 2)); |
| 318 | ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_top() != (kernel_size.y() / 2) || |
| 319 | conv_info.pad_bottom() != (kernel_size.y() / 2)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 320 | |
| 321 | // Validate biases |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 322 | if (biases != nullptr) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 323 | { |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 324 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
Giorgio Arena | ea7de7b | 2020-12-10 16:49:39 +0000 | [diff] [blame] | 325 | ARM_COMPUTE_RETURN_ERROR_ON(weights->tensor_shape()[3] != biases->tensor_shape().x()); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // Checks performed when output is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 329 | if ((output != nullptr) && (output->total_size() != 0)) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 330 | { |
| 331 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 332 | ARM_COMPUTE_RETURN_ERROR_ON((input->tensor_shape()[idx_height] != output->tensor_shape()[idx_height]) || |
| 333 | (input->tensor_shape()[idx_width] != output->tensor_shape()[idx_width])); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 334 | |
| 335 | // Validate Activation Layer |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 336 | if (act_info.enabled()) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 337 | { |
| 338 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info)); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return Status{}; |
| 343 | } |
| 344 | |
| 345 | void CLFFTConvolutionLayer::run() |
| 346 | { |
| 347 | prepare(); |
| 348 | |
Georgios Pinitas | 098516b | 2019-04-25 18:25:06 +0100 | [diff] [blame] | 349 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 350 | |
| 351 | // Transform input |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 352 | if (_needs_permute) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 353 | { |
| 354 | _permute_input_func.run(); |
| 355 | } |
| 356 | _pad_input_func.run(); |
| 357 | _transform_input_func.run(); |
| 358 | |
| 359 | // Perform operations to frequency domain |
| 360 | _prod_func.run(); |
| 361 | _reduce_func.run(); |
| 362 | |
| 363 | // Transform output |
| 364 | _itransform_output_func.run(); |
| 365 | _reshaped_output.allocator()->import_memory(_itransformed_output.cl_buffer()); |
| 366 | _extract_output_func.run(); |
| 367 | // Add bias |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 368 | if (_has_bias) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 369 | { |
| 370 | _bias_add_func.run(); |
| 371 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 372 | if (_needs_permute) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 373 | { |
| 374 | _permute_output_func.run(); |
| 375 | } |
| 376 | |
| 377 | // Run activation layer |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 378 | if (_is_activationlayer_enabled) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 379 | { |
| 380 | _activation_layer_func.run(); |
| 381 | } |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void CLFFTConvolutionLayer::prepare() |
| 385 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 386 | if (!_is_prepared) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 387 | { |
| 388 | // Permute bias to NCHW |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 389 | if (_original_bias != nullptr) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 390 | { |
| 391 | _permuted_bias.allocator()->allocate(); |
| 392 | _permute_bias_func.run(); |
| 393 | _original_bias->mark_as_unused(); |
| 394 | } |
| 395 | |
| 396 | const ICLTensor *cur_weights = _original_weights; |
| 397 | // Permute weights |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 398 | if (_needs_permute) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 399 | { |
| 400 | ARM_COMPUTE_ERROR_ON(!cur_weights->is_used()); |
| 401 | |
| 402 | _permuted_weights.allocator()->allocate(); |
| 403 | _permute_weights_func.run(); |
| 404 | cur_weights->mark_as_unused(); |
| 405 | cur_weights = &_permuted_weights; |
| 406 | } |
| 407 | |
| 408 | // Flip weights |
| 409 | _flipped_weights.allocator()->allocate(); |
| 410 | _flip_weights_func.run(); |
| 411 | cur_weights->mark_as_unused(); |
| 412 | |
| 413 | // Pad weights |
| 414 | _padded_weights.allocator()->allocate(); |
| 415 | _pad_weights_func.run(); |
| 416 | _flipped_weights.mark_as_unused(); |
| 417 | CLScheduler::get().queue().finish(); |
| 418 | _flipped_weights.allocator()->free(); |
| 419 | |
Georgios Pinitas | 098516b | 2019-04-25 18:25:06 +0100 | [diff] [blame] | 420 | // Transform weights to frequency domain |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 421 | _transformed_weights.allocator()->allocate(); |
Georgios Pinitas | 098516b | 2019-04-25 18:25:06 +0100 | [diff] [blame] | 422 | _transform_weights_func->run(); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 423 | _padded_weights.mark_as_unused(); |
| 424 | CLScheduler::get().queue().finish(); |
Georgios Pinitas | 098516b | 2019-04-25 18:25:06 +0100 | [diff] [blame] | 425 | // Delete object and release internal memory |
| 426 | _transform_weights_func.reset(); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 427 | _padded_weights.allocator()->free(); |
| 428 | |
| 429 | _is_prepared = true; |
| 430 | } |
| 431 | } |
| 432 | } // namespace arm_compute |