Compute Library
 21.02
CLInstanceNormalizationLayerKernel Class Reference

Interface for performing an instance normalization. More...

#include <CLInstanceNormalizationLayerKernel.h>

Collaboration diagram for CLInstanceNormalizationLayerKernel:
[legend]

Public Member Functions

 CLInstanceNormalizationLayerKernel ()
 Constructor. More...
 
 CLInstanceNormalizationLayerKernel (const CLInstanceNormalizationLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLInstanceNormalizationLayerKerneloperator= (const CLInstanceNormalizationLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLInstanceNormalizationLayerKernel (CLInstanceNormalizationLayerKernel &&)=default
 Default Move Constructor. More...
 
CLInstanceNormalizationLayerKerneloperator= (CLInstanceNormalizationLayerKernel &&)=default
 Default move assignment operator. More...
 
 ~CLInstanceNormalizationLayerKernel ()=default
 Default destructor. More...
 
void configure (ICLTensor *input, ICLTensor *output, const InstanceNormalizationLayerKernelInfo &info)
 Set the input and output tensors. More...
 
void configure (const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const InstanceNormalizationLayerKernelInfo &info)
 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 *output, const InstanceNormalizationLayerKernelInfo &info)
 Static function to check if given info will lead to a valid configuration of CLInstanceNormalizationLayer. 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 an instance normalization.

Definition at line 37 of file CLInstanceNormalizationLayerKernel.h.

Constructor & Destructor Documentation

◆ CLInstanceNormalizationLayerKernel() [1/3]

Constructor.

Definition at line 74 of file CLInstanceNormalizationLayerKernel.cpp.

75  : _input(nullptr), _output(nullptr), _run_in_place(false)
76 {
77 }

◆ CLInstanceNormalizationLayerKernel() [2/3]

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

◆ CLInstanceNormalizationLayerKernel() [3/3]

Default Move Constructor.

◆ ~CLInstanceNormalizationLayerKernel()

Default destructor.

Member Function Documentation

◆ configure() [1/2]

void configure ( ICLTensor input,
ICLTensor output,
const InstanceNormalizationLayerKernelInfo info 
)

Set the input and output tensors.

Parameters
[in,out]inputSource tensor. Data types supported: F16/F32. Data layout supported: NCHW, NHWC In case of output tensor = nullptr this tensor will store the result of the normalization.
[out]outputDestination tensor. Data types and data layouts supported: same as input.
[in]infoKernel meta-data descriptor

Definition at line 79 of file CLInstanceNormalizationLayerKernel.cpp.

References CLKernelLibrary::get().

80 {
81  configure(CLKernelLibrary::get().get_compile_context(), input, output, info);
82 }
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
void configure(ICLTensor *input, ICLTensor *output, const InstanceNormalizationLayerKernelInfo &info)
Set the input and output tensors.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)

◆ configure() [2/2]

void configure ( const CLCompileContext compile_context,
ICLTensor input,
ICLTensor output,
const InstanceNormalizationLayerKernelInfo info 
)

Set the input and output tensors.

Parameters
[in]compile_contextThe compile context to be used.
[in,out]inputSource tensor. Data types supported: F16/F32. Data layout supported: NCHW, NHWC In case of output tensor = nullptr this tensor will store the result of the normalization.
[out]outputDestination tensor. Data types and data layouts supported: same as input.
[in]infoKernel meta-data descriptor

Definition at line 84 of file CLInstanceNormalizationLayerKernel.cpp.

References CLBuildOptions::add_option(), CLBuildOptions::add_option_if(), ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, InstanceNormalizationLayerKernelInfo::beta, arm_compute::create_kernel(), ITensorInfo::data_type(), ITensorInfo::dimension(), ITensorInfo::element_size(), InstanceNormalizationLayerKernelInfo::epsilon, arm_compute::float_to_string_with_full_precision(), InstanceNormalizationLayerKernelInfo::gamma, arm_compute::get_cl_type_from_data_type(), arm_compute::get_padding_info(), arm_compute::has_padding_changed(), ITensor::info(), arm_compute::test::validation::info, arm_compute::test::validation::input, arm_compute::NHWC, num_elems_processed_per_iteration, CLBuildOptions::options(), arm_compute::support::cpp11::to_string(), InstanceNormalizationLayerKernelInfo::use_mixed_precision, and arm_compute::validate_arguments().

