Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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_CLSOFTMAXLAYER_H__ |
| 25 | #define __ARM_COMPUTE_CLSOFTMAXLAYER_H__ |
| 26 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/kernels/CLFlattenLayerKernel.h" |
| 28 | #include "arm_compute/core/CL/kernels/CLReshapeLayerKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h" |
| 30 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 31 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/IMemoryManager.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/MemoryGroup.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 34 | |
| 35 | #include <memory> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | class ICLTensor; |
| 40 | |
| 41 | /** Basic function to compute a SoftmaxLayer. |
| 42 | * |
| 43 | * Softmax is calculated by : |
Georgios Pinitas | 388d3ec | 2017-11-02 12:17:56 +0000 | [diff] [blame] | 44 | * @f[ out = exp((x - max(x)) * beta) / sum(exp((x - max(x)) * beta)) @f] |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | * |
| 46 | * This function runs the following kernels: |
| 47 | * -# @ref CLLogits1DMaxKernel |
| 48 | * -# @ref CLLogits1DShiftExpSumKernel |
| 49 | * -# @ref CLLogits1DNormKernel |
| 50 | */ |
| 51 | class CLSoftmaxLayer : public IFunction |
| 52 | { |
| 53 | public: |
| 54 | /** Constructor */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 55 | CLSoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | /** Set the input and output tensors. |
| 57 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 58 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 59 | * @param[out] output Destination tensor. Data types supported: same as @p input |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 60 | * @param[in] beta (Optional) A scaling factor for the exponent. Defaults to 1.f |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 61 | * @param[in] axis (Optional) Reduction axis. It has the purpose of squashing the first @p axis |
| 62 | * dimensions together. For instance, given a [4x4x4x4] image, |
| 63 | * when @p axis is 2, the Softmax reduction will be applied on each of the [4x4] planes of the input image. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | */ |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 65 | void configure(const ICLTensor *input, ICLTensor *output, float beta = 1.0f, size_t axis = 1); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 66 | /** Static function to check if given info will lead to a valid configuration of @ref CLSoftmaxLayer |
| 67 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 68 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 69 | * @param[in] output Destination tensor. Data types supported: same as @p input |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 70 | * @param[in] beta (Optional) A scaling factor for the exponent. Defaults to 1.f |
| 71 | * @param[in] axis (Optional) Reduction axis. It has the purpose of squashing the first @p axis |
| 72 | * dimensions together. For instance, given a [4x4x4x4] image, |
| 73 | * when @p axis is 2, the Softmax reduction will be applied on each of the [4x4] planes of the input image. |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 74 | * @return a status |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 75 | */ |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 76 | static Status validate(const ITensorInfo *input, const ITensorInfo *output, float beta = 1.0f, size_t axis = 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | |
| 78 | // Inherited methods overridden: |
| 79 | void run() override; |
| 80 | |
| 81 | private: |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 82 | /** Utility method to configure the kernels needed to flatten the input |
| 83 | * tensor. |
| 84 | * |
| 85 | * @note This function changes the internal state of this class. In particular, |
| 86 | * it initializes the kernel @p _flatten_kernel and the tensors @p _input_flat and |
| 87 | * @p _output_flat |
| 88 | * |
| 89 | * @param[in] input Original source tensor. |
| 90 | * @param[in] output Original destination tensor. |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 91 | * @param[in] axis (Optional) Reduction axis. It has the purpose of squashing the first @p axis |
| 92 | * dimensions together. For instance, given a [4x4x4x4] image, |
| 93 | * when @p axis is 2, the Softmax reduction will be applied on each of the [4x4] planes of the input image. |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 94 | */ |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 95 | void configure_reshape_input_kernel(const ICLTensor *input, const ICLTensor *output, size_t axis); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 96 | |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 97 | MemoryGroup _memory_group; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 98 | CLLogits1DMaxShiftExpSumKernel _max_shift_exp_sum_kernel; |
| 99 | CLLogits1DNormKernel _norm_kernel; |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 100 | std::unique_ptr<ICLKernel> _flatten_kernel_ptr; |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 101 | CLReshapeLayerKernel _reshape_kernel; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 102 | CLTensor _max; |
| 103 | CLTensor _sum; |
| 104 | CLTensor _tmp; |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 105 | CLTensor _input_flattened; |
| 106 | CLTensor _output_flattened; |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 107 | bool _needs_flattening; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | }; |
| 109 | } |
| 110 | #endif /* __ARM_COMPUTE_CLSOFTMAXLAYER_H__ */ |