Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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/CLNormalizationLayerKernel.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" |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +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/TensorInfo.h" |
| 32 | #include "arm_compute/core/Utils.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
| 36 | using namespace arm_compute; |
| 37 | |
Giorgio Arena | c23b633 | 2017-11-28 15:23:51 +0000 | [diff] [blame] | 38 | namespace |
| 39 | { |
| 40 | Error validate_arguments(const ITensorInfo *input, const ITensorInfo *output, NormalizationLayerInfo norm_info) |
| 41 | { |
| 42 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 44 | |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd"); |
Giorgio Arena | c23b633 | 2017-11-28 15:23:51 +0000 | [diff] [blame] | 46 | |
| 47 | if(is_data_type_fixed_point(input->data_type())) |
| 48 | { |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.beta(), input); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.kappa(), input); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.scale_coeff(), input); |
| 52 | } |
| 53 | |
| 54 | // Checks performed when output is configured |
| 55 | if(output->total_size() != 0) |
| 56 | { |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 60 | } |
| 61 | |
| 62 | return Error{}; |
| 63 | } |
| 64 | |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame^] | 65 | std::pair<Error, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, NormalizationLayerInfo norm_info) |
Giorgio Arena | c23b633 | 2017-11-28 15:23:51 +0000 | [diff] [blame] | 66 | { |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame^] | 67 | const unsigned int norm_size = norm_info.norm_size(); |
| 68 | bool is_in_map = norm_info.is_in_map(); |
| 69 | |
Giorgio Arena | c23b633 | 2017-11-28 15:23:51 +0000 | [diff] [blame] | 70 | const unsigned int border_width = is_in_map ? std::min(norm_size / 2, 3U) : 0; |
| 71 | const BorderSize border_size = BorderSize(0, border_width); |
| 72 | |
| 73 | const unsigned int num_elems_processed_per_iteration = (is_data_type_fixed_point(input->data_type())) ? 16 : 4; |
| 74 | const unsigned int num_elems_read_per_iteration = is_in_map ? (num_elems_processed_per_iteration + 2 * (norm_size / 2)) : num_elems_processed_per_iteration; |
| 75 | |
| 76 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 77 | |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame^] | 78 | // We do not use a Rectangle window for IN_MAP_2D as we clamp the top and bottom accesses inside the kernel, avoiding padding |
Giorgio Arena | c23b633 | 2017-11-28 15:23:51 +0000 | [diff] [blame] | 79 | AccessWindowHorizontal input_access(input, -border_size.left, num_elems_read_per_iteration); |
| 80 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 81 | |
| 82 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 83 | |
| 84 | output_access.set_valid_region(win, input->valid_region()); |
| 85 | |
| 86 | Error err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Error{}; |
| 87 | return std::make_pair(err, win); |
| 88 | } |
| 89 | } // namespace |
| 90 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | CLNormalizationLayerKernel::CLNormalizationLayerKernel() |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 92 | : _input(nullptr), _output(nullptr), _border_size(0), _is_in_map(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | { |
| 94 | } |
| 95 | |
| 96 | BorderSize CLNormalizationLayerKernel::border_size() const |
| 97 | { |
| 98 | return _border_size; |
| 99 | } |
| 100 | |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 101 | void CLNormalizationLayerKernel::configure(const ICLTensor *input, ICLTensor *output, NormalizationLayerInfo norm_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | { |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 103 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 09004ca | 2017-07-03 17:30:14 +0100 | [diff] [blame] | 104 | |
| 105 | // Output tensor auto initialization if not yet initialized |
| 106 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 107 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 108 | // Perform validation step |
| 109 | ARM_COMPUTE_ERROR_THROW_ON(CLNormalizationLayerKernel::validate(input->info(), output->info(), norm_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 111 | _input = input; |
| 112 | _output = output; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 114 | _is_in_map = norm_info.is_in_map(); |
steniu01 | f70256b | 2017-07-13 14:03:35 +0100 | [diff] [blame] | 115 | const unsigned int border_width = _is_in_map ? std::min(norm_info.norm_size() / 2, 3U) : 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | _border_size = BorderSize(0, border_width); |
| 117 | |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 118 | const unsigned int num_elems_processed_per_iteration = (is_data_type_fixed_point(input->info()->data_type())) ? 16 : 4; |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame^] | 119 | const bool is_in_map_2D = (norm_info.type() == NormType::IN_MAP_2D); |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 120 | |
| 121 | // Set build options |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame^] | 122 | CLBuildOptions build_opts; |
| 123 | build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); |
| 124 | build_opts.add_option_if(is_data_type_fixed_point(input->info()->data_type()), |
| 125 | "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
| 126 | build_opts.add_option(("-DCOEFF=" + float_to_string_with_full_precision(norm_info.scale_coeff()))); |
| 127 | build_opts.add_option(("-DBETA=" + float_to_string_with_full_precision(norm_info.beta()))); |
| 128 | build_opts.add_option(("-DKAPPA=" + float_to_string_with_full_precision(norm_info.kappa()))); |
| 129 | build_opts.add_option(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration))); |
| 130 | build_opts.add_option(("-DRADIUS=" + support::cpp11::to_string(norm_info.norm_size() / 2))); |
| 131 | build_opts.add_option(("-DNUM_SLICES=" + support::cpp11::to_string(input->info()->dimension(2)))); |
| 132 | build_opts.add_option_if(is_in_map_2D, "-DIN_MAP_2D"); |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 133 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | // Create kernel |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame^] | 135 | std::string kernel_name = _is_in_map ? "normalization_layer_in_map" : "normalization_layer_cross_map"; |
| 136 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | // Configure kernel window |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame^] | 139 | auto win_config = validate_and_configure_window(input->info(), output->info(), norm_info); |
Giorgio Arena | c23b633 | 2017-11-28 15:23:51 +0000 | [diff] [blame] | 140 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 141 | ICLKernel::configure(win_config.second); |
Giorgio Arena | 63485ce | 2017-11-15 16:04:20 +0000 | [diff] [blame] | 142 | |
| 143 | // Set config_id for enabling LWS tuning |
| 144 | _config_id = "normalization_layer_"; |
| 145 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 146 | _config_id += "_"; |
| 147 | _config_id += support::cpp11::to_string(static_cast<std::underlying_type<NormType>::type>(norm_info.type())); |
| 148 | _config_id += "_"; |
| 149 | _config_id += support::cpp11::to_string(norm_info.norm_size()); |
| 150 | _config_id += "_"; |
| 151 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 152 | _config_id += "_"; |
| 153 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 156 | Error CLNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, NormalizationLayerInfo norm_info) |
| 157 | { |
Giorgio Arena | c23b633 | 2017-11-28 15:23:51 +0000 | [diff] [blame] | 158 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, norm_info)); |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame^] | 159 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), norm_info).first); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 160 | |
| 161 | return Error{}; |
| 162 | } |
| 163 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 164 | void CLNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 165 | { |
| 166 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 167 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 168 | |
steniu01 | f70256b | 2017-07-13 14:03:35 +0100 | [diff] [blame] | 169 | const int collapsed_dimension = _is_in_map ? Window::DimZ : 4; |
| 170 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), collapsed_dimension); |
| 171 | Window slice = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | |
| 173 | do |
| 174 | { |
| 175 | unsigned int idx = 0; |
| 176 | add_3D_tensor_argument(idx, _input, slice); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | add_3D_tensor_argument(idx, _output, slice); |
| 178 | enqueue(queue, *this, slice); |
| 179 | } |
steniu01 | f70256b | 2017-07-13 14:03:35 +0100 | [diff] [blame] | 180 | while(window_collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | } |