Compute Library
 20.02.1
CLScaleKernel Class Reference

Interface for the scale kernel. More...

#include <CLScaleKernel.h>

Collaboration diagram for CLScaleKernel:
[legend]

Public Member Functions

void configure (const ICLTensor *input, ICLTensor *output, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy=SamplingPolicy::CENTER, bool align_corners=false)
 Initialise the kernel's inputs, output and interpolation policy. More...
 
const ICLTensorinput () const
 Input tensor accessor. More...
 
const ICLTensoroutput () const
 Output tensor accessor. More...
 
BorderSize border_size () const override
 The size of the border for that kernel. 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 ICLSimpleKernel
 ICLSimpleKernel ()
 Constructor. More...
 
 ICLSimpleKernel (const ICLSimpleKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
ICLSimpleKerneloperator= (const ICLSimpleKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 ICLSimpleKernel (ICLSimpleKernel &&)=default
 Allow instances of this class to be moved. More...
 
ICLSimpleKerneloperator= (ICLSimpleKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~ICLSimpleKernel ()=default
 Default destructor. More...
 
void configure (const ICLTensor *input, ICLTensor *output, unsigned int num_elems_processed_per_iteration, bool border_undefined=false, const BorderSize &border_size=BorderSize())
 Configure the kernel. 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...
 
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, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy=SamplingPolicy::CENTER, bool align_corners=false)
 Static function to check if given info will lead to a valid configuration of CLScaleKernel. 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...
 

Data Fields

InterpolationPolicy _interpolationPolicy = InterpolationPolicy::BILINEAR
 
DataLayout _data_layout = DataLayout::UNKNOWN
 
bool _align_corners = false
 

Detailed Description

Interface for the scale kernel.

Definition at line 35 of file CLScaleKernel.h.

Member Function Documentation

◆ border_size()

BorderSize border_size ( ) const
overridevirtual

The size of the border for that kernel.

Returns
The width in number of elements of the border.

Reimplemented from IKernel.

Definition at line 154 of file CLScaleKernel.cpp.

155 {
156  return BorderSize(1);
157 }
Container for 2D border size.
Definition: Types.h:269

Referenced by CLScaleKernel::configure().

◆ configure()

void configure ( const ICLTensor input,
ICLTensor output,
InterpolationPolicy  policy,
BorderMode  border_mode,
SamplingPolicy  sampling_policy = SamplingPolicy::CENTER,
bool  align_corners = false 
)

Initialise the kernel's inputs, output and interpolation policy.

Parameters
[in]inputSource tensor. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/F16/F32
[out]outputDestination tensor. Data types supported: Same as input All but the lowest two dimensions must be the same size as in the input tensor, i.e. scaling is only performed within the XY-plane.
[in]policyInterpolation type to use
[in]border_modeSelected border mode.
[in]sampling_policy(Optional) Sampling policy used by the interpolation. Defaults to SamplingPolicy::CENTER
[in]align_corners(Optional) Align corners of input and output, only affecting bilinear policy with TOP_LEFT sampling policy. Defaults to false.

Definition at line 182 of file CLScaleKernel.cpp.

183 {
185  && sampling_policy == SamplingPolicy::TOP_LEFT
186  && align_corners;
187 
188  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), policy, _align_corners));
189 
190  _input = input;
191  _output = output;
194 
195  float wr = 0.f;
196  float hr = 0.f;
197  std::tie(wr, hr) = calculate_scale_factors(*input->info(), *output->info(), _align_corners);
198 
199  const bool call_quantized_kernel = is_data_type_quantized_asymmetric(input->info()->data_type()) && policy == InterpolationPolicy::BILINEAR;
200 
203  const bool is_nhwc = _data_layout == DataLayout::NHWC;
204 
205  // Compute actual border size
206  BorderSize border = border_size();
207 
208  // Area interpolation behaves as Nearest Neighbour in case of up-sampling
209  if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
210  {
212  }
213 
214  // Configure kernel window
215  auto win_config = validate_and_configure_window(input->info(), output->info(), policy, border_mode, sampling_policy, border);
216  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
217  ICLKernel::configure_internal(win_config.second);
218 
219  // Create kernel
220  CLBuildOptions build_opts;
221  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
222  build_opts.add_option("-DBORDER_SIZE=" + support::cpp11::to_string(border.right));
223  build_opts.add_option_if(border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
224  build_opts.add_option_if(is_nhwc, "-DDEPTH_OUT=" + support::cpp11::to_string(output->info()->dimension(2)));
225  build_opts.add_option_if_else(sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
226  if(call_quantized_kernel)
227  {
229  build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
230  build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
231  }
232 
233  std::string interpolation_name = string_from_interpolation_policy(policy);
234  std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
235  std::string kernel_name = "scale_" + interpolation_name;
236  kernel_name += call_quantized_kernel ? "_quantized_" : "_";
238  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
239 
240  unsigned int idx = is_nhwc ? 2 * num_arguments_per_4D_tensor() : 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
241 
242  const unsigned int input_width = input->info()->dimension(idx_width);
243  const unsigned int input_height = input->info()->dimension(idx_height);
244 
245  _kernel.setArg<float>(idx++, input_width);
246  _kernel.setArg<float>(idx++, input_height);
247  _kernel.setArg<float>(idx++, wr);
248  _kernel.setArg<float>(idx++, hr);
249 
250  // Set config_id for enabling LWS tuning
251  _config_id = "scale_";
252  _config_id += (border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
253  _config_id += (sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
254  _config_id += (is_nhwc ? "nhwc" : "nchw");
255  _config_id += "_";
256  _config_id += support::cpp11::to_string(output->info()->dimension(0));
257  _config_id += "_";
258  _config_id += support::cpp11::to_string(output->info()->dimension(1));
259  _config_id += "_";
260  _config_id += support::cpp11::to_string(output->info()->dimension(2));
261  _config_id += "_";
262  _config_id += support::cpp11::to_string(output->info()->dimension(3));
263 }
const std::vector< int32_t > & offset() const
Offset vector accessor.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
Container for 2D border size.
Definition: Types.h:269
BorderSize border_size() const override
The size of the border for that kernel.
const StringSet & options() const
Gets the current options list set.
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
Output values are defined by bilinear interpolation between the pixels.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Quantization info when assuming per layer quantization.
Output values are defined to match the source pixel whose center is nearest to the sample position.
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:333
Samples are taken at pixel center.
void add_option(std::string option)
Adds option to the existing build option list.
std::string tolower(std::string string)
Convert string to lower case.
Definition: Utility.h:203
std::string kernel_name
const std::string & string_from_interpolation_policy(InterpolationPolicy policy)
Translates a given interpolation policy to a string.
Definition: Utils.cpp:219
UniformQuantizationInfo uniform() const
Return per layer quantization info.
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's metadata.
Samples are taken at pixel top left corner.
const std::vector< float > & scale() const
Scale vector accessor.
void add_option_if(bool cond, std::string option)
Adds option if a given condition is true;.
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
static constexpr unsigned int num_arguments_per_4D_tensor()
Returns the number of arguments enqueued per 4D tensor object.
Definition: ICLKernel.h:208
const ICLTensor * output() const
Output tensor accessor.
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
unsigned int right
right of the border
Definition: Types.h:350
Output values are determined by averaging the source pixels whose areas fall under the area of the de...
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1139
const std::string & string_from_data_layout(DataLayout dl)
Convert a data layout identity into a string.
Definition: Utils.cpp:132
Pixels outside the image are assumed to have the same value as the closest image pixel.
const QuantizationInfo qinfo
Definition: Im2Col.cpp:150
Num samples, height, width, channels.
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:327
const ICLTensor * input() const
Input tensor accessor.
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
InterpolationPolicy _interpolationPolicy
Definition: CLScaleKernel.h:80
void add_option_if_else(bool cond, std::string option_true, std::string option_false)
Adds first option if condition is true else the second one.

References CLScaleKernel::_align_corners, CLScaleKernel::_data_layout, CLScaleKernel::_interpolationPolicy, CLBuildOptions::add_option(), CLBuildOptions::add_option_if(), CLBuildOptions::add_option_if_else(), arm_compute::AREA, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::BILINEAR, arm_compute::test::validation::border_mode, CLScaleKernel::border_size(), arm_compute::CENTER, arm_compute::create_kernel(), ITensorInfo::data_layout(), ITensorInfo::data_type(), ITensorInfo::dimension(), CLKernelLibrary::get(), arm_compute::get_cl_type_from_data_type(), arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, ITensor::info(), CLScaleKernel::input(), arm_compute::is_data_type_quantized_asymmetric(), kernel_name, arm_compute::lower_string(), arm_compute::NEAREST_NEIGHBOR, arm_compute::NHWC, ICLKernel::num_arguments_per_2D_tensor(), ICLKernel::num_arguments_per_4D_tensor(), QuantizationInfo::offset(), CLBuildOptions::options(), CLScaleKernel::output(), arm_compute::test::validation::policy, arm_compute::test::validation::qinfo, ITensorInfo::quantization_info(), arm_compute::REPLICATE, BorderSize::right, QuantizationInfo::scale(), arm_compute::string_from_data_layout(), arm_compute::string_from_interpolation_policy(), arm_compute::support::cpp11::to_string(), arm_compute::utility::tolower(), arm_compute::TOP_LEFT, QuantizationInfo::uniform(), and arm_compute::WIDTH.

◆ input()

const ICLTensor * input ( ) const

Input tensor accessor.

Returns
Pointer to input tensor.

Definition at line 172 of file CLScaleKernel.cpp.

173 {
174  return _input;
175 }

Referenced by CLScaleKernel::configure(), and CLScaleKernel::validate().

◆ output()

const ICLTensor * output ( ) const

Output tensor accessor.

Returns
Pointer to output tensor.

Definition at line 177 of file CLScaleKernel.cpp.

178 {
179  return _output;
180 }

Referenced by CLScaleKernel::configure(), and CLScaleKernel::validate().

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

Definition at line 265 of file CLScaleKernel.cpp.

266 {
269 
270  switch(_data_layout)
271  {
272  case DataLayout::NCHW:
273  {
275 
276  do
277  {
278  unsigned int idx = 0;
279  add_2D_tensor_argument(idx, _input, slice);
280  add_2D_tensor_argument(idx, _output, slice);
281  enqueue(queue, *this, slice, lws_hint());
282  }
284  break;
285  }
286  case DataLayout::NHWC:
287  {
289  Window slice = collapsed.first_slice_window_4D();
290 
291  unsigned int idx = 0;
292  add_4D_tensor_argument(idx, _input, slice);
293  add_4D_tensor_argument(idx, _output, slice);
294  enqueue(queue, *this, slice, lws_hint());
295  break;
296  }
297  default:
298  ARM_COMPUTE_ERROR("Data layout not supported");
299  }
300 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:281
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
#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:247
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:321
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
Num samples, channels, height, width.
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
Num samples, height, width, channels.
Window first_slice_window_4D() const
First 4D slice of the window.
Definition: Window.h:297
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
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.
Definition: ICLKernel.h:168
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)

References CLScaleKernel::_data_layout, ICLKernel::add_2D_tensor_argument(), ICLKernel::add_4D_tensor_argument(), ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, Window::collapse(), Window::DimZ, arm_compute::enqueue(), Window::first_slice_window_2D(), Window::first_slice_window_4D(), ICLKernel::lws_hint(), arm_compute::NCHW, arm_compute::NHWC, arm_compute::test::validation::reference::slice(), Window::slide_window_slice_2D(), and IKernel::window().

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo output,
InterpolationPolicy  policy,
BorderMode  border_mode,
SamplingPolicy  sampling_policy = SamplingPolicy::CENTER,
bool  align_corners = false 
)
static

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

Parameters
[in]inputSource tensor info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/F16/F32
[in]outputDestination tensor info. Data types supported: Same as input All but the lowest two dimensions must be the same size as in the input tensor, i.e. scaling is only performed within the XY-plane.
[in]policyInterpolation type to use
[in]border_modeSelected border mode.
[in]sampling_policy(Optional) Sampling policy used by the interpolation. Defaults to SamplingPolicy::CENTER
[in]align_corners(Optional) Align corners of input and output, only affecting bilinear policy with TOP_LEFT sampling policy. Defaults to false.
Returns
a status

Definition at line 159 of file CLScaleKernel.cpp.

161 {
162  BorderSize border = BorderSize(1);
163  const bool is_align_corners = policy == InterpolationPolicy::BILINEAR
164  && sampling_policy == SamplingPolicy::TOP_LEFT
165  && align_corners;
166  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, policy, is_align_corners));
167  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), policy, border_mode, sampling_policy, border).first);
168 
169  return Status{};
170 }
Container for 2D border size.
Definition: Types.h:269
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
Output values are defined by bilinear interpolation between the pixels.
Status class.
Definition: Error.h:52
Samples are taken at pixel top left corner.
const ICLTensor * output() const
Output tensor accessor.
const ICLTensor * input() const
Input tensor accessor.

References ARM_COMPUTE_RETURN_ON_ERROR, arm_compute::BILINEAR, arm_compute::test::validation::border_mode, CLScaleKernel::input(), CLScaleKernel::output(), arm_compute::test::validation::policy, and arm_compute::TOP_LEFT.

Referenced by CLScale::validate().

Field Documentation

◆ _align_corners

bool _align_corners = false

Definition at line 82 of file CLScaleKernel.h.

Referenced by CLScaleKernel::configure().

◆ _data_layout

Definition at line 81 of file CLScaleKernel.h.

Referenced by CLScaleKernel::configure(), and CLScaleKernel::run().

◆ _interpolationPolicy

Definition at line 80 of file CLScaleKernel.h.

Referenced by CLScaleKernel::configure().


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