Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | a0d1183 | 2018-01-17 16:13:46 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLValidate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 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" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/Window.h" |
| 35 | |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 36 | #include "arm_compute/core/CL/CLHelpers.h" |
| 37 | #include "arm_compute/core/Types.h" |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 38 | #include "support/ToolchainSupport.h" |
| 39 | |
| 40 | #include <cmath> |
| 41 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | using namespace arm_compute; |
| 43 | |
Giorgio Arena | 2995f5b | 2017-11-29 17:33:59 +0000 | [diff] [blame] | 44 | namespace |
| 45 | { |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 46 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info) |
Giorgio Arena | 2995f5b | 2017-11-29 17:33:59 +0000 | [diff] [blame] | 47 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::QASYMM8, DataType::F16, DataType::F32); |
Michele Di Giorgio | a1f7e33 | 2018-01-22 17:26:36 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((input->data_type() == DataType::QASYMM8) && (act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) |
| 51 | && (act_info.activation() != ActivationLayerInfo::ActivationFunction::BOUNDED_RELU) |
Michele Di Giorgio | d304e80 | 2018-07-06 10:17:33 +0100 | [diff] [blame] | 52 | && (act_info.activation() != ActivationLayerInfo::ActivationFunction::RELU) |
| 53 | && (act_info.activation() != ActivationLayerInfo::ActivationFunction::LOGISTIC), |
| 54 | "For QASYMM8 only logistic, relu, lower bounded relu and lower-upper bounded relu are supported"); |
Giorgio Arena | 2995f5b | 2017-11-29 17:33:59 +0000 | [diff] [blame] | 55 | |
| 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); |
Giorgio Arena | 2995f5b | 2017-11-29 17:33:59 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 63 | return Status{}; |
Giorgio Arena | 2995f5b | 2017-11-29 17:33:59 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 66 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
Giorgio Arena | 2995f5b | 2017-11-29 17:33:59 +0000 | [diff] [blame] | 67 | { |
| 68 | if(output != nullptr) |
| 69 | { |
| 70 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 71 | // Output auto inizialitation if not yet initialized |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 72 | auto_init_if_empty(*output, *input); |
Giorgio Arena | 2995f5b | 2017-11-29 17:33:59 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | const unsigned int num_elems_processed_per_iteration = 16 / input->element_size(); |
| 76 | |
| 77 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 78 | bool window_changed = false; |
| 79 | |
| 80 | if(output != nullptr) |
| 81 | { |
| 82 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 83 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 84 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 85 | output_access.set_valid_region(win, input->valid_region()); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | window_changed = update_window_and_padding(win, |
| 90 | AccessWindowHorizontal(input, 0, num_elems_processed_per_iteration)); |
| 91 | } |
| 92 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 93 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Giorgio Arena | 2995f5b | 2017-11-29 17:33:59 +0000 | [diff] [blame] | 94 | return std::make_pair(err, win); |
| 95 | } |
| 96 | } // namespace |
| 97 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 98 | CLActivationLayerKernel::CLActivationLayerKernel() |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 99 | : _input(nullptr), _output(nullptr), _run_in_place(false) |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 100 | { |
| 101 | } |
| 102 | |
| 103 | void CLActivationLayerKernel::configure(ICLTensor *input, ICLTensor *output, ActivationLayerInfo act_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | { |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 105 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 107 | _run_in_place = (output == nullptr) || (output == input); |
| 108 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 109 | if(output != nullptr) |
| 110 | { |
| 111 | // Output auto inizialitation if not yet initialized |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 112 | auto_init_if_empty(*output->info(), |
Giorgio Arena | 2995f5b | 2017-11-29 17:33:59 +0000 | [diff] [blame] | 113 | *input->info()->clone()); |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 114 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | |
Giorgio Arena | f6a43c5 | 2017-12-01 12:16:25 +0000 | [diff] [blame] | 116 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, act_info)); |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 117 | |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 118 | const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size(); |
Georgios Pinitas | 388d3ec | 2017-11-02 12:17:56 +0000 | [diff] [blame] | 119 | const DataType dt = input->info()->data_type(); |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 120 | float a_const = act_info.a(); |
| 121 | float b_const = act_info.b(); |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 122 | int a_const_int = 0; |
| 123 | int b_const_int = 0; |
| 124 | |
| 125 | // Create quantized version of constants a, b if needed |
| 126 | if(is_data_type_quantized(dt)) |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 127 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 128 | a_const_int = input->info()->quantization_info().quantize(a_const, RoundingPolicy::TO_NEAREST_UP); |
| 129 | b_const_int = input->info()->quantization_info().quantize(b_const, RoundingPolicy::TO_NEAREST_UP); |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | // Set build options |
| 133 | std::set<std::string> build_opts; |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 134 | build_opts.emplace(("-DACT=" + lower_string(string_from_activation_func(act_info.activation())))); |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 135 | build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(dt))); |
Giorgio Arena | 7302302 | 2018-09-04 14:55:55 +0100 | [diff] [blame] | 136 | build_opts.emplace(("-DSELECT_DATA_TYPE=" + get_cl_select_type_from_data_type(dt))); |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 137 | build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration))); |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 138 | |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 139 | if(is_data_type_quantized(dt)) |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 140 | { |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 141 | build_opts.emplace(("-DA_VAL=" + support::cpp11::to_string(a_const_int))); |
| 142 | build_opts.emplace(("-DB_VAL=" + support::cpp11::to_string(b_const_int))); |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 143 | |
Michele Di Giorgio | d304e80 | 2018-07-06 10:17:33 +0100 | [diff] [blame] | 144 | const int o1 = input->info()->quantization_info().offset; |
| 145 | const float s1 = input->info()->quantization_info().scale; |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 146 | // Quantized value of 0 corresponds to the offset o1 |
| 147 | build_opts.emplace(("-DCONST_0=" + support::cpp11::to_string(o1))); |
Michele Di Giorgio | d304e80 | 2018-07-06 10:17:33 +0100 | [diff] [blame] | 148 | build_opts.emplace(("-DS1_VAL=" + float_to_string_with_full_precision(s1))); |
| 149 | build_opts.emplace(("-DO1_VAL=" + support::cpp11::to_string(o1))); |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 150 | |
Giorgio Arena | a0d1183 | 2018-01-17 16:13:46 +0000 | [diff] [blame] | 151 | // Set scale and offset of the input and output if they have different quantization info |
| 152 | if(is_data_type_quantized_asymmetric(dt) && output != nullptr) |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 153 | { |
Giorgio Arena | a0d1183 | 2018-01-17 16:13:46 +0000 | [diff] [blame] | 154 | const float s2 = output->info()->quantization_info().scale; |
Giorgio Arena | a0d1183 | 2018-01-17 16:13:46 +0000 | [diff] [blame] | 155 | const int o2 = output->info()->quantization_info().offset; |
| 156 | |
| 157 | if(o1 != o2 || s1 != s2) |
| 158 | { |
Giorgio Arena | a0d1183 | 2018-01-17 16:13:46 +0000 | [diff] [blame] | 159 | build_opts.emplace(("-DS2_VAL=" + float_to_string_with_full_precision(s2))); |
Giorgio Arena | a0d1183 | 2018-01-17 16:13:46 +0000 | [diff] [blame] | 160 | build_opts.emplace(("-DO2_VAL=" + support::cpp11::to_string(o2))); |
| 161 | } |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 162 | } |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 163 | } |
| 164 | else |
| 165 | { |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 166 | build_opts.emplace(("-DA_VAL=" + float_to_string_with_full_precision(a_const))); |
| 167 | build_opts.emplace(("-DB_VAL=" + float_to_string_with_full_precision(b_const))); |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 170 | build_opts.emplace((_run_in_place) ? "-DIN_PLACE" : ""); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | |
| 172 | // Create kernel |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 173 | std::string kernel_name = is_data_type_quantized_asymmetric(dt) ? std::string("activation_layer_qa8") : std::string("activation_layer"); |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 174 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 175 | |
| 176 | // Make sure _kernel is initialized before calling the parent's configure |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 177 | _input = input; |
| 178 | _output = output; |
| 179 | |
| 180 | // Configure kernel window |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 181 | auto win_config = validate_and_configure_window(input->info(), (_run_in_place) ? nullptr : output->info()); |
Giorgio Arena | 2995f5b | 2017-11-29 17:33:59 +0000 | [diff] [blame] | 182 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 183 | ICLKernel::configure_internal(win_config.second); |
Giorgio Arena | 63485ce | 2017-11-15 16:04:20 +0000 | [diff] [blame] | 184 | |
| 185 | // Set config_id for enabling LWS tuning |
| 186 | _config_id = "activation_layer_"; |
| 187 | _config_id += lower_string(string_from_data_type(dt)); |
| 188 | _config_id += "_"; |
| 189 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 190 | _config_id += "_"; |
| 191 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 194 | Status CLActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info) |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 195 | { |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 196 | const bool run_in_place = (output == nullptr) || (output == input); |
Giorgio Arena | 2995f5b | 2017-11-29 17:33:59 +0000 | [diff] [blame] | 197 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info)); |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 198 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (run_in_place) ? nullptr : output->clone().get()).first); |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 199 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 200 | return Status{}; |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 203 | void CLActivationLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 204 | { |
| 205 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 206 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 207 | |
Anthony Barbier | be35b0e | 2017-07-11 18:13:08 +0100 | [diff] [blame] | 208 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 209 | Window slice = collapsed.first_slice_window_3D(); |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 210 | |
| 211 | do |
| 212 | { |
| 213 | unsigned int idx = 0; |
| 214 | add_3D_tensor_argument(idx, _input, slice); |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 215 | if(!_run_in_place) |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 216 | { |
| 217 | add_3D_tensor_argument(idx, _output, slice); |
| 218 | } |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 219 | enqueue(queue, *this, slice, lws_hint()); |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 220 | } |
Anthony Barbier | be35b0e | 2017-07-11 18:13:08 +0100 | [diff] [blame] | 221 | while(collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 222 | } |