Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 "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 Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 29 | #include "support/ToolchainSupport.h" |
| 30 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include <cmath> |
| 32 | #include <tuple> |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame^] | 33 | #include <utility> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 37 | NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
| 38 | : _memory_manager(std::move(memory_manager)), _function() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 42 | void NEConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | { |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 44 | // Perform validate step |
| 45 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayer::validate(input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv_info, weights_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 48 | switch(NEConvolutionLayer::get_convolution_method(input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv_info, |
| 49 | weights_info)) |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 50 | { |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 51 | case ConvolutionMethod::WINOGRAD: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | { |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 53 | auto f = arm_compute::support::cpp14::make_unique<NEWinogradLayer>(_memory_manager); |
| 54 | f->configure(input, weights, biases, output, conv_info); |
| 55 | _function = std::move(f); |
| 56 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 58 | case ConvolutionMethod::GEMM: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | { |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 60 | auto f = arm_compute::support::cpp14::make_unique<NEGEMMConvolutionLayer>(_memory_manager); |
| 61 | f->configure(input, weights, biases, output, conv_info, weights_info); |
| 62 | _function = std::move(f); |
| 63 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 65 | case ConvolutionMethod::DIRECT: |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 66 | { |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 67 | auto f = arm_compute::support::cpp14::make_unique<NEDirectConvolutionLayer>(_memory_manager); |
| 68 | f->configure(input, weights, biases, output, conv_info); |
| 69 | _function = std::move(f); |
| 70 | break; |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 71 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 72 | default: |
| 73 | ARM_COMPUTE_ERROR("Not supported."); |
| 74 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 78 | Status NEConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 79 | const WeightsInfo &weights_info) |
| 80 | { |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 81 | switch(NEConvolutionLayer::get_convolution_method(input, weights, biases, output, conv_info, weights_info)) |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 82 | { |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 83 | case ConvolutionMethod::WINOGRAD: |
| 84 | //Validate Winograd |
| 85 | NEWinogradLayer::validate(input, weights, biases, output, conv_info); |
| 86 | break; |
| 87 | case ConvolutionMethod::GEMM: |
| 88 | //Validate Gemm-based Convolution |
| 89 | NEGEMMConvolutionLayer::validate(input, weights, biases, output, conv_info, weights_info); |
| 90 | break; |
| 91 | case ConvolutionMethod::DIRECT: |
| 92 | //Validate Gemm-based Convolution |
| 93 | NEDirectConvolutionLayer::validate(input, weights, biases, output, conv_info); |
| 94 | default: |
| 95 | ARM_COMPUTE_ERROR("Not supported."); |
| 96 | break; |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 97 | } |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 98 | |
| 99 | return Status{}; |
| 100 | } |
| 101 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 102 | ConvolutionMethod NEConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 103 | const WeightsInfo &weights_info) |
| 104 | { |
| 105 | ARM_COMPUTE_UNUSED(output); |
| 106 | ARM_COMPUTE_UNUSED(weights_info); |
| 107 | if((input->data_type() == DataType::F32) && (weights->dimension(0) == 3) && (weights->dimension(1) == 3) && (weights->num_dimensions() <= 4) && (conv_info.stride().first == 1) |
| 108 | && (conv_info.stride().second == 1) && (biases != nullptr)) |
| 109 | { |
| 110 | return ConvolutionMethod::WINOGRAD; |
| 111 | } |
| 112 | return ConvolutionMethod::GEMM; |
| 113 | } |
| 114 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | void NEConvolutionLayer::run() |
| 116 | { |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 117 | _function->run(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 119 | } // namespace arm_compute |