85 {
87  auto padding_info = get_padding_info({ input, output });
88 
89  _input = input;
90  _output = output == nullptr ? input : output;
91 
92  _run_in_place = (output == nullptr) || (output == input);
93  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _output->info(), info));
94  const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
95 
96  CLBuildOptions build_opts;
97  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
98  build_opts.add_option("-DINTERNAL_DATA_TYPE=" + (info.use_mixed_precision ? "float" : get_cl_type_from_data_type(input->info()->data_type())));
99  build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
100  build_opts.add_option("-DDIM_X=" + support::cpp11::to_string(input->info()->dimension(0)));
101  build_opts.add_option("-DDIM_Y=" + support::cpp11::to_string(input->info()->dimension(1)));
102  build_opts.add_option("-DDIM_Z=" + support::cpp11::to_string(input->info()->dimension(2)));
103  build_opts.add_option("-DGAMMA=" + float_to_string_with_full_precision(info.gamma));
104  build_opts.add_option("-DBETA=" + float_to_string_with_full_precision(info.beta));
105  build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(info.epsilon));
106  build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
107  build_opts.add_option_if(_input->info()->data_layout() == DataLayout::NHWC, "-DNHWC");
108 
109  // Create kernel
110  _kernel = create_kernel(compile_context, "instance_normalization", build_opts.options());
111 
112  // Configure kernel window
113  auto win_config = validate_and_configure_window(_input->info(), _output->info());
114  ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
115  ICLKernel::configure_internal(std::get<1>(win_config));
117 }
std::string to_string(T &&value)
Convert integer and float values to string.
#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
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
std::string float_to_string_with_full_precision(float val)
Create a string with the float in full precision.
Definition: Utils.h:1262
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.
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:528
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Num samples, height, width, channels.
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:513
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]

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

◆ operator=() [2/2]

Default move assignment operator.

◆ 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 126 of file CLInstanceNormalizationLayerKernel.cpp.

References ICLKernel::add_4D_tensor_argument(), ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, Window::collapse(), ITensorInfo::data_layout(), ITensorInfo::dimension(), Window::DimX, Window::DimY, Window::DimZ, arm_compute::enqueue(), ITensor::info(), ICLKernel::lws_hint(), arm_compute::NCHW, Window::set(), and IKernel::window().

127 {
130 
131  Window collapsed_window = window.collapse(window, Window::DimZ);
132 
133  // We will process the planes together
134  if(_input->info()->data_layout() == DataLayout::NCHW)
135  {
136  collapsed_window.set(Window::DimX, Window::Dimension(0, 1, 1));
137  collapsed_window.set(Window::DimY, Window::Dimension(0, 1, 1));
138  }
139  else
140  {
141  collapsed_window.set(Window::DimY, Window::Dimension(0, 1, 1));
142  collapsed_window.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(3), 1));
143  }
144 
145  unsigned int idx = 0;
146  add_4D_tensor_argument(idx, _input, collapsed_window);
147  if(!_run_in_place)
148  {
149  add_4D_tensor_argument(idx, _output, collapsed_window);
150  }
151 
152  enqueue(queue, *this, collapsed_window, lws_hint());
153 }
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
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
Window collapse(const Window &full_window, size_t first, size_t last=Coordinates::num_max_dimensions) const
Collapse the dimensions between first and last.
Definition: Window.inl:111
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
Num samples, channels, height, width.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 4D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:182
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo output,
const InstanceNormalizationLayerKernelInfo info 
)
static

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

Parameters
[in]inputSource tensor info. Data types supported: F16/F32. Data layout supported: NHWC, NCHW
[in]outputDestination tensor info. Data types and data layouts supported: same as input.
[in]infoKernel meta-data descriptor
Returns
a status

Definition at line 119 of file CLInstanceNormalizationLayerKernel.cpp.

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

Referenced by CLInstanceNormalizationLayer::validate().

120 {
122  ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), (output == nullptr ? input->clone().get() : output->clone().get()))));
123  return Status{};
124 }
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
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: