Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +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_CLSOFTMAXLAYER_H |
| 25 | #define ARM_COMPUTE_CLSOFTMAXLAYER_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | * |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 46 | * Log Softmax is calculated by : |
| 47 | * @f[ out = (x - max(x) * beta) - \sum{e^{x - max(x) * beta}} @f] |
| 48 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | * This function runs the following kernels: |
| 50 | * -# @ref CLLogits1DMaxKernel |
| 51 | * -# @ref CLLogits1DShiftExpSumKernel |
| 52 | * -# @ref CLLogits1DNormKernel |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 53 | * And if the reduce_end_axis is not 0, the function will use one of the the following kernels to reshape the input and |
| 54 | * perform softmax on the reshaped input: |
| 55 | * -# @ref CLFlattenLayerKernel |
| 56 | * -# @ref CLReshapeLayerKernel |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | */ |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 58 | template <bool IS_LOG = false> |
| 59 | class CLSoftmaxLayerGeneric : public IFunction |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | { |
| 61 | public: |
| 62 | /** Constructor */ |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 63 | CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | /** Set the input and output tensors. |
| 65 | * |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 66 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
| 67 | * @param[out] output Destination tensor. Data types supported: same as @p input |
| 68 | * @param[in] beta (Optional) A scaling factor for the exponent. Defaults to 1.f |
| 69 | * @param[in] reduce_end_axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Defaults to 0. |
| 70 | * It has the purpose of squashing together the first n dimensions till (including) the @p reduce_end_axis. For instance, given a [2x3x4x5] image, |
| 71 | * 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. |
| 72 | * Must be in range [0, input_num_dimensions). |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | */ |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 74 | void configure(const ICLTensor *input, ICLTensor *output, float beta = 1.0f, size_t reduce_end_axis = 0); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 75 | /** Set the input and output tensors. |
| 76 | * |
| 77 | * @param[in] compile_context The compile context to be used. |
| 78 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
| 79 | * @param[out] output Destination tensor. Data types supported: same as @p input |
| 80 | * @param[in] beta (Optional) A scaling factor for the exponent. Defaults to 1.f |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 81 | * @param[in] reduce_end_axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Defaults to 0. |
| 82 | * It has the purpose of squashing together the first n dimensions till (including) the @p reduce_end_axis. For instance, given a [2x3x4x5] image, |
| 83 | * 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. |
| 84 | * Must be in range [0, input_num_dimensions). |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 85 | */ |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 86 | void configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, float beta = 1.0f, size_t reduce_end_axis = 0); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 87 | /** Static function to check if given info will lead to a valid configuration of @ref CLSoftmaxLayer |
| 88 | * |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 89 | * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 |
| 90 | * @param[in] output Destination tensor. Data types supported: same as @p input |
| 91 | * @param[in] beta (Optional) A scaling factor for the exponent. Defaults to 1.f |
| 92 | * @param[in] reduce_end_axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Defaults to 0. |
| 93 | * It has the purpose of squashing together the first n dimensions till (including) the @p reduce_end_axis. For instance, given a [2x3x4x5] image, |
| 94 | * 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. |
| 95 | * Must be in range [0, input_num_dimensions). |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 96 | * @return a status |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 97 | */ |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 98 | static Status validate(const ITensorInfo *input, const ITensorInfo *output, float beta = 1.0f, size_t reduce_end_axis = 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | |
| 100 | // Inherited methods overridden: |
| 101 | void run() override; |
| 102 | |
| 103 | private: |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 104 | /** Utility method to configure the kernels needed to flatten the input |
| 105 | * tensor. |
| 106 | * |
| 107 | * @note This function changes the internal state of this class. In particular, |
| 108 | * it initializes the kernel @p _flatten_kernel and the tensors @p _input_flat and |
| 109 | * @p _output_flat |
| 110 | * |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 111 | * @param[in] input Original source tensor. |
| 112 | * @param[in] output Original destination tensor. |
| 113 | * @param[in] reduce_end_axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Defaults to 0. |
| 114 | * It has the purpose of squashing together the first n dimensions till (including) the @p reduce_end_axis. For instance, given a [2x3x4x5] image, |
| 115 | * 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. |
| 116 | * Must be in range [0, input_num_dimensions). |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 117 | */ |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 118 | void configure_reshape_input_kernel(const ICLTensor *input, const ICLTensor *output, size_t reduce_end_axis); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 119 | /** Utility method to configure the kernels needed to flatten the input |
| 120 | * tensor. |
| 121 | * |
| 122 | * @note This function changes the internal state of this class. In particular, |
| 123 | * it initializes the kernel @p _flatten_kernel and the tensors @p _input_flat and |
| 124 | * @p _output_flat |
| 125 | * |
| 126 | * @param[in] compile_context The compile context to be used. |
| 127 | * @param[in] input Original source tensor. |
| 128 | * @param[in] output Original destination tensor. |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 129 | * @param[in] reduce_end_axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Defaults to 0. |
| 130 | * It has the purpose of squashing together the first n dimensions till (including) the @p reduce_end_axis. For instance, given a [2x3x4x5] image, |
| 131 | * 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. |
| 132 | * Must be in range [0, input_num_dimensions). |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 133 | */ |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 134 | void configure_reshape_input_kernel(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *output, size_t reduce_end_axis); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 135 | |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 136 | MemoryGroup _memory_group; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 137 | CLLogits1DMaxShiftExpSumKernel _max_shift_exp_sum_kernel; |
| 138 | CLLogits1DNormKernel _norm_kernel; |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 139 | std::unique_ptr<ICLKernel> _flatten_kernel_ptr; |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 140 | CLReshapeLayerKernel _reshape_kernel; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 141 | CLTensor _max; |
| 142 | CLTensor _sum; |
| 143 | CLTensor _tmp; |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 144 | CLTensor _input_flattened; |
| 145 | CLTensor _output_flattened; |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 146 | bool _needs_flattening; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | }; |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 148 | |
| 149 | using CLSoftmaxLayer = CLSoftmaxLayerGeneric<false>; |
| 150 | using CLLogSoftmaxLayer = CLSoftmaxLayerGeneric<true>; |
| 151 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 152 | #endif /* ARM_COMPUTE_CLSOFTMAXLAYER_H */ |