Compute Library
 23.08
CpuSoftmaxGeneric< IS_LOG > Class Template Reference

Basic function to compute a SoftmaxLayer and a Log SoftmaxLayer. More...

#include <CpuSoftmax.h>

Collaboration diagram for CpuSoftmaxGeneric< IS_LOG >:
[legend]

Public Member Functions

 CpuSoftmaxGeneric ()
 
void configure (const ITensorInfo *src, ITensorInfo *dst, float beta=1.0f, int32_t axis=0)
 Set the input and output tensors. More...
 
void run (ITensorPack &tensors) override
 Run the kernels contained in the function. More...
 
experimental::MemoryRequirements workspace () const override
 Return the memory requirements required by the workspace. More...
 
- Public Member Functions inherited from INEOperator
 INEOperator (IRuntimeContext *ctx=nullptr)
 Constructor. More...
 
 INEOperator (const INEOperator &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 INEOperator (INEOperator &&)=default
 Default move constructor. More...
 
INEOperatoroperator= (const INEOperator &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
INEOperatoroperator= (INEOperator &&)=default
 Default move assignment operator. More...
 
 ~INEOperator ()
 Default destructor. More...
 
void prepare (ITensorPack &constants) override
 Prepare the function for executing. More...
 
- Public Member Functions inherited from IOperator
virtual ~IOperator ()=default
 Destructor. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *src, const ITensorInfo *dst, float beta=1.0f, int32_t axis=0)
 Static function to check if given info will lead to a valid configuration. More...
 

Detailed Description

template<bool IS_LOG = false>
class arm_compute::cpu::CpuSoftmaxGeneric< IS_LOG >

Basic function to compute a SoftmaxLayer and a Log 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 function/kernels:

  1. If axis is not 0:
  2. CpuPermute
  3. kernels::CpuLogits1DMaxKernel
  4. kernels::CpuLogits1DSoftmaxKernel

Definition at line 57 of file CpuSoftmax.h.

Constructor & Destructor Documentation

◆ CpuSoftmaxGeneric()

Definition at line 44 of file CpuSoftmax.cpp.

45  : _permute_input(),
46  _permute_output(),
47  _max_kernel(),
48  _softmax_kernel(),
49  _max(),
50  _tmp(),
51  _input_permuted(),
52  _output_permuted(),
53  _needs_permute(false),
54  _aux_mem(InternalTensorIdx::COUNT)
55 {
56 }

Member Function Documentation

◆ configure()

void configure ( const ITensorInfo src,
ITensorInfo dst,
float  beta = 1.0f,
int32_t  axis = 0 
)

Set the input and output tensors.

Parameters
[in,out]srcSource tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. last value of each row to the nearest multiple.
[out]dstDestination tensor ifo. Data types supported: same as input.
[in]beta(Optional) A scaling factor for the exponent.
[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 59 of file CpuSoftmax.cpp.

60 {
61  // Perform validation step
64  ARM_COMPUTE_LOG_PARAMS(src, dst, beta, axis);
65 
66  const unsigned int actual_axis = static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(src->num_dimensions())));
67 
68  _needs_permute = actual_axis > 0;
69 
70  if(_needs_permute)
71  {
72  _permute_input.configure(src, &_input_permuted, softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis));
73  }
74 
75  // We want to deal with a 2D input. Either it is the permuted version of the original input (4D case)
76  // or it is the original input case (2D case)
77  const ITensorInfo *tmp_input = (_needs_permute ? &_input_permuted : src);
78 
79  // Create intermediate tensors shapes
80  TensorShape max_sum_shape = tmp_input->tensor_shape();
81  max_sum_shape.set(0, 1);
82  const TensorInfo input_info = tmp_input->clone()->reset_padding().set_is_resizable(true);
83  DataType tmp_data_type = is_data_type_quantized_asymmetric(tmp_input->data_type()) ? DataType::F32 : tmp_input->data_type();
84  TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type));
85  TensorInfo max_info(tmp_input->clone()->set_tensor_shape(max_sum_shape));
86 
87  // Init intermediate tensors
88  _max = TensorInfo(max_info);
89  _tmp = TensorInfo(tensor_info_tmp);
90 
91  // Configure kernels
92  auto mk = std::make_unique<kernels::CpuLogits1DMaxKernel>();
93  mk->configure(tmp_input, &_max);
94  _max_kernel = std::move(mk);
95 
96  auto sm = std::make_unique<kernels::CpuLogits1DSoftmaxKernel<IS_LOG>>();
97  if(_needs_permute)
98  {
99  // The normalization kernel stores the result in a permuted output tensor
100  sm->configure(tmp_input, &_max, &_output_permuted, beta, &_tmp);
101 
102  // Re-permute the permuted output into the requested (4D) output
103  _permute_output.configure(&_output_permuted, dst, softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis));
104  }
105  else
106  {
107  // Softmax 2D case
108  sm->configure(tmp_input, &_max, dst, beta, &_tmp);
109  }
110  _softmax_kernel = std::move(sm);
111 
112  _aux_mem[InternalTensorIdx::MAX] = MemoryInfo(offset_int_vec(InternalTensorIdx::MAX), MemoryLifetime::Temporary, _max.total_size());
113  _aux_mem[InternalTensorIdx::TMP] = MemoryInfo(offset_int_vec(InternalTensorIdx::TMP), MemoryLifetime::Temporary, _tmp.total_size());
114 
115  _aux_mem[InternalTensorIdx::PERMUTED_SRC] = MemoryInfo(offset_int_vec(InternalTensorIdx::PERMUTED_SRC), MemoryLifetime::Temporary, _input_permuted.total_size());
116  _aux_mem[InternalTensorIdx::PERMUTED_DST] = MemoryInfo(offset_int_vec(InternalTensorIdx::PERMUTED_DST), MemoryLifetime::Temporary, _output_permuted.total_size());
117 }

References ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, ARM_COMPUTE_LOG_PARAMS, ICloneable< T >::clone(), ITensorInfo::data_type(), arm_compute::test::validation::dst, arm_compute::F32, arm_compute::softmax_helpers::get_permutation_vector_from_softmax_axis(), arm_compute::test::validation::input_info, arm_compute::is_data_type_quantized_asymmetric(), TensorShape::set(), arm_compute::test::validation::src, ITensorInfo::tensor_shape(), CpuSoftmaxGeneric< IS_LOG >::validate(), and arm_compute::wrap_around().

◆ run()

void run ( ITensorPack tensors)
overridevirtual

Run the kernels contained in the function.

Parameters
[in]tensorsVector that contains the tensors to operate on.

Reimplemented from INEOperator.

Definition at line 158 of file CpuSoftmax.cpp.

159 {
160  ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
161 
162  auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
163  auto dst = tensors.get_tensor(TensorType::ACL_DST);
164 
165  CpuAuxTensorHandler tmp(offset_int_vec(InternalTensorIdx::TMP), _tmp, tensors, true);
166  CpuAuxTensorHandler max(offset_int_vec(InternalTensorIdx::MAX), _max, tensors, true);
167 
168  CpuAuxTensorHandler input_permuted(offset_int_vec(InternalTensorIdx::PERMUTED_SRC), _input_permuted, tensors, true);
169  CpuAuxTensorHandler output_permuted(offset_int_vec(InternalTensorIdx::PERMUTED_DST), _output_permuted, tensors, true);
170 
171  ITensorPack max_pack;
172  ITensorPack softmax_pack;
173 
174  if(_needs_permute)
175  {
176  ITensorPack permute_in_pack = { { TensorType::ACL_SRC, src }, { TensorType::ACL_DST, input_permuted.get() } };
177  _permute_input.run(permute_in_pack);
178 
179  max_pack = { { TensorType::ACL_SRC, input_permuted.get() }, { TensorType::ACL_DST, max.get() } };
180 
181  softmax_pack =
182  {
183  { TensorType::ACL_SRC_0, input_permuted.get() },
184  { TensorType::ACL_SRC_1, max.get() },
185  { TensorType::ACL_DST_0, output_permuted.get() },
186  { TensorType::ACL_DST_1, tmp.get() }
187  };
188  }
189  else
190  {
191  max_pack = { { TensorType::ACL_SRC, src }, { TensorType::ACL_DST, max.get() } };
192 
193  softmax_pack =
194  {
196  { TensorType::ACL_SRC_1, max.get() },
198  { TensorType::ACL_DST_1, tmp.get() }
199  };
200  }
201 
202  NEScheduler::get().schedule_op(_max_kernel.get(), Window::DimY, _max_kernel->window(), max_pack);
203  NEScheduler::get().schedule_op(_softmax_kernel.get(), Window::DimY, _softmax_kernel->window(), softmax_pack);
204 
205  if(_needs_permute)
206  {
207  ITensorPack permute_out_pack;
208  permute_out_pack.add_tensor(TensorType::ACL_SRC, output_permuted.get());
209  permute_out_pack.add_tensor(TensorType::ACL_DST, dst);
210  _permute_output.run(permute_out_pack);
211  }
212 }

