blob: 18202c1c5b80c0121f89e3425f79b126b1a7c916 [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
Georgios Pinitas00394ae2017-06-22 18:13:55 +010037#include "support/ToolchainSupport.h"
38
39#include <cmath>
40
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041using namespace arm_compute;
42
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010043CLActivationLayerKernel::CLActivationLayerKernel()
44 : _input(nullptr), _output(nullptr)
45{
46}
47
48void CLActivationLayerKernel::configure(ICLTensor *input, ICLTensor *output, ActivationLayerInfo act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049{
Georgios Pinitas00394ae2017-06-22 18:13:55 +010050 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010052 if(output != nullptr)
53 {
54 // Output auto inizialitation if not yet initialized
55 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010057 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
58 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
59 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
60 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061
Georgios Pinitas00394ae2017-06-22 18:13:55 +010062 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
63 const int fixed_point_position = input->info()->fixed_point_position();
64 float a_const = act_info.a();
65 float b_const = act_info.b();
66 if(is_data_type_fixed_point(input->info()->data_type()))
67 {
68 a_const = static_cast<int>(lround(a_const * (1 << fixed_point_position)));
69 b_const = static_cast<int>(lround(b_const * (1 << fixed_point_position)));
70 }
71
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 // Set build options
73 std::set<std::string> build_opts;
Georgios Pinitas00394ae2017-06-22 18:13:55 +010074 build_opts.emplace(("-DACT=" + lower_string(string_from_activation_func(act_info.activation()))));
75 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
76 build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
77 build_opts.emplace(("-DA_VAL=" + support::cpp11::to_string(a_const)));
78 build_opts.emplace(("-DB_VAL=" + support::cpp11::to_string(b_const)));
79 build_opts.emplace(output == nullptr ? "-DIN_PLACE" : "");
80 if(is_data_type_fixed_point(input->info()->data_type()))
81 {
82 build_opts.emplace(("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(fixed_point_position)));
83 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084
85 // Create kernel
86 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("activation_layer", build_opts));
87
88 // Make sure _kernel is initialized before calling the parent's configure
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010089
90 _input = input;
91 _output = output;
92
93 // Configure kernel window
94 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
95
96 if(output != nullptr)
97 {
Georgios Pinitas00394ae2017-06-22 18:13:55 +010098 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010099 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100100 update_window_and_padding(win, input_access, output_access);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100101 output_access.set_valid_region(win, input->info()->valid_region());
102 }
103 else
104 {
105 update_window_and_padding(win,
106 AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration));
107 }
108
109 ICLKernel::configure(win);
110}
111
112void CLActivationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
113{
114 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
115 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
116
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100117 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
118 Window slice = collapsed.first_slice_window_3D();
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100119
120 do
121 {
122 unsigned int idx = 0;
123 add_3D_tensor_argument(idx, _input, slice);
124 if(_output != nullptr)
125 {
126 add_3D_tensor_argument(idx, _output, slice);
127 }
128 enqueue(queue, *this, slice);
129 }
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100130 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131}