Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 46 | /** Static function to check if given info will lead to a valid configuration of @ref CLLogits1DMaxKernel |
| 47 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 48 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 49 | * @param[in] output Destination tensor. Data types supported: same as @p input |
| 50 | * |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 51 | * @return a status |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 52 | */ |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 53 | static Status validate(const ITensorInfo *input, const ITensorInfo *output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 56 | /** Interface for shifting, exponentiating and summing the logits */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | class CLLogits1DShiftExpSumKernel : public ICLKernel |
| 58 | { |
| 59 | public: |
| 60 | /** Default constructor */ |
| 61 | CLLogits1DShiftExpSumKernel(); |
| 62 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 63 | CLLogits1DShiftExpSumKernel(const CLLogits1DShiftExpSumKernel &) = delete; |
| 64 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 65 | CLLogits1DShiftExpSumKernel &operator=(const CLLogits1DShiftExpSumKernel &) = delete; |
| 66 | /** Allow instances of this class to be moved */ |
| 67 | CLLogits1DShiftExpSumKernel(CLLogits1DShiftExpSumKernel &&) = default; |
| 68 | /** Allow instances of this class to be moved */ |
| 69 | CLLogits1DShiftExpSumKernel &operator=(CLLogits1DShiftExpSumKernel &&) = default; |
| 70 | /** Set the input and output tensors. |
| 71 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 72 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 73 | * @param[in] max Max values tensor. Data types supported: same as @p input |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 74 | * @param[out] output Destination tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input |
| 75 | * @param[out] sum Sum of 1D logits tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input |
| 76 | * @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] | 77 | */ |
Pablo Palmier | 48a60f9 | 2017-10-18 11:03:08 +0100 | [diff] [blame] | 78 | void configure(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] | 79 | /** Static function to check if given info will lead to a valid configuration of @ref CLLogits1DShiftExpSumKernel |
| 80 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 81 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 82 | * @param[in] max Max values tensor. Data types supported: same as @p input |
| 83 | * @param[in] output Destination tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input |
| 84 | * @param[in] sum Sum of 1D logits tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input |
| 85 | * |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 86 | * @return a status |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 87 | */ |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 88 | 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] | 89 | |
| 90 | // Inherited methods overridden: |
| 91 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 92 | |
| 93 | private: |
| 94 | const ICLTensor *_input; |
| 95 | const ICLTensor *_max; |
| 96 | ICLTensor *_output; |
| 97 | ICLTensor *_sum; |
| 98 | }; |
| 99 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 100 | /** Interface for max, shifting, exponentiating and summing the logits */ |
| 101 | class CLLogits1DMaxShiftExpSumKernel : public ICLKernel |
| 102 | { |
| 103 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 104 | /** 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] | 105 | using ParallelReductionInfo = std::tuple<bool, unsigned int>; |
| 106 | |
| 107 | public: |
| 108 | /** Default constructor */ |
| 109 | CLLogits1DMaxShiftExpSumKernel(); |
| 110 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 111 | CLLogits1DMaxShiftExpSumKernel(const CLLogits1DMaxShiftExpSumKernel &) = delete; |
| 112 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 113 | CLLogits1DMaxShiftExpSumKernel &operator=(const CLLogits1DMaxShiftExpSumKernel &) = delete; |
| 114 | /** Allow instances of this class to be moved */ |
| 115 | CLLogits1DMaxShiftExpSumKernel(CLLogits1DMaxShiftExpSumKernel &&) = default; |
| 116 | /** Allow instances of this class to be moved */ |
| 117 | CLLogits1DMaxShiftExpSumKernel &operator=(CLLogits1DMaxShiftExpSumKernel &&) = default; |
| 118 | /** Set the input and output tensors. |
| 119 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 120 | * @param[in] input Source tensor. Data types supported: F16/F32 |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 121 | * @param[in,out] max Max values tensor. Data types supported: same as @p input |
| 122 | * @param[out] output Destination tensor. Data types supported: same as @p input |
| 123 | * @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] | 124 | * @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] | 125 | */ |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 126 | void configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, const SoftmaxKernelInfo &info); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 127 | /** Static function to check if given info will lead to a valid configuration of @ref CLLogits1DMaxShiftExpSumKernel |
| 128 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 129 | * @param[in] input Source tensor. Data types supported: F16/F32 |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 130 | * @param[in] max Max values tensor. Data types supported: same as @p input |
| 131 | * @param[in] output Destination tensor. Data types supported: same as @p input |
| 132 | * @param[in] sum Sum of 1D logits tensor. Data types supported: same as @p input |
| 133 | * |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 134 | * @return a status |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 135 | */ |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 136 | 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] | 137 | /** Checks if the given size is eligible for parallel reduction |
| 138 | * |
| 139 | * @note Serial reduction is launched for width < (_grid_size * _serial_vector_size). |
| 140 | * @note Parallel reduction is launched for width >= (_grid_size * _serial_vector_size) and vector_size is forced to 4. |
| 141 | * |
| 142 | * @param[in] size Size to check |
| 143 | * |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 144 | * @return A two-element tuple where the first element is a boolean specifying if a parallel reduction will be run, |
| 145 | * while the second element is the vector size of the execution. |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 146 | */ |
| 147 | static ParallelReductionInfo is_parallel_reduction(size_t size); |
| 148 | |
| 149 | // Inherited methods overridden: |
| 150 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 151 | |
| 152 | private: |
| 153 | const ICLTensor *_input; |
| 154 | ICLTensor *_max; |
| 155 | ICLTensor *_output; |
| 156 | ICLTensor *_sum; |
| 157 | |
| 158 | private: |
| 159 | static const unsigned int _grid_size; |
| 160 | static const unsigned int _serial_vector_size; |
| 161 | static const unsigned int _parallel_vector_size; |
| 162 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | /** 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. */ |
| 164 | class CLLogits1DNormKernel : public ICLKernel |
| 165 | { |
| 166 | public: |
| 167 | /** Default constructor */ |
| 168 | CLLogits1DNormKernel(); |
| 169 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 170 | CLLogits1DNormKernel(const CLLogits1DNormKernel &) = delete; |
| 171 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 172 | CLLogits1DNormKernel &operator=(const CLLogits1DNormKernel &) = delete; |
| 173 | /** Allow instances of this class to be moved */ |
| 174 | CLLogits1DNormKernel(CLLogits1DNormKernel &&) = default; |
| 175 | /** Allow instances of this class to be moved */ |
| 176 | CLLogits1DNormKernel &operator=(CLLogits1DNormKernel &&) = default; |
| 177 | /** Set the input and output tensors. |
| 178 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 179 | * @param[in] input Source tensor. Data types supported: S32/F16/F32 |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 180 | * @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] | 181 | * @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] | 182 | * @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] | 183 | */ |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 184 | void configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, const SoftmaxKernelInfo &info); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 185 | /** Static function to check if given info will lead to a valid configuration of @ref CLLogits1DNormKernel |
| 186 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 187 | * @param[in] input Source tensor. Data types supported: S32/F16/F32 |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 188 | * @param[in] sum Sum tensor. Dimensions should be dim(input)-1. Data types supported: same as @p input |
| 189 | * @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] | 190 | * @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] | 191 | * |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 192 | * @return a status |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 193 | */ |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 194 | 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] | 195 | |
| 196 | // Inherited methods overridden: |
| 197 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 198 | |
| 199 | private: |
| 200 | const ICLTensor *_input; |
| 201 | const ICLTensor *_sum; |
| 202 | ICLTensor *_output; |
| 203 | }; |
Gian Marco Iodice | f670a0a | 2017-09-18 12:20:45 +0100 | [diff] [blame] | 204 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 205 | #endif /*ARM_COMPUTE_CLSOFTMAXLAYERKERNEL_H */ |