Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +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_NESOFTMAXLAYERKERNEL_H |
| 25 | #define ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 27 | #include "src/core/NEON/INEKernel.h" |
| 28 | #include "src/core/NEON/INESimpleKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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(); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 44 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 45 | NELogits1DMaxKernel(const NELogits1DMaxKernel &) = delete; |
| 46 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 47 | NELogits1DMaxKernel &operator=(const NELogits1DMaxKernel &) = delete; |
| 48 | /** Allow instances of this class to be moved */ |
| 49 | NELogits1DMaxKernel(NELogits1DMaxKernel &&) = default; |
| 50 | /** Allow instances of this class to be moved */ |
| 51 | NELogits1DMaxKernel &operator=(NELogits1DMaxKernel &&) = default; |
| 52 | /** Default destructor */ |
| 53 | ~NELogits1DMaxKernel() = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | /** Set the input and output tensors. |
| 55 | * |
Sheri Zhang | 1f567af | 2020-05-05 11:47:36 +0100 | [diff] [blame] | 56 | * @param[in] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | * @param[out] output Destination tensor. Data types supported: same as @p input |
| 58 | */ |
| 59 | void configure(const ITensor *input, ITensor *output); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 60 | /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DMaxKernel |
| 61 | * |
Sheri Zhang | 1f567af | 2020-05-05 11:47:36 +0100 | [diff] [blame] | 62 | * @param[in] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 63 | * @param[in] output Destination tensor. Data types supported: same as @p input |
| 64 | * |
| 65 | * @return a status |
| 66 | */ |
| 67 | static Status validate(const ITensorInfo *input, const ITensorInfo *output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | |
| 69 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 70 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | BorderSize border_size() const override; |
| 72 | |
| 73 | private: |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 74 | using Logits1DMaxFunction = void(const ITensor &in, ITensor &out, const Window &window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | Logits1DMaxFunction *_func; |
| 78 | BorderSize _border_size; |
| 79 | }; |
| 80 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 81 | /** Interface for softmax computation for QASYMM8 with pre-computed max. */ |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 82 | template <bool IS_LOG = false> |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 83 | class NELogits1DSoftmaxKernel : public INEKernel |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | { |
| 85 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 86 | const char *name() const override |
| 87 | { |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 88 | if(IS_LOG) |
| 89 | { |
| 90 | return "NELogits1DSoftmaxKernel"; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | return "NELogits1DLogSoftmaxKernel"; |
| 95 | } |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 96 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | /** Default constructor */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 98 | NELogits1DSoftmaxKernel(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 100 | NELogits1DSoftmaxKernel(const NELogits1DSoftmaxKernel &) = delete; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 102 | NELogits1DSoftmaxKernel &operator=(const NELogits1DSoftmaxKernel &) = delete; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | /** Allow instances of this class to be moved */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 104 | NELogits1DSoftmaxKernel(NELogits1DSoftmaxKernel &&) = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | /** Allow instances of this class to be moved */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 106 | NELogits1DSoftmaxKernel &operator=(NELogits1DSoftmaxKernel &&) = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | /** Default destructor */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 108 | ~NELogits1DSoftmaxKernel() = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | /** Set the input and output tensors. |
| 110 | * |
Sheri Zhang | 1f567af | 2020-05-05 11:47:36 +0100 | [diff] [blame] | 111 | * @param[in] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 112 | * @param[in] max Max values tensor. Same shape as input with dimension 0 set to 1. |
| 113 | * Data types supported: same as @p input. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 114 | * @param[out] output Destination tensor. Data types supported: same as @p input. |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 115 | * @param[in] beta A scaling factor for the exponent. |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 116 | * |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 117 | * @param tmp Auxiliary tensor. Must be type F32 and same shape as the input. |
| 118 | */ |
| 119 | void configure(const ITensor *input, const ITensor *max, ITensor *output, const float beta, ITensor *tmp); |
| 120 | /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DSoftmaxKernel |
| 121 | * |
Sheri Zhang | 1f567af | 2020-05-05 11:47:36 +0100 | [diff] [blame] | 122 | * @param[in] input Source tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 123 | * @param[in] max Max values tensor info. Same shape as input with dimension 0 set to 1. |
| 124 | * Data types supported: same as @p input. |
| 125 | * @param[in] output Destination tensor info. Data types supported: same as @p input. |
| 126 | * @param[in] beta A scaling factor for the exponent. |
| 127 | * @param[in] tmp Tensor info of auxiliary. Must be type F32 and same shape as the input. |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 128 | * |
| 129 | * @return a status |
| 130 | */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 131 | static Status validate(const ITensorInfo *input, const ITensorInfo *max, |
| 132 | const ITensorInfo *output, const float beta, const ITensorInfo *tmp); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 133 | |
| 134 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 135 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 136 | |
| 137 | private: |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 138 | using LogitsSoftmaxFunction = void(const ITensor &in, const ITensor &max, void *const tmp, ITensor &out, const float beta, |
| 139 | const Window &window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 140 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 141 | LogitsSoftmaxFunction *_func; |
| 142 | const ITensor *_input; |
| 143 | const ITensor *_max; |
| 144 | ITensor *_output; |
| 145 | float _beta; |
| 146 | ITensor *_tmp; //Temporary. Used internally |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | }; |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 148 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 149 | #endif /*ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H */ |