blob: 5509edec87b7998b59499132b81f8cac51a9dd94 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sheri Zhang1f567af2020-05-05 11:47:36 +01002 * Copyright (c) 2017-2020 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 "arm_compute/runtime/NEON/functions/NESoftmaxLayer.h"
25
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/NEON/kernels/NESoftmaxLayerKernel.h"
Manuel Bottini678d83a2019-01-07 16:05:36 +000028#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
Manuel Bottini678d83a2019-01-07 16:05:36 +000031namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032{
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010033template <bool IS_LOG>
34NESoftmaxLayerGeneric<IS_LOG>::NESoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
Manuel Bottini678d83a2019-01-07 16:05:36 +000035 : _memory_group(std::move(memory_manager)), _max_kernel(), _softmax_kernel(), _flat_or_reshape_kernel_ptr(nullptr), _fill_border_kernel(), _reshape_kernel(), _max(), _tmp(), _input_flattened(),
36 _output_flattened(), _needs_flattening(false)
37{
38}
39
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010040template <bool IS_LOG>
SiCong Lid004a7a2020-05-28 15:26:41 +010041void NESoftmaxLayerGeneric<IS_LOG>::configure_reshape_input_kernel(const ITensor *input, const ITensor *output, int32_t first_n_reduce_axes)
Manuel Bottini678d83a2019-01-07 16:05:36 +000042{
43 // Flatten the input
SiCong Lid004a7a2020-05-28 15:26:41 +010044 const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input->info(), first_n_reduce_axes);
Manuel Bottini678d83a2019-01-07 16:05:36 +000045
46 // Initialize the flat input
47 _input_flattened.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_flatten));
48
49 // If we need to flatten the input, we can use NEFlattenKernel or NEReshapeKernel
SiCong Lid004a7a2020-05-28 15:26:41 +010050 // If the number of reduced axes is 3 (max dimension), which means collapsing all axes except the batch axis, we use NEFlattenKernel.
Manuel Bottini678d83a2019-01-07 16:05:36 +000051 // In all other cases we have to use NEReshapeKernel
SiCong Lid004a7a2020-05-28 15:26:41 +010052 // Note that the "other cases" include both:
53 // 1. first_n_reduce_axes < 3: Reduce the first 1 (no need to reduce) or 2 dimensions (inclusive)
54 // 2. first_n_reduce_axes == 4: Reduce all 4 dimensions. This can only be handled by NEReshapeKernel instead of NEFlattenKernel.
55 if(first_n_reduce_axes == 3)
Manuel Bottini678d83a2019-01-07 16:05:36 +000056 {
57 auto flatten_kernel_ptr = support::cpp14::make_unique<NEFlattenLayerKernel>();
58 flatten_kernel_ptr->configure(input, &_input_flattened);
59 _flat_or_reshape_kernel_ptr = std::move(flatten_kernel_ptr);
60 }
SiCong Lid004a7a2020-05-28 15:26:41 +010061 else
62 {
63 auto reshape_kernel_ptr = support::cpp14::make_unique<NEReshapeLayerKernel>();
64 reshape_kernel_ptr->configure(input, &_input_flattened);
65 _flat_or_reshape_kernel_ptr = std::move(reshape_kernel_ptr);
66 }
Manuel Bottini678d83a2019-01-07 16:05:36 +000067
68 // We need to init the output tensor here. Indeed, the reshape kernel expects
69 // both tensors to be already initialized
70 auto_init_if_empty(*output->info(), *input->info()->clone());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071}
72
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010073template <bool IS_LOG>
SiCong Lid004a7a2020-05-28 15:26:41 +010074void NESoftmaxLayerGeneric<IS_LOG>::configure(ITensor *input, ITensor *output, float beta, int32_t reduce_end_axis)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075{
Manuel Bottini678d83a2019-01-07 16:05:36 +000076 // Perform validation step
Michalis Spyrouafa5d812017-11-30 14:25:57 +000077 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
SiCong Lid004a7a2020-05-28 15:26:41 +010078 ARM_COMPUTE_ERROR_THROW_ON(NESoftmaxLayerGeneric::validate(input->info(), output->info(), beta, reduce_end_axis));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079
SiCong Lid004a7a2020-05-28 15:26:41 +010080 // Convert reduce-before axis (inclusive) to first n axes to reduce
81 size_t first_n_reduce_axes = dim_index_2_num_dims(reduce_end_axis, static_cast<int32_t>(input->info()->num_dimensions()));
Sheri Zhang1f567af2020-05-05 11:47:36 +010082
SiCong Lid004a7a2020-05-28 15:26:41 +010083 // We only need flattening when the number of axes to reduce is greater than 1
84 _needs_flattening = first_n_reduce_axes > 1;
Manuel Bottini678d83a2019-01-07 16:05:36 +000085
86 // If we are dealing with a 4D tensor, we will:
87 // - Flatten the input, so that we end up with a [width*height*depth] * batches 2D tensor
88 // - Execute all the pipeline (reduction + normalization) on the flattened tensor
89 // - Reshape the flattened output into the real output
90 if(_needs_flattening)
91 {
92 // Add to the memory manager _input_flattened
93 _memory_group.manage(&_input_flattened);
94
95 // Configure _flatten_kernel and _input_flattened
SiCong Lid004a7a2020-05-28 15:26:41 +010096 configure_reshape_input_kernel(input, output, first_n_reduce_axes);
Manuel Bottini678d83a2019-01-07 16:05:36 +000097 }
98
99 // We want to deal with a 2D input. Either it is the flattened version of the original input (4D case)
100 // or it is the original input case (2D case)
101 ITensor *input_2D = (_needs_flattening ? &_input_flattened : input);
102
103 // Create intermediate tensors shapes
104 const TensorInfo input_info = input_2D->info()->clone()->reset_padding().set_is_resizable(true);
105 DataType tmp_data_type = is_data_type_quantized_asymmetric(input_2D->info()->data_type()) ? DataType::F32 : input_2D->info()->data_type();
106 TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type));
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000107
108 // Init intermediate tensors
Manuel Bottini678d83a2019-01-07 16:05:36 +0000109 TensorShape max_sum_shape = input_2D->info()->tensor_shape();
110 max_sum_shape.set(0, 1);
111 _max.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape));
112 _tmp.allocator()->init(tensor_info_tmp);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000113
114 // Manage intermediate buffers
115 _memory_group.manage(&_max);
116 _memory_group.manage(&_tmp);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117
Manuel Bottini678d83a2019-01-07 16:05:36 +0000118 // Configure Kernels
119 _max_kernel.configure(input_2D, &_max);
120 if(_needs_flattening)
121 {
122 // Add to the memory manager _output_flattened
123 _memory_group.manage(&_output_flattened);
124
125 // The normalization kernel stores the result in a flat output tensor
126 _softmax_kernel.configure(input_2D, &_max, &_output_flattened, beta, &_tmp);
127 _input_flattened.allocator()->allocate();
128
129 // Reshape the flat output into the requested (4D) output
130 _reshape_kernel.configure(&_output_flattened, output);
131
132 // Allocate the intermediate flat tensors
133 _output_flattened.allocator()->allocate();
134 }
135 else
136 {
137 // Softmax 2D case
138 _fill_border_kernel.configure(input_2D, _max_kernel.border_size(), BorderMode::REPLICATE);
139 _softmax_kernel.configure(input_2D, &_max, output, beta, &_tmp);
140 }
141
142 // Allocate intermediate buffers
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 _max.allocator()->allocate();
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000144 _tmp.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145}
146
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100147template <bool IS_LOG>
SiCong Lid004a7a2020-05-28 15:26:41 +0100148Status NESoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t reduce_end_axis)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000149{
150 // Perform validation step
151 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Manuel Bottini678d83a2019-01-07 16:05:36 +0000152 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported");
153 ARM_COMPUTE_UNUSED(beta);
SiCong Lid004a7a2020-05-28 15:26:41 +0100154 ARM_COMPUTE_RETURN_ERROR_ON(reduce_end_axis < static_cast<int32_t>(-input->num_dimensions()) || static_cast<int32_t>(input->num_dimensions()) <= reduce_end_axis);
Sheri Zhang1f567af2020-05-05 11:47:36 +0100155
SiCong Lid004a7a2020-05-28 15:26:41 +0100156 // Convert reduce-before axis (inclusive) to first n axes to reduce
157 size_t first_n_reduce_axes = dim_index_2_num_dims(reduce_end_axis, static_cast<int32_t>(input->num_dimensions()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000158
Manuel Bottini678d83a2019-01-07 16:05:36 +0000159 // Create intermediate tensor info
160 DataType tmp_data_type = input->data_type();
161 const TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
162
163 TensorShape max_sum_shape = input->tensor_shape();
164 max_sum_shape.set(0, 1);
165 const TensorInfo tensor_info_max_sum(input->clone()->set_tensor_shape(max_sum_shape).set_data_type(tmp_data_type).set_quantization_info(input->quantization_info()).set_is_resizable(true));
166 const TensorInfo dont_care;
167
SiCong Lid004a7a2020-05-28 15:26:41 +0100168 const bool needs_flattening = (first_n_reduce_axes > 1);
Manuel Bottini678d83a2019-01-07 16:05:36 +0000169
170 if(needs_flattening)
171 {
SiCong Lid004a7a2020-05-28 15:26:41 +0100172 const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input, first_n_reduce_axes);
Manuel Bottini678d83a2019-01-07 16:05:36 +0000173 TensorInfo tensor_info_flat(input->clone()->set_tensor_shape(shape_flatten).set_is_resizable(true));
174
SiCong Lid004a7a2020-05-28 15:26:41 +0100175 if(first_n_reduce_axes == 3)
Manuel Bottini678d83a2019-01-07 16:05:36 +0000176 {
SiCong Lid004a7a2020-05-28 15:26:41 +0100177 ARM_COMPUTE_RETURN_ON_ERROR(NEFlattenLayerKernel::validate(input, &tensor_info_flat));
Manuel Bottini678d83a2019-01-07 16:05:36 +0000178 }
179 else
180 {
SiCong Lid004a7a2020-05-28 15:26:41 +0100181 ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayerKernel::validate(input, &tensor_info_flat));
Manuel Bottini678d83a2019-01-07 16:05:36 +0000182 }
183 }
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000184
185 ARM_COMPUTE_RETURN_ON_ERROR(NELogits1DMaxKernel::validate(input, &tensor_info_max_sum));
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100186 ARM_COMPUTE_RETURN_ON_ERROR(NELogits1DSoftmaxKernel<IS_LOG>::validate(&tensor_info_tmp, &tensor_info_max_sum, output, beta, &dont_care));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000187
188 return Status{};
189}
190
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100191template <bool IS_LOG>
192void NESoftmaxLayerGeneric<IS_LOG>::run()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100194 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100195
Manuel Bottini678d83a2019-01-07 16:05:36 +0000196 if(_needs_flattening)
197 {
198 NEScheduler::get().schedule(_flat_or_reshape_kernel_ptr.get(), Window::DimY);
199 }
200
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201 NEScheduler::get().schedule(&_fill_border_kernel, Window::DimY);
202 NEScheduler::get().schedule(&_max_kernel, Window::DimY);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000203 NEScheduler::get().schedule(&_softmax_kernel, Window::DimY);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100204
Manuel Bottini678d83a2019-01-07 16:05:36 +0000205 if(_needs_flattening)
206 {
207 NEScheduler::get().schedule(&_reshape_kernel, Window::DimY);
208 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209}
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100210
211template class NESoftmaxLayerGeneric<false>;
212template class NESoftmaxLayerGeneric<true>;
213
Manuel Bottini678d83a2019-01-07 16:05:36 +0000214} // namespace arm_compute