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 | |
| 35 | #include <arm_neon.h> |
| 36 | #include <cmath> |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
| 40 | namespace |
| 41 | { |
| 42 | void l2_normalize_X(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window) |
| 43 | { |
| 44 | Window window_sum(window); |
| 45 | window_sum.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 46 | |
| 47 | Window in_slice = window.first_slice_window_1D(); |
| 48 | Window sum_slice = window_sum.first_slice_window_1D(); |
| 49 | |
| 50 | do |
| 51 | { |
| 52 | Iterator input_it(in, in_slice); |
| 53 | Iterator sum_it(sum, sum_slice); |
| 54 | Iterator output_it(out, in_slice); |
| 55 | |
| 56 | const float sum_value = *reinterpret_cast<const float *>(sum_it.ptr()); |
| 57 | const float32x4_t vec_normalize_value = vdupq_n_f32(1.f / std::sqrt(std::max(sum_value, epsilon))); |
| 58 | |
| 59 | execute_window_loop(in_slice, [&](const Coordinates & id) |
| 60 | { |
| 61 | const auto in_ptr = reinterpret_cast<const float *>(input_it.ptr()); |
| 62 | const auto out_ptr = reinterpret_cast<float *>(output_it.ptr()); |
| 63 | |
| 64 | vst1q_f32(out_ptr, vmulq_f32(vld1q_f32(in_ptr), vec_normalize_value)); |
| 65 | }, |
| 66 | input_it, output_it); |
| 67 | } |
| 68 | while(window.slide_window_slice_1D(in_slice) && window.slide_window_slice_1D(sum_slice)); |
| 69 | } |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 70 | |
| 71 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, unsigned int axis, float epsilon) |
| 72 | { |
| 73 | ARM_COMPUTE_UNUSED(epsilon); |
| 74 | |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output); |
| 76 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum); |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() != DataLayout::NCHW); |
| 79 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 0, "Unsupported normalization axis, Supported axis is 0"); |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Normalization axis greater than max number of dimensions"); |
| 81 | |
| 82 | // Reduce shape on axis |
| 83 | TensorShape sum_shape = input->tensor_shape(); |
| 84 | sum_shape.set(axis, 1); |
| 85 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape); |
| 86 | |
| 87 | if(output->total_size() != 0) |
| 88 | { |
| 89 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 90 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 91 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape()); |
| 92 | ARM_COMPUTE_RETURN_ERROR_ON(output->data_layout() != DataLayout::NCHW); |
| 93 | } |
| 94 | |
| 95 | return Status{}; |
| 96 | } |
| 97 | |
| 98 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *sum, ITensorInfo *output, unsigned int axis) |
| 99 | { |
| 100 | const unsigned int num_elems_processed_per_iteration = 16 / data_size_from_type(input->data_type()); |
| 101 | const unsigned int num_elems_processed_per_iteration_sum = (axis == 0) ? 1 : num_elems_processed_per_iteration; |
| 102 | |
| 103 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 104 | |
| 105 | // Output auto initialization if not yet initialized |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame^] | 106 | auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type()); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 107 | |
| 108 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 109 | AccessWindowHorizontal sum_access(sum, 0, num_elems_processed_per_iteration_sum); |
| 110 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 111 | |
| 112 | bool window_changed = update_window_and_padding(win, input_access, sum_access, output_access); |
| 113 | output_access.set_valid_region(win, input->valid_region()); |
| 114 | |
| 115 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 116 | |
| 117 | return std::make_tuple(err, win); |
| 118 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 119 | } // namespace |
| 120 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 121 | NEL2NormalizeLayerKernel::NEL2NormalizeLayerKernel() |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 122 | : _input(nullptr), _sum(nullptr), _output(nullptr), _axis(0), _epsilon(1e-12) |
| 123 | { |
| 124 | } |
| 125 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 126 | 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] | 127 | { |
| 128 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 129 | 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] | 130 | |
| 131 | _input = input; |
| 132 | _sum = sum; |
| 133 | _output = output; |
| 134 | _axis = axis; |
| 135 | _epsilon = epsilon; |
| 136 | |
| 137 | // Configure kernel window |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 138 | auto win_config = validate_and_configure_window(_input->info(), _sum->info(), _output->info(), axis); |
| 139 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 140 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 141 | INEKernel::configure(std::get<1>(win_config)); |
| 142 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 143 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 144 | Status NEL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, unsigned int axis, float epsilon) |
| 145 | { |
| 146 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon)); |
| 147 | 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] | 148 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 149 | return Status{}; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 152 | void NEL2NormalizeLayerKernel::run(const Window &window, const ThreadInfo &info) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 153 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 154 | ARM_COMPUTE_UNUSED(info); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 155 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 156 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 157 | |
| 158 | switch(_axis) |
| 159 | { |
| 160 | case 0: |
| 161 | l2_normalize_X(_input, _sum, _output, _epsilon, window); |
| 162 | break; |
| 163 | default: |
| 164 | ARM_COMPUTE_ERROR("Unsupported normalization axis"); |
| 165 | } |
| 166 | } |