blob: 37e88a8565fd389cc6f528a73c837589ce1de8fd [file] [log] [blame]
Michele Di Giorgio91753922019-06-13 10:56:59 +01001/*
Dana Zlotnik027bcef2021-12-27 17:35:00 +02002 * Copyright (c) 2019-2022 Arm Limited.
Michele Di Giorgio91753922019-06-13 10:56:59 +01003 *
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 Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEMeanStdDevNormalizationKernel.h"
Michele Di Giorgio91753922019-06-13 10:56:59 +010025
Michele Di Giorgio91753922019-06-13 10:56:59 +010026#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/ITensor.h"
Michele Di Giorgio91753922019-06-13 10:56:59 +010028#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/core/Types.h"
30#include "arm_compute/core/Window.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CPP/Validate.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010032#include "src/core/NEON/NEMath.h"
33#include "src/core/NEON/wrapper/wrapper.h"
Dana Zlotnik027bcef2021-12-27 17:35:00 +020034#include "src/core/common/Registrars.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
Dana Zlotnik027bcef2021-12-27 17:35:00 +020037#include "src/cpu/kernels/meanstddevnorm/list.h"
Michele Di Giorgio91753922019-06-13 10:56:59 +010038
39namespace arm_compute
40{
41namespace
42{
Dana Zlotnik027bcef2021-12-27 17:35:00 +020043struct MeanStdDevNormSelectorData
44{
45 DataType dt;
46};
47
48using MeanStdDevNormSelctorPtr = std::add_pointer<bool(const MeanStdDevNormSelectorData &data)>::type;
49using MeanStdDevNormUKernelPtr = std::add_pointer<void(ITensor *input, ITensor *output, float epsilon, const Window &window)>::type;
50
51struct MeanStdDevNormKernel
52{
53 const char *name;
54 const MeanStdDevNormSelctorPtr is_selected;
55 MeanStdDevNormUKernelPtr ukernel;
56};
57
Murray Kornelsen6e09e142022-07-13 21:40:26 -040058static const std::vector<MeanStdDevNormKernel> available_kernels =
Dana Zlotnik027bcef2021-12-27 17:35:00 +020059{
60 {
61 "fp32_neon_meanstddevnorm",
62 [](const MeanStdDevNormSelectorData & data) { return data.dt == DataType::F32; },
63 REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_meanstddevnorm)
64 },
65#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
66 {
67 "fp16_neon_meanstddevnorm",
68 [](const MeanStdDevNormSelectorData & data) { return data.dt == DataType::F16; },
69 REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_meanstddevnorm)
70 },
71#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Murray Kornelsen6e09e142022-07-13 21:40:26 -040072 {
73 "qasymm8_neon_meanstddevnorm",
74 [](const MeanStdDevNormSelectorData & data) { return data.dt == DataType::QASYMM8; },
75 REGISTER_QASYMM8_NEON(arm_compute::cpu::neon_qasymm8_meanstddevnorm)
76 },
Dana Zlotnik027bcef2021-12-27 17:35:00 +020077};
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 */
85const MeanStdDevNormKernel *get_implementation(const MeanStdDevNormSelectorData &data)
86{
87 for(const auto &uk : available_kernels)
88 {
89 if(uk.is_selected(data))
90 {
91 return &uk;
92 }
93 }
94 return nullptr;
95}
96
Michele Di Giorgio91753922019-06-13 10:56:59 +010097Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, float epsilon)
98{
99 ARM_COMPUTE_UNUSED(epsilon);
100 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
101 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
102 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 2, "Input tensor cannot have more than 2 dimensions");
Murray Kornelsen6e09e142022-07-13 21:40:26 -0400103 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32, DataType::QASYMM8);
Michele Di Giorgio91753922019-06-13 10:56:59 +0100104
105 // Checks performed when output is configured
106 if((output != nullptr) && (output->total_size() != 0))
107 {
108 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
109 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
110 }
111 return Status{};
112}
113
114std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
115{
116 if(output != nullptr)
117 {
118 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
119 // Output auto inizialitation if not yet initialized
120 auto_init_if_empty(*output, *input);
121 }
122
123 // This kernel doesn't need padding. A left-over for loop on dimension X, we cannot have any read or write out of memory
124 // For this reason num_elems_processed_per_iteration is set to 1
125 Window win = calculate_max_window(*input, Steps());
Michele Di Giorgio91753922019-06-13 10:56:59 +0100126
127 return std::make_pair(Status{}, win);
128}
129} // namespace
130
Michele Di Giorgio91753922019-06-13 10:56:59 +0100131NEMeanStdDevNormalizationKernel::NEMeanStdDevNormalizationKernel()
Dana Zlotnik027bcef2021-12-27 17:35:00 +0200132 : _input(nullptr), _output(nullptr), _epsilon(1e-8f)
Michele Di Giorgio91753922019-06-13 10:56:59 +0100133{
134}
135
136void NEMeanStdDevNormalizationKernel::configure(ITensor *input, ITensor *output, float epsilon)
137{
138 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
139
140 ARM_COMPUTE_ERROR_THROW_ON(NEMeanStdDevNormalizationKernel::validate(input->info(), (output != nullptr) ? output->info() : nullptr, epsilon));
141
142 _input = input;
143 _output = (output == nullptr) ? input : output;
144 _epsilon = epsilon;
145
146 // Configure kernel window
147 auto win_config = validate_and_configure_window(input->info(), (output == nullptr) ? nullptr : output->info());
148 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
149 ICPPKernel::configure(win_config.second);
Michele Di Giorgio91753922019-06-13 10:56:59 +0100150}
151
152Status NEMeanStdDevNormalizationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, float epsilon)
153{
154 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, epsilon));
155 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output != nullptr) ? output->clone().get() : nullptr).first);
156 return Status{};
157}
158
159void NEMeanStdDevNormalizationKernel::run(const Window &window, const ThreadInfo &info)
160{
161 ARM_COMPUTE_UNUSED(info);
162 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
163 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Michele Di Giorgio91753922019-06-13 10:56:59 +0100164
Dana Zlotnik027bcef2021-12-27 17:35:00 +0200165 const auto *uk = get_implementation(MeanStdDevNormSelectorData{ _output->info()->data_type() });
166 ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
167
168 uk->ukernel(_input, _output, _epsilon, window);
Michele Di Giorgio91753922019-06-13 10:56:59 +0100169}
170} // namespace arm_compute