Compute Library
 19.08
CLUpsampleLayerKernel Class Reference

Interface for the UpsampleLayer kernel on OpenCL. More...

#include <CLUpsampleLayerKernel.h>

Collaboration diagram for CLUpsampleLayerKernel:
[legend]

Public Member Functions

 CLUpsampleLayerKernel ()
 Constructor. More...
 
 CLUpsampleLayerKernel (const CLUpsampleLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLUpsampleLayerKerneloperator= (const CLUpsampleLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLUpsampleLayerKernel (CLUpsampleLayerKernel &&)=default
 Default Move Constructor. More...
 
CLUpsampleLayerKerneloperator= (CLUpsampleLayerKernel &&)=default
 Default move assignment operator. More...
 
 ~CLUpsampleLayerKernel ()=default
 Default destructor. More...
 
void configure (const ICLTensor *input, ICLTensor *output, const Size2D &info, const InterpolationPolicy upsampling_policy)
 Initialise the kernel's input and output. 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 *output, const Size2D &info, const InterpolationPolicy upsampling_policy)
 Static function to check if given info will lead to a valid configuration of CLUpsampleLayerKernel. 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 the UpsampleLayer kernel on OpenCL.

Definition at line 34 of file CLUpsampleLayerKernel.h.

Constructor & Destructor Documentation

◆ CLUpsampleLayerKernel() [1/3]

Constructor.

Definition at line 39 of file CLUpsampleLayerKernel.cpp.

40  : _input(nullptr), _output(nullptr), _info(), _num_elems_processed_per_iteration_input_x()
41 {
42 }

◆ CLUpsampleLayerKernel() [2/3]

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

◆ CLUpsampleLayerKernel() [3/3]

Default Move Constructor.

◆ ~CLUpsampleLayerKernel()

~CLUpsampleLayerKernel ( )
default

Default destructor.

Member Function Documentation

◆ configure()

void configure ( const ICLTensor input,
ICLTensor output,
const Size2D info,
const InterpolationPolicy  upsampling_policy 
)

Initialise the kernel's input and output.

Parameters
[in]inputSource tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32.
[out]outputDestination tensor. Data types supported: same as input.
[in]infoContains stride information described in Size2D.
[in]upsampling_policyDefines the policy to fill the intermediate pixels.

Definition at line 66 of file CLUpsampleLayerKernel.cpp.

67 {
68  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
69  ARM_COMPUTE_UNUSED(upsampling_policy);
70 
71  _input = input;
72  _output = output;
73  _info = info;
74  _num_elems_processed_per_iteration_input_x = 1;
75 
76  const DataLayout data_layout = input->info()->data_layout();
77 
79  auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
80  output->info()->set_data_layout(data_layout);
81 
82  unsigned int num_elems_processed_per_iteration_x = 16;
83  const int output_width_x = output->info()->dimension(0);
84  const bool multi_access_x = ((output_width_x / num_elems_processed_per_iteration_x) > 0);
85 
86  // Perform validation step
87  ARM_COMPUTE_ERROR_THROW_ON(CLUpsampleLayerKernel::validate(input->info(), output->info(), info, upsampling_policy));
88 
89  Window win{};
90 
91  switch(data_layout)
92  {
93  case DataLayout::NCHW:
94  {
95  win = calculate_max_window(*output->info());
96  win.set(Window::DimY, Window::Dimension(win.y().start(), win.y().end(), info.y()));
97  if(multi_access_x)
98  {
99  _num_elems_processed_per_iteration_input_x = num_elems_processed_per_iteration_x / info.x();
100  win.set(Window::DimX, Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(), num_elems_processed_per_iteration_x), num_elems_processed_per_iteration_x));
101  }
102  break;
103  }
104  case DataLayout::NHWC:
105  {
106  win = calculate_max_window(*output->info());
107  win.set(Window::DimY, Window::Dimension(win.y().start(), win.y().end(), info.x()));
108  win.set(Window::DimZ, Window::Dimension(win.z().start(), win.z().end(), info.y()));
109  if(multi_access_x)
110  {
111  _num_elems_processed_per_iteration_input_x = num_elems_processed_per_iteration_x;
112  win.set(Window::DimX, Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(),
113  num_elems_processed_per_iteration_x),
114  num_elems_processed_per_iteration_x));
115  }
116  break;
117  }
118  default:
119  ARM_COMPUTE_ERROR("Not implemented");
120  }
121 
122  // Create kernel
123  CLBuildOptions build_opts;
124  build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
125  build_opts.add_option_if(multi_access_x, "-DVEC_SIZE_IN=" + support::cpp11::to_string(_num_elems_processed_per_iteration_input_x));
126  build_opts.add_option_if(multi_access_x, "-DVEC_SIZE_OUT=" + support::cpp11::to_string(num_elems_processed_per_iteration_x));
127  build_opts.add_option_if(multi_access_x, "-DLAST_ACCESSED_X_IN=" + support::cpp11::to_string(std::max<int>(_input->info()->dimension(0) - _num_elems_processed_per_iteration_input_x, 0)));
128  build_opts.add_option_if(multi_access_x, "-DLAST_ACCESSED_X_OUT=" + support::cpp11::to_string(std::max<int>(output_width_x - num_elems_processed_per_iteration_x, 0)));
129  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("upsample_layer_" + lower_string(string_from_data_layout(input->info()->data_layout())), build_opts.options()));
130 
131  ICLKernel::configure_internal(win);
132 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
const DataLayout data_layout
Definition: Im2Col.cpp:146
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
std::string to_string(T &&value)
Convert integer and float values to string.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:327
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps=Steps(), bool skip_border=false, BorderSize border_size=BorderSize())
Calculate the maximum window for a given tensor shape and border setting.
Definition: Helpers.cpp:28
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: Helpers.inl:201
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
auto ceil_to_multiple(S value, T divisor) -> decltype(((value+divisor - 1)/divisor) *divisor)
Computes the smallest number larger or equal to value that is a multiple of divisor.
Definition: Utils.h:66
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.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:48
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
Num samples, channels, height, width.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
const std::string & string_from_data_layout(DataLayout dl)
Convert a data layout identity into a string.
Definition: Utils.cpp:132
TensorShape compute_upsample_shape(const ITensorInfo &input, const Size2D &info)
Calculate the upsampled shape of a tensor.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Num samples, height, width, channels.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &info, const InterpolationPolicy upsampling_policy)
Static function to check if given info will lead to a valid configuration of CLUpsampleLayerKernel.
DataLayout
[DataLayout enum definition]
Definition: Types.h:114

