blob: 1a486ce5c7b96683664cb64aa426a60bc3e1af3b [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
Anthony Barbierf45d5a92018-01-24 16:23:15 +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/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 Barbier06ea0482018-02-22 15:45:35 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000031#include "arm_compute/runtime/CL/CLScheduler.h"
32
33#include <cmath>
Kaizen8938bd32017-09-28 14:38:23 +010034#include <memory>
Anthony Barbier871448e2017-03-24 14:54:29 +000035#include <tuple>
36
37using namespace arm_compute;
Anthony Barbier06ea0482018-02-22 15:45:35 +000038using namespace arm_compute::misc::shape_calculator;
Anthony Barbierdbdab852017-06-23 15:42:00 +010039
Kaizen8938bd32017-09-28 14:38:23 +010040CLConvolutionLayer::CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbier06ea0482018-02-22 15:45:35 +000041 : _memory_manager(std::move(memory_manager)), _function()
Anthony Barbierdbdab852017-06-23 15:42:00 +010042{
43}
44
Anthony Barbier06ea0482018-02-22 15:45:35 +000045void CLConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000046{
Anthony Barbier06ea0482018-02-22 15:45:35 +000047 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 Barbier8140e1e2017-12-14 23:48:46 +000052 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000053 case ConvolutionMethod::DIRECT:
Anthony Barbierf45d5a92018-01-24 16:23:15 +000054 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000055 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 Barbierf45d5a92018-01-24 16:23:15 +000059 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000060 case ConvolutionMethod::GEMM:
Anthony Barbierf45d5a92018-01-24 16:23:15 +000061 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000062 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 Barbierf45d5a92018-01-24 16:23:15 +000066 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000067 default:
68 ARM_COMPUTE_ERROR("Not supported.");
69 break;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000070 }
71}
72
Anthony Barbier06ea0482018-02-22 15:45:35 +000073Status CLConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
74 const WeightsInfo &weights_info)
Anthony Barbier871448e2017-03-24 14:54:29 +000075{
Anthony Barbier06ea0482018-02-22 15:45:35 +000076 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000077
Anthony Barbier06ea0482018-02-22 15:45:35 +000078 //Configure if the parameters match the direct convolution or the gemm-based
79 const GPUTarget gpu_target = CLScheduler::get().target();
Anthony Barbier871448e2017-03-24 14:54:29 +000080
Anthony Barbier06ea0482018-02-22 15:45:35 +000081 switch(CLConvolutionLayer::get_convolution_method(input, weights, biases, output, conv_info, weights_info, gpu_target))
Anthony Barbier871448e2017-03-24 14:54:29 +000082 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000083 case ConvolutionMethod::DIRECT:
Anthony Barbier8140e1e2017-12-14 23:48:46 +000084 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000085 // Validate direct convolution layer
86 CLDirectConvolutionLayer::validate(input, weights, biases, output, conv_info);
87 break;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000088 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000089 case ConvolutionMethod::GEMM:
Anthony Barbier8140e1e2017-12-14 23:48:46 +000090 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000091 // Validate gemm-based convolution layer
92 CLGEMMConvolutionLayer::validate(input, weights, biases, output, conv_info, weights_info);
93 break;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000094 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000095 default:
96 ARM_COMPUTE_ERROR("Not supported.");
97 break;
Anthony Barbier871448e2017-03-24 14:54:29 +000098 }
99
Anthony Barbier06ea0482018-02-22 15:45:35 +0000100 return Status{};
101}
Kaizen8938bd32017-09-28 14:38:23 +0100102
Anthony Barbier06ea0482018-02-22 15:45:35 +0000103ConvolutionMethod 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);
Kaizen8938bd32017-09-28 14:38:23 +0100113
Anthony Barbier06ea0482018-02-22 15:45:35 +0000114 return ConvolutionMethod::GEMM;
Anthony Barbier871448e2017-03-24 14:54:29 +0000115}
116
117void CLConvolutionLayer::run()
118{
Anthony Barbier06ea0482018-02-22 15:45:35 +0000119 _function->run();
Anthony Barbier871448e2017-03-24 14:54:29 +0000120}