Compute Library
 21.11
CLL2NormalizeLayerKernel Class Reference

Interface for performing a L2 normalize on a given axis given the square sum of it in this axis. More...

#include <CLL2NormalizeLayerKernel.h>

Collaboration diagram for CLL2NormalizeLayerKernel:
[legend]

Public Member Functions

 CLL2NormalizeLayerKernel ()
 Default constructor. More...
 
 CLL2NormalizeLayerKernel (const CLL2NormalizeLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLL2NormalizeLayerKerneloperator= (const CLL2NormalizeLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLL2NormalizeLayerKernel (CLL2NormalizeLayerKernel &&)=default
 Allow instances of this class to be moved. More...
 
CLL2NormalizeLayerKerneloperator= (CLL2NormalizeLayerKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~CLL2NormalizeLayerKernel ()=default
 Default destructor. More...
 
void configure (const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
 Set the input and output tensors. More...
 
void configure (const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
 Set the input and output tensors. More...
 
void run (const Window &window, cl::CommandQueue &queue) override
 Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue. More...
 
- Public Member Functions inherited from ICLKernel
 ICLKernel ()
 Constructor. More...
 
cl::Kernel & kernel ()
 Returns a reference to the OpenCL kernel of this object. More...
 
CLKernelType type () const
 Returns the CL kernel type. More...
 
template<typename T >
void add_1D_array_argument (unsigned int &idx, const ICLArray< T > *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
 Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_1D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_1D_tensor_argument_if (bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true. More...
 
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. More...
 
void add_2D_tensor_argument_if (bool cond, 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 if the condition is true. More...
 
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. More...
 
void add_4D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
virtual void run_op (ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
 Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue. More...
 
template<typename T >
void add_argument (unsigned int &idx, T value)
 Add the passed parameters to the object's kernel's arguments starting from the index idx. More...
 
void set_lws_hint (const cl::NDRange &lws_hint)
 Set the Local-Workgroup-Size hint. More...
 
cl::NDRange lws_hint () const
 Return the Local-Workgroup-Size hint. More...
 
void set_wbsm_hint (const cl_int &wbsm_hint)
 Set the workgroup batch size modifier hint. More...
 
cl_int wbsm_hint () const
 Return the workgroup batch size modifier hint. More...
 
const std::string & config_id () const
 Get the configuration ID. More...
 
void set_target (GPUTarget target)
 Set the targeted GPU architecture. More...
 
void set_target (cl::Device &device)
 Set the targeted GPU architecture according to the CL device. More...
 
GPUTarget get_target () const
 Get the targeted GPU architecture. More...
 
size_t get_max_workgroup_size ()
 Get the maximum workgroup size for the device the CLKernelLibrary uses. More...
 
template<unsigned int dimension_size>
void add_tensor_argument (unsigned &idx, const ICLTensor *tensor, const Window &window)
 
template<typename T , unsigned int dimension_size>
void add_array_argument (unsigned &idx, const ICLArray< T > *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
 Add the passed array's parameters to the object's kernel's arguments starting from the index idx. More...
 
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
 
virtual ~IKernel ()=default
 Destructor. More...
 
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
 
virtual BorderSize border_size () const
 The size of the border for that kernel. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 
bool is_window_configured () const
 Function to check if the embedded window of this kernel has been configured. More...
 

Static Public Member Functions

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. More...
 
- Static Public Member Functions inherited from ICLKernel
static constexpr unsigned int num_arguments_per_1D_array ()
 Returns the number of arguments enqueued per 1D array object. More...
 
static constexpr unsigned int num_arguments_per_1D_tensor ()
 Returns the number of arguments enqueued per 1D tensor object. More...
 
static constexpr unsigned int num_arguments_per_2D_tensor ()
 Returns the number of arguments enqueued per 2D tensor object. More...
 
static constexpr unsigned int num_arguments_per_3D_tensor ()
 Returns the number of arguments enqueued per 3D tensor object. More...
 
static constexpr unsigned int num_arguments_per_4D_tensor ()
 Returns the number of arguments enqueued per 4D tensor object. More...
 
static cl::NDRange gws_from_window (const Window &window)
 Get the global work size given an execution window. More...
 

Detailed Description

Interface for performing a L2 normalize on a given axis given the square sum of it in this axis.

Definition at line 35 of file CLL2NormalizeLayerKernel.h.

Constructor & Destructor Documentation

◆ CLL2NormalizeLayerKernel() [1/3]

Default constructor.

Definition at line 74 of file CLL2NormalizeLayerKernel.cpp.

References arm_compute::ELEMENTWISE.

75  : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12)
76 {
78 }
Elementeise CL kernel type.
Definition: CLTypes.h:84

◆ CLL2NormalizeLayerKernel() [2/3]

Prevent instances of this class from being copied (As this class contains pointers)

◆ CLL2NormalizeLayerKernel() [3/3]

Allow instances of this class to be moved.

◆ ~CLL2NormalizeLayerKernel()

Default destructor.

Member Function Documentation

◆ configure() [1/2]

void configure ( const ICLTensor input,
const ICLTensor sum,
ICLTensor output,
int  axis,
float  epsilon 
)

Set the input and output tensors.

Parameters
[in]inputSource tensor. Data types supported: F16/F32. Data layouts supported: NCHW/NHWC.
[in]sumSum values tensor. Data types supported: same as input. Sum will have the same number of dimensions as input.
[out]outputDestination tensor. Data types and data layouts supported: Same as input. Output will have the same number of dimensions as input.
[in]axisAxis along which to reduce. Negative values wrap around. Maximum supported actual reduction axis : 2
[in]epsilonLower bound value for the normalization.

Definition at line 80 of file CLL2NormalizeLayerKernel.cpp.

References CLKernelLibrary::get().

81 {
82  configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, axis, epsilon);
83 }
void configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
Set the input and output tensors.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.

◆ configure() [2/2]

void configure ( const CLCompileContext compile_context,
const ICLTensor input,
const ICLTensor sum,
ICLTensor output,
int  axis,
float  epsilon 
)

Set the input and output tensors.

Parameters
[in]compile_contextThe compile context to be used.
[in]inputSource tensor. Data types supported: F16/F32. Data layouts supported: NCHW/NHWC.
[in]sumSum values tensor. Data types supported: same as input. Sum will have the same number of dimensions as input.
[out]outputDestination tensor. Data types and data layouts supported: Same as input. Output will have the same number of dimensions as input.
[in]axisAxis along which to reduce. Negative values wrap around. Maximum supported actual reduction axis : 2
[in]epsilonLower bound value for the normalization.

Definition at line 85 of file CLL2NormalizeLayerKernel.cpp.

References CLBuildOptions::add_option(), arm_compute::adjust_vec_size(), ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::auto_init_if_empty(), arm_compute::calculate_max_window(), arm_compute::create_kernel(), ITensorInfo::data_type(), ITensorInfo::dimension(), ITensorInfo::element_size(), arm_compute::quantization::epsilon, arm_compute::F32, arm_compute::get_cl_type_from_data_type(), arm_compute::get_padding_info(), arm_compute::has_padding_changed(), ITensor::info(), arm_compute::test::validation::input, kernel_name, ICLKernel::num_arguments_per_2D_tensor(), ICLKernel::num_arguments_per_3D_tensor(), CLBuildOptions::options(), ITensorInfo::tensor_shape(), arm_compute::support::cpp11::to_string(), and arm_compute::wrap_around().

86 {
87  ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
88  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
89  auto padding_info = get_padding_info({ input, sum, output });
90 
91  _input = input;
92  _sum = sum;
93  _output = output;
94  _actual_axis = wrap_around(axis, max_input_tensor_dim);
95  _epsilon = epsilon;
96 
97  const unsigned int vec_size_x = adjust_vec_size(max_cl_vector_width / input->info()->element_size(), input->info()->dimension(0));
98  const int vec_size_x_leftovers = input->info()->dimension(0) % vec_size_x;
99 
100  // Set build options
101  CLBuildOptions build_opts;
102  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
103  build_opts.add_option("-DVEC_SIZE_X=" + support::cpp11::to_string(vec_size_x));
104  build_opts.add_option("-DVEC_SIZE_LEFTOVER_X=" + support::cpp11::to_string(vec_size_x_leftovers));
105 
106  // Create kernel
107  std::string kernel_name;
108  unsigned int idx = 0;
109  switch(_actual_axis)
110  {
111  case 0:
112  kernel_name = "l2_normalize_x";
113  idx = num_arguments_per_2D_tensor() * 3;
114  break;
115  case 1:
116  kernel_name = "l2_normalize_y";
117  idx = num_arguments_per_2D_tensor() * 3;
118  break;
119  case 2:
120  kernel_name = "l2_normalize_z";
121  idx = num_arguments_per_3D_tensor() * 3;
122  break;
123  default:
124  ARM_COMPUTE_ERROR("Axis not supported");
125  }
126  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
127 
128  // Set epsilon argument
129  if(input->info()->data_type() == DataType::F32)
130  {
131  _kernel.setArg<cl_float>(idx, _epsilon);
132  }
133  else
134  {
135  _kernel.setArg<cl_half>(idx, _epsilon);
136  }
137 
138  // Configure kernel window
139  Window win = calculate_max_window(*input->info(), Steps(vec_size_x));
140 
141  // Output tensor auto initialization if not yet initialized
142  auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
143 
144  ICLKernel::configure_internal(win);
146 }
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
std::string to_string(T &&value)
Convert integer and float values to string.
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
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
T wrap_around(T x, T m)
Wrap-around a number within the range 0 <= x < m.
Definition: Helpers.h:247
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:391
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:256
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:39
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...
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:248
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:533
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:518
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
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&#39;s first dimension, getting rounded down to its closest valid vector size.
Definition: Utils.h:1171
std::string kernel_name

◆ operator=() [1/2]

CLL2NormalizeLayerKernel& operator= ( const CLL2NormalizeLayerKernel )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ operator=() [2/2]

Allow instances of this class to be moved.

◆ run()

void run ( const Window window,
cl::CommandQueue &  queue 
)
overridevirtual

Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.

Note
The queue is not flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
Parameters
[in]windowRegion on which to execute the kernel. (Must be a valid region of the window returned by window()).
[in,out]queueCommand queue on which to enqueue the kernel.

Reimplemented from ICLKernel.

Definition at line 154 of file CLL2NormalizeLayerKernel.cpp.

References ICLKernel::add_2D_tensor_argument(), ICLKernel::add_3D_tensor_argument(), ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, Window::DimX, Window::DimY, Window::DimZ, arm_compute::enqueue(), Window::first_slice_window_2D(), Window::first_slice_window_3D(), ICLKernel::lws_hint(), Window::set(), Window::slide_window_slice_2D(), Window::slide_window_slice_3D(), and IKernel::window().

155 {
158 
159  Window window_sum(window);
160 
161  switch(_actual_axis)
162  {
163  case 0:
164  {
165  window_sum.set(Window::DimX, Window::Dimension(0, 0, 0));
166  Window in_slice = window.first_slice_window_2D();
167  Window sum_slice = window_sum.first_slice_window_2D();
168  do
169  {
170  unsigned int idx = 0;
171  add_2D_tensor_argument(idx, _input, in_slice);
172  add_2D_tensor_argument(idx, _sum, sum_slice);
173  add_2D_tensor_argument(idx, _output, in_slice);
174  enqueue(queue, *this, in_slice, lws_hint());
175  }
176  while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
177  }
178  break;
179  case 1:
180  {
181  window_sum.set(Window::DimY, Window::Dimension(0, 0, 0));
182  Window in_slice = window.first_slice_window_2D();
183  Window sum_slice = window_sum.first_slice_window_2D();
184  do
185  {
186  unsigned int idx = 0;
187  add_2D_tensor_argument(idx, _input, in_slice);
188  add_2D_tensor_argument(idx, _sum, sum_slice);
189  add_2D_tensor_argument(idx, _output, in_slice);
190  enqueue(queue, *this, in_slice, lws_hint());
191  }
192  while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
193  }
194  break;
195  case 2:
196  {
197  window_sum.set(Window::DimZ, Window::Dimension(0, 0, 0));
198  Window in_slice = window.first_slice_window_3D();
199  Window sum_slice = window_sum.first_slice_window_3D();
200  do
201  {
202  unsigned int idx = 0;
203  add_3D_tensor_argument(idx, _input, in_slice);
204  add_3D_tensor_argument(idx, _sum, sum_slice);
205  add_3D_tensor_argument(idx, _output, in_slice);
206  enqueue(queue, *this, in_slice, lws_hint());
207  }
208  while(window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice));
209  }
210  break;
211  default:
212  ARM_COMPUTE_ERROR("Not supported");
213  }
214 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
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
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:318
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:214
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:323
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:335
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:190
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo sum,
const ITensorInfo output,
int  axis,
float  epsilon 
)
static

Static function to check if given info will lead to a valid configuration of CLL2NormalizeLayerKernel.

Parameters
[in]inputSource tensor info. Data types supported: F16/F32. Data layouts supported: NCHW/NHWC.
[in]sumSum values tensor info. Data types supported: same as input. Sum will have the same number of dimensions as input.
[in]outputDestination tensor info. Data types and data layouts supported: Same as input. Output will have the same number of dimensions as input.
[in]axisAxis along which to reduce. Negative values wrap around. Maximum supported actual reduction axis : 2
[in]epsilonLower bound value for the normalization.
Returns
a status

Definition at line 148 of file CLL2NormalizeLayerKernel.cpp.

References ARM_COMPUTE_RETURN_ON_ERROR.

Referenced by CLL2NormalizeLayer::validate().

149 {
150  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon));
151  return Status{};
152 }
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204

The documentation for this class was generated from the following files: