1 /*
2  * Copyright (c) 2020-2021 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  */
24 #ifndef ARM_COMPUTE_CL_GEMMLOWP_QUANTIZEDOWN_INT32_SCALE_BY_FLOAT_KERNEL_H
25 #define ARM_COMPUTE_CL_GEMMLOWP_QUANTIZEDOWN_INT32_SCALE_BY_FLOAT_KERNEL_H
26 
27 #include "src/core/common/Macros.h"
28 #include "src/gpu/cl/ClCompileContext.h"
29 #include "src/gpu/cl/IClKernel.h"
30 
31 namespace arm_compute
32 {
33 namespace opencl
34 {
35 namespace kernels
36 {
37 /** OpenCL kernel used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8/QASYMM8_SIGNED
38  *
39  * This kernel takes a final int32 accumulator value (the output of the matrix multiplication), and processes it to obtain the final QASYMM8/QASYMM8_SIGNED value.
40  * The following computations will be performed by the kernel:
41  *
42  *  -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
43  *  -# Add bias to final result if bias tensor is not a nullptr
44  *  -# Requantize
45  *  -# Add offset to each result
46  *  -# Clamp the value between the specified min and max bounds
47  *  -# Clamp the resulting int32 values to
48  *      - to the [0..255] range and cast to QASYMM8.
49  *      - to the [-128..127] range and cast to QASYMM8_SIGNED.
50  */
51 class ClGemmLowpQuantizeDownInt32ScaleByFloatKernel : public IClKernel
52 {
53 public:
54     ClGemmLowpQuantizeDownInt32ScaleByFloatKernel();
55     ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClGemmLowpQuantizeDownInt32ScaleByFloatKernel);
56     /** Initialise the kernel's input and output.
57      *
58      * @param[in]  compile_context The compile context to be used.
59      * @param[in]  src             Source tensor. Data type supported: S32
60      * @param[in]  bias            Biases tensor. Only shared biases supported and it can be a nullptr if the biases addition is not required.
61      *                             Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p src.
62      * @param[out] dst             Destination tensor. Data type supported: Data type supported: QASYMM8/QASYMM8_SIGNED
63      * @param[in]  info            Output stage info. Used to pass the quantized output data type
64      */
65     void configure(const CLCompileContext &compile_context, const ITensorInfo *src, const ITensorInfo *bias, ITensorInfo *dst, const GEMMLowpOutputStageInfo *info);
66     /** Static function to check if given info will lead to a valid configuration
67      *
68      * Similar to @ref ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel::configure()
69      *
70      * @return a status
71      */
72     static Status validate(const ITensorInfo *src, const ITensorInfo *bias, const ITensorInfo *dst, const GEMMLowpOutputStageInfo *info);
73 
74     // Inherited methods overridden:
75     void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override;
76 };
77 } // namespace kernels
78 } // namespace opencl
79 } // namespace arm_compute
80 #endif /* ARM_COMPUTE_CL_GEMMLOWP_QUANTIZEDOWN_INT32_SCALE_BY_FLOAT_KERNEL_H */
81