Michalis Spyrou | 96f977e | 2021-07-01 12:20:56 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. |
| 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 "src/runtime/cpu/operators/CpuWinogradConv2d.h" |
| 25 | #include "arm_compute/core/Error.h" |
| 26 | #include "arm_compute/core/Utils.h" |
| 27 | #include "arm_compute/core/Validate.h" |
| 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 29 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 30 | #include "arm_compute/runtime/FunctionDescriptors.h" |
| 31 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 32 | #include "src/core/CPP/Validate.h" |
| 33 | #include "src/core/NEON/kernels/convolution/common/utils.hpp" |
| 34 | #include "src/core/NEON/kernels/convolution/winograd/winograd.hpp" |
| 35 | #include "src/core/cpu/kernels/CpuWinogradConv2dKernel.h" |
| 36 | #include "src/core/helpers/MemoryHelpers.h" |
| 37 | #include "src/runtime/cpu/operators/CpuActivation.h" |
| 38 | #include "src/runtime/cpu/operators/CpuPermute.h" |
| 39 | #include "src/runtime/cpu/operators/CpuWinogradConv2d.h" |
| 40 | #include "src/runtime/cpu/utils/CpuAuxTensorHandler.h" |
| 41 | |
| 42 | #include "support/Cast.h" |
| 43 | |
| 44 | #include <set> |
| 45 | |
| 46 | namespace arm_compute |
| 47 | { |
| 48 | namespace cpu |
| 49 | { |
| 50 | using namespace arm_compute::experimental; |
| 51 | using namespace arm_compute::utils::cast; |
| 52 | |
| 53 | namespace |
| 54 | { |
| 55 | arm_gemm::Activation arm_gemm_activation_from_acl_activation(const ActivationLayerInfo &act_info) |
| 56 | { |
| 57 | switch(act_info.activation()) |
| 58 | { |
| 59 | case ActivationLayerInfo::ActivationFunction::RELU: |
| 60 | { |
| 61 | return arm_gemm::Activation(arm_gemm::Activation::Type::ReLU, act_info.a(), act_info.b()); |
| 62 | } |
| 63 | case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: |
| 64 | { |
| 65 | return arm_gemm::Activation(arm_gemm::Activation::Type::BoundedReLU, act_info.a(), act_info.b()); |
| 66 | } |
| 67 | default: |
| 68 | { |
| 69 | return arm_gemm::Activation(arm_gemm::Activation::Type::None); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | inline Status validate_kernel_3x3(const Size2D input_dims, const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output, |
| 75 | const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info) |
| 76 | { |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 79 | |
| 80 | if(input->data_type() == DataType::F32) |
| 81 | { |
| 82 | if(input_dims.width > 4 && input_dims.height > 4) |
| 83 | { |
| 84 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformInputKernel<float, 4, 4, 3, 3>::validate(input, input0, winograd_info))); |
| 85 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformWeightsKernel<float, 4, 4, 3, 3>::validate(weights, input1, winograd_info))); |
| 86 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformOutputKernel<float, 4, 4, 3, 3>::validate(batched_mm_output, biases, output, winograd_info))); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformInputKernel<float, 2, 2, 3, 3>::validate(input, input0, winograd_info))); |
| 91 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformWeightsKernel<float, 2, 2, 3, 3>::validate(weights, input1, winograd_info))); |
| 92 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformOutputKernel<float, 2, 2, 3, 3>::validate(batched_mm_output, biases, output, winograd_info))); |
| 93 | } |
| 94 | } |
| 95 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 96 | else if(input->data_type() == DataType::F16) |
| 97 | { |
| 98 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformInputKernel<__fp16, 4, 4, 3, 3>::validate(input, input0, winograd_info))); |
| 99 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformWeightsKernel<__fp16, 4, 4, 3, 3>::validate(weights, input1, winograd_info))); |
| 100 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformOutputKernel<__fp16, 4, 4, 3, 3>::validate(batched_mm_output, biases, output, winograd_info))); |
| 101 | } |
| 102 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 103 | |
| 104 | if(act_info.enabled()) |
| 105 | { |
| 106 | CpuActivation::validate(output, nullptr, act_info); |
| 107 | } |
| 108 | return Status{}; |
| 109 | } |
| 110 | |
| 111 | inline Status validate_kernel_5x5(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output, |
| 112 | const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info) |
| 113 | { |
| 114 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformInputKernel<float, 2, 2, 5, 5>::validate(input, input0, winograd_info))); |
| 115 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformWeightsKernel<float, 2, 2, 5, 5>::validate(weights, input1, winograd_info))); |
| 116 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformOutputKernel<float, 2, 2, 5, 5>::validate(batched_mm_output, biases, output, winograd_info))); |
| 117 | if(act_info.enabled()) |
| 118 | { |
| 119 | CpuActivation::validate(output, nullptr, act_info); |
| 120 | } |
| 121 | return Status{}; |
| 122 | } |
| 123 | |
| 124 | inline Status validate_kernel_3x1(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output, |
| 125 | const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info) |
| 126 | { |
| 127 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 128 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformInputKernel<float, 1, 6, 1, 3>::validate(input, input0, winograd_info))); |
| 129 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformWeightsKernel<float, 1, 6, 1, 3>::validate(weights, input1, winograd_info))); |
| 130 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformOutputKernel<float, 1, 6, 1, 3>::validate(batched_mm_output, biases, output, winograd_info))); |
| 131 | if(act_info.enabled()) |
| 132 | { |
| 133 | CpuActivation::validate(output, nullptr, act_info); |
| 134 | } |
| 135 | return Status{}; |
| 136 | } |
| 137 | |
| 138 | inline Status validate_kernel_1x3(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output, |
| 139 | const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info) |
| 140 | { |
| 141 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 142 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformInputKernel<float, 6, 1, 3, 1>::validate(input, input0, winograd_info))); |
| 143 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformWeightsKernel<float, 6, 1, 3, 1>::validate(weights, input1, winograd_info))); |
| 144 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformOutputKernel<float, 6, 1, 3, 1>::validate(batched_mm_output, biases, output, winograd_info))); |
| 145 | |
| 146 | if(act_info.enabled()) |
| 147 | { |
| 148 | CpuActivation::validate(output, nullptr, act_info); |
| 149 | } |
| 150 | return Status{}; |
| 151 | } |
| 152 | |
| 153 | inline Status validate_kernel_5x1(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output, |
| 154 | const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info) |
| 155 | { |
| 156 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 157 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformInputKernel<float, 1, 4, 1, 5>::validate(input, input0, winograd_info))); |
| 158 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformWeightsKernel<float, 1, 4, 1, 5>::validate(weights, input1, winograd_info))); |
| 159 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformOutputKernel<float, 1, 4, 1, 5>::validate(batched_mm_output, biases, output, winograd_info))); |
| 160 | if(act_info.enabled()) |
| 161 | { |
| 162 | CpuActivation::validate(output, nullptr, act_info); |
| 163 | } |
| 164 | return Status{}; |
| 165 | } |
| 166 | inline Status validate_kernel_1x5(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output, |
| 167 | const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info) |
| 168 | { |
| 169 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 170 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformInputKernel<float, 4, 1, 5, 1>::validate(input, input0, winograd_info))); |
| 171 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformWeightsKernel<float, 4, 1, 5, 1>::validate(weights, input1, winograd_info))); |
| 172 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformOutputKernel<float, 4, 1, 5, 1>::validate(batched_mm_output, biases, output, winograd_info))); |
| 173 | if(act_info.enabled()) |
| 174 | { |
| 175 | CpuActivation::validate(output, nullptr, act_info); |
| 176 | } |
| 177 | return Status{}; |
| 178 | } |
| 179 | |
| 180 | inline Status validate_kernel_7x1(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output, |
| 181 | const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info) |
| 182 | { |
| 183 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 184 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformInputKernel<float, 1, 2, 1, 7>::validate(input, input0, winograd_info))); |
| 185 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformWeightsKernel<float, 1, 2, 1, 7>::validate(weights, input1, winograd_info))); |
| 186 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformOutputKernel<float, 1, 2, 1, 7>::validate(batched_mm_output, biases, output, winograd_info))); |
| 187 | if(act_info.enabled()) |
| 188 | { |
| 189 | CpuActivation::validate(output, nullptr, act_info); |
| 190 | } |
| 191 | return Status{}; |
| 192 | } |
| 193 | |
| 194 | inline Status validate_kernel_1x7(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output, |
| 195 | const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info) |
| 196 | { |
| 197 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 198 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformInputKernel<float, 2, 1, 7, 1>::validate(input, input0, winograd_info))); |
| 199 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformWeightsKernel<float, 2, 1, 7, 1>::validate(weights, input1, winograd_info))); |
| 200 | ARM_COMPUTE_RETURN_ON_ERROR((CpuWinogradConv2dTransformOutputKernel<float, 2, 1, 7, 1>::validate(batched_mm_output, biases, output, winograd_info))); |
| 201 | |
| 202 | if(act_info.enabled()) |
| 203 | { |
| 204 | CpuActivation::validate(output, nullptr, act_info); |
| 205 | } |
| 206 | return Status{}; |
| 207 | } |
| 208 | |
| 209 | inline Tensor4DShape internal_get_input_shape(const ITensorInfo *input) |
| 210 | { |
| 211 | const DataLayout data_layout = input->data_layout(); |
| 212 | const int in_width = input->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)); |
| 213 | const int in_height = input->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)); |
| 214 | const int in_channels = input->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL)); |
| 215 | const int in_batches = input->dimension(3); |
| 216 | |
| 217 | return Tensor4DShape{ in_batches, in_height, in_width, in_channels }; |
| 218 | } |
| 219 | |
| 220 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info) |
| 221 | { |
| 222 | ARM_COMPUTE_UNUSED(output); |
| 223 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
| 224 | |
| 225 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.stride().first != 1 || conv_info.stride().second != 1, "Winograd layer only supports unit strides."); |
| 226 | if(biases != nullptr) |
| 227 | { |
| 228 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 229 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 230 | } |
| 231 | return ICpuWinogradConv2dTransformWeightsKernel::validate(input, weights); |
| 232 | } |
| 233 | Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims, DataType data_type) |
| 234 | { |
| 235 | Size2D output_tile = Size2D{}; |
| 236 | if(kernel_dims == Size2D(3U, 3U)) |
| 237 | { |
| 238 | output_tile = (input_dims.width <= 4 || input_dims.height <= 4) ? Size2D(2U, 2U) : Size2D(4U, 4U); |
| 239 | if(data_type == DataType::F16) |
| 240 | { |
| 241 | output_tile = Size2D(4U, 4U); |
| 242 | } |
| 243 | } |
| 244 | else if(kernel_dims == Size2D(5U, 5U)) |
| 245 | { |
| 246 | output_tile = Size2D(2U, 2U); |
| 247 | } |
| 248 | else if(kernel_dims == Size2D(1U, 3U)) |
| 249 | { |
| 250 | output_tile = Size2D(1U, 6U); |
| 251 | } |
| 252 | else if(kernel_dims == Size2D(3U, 1U)) |
| 253 | { |
| 254 | output_tile = Size2D(6U, 1U); |
| 255 | } |
| 256 | else if(kernel_dims == Size2D(1U, 5U)) |
| 257 | { |
| 258 | output_tile = Size2D(1U, 4U); |
| 259 | } |
| 260 | else if(kernel_dims == Size2D(5U, 1U)) |
| 261 | { |
| 262 | output_tile = Size2D(4U, 1U); |
| 263 | } |
| 264 | else if(kernel_dims == Size2D(7U, 1U)) |
| 265 | { |
| 266 | output_tile = Size2D(2U, 1U); |
| 267 | } |
| 268 | else if(kernel_dims == Size2D(1U, 7U)) |
| 269 | { |
| 270 | output_tile = Size2D(1U, 2U); |
| 271 | } |
| 272 | return output_tile; |
| 273 | } |
| 274 | |
| 275 | bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size, DataType data_type) |
| 276 | { |
| 277 | // Check if we want to configure a Winograd configuration which requires fast math |
| 278 | using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>; |
| 279 | |
| 280 | const std::vector<WinogradConfiguration> fast_math_winograd_f16 = |
| 281 | { |
| 282 | WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)) |
| 283 | }; |
| 284 | |
| 285 | const std::vector<WinogradConfiguration> fast_math_winograd_f32 = |
| 286 | { |
| 287 | WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(5, 5)), |
| 288 | WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)) |
| 289 | }; |
| 290 | |
| 291 | auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height), |
| 292 | std::pair<int, int>(kernel_size.width, kernel_size.height)); |
| 293 | |
| 294 | switch(data_type) |
| 295 | { |
| 296 | case DataType::F16: |
| 297 | return std::find(fast_math_winograd_f16.begin(), fast_math_winograd_f16.end(), p) != fast_math_winograd_f16.end(); |
| 298 | case DataType::F32: |
| 299 | return std::find(fast_math_winograd_f32.begin(), fast_math_winograd_f32.end(), p) != fast_math_winograd_f32.end(); |
| 300 | default: |
| 301 | return false; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | inline bool fuse_function_supported(const ActivationLayerInfo &act_info) |
| 306 | { |
| 307 | return act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU || act_info.activation() == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU; |
| 308 | } |
| 309 | |
| 310 | } // namespace |
| 311 | |
| 312 | CpuWinogradConv2d::CpuWinogradConv2d() |
| 313 | : _gemm_function(std::make_unique<CpuGemm>()), |
| 314 | _activation_func(std::make_unique<CpuActivation>()), |
| 315 | _permute_input(std::make_unique<CpuPermute>()), |
| 316 | _permute_output(std::make_unique<CpuPermute>()), |
| 317 | _permute_weights(std::make_unique<CpuPermute>()), |
| 318 | _transform_input_kernel(nullptr), |
| 319 | _transform_weights_kernel(nullptr), |
| 320 | _transform_output_kernel(nullptr), |
| 321 | _data_layout(), |
| 322 | _aux_mem(AuxTensorIdx::Count), |
| 323 | _input_nhwc(), |
| 324 | _output_nhwc(), |
| 325 | _input_workspace(), |
| 326 | _kernel_storage(), |
| 327 | _output_workspace(), |
| 328 | _input_transformed(), |
| 329 | _output_transformed(), |
| 330 | _weights_hwio(), |
| 331 | _run_activation(false), |
| 332 | _is_prepared(false) |
| 333 | { |
| 334 | } |
| 335 | |
| 336 | CpuWinogradConv2d::~CpuWinogradConv2d() = default; |
| 337 | |
| 338 | void CpuWinogradConv2d::configure(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, |
| 339 | const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, bool enable_fast_math) |
| 340 | { |
| 341 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst); |
| 342 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, weights, biases, dst, conv_info)); |
| 343 | |
| 344 | // Get indices for the width and height |
| 345 | _data_layout = src->data_layout(); |
| 346 | const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH); |
| 347 | const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 348 | const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL); |
| 349 | |
| 350 | const Size2D input_dims = Size2D(src->dimension(width_idx), src->dimension(height_idx)); |
| 351 | const Size2D kernel_size = Size2D(weights->dimension(width_idx), weights->dimension(height_idx)); |
| 352 | const DataType data_type = src->data_type(); |
| 353 | const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, data_type); |
| 354 | |
| 355 | // Check if the Winograd configuration requires fast math |
| 356 | if(!enable_fast_math) |
| 357 | { |
| 358 | ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size, data_type), |
| 359 | "This Winograd configuration requires enable_fast_math=true"); |
| 360 | } |
| 361 | |
| 362 | _is_prepared = false; |
| 363 | |
| 364 | std::unique_ptr<ICpuWinogradConv2dTransformInputKernel> transform_input_kernel; |
| 365 | std::unique_ptr<ICpuWinogradConv2dTransformWeightsKernel> transform_weights_kernel; |
| 366 | std::unique_ptr<ICpuWinogradConv2dTransformOutputKernel> transform_output_kernel; |
| 367 | |
| 368 | int n_gemms = 1; |
| 369 | int N_BLOCK = 1; // Size of block used by GEMM. |
| 370 | if(data_type == DataType::F32) |
| 371 | { |
| 372 | if(kernel_size == Size2D(3, 3)) |
| 373 | { |
| 374 | if(src->dimension(width_idx) > 4 && src->dimension(height_idx) > 4) |
| 375 | { |
| 376 | using config = CpuWinogradConv2dConfiguration<float, float, 4, 4, 3, 3>; |
| 377 | transform_input_kernel = std::make_unique<config::TransformInputKernel>(); |
| 378 | transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>(); |
| 379 | transform_output_kernel = std::make_unique<config::TransformOutputKernel>(); |
| 380 | n_gemms = config::WinogradBase::N_GEMMS; |
| 381 | N_BLOCK = config::WinogradConv::N_BLOCK; |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | using config = CpuWinogradConv2dConfiguration<float, float, 2, 2, 3, 3>; |
| 386 | transform_input_kernel = std::make_unique<config::TransformInputKernel>(); |
| 387 | transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>(); |
| 388 | transform_output_kernel = std::make_unique<config::TransformOutputKernel>(); |
| 389 | n_gemms = config::WinogradBase::N_GEMMS; |
| 390 | N_BLOCK = config::WinogradConv::N_BLOCK; |
| 391 | } |
| 392 | } |
| 393 | else if(kernel_size == Size2D(5, 5)) |
| 394 | { |
| 395 | using config = CpuWinogradConv2dConfiguration<float, float, 2, 2, 5, 5>; |
| 396 | transform_input_kernel = std::make_unique<config::TransformInputKernel>(); |
| 397 | transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>(); |
| 398 | transform_output_kernel = std::make_unique<config::TransformOutputKernel>(); |
| 399 | n_gemms = config::WinogradBase::N_GEMMS; |
| 400 | N_BLOCK = config::WinogradConv::N_BLOCK; |
| 401 | } |
| 402 | else if(kernel_size == Size2D(1, 3)) |
| 403 | { |
| 404 | using config = CpuWinogradConv2dConfiguration<float, float, 6, 1, 3, 1>; |
| 405 | transform_input_kernel = std::make_unique<config::TransformInputKernel>(); |
| 406 | transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>(); |
| 407 | transform_output_kernel = std::make_unique<config::TransformOutputKernel>(); |
| 408 | n_gemms = config::WinogradBase::N_GEMMS; |
| 409 | N_BLOCK = config::WinogradConv::N_BLOCK; |
| 410 | } |
| 411 | else if(kernel_size == Size2D(3, 1)) |
| 412 | { |
| 413 | using config = CpuWinogradConv2dConfiguration<float, float, 1, 6, 1, 3>; |
| 414 | transform_input_kernel = std::make_unique<config::TransformInputKernel>(); |
| 415 | transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>(); |
| 416 | transform_output_kernel = std::make_unique<config::TransformOutputKernel>(); |
| 417 | n_gemms = config::WinogradBase::N_GEMMS; |
| 418 | N_BLOCK = config::WinogradConv::N_BLOCK; |
| 419 | } |
| 420 | else if(kernel_size == Size2D(1, 5)) |
| 421 | { |
| 422 | using config = CpuWinogradConv2dConfiguration<float, float, 4, 1, 5, 1>; |
| 423 | transform_input_kernel = std::make_unique<config::TransformInputKernel>(); |
| 424 | transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>(); |
| 425 | transform_output_kernel = std::make_unique<config::TransformOutputKernel>(); |
| 426 | n_gemms = config::WinogradBase::N_GEMMS; |
| 427 | N_BLOCK = config::WinogradConv::N_BLOCK; |
| 428 | } |
| 429 | else if(kernel_size == Size2D(5, 1)) |
| 430 | { |
| 431 | using config = CpuWinogradConv2dConfiguration<float, float, 1, 4, 1, 5>; |
| 432 | transform_input_kernel = std::make_unique<config::TransformInputKernel>(); |
| 433 | transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>(); |
| 434 | transform_output_kernel = std::make_unique<config::TransformOutputKernel>(); |
| 435 | n_gemms = config::WinogradBase::N_GEMMS; |
| 436 | N_BLOCK = config::WinogradConv::N_BLOCK; |
| 437 | } |
| 438 | else if(kernel_size == Size2D(1, 7)) |
| 439 | { |
| 440 | using config = CpuWinogradConv2dConfiguration<float, float, 2, 1, 7, 1>; |
| 441 | transform_input_kernel = std::make_unique<config::TransformInputKernel>(); |
| 442 | transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>(); |
| 443 | transform_output_kernel = std::make_unique<config::TransformOutputKernel>(); |
| 444 | n_gemms = config::WinogradBase::N_GEMMS; |
| 445 | N_BLOCK = config::WinogradConv::N_BLOCK; |
| 446 | } |
| 447 | else if(kernel_size == Size2D(7, 1)) |
| 448 | { |
| 449 | using config = CpuWinogradConv2dConfiguration<float, float, 1, 2, 1, 7>; |
| 450 | transform_input_kernel = std::make_unique<config::TransformInputKernel>(); |
| 451 | transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>(); |
| 452 | transform_output_kernel = std::make_unique<config::TransformOutputKernel>(); |
| 453 | n_gemms = config::WinogradBase::N_GEMMS; |
| 454 | N_BLOCK = config::WinogradConv::N_BLOCK; |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | ARM_COMPUTE_ERROR("Not supported."); |
| 459 | } |
| 460 | } |
| 461 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 462 | else if(data_type == DataType::F16) |
| 463 | { |
| 464 | if(kernel_size == Size2D(3, 3)) |
| 465 | { |
| 466 | using config = CpuWinogradConv2dConfiguration<__fp16, __fp16, 4, 4, 3, 3>; |
| 467 | transform_input_kernel = std::make_unique<config::TransformInputKernel>(); |
| 468 | transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>(); |
| 469 | transform_output_kernel = std::make_unique<config::TransformOutputKernel>(); |
| 470 | n_gemms = config::WinogradBase::N_GEMMS; |
| 471 | N_BLOCK = config::WinogradConv::N_BLOCK; |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | ARM_COMPUTE_ERROR("Not supported."); |
| 476 | } |
| 477 | } |
| 478 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 479 | else |
| 480 | { |
| 481 | ARM_COMPUTE_ERROR("Not supported."); |
| 482 | } |
| 483 | |
| 484 | const PaddingType use_padding_type = (conv_info.pad_top() != 0u || conv_info.pad_left() != 0) ? PADDING_SAME : PADDING_VALID; |
| 485 | const bool use_same_padding = use_padding_type == PADDING_SAME; |
| 486 | |
| 487 | // Get convolved dimensions |
| 488 | const int in_channels = src->dimension(channel_idx); |
| 489 | const int out_channels = dst->dimension(channel_idx); |
| 490 | |
| 491 | const Tensor4DShape in_shape(internal_get_input_shape(src)); |
| 492 | const size_t data_type_size = src->element_size(); |
| 493 | // Get the memory required to instantiate a new Winograd operator. |
| 494 | constexpr size_t storage_alignment = 64; |
| 495 | |
| 496 | // Kernel Storage |
| 497 | const size_t kernel_storage_size = transform_weights_kernel->get_weight_storage_size(out_channels, |
| 498 | in_channels) |
| 499 | * data_type_size; |
| 500 | |
| 501 | // Input storage |
| 502 | const size_t input_storage_size = transform_input_kernel->get_input_storage_size(in_shape.n_batches, in_shape.n_channels, in_shape.n_rows, in_shape.n_cols, |
| 503 | use_same_padding) |
| 504 | * data_type_size; |
| 505 | |
| 506 | // Output storage |
| 507 | const size_t output_storage_size = transform_output_kernel->get_output_storage_size(in_shape.n_batches, in_shape.n_rows, in_shape.n_cols, out_channels) * data_type_size; |
| 508 | const int kernel_matrix_stride = transform_weights_kernel->get_matrix_stride(out_channels, in_channels); |
| 509 | const int output_matrix_stride = transform_output_kernel->get_matrix_stride(in_shape.n_batches, in_shape.n_rows, in_shape.n_cols, out_channels); |
| 510 | const auto output_shape = transform_output_kernel->get_output_shape(in_shape.n_rows, in_shape.n_cols, use_padding_type == PADDING_SAME); |
| 511 | const int input_matrix_stride = transform_input_kernel->get_matrix_stride(in_shape.n_batches, in_channels, in_shape.n_rows, in_shape.n_cols, use_padding_type == PADDING_SAME); |
| 512 | |
| 513 | // Configure GEMM |
| 514 | const int tile_rows = iceildiv(output_shape.first, output_tile.height); |
| 515 | const int tile_cols = iceildiv(output_shape.second, output_tile.width); |
| 516 | const int m = in_shape.n_batches * tile_rows * tile_cols; |
| 517 | const int k = in_shape.n_channels; |
| 518 | const int n = out_channels; |
| 519 | const int kernel_matrix_row_stride = roundup(out_channels, N_BLOCK); |
| 520 | const int output_matrix_row_stride = kernel_matrix_row_stride; |
| 521 | |
| 522 | TensorShape a_shape(k, m, 1, n_gemms); |
| 523 | Strides a_strides(data_type_size); |
| 524 | a_strides.set(1, a_strides[0] * k); |
| 525 | //a_strides.set(2, data_type_size * input_matrix_stride / n_gemms); FIXME: This is the real batch size, but RSH's code crashes if it's not 0. |
| 526 | a_strides.set(2, 0); |
| 527 | a_strides.set(3, data_type_size * input_matrix_stride); |
| 528 | |
| 529 | TensorShape b_shape(n, k, n_gemms); |
| 530 | Strides b_strides(data_type_size); |
| 531 | b_strides.set(1, data_type_size * kernel_matrix_row_stride); |
| 532 | b_strides.set(2, data_type_size * kernel_matrix_stride); |
| 533 | |
| 534 | TensorShape d_shape(n, m, 1, n_gemms); |
| 535 | Strides d_strides(data_type_size); |
| 536 | d_strides.set(1, data_type_size * output_matrix_row_stride); |
| 537 | //d_strides.set(2, data_type_size * output_matrix_stride / n_gemms); FIXME: This is the real batch size, but RSH's code crashes if it's not 0. |
| 538 | d_strides.set(2, 0); |
| 539 | d_strides.set(3, data_type_size * output_matrix_stride); |
| 540 | |
| 541 | TensorInfo a_info{}; |
| 542 | TensorInfo b_info{}; |
| 543 | TensorInfo d_info{}; |
| 544 | a_info.init(a_shape, 1, data_type, a_strides, 0, input_storage_size); |
| 545 | b_info.init(b_shape, 1, data_type, b_strides, 0, kernel_storage_size); |
| 546 | d_info.init(d_shape, 1, data_type, d_strides, 0, output_storage_size); |
| 547 | |
| 548 | _input_transformed = a_info; |
| 549 | _kernel_storage = b_info; |
| 550 | _output_transformed = d_info; |
| 551 | |
| 552 | // configure and allocate dst tensor to be used to convert from winograd domain to spatial domain when calling to reshape_output() |
| 553 | TensorInfo info(TensorShape(dst->dimension(2), dst->dimension(0), |
| 554 | dst->dimension(1), dst->dimension(3)), |
| 555 | 1, dst->data_type()); |
| 556 | _output_nhwc = info; |
| 557 | |
| 558 | const ITensorInfo *input_to_use = src; |
| 559 | ITensorInfo *output_to_use = dst; |
| 560 | PermutationVector weights_permutation_vector(3U, 0U, 1U, 2U); |
| 561 | const unsigned int max_num_threads = NEScheduler::get().num_threads(); |
| 562 | |
| 563 | // Configure the kernel to transform the input tensor from NCHW -> NHWC |
| 564 | if(_data_layout == DataLayout::NCHW) |
| 565 | { |
| 566 | _permute_input->configure(src, &_input_nhwc, PermutationVector(2U, 0U, 1U)); |
| 567 | _aux_mem[PermutedInput] = MemoryInfo(offset_int_vec(PermutedInput), MemoryLifetime::Temporary, src->total_size()); |
| 568 | input_to_use = &_input_nhwc; |
| 569 | weights_permutation_vector = PermutationVector(3U, 2U, 0U, 1U); |
| 570 | } |
| 571 | |
| 572 | // Configure input transform kernel |
| 573 | transform_input_kernel->configure(input_to_use, in_shape.n_batches, in_shape.n_rows, in_shape.n_cols, in_shape.n_channels, use_padding_type, |
| 574 | &_input_transformed, input_matrix_stride, &_input_workspace); |
| 575 | const size_t input_workspace_size = transform_input_kernel->get_working_space_size(max_num_threads); |
| 576 | TensorInfo input_workspace_info(TensorShape(input_workspace_size), 1, src->data_type()); |
| 577 | _input_workspace = input_workspace_info; |
| 578 | |
| 579 | // 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] |
| 580 | _permute_weights->configure(weights, &_weights_hwio, weights_permutation_vector); |
| 581 | transform_weights_kernel->configure(&_weights_hwio, &_kernel_storage, kernel_matrix_stride, out_channels, in_channels); |
| 582 | |
| 583 | // Configure GEMM function |
| 584 | _gemm_function->configure(&_input_transformed, &_kernel_storage, nullptr, &_output_transformed, 1.0f, 0.f); |
| 585 | |
| 586 | // Configure output transform function |
| 587 | // The biases tensor has not been allocated at this point in time, the output transform will add the biases to the final result in the run() method |
| 588 | if(_data_layout == DataLayout::NCHW) |
| 589 | { |
| 590 | output_to_use = &_output_nhwc; |
| 591 | } |
| 592 | const arm_gemm::Activation activation = arm_gemm_activation_from_acl_activation(act_info); |
| 593 | |
| 594 | transform_output_kernel->configure(biases, |
| 595 | &_output_transformed, |
| 596 | output_matrix_stride, |
| 597 | output_to_use, |
| 598 | in_shape.n_batches, |
| 599 | output_shape.first, |
| 600 | output_shape.second, |
| 601 | out_channels, |
| 602 | &_output_workspace, |
| 603 | activation); |
| 604 | |
| 605 | const size_t output_workspace_size = transform_output_kernel->get_working_space_size(max_num_threads); |
| 606 | TensorInfo output_workspace_info(TensorShape(output_workspace_size), 1, dst->data_type()); |
| 607 | _output_workspace = output_workspace_info; |
| 608 | |
| 609 | // Reorder the convoluted output to ACL's ordering NCHW |
| 610 | if(_data_layout == DataLayout::NCHW) |
| 611 | { |
| 612 | _permute_output->configure(&_output_nhwc, dst, PermutationVector(1U, 2U, 0U)); |
| 613 | _aux_mem[PermutedOutput] = MemoryInfo(offset_int_vec(PermutedOutput), MemoryLifetime::Temporary, dst->total_size()); |
| 614 | } |
| 615 | |
| 616 | _transform_input_kernel = std::move(transform_input_kernel); |
| 617 | _transform_weights_kernel = std::move(transform_weights_kernel); |
| 618 | _transform_output_kernel = std::move(transform_output_kernel); |
| 619 | |
| 620 | //Configure Activation Layer |
| 621 | _run_activation = act_info.enabled() && !fuse_function_supported(act_info); |
| 622 | if(_run_activation) |
| 623 | { |
| 624 | _activation_func->configure(dst, nullptr, act_info); |
| 625 | } |
| 626 | |
| 627 | auto asm_mem_req = _gemm_function->workspace(); |
| 628 | _aux_mem[GemmWorkspace] = asm_mem_req[GemmWorkspace]; |
| 629 | _aux_mem[Pretranspose] = asm_mem_req[Pretranspose]; |
| 630 | _aux_mem[InterleavedLHS] = asm_mem_req[InterleavedLHS]; |
| 631 | _aux_mem[TransposedRHS] = asm_mem_req[TransposedRHS]; |
| 632 | _aux_mem[TempResult] = asm_mem_req[TempResult]; |
| 633 | |
| 634 | _aux_mem[InputTransformed] = MemoryInfo(offset_int_vec(InputTransformed), MemoryLifetime::Persistent, input_storage_size, storage_alignment); |
| 635 | _aux_mem[InputWorkspace] = MemoryInfo(offset_int_vec(InputWorkspace), MemoryLifetime::Persistent, input_workspace_size); |
| 636 | if(_aux_mem[Pretranspose].size > 0) |
| 637 | { |
| 638 | // Release permuted weights at the of prepare as they are further transposed by the assembly dispatch |
| 639 | _aux_mem[PermutedWeights] = MemoryInfo(offset_int_vec(PermutedWeights), MemoryLifetime::Prepare, _weights_hwio.total_size()); |
| 640 | } |
| 641 | else |
| 642 | { |
| 643 | _aux_mem[PermutedWeights] = MemoryInfo(offset_int_vec(PermutedWeights), MemoryLifetime::Persistent, _weights_hwio.total_size()); |
| 644 | } |
| 645 | _aux_mem[WeightsTransformed] = MemoryInfo(offset_int_vec(WeightsTransformed), MemoryLifetime::Persistent, kernel_storage_size, storage_alignment); |
| 646 | _aux_mem[OutputTransformed] = MemoryInfo(offset_int_vec(OutputTransformed), MemoryLifetime::Persistent, output_storage_size, storage_alignment); |
| 647 | _aux_mem[OutputWorkspace] = MemoryInfo(offset_int_vec(OutputWorkspace), MemoryLifetime::Persistent, output_workspace_size); |
| 648 | } |
| 649 | |
| 650 | Status CpuWinogradConv2d::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 651 | const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, bool enable_fast_math) |
| 652 | { |
| 653 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 654 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info)); |
| 655 | |
| 656 | // Get indices for the width and height |
| 657 | const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 658 | const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
| 659 | |
| 660 | // Input shape, kernel size and output tile |
| 661 | const Size2D input_dims = Size2D(input->dimension(idx_width), input->dimension(idx_height)); |
| 662 | const Size2D kernel_size = Size2D(weights->dimension(idx_width), weights->dimension(idx_height)); |
| 663 | const DataType data_type = input->data_type(); |
| 664 | const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, data_type); |
| 665 | |
| 666 | // Check if the Winograd configuration requires fast math |
| 667 | if(!enable_fast_math) |
| 668 | { |
| 669 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size, data_type), |
| 670 | "This Winograd configuration requires enable_fast_math=true"); |
| 671 | } |
| 672 | |
| 673 | const WinogradInfo winograd_info = WinogradInfo(output_tile, |
| 674 | kernel_size, |
| 675 | input_dims, |
| 676 | conv_info, |
| 677 | input->data_layout()); |
| 678 | |
| 679 | // Validate input transform |
| 680 | const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info); |
| 681 | const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape); |
| 682 | // Validate filter transform |
| 683 | const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info); |
| 684 | const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape); |
| 685 | // Validate batched matrix multiply |
| 686 | TensorShape batched_mm_output_shape = input0.tensor_shape(); |
| 687 | batched_mm_output_shape[0] = input1.tensor_shape()[0]; |
| 688 | const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape); |
| 689 | |
| 690 | if(kernel_size == Size2D(3, 3)) |
| 691 | { |
| 692 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 1, "Only SAME or VALID padding supported"); |
| 693 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 1, "Only SAME or VALID padding supported"); |
| 694 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 1, "Only SAME or VALID padding supported"); |
| 695 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 1, "Only SAME or VALID padding supported"); |
| 696 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != conv_info.pad_left(), "Only SAME or VALID padding supported"); |
| 697 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != conv_info.pad_bottom(), "Only SAME or VALID padding supported"); |
| 698 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != conv_info.pad_left(), "Only SAME or VALID padding supported"); |
| 699 | return validate_kernel_3x3(input_dims, input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info); |
| 700 | } |
| 701 | else if(kernel_size == Size2D(5, 5)) |
| 702 | { |
| 703 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 2, "Only SAME or VALID padding supported"); |
| 704 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 2, "Only SAME or VALID padding supported"); |
| 705 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 2, "Only SAME or VALID padding supported"); |
| 706 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 2, "Only SAME or VALID padding supported"); |
| 707 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != conv_info.pad_left(), "Only SAME or VALID padding supported"); |
| 708 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != conv_info.pad_bottom(), "Only SAME or VALID padding supported"); |
| 709 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != conv_info.pad_left(), "Only SAME or VALID padding supported"); |
| 710 | return validate_kernel_5x5(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info); |
| 711 | } |
| 712 | if(kernel_size == Size2D(3, 1)) |
| 713 | { |
| 714 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 1, "Only SAME or VALID padding supported"); |
| 715 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 1, "Only SAME or VALID padding supported"); |
| 716 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_bottom() != 0, "Only SAME or VALID padding supported"); |
| 717 | return validate_kernel_3x1(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info); |
| 718 | } |
| 719 | else if(kernel_size == Size2D(1, 3)) |
| 720 | { |
| 721 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 1, "Only SAME or VALID padding supported"); |
| 722 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 1, "Only SAME or VALID padding supported"); |
| 723 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_right() != 0, "Only SAME or VALID padding supported"); |
| 724 | return validate_kernel_1x3(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info); |
| 725 | } |
| 726 | else if(kernel_size == Size2D(5, 1)) |
| 727 | { |
| 728 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 2, "Only SAME or VALID padding supported"); |
| 729 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 2, "Only SAME or VALID padding supported"); |
| 730 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_bottom() != 0, "Only SAME or VALID padding supported"); |
| 731 | return validate_kernel_5x1(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info); |
| 732 | } |
| 733 | else if(kernel_size == Size2D(1, 5)) |
| 734 | { |
| 735 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 2, "Only SAME or VALID padding supported"); |
| 736 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 2, "Only SAME or VALID padding supported"); |
| 737 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_right() != 0, "Only SAME or VALID padding supported"); |
| 738 | return validate_kernel_1x5(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info); |
| 739 | } |
| 740 | else if(kernel_size == Size2D(7, 1)) |
| 741 | { |
| 742 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 3, "Only SAME or VALID padding supported"); |
| 743 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 3, "Only SAME or VALID padding supported"); |
| 744 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_bottom() != 0, "Only SAME or VALID padding supported"); |
| 745 | return validate_kernel_7x1(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info); |
| 746 | } |
| 747 | else if(kernel_size == Size2D(1, 7)) |
| 748 | { |
| 749 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 3, "Only SAME or VALID padding supported"); |
| 750 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 3, "Only SAME or VALID padding supported"); |
| 751 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_right() != 0, "Only SAME or VALID padding supported"); |
| 752 | return validate_kernel_1x7(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info); |
| 753 | } |
| 754 | else |
| 755 | { |
| 756 | ARM_COMPUTE_RETURN_ERROR_MSG("Kernel shape not supported"); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | void CpuWinogradConv2d::run(ITensorPack &tensors) |
| 761 | { |
| 762 | prepare(tensors); |
| 763 | |
| 764 | auto a = tensors.get_const_tensor(ACL_SRC_0); |
| 765 | auto c = tensors.get_const_tensor(ACL_SRC_2); |
| 766 | auto d = tensors.get_tensor(ACL_DST); |
| 767 | |
| 768 | CpuAuxTensorHandler input_nhwc(offset_int_vec(PermutedInput), _input_nhwc, tensors, true); |
| 769 | CpuAuxTensorHandler output_nhwc(offset_int_vec(PermutedOutput), _output_nhwc, tensors, true); |
| 770 | CpuAuxTensorHandler input_transformed(offset_int_vec(InputTransformed), _input_transformed, tensors, true); |
| 771 | CpuAuxTensorHandler input_workspace(offset_int_vec(InputWorkspace), _input_workspace, tensors, true); |
| 772 | |
| 773 | const bool is_nchw = _data_layout == DataLayout::NCHW; |
| 774 | if(is_nchw) |
| 775 | { |
| 776 | //Bring channels to the front as Winograd code expects the tensor to be in the format NHWC |
| 777 | ITensorPack pack{ { ACL_SRC, a }, { ACL_DST, input_nhwc.get() } }; |
| 778 | _permute_input->run(pack); |
| 779 | } |
| 780 | |
| 781 | // Transform input tensor to the winograd domain |
| 782 | ITensorPack transform_input_pack{ { ACL_SRC, is_nchw ? input_nhwc.get() : a }, { ACL_DST, input_transformed.get() }, { ACL_INT, input_workspace.get() } }; |
| 783 | NEScheduler::get().schedule_op(_transform_input_kernel.get(), Window::DimX, _transform_input_kernel->window(), transform_input_pack); |
| 784 | |
| 785 | CpuAuxTensorHandler output_transformed(offset_int_vec(OutputTransformed), _output_transformed, tensors, true); |
| 786 | CpuAuxTensorHandler weights_transformed(offset_int_vec(WeightsTransformed), _kernel_storage, tensors, true); |
| 787 | |
| 788 | // Run 16 GEMMs in multiple threads, each kernel runs one or more GEMMs |
| 789 | ITensorPack gemm_pack{ { ACL_SRC, input_transformed.get() }, { ACL_SRC_1, weights_transformed.get() }, { ACL_DST, output_transformed.get() } }; |
| 790 | _gemm_function->run(gemm_pack); |
| 791 | |
| 792 | // Transform output tensor to the spatial domain |
| 793 | CpuAuxTensorHandler output_workspace(offset_int_vec(OutputWorkspace), _output_workspace, tensors, true); |
| 794 | ITensorPack transform_output_pack{ { ACL_SRC_0, c }, { ACL_SRC_1, output_transformed.get() }, { ACL_DST, is_nchw ? output_nhwc.get() : d }, { ACL_INT, output_workspace.get() } }; |
| 795 | NEScheduler::get().schedule_op(_transform_output_kernel.get(), Window::DimX, _transform_output_kernel->window(), transform_output_pack); |
| 796 | |
| 797 | if(is_nchw) |
| 798 | { |
| 799 | // Reorder the convoluted output to ACL's ordering NCHW |
| 800 | ITensorPack pack{ { ACL_SRC, output_nhwc.get() }, { ACL_DST, d } }; |
| 801 | _permute_output->run(pack); |
| 802 | } |
| 803 | |
| 804 | if(_run_activation) |
| 805 | { |
| 806 | ITensorPack pack{ { ACL_SRC, d }, { ACL_DST, d } }; |
| 807 | _activation_func->run(pack); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | void CpuWinogradConv2d::prepare(ITensorPack &tensors) |
| 812 | { |
| 813 | if(!_is_prepared) |
| 814 | { |
| 815 | // Permute weights |
| 816 | const ITensor *weights = tensors.get_const_tensor(ACL_SRC_1); |
| 817 | ITensor *weights_aux = utils::cast::polymorphic_cast<ITensor *>(tensors.get_tensor(offset_int_vec(PermutedWeights))); |
| 818 | ARM_COMPUTE_ERROR_ON_NULLPTR(weights, weights_aux); |
| 819 | |
| 820 | CpuAuxTensorHandler permuted_weights(_weights_hwio, *weights_aux); |
| 821 | ITensorPack permute_tensors{ { ACL_SRC, weights }, { ACL_DST, permuted_weights.get() } }; |
| 822 | _permute_weights->run(permute_tensors); |
| 823 | |
| 824 | // Transform weights |
| 825 | ITensor *weights_transf = utils::cast::polymorphic_cast<ITensor *>(tensors.get_tensor(offset_int_vec(WeightsTransformed))); |
| 826 | ARM_COMPUTE_ERROR_ON_NULLPTR(weights_transf); |
| 827 | |
| 828 | CpuAuxTensorHandler transformed_weights(_kernel_storage, *weights_transf); |
| 829 | ITensorPack transform_tensors{ { ACL_SRC, permuted_weights.get() }, { ACL_DST, transformed_weights.get() } }; |
| 830 | NEScheduler::get().schedule_op(_transform_weights_kernel.get(), Window::DimX, _transform_weights_kernel->window(), transform_tensors); |
| 831 | |
| 832 | CpuAuxTensorHandler input_transformed(offset_int_vec(InputTransformed), _input_transformed, tensors, true); |
| 833 | CpuAuxTensorHandler output_transformed(offset_int_vec(OutputTransformed), _output_transformed, tensors, true); |
| 834 | ITensorPack gemm_pack = tensors; |
| 835 | gemm_pack.add_const_tensor(ACL_SRC_0, input_transformed.get()); |
| 836 | gemm_pack.add_const_tensor(ACL_SRC_1, transformed_weights.get()); |
| 837 | _gemm_function->prepare(gemm_pack); |
| 838 | |
| 839 | _is_prepared = true; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | experimental::MemoryRequirements CpuWinogradConv2d::workspace() const |
| 844 | { |
| 845 | return _aux_mem; |
| 846 | } |
| 847 | } // namespace cpu |
| 848 | } // namespace arm_compute |