blob: b0b2117cd99d8c3c8fd3fc221c2c3310a2a29999 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottini2b84be52020-04-08 10:15:51 +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/CL/functions/CLSoftmaxLayer.h"
25
Chunosovd6afedc2017-11-06 22:09:45 +070026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/ICLKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h"
29#include "arm_compute/core/Helpers.h"
Chunosovd6afedc2017-11-06 22:09:45 +070030#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Utils.h"
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/runtime/CL/CLScheduler.h"
34
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010035namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036{
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000037template <bool IS_LOG>
38CLSoftmaxLayerGeneric<IS_LOG>::CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
giuros01efbf6c82018-09-03 09:53:53 +010039 : _memory_group(std::move(memory_manager)), _max_shift_exp_sum_kernel(), _norm_kernel(), _flatten_kernel_ptr(), _reshape_kernel(), _max(), _sum(), _tmp(), _input_flattened(), _output_flattened(),
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010040 _needs_flattening(false)
41{
42}
43
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000044template <bool IS_LOG>
45void CLSoftmaxLayerGeneric<IS_LOG>::configure_reshape_input_kernel(const ICLTensor *input, const ICLTensor *output, size_t axis)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010046{
Manuel Bottini2b84be52020-04-08 10:15:51 +010047 configure_reshape_input_kernel(CLKernelLibrary::get().get_compile_context(), input, output, axis);
48}
49
50template <bool IS_LOG>
51void CLSoftmaxLayerGeneric<IS_LOG>::configure_reshape_input_kernel(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *output, size_t axis)
52{
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010053 // Flatten the input
giuros01efbf6c82018-09-03 09:53:53 +010054 const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input->info(), axis);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010055
56 // Initialize the flat input
giuros01efbf6c82018-09-03 09:53:53 +010057 _input_flattened.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_flatten));
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010058
giuros01efbf6c82018-09-03 09:53:53 +010059 // If we need to flatten the input, we can use CLFlattenKernel or CLReshapeKernel
60 // If flattening on the third axes, we use CLFlattenKernel.
61 // In all other cases we have to use CLReshapeKernel
62 if(axis != 3)
63 {
64 auto reshape_kernel_ptr = support::cpp14::make_unique<CLReshapeLayerKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +010065 reshape_kernel_ptr->configure(compile_context, input, &_input_flattened);
giuros01efbf6c82018-09-03 09:53:53 +010066 _flatten_kernel_ptr = std::move(reshape_kernel_ptr);
67 }
68 else
69 {
70 auto flatten_kernel_ptr = support::cpp14::make_unique<CLFlattenLayerKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +010071 flatten_kernel_ptr->configure(compile_context, input, &_input_flattened);
giuros01efbf6c82018-09-03 09:53:53 +010072 _flatten_kernel_ptr = std::move(flatten_kernel_ptr);
73 }
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010074
75 // We need to init the output tensor here. Indeed, the reshape kernel expects
76 // both tensors to be already initialized
77 auto_init_if_empty(*output->info(), *input->info()->clone());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078}
79
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000080template <bool IS_LOG>
81void CLSoftmaxLayerGeneric<IS_LOG>::configure(const ICLTensor *input, ICLTensor *output, float beta, size_t axis)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082{
Manuel Bottini2b84be52020-04-08 10:15:51 +010083 configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis);
84}
85
86template <bool IS_LOG>
87void CLSoftmaxLayerGeneric<IS_LOG>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, float beta, size_t axis)
88{
Georgios Pinitasee8be2d2017-11-22 12:53:45 +000089 // Perform validation step
90 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000091 ARM_COMPUTE_ERROR_THROW_ON(CLSoftmaxLayerGeneric<IS_LOG>::validate(input->info(), output->info(), beta, axis));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092
giuros01efbf6c82018-09-03 09:53:53 +010093 // We don't need flattening only in the case the input is 2D and axis is 1
94 _needs_flattening = axis != 1;
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010095
96 // If we are dealing with a 4D tensor, we will:
97 // - Flatten the input, so that we end up with a [width*height*depth] * batches 2D tensor
98 // - Execute all the pipeline (reduction + normalization) on the flattened tensor
99 // - Reshape the flattened output into the real output
100 if(_needs_flattening)
101 {
giuros01efbf6c82018-09-03 09:53:53 +0100102 // Add to the memory manager _input_flattened
103 _memory_group.manage(&_input_flattened);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100104
giuros01efbf6c82018-09-03 09:53:53 +0100105 // Cofigure _flatten_kernel and _input_flattened
106 configure_reshape_input_kernel(input, output, axis);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100107 }
108
109 // We want to deal with a 2D input. Either it is the flattened version of the original input (4D case)
110 // or it is the original input case (2D case)
giuros01efbf6c82018-09-03 09:53:53 +0100111 const ICLTensor *input_2D = (_needs_flattening ? &_input_flattened : input);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100112
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 // Create intermediate tensors shapes
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100114 TensorInfo input_info = input_2D->info()->clone()->reset_padding().set_is_resizable(true);
115 DataType tmp_data_type = is_data_type_quantized_asymmetric(input_2D->info()->data_type()) ? DataType::S32 : input_2D->info()->data_type();
116 TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type));
Chunosovf450caa2017-11-08 16:09:35 +0700117 _tmp.allocator()->init(tensor_info_tmp);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100119 TensorShape max_sum_shape = input_2D->info()->tensor_shape();
Chunosovf450caa2017-11-08 16:09:35 +0700120 max_sum_shape.set(0, 1);
Georgios Pinitasee8be2d2017-11-22 12:53:45 +0000121 _max.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape));
122 _sum.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape).set_data_type(tmp_data_type));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123
Chunosovd6afedc2017-11-06 22:09:45 +0700124 // Set GPU target to kernels
125 _max_shift_exp_sum_kernel.set_target(CLScheduler::get().target());
126
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100127 // Manage intermediate buffers
128 _memory_group.manage(&_tmp);
129 _memory_group.manage(&_max);
130 _memory_group.manage(&_sum);
131
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000132 SoftmaxKernelInfo softmax_info;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000133 softmax_info.beta = beta;
134 softmax_info.is_log = IS_LOG;
135 softmax_info.input_data_type = input_2D->info()->data_type();
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000136
Chunosovd6afedc2017-11-06 22:09:45 +0700137 // Configure kernels
Manuel Bottini2b84be52020-04-08 10:15:51 +0100138 _max_shift_exp_sum_kernel.configure(compile_context, input_2D, &_max, &_tmp, &_sum, softmax_info);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100139
140 if(_needs_flattening)
141 {
giuros01efbf6c82018-09-03 09:53:53 +0100142 // Add to the memory manager _output_flattened
143 _memory_group.manage(&_output_flattened);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100144
145 // The normalization kernel stores the result in a flat output tensor
Manuel Bottini2b84be52020-04-08 10:15:51 +0100146 _norm_kernel.configure(compile_context, &_tmp, &_sum, &_output_flattened, softmax_info);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100147
148 // Reshape the flat output into a the requested (4D) output
Manuel Bottini2b84be52020-04-08 10:15:51 +0100149 _reshape_kernel.configure(compile_context, &_output_flattened, output);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100150
151 // Allocate the intermediate flat tensors
giuros01efbf6c82018-09-03 09:53:53 +0100152 _input_flattened.allocator()->allocate();
153 _output_flattened.allocator()->allocate();
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100154 }
155 else
156 {
157 // Softmax 2D case
Manuel Bottini2b84be52020-04-08 10:15:51 +0100158 _norm_kernel.configure(compile_context, &_tmp, &_sum, output, softmax_info);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100159 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160
161 // Allocate intermediate buffers
162 _tmp.allocator()->allocate();
163 _max.allocator()->allocate();
164 _sum.allocator()->allocate();
165}
166
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000167template <bool IS_LOG>
168Status CLSoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, size_t axis)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000169{
Georgios Pinitasee8be2d2017-11-22 12:53:45 +0000170 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100171 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported");
giuros01efbf6c82018-09-03 09:53:53 +0100172 ARM_COMPUTE_UNUSED(beta);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000173
174 // Create intermediate tensor info
175 DataType tmp_data_type = is_data_type_quantized_asymmetric(input->data_type()) ? DataType::S32 : input->data_type();
Michele Di Giorgio5cb37732018-06-08 18:07:08 +0100176 TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000177
178 TensorShape max_sum_shape = input->tensor_shape();
179 max_sum_shape.set(0, 1);
Michele Di Giorgio5cb37732018-06-08 18:07:08 +0100180 TensorInfo tensor_info_max(input->clone()->set_tensor_shape(max_sum_shape).set_is_resizable(true));
181 TensorInfo tensor_info_sum(input->clone()->set_tensor_shape(max_sum_shape).set_data_type(tmp_data_type).set_quantization_info(QuantizationInfo()).set_is_resizable(true));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000182
giuros01efbf6c82018-09-03 09:53:53 +0100183 const bool needs_flattening = (axis != 1);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100184
giuros01efbf6c82018-09-03 09:53:53 +0100185 if(needs_flattening)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100186 {
giuros01efbf6c82018-09-03 09:53:53 +0100187 const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input, axis);
188 TensorInfo tensor_info_flat(input->clone()->set_tensor_shape(shape_flatten).set_is_resizable(true));
189
190 if(axis != 3)
191 {
192 ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayerKernel::validate(input, &tensor_info_flat));
193 }
194 else
195 {
196 ARM_COMPUTE_RETURN_ON_ERROR(CLFlattenLayerKernel::validate(input, &tensor_info_flat));
197 }
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100198 }
199
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000200 SoftmaxKernelInfo softmax_info;
201 softmax_info.beta = beta;
202 softmax_info.is_log = IS_LOG;
203 softmax_info.input_data_type = input->data_type();
204
Giorgio Arena4402cb92018-02-15 13:37:40 +0000205 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DMaxShiftExpSumKernel::validate(input, &tensor_info_max, &tensor_info_tmp, &tensor_info_sum));
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000206 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DNormKernel::validate(&tensor_info_tmp, &tensor_info_sum, output, softmax_info));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000207
giuros01efbf6c82018-09-03 09:53:53 +0100208 if(needs_flattening)
209 {
210 const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input);
211 TensorInfo tensor_info_flat(input->clone()->set_tensor_shape(shape_flatten).set_is_resizable(true));
212 }
213
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000214 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000215}
216
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000217template <bool IS_LOG>
Manuel Bottini2b84be52020-04-08 10:15:51 +0100218void CLSoftmaxLayerGeneric<IS_LOG>::run()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100219{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100220 MemoryGroupResourceScope scope_mg(_memory_group);
giuros01efbf6c82018-09-03 09:53:53 +0100221
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100222 if(_needs_flattening)
223 {
giuros01efbf6c82018-09-03 09:53:53 +0100224 CLScheduler::get().enqueue(*_flatten_kernel_ptr, false);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100225 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100226
Giorgio Arena4402cb92018-02-15 13:37:40 +0000227 CLScheduler::get().enqueue(_max_shift_exp_sum_kernel, false);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100228 CLScheduler::get().enqueue(_norm_kernel, !_needs_flattening);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100229
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100230 if(_needs_flattening)
231 {
232 CLScheduler::get().enqueue(_reshape_kernel, true);
233 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234}
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100235
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000236template class CLSoftmaxLayerGeneric<false>;
237template class CLSoftmaxLayerGeneric<true>;
238
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100239} // namespace arm_compute