Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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_CLSOFTMAXLAYERKERNEL_H |
| 25 | #define ARM_COMPUTE_CLSOFTMAXLAYERKERNEL_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/ICLSimple3DKernel.h" |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/KernelDescriptors.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 30 | #include <tuple> |
| 31 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | namespace arm_compute |
| 33 | { |
| 34 | class ICLTensor; |
| 35 | |
| 36 | /** Interface for the identifying the max value of 1D Logits */ |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 37 | class CLLogits1DMaxKernel : public ICLSimple3DKernel |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | { |
| 39 | public: |
| 40 | /** Set the input and output tensors. |
| 41 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 42 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 43 | * @param[out] output Destination tensor. Data types supported: same as @p input |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | */ |
| 45 | void configure(const ICLTensor *input, ICLTensor *output); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame^] | 46 | /** Set the input and output tensors. |
| 47 | * |
| 48 | * @param[in] compile_context The compile context to be used. |
| 49 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
| 50 | * @param[out] output Destination tensor. Data types supported: same as @p input |
| 51 | */ |
| 52 | void configure(CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 53 | /** Static function to check if given info will lead to a valid configuration of @ref CLLogits1DMaxKernel |
| 54 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 55 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 56 | * @param[in] output Destination tensor. Data types supported: same as @p input |
| 57 | * |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 58 | * @return a status |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 59 | */ |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 60 | static Status validate(const ITensorInfo *input, const ITensorInfo *output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 63 | /** Interface for shifting, exponentiating and summing the logits */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | class CLLogits1DShiftExpSumKernel : public ICLKernel |
| 65 | { |
| 66 | public: |
| 67 | /** Default constructor */ |
| 68 | CLLogits1DShiftExpSumKernel(); |
| 69 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 70 | CLLogits1DShiftExpSumKernel(const CLLogits1DShiftExpSumKernel &) = delete; |
| 71 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 72 | CLLogits1DShiftExpSumKernel &operator=(const CLLogits1DShiftExpSumKernel &) = delete; |
| 73 | /** Allow instances of this class to be moved */ |
| 74 | CLLogits1DShiftExpSumKernel(CLLogits1DShiftExpSumKernel &&) = default; |
| 75 | /** Allow instances of this class to be moved */ |
| 76 | CLLogits1DShiftExpSumKernel &operator=(CLLogits1DShiftExpSumKernel &&) = default; |
| 77 | /** Set the input and output tensors. |
| 78 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 79 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 80 | * @param[in] max Max values tensor. Data types supported: same as @p input |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 81 | * @param[out] output Destination tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input |
| 82 | * @param[out] sum Sum of 1D logits tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input |
| 83 | * @param[in] beta (Optional) A scaling factor for the exponent. Defaults to 1.0 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | */ |
Pablo Palmier | 48a60f9 | 2017-10-18 11:03:08 +0100 | [diff] [blame] | 85 | void configure(const ICLTensor *input, const ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta = 1.0f); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame^] | 86 | /** Set the input and output tensors. |
| 87 | * |
| 88 | * @param[in] compile_context The compile context to be used. |
| 89 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
| 90 | * @param[in] max Max values tensor. Data types supported: same as @p input |
| 91 | * @param[out] output Destination tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input |
| 92 | * @param[out] sum Sum of 1D logits tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input |
| 93 | * @param[in] beta (Optional) A scaling factor for the exponent. Defaults to 1.0 |
| 94 | */ |
| 95 | void configure(CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta = 1.0f); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 96 | /** Static function to check if given info will lead to a valid configuration of @ref CLLogits1DShiftExpSumKernel |
| 97 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 98 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 99 | * @param[in] max Max values tensor. Data types supported: same as @p input |
| 100 | * @param[in] output Destination tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input |
| 101 | * @param[in] sum Sum of 1D logits tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input |
| 102 | * |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 103 | * @return a status |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 104 | */ |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 105 | static Status validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | |
| 107 | // Inherited methods overridden: |
| 108 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 109 | |
| 110 | private: |
| 111 | const ICLTensor *_input; |
| 112 | const ICLTensor *_max; |
| 113 | ICLTensor *_output; |
| 114 | ICLTensor *_sum; |
| 115 | }; |
| 116 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 117 | /** Interface for max, shifting, exponentiating and summing the logits */ |
| 118 | class CLLogits1DMaxShiftExpSumKernel : public ICLKernel |
| 119 | { |
| 120 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 121 | /** Info for whether a parallel reduction will be run and the vector size of the execution. */ |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 122 | using ParallelReductionInfo = std::tuple<bool, unsigned int>; |
| 123 | |
| 124 | public: |
| 125 | /** Default constructor */ |
| 126 | CLLogits1DMaxShiftExpSumKernel(); |
| 127 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 128 | CLLogits1DMaxShiftExpSumKernel(const CLLogits1DMaxShiftExpSumKernel &) = delete; |
| 129 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 130 | CLLogits1DMaxShiftExpSumKernel &operator=(const CLLogits1DMaxShiftExpSumKernel &) = delete; |
| 131 | /** Allow instances of this class to be moved */ |
| 132 | CLLogits1DMaxShiftExpSumKernel(CLLogits1DMaxShiftExpSumKernel &&) = default; |
| 133 | /** Allow instances of this class to be moved */ |
| 134 | CLLogits1DMaxShiftExpSumKernel &operator=(CLLogits1DMaxShiftExpSumKernel &&) = default; |
| 135 | /** Set the input and output tensors. |
| 136 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 137 | * @param[in] input Source tensor. Data types supported: F16/F32 |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 138 | * @param[in,out] max Max values tensor. Data types supported: same as @p input |
| 139 | * @param[out] output Destination tensor. Data types supported: same as @p input |
| 140 | * @param[out] sum Sum of 1D logits tensor. Data types supported: same as @p input |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 141 | * @param[in] info Contains information consumed by kernels for softmax described in @ref SoftmaxKernelInfo. |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 142 | */ |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 143 | void configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, const SoftmaxKernelInfo &info); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame^] | 144 | /** Set the input and output tensors. |
| 145 | * |
| 146 | * @param[in] compile_context The compile context to be used. |
| 147 | * @param[in] input Source tensor. Data types supported: F16/F32 |
| 148 | * @param[in,out] max Max values tensor. Data types supported: same as @p input |
| 149 | * @param[out] output Destination tensor. Data types supported: same as @p input |
| 150 | * @param[out] sum Sum of 1D logits tensor. Data types supported: same as @p input |
| 151 | * @param[in] info Contains information consumed by kernels for softmax described in @ref SoftmaxKernelInfo. |
| 152 | */ |
| 153 | void configure(CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, const SoftmaxKernelInfo &info); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 154 | /** Static function to check if given info will lead to a valid configuration of @ref CLLogits1DMaxShiftExpSumKernel |
| 155 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 156 | * @param[in] input Source tensor. Data types supported: F16/F32 |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 157 | * @param[in] max Max values tensor. Data types supported: same as @p input |
| 158 | * @param[in] output Destination tensor. Data types supported: same as @p input |
| 159 | * @param[in] sum Sum of 1D logits tensor. Data types supported: same as @p input |
| 160 | * |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 161 | * @return a status |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 162 | */ |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 163 | static Status validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 164 | /** Checks if the given size is eligible for parallel reduction |
| 165 | * |
| 166 | * @note Serial reduction is launched for width < (_grid_size * _serial_vector_size). |
| 167 | * @note Parallel reduction is launched for width >= (_grid_size * _serial_vector_size) and vector_size is forced to 4. |
| 168 | * |
| 169 | * @param[in] size Size to check |
| 170 | * |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 171 | * @return A two-element tuple where the first element is a boolean specifying if a parallel reduction will be run, |
| 172 | * while the second element is the vector size of the execution. |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 173 | */ |
| 174 | static ParallelReductionInfo is_parallel_reduction(size_t size); |
| 175 | |
| 176 | // Inherited methods overridden: |
| 177 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 178 | |
| 179 | private: |
| 180 | const ICLTensor *_input; |
| 181 | ICLTensor *_max; |
| 182 | ICLTensor *_output; |
| 183 | ICLTensor *_sum; |
| 184 | |
| 185 | private: |
| 186 | static const unsigned int _grid_size; |
| 187 | static const unsigned int _serial_vector_size; |
| 188 | static const unsigned int _parallel_vector_size; |
| 189 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 190 | /** Interface for calculating the final step of the Softmax Layer where each logit value is multiplied by the inverse of the sum of the logits. */ |
| 191 | class CLLogits1DNormKernel : public ICLKernel |
| 192 | { |
| 193 | public: |
| 194 | /** Default constructor */ |
| 195 | CLLogits1DNormKernel(); |
| 196 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 197 | CLLogits1DNormKernel(const CLLogits1DNormKernel &) = delete; |
| 198 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 199 | CLLogits1DNormKernel &operator=(const CLLogits1DNormKernel &) = delete; |
| 200 | /** Allow instances of this class to be moved */ |
| 201 | CLLogits1DNormKernel(CLLogits1DNormKernel &&) = default; |
| 202 | /** Allow instances of this class to be moved */ |
| 203 | CLLogits1DNormKernel &operator=(CLLogits1DNormKernel &&) = default; |
| 204 | /** Set the input and output tensors. |
| 205 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 206 | * @param[in] input Source tensor. Data types supported: S32/F16/F32 |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 207 | * @param[in] sum Sum tensor. Dimensions should be dim(input)-1. Data types supported: same as @p input |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 208 | * @param[out] output Destination tensor. Data types supported: QASYMM8 for S32 @p input, or same as @p input |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 209 | * @param[in] info Contains information consumed by kernels for softmax described in @ref SoftmaxKernelInfo. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 210 | */ |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 211 | void configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, const SoftmaxKernelInfo &info); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame^] | 212 | /** Set the input and output tensors. |
| 213 | * |
| 214 | * @param[in] compile_context The compile context to be used. |
| 215 | * @param[in] input Source tensor. Data types supported: S32/F16/F32 |
| 216 | * @param[in] sum Sum tensor. Dimensions should be dim(input)-1. Data types supported: same as @p input |
| 217 | * @param[out] output Destination tensor. Data types supported: QASYMM8 for S32 @p input, or same as @p input |
| 218 | * @param[in] info Contains information consumed by kernels for softmax described in @ref SoftmaxKernelInfo. |
| 219 | */ |
| 220 | void configure(CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, const SoftmaxKernelInfo &info); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 221 | /** Static function to check if given info will lead to a valid configuration of @ref CLLogits1DNormKernel |
| 222 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 223 | * @param[in] input Source tensor. Data types supported: S32/F16/F32 |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 224 | * @param[in] sum Sum tensor. Dimensions should be dim(input)-1. Data types supported: same as @p input |
| 225 | * @param[in] output Destination tensor. Data types supported: QASYMM8 for S32 @p input, or same as @p input |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 226 | * @param[in] info Contains information consumed by kernels for softmax described in @ref SoftmaxKernelInfo. |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 227 | * |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 228 | * @return a status |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 229 | */ |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 230 | static Status validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, const SoftmaxKernelInfo &info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 231 | |
| 232 | // Inherited methods overridden: |
| 233 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 234 | |
| 235 | private: |
| 236 | const ICLTensor *_input; |
| 237 | const ICLTensor *_sum; |
| 238 | ICLTensor *_output; |
| 239 | }; |
Gian Marco Iodice | f670a0a | 2017-09-18 12:20:45 +0100 | [diff] [blame] | 240 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 241 | #endif /*ARM_COMPUTE_CLSOFTMAXLAYERKERNEL_H */ |