References CLBuildOptions::add_option(), CLBuildOptions::add_option_if(), ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, ARM_COMPUTE_UNUSED, arm_compute::auto_init_if_empty(), arm_compute::calculate_max_window(), arm_compute::ceil_to_multiple(), arm_compute::misc::shape_calculator::compute_upsample_shape(), arm_compute::create_kernel(), arm_compute::test::validation::data_layout, ITensorInfo::data_layout(), ITensorInfo::data_type(), ITensorInfo::dimension(), Window::DimX, Window::DimY, Window::DimZ, CLKernelLibrary::get(), arm_compute::get_cl_type_from_data_type(), ITensor::info(), arm_compute::test::validation::info, arm_compute::lower_string(), arm_compute::NCHW, arm_compute::NHWC, CLBuildOptions::options(), arm_compute::test::validation::output_shape, Window::set(), ITensorInfo::set_data_layout(), arm_compute::string_from_data_layout(), arm_compute::support::cpp11::to_string(), and CLUpsampleLayerKernel::validate().

Referenced by CLUpsampleLayer::configure().

◆ operator=() [1/2]

CLUpsampleLayerKernel& operator= ( const CLUpsampleLayerKernel )
delete

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

◆ operator=() [2/2]

CLUpsampleLayerKernel& operator= ( CLUpsampleLayerKernel &&  )
default

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.