References arm_compute::ACL_DST, arm_compute::ACL_DST_0, arm_compute::ACL_DST_1, arm_compute::ACL_SRC, arm_compute::ACL_SRC_0, arm_compute::ACL_SRC_1, ITensorPack::add_tensor(), ARM_COMPUTE_ERROR_ON_MSG, Window::DimY, arm_compute::test::validation::dst, ITensorPack::empty(), Scheduler::get(), CpuAuxTensorHandler::get(), ITensorPack::get_const_tensor(), ITensorPack::get_tensor(), MAX, arm_compute::offset_int_vec(), IScheduler::schedule_op(), and arm_compute::test::validation::src.

◆ validate()

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

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

Similar to CpuSoftmaxGeneric::configure()

Returns
a status

Definition at line 120 of file CpuSoftmax.cpp.

121 {
122  // Perform validation step
124  ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->num_dimensions() > 4, "Only up to 4 dimensions are supported");
125  ARM_COMPUTE_UNUSED(beta);
126  ARM_COMPUTE_RETURN_ERROR_ON(axis < static_cast<int32_t>(-src->num_dimensions()) || static_cast<int32_t>(src->num_dimensions()) <= axis);
127 
128  // Create intermediate tensor info
129  DataType tmp_data_type = src->data_type();
130  const TensorInfo tensor_info_tmp(src->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
131 
132  TensorShape max_sum_shape = src->tensor_shape();
133  max_sum_shape.set(0, 1);
134  const TensorInfo tensor_info_max_sum(src->clone()->set_tensor_shape(max_sum_shape).set_data_type(tmp_data_type).set_quantization_info(src->quantization_info()).set_is_resizable(true));
135  const TensorInfo dont_care;
136 
137  const unsigned int actual_axis = static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(src->num_dimensions())));
138 
139  const bool needs_permute = actual_axis > 0;
140 
141  if(needs_permute)
142  {
144  const TensorShape permuted_shape = misc::shape_calculator::compute_permutation_output_shape(*src, permutation_vector);
145  TensorInfo input_permuted(src->clone()->set_tensor_shape(permuted_shape));
146  ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(src, &input_permuted, permutation_vector));
147  TensorInfo output_permuted(dst->clone()->set_tensor_shape(permuted_shape));
148  ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(&output_permuted, dst, permutation_vector));
149  }
150 
152  ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuLogits1DSoftmaxKernel<IS_LOG>::validate(&tensor_info_tmp, &tensor_info_max_sum, dst, beta, &dont_care));
153 
154  return Status{};
155 }

References ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_MSG, ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR, ARM_COMPUTE_RETURN_ON_ERROR, ARM_COMPUTE_UNUSED, arm_compute::misc::shape_calculator::compute_permutation_output_shape(), arm_compute::test::validation::dst, arm_compute::softmax_helpers::get_permutation_vector_from_softmax_axis(), TensorShape::set(), arm_compute::test::validation::src, CpuPermute::validate(), CpuLogits1DMaxKernel::validate(), and arm_compute::wrap_around().

Referenced by CpuSoftmaxGeneric< IS_LOG >::configure().

◆ workspace()

experimental::MemoryRequirements workspace ( ) const
overridevirtual

Return the memory requirements required by the workspace.

Reimplemented from INEOperator.

Definition at line 215 of file CpuSoftmax.cpp.

216 {
217  return _aux_mem;
218 }

