blob: 93e63dd7796b4f1ce5b896e8414facd5618f855d [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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Helpers.h"
Chunosovd6afedc2017-11-06 22:09:45 +070028#include "arm_compute/core/Types.h"
29#include "arm_compute/core/Utils.h"
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010030#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/runtime/CL/CLScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010032#include "src/core/CL/ICLKernel.h"
33#include "src/core/CL/kernels/CLFillBorderKernel.h"
34#include "src/core/CL/kernels/CLSoftmaxLayerKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/helpers/SoftmaxHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010037namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038{
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000039template <bool IS_LOG>
40CLSoftmaxLayerGeneric<IS_LOG>::CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010041 : _memory_group(std::move(memory_manager)),
42 _permute_input(),
43 _permute_output(),
Georgios Pinitas40f51a62020-11-21 03:04:18 +000044 _max_shift_exp_sum_kernel(std::make_unique<CLLogits1DMaxShiftExpSumKernel>()),
45 _norm_kernel(std::make_unique<CLLogits1DNormKernel>()),
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010046 _max(),
47 _sum(),
48 _tmp(),
49 _input_permuted(),
50 _output_permuted(),
SiCong Li96209c72020-08-21 12:28:30 +010051 _needs_permute()
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010052{
53}
54
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000055template <bool IS_LOG>
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010056CLSoftmaxLayerGeneric<IS_LOG>::~CLSoftmaxLayerGeneric() = default;
57
58template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010059void CLSoftmaxLayerGeneric<IS_LOG>::configure(const ICLTensor *input, ICLTensor *output, float beta, int32_t axis)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060{
morgolock9c7fed82020-08-05 12:30:56 +010061 configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis);
Manuel Bottini2b84be52020-04-08 10:15:51 +010062}
63
64template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010065void 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 +010066{
Georgios Pinitasee8be2d2017-11-22 12:53:45 +000067 // Perform validation step
68 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
morgolock9c7fed82020-08-05 12:30:56 +010069 ARM_COMPUTE_ERROR_THROW_ON(CLSoftmaxLayerGeneric<IS_LOG>::validate(input->info(), output->info(), beta, axis));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070
SiCong Li96209c72020-08-21 12:28:30 +010071 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 +010072
SiCong Li96209c72020-08-21 12:28:30 +010073 _needs_permute = actual_axis != 0;
74 ICLTensor *tmp_output = output;
75 const ICLTensor *tmp_input = _needs_permute ? &_input_permuted : input;
76 if(_needs_permute)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010077 {
SiCong Li96209c72020-08-21 12:28:30 +010078 _memory_group.manage(&_input_permuted);
79 _memory_group.manage(&_output_permuted);
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010080 _permute_input.configure(compile_context, input, &_input_permuted, softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis));
SiCong Li96209c72020-08-21 12:28:30 +010081 tmp_output = &_output_permuted;
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010082 }
83
SiCong Li96209c72020-08-21 12:28:30 +010084 // Create intermediate tensors
85 DataType tmp_data_type = is_data_type_quantized_asymmetric(tmp_input->info()->data_type()) ? DataType::S32 : tmp_input->info()->data_type();
86 TensorInfo tensor_info_tmp(tmp_input->info()->clone()->set_data_type(tmp_data_type));
Chunosovf450caa2017-11-08 16:09:35 +070087 _tmp.allocator()->init(tensor_info_tmp);
SiCong Li96209c72020-08-21 12:28:30 +010088 TensorShape max_sum_shape = tmp_input->info()->tensor_shape();
Chunosovf450caa2017-11-08 16:09:35 +070089 max_sum_shape.set(0, 1);
SiCong Li96209c72020-08-21 12:28:30 +010090 _max.allocator()->init(tmp_input->info()->clone()->set_tensor_shape(max_sum_shape));
91 _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 +010092
Chunosovd6afedc2017-11-06 22:09:45 +070093 // Set GPU target to kernels
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010094 _max_shift_exp_sum_kernel->set_target(CLScheduler::get().target());
Chunosovd6afedc2017-11-06 22:09:45 +070095
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010096 // Manage intermediate buffers
97 _memory_group.manage(&_tmp);
98 _memory_group.manage(&_max);
99 _memory_group.manage(&_sum);
100
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000101 SoftmaxKernelInfo softmax_info;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000102 softmax_info.beta = beta;
103 softmax_info.is_log = IS_LOG;
SiCong Li96209c72020-08-21 12:28:30 +0100104 softmax_info.input_data_type = tmp_input->info()->data_type();
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000105
Chunosovd6afedc2017-11-06 22:09:45 +0700106 // Configure kernels
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100107 _max_shift_exp_sum_kernel->configure(compile_context, tmp_input, &_max, &_tmp, &_sum, softmax_info);
108 _norm_kernel->configure(compile_context, &_tmp, &_sum, tmp_output, softmax_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109
110 // Allocate intermediate buffers
111 _tmp.allocator()->allocate();
112 _max.allocator()->allocate();
113 _sum.allocator()->allocate();
SiCong Li96209c72020-08-21 12:28:30 +0100114 if(_needs_permute)
115 {
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100116 _permute_output.configure(compile_context, &_output_permuted, output, softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis));
SiCong Li96209c72020-08-21 12:28:30 +0100117 _input_permuted.allocator()->allocate();
118 _output_permuted.allocator()->allocate();
119 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120}
121
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000122template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +0100123Status CLSoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000124{
Georgios Pinitasee8be2d2017-11-22 12:53:45 +0000125 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100126 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported");
giuros01efbf6c82018-09-03 09:53:53 +0100127 ARM_COMPUTE_UNUSED(beta);
SiCong Li96209c72020-08-21 12:28:30 +0100128 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 +0100129
SiCong Li96209c72020-08-21 12:28:30 +0100130 const size_t actual_axis = static_cast<size_t>(wrap_around(axis, static_cast<int32_t>(input->num_dimensions())));
131 const bool needs_permute = actual_axis != 0;
132 if(needs_permute)
133 {
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100134 const PermutationVector permutation_vector = softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis);
SiCong Li96209c72020-08-21 12:28:30 +0100135 const TensorShape permuted_shape = misc::shape_calculator::compute_permutation_output_shape(*input, permutation_vector);
136 TensorInfo input_permuted(input->clone()->set_tensor_shape(permuted_shape));
137 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(input, &input_permuted, permutation_vector));
138 TensorInfo output_permuted(output->clone()->set_tensor_shape(permuted_shape));
139 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(&output_permuted, output, permutation_vector));
140 }
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000141
142 // Create intermediate tensor info
143 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 +0100144 TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000145
146 TensorShape max_sum_shape = input->tensor_shape();
147 max_sum_shape.set(0, 1);
Michele Di Giorgio5cb37732018-06-08 18:07:08 +0100148 TensorInfo tensor_info_max(input->clone()->set_tensor_shape(max_sum_shape).set_is_resizable(true));
149 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 +0000150
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000151 SoftmaxKernelInfo softmax_info;
152 softmax_info.beta = beta;
153 softmax_info.is_log = IS_LOG;
154 softmax_info.input_data_type = input->data_type();
155
Giorgio Arena4402cb92018-02-15 13:37:40 +0000156 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 +0000157 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DNormKernel::validate(&tensor_info_tmp, &tensor_info_sum, output, softmax_info));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000158
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000159 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000160}
161
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000162template <bool IS_LOG>
Manuel Bottini2b84be52020-04-08 10:15:51 +0100163void CLSoftmaxLayerGeneric<IS_LOG>::run()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100165 MemoryGroupResourceScope scope_mg(_memory_group);
giuros01efbf6c82018-09-03 09:53:53 +0100166
SiCong Li96209c72020-08-21 12:28:30 +0100167 if(_needs_permute)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100168 {
SiCong Li96209c72020-08-21 12:28:30 +0100169 _permute_input.run();
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100170 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100171
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100172 CLScheduler::get().enqueue(*_max_shift_exp_sum_kernel, false);
173 CLScheduler::get().enqueue(*_norm_kernel, !_needs_permute);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100174
SiCong Li96209c72020-08-21 12:28:30 +0100175 if(_needs_permute)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100176 {
SiCong Li96209c72020-08-21 12:28:30 +0100177 _permute_output.run();
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100178 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179}
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100180
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000181template class CLSoftmaxLayerGeneric<false>;
182template class CLSoftmaxLayerGeneric<true>;
183
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100184} // namespace arm_compute