Compute Library
 19.08
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 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...
 
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...
 
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...
 
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<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...
 
template<unsigned int dimension_size>
void add_tensor_argument (unsigned &idx, const ICLTensor *tensor, const Window &window)
 
- 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...
 

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 93 of file CLL2NormalizeLayerKernel.cpp.

94  : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12)
95 {
96 }

◆ 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()

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 98 of file CLL2NormalizeLayerKernel.cpp.

99 {
100  ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
101  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
102 
103  _input = input;
104  _sum = sum;
105  _output = output;
106  _actual_axis = wrap_around(axis, max_input_tensor_dim);
107  _epsilon = epsilon;
108 
109  // Set build options
110  std::set<std::string> build_opts;
111  build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
112  build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
113 
114  // Create kernel
115  std::string kernel_name;
116  unsigned int idx = 0;
117  switch(_actual_axis)
118  {
119  case 0:
120  kernel_name = "x";
121  idx = num_arguments_per_2D_tensor() * 3;
122  break;
123  case 1:
124  kernel_name = "y";
125  idx = num_arguments_per_2D_tensor() * 3;
126  break;
127  case 2:
128  kernel_name = "z";
129  idx = num_arguments_per_3D_tensor() * 3;
130  break;
131  default:
132  ARM_COMPUTE_ERROR("Axis not supported");
133  }
134  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("l2_normalize_" + kernel_name, build_opts));
135 
136  // Set epsilon argument
137  if(input->info()->data_type() == DataType::F32)
138  {
139  _kernel.setArg<cl_float>(idx, _epsilon);
140  }
141  else
142  {
143  _kernel.setArg<cl_half>(idx, _epsilon);
144  }
145 
146  // Configure kernel window
147  auto win_config = validate_and_configure_window(_input->info(), _output->info());
148  ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
149 
150  ICLKernel::configure_internal(std::get<1>(win_config));
151 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
DATA_TYPE sum(__global const DATA_TYPE *input)
Calculate sum of a vector.
std::string to_string(T &&value)
Convert integer and float values to string.
constexpr float epsilon
1 channel, 1 F32 per channel
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
T wrap_around(T x, T m)
Wrap-around a number within the range 0 <= x < m.
Definition: Helpers.h:764
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:200
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:35
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:192
std::unique_ptr< Kernel > create_kernel()
Helper function to create and return a unique_ptr pointed to a CL/GLES kernel object.
Definition: Helpers.h:86
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::test::validation::axis, arm_compute::create_kernel(), ITensorInfo::data_type(), epsilon, arm_compute::F32, CLKernelLibrary::get(), arm_compute::get_cl_type_from_data_type(), ITensor::info(), ICLKernel::num_arguments_per_2D_tensor(), ICLKernel::num_arguments_per_3D_tensor(), sum(), arm_compute::support::cpp11::to_string(), arm_compute::validate_and_configure_window(), and arm_compute::wrap_around().

Referenced by CLL2NormalizeLayer::configure().

◆ 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.

Implements ICLKernel.

Definition at line 161 of file CLL2NormalizeLayerKernel.cpp.

162 {
165 
166  Window window_sum(window);
167 
168  switch(_actual_axis)
169  {
170  case 0:
171  {
172  window_sum.set(Window::DimX, Window::Dimension(0, 0, 0));
173  Window in_slice = window.first_slice_window_2D();
174  Window sum_slice = window_sum.first_slice_window_2D();
175  do
176  {
177  unsigned int idx = 0;
178  add_2D_tensor_argument(idx, _input, in_slice);
179  add_2D_tensor_argument(idx, _sum, sum_slice);
180  add_2D_tensor_argument(idx, _output, in_slice);
181  enqueue(queue, *this, in_slice);
182  }
183  while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
184  }
185  break;
186  case 1:
187  {
188  window_sum.set(Window::DimY, Window::Dimension(0, 0, 0));
189  Window in_slice = window.first_slice_window_2D();
190  Window sum_slice = window_sum.first_slice_window_2D();
191  do
192  {
193  unsigned int idx = 0;
194  add_2D_tensor_argument(idx, _input, in_slice);
195  add_2D_tensor_argument(idx, _sum, sum_slice);
196  add_2D_tensor_argument(idx, _output, in_slice);
197  enqueue(queue, *this, in_slice);
198  }
199  while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
200  }
201  break;
202  case 2:
203  {
204  window_sum.set(Window::DimZ, Window::Dimension(0, 0, 0));
205  Window in_slice = window.first_slice_window_3D();
206  Window sum_slice = window_sum.first_slice_window_3D();
207  do
208  {
209  unsigned int idx = 0;
210  add_3D_tensor_argument(idx, _input, in_slice);
211  add_3D_tensor_argument(idx, _sum, sum_slice);
212  add_3D_tensor_argument(idx, _output, in_slice);
213  enqueue(queue, *this, in_slice);
214  }
215  while(window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice));
216  }
217  break;
218  default:
219  ARM_COMPUTE_ERROR("Not supported");
220  }
221 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:267
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
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:39
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:158
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:307
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:319
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's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:134
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:275
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940

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(), Window::set(), Window::slide_window_slice_2D(), Window::slide_window_slice_3D(), and IKernel::window().

◆ 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 153 of file CLL2NormalizeLayerKernel.cpp.

154 {
155  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon));
156  ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
157 
158  return Status{};
159 }
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
DATA_TYPE sum(__global const DATA_TYPE *input)
Calculate sum of a vector.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
constexpr float epsilon

References ARM_COMPUTE_RETURN_ON_ERROR, arm_compute::test::validation::axis, ICloneable< T >::clone(), epsilon, sum(), and arm_compute::validate_and_configure_window().

Referenced by CLL2NormalizeLayer::validate().


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