Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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 | #include "arm_compute/runtime/NEON/functions/NESoftmaxLayer.h" |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 25 | #include "arm_compute/core/Validate.h" |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame^] | 26 | #include "arm_compute/runtime/MemoryGroup.h" |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 27 | #include "arm_compute/runtime/Tensor.h" |
| 28 | #include "src/core/cpu/kernels/CpuSoftmaxKernel.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 29 | #include "src/core/helpers/SoftmaxHelpers.h" |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 30 | #include "src/runtime/cpu/operators/CpuSoftmax.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 32 | namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | { |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 34 | template <bool IS_LOG> |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 35 | struct NESoftmaxLayerGeneric<IS_LOG>::Impl |
| 36 | { |
| 37 | const ITensor *src{ nullptr }; |
| 38 | ITensor *dst{ nullptr }; |
| 39 | Tensor max{ nullptr }; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 40 | std::unique_ptr<cpu::CpuSoftmaxGeneric<IS_LOG>> op{ nullptr }; |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame^] | 41 | MemoryGroup memory_group{}; |
| 42 | ITensorPack run_pack{}; |
| 43 | WorkspaceData<Tensor> workspace_tensors{}; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 44 | }; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 45 | |
| 46 | template <bool IS_LOG> |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 47 | NESoftmaxLayerGeneric<IS_LOG>::NESoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager) |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame^] | 48 | : _impl(std::make_unique<Impl>()) |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 49 | { |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame^] | 50 | _impl->memory_group = MemoryGroup(std::move(memory_manager)); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 53 | template <bool IS_LOG> |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 54 | NESoftmaxLayerGeneric<IS_LOG>::NESoftmaxLayerGeneric(NESoftmaxLayerGeneric &&) = default; |
| 55 | template <bool IS_LOG> |
| 56 | NESoftmaxLayerGeneric<IS_LOG> &NESoftmaxLayerGeneric<IS_LOG>::operator=(NESoftmaxLayerGeneric &&) = default; |
| 57 | template <bool IS_LOG> |
| 58 | NESoftmaxLayerGeneric<IS_LOG>::~NESoftmaxLayerGeneric() = default; |
| 59 | |
| 60 | template <bool IS_LOG> |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 61 | void NESoftmaxLayerGeneric<IS_LOG>::configure(ITensor *input, ITensor *output, float beta, int32_t axis) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 63 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 65 | _impl->src = input; |
| 66 | _impl->dst = output; |
| 67 | _impl->op = std::make_unique<cpu::CpuSoftmaxGeneric<IS_LOG>>(); |
| 68 | _impl->op->configure(input->info(), output->info(), beta, axis); |
Sheri Zhang | 1f567af | 2020-05-05 11:47:36 +0100 | [diff] [blame] | 69 | |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame^] | 70 | _impl->run_pack = { { TensorType::ACL_SRC, _impl->src }, { TensorType::ACL_DST, _impl->dst } }; |
| 71 | _impl->workspace_tensors = manage_workspace<Tensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 74 | template <bool IS_LOG> |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 75 | Status NESoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 76 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 78 | ARM_COMPUTE_RETURN_ON_ERROR(cpu::CpuSoftmaxGeneric<IS_LOG>::validate(input, output, beta, axis)); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 79 | return Status{}; |
| 80 | } |
| 81 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 82 | template <bool IS_LOG> |
| 83 | void NESoftmaxLayerGeneric<IS_LOG>::run() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | { |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame^] | 85 | // Acquire all the temporaries |
| 86 | MemoryGroupResourceScope scope_mg(_impl->memory_group); |
| 87 | ARM_COMPUTE_ERROR_ON_NULLPTR(_impl->src, _impl->dst); |
| 88 | _impl->op->run(_impl->run_pack); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | } |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 90 | |
| 91 | template class NESoftmaxLayerGeneric<false>; |
| 92 | template class NESoftmaxLayerGeneric<true>; |
| 93 | |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 94 | } // namespace arm_compute |