blob: c81543e4c296e22e87c8ea65a337469386141aac [file] [log] [blame]
Georgios Pinitas4a578b92021-06-25 12:13:49 +01001/*
2 * Copyright (c) 2017-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_REDUCTION_KERNEL_H
25#define ARM_COMPUTE_CL_GEMMLOWP_REDUCTION_KERNEL_H
26
27#include "arm_compute/core/KernelDescriptors.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Georgios Pinitas4a578b92021-06-25 12:13:49 +010029#include "src/core/common/Macros.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010030#include "src/gpu/cl/ClCompileContext.h"
31#include "src/gpu/cl/IClKernel.h"
Georgios Pinitas4a578b92021-06-25 12:13:49 +010032
33namespace arm_compute
34{
35namespace opencl
36{
37namespace kernels
38{
39/** Common interface for all OpenCL reduction kernels */
40class IClGemmLowpReductionKernel : public IClKernel
41{
42public:
43 IClGemmLowpReductionKernel();
44 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(IClGemmLowpReductionKernel);
45 /** Initialise the kernel's input and output.
46 *
47 * @param[in] compile_context The compile context to be used.
48 * @param[in] input Input tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8.
49 * @param[out] output Output row-vector of sums of all the entries in each row/col of input tensor. Data type supported: S32
50 * @param[in] info Kernel metadata:
51 * - k Number of matrix columns/rows depending on the type of reduction.
52 * - is_reshaped True if the matrix has been reshaped.
53 * - scalar Scalar value to multiply each reduced column/row by.
54 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
55 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056 virtual void configure(const CLCompileContext &compile_context,
57 const ITensorInfo *input,
58 ITensorInfo *output,
59 const GEMMLowpReductionKernelInfo &info) = 0;
Georgios Pinitas4a578b92021-06-25 12:13:49 +010060};
61
62/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each row of Matrix A.
63 *
64 * @note This stage is needed to handle the offset of matrix product
65 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
66 */
67class ClGemmLowpMatrixAReductionKernel : public IClGemmLowpReductionKernel
68{
69public:
70 /** Initialise the kernel's input and output.
71 *
72 * @param[in] compile_context The compile context to be used.
73 * @param[in] mtx_a Input tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8.
74 * @param[out] vector_sum_row Output row-vector of sums of all the entries in each row of mtx_a. Data type supported: S32
75 * @param[in] info Kernel metadata:
76 * - k Number of matrix columns/rows depending on the type of reduction.
77 * - is_reshaped True if the matrix has been reshaped.
78 * - scalar Scalar value to multiply each reduced column/row by.
79 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
80 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 void configure(const CLCompileContext &compile_context,
82 const ITensorInfo *mtx_a,
83 ITensorInfo *vector_sum_row,
84 const GEMMLowpReductionKernelInfo &info) override;
Georgios Pinitas4a578b92021-06-25 12:13:49 +010085 /** Static function to check if given info will lead to a valid configuration
86 *
87 * Similar to @ref ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel::configure()
88 *
89 * @return a status
90 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010091 static Status
92 validate(const ITensorInfo *mtx_a, const ITensorInfo *vector_sum_row, const GEMMLowpReductionKernelInfo &info);
Georgios Pinitas4a578b92021-06-25 12:13:49 +010093
94 // Inherited methods overridden:
95 void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override;
96};
97
98/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each column of Matrix B.
99 *
100 * @note This stage is needed to handle the offset of matrix product
101 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
102 */
103class ClGemmLowpMatrixBReductionKernel : public IClGemmLowpReductionKernel
104{
105public:
106 /** Initialise the kernel's input and output.
107 *
108 * @param[in] compile_context The compile context to be used.
109 * @param[in] mtx_b Input tensor. Data type supported: Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL.
110 * @param[out] vector_sum_col Output row-vector of sums of all the entries in each column of mtx_b. Data type supported: S32
111 * @param[in] info Kernel metadata:
112 * - k Number of matrix columns/rows depending on the type of reduction.
113 * - is_reshaped True if the matrix has been reshaped.
114 * - scalar Scalar value to multiply each reduced column/row by.
115 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
116 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117 void configure(const CLCompileContext &compile_context,
118 const ITensorInfo *mtx_b,
119 ITensorInfo *vector_sum_col,
120 const GEMMLowpReductionKernelInfo &info) override;
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100121 /** Static function to check if given info will lead to a valid configuration
122 *
123 * Similar to @ref ClGemmLowpQuantizeDownInt32ScaleByFixedPointKernel::configure()
124 *
125 * @return a status
126 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100127 static Status
128 validate(const ITensorInfo *mtx_b, const ITensorInfo *vector_sum_col, const GEMMLowpReductionKernelInfo &info);
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100129
130 // Inherited methods overridden:
131 void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override;
132};
133} // namespace kernels
134} // namespace opencl
135} // namespace arm_compute
136#endif /* ARM_COMPUTE_CL_GEMMLOWP_REDUCTION_KERNEL_H */