Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017-2018 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 | */ |
| 24 | #ifndef __ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H__ |
| 25 | #define __ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H__ |
| 26 | |
| 27 | #include "arm_compute/core/NEON/INEKernel.h" |
| 28 | #include "arm_compute/core/NEON/INESimpleKernel.h" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | class ITensor; |
| 33 | |
| 34 | /** Interface for the identifying the max value of 1D Logits */ |
| 35 | class NELogits1DMaxKernel : public INESimpleKernel |
| 36 | { |
| 37 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame^] | 38 | const char *name() const override |
| 39 | { |
| 40 | return "NELogits1DMaxKernel"; |
| 41 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | /** Default constructor */ |
| 43 | NELogits1DMaxKernel(); |
| 44 | /** Set the input and output tensors. |
| 45 | * |
Pablo Tello | b49a715 | 2017-07-11 16:31:35 +0100 | [diff] [blame] | 46 | * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | * @param[out] output Destination tensor. Data types supported: same as @p input |
| 48 | */ |
| 49 | void configure(const ITensor *input, ITensor *output); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 50 | /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DMaxKernel |
| 51 | * |
| 52 | * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32 |
| 53 | * @param[in] output Destination tensor. Data types supported: same as @p input |
| 54 | * |
| 55 | * @return a status |
| 56 | */ |
| 57 | static Status validate(const ITensorInfo *input, const ITensorInfo *output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | |
| 59 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 60 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | BorderSize border_size() const override; |
| 62 | |
| 63 | private: |
| 64 | using Logits1DMaxFunction = void(const ITensor *in, ITensor *out, const Window &window); |
| 65 | |
| 66 | private: |
| 67 | Logits1DMaxFunction *_func; |
| 68 | BorderSize _border_size; |
| 69 | }; |
| 70 | |
| 71 | /** Interface for shifting the logits values around the max value and exponentiating the result */ |
| 72 | class NELogits1DShiftExpSumKernel : public INEKernel |
| 73 | { |
| 74 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame^] | 75 | const char *name() const override |
| 76 | { |
| 77 | return "NELogits1DShiftExpSumKernel"; |
| 78 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | /** Default constructor */ |
| 80 | NELogits1DShiftExpSumKernel(); |
| 81 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 82 | NELogits1DShiftExpSumKernel(const NELogits1DShiftExpSumKernel &) = delete; |
| 83 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 84 | NELogits1DShiftExpSumKernel &operator=(const NELogits1DShiftExpSumKernel &) = delete; |
| 85 | /** Allow instances of this class to be moved */ |
| 86 | NELogits1DShiftExpSumKernel(NELogits1DShiftExpSumKernel &&) = default; |
| 87 | /** Allow instances of this class to be moved */ |
| 88 | NELogits1DShiftExpSumKernel &operator=(NELogits1DShiftExpSumKernel &&) = default; |
| 89 | /** Default destructor */ |
| 90 | ~NELogits1DShiftExpSumKernel() = default; |
| 91 | /** Set the input and output tensors. |
| 92 | * |
Pablo Tello | b49a715 | 2017-07-11 16:31:35 +0100 | [diff] [blame] | 93 | * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | * @param[in] max Max values tensor. Data types supported: same as @p input. |
| 95 | * @param[out] output Destination tensor. Data types supported: same as @p input. |
| 96 | * @param[out] sum Sum of 1D logits tensor. Data types supported: same as @p input. |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 97 | * @param[in] beta (Optional) A scaling factor for the exponent. QS8/QS16 only support a beta value of 1. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | */ |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 99 | void configure(const ITensor *input, const ITensor *max, ITensor *output, ITensor *sum, float beta = 1.0f); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 100 | /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DShiftExpSumKernel |
| 101 | * |
| 102 | * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32 |
| 103 | * @param[in] max Max values tensor. Data types supported: same as @p input |
| 104 | * @param[in] output Destination tensor. Data types supported: same as @p input. |
| 105 | * @param[in] sum Sum of 1D logits tensor. Data types supported: same as @p input. |
| 106 | * @param[in] beta (Optional) A scaling factor for the exponent. QS8/QS16 only support a beta value of 1. |
| 107 | * |
| 108 | * @return a status |
| 109 | */ |
| 110 | static Status validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum, float beta = 1.0f); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | |
| 112 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 113 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 114 | |
| 115 | private: |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 116 | using Logits1DShiftExpSumFunction = void(const ITensor *in, const ITensor *max, ITensor *out, ITensor *sum, const Window &window, float beta); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | |
| 118 | private: |
| 119 | Logits1DShiftExpSumFunction *_func; |
| 120 | const ITensor *_input; |
| 121 | const ITensor *_max; |
| 122 | ITensor *_output; |
| 123 | ITensor *_sum; |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 124 | float _beta; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | /** 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. */ |
| 128 | class NELogits1DNormKernel : public INEKernel |
| 129 | { |
| 130 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame^] | 131 | const char *name() const override |
| 132 | { |
| 133 | return "NELogits1DNormKernel"; |
| 134 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | /** Default constructor */ |
| 136 | NELogits1DNormKernel(); |
| 137 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 138 | NELogits1DNormKernel(const NELogits1DNormKernel &) = delete; |
| 139 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 140 | NELogits1DNormKernel &operator=(const NELogits1DNormKernel &) = delete; |
| 141 | /** Allow instances of this class to be moved */ |
| 142 | NELogits1DNormKernel(NELogits1DNormKernel &&) = default; |
| 143 | /** Allow instances of this class to be moved */ |
| 144 | NELogits1DNormKernel &operator=(NELogits1DNormKernel &&) = default; |
| 145 | /** Default destructor */ |
| 146 | ~NELogits1DNormKernel() = default; |
| 147 | /** Set the input and output tensors. |
| 148 | * |
Pablo Tello | b49a715 | 2017-07-11 16:31:35 +0100 | [diff] [blame] | 149 | * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | * @param[in] sum Sum tensor. The number of dimensions should be dim(input)-1. Data types supported: same as @p input. |
| 151 | * @param[out] output Destination tensor. Data types supported: same as @p input. |
| 152 | */ |
| 153 | void configure(const ITensor *input, const ITensor *sum, ITensor *output); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 154 | /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DNormKernel |
| 155 | * |
| 156 | * @param[in] input Source tensor. Data types supported: QS8/QS16/S32/F16/F32 |
| 157 | * @param[in] sum Sum tensor. The number of dimensions should be dim(input)-1. Data types supported: same as @p input. |
| 158 | * @param[in] output Destination tensor. Data types supported: same as @p input. |
| 159 | * |
| 160 | * @return a status |
| 161 | */ |
| 162 | static Status validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | |
| 164 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 165 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | |
| 167 | private: |
| 168 | using Logits1DNormFunction = void(const ITensor *in, const ITensor *sum, ITensor *out, const Window &window); |
| 169 | |
| 170 | private: |
| 171 | Logits1DNormFunction *_func; |
| 172 | const ITensor *_input; |
| 173 | const ITensor *_sum; |
| 174 | ITensor *_output; |
| 175 | }; |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 176 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | #endif /*__ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H__ */ |