blob: 42f577cb7d8473da3c5b2ebecc7fc78868cab3b1 [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{
Michel Iwaniec00633802017-10-12 14:14:15 +010053 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32, DataType::QASYMM8);
Georgios Pinitas05078ec2017-11-02 13:06:59 +000054 ARM_COMPUTE_ERROR_ON_MSG((input->info()->data_type() == DataType::QASYMM8) && (act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU),
55 "For QASYMM8 only lower/upper bounded relu is supported");
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010057 if(output != nullptr)
58 {
59 // Output auto inizialitation if not yet initialized
Georgios Pinitas05078ec2017-11-02 13:06:59 +000060 auto_init_if_empty(*output->info(),
61 input->info()->tensor_shape(),
62 1,
63 input->info()->data_type(),
64 input->info()->fixed_point_position(),
65 input->info()->quantization_info());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010067 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
68 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
69 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
70 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071
Georgios Pinitas00394ae2017-06-22 18:13:55 +010072 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
Georgios Pinitas05078ec2017-11-02 13:06:59 +000073 DataType dt = input->info()->data_type();
Georgios Pinitas00394ae2017-06-22 18:13:55 +010074 const int fixed_point_position = input->info()->fixed_point_position();
75 float a_const = act_info.a();
76 float b_const = act_info.b();
Georgios Pinitas05078ec2017-11-02 13:06:59 +000077 int a_const_int = 0;
78 int b_const_int = 0;
79
80 // Create quantized version of constants a, b if needed
81 if(is_data_type_quantized(dt))
Georgios Pinitas00394ae2017-06-22 18:13:55 +010082 {
Georgios Pinitas05078ec2017-11-02 13:06:59 +000083 if(is_data_type_fixed_point(dt))
84 {
85 a_const_int = static_cast<int>(lround(a_const * (1 << fixed_point_position)));
86 b_const_int = static_cast<int>(lround(b_const * (1 << fixed_point_position)));
87 }
88 else
89 {
90 a_const_int = input->info()->quantization_info().quantize(a_const);
91 b_const_int = input->info()->quantization_info().quantize(b_const);
92 }
Georgios Pinitas00394ae2017-06-22 18:13:55 +010093 }
94
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 // Set build options
96 std::set<std::string> build_opts;
Georgios Pinitas00394ae2017-06-22 18:13:55 +010097 build_opts.emplace(("-DACT=" + lower_string(string_from_activation_func(act_info.activation()))));
Georgios Pinitas05078ec2017-11-02 13:06:59 +000098 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)));
Georgios Pinitas00394ae2017-06-22 18:13:55 +010099 build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
Michel Iwaniec00633802017-10-12 14:14:15 +0100100
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000101 if(is_data_type_quantized(dt))
Michel Iwaniec00633802017-10-12 14:14:15 +0100102 {
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000103 build_opts.emplace(("-DA_VAL=" + support::cpp11::to_string(a_const_int)));
104 build_opts.emplace(("-DB_VAL=" + support::cpp11::to_string(b_const_int)));
Michel Iwaniec00633802017-10-12 14:14:15 +0100105
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000106 // Set scale and offset of the input and output
107 if(is_data_type_assymetric(dt))
108 {
109 float s1 = input->info()->quantization_info().scale;
110 int o1 = input->info()->quantization_info().offset;
111 // If output is nullptr, assume same quantization scale/offset as input
112 float s2 = output != nullptr ? output->info()->quantization_info().scale : s1;
113 int o2 = output != nullptr ? output->info()->quantization_info().offset : o1;
114 build_opts.emplace(("-DS1_VAL=" + float_to_string_with_full_precision(s1)));
115 build_opts.emplace(("-DS2_VAL=" + float_to_string_with_full_precision(s2)));
116 build_opts.emplace(("-DO1_VAL=" + support::cpp11::to_string(o1)));
117 build_opts.emplace(("-DO2_VAL=" + support::cpp11::to_string(o2)));
118 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100119 }
120 else
121 {
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000122 build_opts.emplace(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
123 build_opts.emplace(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
Michel Iwaniec00633802017-10-12 14:14:15 +0100124 }
125
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100126 build_opts.emplace(output == nullptr ? "-DIN_PLACE" : "");
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000127 if(is_data_type_fixed_point(dt))
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100128 {
129 build_opts.emplace(("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(fixed_point_position)));
130 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131
132 // Create kernel
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000133 std::string kernel_name = is_data_type_assymetric(dt) ? std::string("activation_layer_qa8") : std::string("activation_layer");
134 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135
136 // Make sure _kernel is initialized before calling the parent's configure
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100137 _input = input;
138 _output = output;
139
140 // Configure kernel window
141 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
142
143 if(output != nullptr)
144 {
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100145 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100146 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100147 update_window_and_padding(win, input_access, output_access);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100148 output_access.set_valid_region(win, input->info()->valid_region());
149 }
150 else
151 {
152 update_window_and_padding(win,
153 AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration));
154 }
155
156 ICLKernel::configure(win);
157}
158
159void CLActivationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
160{
161 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
162 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
163
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100164 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
165 Window slice = collapsed.first_slice_window_3D();
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100166
167 do
168 {
169 unsigned int idx = 0;
170 add_3D_tensor_argument(idx, _input, slice);
171 if(_output != nullptr)
172 {
173 add_3D_tensor_argument(idx, _output, slice);
174 }
175 enqueue(queue, *this, slice);
176 }
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100177 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178}