blob: 938a10a7c00ea3293a5a17734e05c98bbd025332 [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"
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000031#include "src/core/gpu/cl/kernels/ClSoftmaxKernel.h"
32#include "src/runtime/gpu/cl/operators/ClPermute.h"
33#include "src/runtime/gpu/cl/operators/ClSoftmax.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010035namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000037using OperatorType = opencl::ClSoftmax;
38
39template <bool IS_LOG>
40struct CLSoftmaxLayerGeneric<IS_LOG>::Impl
41{
42 const ICLTensor *src{ nullptr };
43 ICLTensor *dst{ nullptr };
44 std::unique_ptr<OperatorType> op{ nullptr };
45 MemoryGroup memory_group{};
46 std::vector<std::pair<TensorType, std::unique_ptr<CLTensor>>> workspace_tensors{};
47};
48
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000049template <bool IS_LOG>
50CLSoftmaxLayerGeneric<IS_LOG>::CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000051 : _impl(std::make_unique<Impl>())
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010052{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000053 _impl->memory_group = MemoryGroup(std::move(memory_manager));
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010054}
55
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000056template <bool IS_LOG>
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010057CLSoftmaxLayerGeneric<IS_LOG>::~CLSoftmaxLayerGeneric() = default;
58
59template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010060void CLSoftmaxLayerGeneric<IS_LOG>::configure(const ICLTensor *input, ICLTensor *output, float beta, int32_t axis)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061{
morgolock9c7fed82020-08-05 12:30:56 +010062 configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis);
Manuel Bottini2b84be52020-04-08 10:15:51 +010063}
64
65template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010066void 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 +010067{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000068 _impl->src = input;
69 _impl->dst = output;
70 _impl->op = std::make_unique<OperatorType>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000072 SoftmaxKernelInfo softmax_info{ beta, IS_LOG, input->info()->data_type(), axis };
73 _impl->op->configure(compile_context, *input->info(), *output->info(), softmax_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074}
75
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000076template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010077Status CLSoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000078{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000079 SoftmaxKernelInfo softmax_info{ beta, IS_LOG, input->data_type(), axis };
80 return OperatorType::validate(*input, *output, softmax_info);
81}
SiCong Lid004a7a2020-05-28 15:26:41 +010082
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000083template <bool IS_LOG>
84void CLSoftmaxLayerGeneric<IS_LOG>::allocate_workspace()
85{
86 const auto memory_requirements = _impl->op->workspace();
87 std::for_each(memory_requirements.begin(), memory_requirements.end(), [this](const experimental::MemoryInfo & memory_info)
SiCong Li96209c72020-08-21 12:28:30 +010088 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000089 auto tensor_info = TensorInfo{ TensorShape(memory_info.size), 1, DataType::U8 };
90 _impl->workspace_tensors.emplace_back(memory_info.type, std::make_unique<CLTensor>());
91 auto tensor = _impl->workspace_tensors.back().second.get();
92 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor);
93 _impl->memory_group.manage(tensor);
94 tensor->allocator()->init(tensor_info);
95 tensor->allocator()->allocate();
96 });
Georgios Pinitas30902ed2017-11-14 15:32:57 +000097}
98
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000099template <bool IS_LOG>
Manuel Bottini2b84be52020-04-08 10:15:51 +0100100void CLSoftmaxLayerGeneric<IS_LOG>::run()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000102 allocate_workspace();
giuros01efbf6c82018-09-03 09:53:53 +0100103
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000104 // Acquire all the temporaries
105 MemoryGroupResourceScope scope_mg(_impl->memory_group);
106
107 ARM_COMPUTE_ERROR_ON_NULLPTR(_impl->src, _impl->dst);
108
109 ITensorPack pack;
110 pack.add_tensor(TensorType::ACL_SRC, _impl->src);
111 pack.add_tensor(TensorType::ACL_DST, _impl->dst);
112
113 std::for_each(_impl->workspace_tensors.begin(), _impl->workspace_tensors.end(), [&pack](std::pair<TensorType, std::unique_ptr<CLTensor>> &wt)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100114 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000115 auto tensor = wt.second.get();
116 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor);
117 pack.add_tensor(wt.first, tensor);
118 });
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100119
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000120 _impl->op->run(pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121}
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100122
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000123template class CLSoftmaxLayerGeneric<false>;
124template class CLSoftmaxLayerGeneric<true>;
125
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100126} // namespace arm_compute