Compute Library
 23.08
CLL2NormalizeLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021, 2023 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 
31 #include "arm_compute/core/Utils.h"
34 #include "src/core/CL/CLValidate.h"
37 
38 #include "support/StringSupport.h"
39 
40 namespace arm_compute
41 {
42 namespace
43 {
44 constexpr int max_input_tensor_dim = 3;
45 
46 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
47 {
49 
50  const uint32_t actual_axis = wrap_around(axis, max_input_tensor_dim);
55  ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis > 2, "Actual axis greater than 2 is not supported");
56  ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis >= TensorShape::num_max_dimensions, "Actual normalization axis greater than max number of dimensions");
57 
58  // Reduce shape on axis
59  TensorShape sum_shape = input->tensor_shape();
60  sum_shape.set(actual_axis, 1);
61  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape);
62 
63  if(output->total_size() != 0)
64  {
68  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
69  }
70 
71  return Status{};
72 }
73 } // namespace
74 
76  : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12)
77 {
79 }
80 
81 void CLL2NormalizeLayerKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
82 {
83  configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, axis, epsilon);
84 }
85 
86 void CLL2NormalizeLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
87 {
88  ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
89  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
90  auto padding_info = get_padding_info({ input, sum, output });
91 
92  _input = input;
93  _sum = sum;
94  _output = output;
95  _actual_axis = wrap_around(axis, max_input_tensor_dim);
96  _epsilon = epsilon;
97 
98  const unsigned int vec_size_x = adjust_vec_size(max_cl_vector_width / input->info()->element_size(), input->info()->dimension(0));
99  const int vec_size_x_leftovers = input->info()->dimension(0) % vec_size_x;
100 
101  // Set build options
102  CLBuildOptions build_opts;
103  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
104  build_opts.add_option("-DVEC_SIZE_X=" + support::cpp11::to_string(vec_size_x));
105  build_opts.add_option("-DVEC_SIZE_LEFTOVER_X=" + support::cpp11::to_string(vec_size_x_leftovers));
106 
107  // Create kernel
108  std::string kernel_name;
109  unsigned int idx = 0;
110  switch(_actual_axis)
111  {
112  case 0:
113  kernel_name = "l2_normalize_x";
114  idx = num_arguments_per_2D_tensor() * 3;
115  break;
116  case 1:
117  kernel_name = "l2_normalize_y";
118  idx = num_arguments_per_2D_tensor() * 3;
119  break;
120  case 2:
121  kernel_name = "l2_normalize_z";
122  idx = num_arguments_per_3D_tensor() * 3;
123  break;
124  default:
125  ARM_COMPUTE_ERROR("Axis not supported");
126  }
127  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
128 
129  // Set epsilon argument
130  if(input->info()->data_type() == DataType::F32)
131  {
132  _kernel.setArg<cl_float>(idx, _epsilon);
133  }
134  else
135  {
136  _kernel.setArg<cl_half>(idx, _epsilon);
137  }
138 
139  // Configure kernel window
140  Window win = calculate_max_window(*input->info(), Steps(vec_size_x));
141 
142  // Output tensor auto initialization if not yet initialized
143  auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
144 
145  ICLKernel::configure_internal(win);
147 }
148 
149 Status CLL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
150 {
152  return Status{};
153 }
154 
155 void CLL2NormalizeLayerKernel::run(const Window &window, cl::CommandQueue &queue)
156 {
159 
160  Window window_sum(window);
161 
162  switch(_actual_axis)
163  {
164  case 0:
165  {
166  window_sum.set(Window::DimX, Window::Dimension(0, 0, 0));
167  Window in_slice = window.first_slice_window_2D();
168  Window sum_slice = window_sum.first_slice_window_2D();
169  do
170  {
171  unsigned int idx = 0;
172  add_2D_tensor_argument(idx, _input, in_slice);
173  add_2D_tensor_argument(idx, _sum, sum_slice);
174  add_2D_tensor_argument(idx, _output, in_slice);
175  enqueue(queue, *this, in_slice, lws_hint());
176  }
177  while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
178  }
179  break;
180  case 1:
181  {
182  window_sum.set(Window::DimY, Window::Dimension(0, 0, 0));
183  Window in_slice = window.first_slice_window_2D();
184  Window sum_slice = window_sum.first_slice_window_2D();
185  do
186  {
187  unsigned int idx = 0;
188  add_2D_tensor_argument(idx, _input, in_slice);
189  add_2D_tensor_argument(idx, _sum, sum_slice);
190  add_2D_tensor_argument(idx, _output, in_slice);
191  enqueue(queue, *this, in_slice, lws_hint());
192  }
193  while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
194  }
195  break;
196  case 2:
197  {
198  window_sum.set(Window::DimZ, Window::Dimension(0, 0, 0));
199  Window in_slice = window.first_slice_window_3D();
200  Window sum_slice = window_sum.first_slice_window_3D();
201  do
202  {
203  unsigned int idx = 0;
204  add_3D_tensor_argument(idx, _input, in_slice);
205  add_3D_tensor_argument(idx, _sum, sum_slice);
206  add_3D_tensor_argument(idx, _output, in_slice);
207  enqueue(queue, *this, in_slice, lws_hint());
208  }
209  while(window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice));
210  }
211  break;
212  default:
213  ARM_COMPUTE_ERROR("Not supported");
214  }
215 }
216 } // namespace arm_compute
arm_compute::Steps
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
arm_compute::support::cpp11::to_string
std::string to_string(T &&value)
Convert integer and float values to string.
Definition: StringSupport.h:168
StringSupport.h
AdjustVecSize.h
ICLTensor.h
CLL2NormalizeLayerKernel.h
Helpers.h
arm_compute::CLBuildOptions::options
const StringSet & options() const
Gets the current options list set.
Definition: CLCompileContext.cpp:72
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::CLL2NormalizeLayerKernel::CLL2NormalizeLayerKernel
CLL2NormalizeLayerKernel()
Default constructor.
Definition: CLL2NormalizeLayerKernel.cpp:75
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::ICLKernel::num_arguments_per_2D_tensor
constexpr static unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:301
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
arm_compute::Window::DimX
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
arm_compute::ICLTensor
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
ARM_COMPUTE_ERROR
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:353
TensorInfo.h
arm_compute::CLL2NormalizeLayerKernel::run
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
Definition: CLL2NormalizeLayerKernel.cpp:155
arm_compute::CLL2NormalizeLayerKernel::configure
void configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
Set the input and output tensors.
Definition: CLL2NormalizeLayerKernel.cpp:81
arm_compute::wrap_around
T wrap_around(T x, T m)
Wrap-around a number within the range 0 <= x < m.
Definition: Helpers.h:268
arm_compute::Window::slide_window_slice_2D
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:337
arm_compute::CLKernelLibrary::get
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Definition: CLKernelLibrary.cpp:39
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
CLKernelLibrary.h
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context.
arm_compute::ICLKernel::add_2D_tensor_argument
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:198
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
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
arm_compute::CLBuildOptions::add_option
void add_option(std::string option)
Adds option to the existing build option list.
Definition: CLCompileContext.cpp:41
arm_compute::CLCompileContext
CLCompileContext class.
Definition: CLCompileContext.h:204
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_RETURN_ERROR_ON_F16_UNSUPPORTED
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
Definition: CLValidate.h:35
arm_compute::create_kernel
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:404
arm_compute::Status
Status class.
Definition: Error.h:52
WindowHelpers.h
ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
arm_compute::Window::slide_window_slice_3D
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:349
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:579
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
arm_compute::Window::Dimension
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:79
arm_compute::Window::set
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
arm_compute::Window::DimY
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
AutoConfiguration.h
arm_compute::Window::first_slice_window_3D
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:305
CLValidate.h
arm_compute::IKernel::window
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
arm_compute::get_cl_type_from_data_type
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:40
arm_compute::ICLKernel::add_3D_tensor_argument
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:222
Utils.h
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute::ELEMENTWISE
@ ELEMENTWISE
Elementwise CL kernel type.
Definition: CLTypes.h:85
arm_compute::ICLKernel::num_arguments_per_3D_tensor
constexpr static unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:309
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::CLL2NormalizeLayerKernel::validate
static Status validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
Static function to check if given info will lead to a valid configuration of CLL2NormalizeLayerKernel...
Definition: CLL2NormalizeLayerKernel.cpp:149
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
arm_compute::DataType::F16
@ F16
16-bit floating-point number
arm_compute::adjust_vec_size
unsigned int adjust_vec_size(unsigned int vec_size, size_t dim0)
Returns the adjusted vector size in case it is less than the input's first dimension,...
Definition: AdjustVecSize.h:38
arm_compute::has_padding_changed
bool has_padding_changed(const std::unordered_map< const ITensorInfo *, PaddingSize > &padding_map)
Check if the previously stored padding info has changed after configuring a kernel.
Definition: Utils.cpp:462
arm_compute::Window::DimZ
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
arm_compute::ICLKernel::lws_hint
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:371
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
arm_compute::CLBuildOptions
Build options.
Definition: CLCompileContext.h:38
Validate.h
arm_compute::get_padding_info
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo * > infos)
Stores padding information before configuring a kernel.
Definition: Utils.cpp:447
arm_compute::Dimensions< size_t >::num_max_dimensions
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:46
arm_compute::Window::first_slice_window_2D
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:297
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486
CLHelpers.h
kernel_name
std::string kernel_name
Definition: ClIm2ColKernel.cpp:57
arm_compute::quantization::epsilon
constexpr float epsilon
Definition: AsymmHelpers.cpp:39
arm_compute::enqueue
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
Definition: ICLKernel.cpp:32