blob: e763caa3a3d226ae309853657b34a7b15508bb74 [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/NEON/functions/NESoftmaxLayer.h"
25
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/NEON/kernels/NESoftmaxLayerKernel.h"
Manuel Bottini678d83a2019-01-07 16:05:36 +000028#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
Manuel Bottini678d83a2019-01-07 16:05:36 +000031namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032{
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010033template <bool IS_LOG>
34NESoftmaxLayerGeneric<IS_LOG>::NESoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
SiCong Li96209c72020-08-21 12:28:30 +010035 : _memory_group(std::move(memory_manager)), _permute_input(), _permute_output(), _max_kernel(), _softmax_kernel(), _fill_border_kernel(), _max(), _tmp(), _input_permuted(), _output_permuted(),
36 _needs_permute(false)
Manuel Bottini678d83a2019-01-07 16:05:36 +000037{
38}
39
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010040template <bool IS_LOG>
morgolock9c7fed82020-08-05 12:30:56 +010041void NESoftmaxLayerGeneric<IS_LOG>::configure(ITensor *input, ITensor *output, float beta, int32_t axis)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042{
Manuel Bottini678d83a2019-01-07 16:05:36 +000043 // Perform validation step
Michalis Spyrouafa5d812017-11-30 14:25:57 +000044 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
morgolock9c7fed82020-08-05 12:30:56 +010045 ARM_COMPUTE_ERROR_THROW_ON(NESoftmaxLayerGeneric::validate(input->info(), output->info(), beta, axis));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046
SiCong Li96209c72020-08-21 12:28:30 +010047 const unsigned int actual_axis = static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(input->info()->num_dimensions())));
Sheri Zhang1f567af2020-05-05 11:47:36 +010048
SiCong Li96209c72020-08-21 12:28:30 +010049 _needs_permute = actual_axis > 0;
Manuel Bottini678d83a2019-01-07 16:05:36 +000050
SiCong Li96209c72020-08-21 12:28:30 +010051 if(_needs_permute)
Manuel Bottini678d83a2019-01-07 16:05:36 +000052 {
SiCong Li96209c72020-08-21 12:28:30 +010053 // Add to the memory manager _input_permuted
54 _memory_group.manage(&_input_permuted);
Manuel Bottini678d83a2019-01-07 16:05:36 +000055
SiCong Li96209c72020-08-21 12:28:30 +010056 _permute_input.configure(input, &_input_permuted, get_permutation_vector_from_softmax_axis(actual_axis));
Manuel Bottini678d83a2019-01-07 16:05:36 +000057 }
58
SiCong Li96209c72020-08-21 12:28:30 +010059 // We want to deal with a 2D input. Either it is the permuted version of the original input (4D case)
Manuel Bottini678d83a2019-01-07 16:05:36 +000060 // or it is the original input case (2D case)
SiCong Li96209c72020-08-21 12:28:30 +010061 ITensor *tmp_input = (_needs_permute ? &_input_permuted : input);
Manuel Bottini678d83a2019-01-07 16:05:36 +000062
63 // Create intermediate tensors shapes
SiCong Li96209c72020-08-21 12:28:30 +010064 const TensorInfo input_info = tmp_input->info()->clone()->reset_padding().set_is_resizable(true);
65 DataType tmp_data_type = is_data_type_quantized_asymmetric(tmp_input->info()->data_type()) ? DataType::F32 : tmp_input->info()->data_type();
Manuel Bottini678d83a2019-01-07 16:05:36 +000066 TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type));
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000067
68 // Init intermediate tensors
SiCong Li96209c72020-08-21 12:28:30 +010069 TensorShape max_sum_shape = tmp_input->info()->tensor_shape();
Manuel Bottini678d83a2019-01-07 16:05:36 +000070 max_sum_shape.set(0, 1);
71 _max.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape));
72 _tmp.allocator()->init(tensor_info_tmp);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000073
74 // Manage intermediate buffers
75 _memory_group.manage(&_max);
76 _memory_group.manage(&_tmp);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077
Manuel Bottini678d83a2019-01-07 16:05:36 +000078 // Configure Kernels
SiCong Li96209c72020-08-21 12:28:30 +010079 _max_kernel.configure(tmp_input, &_max);
80 if(_needs_permute)
Manuel Bottini678d83a2019-01-07 16:05:36 +000081 {
SiCong Li96209c72020-08-21 12:28:30 +010082 // Add to the memory manager _output_permuted
83 _memory_group.manage(&_output_permuted);
Manuel Bottini678d83a2019-01-07 16:05:36 +000084
SiCong Li96209c72020-08-21 12:28:30 +010085 // The normalization kernel stores the result in a permuted output tensor
86 _softmax_kernel.configure(tmp_input, &_max, &_output_permuted, beta, &_tmp);
87 _input_permuted.allocator()->allocate();
Manuel Bottini678d83a2019-01-07 16:05:36 +000088
SiCong Li96209c72020-08-21 12:28:30 +010089 // Re-permute the permuted output into the requested (4D) output
90 _permute_output.configure(&_output_permuted, output, get_permutation_vector_from_softmax_axis(actual_axis));
Manuel Bottini678d83a2019-01-07 16:05:36 +000091
SiCong Li96209c72020-08-21 12:28:30 +010092 // Allocate the intermediate permuted tensors
93 _output_permuted.allocator()->allocate();
Manuel Bottini678d83a2019-01-07 16:05:36 +000094 }
95 else
96 {
97 // Softmax 2D case
SiCong Li96209c72020-08-21 12:28:30 +010098 _fill_border_kernel.configure(tmp_input, _max_kernel.border_size(), BorderMode::REPLICATE);
99 _softmax_kernel.configure(tmp_input, &_max, output, beta, &_tmp);
Manuel Bottini678d83a2019-01-07 16:05:36 +0000100 }
101
102 // Allocate intermediate buffers
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 _max.allocator()->allocate();
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000104 _tmp.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105}
106
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100107template <bool IS_LOG>
morgolock9c7fed82020-08-05 12:30:56 +0100108Status NESoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000109{
110 // Perform validation step
111 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Manuel Bottini678d83a2019-01-07 16:05:36 +0000112 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported");
113 ARM_COMPUTE_UNUSED(beta);
morgolock9c7fed82020-08-05 12:30:56 +0100114 ARM_COMPUTE_RETURN_ERROR_ON(axis < static_cast<int32_t>(-input->num_dimensions()) || static_cast<int32_t>(input->num_dimensions()) <= axis);
Sheri Zhang1f567af2020-05-05 11:47:36 +0100115
Manuel Bottini678d83a2019-01-07 16:05:36 +0000116 // Create intermediate tensor info
117 DataType tmp_data_type = input->data_type();
118 const TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type).set_is_resizable(true));
119
120 TensorShape max_sum_shape = input->tensor_shape();
121 max_sum_shape.set(0, 1);
122 const TensorInfo tensor_info_max_sum(input->clone()->set_tensor_shape(max_sum_shape).set_data_type(tmp_data_type).set_quantization_info(input->quantization_info()).set_is_resizable(true));
123 const TensorInfo dont_care;
124
SiCong Li96209c72020-08-21 12:28:30 +0100125 const unsigned int actual_axis = static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(input->num_dimensions())));
Manuel Bottini678d83a2019-01-07 16:05:36 +0000126
SiCong Li96209c72020-08-21 12:28:30 +0100127 const bool needs_permute = actual_axis > 0;
128
129 if(needs_permute)
Manuel Bottini678d83a2019-01-07 16:05:36 +0000130 {
SiCong Li96209c72020-08-21 12:28:30 +0100131 const PermutationVector permutation_vector = get_permutation_vector_from_softmax_axis(actual_axis);
132 const TensorShape permuted_shape = misc::shape_calculator::compute_permutation_output_shape(*input, permutation_vector);
133 TensorInfo input_permuted(input->clone()->set_tensor_shape(permuted_shape));
134 ARM_COMPUTE_RETURN_ON_ERROR(NEPermute::validate(input, &input_permuted, permutation_vector));
135 TensorInfo output_permuted(output->clone()->set_tensor_shape(permuted_shape));
136 ARM_COMPUTE_RETURN_ON_ERROR(NEPermute::validate(&output_permuted, output, permutation_vector));
Manuel Bottini678d83a2019-01-07 16:05:36 +0000137 }
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000138
139 ARM_COMPUTE_RETURN_ON_ERROR(NELogits1DMaxKernel::validate(input, &tensor_info_max_sum));
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100140 ARM_COMPUTE_RETURN_ON_ERROR(NELogits1DSoftmaxKernel<IS_LOG>::validate(&tensor_info_tmp, &tensor_info_max_sum, output, beta, &dont_care));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000141
142 return Status{};
143}
144
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100145template <bool IS_LOG>
146void NESoftmaxLayerGeneric<IS_LOG>::run()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100148 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100149
SiCong Li96209c72020-08-21 12:28:30 +0100150 if(_needs_permute)
Manuel Bottini678d83a2019-01-07 16:05:36 +0000151 {
SiCong Li96209c72020-08-21 12:28:30 +0100152 _permute_input.run();
Manuel Bottini678d83a2019-01-07 16:05:36 +0000153 }
154
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 NEScheduler::get().schedule(&_fill_border_kernel, Window::DimY);
156 NEScheduler::get().schedule(&_max_kernel, Window::DimY);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000157 NEScheduler::get().schedule(&_softmax_kernel, Window::DimY);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100158
SiCong Li96209c72020-08-21 12:28:30 +0100159 if(_needs_permute)
Manuel Bottini678d83a2019-01-07 16:05:36 +0000160 {
SiCong Li96209c72020-08-21 12:28:30 +0100161 _permute_output.run();
Manuel Bottini678d83a2019-01-07 16:05:36 +0000162 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163}
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100164
165template class NESoftmaxLayerGeneric<false>;
166template class NESoftmaxLayerGeneric<true>;
167
Michalis Spyroubcd23522020-05-21 15:02:36 +0100168} // namespace arm_compute