blob: 720f9111a5831c0dd162916ae456eb5eb23410ff [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +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)
SiCong Li96209c72020-08-21 12:28:30 +010039 : _memory_group(std::move(memory_manager)), _permute_input(), _permute_output(), _max_shift_exp_sum_kernel(), _norm_kernel(), _max(), _sum(), _tmp(), _input_permuted(), _output_permuted(),
40 _needs_permute()
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010041{
42}
43
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000044template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010045void CLSoftmaxLayerGeneric<IS_LOG>::configure(const ICLTensor *input, ICLTensor *output, float beta, int32_t axis)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046{
morgolock9c7fed82020-08-05 12:30:56 +010047 configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis);
Manuel Bottini2b84be52020-04-08 10:15:51 +010048}
49
50template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010051void CLSoftmaxLayerGeneric<IS_LOG>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, float beta, int32_t axis)
Manuel Bottini2b84be52020-04-08 10:15:51 +010052{
Georgios Pinitasee8be2d2017-11-22 12:53:45 +000053 // Perform validation step
54 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
morgolock9c7fed82020-08-05 12:30:56 +010055 ARM_COMPUTE_ERROR_THROW_ON(CLSoftmaxLayerGeneric<IS_LOG>::validate(input->info(), output->info(), beta, axis));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056
SiCong Li96209c72020-08-21 12:28:30 +010057 const size_t actual_axis = static_cast<size_t>(wrap_around(axis, static_cast<int32_t>(input->info()->num_dimensions())));
SiCong Lid004a7a2020-05-28 15:26:41 +010058
SiCong Li96209c72020-08-21 12:28:30 +010059 _needs_permute = actual_axis != 0;
60 ICLTensor *tmp_output = output;
61 const ICLTensor *tmp_input = _needs_permute ? &_input_permuted : input;
62 if(_needs_permute)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010063 {
SiCong Li96209c72020-08-21 12:28:30 +010064 _memory_group.manage(&_input_permuted);
65 _memory_group.manage(&_output_permuted);
66 _permute_input.configure(compile_context, input, &_input_permuted, get_permutation_vector_from_softmax_axis(actual_axis));
67 tmp_output = &_output_permuted;
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010068 }
69
SiCong Li96209c72020-08-21 12:28:30 +010070 // Create intermediate tensors
71 DataType tmp_data_type = is_data_type_quantized_asymmetric(tmp_input->info()->data_type()) ? DataType::S32 : tmp_input->info()->data_type();
72 TensorInfo tensor_info_tmp(tmp_input->info()->clone()->set_data_type(tmp_data_type));
Chunosovf450caa2017-11-08 16:09:35 +070073 _tmp.allocator()->init(tensor_info_tmp);
SiCong Li96209c72020-08-21 12:28:30 +010074 TensorShape max_sum_shape = tmp_input->info()->tensor_shape();
Chunosovf450caa2017-11-08 16:09:35 +070075 max_sum_shape.set(0, 1);
SiCong Li96209c72020-08-21 12:28:30 +010076 _max.allocator()->init(tmp_input->info()->clone()->set_tensor_shape(max_sum_shape));
77 _sum.allocator()->init(tmp_input->info()->clone()->set_tensor_shape(max_sum_shape).set_data_type(tmp_data_type));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078
Chunosovd6afedc2017-11-06 22:09:45 +070079 // Set GPU target to kernels
80 _max_shift_exp_sum_kernel.set_target(CLScheduler::get().target());
81
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010082 // Manage intermediate buffers
83 _memory_group.manage(&_tmp);
84 _memory_group.manage(&_max);
85 _memory_group.manage(&_sum);
86
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000087 SoftmaxKernelInfo softmax_info;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000088 softmax_info.beta = beta;
89 softmax_info.is_log = IS_LOG;
SiCong Li96209c72020-08-21 12:28:30 +010090 softmax_info.input_data_type = tmp_input->info()->data_type();
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000091
Chunosovd6afedc2017-11-06 22:09:45 +070092 // Configure kernels
SiCong Li96209c72020-08-21 12:28:30 +010093 _max_shift_exp_sum_kernel.configure(compile_context, tmp_input, &_max, &_tmp, &_sum, softmax_info);
94 _norm_kernel.configure(compile_context, &_tmp, &_sum, tmp_output, softmax_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
96 // Allocate intermediate buffers
97 _tmp.allocator()->allocate();
98 _max.allocator()->allocate();
99 _sum.allocator()->allocate();
SiCong Li96209c72020-08-21 12:28:30 +0100100 if(_needs_permute)
101 {
102 _permute_output.configure(compile_context, &_output_permuted, output, get_permutation_vector_from_softmax_axis(actual_axis));
103 _input_permuted.allocator()->allocate();
104 _output_permuted.allocator()->allocate();
105 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106}
107
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000108template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +0100109Status CLSoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000110{
Georgios Pinitasee8be2d2017-11-22 12:53:45 +0000111 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100112 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported");
giuros01efbf6c82018-09-03 09:53:53 +0100113 ARM_COMPUTE_UNUSED(beta);
SiCong Li96209c72020-08-21 12:28:30 +0100114 ARM_COMPUTE_RETURN_ERROR_ON(axis < static_cast<int32_t>(-input->num_dimensions()) || static_cast<int32_t>(input->num_dimensions()) <= axis);
SiCong Lid004a7a2020-05-28 15:26:41 +0100115
SiCong Li96209c72020-08-21 12:28:30 +0100116 const size_t actual_axis = static_cast<size_t>(wrap_around(axis, static_cast<int32_t>(input->num_dimensions())));
117 const bool needs_permute = actual_axis != 0;
118 if(needs_permute)
119 {
120 const PermutationVector permutation_vector = get_permutation_vector_from_softmax_axis(actual_axis);
121 const TensorShape permuted_shape = misc::shape_calculator::compute_permutation_output_shape(*input, permutation_vector);
122 TensorInfo input_permuted(input->clone()->set_tensor_shape(permuted_shape));
123 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(input, &input_permuted, permutation_vector));
124 TensorInfo output_permuted(output->clone()->set_tensor_shape(permuted_shape));
125 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(&output_permuted, output, permutation_vector));
126 }
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000127
128 // Create intermediate tensor info
129 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 +0100130 TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000131
132 TensorShape max_sum_shape = input->tensor_shape();
133 max_sum_shape.set(0, 1);
Michele Di Giorgio5cb37732018-06-08 18:07:08 +0100134 TensorInfo tensor_info_max(input->clone()->set_tensor_shape(max_sum_shape).set_is_resizable(true));
135 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 +0000136
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000137 SoftmaxKernelInfo softmax_info;
138 softmax_info.beta = beta;
139 softmax_info.is_log = IS_LOG;
140 softmax_info.input_data_type = input->data_type();
141
Giorgio Arena4402cb92018-02-15 13:37:40 +0000142 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 +0000143 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DNormKernel::validate(&tensor_info_tmp, &tensor_info_sum, output, softmax_info));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000144
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000145 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000146}
147
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000148template <bool IS_LOG>
Manuel Bottini2b84be52020-04-08 10:15:51 +0100149void CLSoftmaxLayerGeneric<IS_LOG>::run()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100151 MemoryGroupResourceScope scope_mg(_memory_group);
giuros01efbf6c82018-09-03 09:53:53 +0100152
SiCong Li96209c72020-08-21 12:28:30 +0100153 if(_needs_permute)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100154 {
SiCong Li96209c72020-08-21 12:28:30 +0100155 _permute_input.run();
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100156 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100157
Giorgio Arena4402cb92018-02-15 13:37:40 +0000158 CLScheduler::get().enqueue(_max_shift_exp_sum_kernel, false);
SiCong Li96209c72020-08-21 12:28:30 +0100159 CLScheduler::get().enqueue(_norm_kernel, !_needs_permute);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100160
SiCong Li96209c72020-08-21 12:28:30 +0100161 if(_needs_permute)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100162 {
SiCong Li96209c72020-08-21 12:28:30 +0100163 _permute_output.run();
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100164 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165}
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100166
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000167template class CLSoftmaxLayerGeneric<false>;
168template class CLSoftmaxLayerGeneric<true>;
169
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100170} // namespace arm_compute