Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 1 | /* |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [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/CL/functions/CLConvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/PixelValue.h" |
| 27 | #include "arm_compute/core/Utils.h" |
| 28 | #include "arm_compute/core/Validate.h" |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 31 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 32 | |
| 33 | #include <cmath> |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 34 | #include <memory> |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 35 | #include <tuple> |
| 36 | |
| 37 | using namespace arm_compute; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 38 | using namespace arm_compute::misc::shape_calculator; |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 39 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 40 | CLConvolutionLayer::CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 41 | : _memory_manager(std::move(memory_manager)), _function() |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 45 | void CLConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 46 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 47 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
| 48 | ARM_COMPUTE_ERROR_THROW_ON(CLConvolutionLayer::validate(input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv_info, weights_info)); |
| 49 | |
| 50 | switch(CLConvolutionLayer::get_convolution_method(input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv_info, |
| 51 | weights_info, CLScheduler::get().target())) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 52 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 53 | case ConvolutionMethod::DIRECT: |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 54 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 55 | auto f = arm_compute::support::cpp14::make_unique<CLDirectConvolutionLayer>(); |
| 56 | f->configure(input, weights, biases, output, conv_info); |
| 57 | _function = std::move(f); |
| 58 | break; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 59 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 60 | case ConvolutionMethod::GEMM: |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 61 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 62 | auto f = arm_compute::support::cpp14::make_unique<CLGEMMConvolutionLayer>(_memory_manager); |
| 63 | f->configure(input, weights, biases, output, conv_info, weights_info); |
| 64 | _function = std::move(f); |
| 65 | break; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 66 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 67 | default: |
| 68 | ARM_COMPUTE_ERROR("Not supported."); |
| 69 | break; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 73 | Status CLConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 74 | const WeightsInfo &weights_info) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 75 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 76 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 77 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 78 | //Configure if the parameters match the direct convolution or the gemm-based |
| 79 | const GPUTarget gpu_target = CLScheduler::get().target(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 80 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 81 | switch(CLConvolutionLayer::get_convolution_method(input, weights, biases, output, conv_info, weights_info, gpu_target)) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 82 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 83 | case ConvolutionMethod::DIRECT: |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 84 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 85 | // Validate direct convolution layer |
| 86 | CLDirectConvolutionLayer::validate(input, weights, biases, output, conv_info); |
| 87 | break; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 88 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 89 | case ConvolutionMethod::GEMM: |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 90 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 91 | // Validate gemm-based convolution layer |
| 92 | CLGEMMConvolutionLayer::validate(input, weights, biases, output, conv_info, weights_info); |
| 93 | break; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 94 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 95 | default: |
| 96 | ARM_COMPUTE_ERROR("Not supported."); |
| 97 | break; |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 100 | return Status{}; |
| 101 | } |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 102 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 103 | ConvolutionMethod CLConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 104 | const WeightsInfo &weights_info, const GPUTarget gpu_target) |
| 105 | { |
| 106 | ARM_COMPUTE_UNUSED(input); |
| 107 | ARM_COMPUTE_UNUSED(weights); |
| 108 | ARM_COMPUTE_UNUSED(biases); |
| 109 | ARM_COMPUTE_UNUSED(output); |
| 110 | ARM_COMPUTE_UNUSED(conv_info); |
| 111 | ARM_COMPUTE_UNUSED(weights_info); |
| 112 | ARM_COMPUTE_UNUSED(gpu_target); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 113 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 114 | return ConvolutionMethod::GEMM; |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void CLConvolutionLayer::run() |
| 118 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame^] | 119 | _function->run(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 120 | } |