Compute Library
 22.11
NEInstanceNormalizationLayerKernel.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 
26 #include "arm_compute/core/Error.h"
31 #include "arm_compute/core/Utils.h"
34 #include "src/core/CPP/Validate.h"
35 #include "src/core/NEON/NEMath.h"
41 
42 #include <arm_neon.h>
43 
44 namespace arm_compute
45 {
46 namespace
47 {
48 struct InstanceNormSelectorData
49 {
51 };
52 
55 
56 struct InstanceNormKernel
57 {
58  const char *name;
59  const InstanceNormSelctorPtr is_selected;
60  InstanceNormUKernelPtr ukernel;
61 };
62 
63 static const InstanceNormKernel available_kernels[] =
64 {
65  {
66  "fp32_neon_instancenorm",
67  [](const InstanceNormSelectorData & data) { return data.dt == DataType::F32; },
69  },
70 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
71  {
72  "fp16_neon_instancenorm",
73  [](const InstanceNormSelectorData & data) { return data.dt == DataType::F16; },
75  },
76 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
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 {
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 gamma, float beta, float epsilon)
98 {
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 
105  ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_layout() == DataLayout::NHWC, "NHWC data layout is not supported by the kernel directly");
106 
107  if(output != nullptr && output->total_size() != 0)
108  {
112  ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_channels() != output->num_channels(), "Input and output have different number of channels");
113  }
114  return Status{};
115 }
116 
117 std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
118 {
119  // We handle the planes manually
120  Window win = calculate_max_window(*input, Steps(1));
121 
122  // Output auto initialization if not yet initialized
123  auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type());
124 
125  // NEInstanceNormalizationLayerKernel doesn't need padding so update_window_and_padding() can be skipped
126  return std::make_pair(Status{}, win);
127 }
128 } // namespace
129 
131  : _input(nullptr), _output(nullptr), _gamma(1), _beta(0), _epsilon(1e-12)
132 {
133 }
134 
136 {
138 
139  _input = input;
140  _output = output == nullptr ? input : output;
141  _gamma = info.gamma;
142  _beta = info.beta;
143  _epsilon = info.epsilon;
144  _use_mixed_precision = info.use_mixed_precision;
145 
146  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _output->info(), _gamma, _beta, _epsilon));
147 
148  // Configure kernel window
149  auto win_config = validate_and_configure_window(_input->info(), _output->info());
150  ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
151 
152  INEKernel::configure(std::get<1>(win_config));
153 }
154 
156 {
157  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info.gamma, info.beta, info.epsilon));
158  ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), (output == nullptr ? input->clone().get() : output->clone().get()))));
159  return Status{};
160 }
161 
163 {
164  ARM_COMPUTE_UNUSED(info);
167 
168  const auto *uk = get_implementation(InstanceNormSelectorData{ _input->info()->data_type() });
169  ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
170 
171  uk->ukernel(_input, _output, _gamma, _beta, _epsilon, _use_mixed_precision, window);
172 }
173 } // namespace arm_compute
void neon_fp32_instancenorm(ITensor *input, ITensor *output, float gamma, float beta, float epsilon, bool use_mixed_precision, const Window &window)
Definition: fp32.cpp:29
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void neon_fp16_instancenorm(ITensor *input, ITensor *output, float gamma, float beta, float epsilon, bool use_mixed_precision, const Window &window)
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:490
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
#define REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:48
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:74
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const InstanceNormalizationLayerKernelInfo &info)
Static function to check if given info will lead to a valid configuration of NEInstanceNormalizationL...
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
void configure(ITensor *input, ITensor *output, const InstanceNormalizationLayerKernelInfo &info)
Set the input and output tensors.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
decltype(strategy::transforms) typedef type
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2022 Arm Limited.
1 channel, 1 F16 per channel
bool use_mixed_precision
Use mixed precision in case of FP16 execution.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
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...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
float epsilon
Lower bound value for the normalization.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst)
Information about executing thread and CPU.
Definition: CPPTypes.h:179
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:439
const InstanceNormSelctorPtr is_selected
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
float gamma
The scale scalar value applied to the normalized tensor.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
InstanceNormUKernelPtr ukernel
Num samples, height, width, channels.
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
Includes all wrapper headers at once.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:690
DataType
Available data types.
Definition: Types.h:79
Describe a multidimensional execution window.
Definition: Window.h:39
float beta
The offset scalar value applied to the normalized tensor.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201