Compute Library
 21.02
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...
 
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...
 

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

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

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

References CLKernelLibrary::get().

100 {
101  configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, axis, epsilon);
102 }
DATA_TYPE sum(__global const DATA_TYPE *input)
Calculate sum of a vector.
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 104 of file CLL2NormalizeLayerKernel.cpp.

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::create_kernel(), ITensorInfo::data_type(), arm_compute::quantization::epsilon, arm_compute::F32, arm_compute::get_cl_type_from_data_type(), ITensor::info(), arm_compute::test::validation::input, kernel_name, ICLKernel::num_arguments_per_2D_tensor(), ICLKernel::num_arguments_per_3D_tensor(), sum(), arm_compute::support::cpp11::to_string(), arm_compute::validate_arguments(), and arm_compute::wrap_around().

105 {
107  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
108 
109  _input = input;
110  _sum = sum;
111  _output = output;
112  _actual_axis = wrap_around(axis, max_input_tensor_dim);
113  _epsilon = epsilon;
114 
115  // Set build options
116  std::set<std::string> build_opts;
117  build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
118  build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
119 
120  // Create kernel
121  std::string kernel_name;
122  unsigned int idx = 0;
123  switch(_actual_axis)
124  {
125  case 0:
126  kernel_name = "x";
127  idx = num_arguments_per_2D_tensor() * 3;
128  break;
129  case 1:
130  kernel_name = "y";
131  idx = num_arguments_per_2D_tensor() * 3;
132  break;
133  case 2:
134  kernel_name = "z";
135  idx = num_arguments_per_3D_tensor() * 3;
136  break;
137  default:
138  ARM_COMPUTE_ERROR("Axis not supported");
139  }
140  _kernel = create_kernel(compile_context, "l2_normalize_" + kernel_name, build_opts);
141 
142  // Set epsilon argument
143  if(input->info()->data_type() == DataType::F32)
144  {
145  _kernel.setArg<cl_float>(idx, _epsilon);
146  }
147  else
148  {
149  _kernel.setArg<cl_half>(idx, _epsilon);
150  }
151 
152  // Configure kernel window
153  auto win_config = validate_and_configure_window(_input->info(), _output->info());
154  ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
155 
156  ICLKernel::configure_internal(std::get<1>(win_config));
157 }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
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.
1 channel, 1 F32 per channel
#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:231
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:403
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:214
std::string kernel_name
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:37
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:206
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
unsigned int num_elems_processed_per_iteration
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161

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

168 {
171 
172  Window window_sum(window);
173 
174  switch(_actual_axis)
175  {
176  case 0:
177  {
178  window_sum.set(Window::DimX, Window::Dimension(0, 0, 0));
179  Window in_slice = window.first_slice_window_2D();
180  Window sum_slice = window_sum.first_slice_window_2D();
181  do
182  {
183  unsigned int idx = 0;
184  add_2D_tensor_argument(idx, _input, in_slice);
185  add_2D_tensor_argument(idx, _sum, sum_slice);
186  add_2D_tensor_argument(idx, _output, in_slice);
187  enqueue(queue, *this, in_slice, lws_hint());
188  }
189  while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
190  }
191  break;
192  case 1:
193  {
194  window_sum.set(Window::DimY, Window::Dimension(0, 0, 0));
195  Window in_slice = window.first_slice_window_2D();
196  Window sum_slice = window_sum.first_slice_window_2D();
197  do
198  {
199  unsigned int idx = 0;
200  add_2D_tensor_argument(idx, _input, in_slice);
201  add_2D_tensor_argument(idx, _sum, sum_slice);
202  add_2D_tensor_argument(idx, _output, in_slice);
203  enqueue(queue, *this, in_slice, lws_hint());
204  }
205  while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
206  }
207  break;
208  case 2:
209  {
210  window_sum.set(Window::DimZ, Window::Dimension(0, 0, 0));
211  Window in_slice = window.first_slice_window_3D();
212  Window sum_slice = window_sum.first_slice_window_3D();
213  do
214  {
215  unsigned int idx = 0;
216  add_3D_tensor_argument(idx, _input, in_slice);
217  add_3D_tensor_argument(idx, _sum, sum_slice);
218  add_3D_tensor_argument(idx, _output, in_slice);
219  enqueue(queue, *this, in_slice, lws_hint());
220  }
221  while(window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice));
222  }
223  break;
224  default:
225  ARM_COMPUTE_ERROR("Not supported");
226  }
227 }
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(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
Definition: IGCKernel.cpp:41
#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:276
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:172
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:941
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:148
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:205

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

References ARM_COMPUTE_RETURN_ON_ERROR, ICloneable< T >::clone(), and arm_compute::validate_arguments().

Referenced by CLL2NormalizeLayer::validate().

160 {
162  ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
163 
164  return Status{};
165 }
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:204
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)

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