blob: fe45f65beb17f29843735932653e1eb7d417092b [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{};
Georgios Pinitas856f66e2021-04-22 21:13:21 +010046 std::vector<std::pair<int, std::unique_ptr<CLTensor>>> workspace_tensors{};
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000047};
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);
Sang-Hoon Park72f13bd2021-03-09 15:51:32 +000074 allocate_workspace();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075}
76
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000077template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010078Status CLSoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000079{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000080 SoftmaxKernelInfo softmax_info{ beta, IS_LOG, input->data_type(), axis };
81 return OperatorType::validate(*input, *output, softmax_info);
82}
SiCong Lid004a7a2020-05-28 15:26:41 +010083
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000084template <bool IS_LOG>
85void CLSoftmaxLayerGeneric<IS_LOG>::allocate_workspace()
86{
87 const auto memory_requirements = _impl->op->workspace();
88 std::for_each(memory_requirements.begin(), memory_requirements.end(), [this](const experimental::MemoryInfo & memory_info)
SiCong Li96209c72020-08-21 12:28:30 +010089 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000090 auto tensor_info = TensorInfo{ TensorShape(memory_info.size), 1, DataType::U8 };
Georgios Pinitas856f66e2021-04-22 21:13:21 +010091 _impl->workspace_tensors.emplace_back(memory_info.slot, std::make_unique<CLTensor>());
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000092 auto tensor = _impl->workspace_tensors.back().second.get();
93 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor);
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000094 tensor->allocator()->init(tensor_info);
Sang-Hoon Park72f13bd2021-03-09 15:51:32 +000095 _impl->memory_group.manage(tensor);
Sang-Hoon Parkd7de9c52021-03-10 17:03:40 +000096 });
97
Georgios Pinitas856f66e2021-04-22 21:13:21 +010098 std::for_each(_impl->workspace_tensors.begin(), _impl->workspace_tensors.end(), [](std::pair<int, std::unique_ptr<CLTensor>> &wt)
Sang-Hoon Parkd7de9c52021-03-10 17:03:40 +000099 {
100 auto tensor = wt.second.get();
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000101 tensor->allocator()->allocate();
102 });
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000103}
104
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000105template <bool IS_LOG>
Manuel Bottini2b84be52020-04-08 10:15:51 +0100106void CLSoftmaxLayerGeneric<IS_LOG>::run()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000108 // Acquire all the temporaries
109 MemoryGroupResourceScope scope_mg(_impl->memory_group);
110
111 ARM_COMPUTE_ERROR_ON_NULLPTR(_impl->src, _impl->dst);
112
113 ITensorPack pack;
114 pack.add_tensor(TensorType::ACL_SRC, _impl->src);
115 pack.add_tensor(TensorType::ACL_DST, _impl->dst);
116
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100117 std::for_each(_impl->workspace_tensors.begin(), _impl->workspace_tensors.end(), [&pack](std::pair<int, std::unique_ptr<CLTensor>> &wt)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100118 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000119 auto tensor = wt.second.get();
120 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor);
121 pack.add_tensor(wt.first, tensor);
122 });
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100123
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000124 _impl->op->run(pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125}
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100126
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000127template class CLSoftmaxLayerGeneric<false>;
128template class CLSoftmaxLayerGeneric<true>;
129
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100130} // namespace arm_compute