Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | d723076 | 2024-06-10 16:11:26 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2019-2022, 2024 Arm Limited. |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +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/NEInstanceNormalizationLayerKernel.h" |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 25 | |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/KernelDescriptors.h" |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 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" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 35 | #include "src/core/common/Registrars.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 36 | #include "src/core/CPP/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 37 | #include "src/core/helpers/AutoConfiguration.h" |
| 38 | #include "src/core/helpers/WindowHelpers.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 39 | #include "src/core/NEON/NEMath.h" |
| 40 | #include "src/core/NEON/wrapper/wrapper.h" |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 41 | #include "src/cpu/kernels/instancenorm/list.h" |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 42 | |
| 43 | #include <arm_neon.h> |
| 44 | |
| 45 | namespace arm_compute |
| 46 | { |
| 47 | namespace |
| 48 | { |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 49 | struct InstanceNormSelectorData |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 50 | { |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 51 | DataType dt; |
| 52 | }; |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 53 | |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 54 | using InstanceNormSelctorPtr = std::add_pointer<bool(const InstanceNormSelectorData &data)>::type; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 55 | using InstanceNormUKernelPtr = std::add_pointer<void(ITensor *input, |
| 56 | ITensor *output, |
| 57 | float gamma, |
| 58 | float beta, |
| 59 | float epsilon, |
| 60 | bool use_mixed_precision, |
| 61 | const Window &window)>::type; |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 62 | |
| 63 | struct InstanceNormKernel |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 64 | { |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 65 | const char *name; |
| 66 | const InstanceNormSelctorPtr is_selected; |
| 67 | InstanceNormUKernelPtr ukernel; |
| 68 | }; |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 69 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 70 | static const InstanceNormKernel available_kernels[] = { |
| 71 | {"fp32_neon_instancenorm", [](const InstanceNormSelectorData &data) { return data.dt == DataType::F32; }, |
| 72 | REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_instancenorm)}, |
Pablo Marquez Tello | d723076 | 2024-06-10 16:11:26 +0100 | [diff] [blame^] | 73 | #ifdef ARM_COMPUTE_ENABLE_FP16 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 74 | {"fp16_neon_instancenorm", [](const InstanceNormSelectorData &data) { return data.dt == DataType::F16; }, |
| 75 | REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_instancenorm)}, |
Pablo Marquez Tello | d723076 | 2024-06-10 16:11:26 +0100 | [diff] [blame^] | 76 | #endif // ARM_COMPUTE_ENABLE_FP16 |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | /** Micro-kernel selector |
| 80 | * |
| 81 | * @param[in] data Selection data passed to help pick the appropriate micro-kernel |
| 82 | * |
| 83 | * @return A matching micro-kernel else nullptr |
| 84 | */ |
| 85 | const InstanceNormKernel *get_implementation(const InstanceNormSelectorData &data) |
| 86 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | for (const auto &uk : available_kernels) |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 88 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 89 | if (uk.is_selected(data)) |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 90 | { |
| 91 | return &uk; |
| 92 | } |
| 93 | } |
| 94 | return nullptr; |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, float gamma, float beta, float epsilon) |
| 98 | { |
| 99 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
| 100 | ARM_COMPUTE_UNUSED(gamma); |
| 101 | ARM_COMPUTE_UNUSED(beta); |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(epsilon == 0.f, "Epsilon must be different than 0"); |
| 103 | |
Manuel Bottini | 581f178 | 2019-11-13 17:24:43 +0000 | [diff] [blame] | 104 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F16, DataType::F32); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 105 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_layout() == DataLayout::NHWC, |
| 106 | "NHWC data layout is not supported by the kernel directly"); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 107 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 108 | if (output != nullptr && output->total_size() != 0) |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 109 | { |
| 110 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 111 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 112 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 113 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_channels() != output->num_channels(), |
| 114 | "Input and output have different number of channels"); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 115 | } |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 116 | return Status{}; |
| 117 | } |
| 118 | |
| 119 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 120 | { |
| 121 | // We handle the planes manually |
| 122 | Window win = calculate_max_window(*input, Steps(1)); |
| 123 | |
| 124 | // Output auto initialization if not yet initialized |
| 125 | auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type()); |
| 126 | |
| 127 | // NEInstanceNormalizationLayerKernel doesn't need padding so update_window_and_padding() can be skipped |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 128 | return std::make_pair(Status{}, win); |
| 129 | } |
| 130 | } // namespace |
| 131 | |
| 132 | NEInstanceNormalizationLayerKernel::NEInstanceNormalizationLayerKernel() |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 133 | : _input(nullptr), _output(nullptr), _gamma(1), _beta(0), _epsilon(1e-12) |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 134 | { |
| 135 | } |
| 136 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 137 | void NEInstanceNormalizationLayerKernel::configure(ITensor *input, |
| 138 | ITensor *output, |
| 139 | const InstanceNormalizationLayerKernelInfo &info) |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 140 | { |
| 141 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
| 142 | |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 143 | _input = input; |
| 144 | _output = output == nullptr ? input : output; |
| 145 | _gamma = info.gamma; |
| 146 | _beta = info.beta; |
| 147 | _epsilon = info.epsilon; |
| 148 | _use_mixed_precision = info.use_mixed_precision; |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 149 | |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 150 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _output->info(), _gamma, _beta, _epsilon)); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 151 | |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 152 | // Configure kernel window |
| 153 | auto win_config = validate_and_configure_window(_input->info(), _output->info()); |
| 154 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
| 155 | |
| 156 | INEKernel::configure(std::get<1>(win_config)); |
| 157 | } |
| 158 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 159 | Status NEInstanceNormalizationLayerKernel::validate(const ITensorInfo *input, |
| 160 | const ITensorInfo *output, |
| 161 | const InstanceNormalizationLayerKernelInfo &info) |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 162 | { |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 163 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info.gamma, info.beta, info.epsilon)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 164 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window( |
| 165 | input->clone().get(), (output == nullptr ? input->clone().get() : output->clone().get())))); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 166 | return Status{}; |
| 167 | } |
| 168 | |
| 169 | void NEInstanceNormalizationLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 170 | { |
| 171 | ARM_COMPUTE_UNUSED(info); |
| 172 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 173 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 174 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 175 | const auto *uk = get_implementation(InstanceNormSelectorData{_input->info()->data_type()}); |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 176 | ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr); |
| 177 | |
| 178 | uk->ukernel(_input, _output, _gamma, _beta, _epsilon, _use_mixed_precision, window); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 179 | } |
| 180 | } // namespace arm_compute |