blob: e3f88c11e69b3eb9125f390c21c4e3114e4efaf7 [file] [log] [blame]
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01003 *
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_CLGEMMLOWPOFFSETCONTRIBUTIONOUTPUTSTAGEKERNEL_H
25#define ARM_COMPUTE_CLGEMMLOWPOFFSETCONTRIBUTIONOUTPUTSTAGEKERNEL_H
Gian Marco Iodice4b908652018-10-18 10:21:02 +010026
27#include "arm_compute/core/CL/ICLKernel.h"
28
29namespace arm_compute
30{
31class ICLTensor;
32
Gian Marco Iodiceeb65f6d2020-04-15 11:42:15 +010033/** OpenCL kernel used to add the offset contribution after the matrix multiplication and perform the output stage.
Gian Marco Iodice4b908652018-10-18 10:21:02 +010034 *
Gian Marco Iodiceeb65f6d2020-04-15 11:42:15 +010035 * This kernel takes a final int32 accumulator value (the output of the matrix multiplication), adds to it the offset contribution
Gian Marco Iodice4b908652018-10-18 10:21:02 +010036 * of matrix A and matrix B and performs the output stage defined by the output_stage argument
37 *
Manuel Bottini959c26d2019-12-02 16:22:35 +000038 * @note For quantized computations the output data type for auto-initialization must be passed as part of the @ref GEMMLowpOutputStageInfo.
Gian Marco Iodice4b908652018-10-18 10:21:02 +010039 */
40class CLGEMMLowpOffsetContributionOutputStageKernel : public ICLKernel
41{
42public:
43 /** Constructor */
44 CLGEMMLowpOffsetContributionOutputStageKernel();
45 /** Prevent instances of this class from being copied (As this class contains pointers)*/
46 CLGEMMLowpOffsetContributionOutputStageKernel(const CLGEMMLowpOffsetContributionOutputStageKernel &) = delete;
47 /** Prevent instances of this class from being copied (As this class contains pointers)*/
48 CLGEMMLowpOffsetContributionOutputStageKernel &operator=(const CLGEMMLowpOffsetContributionOutputStageKernel &) = delete;
49 /** Allow instances of this class to be moved */
50 CLGEMMLowpOffsetContributionOutputStageKernel(CLGEMMLowpOffsetContributionOutputStageKernel &&) = default;
51 /** Allow instances of this class to be moved */
52 CLGEMMLowpOffsetContributionOutputStageKernel &operator=(CLGEMMLowpOffsetContributionOutputStageKernel &&) = default;
53 /** Initialise the kernel's input and output.
54 *
Gian Marco Iodiceeb65f6d2020-04-15 11:42:15 +010055 * @param[in] mm_result Input tensor containing the result of the matrix multiplication. Data type supported: S32
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000056 * @param[in] vector_sum_col Input row-vector of sums of all the entries in each column of matrix B.
57 * Note: vector_sum_col can be a nullptr in case a_offset = 0. Data type supported: same as @p mm_result
58 * @param[in] vector_sum_row Input row-vector of sums of all the entries in each row of matrix A.
59 * Note: vector_sum_row can be a nullptr in case b_offset = 0. Data type supported: same as @p mm_result
60 * @param[in] bias Biases tensor. Only shared biases supported and it can be a nullptr if the addition of biases is not required.
61 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
Manuel Bottini959c26d2019-12-02 16:22:35 +000062 * @param[out] output Output tensor. Data type supported: QASYMM8/QASYMM8_SIGNED.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000063 * @param[in] k Number of matrix A columns or Matrix B rows
64 * @param[in] a_offset Offset to be added to each element of the matrix A.
65 * @param[in] b_offset Offset to be added to each element of the matrix B.
66 * @param[in] output_stage GEMMLowp output stage info
67 * @param[in] output_multipliers Output multipliers tensor. In case of per-channel quantization, the number of multipliers must be equal to the number of filters (OFM).
68 * Supported data types: S32
69 * @param[in] output_shifts Output shifts tensor. In case of per-channel quantization, the number of multipliers must be equal to the number of filters (OFM).
70 * Supported data types: S32
Gian Marco Iodice4b908652018-10-18 10:21:02 +010071 */
72 void configure(const ICLTensor *mm_result, const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row, const ICLTensor *bias, ICLTensor *output, int32_t k, int32_t a_offset, int32_t b_offset,
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000073 const GEMMLowpOutputStageInfo &output_stage, const ICLTensor *output_multipliers, const ICLTensor *output_shifts);
Manuel Bottini4c6bd512020-04-08 10:15:51 +010074 /** Initialise the kernel's input and output.
75 *
76 * @param[in] compile_context The compile context to be used.
Gian Marco Iodiceeb65f6d2020-04-15 11:42:15 +010077 * @param[in] mm_result Input tensor containing the result of the matrix multiplication. Data type supported: S32
Manuel Bottini4c6bd512020-04-08 10:15:51 +010078 * @param[in] vector_sum_col Input row-vector of sums of all the entries in each column of matrix B.
79 * Note: vector_sum_col can be a nullptr in case a_offset = 0. Data type supported: same as @p mm_result
80 * @param[in] vector_sum_row Input row-vector of sums of all the entries in each row of matrix A.
81 * Note: vector_sum_row can be a nullptr in case b_offset = 0. Data type supported: same as @p mm_result
82 * @param[in] bias Biases tensor. Only shared biases supported and it can be a nullptr if the addition of biases is not required.
83 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
84 * @param[out] output Output tensor. Data type supported: QASYMM8/QASYMM8_SIGNED.
85 * @param[in] k Number of matrix A columns or Matrix B rows
86 * @param[in] a_offset Offset to be added to each element of the matrix A.
87 * @param[in] b_offset Offset to be added to each element of the matrix B.
88 * @param[in] output_stage GEMMLowp output stage info
89 * @param[in] output_multipliers Output multipliers tensor. In case of per-channel quantization, the number of multipliers must be equal to the number of filters (OFM).
90 * Supported data types: S32
91 * @param[in] output_shifts Output shifts tensor. In case of per-channel quantization, the number of multipliers must be equal to the number of filters (OFM).
92 * Supported data types: S32
93 */
Manuel Bottini679fc962020-04-21 16:08:53 +010094 void configure(const CLCompileContext &compile_context, const ICLTensor *mm_result, const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row, const ICLTensor *bias, ICLTensor *output, int32_t k,
Manuel Bottini4c6bd512020-04-08 10:15:51 +010095 int32_t a_offset, int32_t b_offset,
96 const GEMMLowpOutputStageInfo &output_stage, const ICLTensor *output_multipliers, const ICLTensor *output_shifts);
Gian Marco Iodice4b908652018-10-18 10:21:02 +010097 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMLowpOffsetContributionKernel
98 *
Manuel Bottini959c26d2019-12-02 16:22:35 +000099 * @param[in] mm_result Input tensor containing the result of @ref CLGEMMLowpOffsetContributionKernel. Data type supported: S32
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000100 * @param[in] vector_sum_col Input row-vector of sums of all the entries in each column of matrix B.
101 * Note: vector_sum_col can be a nullptr in case a_offset = 0. Data type supported: same as @p mm_result
102 * @param[in] vector_sum_row Input row-vector of sums of all the entries in each row of matrix A.
103 * Note: vector_sum_row can be a nullptr in case b_offset = 0. Data type supported: same as @p mm_result
104 * @param[in] bias Biases tensor. Only shared biases supported and it can be a nullptr if the addition of biases is not required.
105 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
Manuel Bottini959c26d2019-12-02 16:22:35 +0000106 * @param[in] output Output tensor. Data type supported: QASYMM8/QASYMM8_SIGNED.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000107 * @param[in] a_offset Offset to be added to each element of the matrix A.
108 * @param[in] b_offset Offset to be added to each element of the matrix B.
109 * @param[in] output_stage GEMMLowp output stage info
110 * @param[in] output_multipliers Output multipliers tensor info. In case of per-channel quantization, the number of multipliers must be equal to the number of filters (OFM).
111 * Supported data types: S32
112 * @param[in] output_shifts Output shifts tensor info. In case of per-channel quantization, the number of multipliers must be equal to the number of filters (OFM).
113 * Supported data types: S32
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100114 *
115 * @return a status
116 */
117 static Status validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias, const ITensorInfo *output, int32_t a_offset,
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000118 int32_t b_offset, const GEMMLowpOutputStageInfo &output_stage, const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts);
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100119
120 // Inherited methods overridden:
121 void run(const Window &window, cl::CommandQueue &queue) override;
122
123private:
124 const ICLTensor *_mm_result;
125 const ICLTensor *_vector_sum_col;
126 const ICLTensor *_vector_sum_row;
127 const ICLTensor *_bias;
128 ICLTensor *_output;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000129 const ICLTensor *_output_multipliers;
130 const ICLTensor *_output_shifts;
131 bool _is_quantized_per_channel;
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100132};
133} // namespace arm_compute
134
Michalis Spyrouf4643372019-11-29 16:17:13 +0000135#endif /* ARM_COMPUTE_CLGEMMLOWPOFFSETCONTRIBUTIONOUTPUTSTAGEKERNEL_H */