Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 1 | /* |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 2 | * Copyright (c) 2019-2022 Arm Limited. |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +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/NEMeanStdDevNormalizationKernel.h" |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 25 | |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/ITensor.h" |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/TensorInfo.h" |
| 29 | #include "arm_compute/core/Types.h" |
| 30 | #include "arm_compute/core/Window.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 32 | #include "src/core/common/Registrars.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | #include "src/core/CPP/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 36 | #include "src/core/NEON/NEMath.h" |
| 37 | #include "src/core/NEON/wrapper/wrapper.h" |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 38 | #include "src/cpu/kernels/meanstddevnorm/list.h" |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace |
| 43 | { |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 44 | struct MeanStdDevNormSelectorData |
| 45 | { |
| 46 | DataType dt; |
| 47 | }; |
| 48 | |
| 49 | using MeanStdDevNormSelctorPtr = std::add_pointer<bool(const MeanStdDevNormSelectorData &data)>::type; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 50 | using MeanStdDevNormUKernelPtr = |
| 51 | std::add_pointer<void(ITensor *input, ITensor *output, float epsilon, const Window &window)>::type; |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 52 | |
| 53 | struct MeanStdDevNormKernel |
| 54 | { |
| 55 | const char *name; |
| 56 | const MeanStdDevNormSelctorPtr is_selected; |
| 57 | MeanStdDevNormUKernelPtr ukernel; |
| 58 | }; |
| 59 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | static const std::vector<MeanStdDevNormKernel> available_kernels = { |
| 61 | {"fp32_neon_meanstddevnorm", [](const MeanStdDevNormSelectorData &data) { return data.dt == DataType::F32; }, |
| 62 | REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_meanstddevnorm)}, |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 63 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 64 | {"fp16_neon_meanstddevnorm", [](const MeanStdDevNormSelectorData &data) { return data.dt == DataType::F16; }, |
| 65 | REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_meanstddevnorm)}, |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 66 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 67 | {"qasymm8_neon_meanstddevnorm", [](const MeanStdDevNormSelectorData &data) { return data.dt == DataType::QASYMM8; }, |
| 68 | REGISTER_QASYMM8_NEON(arm_compute::cpu::neon_qasymm8_meanstddevnorm)}, |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /** Micro-kernel selector |
| 72 | * |
| 73 | * @param[in] data Selection data passed to help pick the appropriate micro-kernel |
| 74 | * |
| 75 | * @return A matching micro-kernel else nullptr |
| 76 | */ |
| 77 | const MeanStdDevNormKernel *get_implementation(const MeanStdDevNormSelectorData &data) |
| 78 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 79 | for (const auto &uk : available_kernels) |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 80 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | if (uk.is_selected(data)) |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 82 | { |
| 83 | return &uk; |
| 84 | } |
| 85 | } |
| 86 | return nullptr; |
| 87 | } |
| 88 | |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 89 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, float epsilon) |
| 90 | { |
| 91 | ARM_COMPUTE_UNUSED(epsilon); |
| 92 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
| 93 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 2, "Input tensor cannot have more than 2 dimensions"); |
Murray Kornelsen | 6e09e14 | 2022-07-13 21:40:26 -0400 | [diff] [blame] | 95 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32, DataType::QASYMM8); |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 96 | |
| 97 | // Checks performed when output is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 98 | if ((output != nullptr) && (output->total_size() != 0)) |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 99 | { |
| 100 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 101 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 102 | } |
| 103 | return Status{}; |
| 104 | } |
| 105 | |
| 106 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 107 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 108 | if (output != nullptr) |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 109 | { |
| 110 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 111 | // Output auto inizialitation if not yet initialized |
| 112 | auto_init_if_empty(*output, *input); |
| 113 | } |
| 114 | |
| 115 | // This kernel doesn't need padding. A left-over for loop on dimension X, we cannot have any read or write out of memory |
| 116 | // For this reason num_elems_processed_per_iteration is set to 1 |
| 117 | Window win = calculate_max_window(*input, Steps()); |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 118 | |
| 119 | return std::make_pair(Status{}, win); |
| 120 | } |
| 121 | } // namespace |
| 122 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 123 | NEMeanStdDevNormalizationKernel::NEMeanStdDevNormalizationKernel() : _input(nullptr), _output(nullptr), _epsilon(1e-8f) |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 124 | { |
| 125 | } |
| 126 | |
| 127 | void NEMeanStdDevNormalizationKernel::configure(ITensor *input, ITensor *output, float epsilon) |
| 128 | { |
| 129 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
| 130 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 131 | ARM_COMPUTE_ERROR_THROW_ON(NEMeanStdDevNormalizationKernel::validate( |
| 132 | input->info(), (output != nullptr) ? output->info() : nullptr, epsilon)); |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 133 | |
| 134 | _input = input; |
| 135 | _output = (output == nullptr) ? input : output; |
| 136 | _epsilon = epsilon; |
| 137 | |
| 138 | // Configure kernel window |
| 139 | auto win_config = validate_and_configure_window(input->info(), (output == nullptr) ? nullptr : output->info()); |
| 140 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 141 | ICPPKernel::configure(win_config.second); |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | Status NEMeanStdDevNormalizationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, float epsilon) |
| 145 | { |
| 146 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, epsilon)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 147 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 148 | validate_and_configure_window(input->clone().get(), (output != nullptr) ? output->clone().get() : nullptr) |
| 149 | .first); |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 150 | return Status{}; |
| 151 | } |
| 152 | |
| 153 | void NEMeanStdDevNormalizationKernel::run(const Window &window, const ThreadInfo &info) |
| 154 | { |
| 155 | ARM_COMPUTE_UNUSED(info); |
| 156 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 157 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 158 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 159 | const auto *uk = get_implementation(MeanStdDevNormSelectorData{_output->info()->data_type()}); |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 160 | ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr); |
| 161 | |
| 162 | uk->ukernel(_input, _output, _epsilon, window); |
Michele Di Giorgio | 9175392 | 2019-06-13 10:56:59 +0100 | [diff] [blame] | 163 | } |
| 164 | } // namespace arm_compute |