blob: bf4c2fa3a266683b10548cd7cfd9b25baf9e3586 [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"
28#include "arm_compute/core/Validate.h"
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30#include "arm_compute/runtime/NEON/NEScheduler.h"
ramelg013ae3d882021-09-12 23:07:47 +010031#include "src/common/utils/Log.h"
Manuel Bottini94f799e2021-06-09 16:37:32 +010032#include "src/core/helpers/MemoryHelpers.h"
Michalis Spyrou373b4072021-01-20 16:41:12 +000033#include "src/core/helpers/SoftmaxHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010034#include "src/cpu/kernels/CpuSoftmaxKernel.h"
35#include "src/cpu/utils/CpuAuxTensorHandler.h"
Manuel Bottini94f799e2021-06-09 16:37:32 +010036
37using namespace arm_compute::experimental;
Michalis Spyrou373b4072021-01-20 16:41:12 +000038
39namespace arm_compute
40{
41namespace cpu
42{
43template <bool IS_LOG>
44CpuSoftmaxGeneric<IS_LOG>::CpuSoftmaxGeneric()
Manuel Bottini94f799e2021-06-09 16:37:32 +010045 : _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)
Michalis Spyrou373b4072021-01-20 16:41:12 +000055{
56}
57
58template <bool IS_LOG>
59void CpuSoftmaxGeneric<IS_LOG>::configure(const ITensorInfo *src, ITensorInfo *dst, float beta, int32_t axis)
60{
61 // Perform validation step
62 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
63 ARM_COMPUTE_ERROR_THROW_ON(CpuSoftmaxGeneric::validate(src, dst, beta, axis));
ramelg013ae3d882021-09-12 23:07:47 +010064 ARM_COMPUTE_LOG_PARAMS(src, dst, beta, axis);
Michalis Spyrou373b4072021-01-20 16:41:12 +000065
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 {
Manuel Bottini94f799e2021-06-09 16:37:32 +010072 _permute_input.configure(src, &_input_permuted, softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis));
Michalis Spyrou373b4072021-01-20 16:41:12 +000073 }
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)
Manuel Bottini94f799e2021-06-09 16:37:32 +010077 const ITensorInfo *tmp_input = (_needs_permute ? &_input_permuted : src);
Michalis Spyrou373b4072021-01-20 16:41:12 +000078
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
Manuel Bottini94f799e2021-06-09 16:37:32 +010088 _max = TensorInfo(max_info);
89 _tmp = TensorInfo(tensor_info_tmp);
Michalis Spyrou373b4072021-01-20 16:41:12 +000090
91 // Configure kernels
92 auto mk = std::make_unique<kernels::CpuLogits1DMaxKernel>();
Manuel Bottini94f799e2021-06-09 16:37:32 +010093 mk->configure(tmp_input, &_max);
Michalis Spyrou373b4072021-01-20 16:41:12 +000094 _max_kernel = std::move(mk);
95
96 auto sm = std::make_unique<kernels::CpuLogits1DSoftmaxKernel<IS_LOG>>();
97 if(_needs_permute)
98 {
Michalis Spyrou373b4072021-01-20 16:41:12 +000099 // The normalization kernel stores the result in a permuted output tensor
Manuel Bottini94f799e2021-06-09 16:37:32 +0100100 sm->configure(tmp_input, &_max, &_output_permuted, beta, &_tmp);
Michalis Spyrou373b4072021-01-20 16:41:12 +0000101
102 // Re-permute the permuted output into the requested (4D) output
Manuel Bottini94f799e2021-06-09 16:37:32 +0100103 _permute_output.configure(&_output_permuted, dst, softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis));
Michalis Spyrou373b4072021-01-20 16:41:12 +0000104 }
105 else
106 {
107 // Softmax 2D case
Manuel Bottini94f799e2021-06-09 16:37:32 +0100108 sm->configure(tmp_input, &_max, dst, beta, &_tmp);
Michalis Spyrou373b4072021-01-20 16:41:12 +0000109 }
110 _softmax_kernel = std::move(sm);
Manuel Bottini94f799e2021-06-09 16:37:32 +0100111
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());
Michalis Spyrou373b4072021-01-20 16:41:12 +0000117}
118
119template <bool IS_LOG>
120Status CpuSoftmaxGeneric<IS_LOG>::validate(const ITensorInfo *src, const ITensorInfo *dst, float beta, int32_t axis)
121{
122 // Perform validation step
123 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
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 {
143 const PermutationVector permutation_vector = softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis);
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
151 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuLogits1DMaxKernel::validate(src, &tensor_info_max_sum));
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}
156
157template <bool IS_LOG>
158void CpuSoftmaxGeneric<IS_LOG>::run(ITensorPack &tensors)
159{
160 ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
161
Manuel Bottini94f799e2021-06-09 16:37:32 +0100162 auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
163 auto dst = tensors.get_tensor(TensorType::ACL_DST);
164
Georgios Pinitas1fa27ad2021-07-22 01:25:57 +0100165 CpuAuxTensorHandler tmp(offset_int_vec(InternalTensorIdx::TMP), _tmp, tensors, true);
166 CpuAuxTensorHandler max(offset_int_vec(InternalTensorIdx::MAX), _max, tensors, true);
Manuel Bottini94f799e2021-06-09 16:37:32 +0100167
Georgios Pinitas1fa27ad2021-07-22 01:25:57 +0100168 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);
Manuel Bottini94f799e2021-06-09 16:37:32 +0100170
Michalis Spyrou373b4072021-01-20 16:41:12 +0000171 ITensorPack max_pack;
172 ITensorPack softmax_pack;
173
174 if(_needs_permute)
175 {
Manuel Bottini94f799e2021-06-09 16:37:32 +0100176 ITensorPack permute_in_pack = { { TensorType::ACL_SRC, src }, { TensorType::ACL_DST, input_permuted.get() } };
Michalis Spyrou373b4072021-01-20 16:41:12 +0000177 _permute_input.run(permute_in_pack);
178
Manuel Bottini94f799e2021-06-09 16:37:32 +0100179 max_pack = { { TensorType::ACL_SRC, input_permuted.get() }, { TensorType::ACL_DST, max.get() } };
Michalis Spyrou373b4072021-01-20 16:41:12 +0000180
Manuel Bottini94f799e2021-06-09 16:37:32 +0100181 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 };
Michalis Spyrou373b4072021-01-20 16:41:12 +0000188 }
189 else
190 {
Manuel Bottini94f799e2021-06-09 16:37:32 +0100191 max_pack = { { TensorType::ACL_SRC, src }, { TensorType::ACL_DST, max.get() } };
Michalis Spyrou373b4072021-01-20 16:41:12 +0000192
Manuel Bottini94f799e2021-06-09 16:37:32 +0100193 softmax_pack =
194 {
195 { TensorType::ACL_SRC_0, src },
196 { TensorType::ACL_SRC_1, max.get() },
197 { TensorType::ACL_DST_0, dst },
198 { TensorType::ACL_DST_1, tmp.get() }
199 };
200 }
Michalis Spyrou373b4072021-01-20 16:41:12 +0000201
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;
Manuel Bottini94f799e2021-06-09 16:37:32 +0100208 permute_out_pack.add_tensor(TensorType::ACL_SRC, output_permuted.get());
209 permute_out_pack.add_tensor(TensorType::ACL_DST, dst);
Michalis Spyrou373b4072021-01-20 16:41:12 +0000210 _permute_output.run(permute_out_pack);
211 }
212}
213
214template <bool IS_LOG>
215experimental::MemoryRequirements CpuSoftmaxGeneric<IS_LOG>::workspace() const
216{
Manuel Bottini94f799e2021-06-09 16:37:32 +0100217 return _aux_mem;
Michalis Spyrou373b4072021-01-20 16:41:12 +0000218}
219
220template class CpuSoftmaxGeneric<false>;
221template class CpuSoftmaxGeneric<true>;
222} // namespace cpu
223} // namespace arm_compute