blob: 7053c7e3458b9756f08e249275b8fcf0d8ba6955 [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier871448e2017-03-24 14:54:29 +00003 *
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/NEON/functions/NEConvolutionLayer.h"
25
26#include "arm_compute/core/PixelValue.h"
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
Kaizen8938bd32017-09-28 14:38:23 +010029#include "support/ToolchainSupport.h"
30
Anthony Barbier871448e2017-03-24 14:54:29 +000031#include <cmath>
32#include <tuple>
Jenkinsb3a371b2018-05-23 11:36:53 +010033#include <utility>
Anthony Barbier871448e2017-03-24 14:54:29 +000034
Kaizen8938bd32017-09-28 14:38:23 +010035namespace arm_compute
36{
Jenkinsb3a371b2018-05-23 11:36:53 +010037NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) //NOLINT
38 : _memory_manager(std::move(memory_manager)),
39 _function()
Anthony Barbierdbdab852017-06-23 15:42:00 +010040{
41}
42
Jenkinsb3a371b2018-05-23 11:36:53 +010043void NEConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
44 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math)
Anthony Barbierdbdab852017-06-23 15:42:00 +010045{
Anthony Barbierf45d5a92018-01-24 16:23:15 +000046 // Perform validate step
47 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Jenkinsb3a371b2018-05-23 11:36:53 +010048 ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayer::validate(input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv_info, weights_info, dilation, act_info,
49 enable_fast_math));
Anthony Barbierdbdab852017-06-23 15:42:00 +010050
Jenkinsb3a371b2018-05-23 11:36:53 +010051 switch(NEConvolutionLayer::get_convolution_method(input->info(), weights->info(), output->info(), conv_info, weights_info, dilation, act_info))
Kaizen8938bd32017-09-28 14:38:23 +010052 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000053 case ConvolutionMethod::WINOGRAD:
Anthony Barbierdbdab852017-06-23 15:42:00 +010054 {
Jenkinsb3a371b2018-05-23 11:36:53 +010055 auto f = arm_compute::support::cpp14::make_unique<NEWinogradConvolutionLayer>(_memory_manager);
56 f->configure(input, weights, biases, output, conv_info, act_info, enable_fast_math);
Anthony Barbier06ea0482018-02-22 15:45:35 +000057 _function = std::move(f);
58 break;
Anthony Barbierdbdab852017-06-23 15:42:00 +010059 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000060 case ConvolutionMethod::GEMM:
Anthony Barbierdbdab852017-06-23 15:42:00 +010061 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000062 auto f = arm_compute::support::cpp14::make_unique<NEGEMMConvolutionLayer>(_memory_manager);
Jenkinsb3a371b2018-05-23 11:36:53 +010063 f->configure(input, weights, biases, output, conv_info, weights_info, dilation, act_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +000064 _function = std::move(f);
65 break;
Anthony Barbierdbdab852017-06-23 15:42:00 +010066 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000067 case ConvolutionMethod::DIRECT:
Kaizen8938bd32017-09-28 14:38:23 +010068 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000069 auto f = arm_compute::support::cpp14::make_unique<NEDirectConvolutionLayer>(_memory_manager);
Jenkinsb3a371b2018-05-23 11:36:53 +010070 f->configure(input, weights, biases, output, conv_info, act_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +000071 _function = std::move(f);
72 break;
Kaizen8938bd32017-09-28 14:38:23 +010073 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000074 default:
75 ARM_COMPUTE_ERROR("Not supported.");
76 break;
Anthony Barbierdbdab852017-06-23 15:42:00 +010077 }
Anthony Barbier871448e2017-03-24 14:54:29 +000078}
79
Anthony Barbierf45d5a92018-01-24 16:23:15 +000080Status NEConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkinsb3a371b2018-05-23 11:36:53 +010081 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math)
Anthony Barbierf45d5a92018-01-24 16:23:15 +000082{
Jenkinsb3a371b2018-05-23 11:36:53 +010083 switch(NEConvolutionLayer::get_convolution_method(input, weights, output, conv_info, weights_info, dilation, act_info))
Anthony Barbierf45d5a92018-01-24 16:23:15 +000084 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000085 case ConvolutionMethod::WINOGRAD:
86 //Validate Winograd
Jenkinsb3a371b2018-05-23 11:36:53 +010087 ARM_COMPUTE_RETURN_ON_ERROR(NEWinogradConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info, enable_fast_math));
Anthony Barbier06ea0482018-02-22 15:45:35 +000088 break;
89 case ConvolutionMethod::GEMM:
90 //Validate Gemm-based Convolution
Jenkinsb3a371b2018-05-23 11:36:53 +010091 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMConvolutionLayer::validate(input, weights, biases, output, conv_info, weights_info, dilation, act_info));
Anthony Barbier06ea0482018-02-22 15:45:35 +000092 break;
93 case ConvolutionMethod::DIRECT:
94 //Validate Gemm-based Convolution
Jenkinsb3a371b2018-05-23 11:36:53 +010095 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info));
Anthony Barbier06ea0482018-02-22 15:45:35 +000096 default:
97 ARM_COMPUTE_ERROR("Not supported.");
98 break;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000099 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000100
101 return Status{};
102}
103
Jenkinsb3a371b2018-05-23 11:36:53 +0100104ConvolutionMethod NEConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights,
105 const ITensorInfo *output, const PadStrideInfo &conv_info,
106 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000107{
Jenkinsb3a371b2018-05-23 11:36:53 +0100108 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, weights);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000109 ARM_COMPUTE_UNUSED(weights_info);
Jenkinsb3a371b2018-05-23 11:36:53 +0100110
111 if(dilation != Size2D(1U, 1U) || Scheduler::get().cpu_info().get_cpu_model() == CPUModel::A53
112 || input->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL)) <= 16)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000113 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100114 return ConvolutionMethod::GEMM;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000115 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100116
117 return bool(NEWinogradConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info, enable_fast_math)) ? ConvolutionMethod::WINOGRAD : ConvolutionMethod::GEMM;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000118}
119
Anthony Barbier871448e2017-03-24 14:54:29 +0000120void NEConvolutionLayer::run()
121{
Anthony Barbier06ea0482018-02-22 15:45:35 +0000122 _function->run();
Anthony Barbier871448e2017-03-24 14:54:29 +0000123}
Kaizen8938bd32017-09-28 14:38:23 +0100124} // namespace arm_compute