The documentation for this class was generated from the following files:
arm_compute::ACL_DST_1
@ ACL_DST_1
Definition: Types.h:57
arm_compute::test::validation::input_info
input_info
Definition: DirectConvolutionLayer.cpp:547
arm_compute::test::validation::src
SimpleTensor< float > src
Definition: DFT.cpp:155
arm_compute::misc::shape_calculator::compute_permutation_output_shape
TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm)
Calculate the permuted shape of an input given a permutation vector.
Definition: ShapeCalculator.h:110
arm_compute::IScheduler::schedule_op
virtual void schedule_op(ICPPKernel *kernel, const Hints &hints, const Window &window, ITensorPack &tensors)=0
Runs the kernel in the same thread as the caller synchronously.
arm_compute::cpu::CpuPermute::validate
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const PermutationVector &perm)
Static function to check if given info will lead to a valid configuration.
Definition: CpuPermute.cpp:42
arm_compute::test::validation::dst
auto dst
Definition: DFT.cpp:170
arm_compute::cpu::kernels::CpuLogits1DMaxKernel::validate
static Status validate(const ITensorInfo *src, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration.
Definition: CpuSoftmaxKernel.cpp:134
arm_compute::ACL_SRC_0
@ ACL_SRC_0
Definition: Types.h:45
arm_compute::wrap_around
T wrap_around(T x, T m)
Wrap-around a number within the range 0 <= x < m.
Definition: Helpers.h:268
arm_compute::cpu::kernels::CpuLogits1DSoftmaxKernel::validate
static Status validate(const ITensorInfo *src, const ITensorInfo *max, const ITensorInfo *dst, const float beta, const ITensorInfo *tmp)
Static function to check if given info will lead to a valid configuration.
Definition: CpuSoftmaxKernel.cpp:285
arm_compute::ACL_SRC_1
@ ACL_SRC_1
Definition: Types.h:46
ARM_COMPUTE_RETURN_ON_ERROR
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
arm_compute::experimental::MemoryInfo
Definition: Types.h:96
arm_compute::PermutationVector
Strides PermutationVector
Permutation vector.
Definition: CoreTypes.h:37
ARM_COMPUTE_ERROR_THROW_ON
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:456
arm_compute::ACL_DST_0
@ ACL_DST_0
Definition: Types.h:56
ARM_COMPUTE_ERROR_ON_MSG
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:457
ARM_COMPUTE_RETURN_ERROR_ON
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:297
arm_compute::TensorInfo::total_size
size_t total_size() const override
Returns the total size of the tensor in bytes.
Definition: TensorInfo.h:251
arm_compute::ACL_DST
@ ACL_DST
Definition: Types.h:55
arm_compute::Scheduler::get
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94
arm_compute::softmax_helpers::get_permutation_vector_from_softmax_axis
PermutationVector get_permutation_vector_from_softmax_axis(size_t axis)
Given a softmax axis, this function returns the permutation vector required to put the axis to the fr...
Definition: SoftmaxHelpers.cpp:30
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
arm_compute::Window::DimY
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
MAX
#define MAX(x, y)
Definition: elementwise_operation_quantized.cl:28
arm_compute::experimental::INEOperator::run
void run(ITensorPack &tensors) override
Run the kernels contained in the function.
Definition: INEOperator.cpp:40
arm_compute::offset_int_vec
int offset_int_vec(int offset)
Definition: MemoryHelpers.h:38
ARM_COMPUTE_RETURN_ERROR_ON_MSG
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:245
arm_compute::cpu::CpuPermute::configure
void configure(const ITensorInfo *src, ITensorInfo *dst, const PermutationVector &perm)
Configure operator for a given list of arguments.
Definition: CpuPermute.cpp:34
arm_compute::is_data_type_quantized_asymmetric
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: DataTypeUtils.h:346
ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
arm_compute::ACL_SRC
@ ACL_SRC
Definition: Types.h:44
arm_compute::DataType::F32
@ F32
32-bit floating-point number
arm_compute::cpu::CpuSoftmaxGeneric::validate
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, float beta=1.0f, int32_t axis=0)
Static function to check if given info will lead to a valid configuration.
Definition: CpuSoftmax.cpp:120
ARM_COMPUTE_LOG_PARAMS
#define ARM_COMPUTE_LOG_PARAMS(...)
Definition: Log.h:35
arm_compute::DataType
DataType
Available data types.
Definition: CoreTypes.h:82