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