Compute Library
 21.11
CLSoftmaxLayerGeneric< IS_LOG > Class Template Reference

Basic function to compute a SoftmaxLayer. More...

#include <CLSoftmaxLayer.h>

Collaboration diagram for CLSoftmaxLayerGeneric< IS_LOG >:
[legend]

Public Member Functions

 CLSoftmaxLayerGeneric (std::shared_ptr< IMemoryManager > memory_manager=nullptr)
 Constructor. More...
 
 ~CLSoftmaxLayerGeneric ()
 Default destructor. More...
 
void configure (const ICLTensor *input, ICLTensor *output, float beta=1.0f, int32_t axis=0)
 Set the input and output tensors. More...
 
void configure (const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, float beta=1.0f, int32_t axis=0)
 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...
 

Static Public Member Functions

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. More...
 

Detailed Description

template<bool IS_LOG = false>
class arm_compute::CLSoftmaxLayerGeneric< IS_LOG >

Basic function to compute a SoftmaxLayer.

Softmax is calculated by :

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

Log Softmax is calculated by :

\[ out = (x - max(x) * beta) - log(\sum{e^{x - max(x) * beta}}) \]

This function runs the following operators/kernels:

  1. If axis is not 0:
  2. opencl::ClPermute
  3. opencl::kernels::ClLogits1DNormKernel
  4. opencl::kernels::ClLogits1DMaxShiftExpSumKernel

Definition at line 54 of file CLSoftmaxLayer.h.

Constructor & Destructor Documentation

◆ CLSoftmaxLayerGeneric()

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

Constructor.

Definition at line 52 of file CLSoftmaxLayer.cpp.

References CLSoftmaxLayerGeneric< IS_LOG >::~CLSoftmaxLayerGeneric().

53  : _impl(std::make_unique<Impl>())
54 {
55  _impl->memory_group = MemoryGroup(std::move(memory_manager));
56 }

◆ ~CLSoftmaxLayerGeneric()

Member Function Documentation

◆ configure() [1/2]

void configure ( const ICLTensor input,
ICLTensor output,
float  beta = 1.0f,
int32_t  axis = 0 
)

Set the input and output tensors.

Valid data layouts:

  • All

Valid data type configurations:

src dst
QASYMM8 QASYMM8
QASYMM8_SIGNED QASYMM8_SIGNED
F16 F16
F32 F32
Parameters
[in]inputSource tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32 for Softmax and F16/F32 for Log Softmax
[out]outputDestination tensor. Data types supported: same as input
[in]beta(Optional) A scaling factor for the exponent. Defaults to 1.f
[in]axis(Optional) The dimension in which to apply the function. E.g. for input of shape 4x5x6 and axis=1, softmax will be applied to 4x6=24 vectors of size 5. Defaults to 0

Definition at line 62 of file CLSoftmaxLayer.cpp.

References CLKernelLibrary::get().

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

63 {
64  configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis);
65 }
void configure(const ICLTensor *input, ICLTensor *output, float beta=1.0f, int32_t axis=0)
Set the input and output tensors.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.

◆ configure() [2/2]

void configure ( const CLCompileContext compile_context,
const ICLTensor input,
ICLTensor output,
float  beta = 1.0f,
int32_t  axis = 0 
)

Set the input and output tensors.

Parameters
[in]compile_contextThe compile context to be used.
[in]inputSource tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32 for Softmax and F16/F32 for Log Softmax
[out]outputDestination tensor. Data types supported: same as input
[in]beta(Optional) A scaling factor for the exponent. Defaults to 1.f
[in]axis(Optional) The dimension in which to apply the function. E.g. for input of shape 4x5x6 and axis=1, softmax will be applied to 4x6=24 vectors of size 5. Defaults to 0

Definition at line 68 of file CLSoftmaxLayer.cpp.

References arm_compute::ACL_DST, arm_compute::ACL_SRC, ITensorInfo::data_type(), ITensor::info(), and arm_compute::test::validation::input.

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 }

◆ run()

void run ( )
overridevirtual

Run the kernels contained in the function.

For CPU 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 89 of file CLSoftmaxLayer.cpp.

References ARM_COMPUTE_ERROR_ON_NULLPTR.

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

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 }
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo output,
float  beta = 1.0f,
int32_t  axis = 0 
)
static

Static function to check if given info will lead to a valid configuration of CLSoftmaxLayer.

Parameters
[in]inputSource tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32 for Softmax and F16/F32 for Log Softmax
[in]outputDestination tensor. Data types supported: same as input
[in]beta(Optional) A scaling factor for the exponent. Defaults to 1.f
[in]axis(Optional) The dimension in which to apply the function. E.g. for input of shape 4x5x6 and axis=1, softmax will be applied to 4x6=24 vectors of size 5. Defaults to 0
Returns
a status

Definition at line 82 of file CLSoftmaxLayer.cpp.

References ITensorInfo::data_type(), and ClGemm::validate().

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

83 {
84  SoftmaxKernelInfo softmax_info{ beta, IS_LOG, input->data_type(), axis };
85  return OperatorType::validate(*input, *output, softmax_info);
86 }
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

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