Compute Library
 21.11
CLSoftmaxLayer.cpp
Go to the documentation of this file.
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  */
29 #include "arm_compute/core/Types.h"
30 #include "arm_compute/core/Utils.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>
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>
60 
61 template <bool IS_LOG>
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>
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>
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>
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
void run() override
Run the kernels contained in the function.
CLSoftmaxLayerGeneric(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
void configure(const ICLTensor *input, ICLTensor *output, float beta=1.0f, int32_t axis=0)
Set the input and output tensors.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
Status class.
Definition: Error.h:52
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
Basic function to compute a SoftmaxLayer.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, float beta=1.0f, int32_t axis=0)
Static function to check if given info will lead to a valid configuration of CLSoftmaxLayer.
CLCompileContext class.
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
opencl::ClGemm OperatorType
Definition: CLGEMM.cpp:40
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
Static function to check if given info will lead to a valid configuration.
Definition: ClGemm.cpp:517
Descriptor used by the softmax kernels.
~CLSoftmaxLayerGeneric()
Default destructor.