Compute Library
 19.08
GCSoftmaxLayer Class Reference

Basic function to compute a SoftmaxLayer. More...

#include <GCSoftmaxLayer.h>

Collaboration diagram for GCSoftmaxLayer:
[legend]

Public Member Functions

 GCSoftmaxLayer (std::shared_ptr< IMemoryManager > memory_manager=nullptr)
 Constructor. More...
 
void configure (const IGCTensor *input, IGCTensor *output, float beta=1.0f, size_t axis=1)
 Set the input and output tensors. More...
 
void run () override
 Run the kernels contained in the function. More...
 
- Public Member Functions inherited from IFunction
virtual ~IFunction ()=default
 Destructor. More...
 
virtual void prepare ()
 Prepare the function for executing. More...
 

Detailed Description

Basic function to compute a SoftmaxLayer.

Softmax is calculated by :

\[ out = exp(x - max(x)) / sum(exp(x - max(x))) \]

This function runs the following kernels:

  1. GCLogits1DMaxKernel
  2. GCLogits1DShiftExpSumKernel
  3. GCLogits1DNormKernel

Definition at line 46 of file GCSoftmaxLayer.h.

Constructor & Destructor Documentation

◆ GCSoftmaxLayer()

GCSoftmaxLayer ( std::shared_ptr< IMemoryManager memory_manager = nullptr)

Constructor.

Definition at line 32 of file GCSoftmaxLayer.cpp.

33  : _memory_group(std::move(memory_manager)), _max_kernel(), _shift_exp_sum_kernel(), _norm_kernel(), _max(), _sum(), _tmp()
34 {
35 }

Member Function Documentation

◆ configure()

void configure ( const IGCTensor input,
IGCTensor output,
float  beta = 1.0f,
size_t  axis = 1 
)

Set the input and output tensors.

Parameters
[in]inputSource tensor. Data types supported: F16/F32
[out]outputDestination tensor. Data types supported: same as input
[in]beta(Optional) A scaling factor for the exponent. Only beta = 1 is supported
[in]axis(Optional) Reduction axis. It has the purpose of squashing the first axis dimensions together. For instance, given a [4x4x4x4] image, when axis is 2, the Softmax reduction will be applied on each of the [4x4] planes of the input image.
Note
The value of axis must be always 1 for GLES

Definition at line 37 of file GCSoftmaxLayer.cpp.

38 {
39  ARM_COMPUTE_UNUSED(beta, axis);
40 
42  ARM_COMPUTE_ERROR_ON(beta != 1.0f);
43  ARM_COMPUTE_ERROR_ON_MSG(axis != 1, "Axis must be 1 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 }
Shape of a tensor.
Definition: TensorShape.h:39
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
void init(const TensorInfo &input, size_t alignment=0)
Initialize a tensor based on the passed TensorInfo.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
void manage(TensorType *obj)
Sets a object to be managed by the given memory group.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
virtual void allocate()=0
Interface to be implemented by the child class to allocate the tensor.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:789
Store the tensor's metadata.
Definition: TensorInfo.h:45
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'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.
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:328

References ITensorAllocator::allocate(), GCTensor::allocator(), ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_ERROR_ON_MSG, ARM_COMPUTE_UNUSED, arm_compute::test::validation::axis, GCLogits1DMaxKernel::configure(), GCLogits1DShiftExpSumKernel::configure(), GCLogits1DNormKernel::configure(), ITensorInfo::data_type(), arm_compute::F16, arm_compute::F32, ITensor::info(), ITensorAllocator::init(), MemoryGroupBase< TensorType >::manage(), ITensorInfo::num_channels(), arm_compute::test::validation::shape, and ITensorInfo::tensor_shape().

Referenced by arm_compute::test::validation::DATA_TEST_CASE().

◆ run()

void run ( )
overridevirtual

Run the kernels contained in the function.

For NEON kernels:

  • Multi-threading is used for the kernels which are parallelisable.
  • By default std::thread::hardware_concurrency() threads are used.
Note
CPPScheduler::set_num_threads() can be used to manually set the number of threads

For OpenCL kernels:

  • All the kernels are enqueued on the queue associated with CLScheduler.
  • The queue is then flushed.
Note
The function will not block until the kernels are executed. It is the user's responsibility to wait.
Will call prepare() on first run if hasn't been done

Implements IFunction.

Definition at line 70 of file GCSoftmaxLayer.cpp.

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 }
void dispatch(IGCKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: GCScheduler.cpp:69
void memory_barrier()
Defines a barrier ordering memory transactions.
Definition: GCScheduler.cpp:78
static GCScheduler & get()
Access the scheduler singleton.
Definition: GCScheduler.cpp:62
Memory group resources scope handling class.
Definition: IMemoryGroup.h:46

References GCScheduler::dispatch(), GCScheduler::get(), and GCScheduler::memory_barrier().


The documentation for this class was generated from the following files: