blob: 237d8099b76110bb9ea184a5e12c65691a8187dd [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Gian Marco05288a22017-11-21 10:57:50 +00003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLGEMMLOWREDUCTIONKERNEL_H
25#define ARM_COMPUTE_CLGEMMLOWREDUCTIONKERNEL_H
Gian Marco05288a22017-11-21 10:57:50 +000026
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010027#include "src/core/CL/ICLKernel.h"
Gian Marco05288a22017-11-21 10:57:50 +000028
29namespace arm_compute
30{
31class ICLTensor;
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010032struct GEMMLowpReductionKernelInfo;
Gian Marco05288a22017-11-21 10:57:50 +000033
34/** Common interface for all OpenCL reduction kernels */
35class ICLGEMMLowpReductionKernel : public ICLKernel
36{
37public:
38 /** Constructor */
39 ICLGEMMLowpReductionKernel();
40 /** Prevent instances of this class from being copied (As this class contains pointers)*/
41 ICLGEMMLowpReductionKernel(const ICLGEMMLowpReductionKernel &) = delete;
42 /** Prevent instances of this class from being copied (As this class contains pointers)*/
43 ICLGEMMLowpReductionKernel &operator=(const ICLGEMMLowpReductionKernel &) = delete;
44 /** Allow instances of this class to be moved */
45 ICLGEMMLowpReductionKernel(ICLGEMMLowpReductionKernel &&) = default;
46 /** Allow instances of this class to be moved */
47 ICLGEMMLowpReductionKernel &operator=(ICLGEMMLowpReductionKernel &&) = default;
48
49 /** Initialise the kernel's input and output.
50 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010051 * @param[in] input Input tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8.
Gian Marco05288a22017-11-21 10:57:50 +000052 * @param[out] output Output row-vector of sums of all the entries in each row/col of input tensor. Data type supported: S32
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010053 * @param[in] info Kernel metadata:
54 * - k Number of matrix columns/rows depending on the type of reduction.
55 * - is_reshaped True if the matrix has been reshaped.
56 * - scalar Scalar value to multiply each reduced column/row by.
57 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
Gian Marco05288a22017-11-21 10:57:50 +000058 */
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010059 virtual void configure(const ICLTensor *input, ICLTensor *output, const GEMMLowpReductionKernelInfo &info) = 0;
Manuel Bottini4c6bd512020-04-08 10:15:51 +010060 /** Initialise the kernel's input and output.
61 *
62 * @param[in] compile_context The compile context to be used.
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010063 * @param[in] input Input tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8.
Manuel Bottini4c6bd512020-04-08 10:15:51 +010064 * @param[out] output Output row-vector of sums of all the entries in each row/col of input tensor. Data type supported: S32
65 * @param[in] info Kernel metadata:
66 * - k Number of matrix columns/rows depending on the type of reduction.
67 * - is_reshaped True if the matrix has been reshaped.
68 * - scalar Scalar value to multiply each reduced column/row by.
69 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
70 */
Manuel Bottini679fc962020-04-21 16:08:53 +010071 virtual void configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const GEMMLowpReductionKernelInfo &info) = 0;
Gian Marco05288a22017-11-21 10:57:50 +000072
73protected:
74 const ICLTensor *_input;
75 ICLTensor *_output;
76};
77
78/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each row of Matrix A.
79 *
80 * @note This stage is needed to handle the offset of matrix product
81 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
82 */
83class CLGEMMLowpMatrixAReductionKernel : public ICLGEMMLowpReductionKernel
84{
85public:
86 /** Initialise the kernel's input and output.
87 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010088 * @param[in] mtx_a Input tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8.
Gian Marco05288a22017-11-21 10:57:50 +000089 * @param[out] vector_sum_row Output row-vector of sums of all the entries in each row of mtx_a. Data type supported: S32
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010090 * @param[in] info Kernel metadata:
91 * - k Number of matrix columns/rows depending on the type of reduction.
92 * - is_reshaped True if the matrix has been reshaped.
93 * - scalar Scalar value to multiply each reduced column/row by.
94 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
Gian Marco05288a22017-11-21 10:57:50 +000095 */
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010096 void configure(const ICLTensor *mtx_a, ICLTensor *vector_sum_row, const GEMMLowpReductionKernelInfo &info) override;
Manuel Bottini4c6bd512020-04-08 10:15:51 +010097 /** Initialise the kernel's input and output.
98 *
99 * @param[in] compile_context The compile context to be used.
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100100 * @param[in] mtx_a Input tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8.
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100101 * @param[out] vector_sum_row Output row-vector of sums of all the entries in each row of mtx_a. Data type supported: S32
102 * @param[in] info Kernel metadata:
103 * - k Number of matrix columns/rows depending on the type of reduction.
104 * - is_reshaped True if the matrix has been reshaped.
105 * - scalar Scalar value to multiply each reduced column/row by.
106 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
107 */
Manuel Bottini679fc962020-04-21 16:08:53 +0100108 void configure(const CLCompileContext &compile_context, const ICLTensor *mtx_a, ICLTensor *vector_sum_row, const GEMMLowpReductionKernelInfo &info) override;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000109 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMLowpMatrixAReductionKernel
110 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100111 * @param[in] mtx_a Input tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8.
Georgios Pinitas358ca202017-12-07 16:47:52 +0000112 * @param[in] vector_sum_row Output row-vector of sums of all the entries in each row of mtx_a. Data type supported: S32
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100113 * @param[in] info Kernel metadata:
114 * - k Number of matrix columns/rows depending on the type of reduction.
115 * - is_reshaped True if the matrix has been reshaped.
116 * - scalar Scalar value to multiply each reduced column/row by.
117 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
Georgios Pinitas358ca202017-12-07 16:47:52 +0000118 *
119 * @return a status
120 */
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100121 static Status validate(const ITensorInfo *mtx_a, const ITensorInfo *vector_sum_row, const GEMMLowpReductionKernelInfo &info);
Gian Marco05288a22017-11-21 10:57:50 +0000122
123 // Inherited methods overridden:
124 void run(const Window &window, cl::CommandQueue &queue) override;
125};
126
127/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each column of Matrix B.
128 *
129 * @note This stage is needed to handle the offset of matrix product
130 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
131 */
132class CLGEMMLowpMatrixBReductionKernel : public ICLGEMMLowpReductionKernel
133{
134public:
135 /** Initialise the kernel's input and output.
136 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100137 * @param[in] mtx_b Input tensor. Data type supported: Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL.
Gian Marco05288a22017-11-21 10:57:50 +0000138 * @param[out] vector_sum_col Output row-vector of sums of all the entries in each column of mtx_b. Data type supported: S32
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100139 * @param[in] info Kernel metadata:
140 * - k Number of matrix columns/rows depending on the type of reduction.
141 * - is_reshaped True if the matrix has been reshaped.
142 * - scalar Scalar value to multiply each reduced column/row by.
143 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
Gian Marco05288a22017-11-21 10:57:50 +0000144 */
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100145 void configure(const ICLTensor *mtx_b, ICLTensor *vector_sum_col, const GEMMLowpReductionKernelInfo &info) override;
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100146 /** Initialise the kernel's input and output.
147 *
148 * @param[in] compile_context The compile context to be used.
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100149 * @param[in] mtx_b Input tensor. Data type supported: Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL.
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100150 * @param[out] vector_sum_col Output row-vector of sums of all the entries in each column of mtx_b. Data type supported: S32
151 * @param[in] info Kernel metadata:
152 * - k Number of matrix columns/rows depending on the type of reduction.
153 * - is_reshaped True if the matrix has been reshaped.
154 * - scalar Scalar value to multiply each reduced column/row by.
155 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
156 */
Manuel Bottini679fc962020-04-21 16:08:53 +0100157 void configure(const CLCompileContext &compile_context, const ICLTensor *mtx_b, ICLTensor *vector_sum_col, const GEMMLowpReductionKernelInfo &info) override;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000158 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMLowpMatrixBReductionKernel
159 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100160 * @param[in] mtx_b Input tensor. Data type supported: Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL.
Georgios Pinitas358ca202017-12-07 16:47:52 +0000161 * @param[in] vector_sum_col Output row-vector of sums of all the entries in each column of mtx_b. Data type supported: S32
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100162 * @param[in] info Kernel metadata:
163 * - k Number of matrix columns/rows depending on the type of reduction.
164 * - is_reshaped True if the matrix has been reshaped.
165 * - scalar Scalar value to multiply each reduced column/row by.
166 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
Georgios Pinitas358ca202017-12-07 16:47:52 +0000167 *
168 * @return a status
169 */
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100170 static Status validate(const ITensorInfo *mtx_b, const ITensorInfo *vector_sum_col, const GEMMLowpReductionKernelInfo &info);
Gian Marco05288a22017-11-21 10:57:50 +0000171
172 // Inherited methods overridden:
173 void run(const Window &window, cl::CommandQueue &queue) override;
174};
175} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000176#endif /* ARM_COMPUTE_CLGEMMLOWREDUCTIONKERNEL_H */