Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +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 | 7068f99 | 2017-10-26 15:23:08 +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_GCSOFTMAXLAYER_H |
| 25 | #define ARM_COMPUTE_GCSOFTMAXLAYER_H |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCSoftmaxLayerKernel.h" |
| 28 | #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h" |
| 29 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/MemoryGroup.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | class IGCTensor; |
| 35 | |
| 36 | /** Basic function to compute a SoftmaxLayer. |
| 37 | * |
| 38 | * Softmax is calculated by : |
| 39 | * @f[ out = exp(x - max(x)) / sum(exp(x - max(x))) @f] |
| 40 | * |
| 41 | * This function runs the following kernels: |
| 42 | * -# @ref GCLogits1DMaxKernel |
| 43 | * -# @ref GCLogits1DShiftExpSumKernel |
| 44 | * -# @ref GCLogits1DNormKernel |
| 45 | */ |
| 46 | class GCSoftmaxLayer : public IFunction |
| 47 | { |
| 48 | public: |
| 49 | /** Constructor */ |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 50 | GCSoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 51 | /** Set the input and output tensors. |
| 52 | * |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 53 | * @param[in] input Source tensor. Data types supported: F16/F32 |
| 54 | * @param[out] output Destination tensor. Data types supported: same as @p input |
| 55 | * @param[in] beta (Optional) A scaling factor for the exponent. Only beta = 1 is supported |
| 56 | * @param[in] reduce_end_axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Defaults to 0. |
| 57 | * It has the purpose of squashing together the first n dimensions till (including) the @p reduce_end_axis. For instance, given a [2x3x4x5] image, |
| 58 | * when @p reduce_end_axis is 1, the reduction will be applied to axes 0 and 1, and the Softmax op will be applied on each of the [2x3] planes of the input image. |
| 59 | * Must be in range [0, input_num_dimensions). |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 60 | * |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 61 | * @note The value of @p reduce_end_axis must be always 0 for GLES |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 62 | */ |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 63 | void configure(const IGCTensor *input, IGCTensor *output, float beta = 1.0f, size_t reduce_end_axis = 0); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 64 | |
| 65 | // Inherited methods overridden: |
| 66 | void run() override; |
| 67 | |
| 68 | private: |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 69 | MemoryGroup _memory_group; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 70 | GCLogits1DMaxKernel _max_kernel; |
| 71 | GCLogits1DShiftExpSumKernel _shift_exp_sum_kernel; |
| 72 | GCLogits1DNormKernel _norm_kernel; |
| 73 | GCTensor _max; |
| 74 | GCTensor _sum; |
| 75 | GCTensor _tmp; |
| 76 | }; |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 77 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 78 | #endif /* ARM_COMPUTE_GCSOFTMAXLAYER_H */ |