blob: 646242dc7e24bd37fecbf60e09a68a7b9bdb8747 [file] [log] [blame]
Manuel Bottiniae58bdf2021-06-17 17:18:45 +01001/*
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_CPU_GEMMLOWP_QUANTIZEDOWNINT32_SCALE_KERNEL_H
25#define ARM_COMPUTE_CPU_GEMMLOWP_QUANTIZEDOWNINT32_SCALE_KERNEL_H
26
27#include "arm_compute/core/KernelDescriptors.h"
28#include "src/core/common/Macros.h"
29#include "src/core/cpu/ICpuKernel.h"
30
31namespace arm_compute
32{
33class ITensor;
34namespace cpu
35{
36namespace kernels
37{
38/** Kernel used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8/QASYMM8_SIGNED
39 *
40 * This kernel takes a final int32 accumulator value (the output of @ref NEGEMMLowpMatrixMultiplyKernel), and processes it to obtain the final QASYMM8/QASYMM8_SIGNED value.
41 * The following computations will be performed by the kernel:
42 *
43 * -# Add offset terms to final result
44 * -# Multiply each entry of result by result_mult_int
45 * -# Add bias to final result if bias tensor is not a nullptr
46 * -# Shift the int32 accumulator by result_shift
47 * -# Clamp the value between the specified min and max bounds
48 * -# Clamp the resulting int32 values:
49 * -# -to the [0..255] range and cast to QASYMM8.
50 * -# -to the [-128..127] range and cast to QASYMM8_SIGNED.
51 *
52 */
53class CpuGemmLowpQuantizeDownInt32ScaleKernel : public ICpuKernel
54{
55public:
56 /** Default constructor */
57 CpuGemmLowpQuantizeDownInt32ScaleKernel() = default;
58 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuGemmLowpQuantizeDownInt32ScaleKernel);
59 /** Initialise the kernel's input and output.
60 *
61 * @param[in] src Input tensor info. Data type supported: S32
62 * @param[in] bias Biases tensor info. Only shared biases supported and it can be a nullptr if the biases addition is not required.
63 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
64 * @param[out] dst Output tensor info. Data type supported: Data type supported: QASYMM8/QASYMM8_SIGNED
65 * @param[out] output_stage GEMMLowp output stage metadata.
66 */
67 void configure(ITensorInfo *src, ITensorInfo *bias, ITensorInfo *dst, const GEMMLowpOutputStageInfo *output_stage);
68 /** Static function to check if given info will lead to a valid configuration
69 *
70 * Similar to CpuGemmLowpQuantizeDownInt32ScaleKernel::configure()
71 *
72 * @return a status
73 */
74 static Status validate(const ITensorInfo *src, const ITensorInfo *bias, const ITensorInfo *dst, const GEMMLowpOutputStageInfo *output_stage);
75
76 // Inherited methods overridden:
77 void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override;
78 const char *name() const override;
79
80private:
81 /** Template function to run the NEGEMMLowpQuantizeDownInt32ScaleKernel
82 *
83 * @param[in] src Input tensor info
84 * @param[in] bias Biases tensor info
85 * @param[out] dst Output tensor info
86 * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window())
87 */
88 template <typename T>
89 void run_internal(const ITensor *src, const ITensor *bias, ITensor *dst, const Window &window);
90
91 /** Common signature for all the specialised CpuGemmLowpQuantizeDownInt32ScaleKernel functions
92 *
93 * @param[in] src Input tensor info
94 * @param[in] bias Biases tensor info
95 * @param[out] dst Output tensor info
96 * @param[in] window Region on which to execute the kernel.
97 */
98 using QuantizeDownFunctionPtr = void (CpuGemmLowpQuantizeDownInt32ScaleKernel::*)(const ITensor *src, const ITensor *bias, ITensor *dst, const Window &window);
99
100 QuantizeDownFunctionPtr _func{ nullptr };
101 const GEMMLowpOutputStageInfo *_output_stage{ nullptr };
102 bool _is_bounded_relu{ false };
103};
104} // namespace kernels
105} // namespace cpu
106} // namespace arm_compute
107#endif /* ARM_COMPUTE_CPU_GEMMLOWP_QUANTIZEDOWNINT32_SCALE_KERNEL_H */