Compute Library
 20.11
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 
30 #include "arm_compute/core/Utils.h"
33 #include "src/core/CL/CLValidate.h"
36 #include "support/StringSupport.h"
37 
38 namespace arm_compute
39 {
40 namespace
41 {
42 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
43 {
46 
47  if(output->tensor_shape().total_size() > 0)
48  {
52  }
53 
54  return Status{};
55 }
56 } // namespace
57 
59  : _input(nullptr), _output(nullptr)
60 {
61 }
62 
64 {
65  configure(CLKernelLibrary::get().get_compile_context(), input, output);
66 }
67 
69 {
71 
72  // Output tensor auto initialization if not yet initialized
73  auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, DataType::F32);
74 
75  auto padding_info = get_padding_info({ input, output });
76 
78 
79  _input = input;
80  _output = output;
81 
82  const int vec_size_x = 16 / output->info()->element_size();
83  const int output_width_x = output->info()->tensor_shape().x();
84  const bool multi_access_x = (output_width_x / vec_size_x > 0);
85 
86  const bool is_quantized_per_channel = is_data_type_quantized_per_channel(input->info()->data_type());
87  std::string kernel_name = "dequantization_layer";
88 
89  // Create kernel
90  CLBuildOptions build_opts;
91  if(!is_quantized_per_channel)
92  {
93  const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
94  const int qoffset = is_data_type_quantized_asymmetric(input->info()->data_type()) ? qinfo.offset : 0;
96  build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qoffset));
97  }
98  else
99  {
100  kernel_name += "_per_channel";
101  kernel_name += input->info()->data_layout() == DataLayout::NCHW ? "_nchw" : "_nhwc";
102  }
103 
104  build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
105  build_opts.add_option("-DDATA_TYPE_SRC=" + get_cl_type_from_data_type(input->info()->data_type()));
106  build_opts.add_option("-DDATA_TYPE_DST=" + get_cl_type_from_data_type(output->info()->data_type()));
107  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)));
108 
109  // Create kernel name
110  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
111 
112  // Configure kernel window
113  Window win = calculate_max_window(*output->info());
114  if(multi_access_x)
115  {
116  win.set(Window::DimX,
117  Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(), vec_size_x), vec_size_x));
118  }
119  ICLKernel::configure_internal(win);
120 
121  // Set output valid region
122  output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
123 
125 }
126 
128 {
130  return Status{};
131 }
132 
133 void CLDequantizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
134 {
137 
138  const bool is_quantized_per_channel = is_data_type_quantized_per_channel(_input->info()->data_type());
139 
140  // Collapse windo
141  Window new_window = is_quantized_per_channel ? window.collapse_if_possible(ICLKernel::window(), 4) : window.collapse_if_possible(ICLKernel::window(), 3);
142  Window slice = new_window.first_slice_window_3D();
143 
144  if(is_quantized_per_channel)
145  {
146  unsigned int idx = num_arguments_per_3D_tensor() * 2; //Skip the input and output parameters
147  _kernel.setArg(idx++, _input->quantization().scale->cl_buffer());
148  }
149 
150  do
151  {
152  unsigned int idx = 0;
153  add_3D_tensor_argument(idx, _input, slice);
154  add_3D_tensor_argument(idx, _output, slice);
155  enqueue(queue, *this, slice, lws_hint());
156  }
157  while(new_window.slide_window_slice_3D(slice));
158 }
159 } // namespace arm_compute
const std::vector< int32_t > & offset() const
Offset vector accessor.
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
Definition: CLValidate.h:35
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(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
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
const StringSet & options() const
Gets the current options list set.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:264
#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.
1 channel, 1 F32 per channel
#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
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:77
Status class.
Definition: Error.h:52
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:160
Copyright (c) 2017-2020 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
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...
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
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:202
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
bool is_data_type_quantized_per_channel(DataType dt)
Check if a given data type is of per channel type.
Definition: Utils.h:1245
std::string float_to_string_with_full_precision(float val)
Create a string with the float in full precision.
Definition: Utils.h:1262
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:71
quantized, asymmetric fixed-point 8-bit number unsigned
Coordinates of an item.
Definition: Coordinates.h:37
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
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...
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
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
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
quantized, symmetric fixed-point 8-bit number
Num samples, channels, height, width.
CLCompileContext class.
const ICLFloatArray * scale
Quantization scale array.
Definition: CLTypes.h:73
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1190
quantized, symmetric per channel fixed-point 8-bit number
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
void configure(const ICLTensor *input, ICLTensor *output)
Set the input, output, min and max.
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context.
const QuantizationInfo qinfo
Definition: Im2Col.cpp:150
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
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
Container for valid region of a window.
Definition: Types.h:188
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 ...
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)