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