Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 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 Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 31 | namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | { |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 33 | template <bool IS_LOG> |
| 34 | NESoftmaxLayerGeneric<IS_LOG>::NESoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager) |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 35 | : _memory_group(std::move(memory_manager)), _max_kernel(), _softmax_kernel(), _flat_or_reshape_ptr(nullptr), _fill_border_kernel(), _reshape(), _max(), _tmp(), _input_flattened(), _output_flattened(), |
| 36 | _needs_flattening(false) |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 40 | template <bool IS_LOG> |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 41 | void NESoftmaxLayerGeneric<IS_LOG>::configure_reshape_input_kernel(const ITensor *input, const ITensor *output, int32_t first_n_reduce_axes) |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 42 | { |
| 43 | // Flatten the input |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 44 | const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input->info(), first_n_reduce_axes); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 45 | |
| 46 | // Initialize the flat input |
| 47 | _input_flattened.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_flatten)); |
| 48 | |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 49 | // Note that the "other cases" include both: |
| 50 | // 1. first_n_reduce_axes < 3: Reduce the first 1 (no need to reduce) or 2 dimensions (inclusive) |
| 51 | // 2. first_n_reduce_axes == 4: Reduce all 4 dimensions. This can only be handled by NEReshapeKernel instead of NEFlattenKernel. |
| 52 | if(first_n_reduce_axes == 3) |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 53 | { |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 54 | auto flatten_kernel_ptr = support::cpp14::make_unique<NEFlattenLayer>(); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 55 | flatten_kernel_ptr->configure(input, &_input_flattened); |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 56 | _flat_or_reshape_ptr = std::move(flatten_kernel_ptr); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 57 | } |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 58 | else |
| 59 | { |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 60 | auto reshape_kernel_ptr = support::cpp14::make_unique<NEReshapeLayer>(); |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 61 | reshape_kernel_ptr->configure(input, &_input_flattened); |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 62 | _flat_or_reshape_ptr = std::move(reshape_kernel_ptr); |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 63 | } |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 64 | |
| 65 | // We need to init the output tensor here. Indeed, the reshape kernel expects |
| 66 | // both tensors to be already initialized |
| 67 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 70 | template <bool IS_LOG> |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 71 | void NESoftmaxLayerGeneric<IS_LOG>::configure(ITensor *input, ITensor *output, float beta, int32_t axis) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | { |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 73 | // Perform validation step |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 74 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 75 | ARM_COMPUTE_ERROR_THROW_ON(NESoftmaxLayerGeneric::validate(input->info(), output->info(), beta, axis)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 77 | // Convert reduce-before axis (inclusive) to first n axes to reduce |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 78 | size_t first_n_reduce_axes = dim_index_2_num_dims(axis, static_cast<int32_t>(input->info()->num_dimensions())); |
Sheri Zhang | 1f567af | 2020-05-05 11:47:36 +0100 | [diff] [blame] | 79 | |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 80 | // We only need flattening when the number of axes to reduce is greater than 1 |
| 81 | _needs_flattening = first_n_reduce_axes > 1; |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 82 | |
| 83 | // If we are dealing with a 4D tensor, we will: |
| 84 | // - Flatten the input, so that we end up with a [width*height*depth] * batches 2D tensor |
| 85 | // - Execute all the pipeline (reduction + normalization) on the flattened tensor |
| 86 | // - Reshape the flattened output into the real output |
| 87 | if(_needs_flattening) |
| 88 | { |
| 89 | // Add to the memory manager _input_flattened |
| 90 | _memory_group.manage(&_input_flattened); |
| 91 | |
| 92 | // Configure _flatten_kernel and _input_flattened |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 93 | configure_reshape_input_kernel(input, output, first_n_reduce_axes); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // We want to deal with a 2D input. Either it is the flattened version of the original input (4D case) |
| 97 | // or it is the original input case (2D case) |
| 98 | ITensor *input_2D = (_needs_flattening ? &_input_flattened : input); |
| 99 | |
| 100 | // Create intermediate tensors shapes |
| 101 | const TensorInfo input_info = input_2D->info()->clone()->reset_padding().set_is_resizable(true); |
| 102 | DataType tmp_data_type = is_data_type_quantized_asymmetric(input_2D->info()->data_type()) ? DataType::F32 : input_2D->info()->data_type(); |
| 103 | TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type)); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 104 | |
| 105 | // Init intermediate tensors |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 106 | TensorShape max_sum_shape = input_2D->info()->tensor_shape(); |
| 107 | max_sum_shape.set(0, 1); |
| 108 | _max.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape)); |
| 109 | _tmp.allocator()->init(tensor_info_tmp); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 110 | |
| 111 | // Manage intermediate buffers |
| 112 | _memory_group.manage(&_max); |
| 113 | _memory_group.manage(&_tmp); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 114 | |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 115 | // Configure Kernels |
| 116 | _max_kernel.configure(input_2D, &_max); |
| 117 | if(_needs_flattening) |
| 118 | { |
| 119 | // Add to the memory manager _output_flattened |
| 120 | _memory_group.manage(&_output_flattened); |
| 121 | |
| 122 | // The normalization kernel stores the result in a flat output tensor |
| 123 | _softmax_kernel.configure(input_2D, &_max, &_output_flattened, beta, &_tmp); |
| 124 | _input_flattened.allocator()->allocate(); |
| 125 | |
| 126 | // Reshape the flat output into the requested (4D) output |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 127 | _reshape.configure(&_output_flattened, output); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 128 | |
| 129 | // Allocate the intermediate flat tensors |
| 130 | _output_flattened.allocator()->allocate(); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | // Softmax 2D case |
| 135 | _fill_border_kernel.configure(input_2D, _max_kernel.border_size(), BorderMode::REPLICATE); |
| 136 | _softmax_kernel.configure(input_2D, &_max, output, beta, &_tmp); |
| 137 | } |
| 138 | |
| 139 | // Allocate intermediate buffers |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 140 | _max.allocator()->allocate(); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 141 | _tmp.allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 144 | template <bool IS_LOG> |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 145 | Status NESoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 146 | { |
| 147 | // Perform validation step |
| 148 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 149 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported"); |
| 150 | ARM_COMPUTE_UNUSED(beta); |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 151 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis != 0, "Only axis 0 supported"); |
| 152 | ARM_COMPUTE_RETURN_ERROR_ON(axis < static_cast<int32_t>(-input->num_dimensions()) || static_cast<int32_t>(input->num_dimensions()) <= axis); |
Sheri Zhang | 1f567af | 2020-05-05 11:47:36 +0100 | [diff] [blame] | 153 | |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 154 | // Convert reduce-before axis (inclusive) to first n axes to reduce |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 155 | size_t first_n_reduce_axes = dim_index_2_num_dims(axis, static_cast<int32_t>(input->num_dimensions())); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 156 | |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 157 | // Create intermediate tensor info |
| 158 | DataType tmp_data_type = input->data_type(); |
| 159 | const TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type).set_is_resizable(true)); |
| 160 | |
| 161 | TensorShape max_sum_shape = input->tensor_shape(); |
| 162 | max_sum_shape.set(0, 1); |
| 163 | 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)); |
| 164 | const TensorInfo dont_care; |
| 165 | |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 166 | const bool needs_flattening = (first_n_reduce_axes > 1); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 167 | |
| 168 | if(needs_flattening) |
| 169 | { |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 170 | const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input, first_n_reduce_axes); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 171 | TensorInfo tensor_info_flat(input->clone()->set_tensor_shape(shape_flatten).set_is_resizable(true)); |
| 172 | |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 173 | if(first_n_reduce_axes == 3) |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 174 | { |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 175 | ARM_COMPUTE_RETURN_ON_ERROR(NEFlattenLayer::validate(input, &tensor_info_flat)); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 176 | } |
| 177 | else |
| 178 | { |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 179 | ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayer::validate(input, &tensor_info_flat)); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 182 | |
| 183 | ARM_COMPUTE_RETURN_ON_ERROR(NELogits1DMaxKernel::validate(input, &tensor_info_max_sum)); |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 184 | ARM_COMPUTE_RETURN_ON_ERROR(NELogits1DSoftmaxKernel<IS_LOG>::validate(&tensor_info_tmp, &tensor_info_max_sum, output, beta, &dont_care)); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 185 | |
| 186 | return Status{}; |
| 187 | } |
| 188 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 189 | template <bool IS_LOG> |
| 190 | void NESoftmaxLayerGeneric<IS_LOG>::run() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 191 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 192 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 193 | |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 194 | if(_needs_flattening) |
| 195 | { |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 196 | _flat_or_reshape_ptr->run(); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 199 | NEScheduler::get().schedule(&_fill_border_kernel, Window::DimY); |
| 200 | NEScheduler::get().schedule(&_max_kernel, Window::DimY); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 201 | NEScheduler::get().schedule(&_softmax_kernel, Window::DimY); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 202 | |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 203 | if(_needs_flattening) |
| 204 | { |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 205 | _reshape.run(); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 206 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 207 | } |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 208 | |
| 209 | template class NESoftmaxLayerGeneric<false>; |
| 210 | template class NESoftmaxLayerGeneric<true>; |
| 211 | |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 212 | } // namespace arm_compute |