blob: d52352fc8d5b607655890f1af3e1d152121e260e [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"
Chunosovd6afedc2017-11-06 22:09:45 +070025#include "arm_compute/core/CL/CLHelpers.h"
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000026#include "arm_compute/core/CL/CLKernelLibrary.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Helpers.h"
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000028#include "arm_compute/core/KernelDescriptors.h"
Chunosovd6afedc2017-11-06 22:09:45 +070029#include "arm_compute/core/Types.h"
30#include "arm_compute/core/Utils.h"
Manuel Bottini94f799e2021-06-09 16:37:32 +010031#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010032#include "src/gpu/cl/kernels/ClSoftmaxKernel.h"
33#include "src/gpu/cl/operators/ClPermute.h"
34#include "src/gpu/cl/operators/ClSoftmax.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010036namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000038using OperatorType = opencl::ClSoftmax;
39
40template <bool IS_LOG>
41struct CLSoftmaxLayerGeneric<IS_LOG>::Impl
42{
43 const ICLTensor *src{ nullptr };
44 ICLTensor *dst{ nullptr };
45 std::unique_ptr<OperatorType> op{ nullptr };
46 MemoryGroup memory_group{};
Manuel Bottini94f799e2021-06-09 16:37:32 +010047 ITensorPack run_pack{};
48 WorkspaceData<CLTensor> workspace_tensors{};
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000049};
50
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000051template <bool IS_LOG>
52CLSoftmaxLayerGeneric<IS_LOG>::CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000053 : _impl(std::make_unique<Impl>())
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010054{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000055 _impl->memory_group = MemoryGroup(std::move(memory_manager));
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010056}
57
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000058template <bool IS_LOG>
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010059CLSoftmaxLayerGeneric<IS_LOG>::~CLSoftmaxLayerGeneric() = default;
60
61template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010062void CLSoftmaxLayerGeneric<IS_LOG>::configure(const ICLTensor *input, ICLTensor *output, float beta, int32_t axis)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063{
morgolock9c7fed82020-08-05 12:30:56 +010064 configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis);
Manuel Bottini2b84be52020-04-08 10:15:51 +010065}
66
67template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010068void CLSoftmaxLayerGeneric<IS_LOG>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, float beta, int32_t axis)
Manuel Bottini2b84be52020-04-08 10:15:51 +010069{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000070 _impl->src = input;
71 _impl->dst = output;
72 _impl->op = std::make_unique<OperatorType>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000074 SoftmaxKernelInfo softmax_info{ beta, IS_LOG, input->info()->data_type(), axis };
75 _impl->op->configure(compile_context, *input->info(), *output->info(), softmax_info);
Manuel Bottini94f799e2021-06-09 16:37:32 +010076
77 _impl->run_pack = { { TensorType::ACL_SRC, _impl->src }, { TensorType::ACL_DST, _impl->dst } };
78 _impl->workspace_tensors = manage_workspace<CLTensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079}
80
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000081template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010082Status CLSoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000083{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000084 SoftmaxKernelInfo softmax_info{ beta, IS_LOG, input->data_type(), axis };
85 return OperatorType::validate(*input, *output, softmax_info);
86}
SiCong Lid004a7a2020-05-28 15:26:41 +010087
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000088template <bool IS_LOG>
Manuel Bottini2b84be52020-04-08 10:15:51 +010089void CLSoftmaxLayerGeneric<IS_LOG>::run()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000091 // Acquire all the temporaries
92 MemoryGroupResourceScope scope_mg(_impl->memory_group);
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000093 ARM_COMPUTE_ERROR_ON_NULLPTR(_impl->src, _impl->dst);
Manuel Bottini94f799e2021-06-09 16:37:32 +010094 _impl->op->run(_impl->run_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095}
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010096
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000097template class CLSoftmaxLayerGeneric<false>;
98template class CLSoftmaxLayerGeneric<true>;
99
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100100} // namespace arm_compute