Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1 | /* |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 2 | * Copyright (c) 2017-2022 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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/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" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Utils.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/Window.h" |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 33 | #include "src/common/cpuinfo/CpuIsaInfo.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 34 | #include "src/core/NEON/NEMath.h" |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 35 | #include "src/core/common/Registrars.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 36 | #include "src/core/helpers/AutoConfiguration.h" |
| 37 | #include "src/core/helpers/WindowHelpers.h" |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 38 | #include "src/cpu/kernels/l2normlayer/list.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 39 | |
| 40 | #include <arm_neon.h> |
| 41 | #include <cmath> |
| 42 | |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 43 | namespace arm_compute |
| 44 | { |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 45 | namespace |
| 46 | { |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 47 | constexpr int max_input_tensor_dim = 3; |
| 48 | |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 49 | struct L2NormalizeLayerSelectorData |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 50 | { |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 51 | DataType dt; |
| 52 | unsigned int actual_axis; |
| 53 | cpuinfo::CpuIsaInfo isa; |
| 54 | }; |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 55 | |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 56 | using L2NormalizeLayerKernelSelctorPtr = std::add_pointer<bool(const L2NormalizeLayerSelectorData &data)>::type; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 57 | |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 58 | using L2NormalizeLayerPtr = std::add_pointer<void(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window, size_t axis)>::type; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 59 | |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 60 | struct L2NormalizeLayerKernel |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 61 | { |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 62 | const char *name; |
| 63 | const L2NormalizeLayerKernelSelctorPtr is_selected; |
| 64 | L2NormalizeLayerPtr ukernel; |
| 65 | }; |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 66 | |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 67 | static const L2NormalizeLayerKernel available_kernels[] = |
| 68 | { |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 69 | { |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 70 | "fp32_neon_l2normalize_x", |
| 71 | [](const L2NormalizeLayerSelectorData & data) { return data.dt == DataType::F32 && data.actual_axis == Window::DimX; }, |
| 72 | REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_l2_normalize_x) |
Georgios Pinitas | 6cb26ce | 2020-06-24 17:20:23 +0100 | [diff] [blame] | 73 | }, |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 74 | { |
| 75 | "fp32_neon_l2normalize_yz", |
| 76 | [](const L2NormalizeLayerSelectorData & data) { return data.dt == DataType::F32 && data.actual_axis != Window::DimX; }, |
| 77 | REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_l2_normalize_yz) |
| 78 | }, |
| 79 | { |
| 80 | "fp16_neon_l2normalize_x", |
| 81 | [](const L2NormalizeLayerSelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16 && data.actual_axis == Window::DimX; }, |
| 82 | REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_l2_normalize_x), |
| 83 | }, |
| 84 | { |
| 85 | "fp16_neon_l2normalize_yz", |
| 86 | [](const L2NormalizeLayerSelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16 && data.actual_axis != Window::DimX; }, |
| 87 | REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_l2_normalize_yz), |
| 88 | }, |
| 89 | }; |
| 90 | |
| 91 | /** Micro-kernel selector |
| 92 | * |
| 93 | * @param[in] data Selection data passed to help pick the appropriate micro-kernel |
| 94 | * |
| 95 | * @return A matching micro-kernel else nullptr |
| 96 | */ |
| 97 | const L2NormalizeLayerKernel *get_implementation(const L2NormalizeLayerSelectorData &data) |
| 98 | { |
| 99 | for(const auto &uk : available_kernels) |
| 100 | { |
| 101 | if(uk.is_selected(data)) |
| 102 | { |
| 103 | return &uk; |
| 104 | } |
| 105 | } |
| 106 | return nullptr; |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 109 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon) |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 110 | { |
| 111 | ARM_COMPUTE_UNUSED(epsilon); |
| 112 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 113 | const uint32_t actual_axis = wrap_around(axis, max_input_tensor_dim); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 114 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output); |
| 115 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 116 | 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] | 117 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis > 2, "Actual axis greater than 2 is not supported"); |
| 118 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis >= TensorShape::num_max_dimensions, "Actual normalization axis greater than max number of dimensions"); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 119 | |
| 120 | // Reduce shape on axis |
| 121 | TensorShape sum_shape = input->tensor_shape(); |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 122 | sum_shape.set(actual_axis, 1); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 123 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape); |
| 124 | |
| 125 | if(output->total_size() != 0) |
| 126 | { |
| 127 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 128 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 129 | 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] | 130 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | return Status{}; |
| 134 | } |
| 135 | |
Georgios Pinitas | 6cb26ce | 2020-06-24 17:20:23 +0100 | [diff] [blame] | 136 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 137 | { |
Georgios Pinitas | 6cb26ce | 2020-06-24 17:20:23 +0100 | [diff] [blame] | 138 | Window win = calculate_max_window(*input, Steps()); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 139 | |
| 140 | // Output auto initialization if not yet initialized |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 141 | auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type()); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 142 | |
Georgios Pinitas | 6cb26ce | 2020-06-24 17:20:23 +0100 | [diff] [blame] | 143 | // NEL2NormalizeLayerKernel doesn't need padding so update_window_and_padding() can be skipped |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 144 | |
Georgios Pinitas | 6cb26ce | 2020-06-24 17:20:23 +0100 | [diff] [blame] | 145 | return std::make_tuple(Status{}, win); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 146 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 147 | } // namespace |
| 148 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 149 | NEL2NormalizeLayerKernel::NEL2NormalizeLayerKernel() |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 150 | : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 151 | { |
| 152 | } |
| 153 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 154 | void NEL2NormalizeLayerKernel::configure(const ITensor *input, const ITensor *sum, ITensor *output, int axis, float epsilon) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 155 | { |
| 156 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 157 | 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] | 158 | |
Georgios Pinitas | 6cb26ce | 2020-06-24 17:20:23 +0100 | [diff] [blame] | 159 | _input = input; |
| 160 | _sum = sum; |
| 161 | _output = output; |
| 162 | _actual_axis = wrap_around(axis, max_input_tensor_dim); |
| 163 | _epsilon = epsilon; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 164 | |
| 165 | // Configure kernel window |
Georgios Pinitas | 6cb26ce | 2020-06-24 17:20:23 +0100 | [diff] [blame] | 166 | auto win_config = validate_and_configure_window(_input->info(), _output->info()); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 167 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 168 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 169 | INEKernel::configure(std::get<1>(win_config)); |
| 170 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 171 | |
Manuel Bottini | 4b5c588 | 2019-05-14 10:38:30 +0100 | [diff] [blame] | 172 | Status NEL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon) |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 173 | { |
| 174 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon)); |
Georgios Pinitas | 6cb26ce | 2020-06-24 17:20:23 +0100 | [diff] [blame] | 175 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get()))); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 176 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 177 | return Status{}; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 180 | void NEL2NormalizeLayerKernel::run(const Window &window, const ThreadInfo &info) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 181 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 182 | ARM_COMPUTE_UNUSED(info); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 183 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 184 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 185 | |
Georgios Pinitas | 6cb26ce | 2020-06-24 17:20:23 +0100 | [diff] [blame] | 186 | if(_actual_axis > 2) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 187 | { |
Georgios Pinitas | 6cb26ce | 2020-06-24 17:20:23 +0100 | [diff] [blame] | 188 | ARM_COMPUTE_ERROR("Unsupported normalization axis"); |
| 189 | } |
| 190 | |
Yair Schwarzbaum | 5e99318 | 2022-01-10 15:11:07 +0200 | [diff] [blame] | 191 | const auto *uk = get_implementation(L2NormalizeLayerSelectorData{ _output->info()->data_type(), _actual_axis, CPUInfo::get().get_isa() }); |
| 192 | ARM_COMPUTE_ERROR_ON(uk == nullptr); |
| 193 | ARM_COMPUTE_ERROR_ON(uk->ukernel == nullptr); |
| 194 | |
| 195 | uk->ukernel(_input, _sum, _output, _epsilon, window, _actual_axis); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 196 | } |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 197 | } // namespace arm_compute |