blob: 5bfc832518ab72ebe4f637a26ca971d19771dbe4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 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 "arm_compute/core/CL/kernels/CLActivationLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
Georgios Pinitas00394ae2017-06-22 18:13:55 +010029#include "arm_compute/core/FixedPoint.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/IAccessWindow.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#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"
39#include "arm_compute/core/Validate.h"
Georgios Pinitas00394ae2017-06-22 18:13:55 +010040#include "support/ToolchainSupport.h"
41
42#include <cmath>
43
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044using namespace arm_compute;
45
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010046CLActivationLayerKernel::CLActivationLayerKernel()
47 : _input(nullptr), _output(nullptr)
48{
49}
50
51void CLActivationLayerKernel::configure(ICLTensor *input, ICLTensor *output, ActivationLayerInfo act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052{
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000053 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010055 if(output != nullptr)
56 {
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000057 ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), output->info());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010058 // Output auto inizialitation if not yet initialized
Georgios Pinitas05078ec2017-11-02 13:06:59 +000059 auto_init_if_empty(*output->info(),
60 input->info()->tensor_shape(),
61 1,
62 input->info()->data_type(),
63 input->info()->fixed_point_position(),
64 input->info()->quantization_info());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010065 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000067 ARM_COMPUTE_ERROR_THROW_ON(CLActivationLayerKernel::validate(input->info(), (output != nullptr) ? output->info() : nullptr, act_info));
68
Georgios Pinitas00394ae2017-06-22 18:13:55 +010069 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
Georgios Pinitas388d3ec2017-11-02 12:17:56 +000070 const DataType dt = input->info()->data_type();
Georgios Pinitas00394ae2017-06-22 18:13:55 +010071 const int fixed_point_position = input->info()->fixed_point_position();
72 float a_const = act_info.a();
73 float b_const = act_info.b();
Georgios Pinitas05078ec2017-11-02 13:06:59 +000074 int a_const_int = 0;
75 int b_const_int = 0;
76
77 // Create quantized version of constants a, b if needed
78 if(is_data_type_quantized(dt))
Georgios Pinitas00394ae2017-06-22 18:13:55 +010079 {
Georgios Pinitas05078ec2017-11-02 13:06:59 +000080 if(is_data_type_fixed_point(dt))
81 {
82 a_const_int = static_cast<int>(lround(a_const * (1 << fixed_point_position)));
83 b_const_int = static_cast<int>(lround(b_const * (1 << fixed_point_position)));
84 }
85 else
86 {
87 a_const_int = input->info()->quantization_info().quantize(a_const);
88 b_const_int = input->info()->quantization_info().quantize(b_const);
89 }
Georgios Pinitas00394ae2017-06-22 18:13:55 +010090 }
91
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092 // Set build options
93 std::set<std::string> build_opts;
Georgios Pinitas00394ae2017-06-22 18:13:55 +010094 build_opts.emplace(("-DACT=" + lower_string(string_from_activation_func(act_info.activation()))));
Georgios Pinitas05078ec2017-11-02 13:06:59 +000095 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)));
Georgios Pinitas00394ae2017-06-22 18:13:55 +010096 build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
Michel Iwaniec00633802017-10-12 14:14:15 +010097
Georgios Pinitas05078ec2017-11-02 13:06:59 +000098 if(is_data_type_quantized(dt))
Michel Iwaniec00633802017-10-12 14:14:15 +010099 {
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000100 build_opts.emplace(("-DA_VAL=" + support::cpp11::to_string(a_const_int)));
101 build_opts.emplace(("-DB_VAL=" + support::cpp11::to_string(b_const_int)));
Michel Iwaniec00633802017-10-12 14:14:15 +0100102
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000103 // Set scale and offset of the input and output
Georgios Pinitas388d3ec2017-11-02 12:17:56 +0000104 if(is_data_type_quantized_assymetric(dt))
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000105 {
106 float s1 = input->info()->quantization_info().scale;
107 int o1 = input->info()->quantization_info().offset;
108 // If output is nullptr, assume same quantization scale/offset as input
109 float s2 = output != nullptr ? output->info()->quantization_info().scale : s1;
110 int o2 = output != nullptr ? output->info()->quantization_info().offset : o1;
111 build_opts.emplace(("-DS1_VAL=" + float_to_string_with_full_precision(s1)));
112 build_opts.emplace(("-DS2_VAL=" + float_to_string_with_full_precision(s2)));
113 build_opts.emplace(("-DO1_VAL=" + support::cpp11::to_string(o1)));
114 build_opts.emplace(("-DO2_VAL=" + support::cpp11::to_string(o2)));
115 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100116 }
117 else
118 {
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000119 build_opts.emplace(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
120 build_opts.emplace(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
Michel Iwaniec00633802017-10-12 14:14:15 +0100121 }
122
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100123 build_opts.emplace(output == nullptr ? "-DIN_PLACE" : "");
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000124 if(is_data_type_fixed_point(dt))
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100125 {
126 build_opts.emplace(("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(fixed_point_position)));
127 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128
129 // Create kernel
Georgios Pinitas388d3ec2017-11-02 12:17:56 +0000130 std::string kernel_name = is_data_type_quantized_assymetric(dt) ? std::string("activation_layer_qa8") : std::string("activation_layer");
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000131 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132
133 // Make sure _kernel is initialized before calling the parent's configure
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100134 _input = input;
135 _output = output;
136
137 // Configure kernel window
138 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
139
140 if(output != nullptr)
141 {
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100142 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100143 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100144 update_window_and_padding(win, input_access, output_access);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100145 output_access.set_valid_region(win, input->info()->valid_region());
146 }
147 else
148 {
149 update_window_and_padding(win,
150 AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration));
151 }
152
153 ICLKernel::configure(win);
154}
155
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000156Error CLActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
157{
158 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::QASYMM8, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
159 ARM_COMPUTE_RETURN_ERROR_ON_MSG((input->data_type() == DataType::QASYMM8) && (act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU),
160 "For QASYMM8 only lower/upper bounded relu is supported");
161
162 // Checks performed when output is configured
163 if((output != nullptr) && (output->total_size() != 0))
164 {
165 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
166 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
167 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
168 }
169
170 return Error{};
171}
172
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100173void CLActivationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
174{
175 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
176 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
177
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100178 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
179 Window slice = collapsed.first_slice_window_3D();
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100180
181 do
182 {
183 unsigned int idx = 0;
184 add_3D_tensor_argument(idx, _input, slice);
185 if(_output != nullptr)
186 {
187 add_3D_tensor_argument(idx, _output, slice);
188 }
189 enqueue(queue, *this, slice);
190 }
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100191 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192}