Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +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 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 24 | #include "arm_compute/core/CL/kernels/CLL2NormalizeLayerKernel.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLValidate.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/ICLTensor.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +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 | |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 36 | #include "support/StringSupport.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 37 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 40 | namespace |
| 41 | { |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 42 | constexpr int max_input_tensor_dim = 3; |
| 43 | |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame] | 44 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 45 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 46 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon) |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 47 | { |
| 48 | ARM_COMPUTE_UNUSED(epsilon); |
| 49 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 50 | const uint32_t actual_axis = wrap_around(axis, max_input_tensor_dim); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum); |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 53 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis > 2, "Actual axis greater than 2 is not supported"); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis >= TensorShape::num_max_dimensions, "Actual normalization axis greater than max number of dimensions"); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 57 | |
| 58 | // Reduce shape on axis |
| 59 | TensorShape sum_shape = input->tensor_shape(); |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 60 | sum_shape.set(actual_axis, 1); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape); |
| 62 | |
| 63 | if(output->total_size() != 0) |
| 64 | { |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 67 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape()); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | return Status{}; |
| 72 | } |
| 73 | |
| 74 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 75 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 76 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 77 | |
| 78 | // Output tensor auto initialization if not yet initialized |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 79 | auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type()); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 80 | |
| 81 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 82 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 83 | |
| 84 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 85 | output_access.set_valid_region(win, input->valid_region()); |
| 86 | |
| 87 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 88 | |
| 89 | return std::make_tuple(err, win); |
| 90 | } |
| 91 | } // namespace |
| 92 | |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame] | 93 | CLL2NormalizeLayerKernel::CLL2NormalizeLayerKernel() |
| 94 | : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12) |
| 95 | { |
| 96 | } |
| 97 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 98 | void CLL2NormalizeLayerKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 99 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 100 | configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, axis, epsilon); |
| 101 | } |
| 102 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 103 | void CLL2NormalizeLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 104 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 105 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output); |
| 106 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 107 | |
Georgios Pinitas | 51403b5 | 2019-06-18 14:10:48 +0100 | [diff] [blame] | 108 | _input = input; |
| 109 | _sum = sum; |
| 110 | _output = output; |
| 111 | _actual_axis = wrap_around(axis, max_input_tensor_dim); |
| 112 | _epsilon = epsilon; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 113 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 114 | // Set build options |
| 115 | std::set<std::string> build_opts; |
| 116 | build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); |
| 117 | build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration))); |
| 118 | |
| 119 | // Create kernel |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 120 | std::string kernel_name; |
| 121 | unsigned int idx = 0; |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 122 | switch(_actual_axis) |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 123 | { |
| 124 | case 0: |
| 125 | kernel_name = "x"; |
Usama Arif | ae0001e | 2019-03-26 13:44:01 +0000 | [diff] [blame] | 126 | idx = num_arguments_per_2D_tensor() * 3; |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 127 | break; |
| 128 | case 1: |
| 129 | kernel_name = "y"; |
| 130 | idx = num_arguments_per_2D_tensor() * 3; |
| 131 | break; |
| 132 | case 2: |
| 133 | kernel_name = "z"; |
| 134 | idx = num_arguments_per_3D_tensor() * 3; |
| 135 | break; |
| 136 | default: |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 137 | ARM_COMPUTE_ERROR("Axis not supported"); |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 138 | } |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 139 | _kernel = create_kernel(compile_context, "l2_normalize_" + kernel_name, build_opts); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 140 | |
| 141 | // Set epsilon argument |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 142 | if(input->info()->data_type() == DataType::F32) |
| 143 | { |
Georgios Pinitas | 51403b5 | 2019-06-18 14:10:48 +0100 | [diff] [blame] | 144 | _kernel.setArg<cl_float>(idx, _epsilon); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 145 | } |
| 146 | else |
| 147 | { |
Georgios Pinitas | 51403b5 | 2019-06-18 14:10:48 +0100 | [diff] [blame] | 148 | _kernel.setArg<cl_half>(idx, _epsilon); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 149 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 150 | |
| 151 | // Configure kernel window |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 152 | auto win_config = validate_and_configure_window(_input->info(), _output->info()); |
| 153 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 154 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 155 | ICLKernel::configure_internal(std::get<1>(win_config)); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 156 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 157 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 158 | Status CLL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon) |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 159 | { |
| 160 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon)); |
| 161 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get()))); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 162 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 163 | return Status{}; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 166 | void CLL2NormalizeLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 167 | { |
| 168 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 169 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 170 | |
| 171 | Window window_sum(window); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 172 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 173 | switch(_actual_axis) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 174 | { |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 175 | case 0: |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 176 | { |
| 177 | window_sum.set(Window::DimX, Window::Dimension(0, 0, 0)); |
Usama Arif | ae0001e | 2019-03-26 13:44:01 +0000 | [diff] [blame] | 178 | Window in_slice = window.first_slice_window_2D(); |
| 179 | Window sum_slice = window_sum.first_slice_window_2D(); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 180 | do |
| 181 | { |
| 182 | unsigned int idx = 0; |
Usama Arif | ae0001e | 2019-03-26 13:44:01 +0000 | [diff] [blame] | 183 | add_2D_tensor_argument(idx, _input, in_slice); |
| 184 | add_2D_tensor_argument(idx, _sum, sum_slice); |
| 185 | add_2D_tensor_argument(idx, _output, in_slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 186 | enqueue(queue, *this, in_slice, lws_hint()); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 187 | } |
Usama Arif | ae0001e | 2019-03-26 13:44:01 +0000 | [diff] [blame] | 188 | while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice)); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 189 | } |
| 190 | break; |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 191 | case 1: |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 192 | { |
| 193 | window_sum.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 194 | Window in_slice = window.first_slice_window_2D(); |
| 195 | Window sum_slice = window_sum.first_slice_window_2D(); |
| 196 | do |
| 197 | { |
| 198 | unsigned int idx = 0; |
| 199 | add_2D_tensor_argument(idx, _input, in_slice); |
| 200 | add_2D_tensor_argument(idx, _sum, sum_slice); |
| 201 | add_2D_tensor_argument(idx, _output, in_slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 202 | enqueue(queue, *this, in_slice, lws_hint()); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 203 | } |
| 204 | while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice)); |
| 205 | } |
| 206 | break; |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 207 | case 2: |
| 208 | { |
| 209 | window_sum.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 210 | Window in_slice = window.first_slice_window_3D(); |
| 211 | Window sum_slice = window_sum.first_slice_window_3D(); |
| 212 | do |
| 213 | { |
| 214 | unsigned int idx = 0; |
| 215 | add_3D_tensor_argument(idx, _input, in_slice); |
| 216 | add_3D_tensor_argument(idx, _sum, sum_slice); |
| 217 | add_3D_tensor_argument(idx, _output, in_slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 218 | enqueue(queue, *this, in_slice, lws_hint()); |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 219 | } |
| 220 | while(window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice)); |
| 221 | } |
| 222 | break; |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 223 | default: |
| 224 | ARM_COMPUTE_ERROR("Not supported"); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 225 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 226 | } |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame] | 227 | } // namespace arm_compute |