blob: d6718467d5dd25a286dcfb987155b7b87e558861 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arena4402cb92018-02-15 13:37:40 +00002 * Copyright (c) 2017-2018 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"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010033#include "arm_compute/runtime/CL/CLMemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/runtime/CL/CLScheduler.h"
35
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010036namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037{
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010038CLSoftmaxLayer::CLSoftmaxLayer(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
giuros01efbf6c82018-09-03 09:53:53 +010044void CLSoftmaxLayer::configure_reshape_input_kernel(const ICLTensor *input, const ICLTensor *output, size_t axis)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010045{
46 // Flatten the input
giuros01efbf6c82018-09-03 09:53:53 +010047 const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input->info(), axis);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010048
49 // Initialize the flat input
giuros01efbf6c82018-09-03 09:53:53 +010050 _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 +010051
giuros01efbf6c82018-09-03 09:53:53 +010052 // 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 }
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010067
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
giuros01efbf6c82018-09-03 09:53:53 +010073void CLSoftmaxLayer::configure(const ICLTensor *input, ICLTensor *output, float beta, size_t axis)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074{
Georgios Pinitasee8be2d2017-11-22 12:53:45 +000075 // Perform validation step
76 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
giuros01efbf6c82018-09-03 09:53:53 +010077 ARM_COMPUTE_ERROR_THROW_ON(CLSoftmaxLayer::validate(input->info(), output->info(), beta, axis));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078
giuros01efbf6c82018-09-03 09:53:53 +010079 // We don't need flattening only in the case the input is 2D and axis is 1
80 _needs_flattening = axis != 1;
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010081
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 {
giuros01efbf6c82018-09-03 09:53:53 +010088 // Add to the memory manager _input_flattened
89 _memory_group.manage(&_input_flattened);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010090
giuros01efbf6c82018-09-03 09:53:53 +010091 // Cofigure _flatten_kernel and _input_flattened
92 configure_reshape_input_kernel(input, output, axis);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010093 }
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)
giuros01efbf6c82018-09-03 09:53:53 +010097 const ICLTensor *input_2D = (_needs_flattening ? &_input_flattened : input);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010098
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 // Create intermediate tensors shapes
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100100 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));
Chunosovf450caa2017-11-08 16:09:35 +0700103 _tmp.allocator()->init(tensor_info_tmp);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100105 TensorShape max_sum_shape = input_2D->info()->tensor_shape();
Chunosovf450caa2017-11-08 16:09:35 +0700106 max_sum_shape.set(0, 1);
Georgios Pinitasee8be2d2017-11-22 12:53:45 +0000107 _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));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109
Chunosovd6afedc2017-11-06 22:09:45 +0700110 // Set GPU target to kernels
111 _max_shift_exp_sum_kernel.set_target(CLScheduler::get().target());
112
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100113 // Manage intermediate buffers
114 _memory_group.manage(&_tmp);
115 _memory_group.manage(&_max);
116 _memory_group.manage(&_sum);
117
Chunosovd6afedc2017-11-06 22:09:45 +0700118 // Configure kernels
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100119 _max_shift_exp_sum_kernel.configure(input_2D, &_max, &_tmp, &_sum, beta);
120
121 if(_needs_flattening)
122 {
giuros01efbf6c82018-09-03 09:53:53 +0100123 // Add to the memory manager _output_flattened
124 _memory_group.manage(&_output_flattened);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100125
126 // The normalization kernel stores the result in a flat output tensor
giuros01efbf6c82018-09-03 09:53:53 +0100127 _norm_kernel.configure(&_tmp, &_sum, &_output_flattened, beta);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100128
129 // Reshape the flat output into a the requested (4D) output
giuros01efbf6c82018-09-03 09:53:53 +0100130 _reshape_kernel.configure(&_output_flattened, output);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100131
132 // Allocate the intermediate flat tensors
giuros01efbf6c82018-09-03 09:53:53 +0100133 _input_flattened.allocator()->allocate();
134 _output_flattened.allocator()->allocate();
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100135 }
136 else
137 {
138 // Softmax 2D case
139 _norm_kernel.configure(&_tmp, &_sum, output, beta);
140 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141
142 // Allocate intermediate buffers
143 _tmp.allocator()->allocate();
144 _max.allocator()->allocate();
145 _sum.allocator()->allocate();
146}
147
giuros01efbf6c82018-09-03 09:53:53 +0100148Status CLSoftmaxLayer::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, size_t axis)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000149{
Georgios Pinitasee8be2d2017-11-22 12:53:45 +0000150 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100151 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported");
giuros01efbf6c82018-09-03 09:53:53 +0100152 ARM_COMPUTE_UNUSED(beta);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000153
154 // Create intermediate tensor info
155 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 +0100156 TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000157
158 TensorShape max_sum_shape = input->tensor_shape();
159 max_sum_shape.set(0, 1);
Michele Di Giorgio5cb37732018-06-08 18:07:08 +0100160 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));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000162
giuros01efbf6c82018-09-03 09:53:53 +0100163 const bool needs_flattening = (axis != 1);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100164
giuros01efbf6c82018-09-03 09:53:53 +0100165 if(needs_flattening)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100166 {
giuros01efbf6c82018-09-03 09:53:53 +0100167 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 }
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100178 }
179
Giorgio Arena4402cb92018-02-15 13:37:40 +0000180 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DMaxShiftExpSumKernel::validate(input, &tensor_info_max, &tensor_info_tmp, &tensor_info_sum));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000181 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DNormKernel::validate(&tensor_info_tmp, &tensor_info_sum, output));
182
giuros01efbf6c82018-09-03 09:53:53 +0100183 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
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000189 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000190}
191
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192void CLSoftmaxLayer::run()
193{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100194 _memory_group.acquire();
giuros01efbf6c82018-09-03 09:53:53 +0100195
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100196 if(_needs_flattening)
197 {
giuros01efbf6c82018-09-03 09:53:53 +0100198 CLScheduler::get().enqueue(*_flatten_kernel_ptr, false);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100199 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100200
Giorgio Arena4402cb92018-02-15 13:37:40 +0000201 CLScheduler::get().enqueue(_max_shift_exp_sum_kernel, false);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100202 CLScheduler::get().enqueue(_norm_kernel, !_needs_flattening);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100203
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100204 if(_needs_flattening)
205 {
206 CLScheduler::get().enqueue(_reshape_kernel, true);
207 }
208
209 // Relase intermediate buffers
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100210 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211}
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100212
213} // namespace arm_compute