Implements ICLKernel.

Definition at line 134 of file CLUpsampleLayerKernel.cpp.

135 {
138 
139  Window collapsed_window = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
140  Window slice_out = collapsed_window.first_slice_window_3D();
141  Window slice_in = collapsed_window.first_slice_window_3D();
142 
143  DataLayout data_layout = _input->info()->data_layout();
144  switch(data_layout)
145  {
146  case DataLayout::NCHW:
147  slice_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _num_elems_processed_per_iteration_input_x));
148  slice_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), 1));
149  break;
150  case DataLayout::NHWC:
151  slice_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), 1));
152  slice_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), 1));
153  break;
154  default:
155  ARM_COMPUTE_ERROR("Not implemented");
156  }
157 
158  do
159  {
160  unsigned int idx = 0;
161  add_3D_tensor_argument(idx, _input, slice_in);
162  add_3D_tensor_argument(idx, _output, slice_out);
163  enqueue(queue, *this, slice_out);
164  }
165  while(collapsed_window.slide_window_slice_3D(slice_out) && collapsed_window.slide_window_slice_3D(slice_in));
166 }
#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
const DataLayout data_layout
Definition: Im2Col.cpp:146
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
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
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:54
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
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
Num samples, height, width, channels.
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
DataLayout
[DataLayout enum definition]
Definition: Types.h:114
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.

References ICLKernel::add_3D_tensor_argument(), ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, Window::collapse_if_possible(), arm_compute::test::validation::data_layout, ITensorInfo::data_layout(), ITensorInfo::dimension(), Window::DimX, Window::DimY, Window::DimZ, arm_compute::enqueue(), Window::first_slice_window_3D(), ITensor::info(), arm_compute::NCHW, arm_compute::NHWC, Window::set(), Window::slide_window_slice_3D(), and IKernel::window().

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo output,
const Size2D info,
const InterpolationPolicy  upsampling_policy 
)
static

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

Parameters
[in]inputSource tensor info. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32.
[in]outputDestination tensor info. Data types supported: same as input.
[in]infoContains stride information described in Size2D.
[in]upsampling_policyDefines the policy to fill the intermediate pixels.
Returns
a status

Definition at line 44 of file CLUpsampleLayerKernel.cpp.

45 {
47  ARM_COMPUTE_UNUSED(upsampling_policy);
48 
49  DataLayout data_layout = input->data_layout();
52 
57  ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_width) != info.x() * input->dimension(idx_width));
58  ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_height) != info.y() * input->dimension(idx_height));
59  ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.x() != 2 || info.y() != 2, "Only stride 2 is supported");
60  ARM_COMPUTE_RETURN_ERROR_ON_MSG(upsampling_policy != InterpolationPolicy::NEAREST_NEIGHBOR, "Only nearest neighbor policy supported");
62 
63  return Status{};
64 }
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
Definition: CLValidate.h:34
const DataLayout data_layout
Definition: Im2Col.cpp:146
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:494
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:791
1 channel, 1 F32 per channel
Output values are defined to match the source pixel whose center is nearest to the sample position.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:244
1 channel, 1 F16 per channel
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond,...)
If the condition is true, an error is returned.
Definition: Error.h:214
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...)
Definition: Validate.h:610
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:326
DataLayout
[DataLayout enum definition]
Definition: Types.h:114

References ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO, ARM_COMPUTE_RETURN_ERROR_ON_MSG, ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR, ARM_COMPUTE_UNUSED, arm_compute::test::validation::data_layout, ITensorInfo::data_layout(), ITensorInfo::dimension(), arm_compute::F16, arm_compute::F32, arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, arm_compute::test::validation::info, arm_compute::NEAREST_NEIGHBOR, and arm_compute::WIDTH.

Referenced by CLUpsampleLayerKernel::configure(), and CLUpsampleLayer::validate().


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