blob: 79a94961d82873df310d1e486bc86e6a257b2033 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottini678d83a2019-01-07 16:05:36 +00002 * Copyright (c) 2017-2019 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"
Manuel Bottini678d83a2019-01-07 16:05:36 +000030#include "utils/TypePrinter.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031
32#include <cfloat>
33
Manuel Bottini678d83a2019-01-07 16:05:36 +000034namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035{
Manuel Bottini678d83a2019-01-07 16:05:36 +000036NESoftmaxLayer::NESoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager)
37 : _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(),
38 _output_flattened(), _needs_flattening(false)
39{
40}
41
42void NESoftmaxLayer::configure_reshape_input_kernel(const ITensor *input, const ITensor *output, size_t axis)
43{
44 // Flatten the input
45 const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input->info(), axis);
46
47 // Initialize the flat input
48 _input_flattened.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_flatten));
49
50 // If we need to flatten the input, we can use NEFlattenKernel or NEReshapeKernel
51 // If flattening on the third axes, we use NEFlattenKernel.
52 // In all other cases we have to use NEReshapeKernel
53 if(axis != 3)
54 {
55 auto reshape_kernel_ptr = support::cpp14::make_unique<NEReshapeLayerKernel>();
56 reshape_kernel_ptr->configure(input, &_input_flattened);
57 _flat_or_reshape_kernel_ptr = std::move(reshape_kernel_ptr);
58 }
59 else
60 {
61 auto flatten_kernel_ptr = support::cpp14::make_unique<NEFlattenLayerKernel>();
62 flatten_kernel_ptr->configure(input, &_input_flattened);
63 _flat_or_reshape_kernel_ptr = std::move(flatten_kernel_ptr);
64 }
65
66 // We need to init the output tensor here. Indeed, the reshape kernel expects
67 // both tensors to be already initialized
68 auto_init_if_empty(*output->info(), *input->info()->clone());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069}
70
giuros01efbf6c82018-09-03 09:53:53 +010071void NESoftmaxLayer::configure(ITensor *input, ITensor *output, float beta, size_t axis)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072{
Manuel Bottini678d83a2019-01-07 16:05:36 +000073 // Perform validation step
Michalis Spyrouafa5d812017-11-30 14:25:57 +000074 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Manuel Bottini678d83a2019-01-07 16:05:36 +000075 ARM_COMPUTE_ERROR_THROW_ON(NESoftmaxLayer::validate(input->info(), output->info(), beta, axis));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076
Manuel Bottini678d83a2019-01-07 16:05:36 +000077 // We don't need flattening only in the case the input is 2D and axis is 1
78 _needs_flattening = axis != 1;
79
80 // If we are dealing with a 4D tensor, we will:
81 // - Flatten the input, so that we end up with a [width*height*depth] * batches 2D tensor
82 // - Execute all the pipeline (reduction + normalization) on the flattened tensor
83 // - Reshape the flattened output into the real output
84 if(_needs_flattening)
85 {
86 // Add to the memory manager _input_flattened
87 _memory_group.manage(&_input_flattened);
88
89 // Configure _flatten_kernel and _input_flattened
90 configure_reshape_input_kernel(input, output, axis);
91 }
92
93 // We want to deal with a 2D input. Either it is the flattened version of the original input (4D case)
94 // or it is the original input case (2D case)
95 ITensor *input_2D = (_needs_flattening ? &_input_flattened : input);
96
97 // Create intermediate tensors shapes
98 const TensorInfo input_info = input_2D->info()->clone()->reset_padding().set_is_resizable(true);
99 DataType tmp_data_type = is_data_type_quantized_asymmetric(input_2D->info()->data_type()) ? DataType::F32 : input_2D->info()->data_type();
100 TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type));
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000101
102 // Init intermediate tensors
Manuel Bottini678d83a2019-01-07 16:05:36 +0000103 TensorShape max_sum_shape = input_2D->info()->tensor_shape();
104 max_sum_shape.set(0, 1);
105 _max.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape));
106 _tmp.allocator()->init(tensor_info_tmp);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000107
108 // Manage intermediate buffers
109 _memory_group.manage(&_max);
110 _memory_group.manage(&_tmp);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111
Manuel Bottini678d83a2019-01-07 16:05:36 +0000112 // Configure Kernels
113 _max_kernel.configure(input_2D, &_max);
114 if(_needs_flattening)
115 {
116 // Add to the memory manager _output_flattened
117 _memory_group.manage(&_output_flattened);
118
119 // The normalization kernel stores the result in a flat output tensor
120 _softmax_kernel.configure(input_2D, &_max, &_output_flattened, beta, &_tmp);
121 _input_flattened.allocator()->allocate();
122
123 // Reshape the flat output into the requested (4D) output
124 _reshape_kernel.configure(&_output_flattened, output);
125
126 // Allocate the intermediate flat tensors
127 _output_flattened.allocator()->allocate();
128 }
129 else
130 {
131 // Softmax 2D case
132 _fill_border_kernel.configure(input_2D, _max_kernel.border_size(), BorderMode::REPLICATE);
133 _softmax_kernel.configure(input_2D, &_max, output, beta, &_tmp);
134 }
135
136 // Allocate intermediate buffers
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 _max.allocator()->allocate();
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000138 _tmp.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139}
140
giuros01efbf6c82018-09-03 09:53:53 +0100141Status NESoftmaxLayer::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, size_t axis)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000142{
143 // Perform validation step
144 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Manuel Bottini678d83a2019-01-07 16:05:36 +0000145 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported");
146 ARM_COMPUTE_UNUSED(beta);
147 ARM_COMPUTE_RETURN_ERROR_ON(axis < 1 || input->num_dimensions() < axis);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000148
Manuel Bottini678d83a2019-01-07 16:05:36 +0000149 // Create intermediate tensor info
150 DataType tmp_data_type = input->data_type();
151 const TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
152
153 TensorShape max_sum_shape = input->tensor_shape();
154 max_sum_shape.set(0, 1);
155 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));
156 const TensorInfo dont_care;
157
158 const bool needs_flattening = (axis != 1);
159
160 if(needs_flattening)
161 {
162 const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input, axis);
163 TensorInfo tensor_info_flat(input->clone()->set_tensor_shape(shape_flatten).set_is_resizable(true));
164
165 if(axis != 3)
166 {
167 ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayerKernel::validate(input, &tensor_info_flat));
168 }
169 else
170 {
171 ARM_COMPUTE_RETURN_ON_ERROR(NEFlattenLayerKernel::validate(input, &tensor_info_flat));
172 }
173 }
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000174
175 ARM_COMPUTE_RETURN_ON_ERROR(NELogits1DMaxKernel::validate(input, &tensor_info_max_sum));
Manuel Bottini678d83a2019-01-07 16:05:36 +0000176 ARM_COMPUTE_RETURN_ON_ERROR(NELogits1DSoftmaxKernel::validate(&tensor_info_tmp, &tensor_info_max_sum, output, beta, &dont_care));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000177
178 return Status{};
179}
180
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181void NESoftmaxLayer::run()
182{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100183 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100184
Manuel Bottini678d83a2019-01-07 16:05:36 +0000185 if(_needs_flattening)
186 {
187 NEScheduler::get().schedule(_flat_or_reshape_kernel_ptr.get(), Window::DimY);
188 }
189
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 NEScheduler::get().schedule(&_fill_border_kernel, Window::DimY);
191 NEScheduler::get().schedule(&_max_kernel, Window::DimY);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000192 NEScheduler::get().schedule(&_softmax_kernel, Window::DimY);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100193
Manuel Bottini678d83a2019-01-07 16:05:36 +0000194 if(_needs_flattening)
195 {
196 NEScheduler::get().schedule(&_reshape_kernel, Window::DimY);
197 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198}
Manuel Bottini678d83a2019-01-07 16:05:36 +0000199} // namespace arm_compute