blob: 1fb6d33a61425f0fddcf0e735a5173c1caa78714 [file] [log] [blame]
Michalis Spyrou96f977e2021-07-01 12:20:56 +01001/*
SiCong Lic5ab4df2023-10-17 17:38:57 +01002 * Copyright (c) 2021-2023 Arm Limited.
Michalis Spyrou96f977e2021-07-01 12:20:56 +01003 *
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 Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/operators/CpuWinogradConv2d.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Michalis Spyrou96f977e2021-07-01 12:20:56 +010026#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Utils.h"
Michalis Spyrou96f977e2021-07-01 12:20:56 +010028#include "arm_compute/core/utils/misc/ShapeCalculator.h"
29#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "arm_compute/core/Validate.h"
Michalis Spyrou96f977e2021-07-01 12:20:56 +010031#include "arm_compute/runtime/FunctionDescriptors.h"
32#include "arm_compute/runtime/NEON/NEScheduler.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033
ramelg013ae3d882021-09-12 23:07:47 +010034#include "src/common/utils/Log.h"
Michalis Spyrou96f977e2021-07-01 12:20:56 +010035#include "src/core/CPP/Validate.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010036#include "src/core/helpers/MemoryHelpers.h"
37#include "src/core/helpers/WindowHelpers.h"
ramelg01a1f78512022-06-29 16:28:10 +010038#include "src/core/NEON/kernels/assembly/winograd.hpp"
39#include "src/core/NEON/kernels/convolution/common/tensor.hpp"
Michalis Spyrou96f977e2021-07-01 12:20:56 +010040#include "src/core/NEON/kernels/convolution/common/utils.hpp"
ramelg01a1f78512022-06-29 16:28:10 +010041#include "src/core/utils/AssemblyUtils.h"
ramelg01a1f78512022-06-29 16:28:10 +010042#include "src/cpu/kernels/assembly/arm_gemm.hpp"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010043#include "src/cpu/kernels/CpuWinogradConv2dKernel.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010044#include "src/cpu/operators/CpuActivation.h"
45#include "src/cpu/operators/CpuPermute.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010046#include "src/cpu/utils/CpuAuxTensorHandler.h"
Michalis Spyrou96f977e2021-07-01 12:20:56 +010047#include "support/Cast.h"
48
Michalis Spyrou96f977e2021-07-01 12:20:56 +010049namespace arm_compute
50{
51namespace cpu
52{
53using namespace arm_compute::experimental;
54using namespace arm_compute::utils::cast;
55
56namespace
57{
ramelg01a1f78512022-06-29 16:28:10 +010058inline Tensor4DShape internal_get_shape(const ITensorInfo *in)
Michalis Spyrou96f977e2021-07-01 12:20:56 +010059{
ramelg01a1f78512022-06-29 16:28:10 +010060 const DataLayout data_layout = in->data_layout();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010061 const int in_width = in->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH));
62 const int in_height = in->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT));
63 const int in_channels = in->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL));
64 const int in_batches = in->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES));
Michalis Spyrou96f977e2021-07-01 12:20:56 +010065
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010066 return Tensor4DShape{in_batches, in_height, in_width, in_channels};
Michalis Spyrou96f977e2021-07-01 12:20:56 +010067}
68
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069Status validate_arguments(const ITensorInfo *src,
70 const ITensorInfo *weights,
71 const ITensorInfo *biases,
72 const ITensorInfo *dst,
73 const PadStrideInfo &conv_info)
Michalis Spyrou96f977e2021-07-01 12:20:56 +010074{
ramelg01a1f78512022-06-29 16:28:10 +010075 ARM_COMPUTE_UNUSED(dst, weights);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010076 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
Michalis Spyrou96f977e2021-07-01 12:20:56 +010077
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.stride().first != 1 || conv_info.stride().second != 1,
79 "Winograd layer only supports unit strides.");
80 if (biases != nullptr)
Michalis Spyrou96f977e2021-07-01 12:20:56 +010081 {
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010082 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, biases);
Michalis Spyrou96f977e2021-07-01 12:20:56 +010083 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
84 }
ramelg01a1f78512022-06-29 16:28:10 +010085 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
86 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights);
87 return Status{};
Michalis Spyrou96f977e2021-07-01 12:20:56 +010088}
ramelg01a1f78512022-06-29 16:28:10 +010089
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090bool get_winograd_kernel_implementation(const ITensorInfo *src,
91 const ITensorInfo *weights,
92 const ITensorInfo *dst,
93 const PadStrideInfo &conv_info,
94 const ActivationLayerInfo &act_info,
95 bool enable_fast_math,
96 arm_conv::winograd::WinogradImpl *winograd_impl,
97 std::unique_ptr<arm_conv::ConvolutionArgs> &conv_args)
Michalis Spyrou96f977e2021-07-01 12:20:56 +010098{
ramelg01a1f78512022-06-29 16:28:10 +010099 arm_conv::winograd::WinogradConfig winograd_cfg;
100 arm_gemm::GemmConfig cfg;
101
102 const DataType data_type = src->data_type();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 Tensor4DShape in_shape{internal_get_shape(src)};
104 Tensor4DShape out_shape{internal_get_shape(dst)};
105 Tensor4DShape kernel_shape{internal_get_shape(weights)};
David Svantessonded5b182023-08-02 14:23:00 +0000106 uint32_t nthreads = NEScheduler::num_threads();
ramelg01a1f78512022-06-29 16:28:10 +0100107 // Get configuration arguments for Winograd
108 winograd_cfg.output_rows = 0;
109 winograd_cfg.output_cols = 0;
110 conv_args = std::make_unique<arm_conv::ConvolutionArgs>(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100111 in_shape.n_batches,
112 arm_conv::Shape2D{static_cast<uint32_t>(in_shape.n_rows), static_cast<uint32_t>(in_shape.n_cols)},
113 in_shape.n_channels, conv_info.pad_top(), conv_info.pad_left(),
114 arm_conv::Shape2D{static_cast<uint32_t>(out_shape.n_rows), static_cast<uint32_t>(out_shape.n_cols)},
115 out_shape.n_channels,
116 arm_conv::Shape2D{static_cast<uint32_t>(kernel_shape.n_rows), static_cast<uint32_t>(kernel_shape.n_cols)},
117 assembly_utils::map_to_arm_gemm_activation(act_info));
ramelg01a1f78512022-06-29 16:28:10 +0100118
119 bool success = false;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 if (data_type == DataType::F32)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100121 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 success = arm_conv::winograd::get_implementation<float>(*winograd_impl, &CPUInfo::get(), *conv_args, nthreads,
123 enable_fast_math, &winograd_cfg, nullptr);
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100124 }
ramelg01a1f78512022-06-29 16:28:10 +0100125#if defined(__aarch64__) && defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 else if (data_type == DataType::F16)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100127 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100128 success = arm_conv::winograd::get_implementation<__fp16>(*winograd_impl, &CPUInfo::get(), *conv_args, nthreads,
129 enable_fast_math, &winograd_cfg, nullptr);
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100130 }
ramelg01a1f78512022-06-29 16:28:10 +0100131#endif // defined(__aarch64__) && defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
132 else
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100133 {
ramelg01a1f78512022-06-29 16:28:10 +0100134 success = false;
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100135 }
ramelg01a1f78512022-06-29 16:28:10 +0100136 return success;
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100137}
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100138inline bool fuse_function_supported(const ActivationLayerInfo &act_info)
139{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 return act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ||
141 act_info.activation() == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU;
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100142}
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100143} // namespace
144
145CpuWinogradConv2d::CpuWinogradConv2d()
ramelg01a1f78512022-06-29 16:28:10 +0100146
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100147 : _gemm_function(std::make_unique<CpuGemm>()),
148 _activation_func(std::make_unique<CpuActivation>()),
ramelg01a1f78512022-06-29 16:28:10 +0100149 _transform_input_kernel(nullptr),
150 _transform_output_kernel(nullptr),
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100151 _permute_input(std::make_unique<CpuPermute>()),
152 _permute_output(std::make_unique<CpuPermute>()),
153 _permute_weights(std::make_unique<CpuPermute>()),
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100154 _aux_mem(AuxTensorIdx::Count),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 _conv_args{nullptr},
ramelg01a1f78512022-06-29 16:28:10 +0100156 _winograd_impl{},
157 _data_layout(),
158 _winograd_transformed_input{},
159 _winograd_transformed_output{},
160 _winograd_transformed_weights{},
161 _input_workspace(),
162 _output_workspace(),
163 _weights_hwio(),
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100164 _input_nhwc(),
165 _output_nhwc(),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100166 _is_prepared{false},
167 _run_activation{false}
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100168{
169}
170
171CpuWinogradConv2d::~CpuWinogradConv2d() = default;
172
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100173void CpuWinogradConv2d::configure(const ITensorInfo *src,
174 const ITensorInfo *weights,
175 const ITensorInfo *biases,
176 ITensorInfo *dst,
177 const PadStrideInfo &conv_info,
178 const ActivationLayerInfo &act_info,
179 bool enable_fast_math)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100180{
181 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
Ramy Elgammale4e3b2e2022-09-07 12:38:46 +0100182 ARM_COMPUTE_ERROR_THROW_ON(validate(src, weights, biases, dst, conv_info, act_info, enable_fast_math));
ramelg013ae3d882021-09-12 23:07:47 +0100183 ARM_COMPUTE_LOG_PARAMS(src, weights, biases, dst, conv_info, act_info, enable_fast_math);
ramelg01a1f78512022-06-29 16:28:10 +0100184 ARM_COMPUTE_UNUSED(biases);
185 const DataType data_type = src->data_type();
David Svantessonded5b182023-08-02 14:23:00 +0000186 uint32_t nthreads = NEScheduler::num_threads();
ramelg01a1f78512022-06-29 16:28:10 +0100187 _data_layout = src->data_layout();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100188 const Tensor4DShape kernel_shape{internal_get_shape(weights)};
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100189
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100190 bool success = get_winograd_kernel_implementation(src, weights, dst, conv_info, act_info, enable_fast_math,
191 &_winograd_impl, _conv_args);
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100192
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100193 ARM_COMPUTE_EXIT_ON_MSG_VAR(!success, "Unsupported kernel size: %d x %d.\n", kernel_shape.n_rows,
194 kernel_shape.n_cols);
195 ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(arm_compute::logging::LogLevel::INFO, "Using input transform: %s\n",
196 _winograd_impl.input_transform->get_name().c_str());
197 ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(arm_compute::logging::LogLevel::INFO, "Using weight transform: %s\n",
198 _winograd_impl.input_transform->get_name().c_str());
199 ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(arm_compute::logging::LogLevel::INFO, "Using output transform: %s\n",
200 _winograd_impl.input_transform->get_name().c_str());
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100201
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100202 const bool has_impl = ((_winograd_impl.input_transform != nullptr) &&
203 (_winograd_impl.output_transform != nullptr) && (_winograd_impl.gemm_args != nullptr));
204 if (has_impl)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100205 {
ramelg01a1f78512022-06-29 16:28:10 +0100206 // Determine how much working space is required, allocate it.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100207 const size_t input_workspace_size =
208 _winograd_impl.input_transform->get_working_space_size(*_conv_args, nthreads);
209 const size_t output_workspace_size =
210 _winograd_impl.output_transform->get_working_space_size(*_conv_args, nthreads);
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100211
ramelg01a1f78512022-06-29 16:28:10 +0100212 TensorInfo input_workspace_info(TensorShape(input_workspace_size), 1, DataType::U8);
213 TensorInfo output_workspace_info(TensorShape(output_workspace_size), 1, DataType::U8);
214 _input_workspace = input_workspace_info;
215 _output_workspace = output_workspace_info;
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100216
ramelg01a1f78512022-06-29 16:28:10 +0100217 const auto &wds = _winograd_impl.winograd_spec;
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100218
ramelg01a1f78512022-06-29 16:28:10 +0100219 // Preparing winograd transformed input tensor
220 const size_t data_type_size = src->element_size();
221 const uint32_t m = _winograd_impl.gemm_args->_Msize; // Total number of tiles
222 const uint32_t k = _winograd_impl.gemm_args->_Ksize; // Input channels
223 const uint32_t n = _winograd_impl.gemm_args->_Nsize; // Output channels
224 const uint32_t n_gemms = _winograd_impl.gemm_args->_nmulti;
225 const uint32_t n_batches = _winograd_impl.gemm_args->_nbatches;
226 constexpr size_t storage_alignment = 64;
227
228 const TensorShape a_shape(k, m, n_batches, n_gemms);
229 Strides a_strides(data_type_size);
230 a_strides.set(1, data_type_size * _winograd_impl.winograd_spec.input_ld_row);
231 a_strides.set(2, data_type_size * _winograd_impl.winograd_spec.input_ld_batch);
232 a_strides.set(3, data_type_size * _winograd_impl.winograd_spec.input_ld_matrix);
233
234 const TensorShape b_shape(n, k, n_gemms);
235 Strides b_strides(data_type_size);
236 b_strides.set(1, data_type_size * _winograd_impl.winograd_spec.weight_ld_row);
237 b_strides.set(2, data_type_size * _winograd_impl.winograd_spec.weight_ld_matrix);
238
239 const TensorShape d_shape(n, m, n_batches, n_gemms);
240 Strides d_strides(data_type_size);
241 d_strides.set(1, data_type_size * _winograd_impl.winograd_spec.output_ld_row);
242 d_strides.set(2, data_type_size * _winograd_impl.winograd_spec.output_ld_batch);
243 d_strides.set(3, data_type_size * _winograd_impl.winograd_spec.output_ld_matrix);
244
245 TensorInfo a_info{};
246 TensorInfo b_info{};
247 TensorInfo d_info{};
248 a_info.init(a_shape, 1, data_type, a_strides, 0, wds.input_matrix_size_bytes);
249 b_info.init(b_shape, 1, data_type, b_strides, 0, wds.weight_matrix_size_bytes);
250 d_info.init(d_shape, 1, data_type, d_strides, 0, wds.output_matrix_size_bytes);
251
252 _winograd_transformed_input = a_info;
253 _winograd_transformed_weights = b_info;
254 _winograd_transformed_output = d_info;
255
256 PermutationVector weights_permutation_vector(3U, 0U, 1U, 2U);
257
258 // Configure the kernel to transform the input tensor from NCHW -> NHWC
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100259 if (_data_layout == DataLayout::NCHW)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100260 {
ramelg01a1f78512022-06-29 16:28:10 +0100261 _permute_input->configure(src, &_input_nhwc, PermutationVector(2U, 0U, 1U));
262 weights_permutation_vector = PermutationVector(3U, 2U, 0U, 1U);
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100263 }
ramelg01a1f78512022-06-29 16:28:10 +0100264
265 // Re-order a weight tensor from [Output feature map x Input feature map x Height x Width] to [Height x Width x Input feature map x Output feature map]
266 _permute_weights->configure(weights, &_weights_hwio, weights_permutation_vector);
267
268 // Reorder the convoluted output to ACL's ordering NCHW
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100269 if (_data_layout == DataLayout::NCHW)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100270 {
ramelg01a1f78512022-06-29 16:28:10 +0100271 // configure and allocate dst tensor to be used to convert from winograd domain to spatial domain when calling to reshape_output()
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100272 TensorInfo info(TensorShape(dst->dimension(2), dst->dimension(0), dst->dimension(1), dst->dimension(3)), 1,
273 dst->data_type());
ramelg01a1f78512022-06-29 16:28:10 +0100274 _output_nhwc = info;
275 _permute_output->configure(&_output_nhwc, dst, PermutationVector(1U, 2U, 0U));
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100276 }
ramelg01a1f78512022-06-29 16:28:10 +0100277
Viet-Hoa Do65c8db82022-08-03 16:39:23 +0100278 // Configure input transform kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100279 _transform_input_kernel =
280 std::make_unique<CpuWinogradConv2dTransformInputKernel>(_winograd_impl, *_conv_args, nthreads);
Viet-Hoa Do65c8db82022-08-03 16:39:23 +0100281
ramelg01a1f78512022-06-29 16:28:10 +0100282 // Configure GEMM function
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100283 _gemm_function->configure(&_winograd_transformed_input, &_winograd_transformed_weights, nullptr,
284 &_winograd_transformed_output, 1.0f, 0.f);
ramelg01a1f78512022-06-29 16:28:10 +0100285
Viet-Hoa Do65c8db82022-08-03 16:39:23 +0100286 // Configure output transform kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100287 _transform_output_kernel =
288 std::make_unique<CpuWinogradConv2dTransformOutputKernel>(_winograd_impl, *_conv_args, nthreads);
Viet-Hoa Do65c8db82022-08-03 16:39:23 +0100289
ramelg01a1f78512022-06-29 16:28:10 +0100290 //Configure Activation Layer
291 _run_activation = act_info.enabled() && !fuse_function_supported(act_info);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100292 if (_run_activation)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100293 {
ramelg01a1f78512022-06-29 16:28:10 +0100294 _activation_func->configure(dst, nullptr, act_info);
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100295 }
ramelg01a1f78512022-06-29 16:28:10 +0100296
SiCong Lic5ab4df2023-10-17 17:38:57 +0100297 const auto mm_mem_req = _gemm_function->workspace();
298 for (unsigned int slot = 0; slot < mm_mem_req.size(); ++slot)
299 {
300 _aux_mem[slot] = mm_mem_req[slot];
301 }
ramelg01a1f78512022-06-29 16:28:10 +0100302
303 // Request temporary memory. Overlap memory needed for Input/Output transformations as they run on different non-overlapping time-steps.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100304 _aux_mem[TransformedInput] = MemoryInfo(offset_int_vec(TransformedInput), MemoryLifetime::Temporary,
305 wds.input_matrix_size_bytes, storage_alignment);
306 _aux_mem[TransformedOutput] = MemoryInfo(offset_int_vec(TransformedOutput), MemoryLifetime::Temporary,
307 wds.output_matrix_size_bytes, storage_alignment);
308 _aux_mem[WorkspaceIO] = MemoryInfo(offset_int_vec(WorkspaceIO), MemoryLifetime::Temporary,
309 std::max(input_workspace_size, output_workspace_size));
310 _aux_mem[PermutedWeights] =
311 MemoryInfo(offset_int_vec(PermutedWeights), MemoryLifetime::Prepare, _weights_hwio.total_size());
312 _aux_mem[TransformedWeights] = MemoryInfo(offset_int_vec(TransformedWeights), MemoryLifetime::Persistent,
313 wds.weight_matrix_size_bytes, storage_alignment);
314 if (_data_layout == DataLayout::NCHW)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100315 {
ramelg01a1f78512022-06-29 16:28:10 +0100316 _aux_mem[PermutedInput].merge(offset_int_vec(PermutedInput), src->total_size());
317 _aux_mem[PermutedOutput].merge(offset_int_vec(PermutedOutput), dst->total_size());
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100318 }
Georgios Pinitas87a74ef2021-08-20 17:26:45 +0100319 }
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100320}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100321Status CpuWinogradConv2d::validate(const ITensorInfo *src,
322 const ITensorInfo *weights,
323 const ITensorInfo *biases,
324 const ITensorInfo *dst,
325 const PadStrideInfo &conv_info,
326 const ActivationLayerInfo &act_info,
327 bool enable_fast_math)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100328{
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100329 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst);
330 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, dst, conv_info));
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100331
Ramy Elgammale4e3b2e2022-09-07 12:38:46 +0100332 // Disable winograd for fp16 if fast math is false.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100333 if (!enable_fast_math)
Ramy Elgammale4e3b2e2022-09-07 12:38:46 +0100334 {
335 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F32);
336 }
337
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100338 const Tensor4DShape kernel_shape{internal_get_shape(weights)};
ramelg01a1f78512022-06-29 16:28:10 +0100339 arm_conv::winograd::WinogradImpl winograd_impl{};
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100340
ramelg01a1f78512022-06-29 16:28:10 +0100341 std::unique_ptr<arm_conv::ConvolutionArgs> conv_args;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100342 const bool success = get_winograd_kernel_implementation(src, weights, dst, conv_info, act_info, enable_fast_math,
343 &winograd_impl, conv_args);
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100344
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100345 ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR(success == false, "Unsupported kernel size: %d x %d.\n", kernel_shape.n_rows,
346 kernel_shape.n_cols);
347 ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(arm_compute::logging::LogLevel::INFO, "Using input transform: %s\n",
348 winograd_impl.input_transform->get_name().c_str());
349 ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(arm_compute::logging::LogLevel::INFO, "Using weight transform: %s\n",
350 winograd_impl.input_transform->get_name().c_str());
351 ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(arm_compute::logging::LogLevel::INFO, "Using output transform: %s\n",
352 winograd_impl.input_transform->get_name().c_str());
ramelg01a1f78512022-06-29 16:28:10 +0100353 return Status{};
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100354}
355
356void CpuWinogradConv2d::run(ITensorPack &tensors)
357{
358 prepare(tensors);
ramelg01a1f78512022-06-29 16:28:10 +0100359 auto src = tensors.get_const_tensor(ACL_SRC_0);
360 auto biases = tensors.get_const_tensor(ACL_SRC_2);
361 auto output = tensors.get_tensor(ACL_DST);
362 Window win;
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100363
David Svantessonded5b182023-08-02 14:23:00 +0000364 const uint32_t nthreads = NEScheduler::num_threads();
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100365
ramelg01a1f78512022-06-29 16:28:10 +0100366 // The Winograd transform implementation does fine-grain threading inside the transforms. Just pass thread_id and nthreads.
367 win.set(Window::DimX, Window::Dimension(0, nthreads, 1));
368
369 // Wrap the winograd-domain tensorInfos created in configuration in tensors and allocate the required memory.
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100370 CpuAuxTensorHandler input_nhwc(offset_int_vec(PermutedInput), _input_nhwc, tensors, true);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100371 CpuAuxTensorHandler winograd_input_transformed(offset_int_vec(TransformedInput), _winograd_transformed_input,
372 tensors, true);
Georgios Pinitas87a74ef2021-08-20 17:26:45 +0100373 CpuAuxTensorHandler input_workspace(offset_int_vec(WorkspaceIO), _input_workspace, tensors, true);
ramelg01a1f78512022-06-29 16:28:10 +0100374 const bool is_nchw = _data_layout == DataLayout::NCHW;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100375 if (is_nchw)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100376 {
377 //Bring channels to the front as Winograd code expects the tensor to be in the format NHWC
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100378 ITensorPack pack{{ACL_SRC, src}, {ACL_DST, input_nhwc.get()}};
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100379 _permute_input->run(pack);
380 }
381
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100382 CpuAuxTensorHandler winograd_output_transformed(offset_int_vec(TransformedOutput), _winograd_transformed_output,
383 tensors, true);
ramelg01a1f78512022-06-29 16:28:10 +0100384 CpuAuxTensorHandler output_workspace(offset_int_vec(WorkspaceIO), _output_workspace, tensors, true);
385 CpuAuxTensorHandler output_nhwc(offset_int_vec(PermutedOutput), _output_nhwc, tensors, true);
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100386
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100387 ITensorPack transform_input_pack{{ACL_SRC, is_nchw ? input_nhwc.get() : src},
388 {ACL_DST, winograd_input_transformed.get()},
389 {ACL_INT, input_workspace.get()}};
ramelg01a1f78512022-06-29 16:28:10 +0100390 NEScheduler::get().schedule_op(_transform_input_kernel.get(), Window::DimX, win, transform_input_pack);
391
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100392 CpuAuxTensorHandler winograd_weights_transformed(offset_int_vec(TransformedWeights), _winograd_transformed_weights,
393 tensors, true);
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100394
395 // Run 16 GEMMs in multiple threads, each kernel runs one or more GEMMs
Georgios Pinitas87a74ef2021-08-20 17:26:45 +0100396 ITensorPack gemm_pack = tensors;
ramelg01a1f78512022-06-29 16:28:10 +0100397 gemm_pack.add_const_tensor(ACL_SRC, winograd_input_transformed.get());
398 gemm_pack.add_const_tensor(ACL_SRC_1, winograd_weights_transformed.get());
Georgios Pinitas87a74ef2021-08-20 17:26:45 +0100399 gemm_pack.add_const_tensor(ACL_BIAS, nullptr);
ramelg01a1f78512022-06-29 16:28:10 +0100400 gemm_pack.add_tensor(ACL_DST, winograd_output_transformed.get());
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100401 _gemm_function->run(gemm_pack);
402
ramelg01a1f78512022-06-29 16:28:10 +0100403 // Output transform
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100404 ITensorPack transform_output_pack{{ACL_SRC_0, winograd_output_transformed.get()},
405 {ACL_DST, is_nchw ? output_nhwc.get() : output},
406 {ACL_SRC_1, biases},
407 {ACL_INT, output_workspace.get()}};
ramelg01a1f78512022-06-29 16:28:10 +0100408 NEScheduler::get().schedule_op(_transform_output_kernel.get(), Window::DimX, win, transform_output_pack);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100409 if (is_nchw)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100410 {
411 // Reorder the convoluted output to ACL's ordering NCHW
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100412 ITensorPack pack{{ACL_SRC, output_nhwc.get()}, {ACL_DST, output}};
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100413 _permute_output->run(pack);
414 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100415 if (_run_activation)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100416 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100417 ITensorPack pack{{ACL_SRC, output}, {ACL_DST, output}};
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100418 _activation_func->run(pack);
419 }
420}
421
422void CpuWinogradConv2d::prepare(ITensorPack &tensors)
423{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100424 if (!_is_prepared)
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100425 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100426 const ITensor *weights = tensors.get_const_tensor(ACL_SRC_1);
427 ITensor *weights_aux =
428 utils::cast::polymorphic_cast<ITensor *>(tensors.get_tensor(offset_int_vec(PermutedWeights)));
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100429
430 CpuAuxTensorHandler permuted_weights(_weights_hwio, *weights_aux);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100431 ITensorPack permute_tensors{{ACL_SRC, weights}, {ACL_DST, permuted_weights.get()}};
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100432 _permute_weights->run(permute_tensors);
ramelg01a1f78512022-06-29 16:28:10 +0100433 const int element_size_in_bytes = permuted_weights.get()->info()->element_size();
434 // Weights were in OHWI format, before being permuted "permuted_weights" to be in HWIO format.
435 const unsigned int height_idx = 3; // H in HWIO
436 const unsigned int width_idx = 2; // W in HWIO
437 const unsigned int channel_idx = 1; // I in HWIO
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100438
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100439 const int permuted_weight_row_stride =
440 permuted_weights.get()->info()->strides_in_bytes()[height_idx] / element_size_in_bytes;
441 const int permuted_weight_col_stride =
442 permuted_weights.get()->info()->strides_in_bytes()[width_idx] / element_size_in_bytes;
443 const int permuted_weight_channel_stride =
444 permuted_weights.get()->info()->strides_in_bytes()[channel_idx] / element_size_in_bytes;
ramelg01a1f78512022-06-29 16:28:10 +0100445
446 // Wrap the winograd-domain transformed weight TensorInfo in Auxiliary tensor and allocate the required memory.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100447 ITensor *weights_transf =
448 utils::cast::polymorphic_cast<ITensor *>(tensors.get_tensor(offset_int_vec(TransformedWeights)));
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100449 ARM_COMPUTE_ERROR_ON_NULLPTR(weights_transf);
ramelg01a1f78512022-06-29 16:28:10 +0100450 CpuAuxTensorHandler winograd_transformed_weights(_winograd_transformed_weights, *weights_transf);
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100451
ramelg01a1f78512022-06-29 16:28:10 +0100452 const void *permuted_weights_ptr;
453 void *win_wght_transf_ptr;
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100454
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100455 permuted_weights_ptr = reinterpret_cast<const void *>(
456 permuted_weights.get()->buffer() + permuted_weights.get()->info()->offset_first_element_in_bytes());
457 win_wght_transf_ptr =
458 reinterpret_cast<void *>(winograd_transformed_weights.get()->buffer() +
459 winograd_transformed_weights.get()->info()->offset_first_element_in_bytes());
ramelg01a1f78512022-06-29 16:28:10 +0100460
461 // Prepare Weights
462 _winograd_impl.weight_transform->execute(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100463 *_conv_args, permuted_weights_ptr, permuted_weight_row_stride, permuted_weight_col_stride,
464 permuted_weight_channel_stride, win_wght_transf_ptr, _winograd_impl.winograd_spec, 0, 1 // Thread 1 of 1
ramelg01a1f78512022-06-29 16:28:10 +0100465 );
Georgios Pinitas66341942021-07-30 12:21:07 +0100466 ITensorPack gemm_pack = tensors;
ramelg01a1f78512022-06-29 16:28:10 +0100467 gemm_pack.add_const_tensor(ACL_SRC_1, winograd_transformed_weights.get());
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100468 _gemm_function->prepare(gemm_pack);
ramelg01a1f78512022-06-29 16:28:10 +0100469 _is_prepared = 1;
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100470 }
471}
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100472experimental::MemoryRequirements CpuWinogradConv2d::workspace() const
473{
474 return _aux_mem;
475}
ramelg01a1f78512022-06-29 16:28:10 +0100476
Michalis Spyrou96f977e2021-07-01 12:20:56 +0100477} // namespace cpu
ramelg01a1f78512022-06-29 16:28:10 +0100478} // namespace arm_compute