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/CL/functions/CLSoftmaxLayer.h" |
| 25 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/ICLKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Utils.h" |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 34 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 35 | namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | { |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 37 | template <bool IS_LOG> |
| 38 | CLSoftmaxLayerGeneric<IS_LOG>::CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager) |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 39 | : _memory_group(std::move(memory_manager)), _max_shift_exp_sum_kernel(), _norm_kernel(), _flatten_ptr(), _reshape(), _max(), _sum(), _tmp(), _input_flattened(), _output_flattened(), |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 40 | _needs_flattening(false) |
| 41 | { |
| 42 | } |
| 43 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 44 | template <bool IS_LOG> |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 45 | void CLSoftmaxLayerGeneric<IS_LOG>::configure_reshape_input_kernel(const ICLTensor *input, const ICLTensor *output, size_t first_n_reduce_axes) |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 46 | { |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 47 | configure_reshape_input_kernel(CLKernelLibrary::get().get_compile_context(), input, output, first_n_reduce_axes); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | template <bool IS_LOG> |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 51 | void CLSoftmaxLayerGeneric<IS_LOG>::configure_reshape_input_kernel(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *output, size_t first_n_reduce_axes) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 52 | { |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 53 | // Flatten the input |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 54 | const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input->info(), first_n_reduce_axes); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 55 | |
| 56 | // Initialize the flat input |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 57 | _input_flattened.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_flatten)); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 58 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 59 | // If we need to flatten the input, we can use CLFlattenKernel or CLReshapeKernel |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 60 | // If the number of reduced axes is 3 (max dimension), which means collapsing all axes except the batch axis, we use CLFlattenKernel. |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 61 | // In all other cases we have to use CLReshapeKernel |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 62 | // Note that the "other cases" include both: |
| 63 | // 1. first_n_reduce_axes < 3: Reduce the first 1 (no need to reduce) or 2 dimensions (inclusive) |
| 64 | // 2. first_n_reduce_axes == 4: Reduce all 4 dimensions. This can only be handled by CLReshapeKernel instead of CLFlattenKernel. |
| 65 | if(first_n_reduce_axes == 3) |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 66 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 67 | auto flatten = support::cpp14::make_unique<CLFlattenLayer>(); |
| 68 | flatten->configure(compile_context, input, &_input_flattened); |
| 69 | _flatten_ptr = std::move(flatten); |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 70 | } |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 71 | else |
| 72 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 73 | auto reshape_ptr = support::cpp14::make_unique<CLReshapeLayer>(); |
| 74 | reshape_ptr->configure(compile_context, input, &_input_flattened); |
| 75 | _flatten_ptr = std::move(reshape_ptr); |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 76 | } |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 77 | |
| 78 | // We need to init the output tensor here. Indeed, the reshape kernel expects |
| 79 | // both tensors to be already initialized |
| 80 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 83 | template <bool IS_LOG> |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 84 | void CLSoftmaxLayerGeneric<IS_LOG>::configure(const ICLTensor *input, ICLTensor *output, float beta, size_t axis) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | { |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 86 | configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | template <bool IS_LOG> |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 90 | void CLSoftmaxLayerGeneric<IS_LOG>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, float beta, size_t axis) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 91 | { |
Georgios Pinitas | ee8be2d | 2017-11-22 12:53:45 +0000 | [diff] [blame] | 92 | // Perform validation step |
| 93 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 94 | ARM_COMPUTE_ERROR_THROW_ON(CLSoftmaxLayerGeneric<IS_LOG>::validate(input->info(), output->info(), beta, axis)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 96 | // Convert reduce-before axis (inclusive) to first n axes to reduce |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 97 | size_t first_n_reduce_axes = dim_index_2_num_dims(axis, input->info()->num_dimensions()); |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 98 | |
| 99 | // We only need flattening when the number of axes to reduce is greater than 1 |
| 100 | _needs_flattening = first_n_reduce_axes > 1; |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 101 | |
| 102 | // If we are dealing with a 4D tensor, we will: |
| 103 | // - Flatten the input, so that we end up with a [width*height*depth] * batches 2D tensor |
| 104 | // - Execute all the pipeline (reduction + normalization) on the flattened tensor |
| 105 | // - Reshape the flattened output into the real output |
| 106 | if(_needs_flattening) |
| 107 | { |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 108 | // Add to the memory manager _input_flattened |
| 109 | _memory_group.manage(&_input_flattened); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 110 | |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 111 | // Cofigure _flatten_kernel and _input_flattened |
| 112 | configure_reshape_input_kernel(input, output, first_n_reduce_axes); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // We want to deal with a 2D input. Either it is the flattened version of the original input (4D case) |
| 116 | // or it is the original input case (2D case) |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 117 | const ICLTensor *input_2D = (_needs_flattening ? &_input_flattened : input); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 118 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | // Create intermediate tensors shapes |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 120 | TensorInfo input_info = input_2D->info()->clone()->reset_padding().set_is_resizable(true); |
| 121 | DataType tmp_data_type = is_data_type_quantized_asymmetric(input_2D->info()->data_type()) ? DataType::S32 : input_2D->info()->data_type(); |
| 122 | TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type)); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 123 | _tmp.allocator()->init(tensor_info_tmp); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 125 | TensorShape max_sum_shape = input_2D->info()->tensor_shape(); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 126 | max_sum_shape.set(0, 1); |
Georgios Pinitas | ee8be2d | 2017-11-22 12:53:45 +0000 | [diff] [blame] | 127 | _max.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape)); |
| 128 | _sum.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape).set_data_type(tmp_data_type)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 129 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 130 | // Set GPU target to kernels |
| 131 | _max_shift_exp_sum_kernel.set_target(CLScheduler::get().target()); |
| 132 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 133 | // Manage intermediate buffers |
| 134 | _memory_group.manage(&_tmp); |
| 135 | _memory_group.manage(&_max); |
| 136 | _memory_group.manage(&_sum); |
| 137 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 138 | SoftmaxKernelInfo softmax_info; |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 139 | softmax_info.beta = beta; |
| 140 | softmax_info.is_log = IS_LOG; |
| 141 | softmax_info.input_data_type = input_2D->info()->data_type(); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 142 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 143 | // Configure kernels |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 144 | _max_shift_exp_sum_kernel.configure(compile_context, input_2D, &_max, &_tmp, &_sum, softmax_info); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 145 | |
| 146 | if(_needs_flattening) |
| 147 | { |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 148 | // Add to the memory manager _output_flattened |
| 149 | _memory_group.manage(&_output_flattened); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 150 | |
| 151 | // The normalization kernel stores the result in a flat output tensor |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 152 | _norm_kernel.configure(compile_context, &_tmp, &_sum, &_output_flattened, softmax_info); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 153 | |
| 154 | // Reshape the flat output into a the requested (4D) output |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 155 | _reshape.configure(compile_context, &_output_flattened, output); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 156 | |
| 157 | // Allocate the intermediate flat tensors |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 158 | _input_flattened.allocator()->allocate(); |
| 159 | _output_flattened.allocator()->allocate(); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 160 | } |
| 161 | else |
| 162 | { |
| 163 | // Softmax 2D case |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 164 | _norm_kernel.configure(compile_context, &_tmp, &_sum, output, softmax_info); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 165 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | |
| 167 | // Allocate intermediate buffers |
| 168 | _tmp.allocator()->allocate(); |
| 169 | _max.allocator()->allocate(); |
| 170 | _sum.allocator()->allocate(); |
| 171 | } |
| 172 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 173 | template <bool IS_LOG> |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 174 | Status CLSoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, size_t axis) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 175 | { |
Georgios Pinitas | ee8be2d | 2017-11-22 12:53:45 +0000 | [diff] [blame] | 176 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 177 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported"); |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 178 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis != 0, "Only axis 0 supported in tensors"); |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 179 | ARM_COMPUTE_UNUSED(beta); |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 180 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() <= axis); |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 181 | |
| 182 | // Convert reduce-before axis (inclusive) to first n axes to reduce |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 183 | size_t first_n_reduce_axes = dim_index_2_num_dims(axis, input->num_dimensions()); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 184 | |
| 185 | // Create intermediate tensor info |
| 186 | DataType tmp_data_type = is_data_type_quantized_asymmetric(input->data_type()) ? DataType::S32 : input->data_type(); |
Michele Di Giorgio | 5cb3773 | 2018-06-08 18:07:08 +0100 | [diff] [blame] | 187 | TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type).set_is_resizable(true)); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 188 | |
| 189 | TensorShape max_sum_shape = input->tensor_shape(); |
| 190 | max_sum_shape.set(0, 1); |
Michele Di Giorgio | 5cb3773 | 2018-06-08 18:07:08 +0100 | [diff] [blame] | 191 | TensorInfo tensor_info_max(input->clone()->set_tensor_shape(max_sum_shape).set_is_resizable(true)); |
| 192 | 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 Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 193 | |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 194 | const bool needs_flattening = (first_n_reduce_axes > 1); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 195 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 196 | if(needs_flattening) |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 197 | { |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 198 | const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input, first_n_reduce_axes); |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 199 | TensorInfo tensor_info_flat(input->clone()->set_tensor_shape(shape_flatten).set_is_resizable(true)); |
| 200 | |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 201 | if(first_n_reduce_axes == 3) |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 202 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 203 | ARM_COMPUTE_RETURN_ON_ERROR(CLFlattenLayer::validate(input, &tensor_info_flat)); |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 204 | } |
| 205 | else |
| 206 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 207 | ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(input, &tensor_info_flat)); |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 208 | } |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 211 | SoftmaxKernelInfo softmax_info; |
| 212 | softmax_info.beta = beta; |
| 213 | softmax_info.is_log = IS_LOG; |
| 214 | softmax_info.input_data_type = input->data_type(); |
| 215 | |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 216 | ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DMaxShiftExpSumKernel::validate(input, &tensor_info_max, &tensor_info_tmp, &tensor_info_sum)); |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 217 | ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DNormKernel::validate(&tensor_info_tmp, &tensor_info_sum, output, softmax_info)); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 218 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 219 | if(needs_flattening) |
| 220 | { |
| 221 | const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input); |
| 222 | TensorInfo tensor_info_flat(input->clone()->set_tensor_shape(shape_flatten).set_is_resizable(true)); |
| 223 | } |
| 224 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 225 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 228 | template <bool IS_LOG> |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 229 | void CLSoftmaxLayerGeneric<IS_LOG>::run() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 230 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 231 | MemoryGroupResourceScope scope_mg(_memory_group); |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 232 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 233 | if(_needs_flattening) |
| 234 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 235 | _flatten_ptr->run(); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 236 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 237 | |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 238 | CLScheduler::get().enqueue(_max_shift_exp_sum_kernel, false); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 239 | CLScheduler::get().enqueue(_norm_kernel, !_needs_flattening); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 240 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 241 | if(_needs_flattening) |
| 242 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 243 | _reshape.run(); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 244 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 245 | } |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 246 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 247 | template class CLSoftmaxLayerGeneric<false>; |
| 248 | template class CLSoftmaxLayerGeneric<true>; |
| 249 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 250 | } // namespace arm_compute |