blob: 0e1bcd5c6967be3237b4221b7b44bb4c2e2093ec [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 */
24#include "src/runtime/cpu/operators/CpuSoftmax.h"
25
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"
31#include "src/core/cpu/kernels/CpuSoftmaxKernel.h"
32#include "src/core/helpers/SoftmaxHelpers.h"
33
34namespace arm_compute
35{
36namespace cpu
37{
38template <bool IS_LOG>
39CpuSoftmaxGeneric<IS_LOG>::CpuSoftmaxGeneric()
40 : _permute_input(), _permute_output(), _max_kernel(), _softmax_kernel(), _max(nullptr), _tmp(nullptr), _input_permuted(nullptr), _output_permuted(nullptr), _needs_permute(false)
41{
42}
43
44template <bool IS_LOG>
45void CpuSoftmaxGeneric<IS_LOG>::configure(const ITensorInfo *src, ITensorInfo *dst, float beta, int32_t axis)
46{
47 // Perform validation step
48 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
49 ARM_COMPUTE_ERROR_THROW_ON(CpuSoftmaxGeneric::validate(src, dst, beta, axis));
50
51 const unsigned int actual_axis = static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(src->num_dimensions())));
52
53 _needs_permute = actual_axis > 0;
54
55 if(_needs_permute)
56 {
57 _input_permuted = std::make_unique<TensorInfo>();
58 _permute_input.configure(src, _input_permuted.get(), softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis));
59 }
60
61 // We want to deal with a 2D input. Either it is the permuted version of the original input (4D case)
62 // or it is the original input case (2D case)
63 const ITensorInfo *tmp_input = (_needs_permute ? _input_permuted.get() : src);
64
65 // Create intermediate tensors shapes
66 TensorShape max_sum_shape = tmp_input->tensor_shape();
67 max_sum_shape.set(0, 1);
68 const TensorInfo input_info = tmp_input->clone()->reset_padding().set_is_resizable(true);
69 DataType tmp_data_type = is_data_type_quantized_asymmetric(tmp_input->data_type()) ? DataType::F32 : tmp_input->data_type();
70 TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type));
71 TensorInfo max_info(tmp_input->clone()->set_tensor_shape(max_sum_shape));
72
73 // Init intermediate tensors
74 _max = std::make_unique<TensorInfo>(max_info);
75 _tmp = std::make_unique<TensorInfo>(tensor_info_tmp);
76
77 // Configure kernels
78 auto mk = std::make_unique<kernels::CpuLogits1DMaxKernel>();
79 mk->configure(tmp_input, _max.get());
80 _max_kernel = std::move(mk);
81
82 auto sm = std::make_unique<kernels::CpuLogits1DSoftmaxKernel<IS_LOG>>();
83 if(_needs_permute)
84 {
85 _output_permuted = std::make_unique<TensorInfo>();
86
87 // The normalization kernel stores the result in a permuted output tensor
88 sm->configure(tmp_input, _max.get(), _output_permuted.get(), beta, _tmp.get());
89
90 // Re-permute the permuted output into the requested (4D) output
91 _permute_output.configure(_output_permuted.get(), dst, softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis));
92 }
93 else
94 {
95 // Softmax 2D case
96 sm->configure(tmp_input, _max.get(), dst, beta, _tmp.get());
97 }
98 _softmax_kernel = std::move(sm);
99}
100
101template <bool IS_LOG>
102Status CpuSoftmaxGeneric<IS_LOG>::validate(const ITensorInfo *src, const ITensorInfo *dst, float beta, int32_t axis)
103{
104 // Perform validation step
105 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
106 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->num_dimensions() > 4, "Only up to 4 dimensions are supported");
107 ARM_COMPUTE_UNUSED(beta);
108 ARM_COMPUTE_RETURN_ERROR_ON(axis < static_cast<int32_t>(-src->num_dimensions()) || static_cast<int32_t>(src->num_dimensions()) <= axis);
109
110 // Create intermediate tensor info
111 DataType tmp_data_type = src->data_type();
112 const TensorInfo tensor_info_tmp(src->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
113
114 TensorShape max_sum_shape = src->tensor_shape();
115 max_sum_shape.set(0, 1);
116 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));
117 const TensorInfo dont_care;
118
119 const unsigned int actual_axis = static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(src->num_dimensions())));
120
121 const bool needs_permute = actual_axis > 0;
122
123 if(needs_permute)
124 {
125 const PermutationVector permutation_vector = softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis);
126 const TensorShape permuted_shape = misc::shape_calculator::compute_permutation_output_shape(*src, permutation_vector);
127 TensorInfo input_permuted(src->clone()->set_tensor_shape(permuted_shape));
128 ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(src, &input_permuted, permutation_vector));
129 TensorInfo output_permuted(dst->clone()->set_tensor_shape(permuted_shape));
130 ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(&output_permuted, dst, permutation_vector));
131 }
132
133 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuLogits1DMaxKernel::validate(src, &tensor_info_max_sum));
134 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuLogits1DSoftmaxKernel<IS_LOG>::validate(&tensor_info_tmp, &tensor_info_max_sum, dst, beta, &dont_care));
135
136 return Status{};
137}
138
139template <bool IS_LOG>
140void CpuSoftmaxGeneric<IS_LOG>::run(ITensorPack &tensors)
141{
142 ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
143
144 ITensorPack max_pack;
145 ITensorPack softmax_pack;
146
147 if(_needs_permute)
148 {
149 ITensorPack permute_in_pack;
150 permute_in_pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(ACL_SRC));
151 permute_in_pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(ACL_INT_2));
152 _permute_input.run(permute_in_pack);
153
154 max_pack.add_tensor(TensorType::ACL_SRC, tensors.get_tensor(ACL_INT_2));
155
156 softmax_pack.add_tensor(TensorType::ACL_SRC_0, tensors.get_tensor(ACL_INT_2));
157 softmax_pack.add_tensor(TensorType::ACL_SRC_1, tensors.get_tensor(ACL_INT_1));
158 softmax_pack.add_tensor(TensorType::ACL_DST_0, tensors.get_tensor(ACL_INT_3));
159 softmax_pack.add_tensor(TensorType::ACL_DST_1, tensors.get_tensor(ACL_INT_0));
160 }
161 else
162 {
163 max_pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(ACL_SRC));
164 softmax_pack.add_tensor(TensorType::ACL_SRC_0, tensors.get_const_tensor(ACL_SRC));
165 softmax_pack.add_tensor(TensorType::ACL_SRC_1, tensors.get_tensor(ACL_INT_1));
166 softmax_pack.add_tensor(TensorType::ACL_DST_0, tensors.get_tensor(ACL_DST));
167 softmax_pack.add_tensor(TensorType::ACL_DST_1, tensors.get_tensor(ACL_INT_0));
168 }
169
170 max_pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(ACL_INT_1));
171
172 NEScheduler::get().schedule_op(_max_kernel.get(), Window::DimY, _max_kernel->window(), max_pack);
173 NEScheduler::get().schedule_op(_softmax_kernel.get(), Window::DimY, _softmax_kernel->window(), softmax_pack);
174
175 if(_needs_permute)
176 {
177 ITensorPack permute_out_pack;
178 permute_out_pack.add_tensor(TensorType::ACL_SRC, tensors.get_tensor(ACL_INT_3));
179 permute_out_pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(ACL_DST));
180 _permute_output.run(permute_out_pack);
181 }
182}
183
184template <bool IS_LOG>
185experimental::MemoryRequirements CpuSoftmaxGeneric<IS_LOG>::workspace() const
186{
187 experimental::MemoryRequirements req{};
188
189 req.push_back({ TensorType::ACL_INT_0, _tmp->total_size(), 0 });
190 req.push_back({ TensorType::ACL_INT_1, _max->total_size(), 0 });
191
192 if(_needs_permute)
193 {
194 req.push_back({ TensorType::ACL_INT_2, _input_permuted->total_size(), 0 });
195 req.push_back({ TensorType::ACL_INT_3, _output_permuted->total_size(), 0 });
196 }
197
198 return req;
199}
200
201template class CpuSoftmaxGeneric<false>;
202template class CpuSoftmaxGeneric<true>;
203} // namespace cpu
204} // namespace arm_compute