blob: be588c5b52e145d47ec26343393d98999cad6e0c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gunes Bayirfadc9b12023-11-07 05:43:07 +00002 * Copyright (c) 2017-2021, 2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Michalis Spyrou373b4072021-01-20 16:41:12 +000026#include "arm_compute/core/Validate.h"
Manuel Bottini94f799e2021-06-09 16:37:32 +010027#include "arm_compute/runtime/MemoryGroup.h"
Michalis Spyrou373b4072021-01-20 16:41:12 +000028#include "arm_compute/runtime/Tensor.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029
Georgios Pinitasa3417912021-06-17 18:51:30 +010030#include "src/core/helpers/MemoryHelpers.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/helpers/SoftmaxHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010032#include "src/cpu/operators/CpuSoftmax.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
Manuel Bottini678d83a2019-01-07 16:05:36 +000034namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035{
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010036template <bool IS_LOG>
Michalis Spyrou373b4072021-01-20 16:41:12 +000037struct NESoftmaxLayerGeneric<IS_LOG>::Impl
38{
Gunes Bayirfadc9b12023-11-07 05:43:07 +000039 const ITensor *src{nullptr};
40 ITensor *dst{nullptr};
41 std::unique_ptr<cpu::CpuSoftmaxGeneric> op{nullptr};
42 MemoryGroup memory_group{};
43 ITensorPack run_pack{};
44 WorkspaceData<Tensor> workspace_tensors{};
Michalis Spyrou373b4072021-01-20 16:41:12 +000045};
Michalis Spyrouebcebf12020-10-21 00:04:14 +010046
47template <bool IS_LOG>
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010048NESoftmaxLayerGeneric<IS_LOG>::NESoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
Manuel Bottini94f799e2021-06-09 16:37:32 +010049 : _impl(std::make_unique<Impl>())
Manuel Bottini678d83a2019-01-07 16:05:36 +000050{
Manuel Bottini94f799e2021-06-09 16:37:32 +010051 _impl->memory_group = MemoryGroup(std::move(memory_manager));
Manuel Bottini678d83a2019-01-07 16:05:36 +000052}
53
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010054template <bool IS_LOG>
Michalis Spyrou373b4072021-01-20 16:41:12 +000055NESoftmaxLayerGeneric<IS_LOG>::NESoftmaxLayerGeneric(NESoftmaxLayerGeneric &&) = default;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056template <bool IS_LOG>
Michalis Spyrou373b4072021-01-20 16:41:12 +000057NESoftmaxLayerGeneric<IS_LOG> &NESoftmaxLayerGeneric<IS_LOG>::operator=(NESoftmaxLayerGeneric &&) = default;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010058template <bool IS_LOG>
Michalis Spyrou373b4072021-01-20 16:41:12 +000059NESoftmaxLayerGeneric<IS_LOG>::~NESoftmaxLayerGeneric() = default;
60
61template <bool IS_LOG>
morgolock9c7fed82020-08-05 12:30:56 +010062void NESoftmaxLayerGeneric<IS_LOG>::configure(ITensor *input, ITensor *output, float beta, int32_t axis)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063{
Michalis Spyrouafa5d812017-11-30 14:25:57 +000064 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065
Michalis Spyrou373b4072021-01-20 16:41:12 +000066 _impl->src = input;
67 _impl->dst = output;
Gunes Bayirfadc9b12023-11-07 05:43:07 +000068 _impl->op = std::make_unique<cpu::CpuSoftmaxGeneric>();
69 _impl->op->configure(input->info(), output->info(), beta, axis, IS_LOG);
Sheri Zhang1f567af2020-05-05 11:47:36 +010070
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 _impl->run_pack = {{TensorType::ACL_SRC, _impl->src}, {TensorType::ACL_DST, _impl->dst}};
Manuel Bottini94f799e2021-06-09 16:37:32 +010072 _impl->workspace_tensors = manage_workspace<Tensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073}
74
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010075template <bool IS_LOG>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076Status
77NESoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis)
Michalis Spyrouafa5d812017-11-30 14:25:57 +000078{
Michalis Spyrouafa5d812017-11-30 14:25:57 +000079 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Gunes Bayirfadc9b12023-11-07 05:43:07 +000080 ARM_COMPUTE_RETURN_ON_ERROR(cpu::CpuSoftmaxGeneric::validate(input, output, beta, axis, IS_LOG));
Michalis Spyrouafa5d812017-11-30 14:25:57 +000081 return Status{};
82}
83
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010084template <bool IS_LOG>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085void NESoftmaxLayerGeneric<IS_LOG>::run()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086{
Manuel Bottini94f799e2021-06-09 16:37:32 +010087 // Acquire all the temporaries
88 MemoryGroupResourceScope scope_mg(_impl->memory_group);
89 ARM_COMPUTE_ERROR_ON_NULLPTR(_impl->src, _impl->dst);
90 _impl->op->run(_impl->run_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091}
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010092
93template class NESoftmaxLayerGeneric<false>;
94template class NESoftmaxLayerGeneric<true>;
95
Michalis Spyroubcd23522020-05-21 15:02:36 +010096} // namespace arm_compute