Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 1 | /* |
Omar Al Khatib | 93e743f | 2024-01-02 14:45:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2024 Arm Limited. |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [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 | */ |
Gunes Bayir | fadc9b1 | 2023-11-07 05:43:07 +0000 | [diff] [blame] | 24 | #ifndef ACL_SRC_CPU_OPERATORS_CPUSOFTMAX_H |
| 25 | #define ACL_SRC_CPU_OPERATORS_CPUSOFTMAX_H |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 26 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/experimental/Types.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/TensorInfo.h" |
| 29 | |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 30 | #include "src/cpu/ICpuKernel.h" |
| 31 | #include "src/cpu/ICpuOperator.h" |
| 32 | #include "src/cpu/operators/CpuPermute.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 34 | #include <memory> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace cpu |
| 39 | { |
Gunes Bayir | fadc9b1 | 2023-11-07 05:43:07 +0000 | [diff] [blame] | 40 | class CpuSoftmaxKernel; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 41 | |
| 42 | /** Basic function to compute a SoftmaxLayer and a Log SoftmaxLayer. |
| 43 | * |
| 44 | * Softmax is calculated by : |
| 45 | * @f[ out = exp((x - max(x)) * beta) / sum(exp((x - max(x)) * beta)) @f] |
| 46 | * |
| 47 | * Log Softmax is calculated by : |
| 48 | * @f[ out = (x - max(x) * beta) - log(\sum{e^{x - max(x) * beta}}) @f] |
| 49 | * |
| 50 | * This function runs the following function/kernels: |
| 51 | * -# If axis is not 0: |
| 52 | * -# @ref CpuPermute |
Gunes Bayir | fadc9b1 | 2023-11-07 05:43:07 +0000 | [diff] [blame] | 53 | * -# @ref kernels::CpuSoftmaxKernel |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 54 | */ |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 55 | class CpuSoftmaxGeneric : public ICpuOperator |
| 56 | { |
| 57 | public: |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 58 | CpuSoftmaxGeneric(); |
| 59 | /** Set the input and output tensors. |
| 60 | * |
Gunes Bayir | fadc9b1 | 2023-11-07 05:43:07 +0000 | [diff] [blame] | 61 | * @param[in,out] src Source tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
| 62 | * last value of each row to the nearest multiple. |
| 63 | * @param[out] dst Destination tensor ifo. Data types supported: same as @p input. |
| 64 | * @param[in] beta (Optional) A scaling factor for the exponent. |
| 65 | * @param[in] axis (Optional) The dimension in which to apply the function. E.g. for input of shape 4x5x6 and |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 66 | * axis=1, softmax will be applied to 4x6=24 vectors of size 5. Defaults to 0 |
Gunes Bayir | fadc9b1 | 2023-11-07 05:43:07 +0000 | [diff] [blame] | 67 | * @param[in] is_log True if the operation is log-softmax |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 68 | */ |
Gunes Bayir | fadc9b1 | 2023-11-07 05:43:07 +0000 | [diff] [blame] | 69 | void configure(const ITensorInfo *src, ITensorInfo *dst, float beta = 1.0f, int32_t axis = 0, bool is_log = false); |
Georgios Pinitas | 2eb5d16 | 2021-07-02 09:01:49 +0100 | [diff] [blame] | 70 | /** Static function to check if given info will lead to a valid configuration |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 71 | * |
Georgios Pinitas | 2eb5d16 | 2021-07-02 09:01:49 +0100 | [diff] [blame] | 72 | * Similar to @ref CpuSoftmaxGeneric::configure() |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 73 | * |
| 74 | * @return a status |
| 75 | */ |
Gunes Bayir | fadc9b1 | 2023-11-07 05:43:07 +0000 | [diff] [blame] | 76 | static Status |
| 77 | validate(const ITensorInfo *src, const ITensorInfo *dst, float beta = 1.0f, int32_t axis = 0, bool is_log = false); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 78 | |
| 79 | // Inherited methods overridden: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | void run(ITensorPack &tensors) override; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 81 | experimental::MemoryRequirements workspace() const override; |
| 82 | |
| 83 | private: |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 84 | enum InternalTensorIdx |
| 85 | { |
Gunes Bayir | fadc9b1 | 2023-11-07 05:43:07 +0000 | [diff] [blame] | 86 | TMP = 0, |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 87 | PERMUTED_SRC, |
| 88 | PERMUTED_DST, |
| 89 | COUNT |
| 90 | }; |
| 91 | |
Yair Schwarzbaum | 46d44d2 | 2022-01-12 16:38:58 +0200 | [diff] [blame] | 92 | std::unique_ptr<ICPPKernel> _softmax_kernel; |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 93 | |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 94 | TensorInfo _tmp; |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 95 | |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 96 | experimental::MemoryRequirements _aux_mem{}; |
Omar Al Khatib | 93e743f | 2024-01-02 14:45:07 +0000 | [diff] [blame] | 97 | |
| 98 | unsigned int _axis = 0; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 99 | }; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 100 | |
| 101 | } // namespace cpu |
| 102 | } // namespace arm_compute |
Gunes Bayir | fadc9b1 | 2023-11-07 05:43:07 +0000 | [diff] [blame] | 103 | #endif // ACL_SRC_CPU_OPERATORS_CPUSOFTMAX_H |