blob: badeb07405450d58a4cc6272d384398e7eaea40d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Isabella Gottardie6630e42018-01-18 15:50:39 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 */
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"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010029#include "support/ToolchainSupport.h"
30
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include <cmath>
32#include <tuple>
Georgios Pinitasd8734b52017-12-22 15:27:52 +000033#include <utility>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010035namespace arm_compute
36{
Anthony Barbier61b4fca2018-03-29 11:31:55 +010037NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) //NOLINT
38 : _memory_manager(std::move(memory_manager)),
39 _function()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040{
41}
42
Alex Gilday7da29b62018-03-23 14:16:00 +000043void NEConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000044 const Size2D &dilation, const ActivationLayerInfo &act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045{
Giorgio Arena7c23ad02017-11-30 15:08:38 +000046 // Perform validate step
47 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000048 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));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000050 switch(NEConvolutionLayer::get_convolution_method(input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000051 weights_info, dilation, act_info))
Moritz Pflanzer80373f62017-09-15 10:42:58 +010052 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000053 case ConvolutionMethod::WINOGRAD:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000055 auto f = arm_compute::support::cpp14::make_unique<NEWinogradLayer>(_memory_manager);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000056 f->configure(input, weights, biases, output, conv_info, act_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000057 _function = std::move(f);
58 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000060 case ConvolutionMethod::GEMM:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000062 auto f = arm_compute::support::cpp14::make_unique<NEGEMMConvolutionLayer>(_memory_manager);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000063 f->configure(input, weights, biases, output, conv_info, weights_info, dilation, act_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000064 _function = std::move(f);
65 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000067 case ConvolutionMethod::DIRECT:
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010068 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000069 auto f = arm_compute::support::cpp14::make_unique<NEDirectConvolutionLayer>(_memory_manager);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000070 f->configure(input, weights, biases, output, conv_info, act_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000071 _function = std::move(f);
72 break;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010073 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000074 default:
75 ARM_COMPUTE_ERROR("Not supported.");
76 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078}
79
Giorgio Arena7c23ad02017-11-30 15:08:38 +000080Status NEConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000081 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Giorgio Arena7c23ad02017-11-30 15:08:38 +000082{
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000083 switch(NEConvolutionLayer::get_convolution_method(input, weights, biases, output, conv_info, weights_info, dilation, act_info))
Giorgio Arena7c23ad02017-11-30 15:08:38 +000084 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000085 case ConvolutionMethod::WINOGRAD:
86 //Validate Winograd
87 NEWinogradLayer::validate(input, weights, biases, output, conv_info);
88 break;
89 case ConvolutionMethod::GEMM:
90 //Validate Gemm-based Convolution
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000091 NEGEMMConvolutionLayer::validate(input, weights, biases, output, conv_info, weights_info, dilation, act_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000092 break;
93 case ConvolutionMethod::DIRECT:
94 //Validate Gemm-based Convolution
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000095 NEDirectConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000096 default:
97 ARM_COMPUTE_ERROR("Not supported.");
98 break;
Giorgio Arena7c23ad02017-11-30 15:08:38 +000099 }
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000100
101 return Status{};
102}
103
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000104ConvolutionMethod NEConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000105 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000106{
107 ARM_COMPUTE_UNUSED(output);
108 ARM_COMPUTE_UNUSED(weights_info);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000109 ARM_COMPUTE_UNUSED(act_info);
110
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000111 if((input->data_type() == DataType::F32) && (weights->dimension(0) == 3) && (weights->dimension(1) == 3) && (weights->num_dimensions() <= 4) && (conv_info.stride().first == 1)
Alex Gilday7da29b62018-03-23 14:16:00 +0000112 && (conv_info.stride().second == 1) && (biases != nullptr) && (dilation == Size2D(1U, 1U)))
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000113 {
114 return ConvolutionMethod::WINOGRAD;
115 }
116 return ConvolutionMethod::GEMM;
117}
118
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119void NEConvolutionLayer::run()
120{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000121 _function->run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100123} // namespace arm_compute