Compute Library
 23.11
ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2023 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 
35 
38 #include "support/Cast.h"
39 #include "support/StringSupport.h"
40 
41 namespace arm_compute
42 {
43 namespace opencl
44 {
45 namespace kernels
46 {
47 namespace
48 {
49 Status validate_arguments(const ITensorInfo *src,
50  const ITensorInfo *bias,
51  const ITensorInfo *dst,
52  const GEMMLowpOutputStageInfo *info)
53 {
56 
57  // Check biases if exist
58  if (bias != nullptr)
59  {
61  ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
62  ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(0) != bias->dimension(0));
63  }
64 
65  if (dst->total_size() != 0)
66  {
67  ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() != info->output_data_type, "Mismatching dst data type");
69  }
70 
71  return Status{};
72 }
73 } // namespace
74 
76 {
78 }
79 
81  const ITensorInfo *bias,
82  const ITensorInfo *dst,
84 {
87 
88  return Status{};
89 }
90 
92  const ITensorInfo *src,
93  const ITensorInfo *bias,
96 {
97  // Perform validate step
100 
101  auto padding_info = get_padding_info({src, bias, dst});
102 
103  // dst auto inizialitation if not yet initialized
104  auto_init_if_empty(*dst, src->clone()->set_data_type(info->output_data_type));
105 
106  const unsigned int num_elems_processed_per_iteration = adjust_vec_size(4, src->dimension(0));
107 
108  // Set the arguments to pass at compile time
109  auto min = info->gemmlowp_min_bound;
110  auto max = info->gemmlowp_max_bound;
111  CLBuildOptions build_opts;
113  build_opts.add_option("-DVEC_SIZE_LEFTOVER=" +
115  build_opts.add_option("-DRESULT_OFFSET_AFTER_SHIFT=" + support::cpp11::to_string(info->gemmlowp_offset));
116  build_opts.add_option("-DRESULT_FIXEDPOINT_MULTIPLIER=" + support::cpp11::to_string(info->gemmlowp_multiplier));
117  build_opts.add_option("-DRESULT_SHIFT=" + support::cpp11::to_string(info->gemmlowp_shift));
118  build_opts.add_option("-DOUTPUT_DATA_TYPE=" + get_cl_type_from_data_type(dst->data_type()));
119  build_opts.add_option_if(
120  (min > std::get<0>(quantization::get_min_max_values_from_quantized_data_type(info->output_data_type))) &&
121  (min != max),
122  "-DMIN_BOUND=" + support::cpp11::to_string(min));
123  build_opts.add_option_if(
124  (max < std::get<1>(quantization::get_min_max_values_from_quantized_data_type(info->output_data_type))) &&
125  (min != max),
126  "-DMAX_BOUND=" + support::cpp11::to_string(max));
127  build_opts.add_option_if(bias != nullptr, "-DADD_BIAS");
128 
129  // Create kernel
130  const std::string kernel_name = (info->output_data_type == DataType::QSYMM16)
131  ? "gemmlowp_output_stage_quantize_down_fixedpoint_qsymm16"
132  : "gemmlowp_output_stage_quantize_down_fixedpoint";
133 
134  // A macro guard to compile ONLY the kernel of interest
135  build_opts.add_option("-D" + upper_string(kernel_name));
136 
137  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
138 
139  // Configure kernel window
141  ICLKernel::configure_internal(win);
142 
144 }
145 
147  const Window &window,
148  cl::CommandQueue &queue)
149 {
152 
153  const auto src =
154  utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
155  const auto bias =
156  utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_BIAS));
157  auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
158 
159  // Create src window
161  Window slice = collapsed.first_slice_window_3D();
162 
163  // Setup bias slice
164  unsigned int idx1 = num_arguments_per_3D_tensor();
165  if (bias != nullptr)
166  {
167  Window biases_slice(slice);
168  biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1));
169  biases_slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
170  add_1D_tensor_argument(idx1, bias, biases_slice);
171  }
172 
173  do
174  {
175  unsigned int idx = 0;
178  enqueue(queue, *this, slice, lws_hint());
179  } while (collapsed.slide_window_slice_3D(slice));
180 }
181 } // namespace kernels
182 } // namespace opencl
183 } // namespace arm_compute
arm_compute::Steps
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
arm_compute::support::cpp11::to_string
std::string to_string(T &&value)
Convert integer and float values to string.
Definition: StringSupport.h:168
Cast.h
StringSupport.h
arm_compute::ICLKernel::add_1D_tensor_argument
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.
Definition: ICLKernel.h:186
arm_compute::test::validation::src
SimpleTensor< float > src
Definition: DFT.cpp:155
AdjustVecSize.h
ICLTensor.h
Helpers.h
arm_compute::CLBuildOptions::options
const StringSet & options() const
Gets the current options list set.
Definition: CLCompileContext.cpp:72
arm_compute::calculate_max_window
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Definition: WindowHelpers.cpp:29
arm_compute::GEMMLowpOutputStageInfo
GEMMLowp output stage info.
Definition: GEMMInfo.h:45
arm_compute::test::validation::dst
auto dst
Definition: DFT.cpp:170
arm_compute::cpu::kernels::validate_arguments
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
Definition: CpuDirectConv2dKernel.cpp:57
ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:1079
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:574
arm_compute::Window::collapse_if_possible
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:72
TensorInfo.h
arm_compute::ITensorPack::get_tensor
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:63
arm_compute::upper_string
std::string upper_string(const std::string &val)
Raise a given string to upper case.
Definition: StringUtils.cpp:45
arm_compute::opencl::kernels::ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel::configure
void configure(const CLCompileContext &compile_context, const ITensorInfo *src, const ITensorInfo *bias, ITensorInfo *dst, const GEMMLowpOutputStageInfo *info)
Initialise the kernel's source and destination.
Definition: ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel.cpp:91
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:677
ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:952
arm_compute::DataType::QSYMM16
@ QSYMM16
quantized, symmetric fixed-point 16-bit number
StringUtils.h
ARM_COMPUTE_RETURN_ON_ERROR
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:205
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
ARM_COMPUTE_ERROR_ON
#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
arm_compute::ITensorPack::get_const_tensor
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:53
ARM_COMPUTE_ERROR_THROW_ON
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
arm_compute::ITensorPack
Tensor packing service.
Definition: ITensorPack.h:39
arm_compute::CLBuildOptions::add_option
void add_option(std::string option)
Adds option to the existing build option list.
Definition: CLCompileContext.cpp:41
arm_compute::CLCompileContext
CLCompileContext class.
Definition: CLCompileContext.h:204
ARM_COMPUTE_RETURN_ERROR_ON
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:298
arm_compute::ACL_DST
@ ACL_DST
Definition: Types.h:55
arm_compute::auto_init_if_empty
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: AutoConfiguration.h:43
arm_compute::quantization::get_min_max_values_from_quantized_data_type
std::pair< int, int > get_min_max_values_from_quantized_data_type(DataType data_type)
Get minimum and maximum values for the input quantized data type.
Definition: AsymmHelpers.cpp:154
arm_compute::create_kernel
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:409
arm_compute::Status
Status class.
Definition: Error.h:52
WindowHelpers.h
arm_compute::CLBuildOptions::add_option_if
void add_option_if(bool cond, std::string option)
Adds option if a given condition is true;.
Definition: CLCompileContext.cpp:46
ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:203
arm_compute::Window::slide_window_slice_3D
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:350
bias
const int32_t * bias
Definition: working_space.hpp:322
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:151
arm_compute::Window::Dimension
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:79
arm_compute::Window::set
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:53
arm_compute::Window::DimY
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
AutoConfiguration.h
arm_compute::Window::first_slice_window_3D
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:306
AsymmHelpers.h
arm_compute::IKernel::window
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
arm_compute::get_cl_type_from_data_type
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:41
arm_compute::ICLKernel::add_3D_tensor_argument
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:234
ShapeCalculator.h
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute::ELEMENTWISE
@ ELEMENTWISE
Elementwise CL kernel type.
Definition: CLTypes.h:83
arm_compute::ACL_BIAS
@ ACL_BIAS
Definition: Types.h:74
arm_compute::ICLKernel::num_arguments_per_3D_tensor
constexpr static unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:321
ARM_COMPUTE_RETURN_ERROR_ON_MSG
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:245
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::opencl::kernels::ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel::validate
static Status validate(const ITensorInfo *src, const ITensorInfo *bias, const ITensorInfo *dst, const GEMMLowpOutputStageInfo *info)
Static function to check if given info will lead to a valid configuration.
Definition: ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel.cpp:80
arm_compute::adjust_vec_size
unsigned int adjust_vec_size(unsigned int vec_size, size_t dim0)
Returns the adjusted vector size in case it is less than the input's first dimension,...
Definition: AdjustVecSize.h:38
arm_compute::has_padding_changed
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:491
arm_compute::Window::DimZ
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
arm_compute::DataType::S32
@ S32
signed 32-bit number
arm_compute::ICLKernel::lws_hint
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:383
arm_compute::ACL_SRC
@ ACL_SRC
Definition: Types.h:44
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
arm_compute::test::validation::info
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
arm_compute::CLBuildOptions
Build options.
Definition: CLCompileContext.h:38
ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel.h
Validate.h
num_elems_processed_per_iteration
unsigned int num_elems_processed_per_iteration
Definition: ClIm2ColKernel.cpp:60
arm_compute::get_padding_info
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:476
arm_compute::test::validation::reference::slice
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
Definition: SliceOperations.cpp:38
CLHelpers.h
kernel_name
std::string kernel_name
Definition: ClIm2ColKernel.cpp:58
arm_compute::opencl::kernels::ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel::run_op
void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
Definition: ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel.cpp:146
arm_compute::opencl::kernels::ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel::ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel
ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel()
Definition: ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel.cpp:75
arm_compute::enqueue
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:33