Compute Library
 21.02
GCSoftmaxLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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  */
25 
29 
30 namespace arm_compute
31 {
32 GCSoftmaxLayer::GCSoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager)
33  : _memory_group(std::move(memory_manager)), _max_kernel(), _shift_exp_sum_kernel(), _norm_kernel(), _max(), _sum(), _tmp()
34 {
35 }
36 
37 void GCSoftmaxLayer::configure(const IGCTensor *input, IGCTensor *output, float beta, int32_t axis)
38 {
39  ARM_COMPUTE_UNUSED(beta, axis);
40 
42  ARM_COMPUTE_ERROR_ON(beta != 1.0f);
43  ARM_COMPUTE_ERROR_ON_MSG(axis != 0, "axis must be 0 for GLES");
44 
45  // Create intermediate tensors shapes
46  _tmp.allocator()->init(TensorInfo(input->info()->tensor_shape(), input->info()->num_channels(), input->info()->data_type()));
47 
48  TensorShape shape = input->info()->tensor_shape();
49  shape.set(0, 1);
50  TensorInfo tensor_info_max_sum(shape, input->info()->num_channels(), input->info()->data_type());
51  _max.allocator()->init(tensor_info_max_sum);
52  _sum.allocator()->init(tensor_info_max_sum);
53 
54  // Manage intermediate buffers
55  _memory_group.manage(&_tmp);
56  _memory_group.manage(&_max);
57  _memory_group.manage(&_sum);
58 
59  // Configure Kernels
60  _max_kernel.configure(input, &_max);
61  _shift_exp_sum_kernel.configure(input, &_max, &_tmp, &_sum);
62  _norm_kernel.configure(&_tmp, &_sum, output);
63 
64  // Allocate intermediate buffers
65  _tmp.allocator()->allocate();
66  _max.allocator()->allocate();
67  _sum.allocator()->allocate();
68 }
69 
71 {
72  MemoryGroupResourceScope scope_mg(_memory_group);
73 
74  GCScheduler::get().dispatch(_max_kernel, false);
76  GCScheduler::get().dispatch(_shift_exp_sum_kernel, false);
78  GCScheduler::get().dispatch(_norm_kernel);
79 }
80 
81 } // namespace arm_compute
Shape of a tensor.
Definition: TensorShape.h:39
void dispatch(IGCKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: GCScheduler.cpp:77
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
void memory_barrier()
Defines a barrier ordering memory transactions.
Definition: GCScheduler.cpp:86
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Interface for GLES Compute tensor.
Definition: IGCTensor.h:35
void init(const TensorInfo &input, size_t alignment=0)
Initialize a tensor based on the passed TensorInfo.
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
static GCScheduler & get()
Access the scheduler singleton.
Definition: GCScheduler.cpp:70
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
GCSoftmaxLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
virtual void allocate()=0
Interface to be implemented by the child class to allocate the tensor.
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
Interface to enqueue GLES kernels and get/set the GLES CommandQueue.
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
void run() override
Run the kernels contained in the function.
void configure(const IGCTensor *input, IGCTensor *output, float beta=1.0f, int32_t axis=0)
Set the input and output tensors.
void configure(const IGCTensor *input, IGCTensor *output)
Set the input and output tensors.
void configure(const IGCTensor *input, const IGCTensor *max, IGCTensor *output, IGCTensor *sum)
Set the input and output tensors.
ITensorAllocator * allocator()
Return a pointer to the tensor&#39;s allocator.
Definition: GCTensor.cpp:34
void configure(const IGCTensor *input, const IGCTensor *sum, IGCTensor *output)
Set the input and output tensors.
virtual size_t num_channels() const =0
The number of channels for each tensor element.
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:79