xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/operators/CpuSoftmax.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "src/cpu/operators/CpuSoftmax.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/Helpers.h"
27*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
28*c217d954SCole Faust #include "arm_compute/core/Validate.h"
29*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
30*c217d954SCole Faust #include "arm_compute/runtime/NEON/NEScheduler.h"
31*c217d954SCole Faust #include "src/common/utils/Log.h"
32*c217d954SCole Faust #include "src/core/helpers/MemoryHelpers.h"
33*c217d954SCole Faust #include "src/core/helpers/SoftmaxHelpers.h"
34*c217d954SCole Faust #include "src/cpu/kernels/CpuSoftmaxKernel.h"
35*c217d954SCole Faust #include "src/cpu/utils/CpuAuxTensorHandler.h"
36*c217d954SCole Faust 
37*c217d954SCole Faust using namespace arm_compute::experimental;
38*c217d954SCole Faust 
39*c217d954SCole Faust namespace arm_compute
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace cpu
42*c217d954SCole Faust {
43*c217d954SCole Faust template <bool IS_LOG>
CpuSoftmaxGeneric()44*c217d954SCole Faust CpuSoftmaxGeneric<IS_LOG>::CpuSoftmaxGeneric()
45*c217d954SCole Faust     : _permute_input(),
46*c217d954SCole Faust       _permute_output(),
47*c217d954SCole Faust       _max_kernel(),
48*c217d954SCole Faust       _softmax_kernel(),
49*c217d954SCole Faust       _max(),
50*c217d954SCole Faust       _tmp(),
51*c217d954SCole Faust       _input_permuted(),
52*c217d954SCole Faust       _output_permuted(),
53*c217d954SCole Faust       _needs_permute(false),
54*c217d954SCole Faust       _aux_mem(InternalTensorIdx::COUNT)
55*c217d954SCole Faust {
56*c217d954SCole Faust }
57*c217d954SCole Faust 
58*c217d954SCole Faust template <bool IS_LOG>
configure(const ITensorInfo * src,ITensorInfo * dst,float beta,int32_t axis)59*c217d954SCole Faust void CpuSoftmaxGeneric<IS_LOG>::configure(const ITensorInfo *src, ITensorInfo *dst, float beta, int32_t axis)
60*c217d954SCole Faust {
61*c217d954SCole Faust     // Perform validation step
62*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
63*c217d954SCole Faust     ARM_COMPUTE_ERROR_THROW_ON(CpuSoftmaxGeneric::validate(src, dst, beta, axis));
64*c217d954SCole Faust     ARM_COMPUTE_LOG_PARAMS(src, dst, beta, axis);
65*c217d954SCole Faust 
66*c217d954SCole Faust     const unsigned int actual_axis = static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(src->num_dimensions())));
67*c217d954SCole Faust 
68*c217d954SCole Faust     _needs_permute = actual_axis > 0;
69*c217d954SCole Faust 
70*c217d954SCole Faust     if(_needs_permute)
71*c217d954SCole Faust     {
72*c217d954SCole Faust         _permute_input.configure(src, &_input_permuted, softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis));
73*c217d954SCole Faust     }
74*c217d954SCole Faust 
75*c217d954SCole Faust     // We want to deal with a 2D input. Either it is the permuted version of the original input (4D case)
76*c217d954SCole Faust     // or it is the original input case (2D case)
77*c217d954SCole Faust     const ITensorInfo *tmp_input = (_needs_permute ? &_input_permuted : src);
78*c217d954SCole Faust 
79*c217d954SCole Faust     // Create intermediate tensors shapes
80*c217d954SCole Faust     TensorShape max_sum_shape = tmp_input->tensor_shape();
81*c217d954SCole Faust     max_sum_shape.set(0, 1);
82*c217d954SCole Faust     const TensorInfo input_info    = tmp_input->clone()->reset_padding().set_is_resizable(true);
83*c217d954SCole Faust     DataType         tmp_data_type = is_data_type_quantized_asymmetric(tmp_input->data_type()) ? DataType::F32 : tmp_input->data_type();
84*c217d954SCole Faust     TensorInfo       tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type));
85*c217d954SCole Faust     TensorInfo       max_info(tmp_input->clone()->set_tensor_shape(max_sum_shape));
86*c217d954SCole Faust 
87*c217d954SCole Faust     // Init intermediate tensors
88*c217d954SCole Faust     _max = TensorInfo(max_info);
89*c217d954SCole Faust     _tmp = TensorInfo(tensor_info_tmp);
90*c217d954SCole Faust 
91*c217d954SCole Faust     // Configure kernels
92*c217d954SCole Faust     auto mk = std::make_unique<kernels::CpuLogits1DMaxKernel>();
93*c217d954SCole Faust     mk->configure(tmp_input, &_max);
94*c217d954SCole Faust     _max_kernel = std::move(mk);
95*c217d954SCole Faust 
96*c217d954SCole Faust     auto sm = std::make_unique<kernels::CpuLogits1DSoftmaxKernel<IS_LOG>>();
97*c217d954SCole Faust     if(_needs_permute)
98*c217d954SCole Faust     {
99*c217d954SCole Faust         // The normalization kernel stores the result in a permuted output tensor
100*c217d954SCole Faust         sm->configure(tmp_input, &_max, &_output_permuted, beta, &_tmp);
101*c217d954SCole Faust 
102*c217d954SCole Faust         // Re-permute the permuted output into the requested (4D) output
103*c217d954SCole Faust         _permute_output.configure(&_output_permuted, dst, softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis));
104*c217d954SCole Faust     }
105*c217d954SCole Faust     else
106*c217d954SCole Faust     {
107*c217d954SCole Faust         // Softmax 2D case
108*c217d954SCole Faust         sm->configure(tmp_input, &_max, dst, beta, &_tmp);
109*c217d954SCole Faust     }
110*c217d954SCole Faust     _softmax_kernel = std::move(sm);
111*c217d954SCole Faust 
112*c217d954SCole Faust     _aux_mem[InternalTensorIdx::MAX] = MemoryInfo(offset_int_vec(InternalTensorIdx::MAX), MemoryLifetime::Temporary, _max.total_size());
113*c217d954SCole Faust     _aux_mem[InternalTensorIdx::TMP] = MemoryInfo(offset_int_vec(InternalTensorIdx::TMP), MemoryLifetime::Temporary, _tmp.total_size());
114*c217d954SCole Faust 
115*c217d954SCole Faust     _aux_mem[InternalTensorIdx::PERMUTED_SRC] = MemoryInfo(offset_int_vec(InternalTensorIdx::PERMUTED_SRC), MemoryLifetime::Temporary, _input_permuted.total_size());
116*c217d954SCole Faust     _aux_mem[InternalTensorIdx::PERMUTED_DST] = MemoryInfo(offset_int_vec(InternalTensorIdx::PERMUTED_DST), MemoryLifetime::Temporary, _output_permuted.total_size());
117*c217d954SCole Faust }
118*c217d954SCole Faust 
119*c217d954SCole Faust template <bool IS_LOG>
validate(const ITensorInfo * src,const ITensorInfo * dst,float beta,int32_t axis)120*c217d954SCole Faust Status CpuSoftmaxGeneric<IS_LOG>::validate(const ITensorInfo *src, const ITensorInfo *dst, float beta, int32_t axis)
121*c217d954SCole Faust {
122*c217d954SCole Faust     // Perform validation step
123*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
124*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->num_dimensions() > 4, "Only up to 4 dimensions are supported");
125*c217d954SCole Faust     ARM_COMPUTE_UNUSED(beta);
126*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(axis < static_cast<int32_t>(-src->num_dimensions()) || static_cast<int32_t>(src->num_dimensions()) <= axis);
127*c217d954SCole Faust 
128*c217d954SCole Faust     // Create intermediate tensor info
129*c217d954SCole Faust     DataType         tmp_data_type = src->data_type();
130*c217d954SCole Faust     const TensorInfo tensor_info_tmp(src->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
131*c217d954SCole Faust 
132*c217d954SCole Faust     TensorShape max_sum_shape = src->tensor_shape();
133*c217d954SCole Faust     max_sum_shape.set(0, 1);
134*c217d954SCole Faust     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*c217d954SCole Faust     const TensorInfo dont_care;
136*c217d954SCole Faust 
137*c217d954SCole Faust     const unsigned int actual_axis = static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(src->num_dimensions())));
138*c217d954SCole Faust 
139*c217d954SCole Faust     const bool needs_permute = actual_axis > 0;
140*c217d954SCole Faust 
141*c217d954SCole Faust     if(needs_permute)
142*c217d954SCole Faust     {
143*c217d954SCole Faust         const PermutationVector permutation_vector = softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis);
144*c217d954SCole Faust         const TensorShape       permuted_shape     = misc::shape_calculator::compute_permutation_output_shape(*src, permutation_vector);
145*c217d954SCole Faust         TensorInfo              input_permuted(src->clone()->set_tensor_shape(permuted_shape));
146*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(src, &input_permuted, permutation_vector));
147*c217d954SCole Faust         TensorInfo output_permuted(dst->clone()->set_tensor_shape(permuted_shape));
148*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(&output_permuted, dst, permutation_vector));
149*c217d954SCole Faust     }
150*c217d954SCole Faust 
151*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuLogits1DMaxKernel::validate(src, &tensor_info_max_sum));
152*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuLogits1DSoftmaxKernel<IS_LOG>::validate(&tensor_info_tmp, &tensor_info_max_sum, dst, beta, &dont_care));
153*c217d954SCole Faust 
154*c217d954SCole Faust     return Status{};
155*c217d954SCole Faust }
156*c217d954SCole Faust 
157*c217d954SCole Faust template <bool IS_LOG>
run(ITensorPack & tensors)158*c217d954SCole Faust void CpuSoftmaxGeneric<IS_LOG>::run(ITensorPack &tensors)
159*c217d954SCole Faust {
160*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
161*c217d954SCole Faust 
162*c217d954SCole Faust     auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
163*c217d954SCole Faust     auto dst = tensors.get_tensor(TensorType::ACL_DST);
164*c217d954SCole Faust 
165*c217d954SCole Faust     CpuAuxTensorHandler tmp(offset_int_vec(InternalTensorIdx::TMP), _tmp, tensors, true);
166*c217d954SCole Faust     CpuAuxTensorHandler max(offset_int_vec(InternalTensorIdx::MAX), _max, tensors, true);
167*c217d954SCole Faust 
168*c217d954SCole Faust     CpuAuxTensorHandler input_permuted(offset_int_vec(InternalTensorIdx::PERMUTED_SRC), _input_permuted, tensors, true);
169*c217d954SCole Faust     CpuAuxTensorHandler output_permuted(offset_int_vec(InternalTensorIdx::PERMUTED_DST), _output_permuted, tensors, true);
170*c217d954SCole Faust 
171*c217d954SCole Faust     ITensorPack max_pack;
172*c217d954SCole Faust     ITensorPack softmax_pack;
173*c217d954SCole Faust 
174*c217d954SCole Faust     if(_needs_permute)
175*c217d954SCole Faust     {
176*c217d954SCole Faust         ITensorPack permute_in_pack = { { TensorType::ACL_SRC, src }, { TensorType::ACL_DST, input_permuted.get() } };
177*c217d954SCole Faust         _permute_input.run(permute_in_pack);
178*c217d954SCole Faust 
179*c217d954SCole Faust         max_pack = { { TensorType::ACL_SRC, input_permuted.get() }, { TensorType::ACL_DST, max.get() } };
180*c217d954SCole Faust 
181*c217d954SCole Faust         softmax_pack =
182*c217d954SCole Faust         {
183*c217d954SCole Faust             { TensorType::ACL_SRC_0, input_permuted.get() },
184*c217d954SCole Faust             { TensorType::ACL_SRC_1, max.get() },
185*c217d954SCole Faust             { TensorType::ACL_DST_0, output_permuted.get() },
186*c217d954SCole Faust             { TensorType::ACL_DST_1, tmp.get() }
187*c217d954SCole Faust         };
188*c217d954SCole Faust     }
189*c217d954SCole Faust     else
190*c217d954SCole Faust     {
191*c217d954SCole Faust         max_pack = { { TensorType::ACL_SRC, src }, { TensorType::ACL_DST, max.get() } };
192*c217d954SCole Faust 
193*c217d954SCole Faust         softmax_pack =
194*c217d954SCole Faust         {
195*c217d954SCole Faust             { TensorType::ACL_SRC_0, src },
196*c217d954SCole Faust             { TensorType::ACL_SRC_1, max.get() },
197*c217d954SCole Faust             { TensorType::ACL_DST_0, dst },
198*c217d954SCole Faust             { TensorType::ACL_DST_1, tmp.get() }
199*c217d954SCole Faust         };
200*c217d954SCole Faust     }
201*c217d954SCole Faust 
202*c217d954SCole Faust     NEScheduler::get().schedule_op(_max_kernel.get(), Window::DimY, _max_kernel->window(), max_pack);
203*c217d954SCole Faust     NEScheduler::get().schedule_op(_softmax_kernel.get(), Window::DimY, _softmax_kernel->window(), softmax_pack);
204*c217d954SCole Faust 
205*c217d954SCole Faust     if(_needs_permute)
206*c217d954SCole Faust     {
207*c217d954SCole Faust         ITensorPack permute_out_pack;
208*c217d954SCole Faust         permute_out_pack.add_tensor(TensorType::ACL_SRC, output_permuted.get());
209*c217d954SCole Faust         permute_out_pack.add_tensor(TensorType::ACL_DST, dst);
210*c217d954SCole Faust         _permute_output.run(permute_out_pack);
211*c217d954SCole Faust     }
212*c217d954SCole Faust }
213*c217d954SCole Faust 
214*c217d954SCole Faust template <bool                   IS_LOG>
workspace() const215*c217d954SCole Faust experimental::MemoryRequirements CpuSoftmaxGeneric<IS_LOG>::workspace() const
216*c217d954SCole Faust {
217*c217d954SCole Faust     return _aux_mem;
218*c217d954SCole Faust }
219*c217d954SCole Faust 
220*c217d954SCole Faust template class CpuSoftmaxGeneric<false>;
221*c217d954SCole Faust template class CpuSoftmaxGeneric<true>;
222*c217d954SCole Faust } // namespace cpu
223*c217d954SCole Faust } // namespace arm_compute
224