blob: 2e70e2aa08b0a299454bfcd85327da7c98081d55 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +00002 * Copyright (c) 2017-2021 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/CL/functions/CLSoftmaxLayer.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Chunosovd6afedc2017-11-06 22:09:45 +070026#include "arm_compute/core/CL/CLHelpers.h"
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000027#include "arm_compute/core/CL/CLKernelLibrary.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Helpers.h"
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000029#include "arm_compute/core/KernelDescriptors.h"
Chunosovd6afedc2017-11-06 22:09:45 +070030#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Utils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032
Manuel Bottini94f799e2021-06-09 16:37:32 +010033#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010034#include "src/gpu/cl/kernels/ClSoftmaxKernel.h"
35#include "src/gpu/cl/operators/ClPermute.h"
36#include "src/gpu/cl/operators/ClSoftmax.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010038namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000040using OperatorType = opencl::ClSoftmax;
41
42template <bool IS_LOG>
43struct CLSoftmaxLayerGeneric<IS_LOG>::Impl
44{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010045 const ICLTensor *src{nullptr};
46 ICLTensor *dst{nullptr};
47 std::unique_ptr<OperatorType> op{nullptr};
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000048 MemoryGroup memory_group{};
Manuel Bottini94f799e2021-06-09 16:37:32 +010049 ITensorPack run_pack{};
50 WorkspaceData<CLTensor> workspace_tensors{};
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000051};
52
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000053template <bool IS_LOG>
54CLSoftmaxLayerGeneric<IS_LOG>::CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000055 : _impl(std::make_unique<Impl>())
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010056{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000057 _impl->memory_group = MemoryGroup(std::move(memory_manager));
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010058}
59
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000060template <bool IS_LOG>
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010061CLSoftmaxLayerGeneric<IS_LOG>::~CLSoftmaxLayerGeneric() = default;
62
63template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010064void CLSoftmaxLayerGeneric<IS_LOG>::configure(const ICLTensor *input, ICLTensor *output, float beta, int32_t axis)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065{
morgolock9c7fed82020-08-05 12:30:56 +010066 configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis);
Manuel Bottini2b84be52020-04-08 10:15:51 +010067}
68
69template <bool IS_LOG>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070void CLSoftmaxLayerGeneric<IS_LOG>::configure(
71 const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, float beta, int32_t axis)
Manuel Bottini2b84be52020-04-08 10:15:51 +010072{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000073 _impl->src = input;
74 _impl->dst = output;
75 _impl->op = std::make_unique<OperatorType>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 SoftmaxKernelInfo softmax_info{beta, IS_LOG, input->info()->data_type(), axis};
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000078 _impl->op->configure(compile_context, *input->info(), *output->info(), softmax_info);
Manuel Bottini94f799e2021-06-09 16:37:32 +010079
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 _impl->run_pack = {{TensorType::ACL_SRC, _impl->src}, {TensorType::ACL_DST, _impl->dst}};
Manuel Bottini94f799e2021-06-09 16:37:32 +010081 _impl->workspace_tensors = manage_workspace<CLTensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082}
83
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000084template <bool IS_LOG>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085Status
86CLSoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000087{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 SoftmaxKernelInfo softmax_info{beta, IS_LOG, input->data_type(), axis};
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000089 return OperatorType::validate(*input, *output, softmax_info);
90}
SiCong Lid004a7a2020-05-28 15:26:41 +010091
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000092template <bool IS_LOG>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010093void CLSoftmaxLayerGeneric<IS_LOG>::run()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000095 // Acquire all the temporaries
96 MemoryGroupResourceScope scope_mg(_impl->memory_group);
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000097 ARM_COMPUTE_ERROR_ON_NULLPTR(_impl->src, _impl->dst);
Manuel Bottini94f799e2021-06-09 16:37:32 +010098 _impl->op->run(_impl->run_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099}
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100100
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000101template class CLSoftmaxLayerGeneric<false>;
102template class CLSoftmaxLayerGeneric<true>;
103
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100104} // namespace arm_compute