blob: 3382a6c3c52c825f571eaddb5cbe2346e9fa7104 [file] [log] [blame]
Sheri Zhang1efed922021-03-10 22:43:38 +00001/*
2 * Copyright (c) 2021 Arm Limited.
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 "src/runtime/gpu/cl/operators/ClDirectConvolution.h"
25
26#include "arm_compute/runtime/CL/CLScheduler.h"
27#include "src/core/CL/kernels/CLFillBorderKernel.h"
28#include "src/core/gpu/cl/ClCompileContext.h"
29#include "src/core/gpu/cl/kernels/ClActivationKernel.h"
30#include "src/core/gpu/cl/kernels/ClDirectConvolutionKernel.h"
31
32namespace arm_compute
33{
34namespace opencl
35{
36namespace
37{
38ITensorPack select_activation_src_dst(ITensorPack &tensors)
39{
40 ITensorPack pack;
41 pack.add_tensor(TensorType::ACL_SRC, tensors.get_tensor(TensorType::ACL_DST));
42 pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(TensorType::ACL_DST));
43 return pack;
44}
45} // namespace
46
47void ClDirectConvolution::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst,
48 const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info)
49{
50 // Configure direct convolution kernel
51 auto k = std::make_unique<kernels::ClDirectConvolutionKernel>();
52 k->set_target(CLScheduler::get().target());
53 k->configure(compile_context, src, weights, biases, dst, conv_info);
54 _direct_conv_kernel = std::move(k);
55
56 // Configure border handler
57 PixelValue zero_value(0.f);
58 if(is_data_type_quantized_asymmetric(src->data_type()))
59 {
60 zero_value = PixelValue(0, src->data_type(), src->quantization_info());
61 }
62 auto b = std::make_unique<CLFillBorderKernel>();
63 b->configure(compile_context, src, _direct_conv_kernel->border_size(), BorderMode::CONSTANT, zero_value);
64 _src_border_handler = std::move(b);
65
66 if(act_info.enabled())
67 {
68 auto a = std::make_unique<kernels::ClActivationKernel>();
69 a->configure(compile_context, dst, dst, act_info);
70 _activation_kernel = std::move(a);
71 }
72
73 // Tune kernels
74 CLScheduler::get().tune_kernel_static(*_direct_conv_kernel);
75}
76
77Status ClDirectConvolution::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
78 const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info)
79{
80 ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClDirectConvolutionKernel::validate(src, weights, biases, dst, conv_info, CLScheduler::get().target()));
81 if(act_info.enabled())
82 {
83 ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClActivationKernel::validate(dst, dst, act_info));
84 }
85 return Status{};
86}
87
88void ClDirectConvolution::run(ITensorPack &tensors)
89{
90 // Run border handler
91 CLScheduler::get().enqueue_op(*_src_border_handler.get(), tensors, false);
92 // Run direct convolution
93 CLScheduler::get().enqueue_op(*_direct_conv_kernel.get(), tensors, false);
94 // Run activation kernel
95 if(_activation_kernel)
96 {
97 auto act_pack = select_activation_src_dst(tensors);
98 CLScheduler::get().enqueue_op(*_activation_kernel.get(), act_pack, false);
99 }
100}
101} // namespace opencl
102} // namespace arm_compute