Compute Library
 20.02.1
CLDequantizationLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 ARM Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
25 
32 #include "arm_compute/core/Utils.h"
35 
36 namespace arm_compute
37 {
38 namespace
39 {
40 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
41 {
44 
45  if(output->tensor_shape().total_size() > 0)
46  {
50  }
51 
52  return Status{};
53 }
54 
55 std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
56 {
57  // Configure kernel window
58  Window win = calculate_max_window(*input, Steps());
59 
60  // Output tensor auto initialization if not yet initialized
61  auto_init_if_empty(*output, input->tensor_shape(), 1, DataType::F32);
62 
63  // CLDequantizationLayerKernel doesn't need padding so update_window_and_padding() can be skipped
64  Coordinates coord;
65  coord.set_num_dimensions(output->num_dimensions());
66  output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
67 
68  return std::make_tuple(Status{}, win);
69 }
70 } // namespace
71 
73  : _input(nullptr), _output(nullptr)
74 {
75 }
76 
78 {
80  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
81 
82  _input = input;
83  _output = output;
84 
85  const int vec_size_x = 16 / output->info()->element_size();
86  const int output_width_x = output->info()->tensor_shape().x();
87  const bool multi_access_x = (output_width_x / vec_size_x > 0);
88 
89  // Create and update the window (if needed)
90  Window win = calculate_max_window(*output->info());
91  if(multi_access_x)
92  {
93  win.set(Window::DimX,
94  Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(), vec_size_x), vec_size_x));
95  }
96  ICLKernel::configure_internal(win);
97 
98  const bool is_quantized_per_channel = is_data_type_quantized_per_channel(input->info()->data_type());
99  std::string kernel_name = "dequantization_layer";
100 
101  // Create kernel
102  CLBuildOptions build_opts;
103  if(!is_quantized_per_channel)
104  {
105  const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
106  const int qoffset = is_data_type_quantized_asymmetric(input->info()->data_type()) ? qinfo.offset : 0;
107  build_opts.add_option("-DSCALE=" + float_to_string_with_full_precision(qinfo.scale));
108  build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qoffset));
109  }
110  else
111  {
112  kernel_name += "_per_channel";
113  kernel_name += input->info()->data_layout() == DataLayout::NCHW ? "_nchw" : "_nhwc";
114  }
115 
116  build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
117  build_opts.add_option("-DDATA_TYPE_SRC=" + get_cl_type_from_data_type(input->info()->data_type()));
118  build_opts.add_option("-DDATA_TYPE_DST=" + get_cl_type_from_data_type(output->info()->data_type()));
119  build_opts.add_option_if(multi_access_x, "-DLAST_ACCESSED_X=" + support::cpp11::to_string(std::max<int>(output_width_x - vec_size_x, 0)));
120 
121  // Create kernel name
122  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
123 }
124 
126 {
127  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
128  ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
129  return Status{};
130 }
131 
132 void CLDequantizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
133 {
136 
137  const bool is_quantized_per_channel = is_data_type_quantized_per_channel(_input->info()->data_type());
138 
139  // Collapse windo
140  Window new_window = is_quantized_per_channel ? window.collapse_if_possible(ICLKernel::window(), 4) : window.collapse_if_possible(ICLKernel::window(), 3);
141  Window slice = new_window.first_slice_window_3D();
142 
143  if(is_quantized_per_channel)
144  {
145  unsigned int idx = num_arguments_per_3D_tensor() * 2; //Skip the input and output parameters
146  _kernel.setArg(idx++, _input->quantization().scale->cl_buffer());
147  }
148 
149  do
150  {
151  unsigned int idx = 0;
152  add_3D_tensor_argument(idx, _input, slice);
153  add_3D_tensor_argument(idx, _output, slice);
154  enqueue(queue, *this, slice, lws_hint());
155  }
156  while(new_window.slide_window_slice_3D(slice));
157 }
158 } // namespace arm_compute
const std::vector< int32_t > & offset() const
Offset vector accessor.
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
Definition: CLValidate.h:34
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
quantized, symmetric fixed-point 16-bit number
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
const StringSet & options() const
Gets the current options list set.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:247
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
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.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
1 channel, 1 F32 per channel
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor's metadata.
Definition: ITensorInfo.h:40
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Quantization info when assuming per layer quantization.
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:75
Status class.
Definition: Error.h:52
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
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
Copyright (c) 2017-2020 ARM Limited.
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:202
1 channel, 1 F16 per channel
void add_option(std::string option)
Adds option to the existing build option list.
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:81
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of CLDequantizationLayerKer...
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:200
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:68
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
bool is_data_type_quantized_per_channel(DataType dt)
Check if a given data type is of per channel type.
Definition: Utils.h:1194
std::string float_to_string_with_full_precision(float val)
Create a string with the float in full precision.
Definition: Utils.h:1211
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
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
quantized, asymmetric fixed-point 8-bit number unsigned
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 std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
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;.
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
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
quantized, symmetric fixed-point 8-bit number
Num samples, channels, height, width.
const ICLFloatArray * scale
Quantization scale array.
Definition: CLTypes.h:70
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1139
quantized, symmetric per channel fixed-point 8-bit number
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
void configure(const ICLTensor *input, ICLTensor *output)
Set the input, output, min and max.
const QuantizationInfo qinfo
Definition: Im2Col.cpp:150
virtual CLQuantization quantization() const =0
Interface to be implemented by the child class to return the wrapped quantization info data.
quantized, asymmetric fixed-point 8-bit number signed
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:97
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
virtual const cl::Buffer & cl_buffer() const =0
Interface to be implemented by the child class to return a reference to the OpenCL buffer containing ...
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:92
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)
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:143