Compute Library
 19.08
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)
 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)
 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
 

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 136 of file CLScaleKernel.cpp.

137 {
138  return BorderSize(1);
139 }
Container for 2D border size.
Definition: Types.h:259

Referenced by CLScaleKernel::configure().

◆ configure()

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

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

Parameters
[in]inputSource tensor. Data types supported: U8/QASYMM8/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

Definition at line 161 of file CLScaleKernel.cpp.

162 {
163  _input = input;
164  _output = output;
166 
167  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), policy));
168 
169  float wr = 0.f;
170  float hr = 0.f;
171  std::tie(wr, hr) = calculate_scale_factors(*input->info(), *output->info());
172 
173  const bool call_quantized_kernel = is_data_type_quantized_asymmetric(input->info()->data_type()) && policy == InterpolationPolicy::BILINEAR;
174 
178  const bool is_nhwc = data_layout == DataLayout::NHWC;
179 
180  // Compute the ratio between source width/height and destination width/height
181  const unsigned int input_width = input->info()->dimension(idx_width);
182  const unsigned int input_height = input->info()->dimension(idx_height);
183  const unsigned int output_width = output->info()->dimension(idx_width);
184  const unsigned int output_height = output->info()->dimension(idx_height);
185 
186  // Compute actual border size
187  BorderSize border = border_size();
188 
189  // Area interpolation behaves as Nearest Neighbour in case of up-sampling
190  if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
191  {
193  }
194 
195  // Configure kernel window
196  auto win_config = validate_and_configure_window(input->info(), output->info(), policy, border_mode, sampling_policy, border);
197  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
198  ICLKernel::configure_internal(win_config.second);
199 
200  // Create kernel
201  CLBuildOptions build_opts;
202  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
203  build_opts.add_option("-DBORDER_SIZE=" + support::cpp11::to_string(border.right));
204  build_opts.add_option_if(border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
205  build_opts.add_option_if(is_nhwc, "-DDEPTH_OUT=" + support::cpp11::to_string(output->info()->dimension(2)));
206  build_opts.add_option_if_else(sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
207  if(call_quantized_kernel)
208  {
210  build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
211  build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
212  }
213 
214  std::string interpolation_name = string_from_interpolation_policy(policy);
215  std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
216  std::string kernel_name = "scale_" + interpolation_name;
217  kernel_name += call_quantized_kernel ? "_quantized_" : "_";
219  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
220 
221  unsigned int idx = is_nhwc ? 2 * num_arguments_per_4D_tensor() : 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
222 
223  // Set static kernel arguments
224  const float scale_x = static_cast<float>(input_width) / output_width;
225  const float scale_y = static_cast<float>(input_height) / output_height;
226 
227  _kernel.setArg<float>(idx++, input_width);
228  _kernel.setArg<float>(idx++, input_height);
229  _kernel.setArg<float>(idx++, scale_x);
230  _kernel.setArg<float>(idx++, scale_y);
231 
232  // Set config_id for enabling LWS tuning
233  _config_id = "scale_";
234  _config_id += (border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
235  _config_id += (sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
236  _config_id += (is_nhwc ? "nhwc" : "nchw");
237  _config_id += "_";
238  _config_id += support::cpp11::to_string(output->info()->dimension(0));
239  _config_id += "_";
240  _config_id += support::cpp11::to_string(output->info()->dimension(1));
241  _config_id += "_";
242  _config_id += support::cpp11::to_string(output->info()->dimension(2));
243  _config_id += "_";
244  _config_id += support::cpp11::to_string(output->info()->dimension(3));
245 }
const std::vector< int32_t > & offset() const
Offset vector accessor.
const DataLayout data_layout
Definition: Im2Col.cpp:146
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
Container for 2D border size.
Definition: Types.h:259
BorderSize border_size() const override
The size of the border for that kernel.
const StringSet & options() const
Gets the current options list set.
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)
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:327
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:327
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:188
const std::string & string_from_interpolation_policy(InterpolationPolicy policy)
Translates a given interpolation policy to a string.
Definition: Utils.cpp:216
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:35
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
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:340
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:1030
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:326
const ICLTensor * input() const
Input tensor accessor.
DataLayout
[DataLayout enum definition]
Definition: Types.h:114
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
InterpolationPolicy _interpolationPolicy
Definition: CLScaleKernel.h:77
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::_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(), arm_compute::test::validation::data_layout, 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(), 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(), QuantizationInfo::uniform(), arm_compute::validate_and_configure_window(), and arm_compute::WIDTH.

◆ input()

const ICLTensor * input ( ) const

Input tensor accessor.

Returns
Pointer to input tensor.

Definition at line 151 of file CLScaleKernel.cpp.

152 {
153  return _input;
154 }

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

◆ output()

const ICLTensor * output ( ) const

Output tensor accessor.

Returns
Pointer to output tensor.

Definition at line 156 of file CLScaleKernel.cpp.

157 {
158  return _output;
159 }

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 247 of file CLScaleKernel.cpp.

248 {
251 
252  switch(_input->info()->data_layout())
253  {
254  case DataLayout::NCHW:
255  {
257 
258  do
259  {
260  unsigned int idx = 0;
261  add_2D_tensor_argument(idx, _input, slice);
262  add_2D_tensor_argument(idx, _output, slice);
263  enqueue(queue, *this, slice, lws_hint());
264  }
266  break;
267  }
268  case DataLayout::NHWC:
269  {
271  Window slice = collapsed.first_slice_window_4D();
272 
273  unsigned int idx = 0;
274  add_4D_tensor_argument(idx, _input, slice);
275  add_4D_tensor_argument(idx, _output, slice);
276  enqueue(queue, *this, slice, lws_hint());
277  break;
278  }
279  default:
280  ARM_COMPUTE_ERROR("Data layout not supported");
281  }
282 }
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
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:307
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:97
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:283
#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:940
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)

References 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 
)
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/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
Returns
a status

Definition at line 141 of file CLScaleKernel.cpp.

143 {
144  BorderSize border = BorderSize(1);
145  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, policy));
146  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), policy, border_mode, sampling_policy, border).first);
147 
148  return Status{};
149 }
Container for 2D border size.
Definition: Types.h:259
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)
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
Status class.
Definition: Error.h:52
const ICLTensor * output() const
Output tensor accessor.
const ICLTensor * input() const
Input tensor accessor.

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

Referenced by CLScale::validate().

Field Documentation

◆ _interpolationPolicy

Definition at line 77 of file CLScaleKernel.h.

Referenced by CLScaleKernel::configure().


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