1 /*
2 * Copyright (c) 2017-2021 Arm Limited.
3 *
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"
25 #include "arm_compute/core/CL/CLHelpers.h"
26 #include "arm_compute/core/CL/CLKernelLibrary.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/KernelDescriptors.h"
29 #include "arm_compute/core/Types.h"
30 #include "arm_compute/core/Utils.h"
31 #include "src/core/helpers/MemoryHelpers.h"
32 #include "src/gpu/cl/kernels/ClSoftmaxKernel.h"
33 #include "src/gpu/cl/operators/ClPermute.h"
34 #include "src/gpu/cl/operators/ClSoftmax.h"
35
36 namespace arm_compute
37 {
38 using OperatorType = opencl::ClSoftmax;
39
40 template <bool IS_LOG>
41 struct 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{};
47 ITensorPack run_pack{};
48 WorkspaceData<CLTensor> workspace_tensors{};
49 };
50
51 template <bool IS_LOG>
CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)52 CLSoftmaxLayerGeneric<IS_LOG>::CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
53 : _impl(std::make_unique<Impl>())
54 {
55 _impl->memory_group = MemoryGroup(std::move(memory_manager));
56 }
57
58 template <bool IS_LOG>
59 CLSoftmaxLayerGeneric<IS_LOG>::~CLSoftmaxLayerGeneric() = default;
60
61 template <bool IS_LOG>
configure(const ICLTensor * input,ICLTensor * output,float beta,int32_t axis)62 void CLSoftmaxLayerGeneric<IS_LOG>::configure(const ICLTensor *input, ICLTensor *output, float beta, int32_t axis)
63 {
64 configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis);
65 }
66
67 template <bool IS_LOG>
configure(const CLCompileContext & compile_context,const ICLTensor * input,ICLTensor * output,float beta,int32_t axis)68 void CLSoftmaxLayerGeneric<IS_LOG>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, float beta, int32_t axis)
69 {
70 _impl->src = input;
71 _impl->dst = output;
72 _impl->op = std::make_unique<OperatorType>();
73
74 SoftmaxKernelInfo softmax_info{ beta, IS_LOG, input->info()->data_type(), axis };
75 _impl->op->configure(compile_context, *input->info(), *output->info(), softmax_info);
76
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);
79 }
80
81 template <bool IS_LOG>
validate(const ITensorInfo * input,const ITensorInfo * output,float beta,int32_t axis)82 Status CLSoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis)
83 {
84 SoftmaxKernelInfo softmax_info{ beta, IS_LOG, input->data_type(), axis };
85 return OperatorType::validate(*input, *output, softmax_info);
86 }
87
88 template <bool IS_LOG>
run()89 void CLSoftmaxLayerGeneric<IS_LOG>::run()
90 {
91 // Acquire all the temporaries
92 MemoryGroupResourceScope scope_mg(_impl->memory_group);
93 ARM_COMPUTE_ERROR_ON_NULLPTR(_impl->src, _impl->dst);
94 _impl->op->run(_impl->run_pack);
95 }
96
97 template class CLSoftmaxLayerGeneric<false>;
98 template class CLSoftmaxLayerGeneric<true>;
99
100 } // namespace arm_compute
101