Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1 | /* |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +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/NEON/kernels/NEL2NormalizeLayerKernel.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/NEON/NEMath.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Utils.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame^] | 35 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 36 | #include <arm_neon.h> |
| 37 | #include <cmath> |
| 38 | |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame^] | 39 | namespace arm_compute |
| 40 | { |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 41 | namespace |
| 42 | { |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame^] | 43 | template <typename T, int S> |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 44 | void l2_normalize_X(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window) |
| 45 | { |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame^] | 46 | /** NEON vector tag type. */ |
| 47 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 48 | |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 49 | Window window_sum(window); |
| 50 | window_sum.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 51 | |
| 52 | Window in_slice = window.first_slice_window_1D(); |
| 53 | Window sum_slice = window_sum.first_slice_window_1D(); |
| 54 | |
| 55 | do |
| 56 | { |
| 57 | Iterator input_it(in, in_slice); |
| 58 | Iterator sum_it(sum, sum_slice); |
| 59 | Iterator output_it(out, in_slice); |
| 60 | |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame^] | 61 | const auto sum_value = *reinterpret_cast<const T *>(sum_it.ptr()); |
| 62 | const auto vec_normalize_value = wrapper::vdup_n(static_cast<T>(1.f / std::sqrt(std::max(sum_value, static_cast<T>(epsilon)))), ExactTagType{}); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 63 | |
| 64 | execute_window_loop(in_slice, [&](const Coordinates & id) |
| 65 | { |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame^] | 66 | const auto in_ptr = reinterpret_cast<const T *>(input_it.ptr()); |
| 67 | const auto out_ptr = reinterpret_cast<T *>(output_it.ptr()); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 68 | |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame^] | 69 | wrapper::vstore(out_ptr, wrapper::vmul(wrapper::vloadq(in_ptr), vec_normalize_value)); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 70 | }, |
| 71 | input_it, output_it); |
| 72 | } |
| 73 | while(window.slide_window_slice_1D(in_slice) && window.slide_window_slice_1D(sum_slice)); |
| 74 | } |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 75 | |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame^] | 76 | template <typename T, int S> |
| 77 | void l2_normalize_Y(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window) |
| 78 | { |
| 79 | /** NEON vector tag type. */ |
| 80 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 81 | |
| 82 | Window window_sum(window); |
| 83 | window_sum.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 84 | |
| 85 | Window in_slice = window.first_slice_window_2D(); |
| 86 | Window sum_slice = window_sum.first_slice_window_2D(); |
| 87 | |
| 88 | do |
| 89 | { |
| 90 | Iterator input_it(in, in_slice); |
| 91 | Iterator sum_it(sum, sum_slice); |
| 92 | Iterator output_it(out, in_slice); |
| 93 | |
| 94 | auto eps = wrapper::vdup_n(static_cast<T>(epsilon), ExactTagType{}); |
| 95 | |
| 96 | execute_window_loop(in_slice, [&](const Coordinates & id) |
| 97 | { |
| 98 | const auto in_ptr = reinterpret_cast<const T *>(input_it.ptr()); |
| 99 | const auto sum_ptr = reinterpret_cast<const T *>(sum_it.ptr()); |
| 100 | const auto out_ptr = reinterpret_cast<T *>(output_it.ptr()); |
| 101 | |
| 102 | const auto vec_normalize_value = wrapper::vinvsqrt(wrapper::vmax(wrapper::vloadq(sum_ptr), eps)); |
| 103 | wrapper::vstore(out_ptr, wrapper::vmul(wrapper::vloadq(in_ptr), vec_normalize_value)); |
| 104 | }, |
| 105 | input_it, sum_it, output_it); |
| 106 | } |
| 107 | while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice)); |
| 108 | } |
| 109 | |
| 110 | template <typename T, int S> |
| 111 | void l2_normalize_Z(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window) |
| 112 | { |
| 113 | /** NEON vector tag type. */ |
| 114 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 115 | |
| 116 | Window window_sum(window); |
| 117 | window_sum.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 118 | |
| 119 | Window in_slice = window.first_slice_window_3D(); |
| 120 | Window sum_slice = window_sum.first_slice_window_3D(); |
| 121 | |
| 122 | do |
| 123 | { |
| 124 | Iterator input_it(in, in_slice); |
| 125 | Iterator sum_it(sum, sum_slice); |
| 126 | Iterator output_it(out, in_slice); |
| 127 | |
| 128 | auto eps = wrapper::vdup_n(static_cast<T>(epsilon), ExactTagType{}); |
| 129 | |
| 130 | execute_window_loop(in_slice, [&](const Coordinates & id) |
| 131 | { |
| 132 | const auto in_ptr = reinterpret_cast<const T *>(input_it.ptr()); |
| 133 | const auto sum_ptr = reinterpret_cast<const T *>(sum_it.ptr()); |
| 134 | const auto out_ptr = reinterpret_cast<T *>(output_it.ptr()); |
| 135 | |
| 136 | const auto vec_normalize_value = wrapper::vinvsqrt(wrapper::vmax(wrapper::vloadq(sum_ptr), eps)); |
| 137 | wrapper::vstore(out_ptr, wrapper::vmul(wrapper::vloadq(in_ptr), vec_normalize_value)); |
| 138 | }, |
| 139 | input_it, sum_it, output_it); |
| 140 | } |
| 141 | while(window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice)); |
| 142 | } |
| 143 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 144 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, unsigned int axis, float epsilon) |
| 145 | { |
| 146 | ARM_COMPUTE_UNUSED(epsilon); |
| 147 | |
| 148 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output); |
| 149 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame^] | 150 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 151 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 2, "Axis greater than 2 is not supported"); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 152 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Normalization axis greater than max number of dimensions"); |
| 153 | |
| 154 | // Reduce shape on axis |
| 155 | TensorShape sum_shape = input->tensor_shape(); |
| 156 | sum_shape.set(axis, 1); |
| 157 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape); |
| 158 | |
| 159 | if(output->total_size() != 0) |
| 160 | { |
| 161 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 162 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 163 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape()); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame^] | 164 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return Status{}; |
| 168 | } |
| 169 | |
| 170 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *sum, ITensorInfo *output, unsigned int axis) |
| 171 | { |
| 172 | const unsigned int num_elems_processed_per_iteration = 16 / data_size_from_type(input->data_type()); |
| 173 | const unsigned int num_elems_processed_per_iteration_sum = (axis == 0) ? 1 : num_elems_processed_per_iteration; |
| 174 | |
| 175 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 176 | |
| 177 | // Output auto initialization if not yet initialized |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 178 | auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type()); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 179 | |
| 180 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 181 | AccessWindowHorizontal sum_access(sum, 0, num_elems_processed_per_iteration_sum); |
| 182 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 183 | |
| 184 | bool window_changed = update_window_and_padding(win, input_access, sum_access, output_access); |
| 185 | output_access.set_valid_region(win, input->valid_region()); |
| 186 | |
| 187 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 188 | |
| 189 | return std::make_tuple(err, win); |
| 190 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 191 | } // namespace |
| 192 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 193 | NEL2NormalizeLayerKernel::NEL2NormalizeLayerKernel() |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 194 | : _input(nullptr), _sum(nullptr), _output(nullptr), _axis(0), _epsilon(1e-12) |
| 195 | { |
| 196 | } |
| 197 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 198 | void NEL2NormalizeLayerKernel::configure(const ITensor *input, const ITensor *sum, ITensor *output, unsigned int axis, float epsilon) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 199 | { |
| 200 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 201 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon)); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 202 | |
| 203 | _input = input; |
| 204 | _sum = sum; |
| 205 | _output = output; |
| 206 | _axis = axis; |
| 207 | _epsilon = epsilon; |
| 208 | |
| 209 | // Configure kernel window |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 210 | auto win_config = validate_and_configure_window(_input->info(), _sum->info(), _output->info(), axis); |
| 211 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 212 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 213 | INEKernel::configure(std::get<1>(win_config)); |
| 214 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 215 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 216 | Status NEL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, unsigned int axis, float epsilon) |
| 217 | { |
| 218 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon)); |
| 219 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), sum->clone().get(), output->clone().get(), axis))); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 220 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 221 | return Status{}; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 224 | void NEL2NormalizeLayerKernel::run(const Window &window, const ThreadInfo &info) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 225 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 226 | ARM_COMPUTE_UNUSED(info); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 227 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 228 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 229 | |
| 230 | switch(_axis) |
| 231 | { |
| 232 | case 0: |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame^] | 233 | switch(_input->info()->data_type()) |
| 234 | { |
| 235 | case DataType::F32: |
| 236 | l2_normalize_X<float, 4>(_input, _sum, _output, _epsilon, window); |
| 237 | break; |
| 238 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 239 | case DataType::F16: |
| 240 | l2_normalize_X<float16_t, 8>(_input, _sum, _output, _epsilon, window); |
| 241 | break; |
| 242 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 243 | default: |
| 244 | ARM_COMPUTE_ERROR("Not implemented"); |
| 245 | } |
| 246 | break; |
| 247 | case 1: |
| 248 | switch(_input->info()->data_type()) |
| 249 | { |
| 250 | case DataType::F32: |
| 251 | l2_normalize_Y<float, 4>(_input, _sum, _output, _epsilon, window); |
| 252 | break; |
| 253 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 254 | case DataType::F16: |
| 255 | l2_normalize_Y<float16_t, 8>(_input, _sum, _output, _epsilon, window); |
| 256 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 257 | break; |
| 258 | default: |
| 259 | ARM_COMPUTE_ERROR("Not implemented"); |
| 260 | } |
| 261 | break; |
| 262 | case 2: |
| 263 | switch(_input->info()->data_type()) |
| 264 | { |
| 265 | case DataType::F32: |
| 266 | l2_normalize_Z<float, 4>(_input, _sum, _output, _epsilon, window); |
| 267 | break; |
| 268 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 269 | case DataType::F16: |
| 270 | l2_normalize_Z<float16_t, 8>(_input, _sum, _output, _epsilon, window); |
| 271 | break; |
| 272 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 273 | default: |
| 274 | ARM_COMPUTE_ERROR("Not implemented"); |
| 275 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 276 | break; |
| 277 | default: |
| 278 | ARM_COMPUTE_ERROR("Unsupported normalization axis"); |
| 279 | } |
| 280 | } |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame^] | 281 | } // namespace arm_compute |