Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/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" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Utils.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/CL/CLValidate.h" |
| 35 | #include "src/core/helpers/AutoConfiguration.h" |
| 36 | #include "src/core/helpers/WindowHelpers.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 37 | |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 38 | #include "support/StringSupport.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 39 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 40 | namespace arm_compute |
| 41 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 42 | namespace |
| 43 | { |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 44 | constexpr int max_input_tensor_dim = 3; |
| 45 | |
| 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 | } |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 73 | } // namespace |
| 74 | |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame] | 75 | CLL2NormalizeLayerKernel::CLL2NormalizeLayerKernel() |
| 76 | : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12) |
| 77 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 78 | _type = CLKernelType::ELEMENTWISE; |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 81 | 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] | 82 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 83 | configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, axis, epsilon); |
| 84 | } |
| 85 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 86 | 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] | 87 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 88 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output); |
| 89 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon)); |
Manuel Bottini | 7a452fe | 2021-03-31 18:22:59 +0100 | [diff] [blame] | 90 | auto padding_info = get_padding_info({ input, sum, output }); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 91 | |
Georgios Pinitas | 51403b5 | 2019-06-18 14:10:48 +0100 | [diff] [blame] | 92 | _input = input; |
| 93 | _sum = sum; |
| 94 | _output = output; |
| 95 | _actual_axis = wrap_around(axis, max_input_tensor_dim); |
| 96 | _epsilon = epsilon; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 97 | |
Manuel Bottini | 7a452fe | 2021-03-31 18:22:59 +0100 | [diff] [blame] | 98 | const unsigned int vec_size_x = adjust_vec_size(max_cl_vector_width / input->info()->element_size(), input->info()->dimension(0)); |
| 99 | const int vec_size_x_leftovers = input->info()->dimension(0) % vec_size_x; |
| 100 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 101 | // Set build options |
Manuel Bottini | 7a452fe | 2021-03-31 18:22:59 +0100 | [diff] [blame] | 102 | CLBuildOptions build_opts; |
| 103 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 104 | build_opts.add_option("-DVEC_SIZE_X=" + support::cpp11::to_string(vec_size_x)); |
| 105 | build_opts.add_option("-DVEC_SIZE_LEFTOVER_X=" + support::cpp11::to_string(vec_size_x_leftovers)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 106 | |
| 107 | // Create kernel |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 108 | std::string kernel_name; |
| 109 | unsigned int idx = 0; |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 110 | switch(_actual_axis) |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 111 | { |
| 112 | case 0: |
Manuel Bottini | 7a452fe | 2021-03-31 18:22:59 +0100 | [diff] [blame] | 113 | kernel_name = "l2_normalize_x"; |
Usama Arif | ae0001e | 2019-03-26 13:44:01 +0000 | [diff] [blame] | 114 | idx = num_arguments_per_2D_tensor() * 3; |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 115 | break; |
| 116 | case 1: |
Manuel Bottini | 7a452fe | 2021-03-31 18:22:59 +0100 | [diff] [blame] | 117 | kernel_name = "l2_normalize_y"; |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 118 | idx = num_arguments_per_2D_tensor() * 3; |
| 119 | break; |
| 120 | case 2: |
Manuel Bottini | 7a452fe | 2021-03-31 18:22:59 +0100 | [diff] [blame] | 121 | kernel_name = "l2_normalize_z"; |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 122 | idx = num_arguments_per_3D_tensor() * 3; |
| 123 | break; |
| 124 | default: |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 125 | ARM_COMPUTE_ERROR("Axis not supported"); |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 126 | } |
Manuel Bottini | 7a452fe | 2021-03-31 18:22:59 +0100 | [diff] [blame] | 127 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 128 | |
| 129 | // Set epsilon argument |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 130 | if(input->info()->data_type() == DataType::F32) |
| 131 | { |
Georgios Pinitas | 51403b5 | 2019-06-18 14:10:48 +0100 | [diff] [blame] | 132 | _kernel.setArg<cl_float>(idx, _epsilon); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 133 | } |
| 134 | else |
| 135 | { |
Georgios Pinitas | 51403b5 | 2019-06-18 14:10:48 +0100 | [diff] [blame] | 136 | _kernel.setArg<cl_half>(idx, _epsilon); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 137 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 138 | |
| 139 | // Configure kernel window |
Manuel Bottini | 7a452fe | 2021-03-31 18:22:59 +0100 | [diff] [blame] | 140 | Window win = calculate_max_window(*input->info(), Steps(vec_size_x)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 141 | |
Manuel Bottini | 7a452fe | 2021-03-31 18:22:59 +0100 | [diff] [blame] | 142 | // Output tensor auto initialization if not yet initialized |
| 143 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type()); |
| 144 | |
| 145 | ICLKernel::configure_internal(win); |
| 146 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 147 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 148 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 149 | 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] | 150 | { |
| 151 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon)); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 152 | return Status{}; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 155 | void CLL2NormalizeLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 156 | { |
| 157 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 158 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 159 | |
| 160 | Window window_sum(window); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 161 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 162 | switch(_actual_axis) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 163 | { |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 164 | case 0: |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 165 | { |
| 166 | window_sum.set(Window::DimX, Window::Dimension(0, 0, 0)); |
Usama Arif | ae0001e | 2019-03-26 13:44:01 +0000 | [diff] [blame] | 167 | Window in_slice = window.first_slice_window_2D(); |
| 168 | Window sum_slice = window_sum.first_slice_window_2D(); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 169 | do |
| 170 | { |
| 171 | unsigned int idx = 0; |
Usama Arif | ae0001e | 2019-03-26 13:44:01 +0000 | [diff] [blame] | 172 | add_2D_tensor_argument(idx, _input, in_slice); |
| 173 | add_2D_tensor_argument(idx, _sum, sum_slice); |
| 174 | add_2D_tensor_argument(idx, _output, in_slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 175 | enqueue(queue, *this, in_slice, lws_hint()); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 176 | } |
Usama Arif | ae0001e | 2019-03-26 13:44:01 +0000 | [diff] [blame] | 177 | 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] | 178 | } |
| 179 | break; |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 180 | case 1: |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 181 | { |
| 182 | window_sum.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 183 | Window in_slice = window.first_slice_window_2D(); |
| 184 | Window sum_slice = window_sum.first_slice_window_2D(); |
| 185 | do |
| 186 | { |
| 187 | unsigned int idx = 0; |
| 188 | add_2D_tensor_argument(idx, _input, in_slice); |
| 189 | add_2D_tensor_argument(idx, _sum, sum_slice); |
| 190 | add_2D_tensor_argument(idx, _output, in_slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 191 | enqueue(queue, *this, in_slice, lws_hint()); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 192 | } |
| 193 | while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice)); |
| 194 | } |
| 195 | break; |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 196 | case 2: |
| 197 | { |
| 198 | window_sum.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 199 | Window in_slice = window.first_slice_window_3D(); |
| 200 | Window sum_slice = window_sum.first_slice_window_3D(); |
| 201 | do |
| 202 | { |
| 203 | unsigned int idx = 0; |
| 204 | add_3D_tensor_argument(idx, _input, in_slice); |
| 205 | add_3D_tensor_argument(idx, _sum, sum_slice); |
| 206 | add_3D_tensor_argument(idx, _output, in_slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 207 | enqueue(queue, *this, in_slice, lws_hint()); |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 208 | } |
| 209 | while(window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice)); |
| 210 | } |
| 211 | break; |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 212 | default: |
| 213 | ARM_COMPUTE_ERROR("Not supported"); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 214 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 215 | } |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame] | 216 | } // namespace arm_compute |