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