Compute Library
 23.08
NEMeanStdDevNormalizationKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2022 Arm Limited.
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  */
25 
29 #include "arm_compute/core/Types.h"
31 #include "src/core/CPP/Validate.h"
32 #include "src/core/NEON/NEMath.h"
38 
39 namespace arm_compute
40 {
41 namespace
42 {
43 struct MeanStdDevNormSelectorData
44 {
46 };
47 
48 using MeanStdDevNormSelctorPtr = std::add_pointer<bool(const MeanStdDevNormSelectorData &data)>::type;
49 using MeanStdDevNormUKernelPtr = std::add_pointer<void(ITensor *input, ITensor *output, float epsilon, const Window &window)>::type;
50 
51 struct MeanStdDevNormKernel
52 {
53  const char *name;
54  const MeanStdDevNormSelctorPtr is_selected;
55  MeanStdDevNormUKernelPtr ukernel;
56 };
57 
58 static const std::vector<MeanStdDevNormKernel> available_kernels =
59 {
60  {
61  "fp32_neon_meanstddevnorm",
62  [](const MeanStdDevNormSelectorData & data) { return data.dt == DataType::F32; },
64  },
65 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
66  {
67  "fp16_neon_meanstddevnorm",
68  [](const MeanStdDevNormSelectorData & data) { return data.dt == DataType::F16; },
70  },
71 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
72  {
73  "qasymm8_neon_meanstddevnorm",
74  [](const MeanStdDevNormSelectorData & data) { return data.dt == DataType::QASYMM8; },
76  },
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 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 
97 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, float epsilon)
98 {
102  ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 2, "Input tensor cannot have more than 2 dimensions");
104 
105  // Checks performed when output is configured
106  if((output != nullptr) && (output->total_size() != 0))
107  {
110  }
111  return Status{};
112 }
113 
114 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
115 {
116  if(output != nullptr)
117  {
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());
126 
127  return std::make_pair(Status{}, win);
128 }
129 } // namespace
130 
132  : _input(nullptr), _output(nullptr), _epsilon(1e-8f)
133 {
134 }
135 
137 {
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);
150 }
151 
153 {
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 
160 {
164 
165  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);
169 }
170 } // namespace arm_compute
type
decltype(strategy::transforms) typedef type
Definition: gemm_interleaved.hpp:261
Helpers.h
arm_compute::calculate_max_window
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Definition: WindowHelpers.cpp:28
arm_compute::DataType::QASYMM8
@ QASYMM8
quantized, asymmetric fixed-point 8-bit number unsigned
arm_compute::NEMeanStdDevNormalizationKernel::NEMeanStdDevNormalizationKernel
NEMeanStdDevNormalizationKernel()
Default constructor.
Definition: NEMeanStdDevNormalizationKernel.cpp:131
arm_compute::cpu::kernels::validate_arguments
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
Definition: CpuDirectConv2dKernel.cpp:60
ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:1004
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:528
Types.h
Window.h
arm_compute::NEMeanStdDevNormalizationKernel::configure
void configure(ITensor *input, ITensor *output=nullptr, float epsilon=1e-8f)
Initialise the kernel's input and outputs.
Definition: NEMeanStdDevNormalizationKernel.cpp:136
TensorInfo.h
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
REGISTER_FP16_NEON
#define REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:48
arm_compute::cpu::neon_qasymm8_meanstddevnorm
void neon_qasymm8_meanstddevnorm(ITensor *input, ITensor *output, float epsilon, const Window &window)
Definition: qasymm8.cpp:51
Registrars.h
REGISTER_QASYMM8_NEON
#define REGISTER_QASYMM8_NEON(func_name)
Definition: Registrars.h:117
wrapper.h
Includes all wrapper headers at once.
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:630
ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:877
NEMath.h
ARM_COMPUTE_RETURN_ON_ERROR
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
dt
DataType dt
Definition: NEMeanStdDevNormalizationKernel.cpp:45
REGISTER_FP32_NEON
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:74
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
arm_compute::ITensor::info
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
ARM_COMPUTE_ERROR_ON
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:467
ARM_COMPUTE_ERROR_THROW_ON
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:456
ukernel
MeanStdDevNormUKernelPtr ukernel
Definition: NEMeanStdDevNormalizationKernel.cpp:55
arm_compute::cpu::neon_fp16_meanstddevnorm
void neon_fp16_meanstddevnorm(ITensor *input, ITensor *output, float epsilon, const Window &window)
ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
arm_compute::auto_init_if_empty
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
Definition: AutoConfiguration.h:43
arm_compute::Status
Status class.
Definition: Error.h:52
WindowHelpers.h
is_selected
const MeanStdDevNormSelctorPtr is_selected
Definition: NEMeanStdDevNormalizationKernel.cpp:54
ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
arm_compute::NEMeanStdDevNormalizationKernel::run
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
Definition: NEMeanStdDevNormalizationKernel.cpp:159
arm_compute::cpu::kernels::validate_and_configure_window
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst)
Definition: CpuDirectConv2dKernel.cpp:92
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
AutoConfiguration.h
arm_compute::test::validation::data_type
data_type
Definition: Cast.cpp:223
arm_compute::IKernel::window
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
arm_compute::ThreadInfo
Information about executing thread and CPU.
Definition: CPPTypes.h:180
arm_compute::misc::ICloneable::clone
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
name
const char * name
Definition: NEMeanStdDevNormalizationKernel.cpp:53
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
Validate.h
ARM_COMPUTE_RETURN_ERROR_ON_MSG
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:245
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::DataType::F16
@ F16
16-bit floating-point number
ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:43
arm_compute::DataType::F32
@ F32
32-bit floating-point number
list.h
ITensor.h
arm_compute::test::validation::info
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
arm_compute::NEMeanStdDevNormalizationKernel::validate
static Status validate(const ITensorInfo *input, const ITensorInfo *output=nullptr, float epsilon=1e-8f)
Static function to check if given info will lead to a valid configuration of NEMeanStdDevNormalizatio...
Definition: NEMeanStdDevNormalizationKernel.cpp:152
arm_compute::cpu::neon_fp32_meanstddevnorm
void neon_fp32_meanstddevnorm(ITensor *input, ITensor *output, float epsilon, const Window &window)
Definition: fp32.cpp:30
arm_compute::DataType
DataType
Available data types.
Definition: CoreTypes.h:82
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486
NEMeanStdDevNormalizationKernel.h
arm_compute::quantization::epsilon
constexpr float epsilon
Definition: AsymmHelpers.cpp:39