Compute Library
 22.05
ClDirectConv2d.cpp
Go to the documentation of this file.
1 /*
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  */
25 
30 
31 #include "src/common/utils/Log.h"
32 
33 namespace arm_compute
34 {
35 namespace opencl
36 {
37 namespace
38 {
39 ITensorPack select_activation_src_dst(ITensorPack &tensors)
40 {
41  ITensorPack pack;
42  pack.add_tensor(TensorType::ACL_SRC, tensors.get_tensor(TensorType::ACL_DST));
43  pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(TensorType::ACL_DST));
44  return pack;
45 }
46 } // namespace
47 
49  const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info)
50 {
52  ARM_COMPUTE_LOG_PARAMS(src, weights, biases, dst, conv_info, act_info);
53 
54  // Configure direct convolution kernel
55  const ActivationLayerInfo conv2d_act_info = (src->data_layout() == DataLayout::NHWC && is_data_type_float(src->data_type())) ? act_info : ActivationLayerInfo();
56  auto k = std::make_unique<kernels::ClDirectConv2dKernel>();
57  k->set_target(CLScheduler::get().target());
58  k->configure(compile_context, src, weights, biases, dst, conv_info, conv2d_act_info);
59  _direct_conv_kernel = std::move(k);
60 
61  // Configure border handler
62  PixelValue zero_value(0.f);
64  {
65  zero_value = PixelValue(0, src->data_type(), src->quantization_info());
66  }
67  auto b = std::make_unique<CLFillBorderKernel>();
68  b->configure(compile_context, src, _direct_conv_kernel->border_size(), BorderMode::CONSTANT, zero_value);
69  _src_border_handler = std::move(b);
70 
71  // Fused activation is currently supported for NHWC and floating point types
72  if(act_info.enabled() && !conv2d_act_info.enabled())
73  {
74  auto a = std::make_unique<kernels::ClActivationKernel>();
75  a->configure(compile_context, dst, dst, act_info);
76  _activation_kernel = std::move(a);
77  }
78 
79  // Tune kernels
80  CLScheduler::get().tune_kernel_static(*_direct_conv_kernel);
81 }
82 
83 Status ClDirectConv2d::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
84  const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info)
85 {
87  if(act_info.enabled())
88  {
90  }
91  return Status{};
92 }
93 
95 {
96  // Run border handler
97  CLScheduler::get().enqueue_op(*_src_border_handler.get(), tensors, false);
98  // Run direct convolution
99  CLScheduler::get().enqueue_op(*_direct_conv_kernel.get(), tensors, false);
100  // Run activation kernel
101  if(_activation_kernel)
102  {
103  auto act_pack = select_activation_src_dst(tensors);
104  CLScheduler::get().enqueue_op(*_activation_kernel.get(), act_pack, false);
105  }
106 }
107 } // namespace opencl
108 } // namespace arm_compute
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration.
bool enabled() const
Check if initialised.
Definition: Types.h:1675
SimpleTensor< float > b
Definition: DFT.cpp:157
static CLScheduler & get()
Access the scheduler singleton.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Status class.
Definition: Error.h:52
Activation Layer Information class.
Definition: Types.h:1625
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
void enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush=true)
Schedule the execution of the passed kernel if possible.
void configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Set the src and dst tensors.
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Static function to check if given info will lead to a valid configuration.
Padding and stride information class.
Definition: Types.h:669
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
CLCompileContext class.
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1052
static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info)
Static function to check if given info will lead to a valid configuration.
Num samples, height, width, channels.
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_LOG_PARAMS(...)
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
void run(ITensorPack &tensors) override
Run the kernels contained in the function.
void tune_kernel_static(ICLKernel &kernel)
Tunes OpenCL kernel.
Definition: CLScheduler.cpp:86
bool is_data_type_float(DataType dt)
Check if a given data type is of floating point type.
Definition: Utils.h:1010
void add_tensor(int id, ITensor *tensor)
Add tensor to the pack.
Definition: ITensorPack.cpp:39
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.