blob: 4caf91488e95cdb74be26c6141e6066c2d62b511 [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"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010036#include "support/MemorySupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010038namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039{
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000040template <bool IS_LOG>
41CLSoftmaxLayerGeneric<IS_LOG>::CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010042 : _memory_group(std::move(memory_manager)),
43 _permute_input(),
44 _permute_output(),
45 _max_shift_exp_sum_kernel(support::cpp14::make_unique<CLLogits1DMaxShiftExpSumKernel>()),
46 _norm_kernel(support::cpp14::make_unique<CLLogits1DNormKernel>()),
47 _max(),
48 _sum(),
49 _tmp(),
50 _input_permuted(),
51 _output_permuted(),
SiCong Li96209c72020-08-21 12:28:30 +010052 _needs_permute()
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010053{
54}
55
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000056template <bool IS_LOG>
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010057CLSoftmaxLayerGeneric<IS_LOG>::~CLSoftmaxLayerGeneric() = default;
58
59template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010060void CLSoftmaxLayerGeneric<IS_LOG>::configure(const ICLTensor *input, ICLTensor *output, float beta, int32_t axis)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061{
morgolock9c7fed82020-08-05 12:30:56 +010062 configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis);
Manuel Bottini2b84be52020-04-08 10:15:51 +010063}
64
65template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +010066void 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 +010067{
Georgios Pinitasee8be2d2017-11-22 12:53:45 +000068 // Perform validation step
69 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
morgolock9c7fed82020-08-05 12:30:56 +010070 ARM_COMPUTE_ERROR_THROW_ON(CLSoftmaxLayerGeneric<IS_LOG>::validate(input->info(), output->info(), beta, axis));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071
SiCong Li96209c72020-08-21 12:28:30 +010072 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 +010073
SiCong Li96209c72020-08-21 12:28:30 +010074 _needs_permute = actual_axis != 0;
75 ICLTensor *tmp_output = output;
76 const ICLTensor *tmp_input = _needs_permute ? &_input_permuted : input;
77 if(_needs_permute)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010078 {
SiCong Li96209c72020-08-21 12:28:30 +010079 _memory_group.manage(&_input_permuted);
80 _memory_group.manage(&_output_permuted);
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010081 _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 +010082 tmp_output = &_output_permuted;
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010083 }
84
SiCong Li96209c72020-08-21 12:28:30 +010085 // Create intermediate tensors
86 DataType tmp_data_type = is_data_type_quantized_asymmetric(tmp_input->info()->data_type()) ? DataType::S32 : tmp_input->info()->data_type();
87 TensorInfo tensor_info_tmp(tmp_input->info()->clone()->set_data_type(tmp_data_type));
Chunosovf450caa2017-11-08 16:09:35 +070088 _tmp.allocator()->init(tensor_info_tmp);
SiCong Li96209c72020-08-21 12:28:30 +010089 TensorShape max_sum_shape = tmp_input->info()->tensor_shape();
Chunosovf450caa2017-11-08 16:09:35 +070090 max_sum_shape.set(0, 1);
SiCong Li96209c72020-08-21 12:28:30 +010091 _max.allocator()->init(tmp_input->info()->clone()->set_tensor_shape(max_sum_shape));
92 _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 +010093
Chunosovd6afedc2017-11-06 22:09:45 +070094 // Set GPU target to kernels
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010095 _max_shift_exp_sum_kernel->set_target(CLScheduler::get().target());
Chunosovd6afedc2017-11-06 22:09:45 +070096
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010097 // Manage intermediate buffers
98 _memory_group.manage(&_tmp);
99 _memory_group.manage(&_max);
100 _memory_group.manage(&_sum);
101
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000102 SoftmaxKernelInfo softmax_info;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000103 softmax_info.beta = beta;
104 softmax_info.is_log = IS_LOG;
SiCong Li96209c72020-08-21 12:28:30 +0100105 softmax_info.input_data_type = tmp_input->info()->data_type();
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000106
Chunosovd6afedc2017-11-06 22:09:45 +0700107 // Configure kernels
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100108 _max_shift_exp_sum_kernel->configure(compile_context, tmp_input, &_max, &_tmp, &_sum, softmax_info);
109 _norm_kernel->configure(compile_context, &_tmp, &_sum, tmp_output, softmax_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110
111 // Allocate intermediate buffers
112 _tmp.allocator()->allocate();
113 _max.allocator()->allocate();
114 _sum.allocator()->allocate();
SiCong Li96209c72020-08-21 12:28:30 +0100115 if(_needs_permute)
116 {
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100117 _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 +0100118 _input_permuted.allocator()->allocate();
119 _output_permuted.allocator()->allocate();
120 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121}
122
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000123template <bool IS_LOG>
SiCong Li96209c72020-08-21 12:28:30 +0100124Status CLSoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000125{
Georgios Pinitasee8be2d2017-11-22 12:53:45 +0000126 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100127 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported");
giuros01efbf6c82018-09-03 09:53:53 +0100128 ARM_COMPUTE_UNUSED(beta);
SiCong Li96209c72020-08-21 12:28:30 +0100129 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 +0100130
SiCong Li96209c72020-08-21 12:28:30 +0100131 const size_t actual_axis = static_cast<size_t>(wrap_around(axis, static_cast<int32_t>(input->num_dimensions())));
132 const bool needs_permute = actual_axis != 0;
133 if(needs_permute)
134 {
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100135 const PermutationVector permutation_vector = softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis);
SiCong Li96209c72020-08-21 12:28:30 +0100136 const TensorShape permuted_shape = misc::shape_calculator::compute_permutation_output_shape(*input, permutation_vector);
137 TensorInfo input_permuted(input->clone()->set_tensor_shape(permuted_shape));
138 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(input, &input_permuted, permutation_vector));
139 TensorInfo output_permuted(output->clone()->set_tensor_shape(permuted_shape));
140 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(&output_permuted, output, permutation_vector));
141 }
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000142
143 // Create intermediate tensor info
144 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 +0100145 TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000146
147 TensorShape max_sum_shape = input->tensor_shape();
148 max_sum_shape.set(0, 1);
Michele Di Giorgio5cb37732018-06-08 18:07:08 +0100149 TensorInfo tensor_info_max(input->clone()->set_tensor_shape(max_sum_shape).set_is_resizable(true));
150 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 +0000151
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000152 SoftmaxKernelInfo softmax_info;
153 softmax_info.beta = beta;
154 softmax_info.is_log = IS_LOG;
155 softmax_info.input_data_type = input->data_type();
156
Giorgio Arena4402cb92018-02-15 13:37:40 +0000157 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 +0000158 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DNormKernel::validate(&tensor_info_tmp, &tensor_info_sum, output, softmax_info));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000159
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000160 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000161}
162
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000163template <bool IS_LOG>
Manuel Bottini2b84be52020-04-08 10:15:51 +0100164void CLSoftmaxLayerGeneric<IS_LOG>::run()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100166 MemoryGroupResourceScope scope_mg(_memory_group);
giuros01efbf6c82018-09-03 09:53:53 +0100167
SiCong Li96209c72020-08-21 12:28:30 +0100168 if(_needs_permute)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100169 {
SiCong Li96209c72020-08-21 12:28:30 +0100170 _permute_input.run();
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100171 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100172
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100173 CLScheduler::get().enqueue(*_max_shift_exp_sum_kernel, false);
174 CLScheduler::get().enqueue(*_norm_kernel, !_needs_permute);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100175
SiCong Li96209c72020-08-21 12:28:30 +0100176 if(_needs_permute)
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100177 {
SiCong Li96209c72020-08-21 12:28:30 +0100178 _permute_output.run();
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100179 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180}
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100181
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000182template class CLSoftmaxLayerGeneric<false>;
183template class CLSoftmaxLayerGeneric<true>;
184
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100185} // namespace arm_compute