blob: e55d7f903e046556e87b56ad32782df4ba067c20 [file] [log] [blame]
Michalis Spyrou373b4072021-01-20 16:41:12 +00001/*
2 * Copyright (c) 2021 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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/operators/CpuSoftmax.h"
Michalis Spyrou373b4072021-01-20 16:41:12 +000025
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/TensorInfo.h"
Michalis Spyrou373b4072021-01-20 16:41:12 +000028#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029#include "arm_compute/core/Validate.h"
Michalis Spyrou373b4072021-01-20 16:41:12 +000030#include "arm_compute/runtime/NEON/NEScheduler.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
ramelg013ae3d882021-09-12 23:07:47 +010032#include "src/common/utils/Log.h"
Manuel Bottini94f799e2021-06-09 16:37:32 +010033#include "src/core/helpers/MemoryHelpers.h"
Michalis Spyrou373b4072021-01-20 16:41:12 +000034#include "src/core/helpers/SoftmaxHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010035#include "src/cpu/kernels/CpuSoftmaxKernel.h"
36#include "src/cpu/utils/CpuAuxTensorHandler.h"
Manuel Bottini94f799e2021-06-09 16:37:32 +010037
38using namespace arm_compute::experimental;
Michalis Spyrou373b4072021-01-20 16:41:12 +000039
40namespace arm_compute
41{
42namespace cpu
43{
44template <bool IS_LOG>
45CpuSoftmaxGeneric<IS_LOG>::CpuSoftmaxGeneric()
Manuel Bottini94f799e2021-06-09 16:37:32 +010046 : _permute_input(),
47 _permute_output(),
48 _max_kernel(),
49 _softmax_kernel(),
50 _max(),
51 _tmp(),
52 _input_permuted(),
53 _output_permuted(),
54 _needs_permute(false),
55 _aux_mem(InternalTensorIdx::COUNT)
Michalis Spyrou373b4072021-01-20 16:41:12 +000056{
57}
58
59template <bool IS_LOG>
60void CpuSoftmaxGeneric<IS_LOG>::configure(const ITensorInfo *src, ITensorInfo *dst, float beta, int32_t axis)
61{
62 // Perform validation step
63 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
64 ARM_COMPUTE_ERROR_THROW_ON(CpuSoftmaxGeneric::validate(src, dst, beta, axis));
ramelg013ae3d882021-09-12 23:07:47 +010065 ARM_COMPUTE_LOG_PARAMS(src, dst, beta, axis);
Michalis Spyrou373b4072021-01-20 16:41:12 +000066
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 const unsigned int actual_axis =
68 static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(src->num_dimensions())));
Michalis Spyrou373b4072021-01-20 16:41:12 +000069
70 _needs_permute = actual_axis > 0;
71
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 if (_needs_permute)
Michalis Spyrou373b4072021-01-20 16:41:12 +000073 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 _permute_input.configure(src, &_input_permuted,
75 softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis));
Michalis Spyrou373b4072021-01-20 16:41:12 +000076 }
77
78 // We want to deal with a 2D input. Either it is the permuted version of the original input (4D case)
79 // or it is the original input case (2D case)
Manuel Bottini94f799e2021-06-09 16:37:32 +010080 const ITensorInfo *tmp_input = (_needs_permute ? &_input_permuted : src);
Michalis Spyrou373b4072021-01-20 16:41:12 +000081
82 // Create intermediate tensors shapes
83 TensorShape max_sum_shape = tmp_input->tensor_shape();
84 max_sum_shape.set(0, 1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 const TensorInfo input_info = tmp_input->clone()->reset_padding().set_is_resizable(true);
86 DataType tmp_data_type =
87 is_data_type_quantized_asymmetric(tmp_input->data_type()) ? DataType::F32 : tmp_input->data_type();
88 TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type));
89 TensorInfo max_info(tmp_input->clone()->set_tensor_shape(max_sum_shape));
Michalis Spyrou373b4072021-01-20 16:41:12 +000090
91 // Init intermediate tensors
Manuel Bottini94f799e2021-06-09 16:37:32 +010092 _max = TensorInfo(max_info);
93 _tmp = TensorInfo(tensor_info_tmp);
Michalis Spyrou373b4072021-01-20 16:41:12 +000094
95 // Configure kernels
96 auto mk = std::make_unique<kernels::CpuLogits1DMaxKernel>();
Manuel Bottini94f799e2021-06-09 16:37:32 +010097 mk->configure(tmp_input, &_max);
Michalis Spyrou373b4072021-01-20 16:41:12 +000098 _max_kernel = std::move(mk);
99
100 auto sm = std::make_unique<kernels::CpuLogits1DSoftmaxKernel<IS_LOG>>();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100101 if (_needs_permute)
Michalis Spyrou373b4072021-01-20 16:41:12 +0000102 {
Michalis Spyrou373b4072021-01-20 16:41:12 +0000103 // The normalization kernel stores the result in a permuted output tensor
Manuel Bottini94f799e2021-06-09 16:37:32 +0100104 sm->configure(tmp_input, &_max, &_output_permuted, beta, &_tmp);
Michalis Spyrou373b4072021-01-20 16:41:12 +0000105
106 // Re-permute the permuted output into the requested (4D) output
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107 _permute_output.configure(&_output_permuted, dst,
108 softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis));
Michalis Spyrou373b4072021-01-20 16:41:12 +0000109 }
110 else
111 {
112 // Softmax 2D case
Manuel Bottini94f799e2021-06-09 16:37:32 +0100113 sm->configure(tmp_input, &_max, dst, beta, &_tmp);
Michalis Spyrou373b4072021-01-20 16:41:12 +0000114 }
115 _softmax_kernel = std::move(sm);
Manuel Bottini94f799e2021-06-09 16:37:32 +0100116
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117 _aux_mem[InternalTensorIdx::MAX] =
118 MemoryInfo(offset_int_vec(InternalTensorIdx::MAX), MemoryLifetime::Temporary, _max.total_size());
119 _aux_mem[InternalTensorIdx::TMP] =
120 MemoryInfo(offset_int_vec(InternalTensorIdx::TMP), MemoryLifetime::Temporary, _tmp.total_size());
Manuel Bottini94f799e2021-06-09 16:37:32 +0100121
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 _aux_mem[InternalTensorIdx::PERMUTED_SRC] = MemoryInfo(offset_int_vec(InternalTensorIdx::PERMUTED_SRC),
123 MemoryLifetime::Temporary, _input_permuted.total_size());
124 _aux_mem[InternalTensorIdx::PERMUTED_DST] = MemoryInfo(offset_int_vec(InternalTensorIdx::PERMUTED_DST),
125 MemoryLifetime::Temporary, _output_permuted.total_size());
Michalis Spyrou373b4072021-01-20 16:41:12 +0000126}
127
128template <bool IS_LOG>
129Status CpuSoftmaxGeneric<IS_LOG>::validate(const ITensorInfo *src, const ITensorInfo *dst, float beta, int32_t axis)
130{
131 // Perform validation step
132 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
133 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->num_dimensions() > 4, "Only up to 4 dimensions are supported");
134 ARM_COMPUTE_UNUSED(beta);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100135 ARM_COMPUTE_RETURN_ERROR_ON(axis < static_cast<int32_t>(-src->num_dimensions()) ||
136 static_cast<int32_t>(src->num_dimensions()) <= axis);
Michalis Spyrou373b4072021-01-20 16:41:12 +0000137
138 // Create intermediate tensor info
139 DataType tmp_data_type = src->data_type();
140 const TensorInfo tensor_info_tmp(src->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
141
142 TensorShape max_sum_shape = src->tensor_shape();
143 max_sum_shape.set(0, 1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 const TensorInfo tensor_info_max_sum(src->clone()
145 ->set_tensor_shape(max_sum_shape)
146 .set_data_type(tmp_data_type)
147 .set_quantization_info(src->quantization_info())
148 .set_is_resizable(true));
Michalis Spyrou373b4072021-01-20 16:41:12 +0000149 const TensorInfo dont_care;
150
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100151 const unsigned int actual_axis =
152 static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(src->num_dimensions())));
Michalis Spyrou373b4072021-01-20 16:41:12 +0000153
154 const bool needs_permute = actual_axis > 0;
155
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156 if (needs_permute)
Michalis Spyrou373b4072021-01-20 16:41:12 +0000157 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100158 const PermutationVector permutation_vector =
159 softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis);
160 const TensorShape permuted_shape =
161 misc::shape_calculator::compute_permutation_output_shape(*src, permutation_vector);
162 TensorInfo input_permuted(src->clone()->set_tensor_shape(permuted_shape));
Michalis Spyrou373b4072021-01-20 16:41:12 +0000163 ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(src, &input_permuted, permutation_vector));
164 TensorInfo output_permuted(dst->clone()->set_tensor_shape(permuted_shape));
165 ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(&output_permuted, dst, permutation_vector));
166 }
167
168 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuLogits1DMaxKernel::validate(src, &tensor_info_max_sum));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100169 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuLogits1DSoftmaxKernel<IS_LOG>::validate(
170 &tensor_info_tmp, &tensor_info_max_sum, dst, beta, &dont_care));
Michalis Spyrou373b4072021-01-20 16:41:12 +0000171
172 return Status{};
173}
174
175template <bool IS_LOG>
176void CpuSoftmaxGeneric<IS_LOG>::run(ITensorPack &tensors)
177{
178 ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
179
Manuel Bottini94f799e2021-06-09 16:37:32 +0100180 auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
181 auto dst = tensors.get_tensor(TensorType::ACL_DST);
182
Georgios Pinitas1fa27ad2021-07-22 01:25:57 +0100183 CpuAuxTensorHandler tmp(offset_int_vec(InternalTensorIdx::TMP), _tmp, tensors, true);
184 CpuAuxTensorHandler max(offset_int_vec(InternalTensorIdx::MAX), _max, tensors, true);
Manuel Bottini94f799e2021-06-09 16:37:32 +0100185
Georgios Pinitas1fa27ad2021-07-22 01:25:57 +0100186 CpuAuxTensorHandler input_permuted(offset_int_vec(InternalTensorIdx::PERMUTED_SRC), _input_permuted, tensors, true);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100187 CpuAuxTensorHandler output_permuted(offset_int_vec(InternalTensorIdx::PERMUTED_DST), _output_permuted, tensors,
188 true);
Manuel Bottini94f799e2021-06-09 16:37:32 +0100189
Michalis Spyrou373b4072021-01-20 16:41:12 +0000190 ITensorPack max_pack;
191 ITensorPack softmax_pack;
192
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100193 if (_needs_permute)
Michalis Spyrou373b4072021-01-20 16:41:12 +0000194 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100195 ITensorPack permute_in_pack = {{TensorType::ACL_SRC, src}, {TensorType::ACL_DST, input_permuted.get()}};
Michalis Spyrou373b4072021-01-20 16:41:12 +0000196 _permute_input.run(permute_in_pack);
197
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100198 max_pack = {{TensorType::ACL_SRC, input_permuted.get()}, {TensorType::ACL_DST, max.get()}};
Michalis Spyrou373b4072021-01-20 16:41:12 +0000199
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100200 softmax_pack = {{TensorType::ACL_SRC_0, input_permuted.get()},
201 {TensorType::ACL_SRC_1, max.get()},
202 {TensorType::ACL_DST_0, output_permuted.get()},
203 {TensorType::ACL_DST_1, tmp.get()}};
Michalis Spyrou373b4072021-01-20 16:41:12 +0000204 }
205 else
206 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100207 max_pack = {{TensorType::ACL_SRC, src}, {TensorType::ACL_DST, max.get()}};
Michalis Spyrou373b4072021-01-20 16:41:12 +0000208
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100209 softmax_pack = {{TensorType::ACL_SRC_0, src},
210 {TensorType::ACL_SRC_1, max.get()},
211 {TensorType::ACL_DST_0, dst},
212 {TensorType::ACL_DST_1, tmp.get()}};
Manuel Bottini94f799e2021-06-09 16:37:32 +0100213 }
Michalis Spyrou373b4072021-01-20 16:41:12 +0000214
215 NEScheduler::get().schedule_op(_max_kernel.get(), Window::DimY, _max_kernel->window(), max_pack);
216 NEScheduler::get().schedule_op(_softmax_kernel.get(), Window::DimY, _softmax_kernel->window(), softmax_pack);
217
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100218 if (_needs_permute)
Michalis Spyrou373b4072021-01-20 16:41:12 +0000219 {
220 ITensorPack permute_out_pack;
Manuel Bottini94f799e2021-06-09 16:37:32 +0100221 permute_out_pack.add_tensor(TensorType::ACL_SRC, output_permuted.get());
222 permute_out_pack.add_tensor(TensorType::ACL_DST, dst);
Michalis Spyrou373b4072021-01-20 16:41:12 +0000223 _permute_output.run(permute_out_pack);
224 }
225}
226
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100227template <bool IS_LOG>
Michalis Spyrou373b4072021-01-20 16:41:12 +0000228experimental::MemoryRequirements CpuSoftmaxGeneric<IS_LOG>::workspace() const
229{
Manuel Bottini94f799e2021-06-09 16:37:32 +0100230 return _aux_mem;
Michalis Spyrou373b4072021-01-20 16:41:12 +0000231}
232
233template class CpuSoftmaxGeneric<false>;
234template class CpuSoftmaxGeneric<true>;
235} // namespace cpu
236} // namespace arm_compute