Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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 Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame^] | 29 | #include "arm_compute/core/FixedPoint.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #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 Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame^] | 37 | #include "support/ToolchainSupport.h" |
| 38 | |
| 39 | #include <cmath> |
| 40 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | using namespace arm_compute; |
| 42 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 43 | CLActivationLayerKernel::CLActivationLayerKernel() |
| 44 | : _input(nullptr), _output(nullptr) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | void CLActivationLayerKernel::configure(ICLTensor *input, ICLTensor *output, ActivationLayerInfo act_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | { |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame^] | 50 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 52 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 57 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame^] | 62 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | // Set build options |
| 73 | std::set<std::string> build_opts; |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame^] | 74 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | |
| 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 Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 89 | |
| 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 Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame^] | 98 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 99 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame^] | 100 | update_window_and_padding(win, input_access, output_access); |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 101 | 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 | |
| 112 | void 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 Barbier | be35b0e | 2017-07-11 18:13:08 +0100 | [diff] [blame] | 117 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 118 | Window slice = collapsed.first_slice_window_3D(); |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 119 | |
| 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 Barbier | be35b0e | 2017-07-11 18:13:08 +0100 | [diff] [blame] | 130 | while(collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | } |