Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CLGEMMLOWPOFFSETCONTRIBUTIONOUTPUTSTAGEKERNEL_H |
| 25 | #define ARM_COMPUTE_CLGEMMLOWPOFFSETCONTRIBUTIONOUTPUTSTAGEKERNEL_H |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 26 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 27 | #include "src/core/CL/ICLKernel.h" |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | class ICLTensor; |
| 32 | |
Gian Marco Iodice | eb65f6d | 2020-04-15 11:42:15 +0100 | [diff] [blame] | 33 | /** OpenCL kernel used to add the offset contribution after the matrix multiplication and perform the output stage. |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 34 | * |
Gian Marco Iodice | eb65f6d | 2020-04-15 11:42:15 +0100 | [diff] [blame] | 35 | * This kernel takes a final int32 accumulator value (the output of the matrix multiplication), adds to it the offset contribution |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 36 | * of matrix A and matrix B and performs the output stage defined by the output_stage argument |
| 37 | * |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 38 | * @note For quantized computations the output data type for auto-initialization must be passed as part of the @ref GEMMLowpOutputStageInfo. |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 39 | */ |
| 40 | class CLGEMMLowpOffsetContributionOutputStageKernel : public ICLKernel |
| 41 | { |
| 42 | public: |
| 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 Iodice | eb65f6d | 2020-04-15 11:42:15 +0100 | [diff] [blame] | 55 | * @param[in] mm_result Input tensor containing the result of the matrix multiplication. Data type supported: S32 |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 56 | * @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 Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 62 | * @param[out] output Output tensor. Data type supported: QASYMM8/QASYMM8_SIGNED. |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 63 | * @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 Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 71 | */ |
| 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 Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 73 | const GEMMLowpOutputStageInfo &output_stage, const ICLTensor *output_multipliers, const ICLTensor *output_shifts); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 74 | /** Initialise the kernel's input and output. |
| 75 | * |
| 76 | * @param[in] compile_context The compile context to be used. |
Gian Marco Iodice | eb65f6d | 2020-04-15 11:42:15 +0100 | [diff] [blame] | 77 | * @param[in] mm_result Input tensor containing the result of the matrix multiplication. Data type supported: S32 |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 78 | * @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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 94 | 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, |
| 95 | int32_t k, |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 96 | int32_t a_offset, int32_t b_offset, |
| 97 | const GEMMLowpOutputStageInfo &output_stage, const ICLTensor *output_multipliers, const ICLTensor *output_shifts); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 98 | /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMLowpOffsetContributionKernel |
| 99 | * |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 100 | * @param[in] mm_result Input tensor containing the result of @ref CLGEMMLowpOffsetContributionKernel. Data type supported: S32 |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 101 | * @param[in] vector_sum_col Input row-vector of sums of all the entries in each column of matrix B. |
| 102 | * Note: vector_sum_col can be a nullptr in case a_offset = 0. Data type supported: same as @p mm_result |
| 103 | * @param[in] vector_sum_row Input row-vector of sums of all the entries in each row of matrix A. |
| 104 | * Note: vector_sum_row can be a nullptr in case b_offset = 0. Data type supported: same as @p mm_result |
| 105 | * @param[in] bias Biases tensor. Only shared biases supported and it can be a nullptr if the addition of biases is not required. |
| 106 | * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input. |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 107 | * @param[in] output Output tensor. Data type supported: QASYMM8/QASYMM8_SIGNED. |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 108 | * @param[in] a_offset Offset to be added to each element of the matrix A. |
| 109 | * @param[in] b_offset Offset to be added to each element of the matrix B. |
| 110 | * @param[in] output_stage GEMMLowp output stage info |
| 111 | * @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). |
| 112 | * Supported data types: S32 |
| 113 | * @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). |
| 114 | * Supported data types: S32 |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 115 | * |
| 116 | * @return a status |
| 117 | */ |
| 118 | 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 Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 119 | int32_t b_offset, const GEMMLowpOutputStageInfo &output_stage, const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 120 | |
| 121 | // Inherited methods overridden: |
| 122 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 123 | |
| 124 | private: |
| 125 | const ICLTensor *_mm_result; |
| 126 | const ICLTensor *_vector_sum_col; |
| 127 | const ICLTensor *_vector_sum_row; |
| 128 | const ICLTensor *_bias; |
| 129 | ICLTensor *_output; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 130 | const ICLTensor *_output_multipliers; |
| 131 | const ICLTensor *_output_shifts; |
| 132 | bool _is_quantized_per_channel; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 133 | }; |
| 134 | } // namespace arm_compute |
| 135 | |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 136 | #endif /* ARM_COMPUTE_CLGEMMLOWPOFFSETCONTRIBUTIONOUTPUTSTAGEKERNEL_H */ |