blob: 0a491589ff63a6429fa2d83272f17c0e0d5015c6 [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>
33
Kaizen8938bd32017-09-28 14:38:23 +010034namespace arm_compute
35{
Kaizen8938bd32017-09-28 14:38:23 +010036NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbier06ea0482018-02-22 15:45:35 +000037 : _memory_manager(std::move(memory_manager)), _function()
Anthony Barbierdbdab852017-06-23 15:42:00 +010038{
39}
40
Anthony Barbier06ea0482018-02-22 15:45:35 +000041void NEConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
Anthony Barbierdbdab852017-06-23 15:42:00 +010042{
Anthony Barbierf45d5a92018-01-24 16:23:15 +000043 // Perform validate step
44 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Anthony Barbier06ea0482018-02-22 15:45:35 +000045 ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayer::validate(input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv_info, weights_info));
Anthony Barbierdbdab852017-06-23 15:42:00 +010046
Anthony Barbier06ea0482018-02-22 15:45:35 +000047 switch(NEConvolutionLayer::get_convolution_method(input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv_info,
48 weights_info))
Kaizen8938bd32017-09-28 14:38:23 +010049 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000050 case ConvolutionMethod::WINOGRAD:
Anthony Barbierdbdab852017-06-23 15:42:00 +010051 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000052 auto f = arm_compute::support::cpp14::make_unique<NEWinogradLayer>(_memory_manager);
53 f->configure(input, weights, biases, output, conv_info);
54 _function = std::move(f);
55 break;
Anthony Barbierdbdab852017-06-23 15:42:00 +010056 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000057 case ConvolutionMethod::GEMM:
Anthony Barbierdbdab852017-06-23 15:42:00 +010058 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000059 auto f = arm_compute::support::cpp14::make_unique<NEGEMMConvolutionLayer>(_memory_manager);
60 f->configure(input, weights, biases, output, conv_info, weights_info);
61 _function = std::move(f);
62 break;
Anthony Barbierdbdab852017-06-23 15:42:00 +010063 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000064 case ConvolutionMethod::DIRECT:
Kaizen8938bd32017-09-28 14:38:23 +010065 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000066 auto f = arm_compute::support::cpp14::make_unique<NEDirectConvolutionLayer>(_memory_manager);
67 f->configure(input, weights, biases, output, conv_info);
68 _function = std::move(f);
69 break;
Kaizen8938bd32017-09-28 14:38:23 +010070 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000071 default:
72 ARM_COMPUTE_ERROR("Not supported.");
73 break;
Anthony Barbierdbdab852017-06-23 15:42:00 +010074 }
Anthony Barbier871448e2017-03-24 14:54:29 +000075}
76
Anthony Barbierf45d5a92018-01-24 16:23:15 +000077Status NEConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
78 const WeightsInfo &weights_info)
79{
Anthony Barbier06ea0482018-02-22 15:45:35 +000080 switch(NEConvolutionLayer::get_convolution_method(input, weights, biases, output, conv_info, weights_info))
Anthony Barbierf45d5a92018-01-24 16:23:15 +000081 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000082 case ConvolutionMethod::WINOGRAD:
83 //Validate Winograd
84 NEWinogradLayer::validate(input, weights, biases, output, conv_info);
85 break;
86 case ConvolutionMethod::GEMM:
87 //Validate Gemm-based Convolution
88 NEGEMMConvolutionLayer::validate(input, weights, biases, output, conv_info, weights_info);
89 break;
90 case ConvolutionMethod::DIRECT:
91 //Validate Gemm-based Convolution
92 NEDirectConvolutionLayer::validate(input, weights, biases, output, conv_info);
93 default:
94 ARM_COMPUTE_ERROR("Not supported.");
95 break;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000096 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +000097
98 return Status{};
99}
100
Anthony Barbier06ea0482018-02-22 15:45:35 +0000101ConvolutionMethod NEConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
102 const WeightsInfo &weights_info)
103{
104 ARM_COMPUTE_UNUSED(output);
105 ARM_COMPUTE_UNUSED(weights_info);
106 if((input->data_type() == DataType::F32) && (weights->dimension(0) == 3) && (weights->dimension(1) == 3) && (weights->num_dimensions() <= 4) && (conv_info.stride().first == 1)
107 && (conv_info.stride().second == 1) && (biases != nullptr))
108 {
109 return ConvolutionMethod::WINOGRAD;
110 }
111 return ConvolutionMethod::GEMM;
112}
113
Anthony Barbier871448e2017-03-24 14:54:29 +0000114void NEConvolutionLayer::run()
115{
Anthony Barbier06ea0482018-02-22 15:45:35 +0000116 _function->run();
Anthony Barbier871448e2017-03-24 14:54:29 +0000117}
Kaizen8938bd32017-09-28 14:38:23 +0100118} // namespace arm_compute