blob: 3d8824aa2a9b93c71195b74d5ccef0389d172df4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arenaa0d11832018-01-17 16:13:46 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/core/CL/kernels/CLActivationLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010028#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/CL/ICLTensor.h"
Georgios Pinitas00394ae2017-06-22 18:13:55 +010030#include "arm_compute/core/FixedPoint.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/IAccessWindow.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include "arm_compute/core/Window.h"
36
Michel Iwaniec00633802017-10-12 14:14:15 +010037#include "arm_compute/core/CL/CLHelpers.h"
38#include "arm_compute/core/Types.h"
Georgios Pinitas00394ae2017-06-22 18:13:55 +010039#include "support/ToolchainSupport.h"
40
41#include <cmath>
42
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043using namespace arm_compute;
44
Giorgio Arena2995f5b2017-11-29 17:33:59 +000045namespace
46{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000047Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Giorgio Arena2995f5b2017-11-29 17:33:59 +000048{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010049 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Giorgio Arena2995f5b2017-11-29 17:33:59 +000050 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::QASYMM8, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +000051 ARM_COMPUTE_RETURN_ERROR_ON_MSG((input->data_type() == DataType::QASYMM8) && (act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
52 && (act_info.activation() != ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
53 && (act_info.activation() != ActivationLayerInfo::ActivationFunction::RELU),
54 "For QASYMM8 only relu, lower bounded relu and lower-upper bounded relu are supported");
Giorgio Arena2995f5b2017-11-29 17:33:59 +000055
56 // Checks performed when output is configured
57 if((output != nullptr) && (output->total_size() != 0))
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
62 }
63
Georgios Pinitas631c41a2017-12-06 11:53:03 +000064 return Status{};
Giorgio Arena2995f5b2017-11-29 17:33:59 +000065}
66
Georgios Pinitas631c41a2017-12-06 11:53:03 +000067std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
Giorgio Arena2995f5b2017-11-29 17:33:59 +000068{
69 if(output != nullptr)
70 {
71 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
72 // Output auto inizialitation if not yet initialized
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010073 auto_init_if_empty(*output, *input);
Giorgio Arena2995f5b2017-11-29 17:33:59 +000074 }
75
76 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
77
78 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
79 bool window_changed = false;
80
81 if(output != nullptr)
82 {
83 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
84 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
85 window_changed = update_window_and_padding(win, input_access, output_access);
86 output_access.set_valid_region(win, input->valid_region());
87 }
88 else
89 {
90 window_changed = update_window_and_padding(win,
91 AccessWindowHorizontal(input, 0, num_elems_processed_per_iteration));
92 }
93
Georgios Pinitas631c41a2017-12-06 11:53:03 +000094 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena2995f5b2017-11-29 17:33:59 +000095 return std::make_pair(err, win);
96}
97} // namespace
98
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010099CLActivationLayerKernel::CLActivationLayerKernel()
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000100 : _input(nullptr), _output(nullptr), _run_in_place(false)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100101{
102}
103
104void CLActivationLayerKernel::configure(ICLTensor *input, ICLTensor *output, ActivationLayerInfo act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105{
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000106 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000108 _run_in_place = (output == nullptr) || (output == input);
109
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100110 if(output != nullptr)
111 {
112 // Output auto inizialitation if not yet initialized
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000113 auto_init_if_empty(*output->info(),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000114 *input->info()->clone());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100115 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000117 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, act_info));
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000118
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100119 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
Georgios Pinitas388d3ec2017-11-02 12:17:56 +0000120 const DataType dt = input->info()->data_type();
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100121 const int fixed_point_position = input->info()->fixed_point_position();
122 float a_const = act_info.a();
123 float b_const = act_info.b();
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000124 int a_const_int = 0;
125 int b_const_int = 0;
126
127 // Create quantized version of constants a, b if needed
128 if(is_data_type_quantized(dt))
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100129 {
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000130 if(is_data_type_fixed_point(dt))
131 {
132 a_const_int = static_cast<int>(lround(a_const * (1 << fixed_point_position)));
133 b_const_int = static_cast<int>(lround(b_const * (1 << fixed_point_position)));
134 }
135 else
136 {
Jaroslaw Rzepecki0a878ae2017-11-22 17:16:39 +0000137 a_const_int = input->info()->quantization_info().quantize(a_const, RoundingPolicy::TO_NEAREST_UP);
138 b_const_int = input->info()->quantization_info().quantize(b_const, RoundingPolicy::TO_NEAREST_UP);
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000139 }
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100140 }
141
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142 // Set build options
143 std::set<std::string> build_opts;
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100144 build_opts.emplace(("-DACT=" + lower_string(string_from_activation_func(act_info.activation()))));
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000145 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)));
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100146 build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
Michel Iwaniec00633802017-10-12 14:14:15 +0100147
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000148 if(is_data_type_quantized(dt))
Michel Iwaniec00633802017-10-12 14:14:15 +0100149 {
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000150 build_opts.emplace(("-DA_VAL=" + support::cpp11::to_string(a_const_int)));
151 build_opts.emplace(("-DB_VAL=" + support::cpp11::to_string(b_const_int)));
Michel Iwaniec00633802017-10-12 14:14:15 +0100152
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000153 const int o1 = input->info()->quantization_info().offset;
154 // Quantized value of 0 corresponds to the offset o1
155 build_opts.emplace(("-DCONST_0=" + support::cpp11::to_string(o1)));
156
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000157 // Set scale and offset of the input and output if they have different quantization info
158 if(is_data_type_quantized_asymmetric(dt) && output != nullptr)
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000159 {
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000160 const float s1 = input->info()->quantization_info().scale;
161 const float s2 = output->info()->quantization_info().scale;
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000162 const int o2 = output->info()->quantization_info().offset;
163
164 if(o1 != o2 || s1 != s2)
165 {
166 build_opts.emplace(("-DS1_VAL=" + float_to_string_with_full_precision(s1)));
167 build_opts.emplace(("-DS2_VAL=" + float_to_string_with_full_precision(s2)));
168 build_opts.emplace(("-DO1_VAL=" + support::cpp11::to_string(o1)));
169 build_opts.emplace(("-DO2_VAL=" + support::cpp11::to_string(o2)));
170 }
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000171 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100172 }
173 else
174 {
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000175 build_opts.emplace(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
176 build_opts.emplace(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
Michel Iwaniec00633802017-10-12 14:14:15 +0100177 }
178
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000179 build_opts.emplace((_run_in_place) ? "-DIN_PLACE" : "");
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000180 if(is_data_type_fixed_point(dt))
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100181 {
182 build_opts.emplace(("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(fixed_point_position)));
183 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184
185 // Create kernel
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000186 std::string kernel_name = is_data_type_quantized_asymmetric(dt) ? std::string("activation_layer_qa8") : std::string("activation_layer");
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000187 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188
189 // Make sure _kernel is initialized before calling the parent's configure
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100190 _input = input;
191 _output = output;
192
193 // Configure kernel window
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000194 auto win_config = validate_and_configure_window(input->info(), (_run_in_place) ? nullptr : output->info());
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000195 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
196 ICLKernel::configure(win_config.second);
Giorgio Arena63485ce2017-11-15 16:04:20 +0000197
198 // Set config_id for enabling LWS tuning
199 _config_id = "activation_layer_";
200 _config_id += lower_string(string_from_data_type(dt));
201 _config_id += "_";
202 _config_id += support::cpp11::to_string(input->info()->dimension(0));
203 _config_id += "_";
204 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100205}
206
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000207Status CLActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000208{
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000209 const bool run_in_place = (output == nullptr) || (output == input);
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000210 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info));
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000211 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (run_in_place) ? nullptr : output->clone().get()).first);
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000212
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000213 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000214}
215
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100216void CLActivationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
217{
218 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
219 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
220
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100221 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
222 Window slice = collapsed.first_slice_window_3D();
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100223
224 do
225 {
226 unsigned int idx = 0;
227 add_3D_tensor_argument(idx, _input, slice);
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000228 if(!_run_in_place)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100229 {
230 add_3D_tensor_argument(idx, _output, slice);
231 }
Gian Marcod7779da2017-11-22 14:46:28 +0000232 enqueue(queue, *this, slice, _lws_hint);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100233 }
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100234 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100235}