Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +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_NESOFTMAXLAYER_H__ |
| 25 | #define __ARM_COMPUTE_NESOFTMAXLAYER_H__ |
| 26 | |
| 27 | #include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h" |
| 28 | #include "arm_compute/core/NEON/kernels/NESoftmaxLayerKernel.h" |
| 29 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/MemoryGroup.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/Tensor.h" |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | class ITensor; |
| 36 | |
| 37 | /** Basic function to compute a SoftmaxLayer. |
| 38 | * |
| 39 | * Softmax is calculated by : |
| 40 | * @f[ out = \frac{e^{x - max(x)}}{\sum{e^{x - max(x)}}} @f] |
| 41 | * |
| 42 | * This function runs the following kernels: |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 43 | * -# @ref NEFillBorderKernel |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | * -# @ref NELogits1DMaxKernel |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 45 | * -# @ref NELogits1DSoftmaxKernel |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | */ |
| 47 | class NESoftmaxLayer : public IFunction |
| 48 | { |
| 49 | public: |
| 50 | /** Constructor */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 51 | NESoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | /** Set the input and output tensors. |
| 53 | * |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 54 | * @param[in,out] input Source tensor. Data types supported: QASYMM8/QS8/QS16/F16/F32. If the width is not a |
| 55 | * multiple of the internal processing block size, @ref NEFillBorderKernel replicates the |
| 56 | * last value of each row to the nearest multiple. |
| 57 | * @param[out] output Destination tensor. Data types supported: same as @p input. |
| 58 | * @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] | 59 | */ |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 60 | void configure(ITensor *input, ITensor *output, float beta = 1.0f); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 61 | /** Static function to check if given info will lead to a valid configuration of @ref NESoftmaxLayer |
| 62 | * |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 63 | * @param[in] input Source tensor. Data types supported: QASYMM8/QS8/QS16/F16/F32. |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 64 | * @param[in] output Destination tensor. Data types supported: same as @p input |
| 65 | * @param[in] beta (Optional) A scaling factor for the exponent. QS8/QS16 only support a beta value of 1. |
| 66 | * |
| 67 | * @return a status |
| 68 | */ |
| 69 | static Status validate(const ITensorInfo *input, const ITensorInfo *output, float beta = 1.0f); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | |
| 71 | // Inherited methods overridden: |
| 72 | void run() override; |
| 73 | |
| 74 | private: |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 75 | MemoryGroup _memory_group; |
| 76 | NELogits1DMaxKernel _max_kernel; |
| 77 | NELogits1DSoftmaxKernel _softmax_kernel; |
| 78 | NEFillBorderKernel _fill_border_kernel; |
| 79 | Tensor _max; |
| 80 | Tensor _tmp; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | }; |
| 82 | } |
| 83 | #endif /* __ARM_COMPUTE_NESOFTMAXLAYER_H__ */ |