blob: 7e41dba8abb3abc4438d1d387b78ed65fdbecd61 [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
Jenkins4ba87db2019-05-23 17:11:51 +01002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier871448e2017-03-24 14:54:29 +00003 *
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
Anthony Barbier8140e1e2017-12-14 23:48:46 +000026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/ICLKernel.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000028#include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h"
29#include "arm_compute/core/Helpers.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000030#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Utils.h"
Jenkinsb9abeae2018-11-22 11:58:08 +000032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Kaizen8938bd32017-09-28 14:38:23 +010033#include "arm_compute/runtime/CL/CLMemoryGroup.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000034#include "arm_compute/runtime/CL/CLScheduler.h"
35
Jenkinsb9abeae2018-11-22 11:58:08 +000036namespace arm_compute
37{
Kaizen8938bd32017-09-28 14:38:23 +010038CLSoftmaxLayer::CLSoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager)
Jenkinsb9abeae2018-11-22 11:58:08 +000039 : _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(),
40 _needs_flattening(false)
Anthony Barbier871448e2017-03-24 14:54:29 +000041{
42}
43
Jenkinsb9abeae2018-11-22 11:58:08 +000044void CLSoftmaxLayer::configure_reshape_input_kernel(const ICLTensor *input, const ICLTensor *output, size_t axis)
45{
46 // Flatten the input
47 const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input->info(), axis);
48
49 // Initialize the flat input
50 _input_flattened.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_flatten));
51
52 // If we need to flatten the input, we can use CLFlattenKernel or CLReshapeKernel
53 // If flattening on the third axes, we use CLFlattenKernel.
54 // In all other cases we have to use CLReshapeKernel
55 if(axis != 3)
56 {
57 auto reshape_kernel_ptr = support::cpp14::make_unique<CLReshapeLayerKernel>();
58 reshape_kernel_ptr->configure(input, &_input_flattened);
59 _flatten_kernel_ptr = std::move(reshape_kernel_ptr);
60 }
61 else
62 {
63 auto flatten_kernel_ptr = support::cpp14::make_unique<CLFlattenLayerKernel>();
64 flatten_kernel_ptr->configure(input, &_input_flattened);
65 _flatten_kernel_ptr = std::move(flatten_kernel_ptr);
66 }
67
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());
71}
72
73void CLSoftmaxLayer::configure(const ICLTensor *input, ICLTensor *output, float beta, size_t axis)
Anthony Barbier871448e2017-03-24 14:54:29 +000074{
Anthony Barbier8140e1e2017-12-14 23:48:46 +000075 // Perform validation step
76 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Jenkinsb9abeae2018-11-22 11:58:08 +000077 ARM_COMPUTE_ERROR_THROW_ON(CLSoftmaxLayer::validate(input->info(), output->info(), beta, axis));
78
79 // We don't need flattening only in the case the input is 2D and axis is 1
80 _needs_flattening = axis != 1;
81
82 // If we are dealing with a 4D tensor, we will:
83 // - Flatten the input, so that we end up with a [width*height*depth] * batches 2D tensor
84 // - Execute all the pipeline (reduction + normalization) on the flattened tensor
85 // - Reshape the flattened output into the real output
86 if(_needs_flattening)
87 {
88 // Add to the memory manager _input_flattened
89 _memory_group.manage(&_input_flattened);
90
91 // Cofigure _flatten_kernel and _input_flattened
92 configure_reshape_input_kernel(input, output, axis);
93 }
94
95 // We want to deal with a 2D input. Either it is the flattened version of the original input (4D case)
96 // or it is the original input case (2D case)
97 const ICLTensor *input_2D = (_needs_flattening ? &_input_flattened : input);
Anthony Barbier871448e2017-03-24 14:54:29 +000098
99 // Create intermediate tensors shapes
Jenkinsb9abeae2018-11-22 11:58:08 +0000100 TensorInfo input_info = input_2D->info()->clone()->reset_padding().set_is_resizable(true);
101 DataType tmp_data_type = is_data_type_quantized_asymmetric(input_2D->info()->data_type()) ? DataType::S32 : input_2D->info()->data_type();
102 TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000103 _tmp.allocator()->init(tensor_info_tmp);
Anthony Barbier871448e2017-03-24 14:54:29 +0000104
Jenkinsb9abeae2018-11-22 11:58:08 +0000105 TensorShape max_sum_shape = input_2D->info()->tensor_shape();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000106 max_sum_shape.set(0, 1);
107 _max.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape));
108 _sum.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape).set_data_type(tmp_data_type));
109
110 // Set GPU target to kernels
111 _max_shift_exp_sum_kernel.set_target(CLScheduler::get().target());
Anthony Barbier871448e2017-03-24 14:54:29 +0000112
Kaizen8938bd32017-09-28 14:38:23 +0100113 // Manage intermediate buffers
114 _memory_group.manage(&_tmp);
115 _memory_group.manage(&_max);
116 _memory_group.manage(&_sum);
117
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000118 // Configure kernels
Jenkinsb9abeae2018-11-22 11:58:08 +0000119 _max_shift_exp_sum_kernel.configure(input_2D, &_max, &_tmp, &_sum, beta);
120
121 if(_needs_flattening)
122 {
123 // Add to the memory manager _output_flattened
124 _memory_group.manage(&_output_flattened);
125
126 // The normalization kernel stores the result in a flat output tensor
127 _norm_kernel.configure(&_tmp, &_sum, &_output_flattened, beta);
128
129 // Reshape the flat output into a the requested (4D) output
130 _reshape_kernel.configure(&_output_flattened, output);
131
132 // Allocate the intermediate flat tensors
133 _input_flattened.allocator()->allocate();
134 _output_flattened.allocator()->allocate();
135 }
136 else
137 {
138 // Softmax 2D case
139 _norm_kernel.configure(&_tmp, &_sum, output, beta);
140 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000141
142 // Allocate intermediate buffers
143 _tmp.allocator()->allocate();
144 _max.allocator()->allocate();
145 _sum.allocator()->allocate();
146}
147
Jenkinsb9abeae2018-11-22 11:58:08 +0000148Status CLSoftmaxLayer::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, size_t axis)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000149{
150 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Jenkinsb9abeae2018-11-22 11:58:08 +0000151 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported");
152 ARM_COMPUTE_UNUSED(beta);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000153
154 // Create intermediate tensor info
155 DataType tmp_data_type = is_data_type_quantized_asymmetric(input->data_type()) ? DataType::S32 : input->data_type();
Jenkins52ba29e2018-08-29 15:32:11 +0000156 TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000157
158 TensorShape max_sum_shape = input->tensor_shape();
159 max_sum_shape.set(0, 1);
Jenkins52ba29e2018-08-29 15:32:11 +0000160 TensorInfo tensor_info_max(input->clone()->set_tensor_shape(max_sum_shape).set_is_resizable(true));
161 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));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000162
Jenkinsb9abeae2018-11-22 11:58:08 +0000163 const bool needs_flattening = (axis != 1);
164
165 if(needs_flattening)
166 {
167 const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input, axis);
168 TensorInfo tensor_info_flat(input->clone()->set_tensor_shape(shape_flatten).set_is_resizable(true));
169
170 if(axis != 3)
171 {
172 ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayerKernel::validate(input, &tensor_info_flat));
173 }
174 else
175 {
176 ARM_COMPUTE_RETURN_ON_ERROR(CLFlattenLayerKernel::validate(input, &tensor_info_flat));
177 }
178 }
179
Anthony Barbier06ea0482018-02-22 15:45:35 +0000180 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DMaxShiftExpSumKernel::validate(input, &tensor_info_max, &tensor_info_tmp, &tensor_info_sum));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000181 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DNormKernel::validate(&tensor_info_tmp, &tensor_info_sum, output));
182
Jenkinsb9abeae2018-11-22 11:58:08 +0000183 if(needs_flattening)
184 {
185 const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input);
186 TensorInfo tensor_info_flat(input->clone()->set_tensor_shape(shape_flatten).set_is_resizable(true));
187 }
188
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000189 return Status{};
190}
191
Anthony Barbier871448e2017-03-24 14:54:29 +0000192void CLSoftmaxLayer::run()
193{
Jenkins4ba87db2019-05-23 17:11:51 +0100194 MemoryGroupResourceScope scope_mg(_memory_group);
Kaizen8938bd32017-09-28 14:38:23 +0100195
Jenkinsb9abeae2018-11-22 11:58:08 +0000196 if(_needs_flattening)
197 {
198 CLScheduler::get().enqueue(*_flatten_kernel_ptr, false);
199 }
Kaizen8938bd32017-09-28 14:38:23 +0100200
Jenkinsb9abeae2018-11-22 11:58:08 +0000201 CLScheduler::get().enqueue(_max_shift_exp_sum_kernel, false);
202 CLScheduler::get().enqueue(_norm_kernel, !_needs_flattening);
203
204 if(_needs_flattening)
205 {
206 CLScheduler::get().enqueue(_reshape_kernel, true);
207 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000208}
Jenkinsb9abeae2018-11-22 11:58:08 +0000209
210} // namespace arm_compute