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 | * |
| 42 | * @param[in] input Source tensor. Data types supported: QS8, F32. |
| 43 | * @param[out] output Destination tensor. Data types supported: same as @p input |
| 44 | */ |
| 45 | void configure(const ITensor *input, ITensor *output); |
| 46 | |
| 47 | // Inherited methods overridden: |
| 48 | void run(const Window &window) override; |
| 49 | BorderSize border_size() const override; |
| 50 | |
| 51 | private: |
| 52 | using Logits1DMaxFunction = void(const ITensor *in, ITensor *out, const Window &window); |
| 53 | |
| 54 | private: |
| 55 | Logits1DMaxFunction *_func; |
| 56 | BorderSize _border_size; |
| 57 | }; |
| 58 | |
| 59 | /** Interface for shifting the logits values around the max value and exponentiating the result */ |
| 60 | class NELogits1DShiftExpSumKernel : public INEKernel |
| 61 | { |
| 62 | public: |
| 63 | /** Default constructor */ |
| 64 | NELogits1DShiftExpSumKernel(); |
| 65 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 66 | NELogits1DShiftExpSumKernel(const NELogits1DShiftExpSumKernel &) = delete; |
| 67 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 68 | NELogits1DShiftExpSumKernel &operator=(const NELogits1DShiftExpSumKernel &) = delete; |
| 69 | /** Allow instances of this class to be moved */ |
| 70 | NELogits1DShiftExpSumKernel(NELogits1DShiftExpSumKernel &&) = default; |
| 71 | /** Allow instances of this class to be moved */ |
| 72 | NELogits1DShiftExpSumKernel &operator=(NELogits1DShiftExpSumKernel &&) = default; |
| 73 | /** Default destructor */ |
| 74 | ~NELogits1DShiftExpSumKernel() = default; |
| 75 | /** Set the input and output tensors. |
| 76 | * |
| 77 | * @param[in] input Source tensor. Data types supported: QS8, F32. |
| 78 | * @param[in] max Max values tensor. Data types supported: same as @p input. |
| 79 | * @param[out] output Destination tensor. Data types supported: same as @p input. |
| 80 | * @param[out] sum Sum of 1D logits tensor. Data types supported: same as @p input. |
| 81 | */ |
| 82 | void configure(const ITensor *input, const ITensor *max, ITensor *output, ITensor *sum); |
| 83 | |
| 84 | // Inherited methods overridden: |
| 85 | void run(const Window &window) override; |
| 86 | |
| 87 | private: |
| 88 | using Logits1DShiftExpSumFunction = void(const ITensor *in, const ITensor *max, ITensor *out, ITensor *sum, const Window &window); |
| 89 | |
| 90 | private: |
| 91 | Logits1DShiftExpSumFunction *_func; |
| 92 | const ITensor *_input; |
| 93 | const ITensor *_max; |
| 94 | ITensor *_output; |
| 95 | ITensor *_sum; |
| 96 | }; |
| 97 | |
| 98 | /** 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. */ |
| 99 | class NELogits1DNormKernel : public INEKernel |
| 100 | { |
| 101 | public: |
| 102 | /** Default constructor */ |
| 103 | NELogits1DNormKernel(); |
| 104 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 105 | NELogits1DNormKernel(const NELogits1DNormKernel &) = delete; |
| 106 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 107 | NELogits1DNormKernel &operator=(const NELogits1DNormKernel &) = delete; |
| 108 | /** Allow instances of this class to be moved */ |
| 109 | NELogits1DNormKernel(NELogits1DNormKernel &&) = default; |
| 110 | /** Allow instances of this class to be moved */ |
| 111 | NELogits1DNormKernel &operator=(NELogits1DNormKernel &&) = default; |
| 112 | /** Default destructor */ |
| 113 | ~NELogits1DNormKernel() = default; |
| 114 | /** Set the input and output tensors. |
| 115 | * |
| 116 | * @param[in] input Source tensor. Data types supported: QS8, F32. |
| 117 | * @param[in] sum Sum tensor. The number of dimensions should be dim(input)-1. Data types supported: same as @p input. |
| 118 | * @param[out] output Destination tensor. Data types supported: same as @p input. |
| 119 | */ |
| 120 | void configure(const ITensor *input, const ITensor *sum, ITensor *output); |
| 121 | |
| 122 | // Inherited methods overridden: |
| 123 | void run(const Window &window) override; |
| 124 | |
| 125 | private: |
| 126 | using Logits1DNormFunction = void(const ITensor *in, const ITensor *sum, ITensor *out, const Window &window); |
| 127 | |
| 128 | private: |
| 129 | Logits1DNormFunction *_func; |
| 130 | const ITensor *_input; |
| 131 | const ITensor *_sum; |
| 132 | ITensor *_output; |
| 133 | }; |
| 134 | } |
| 135 | #endif /*__ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H__ */ |