blob: 71681cf628f72da4e89c12c5138952bfbf579baf [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Manuel Bottini959c26d2019-12-02 16:22:35 +00002 * 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
27#include "arm_compute/core/CL/ICLKernel.h"
28
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 *
51 * @param[in] input Input tensor. Data type supported: S8
52 * @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;
Gian Marco05288a22017-11-21 10:57:50 +000060
61protected:
62 const ICLTensor *_input;
63 ICLTensor *_output;
64};
65
66/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each row of Matrix A.
67 *
68 * @note This stage is needed to handle the offset of matrix product
69 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
70 */
71class CLGEMMLowpMatrixAReductionKernel : public ICLGEMMLowpReductionKernel
72{
73public:
74 /** Initialise the kernel's input and output.
75 *
Manuel Bottini959c26d2019-12-02 16:22:35 +000076 * @param[in] mtx_a Input tensor. Data type supported: QASYMM8/QASYMM8_SIGNED
Gian Marco05288a22017-11-21 10:57:50 +000077 * @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 +010078 * @param[in] info Kernel metadata:
79 * - k Number of matrix columns/rows depending on the type of reduction.
80 * - is_reshaped True if the matrix has been reshaped.
81 * - scalar Scalar value to multiply each reduced column/row by.
82 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
Gian Marco05288a22017-11-21 10:57:50 +000083 */
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010084 void configure(const ICLTensor *mtx_a, ICLTensor *vector_sum_row, const GEMMLowpReductionKernelInfo &info) override;
Georgios Pinitas358ca202017-12-07 16:47:52 +000085 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMLowpMatrixAReductionKernel
86 *
Manuel Bottini959c26d2019-12-02 16:22:35 +000087 * @param[in] mtx_a Input tensor. Data type supported: QASYMM8/QASYMM8_SIGNED
Georgios Pinitas358ca202017-12-07 16:47:52 +000088 * @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 +010089 * @param[in] info Kernel metadata:
90 * - k Number of matrix columns/rows depending on the type of reduction.
91 * - is_reshaped True if the matrix has been reshaped.
92 * - scalar Scalar value to multiply each reduced column/row by.
93 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
Georgios Pinitas358ca202017-12-07 16:47:52 +000094 *
95 * @return a status
96 */
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010097 static Status validate(const ITensorInfo *mtx_a, const ITensorInfo *vector_sum_row, const GEMMLowpReductionKernelInfo &info);
Gian Marco05288a22017-11-21 10:57:50 +000098
99 // Inherited methods overridden:
100 void run(const Window &window, cl::CommandQueue &queue) override;
101};
102
103/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each column of Matrix B.
104 *
105 * @note This stage is needed to handle the offset of matrix product
106 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
107 */
108class CLGEMMLowpMatrixBReductionKernel : public ICLGEMMLowpReductionKernel
109{
110public:
111 /** Initialise the kernel's input and output.
112 *
Manuel Bottini959c26d2019-12-02 16:22:35 +0000113 * @param[in] mtx_b Input tensor. Data type supported: Data type supported: QASYMM8/QASYMM8_SIGNED
Gian Marco05288a22017-11-21 10:57:50 +0000114 * @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 +0100115 * @param[in] info Kernel metadata:
116 * - k Number of matrix columns/rows depending on the type of reduction.
117 * - is_reshaped True if the matrix has been reshaped.
118 * - scalar Scalar value to multiply each reduced column/row by.
119 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
Gian Marco05288a22017-11-21 10:57:50 +0000120 */
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100121 void configure(const ICLTensor *mtx_b, ICLTensor *vector_sum_col, const GEMMLowpReductionKernelInfo &info) override;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000122 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMLowpMatrixBReductionKernel
123 *
Manuel Bottini959c26d2019-12-02 16:22:35 +0000124 * @param[in] mtx_b Input tensor. Data type supported: Data type supported: QASYMM8/QASYMM8_SIGNED
Georgios Pinitas358ca202017-12-07 16:47:52 +0000125 * @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 +0100126 * @param[in] info Kernel metadata:
127 * - k Number of matrix columns/rows depending on the type of reduction.
128 * - is_reshaped True if the matrix has been reshaped.
129 * - scalar Scalar value to multiply each reduced column/row by.
130 * - mul_byscalar True if each reduced column/row must be multiplied by a scalar value.
Georgios Pinitas358ca202017-12-07 16:47:52 +0000131 *
132 * @return a status
133 */
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100134 static Status validate(const ITensorInfo *mtx_b, const ITensorInfo *vector_sum_col, const GEMMLowpReductionKernelInfo &info);
Gian Marco05288a22017-11-21 10:57:50 +0000135
136 // Inherited methods overridden:
137 void run(const Window &window, cl::CommandQueue &queue) override;
138};
139} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000140#endif /* ARM_COMPUTE_CLGEMMLOWREDUCTIONKERNEL_H */