Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/CL/CLMemoryGroup.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 35 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 36 | namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | { |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 38 | CLSoftmaxLayer::CLSoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager) |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 39 | : _memory_group(std::move(memory_manager)), _max_shift_exp_sum_kernel(), _norm_kernel(), _flatten_kernel_ptr(), _reshape_kernel(), _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 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 44 | void CLSoftmaxLayer::configure_reshape_input_kernel(const ICLTensor *input, const ICLTensor *output, size_t axis) |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 45 | { |
| 46 | // Flatten the input |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 47 | const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input->info(), axis); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 48 | |
| 49 | // Initialize the flat input |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 50 | _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] | 51 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 52 | // If we need to flatten the input, we can use CLFlattenKernel or CLReshapeKernel |
| 53 | // If flattening on the third axes, we use CLFlattenKernel. |
| 54 | // In all other cases we have to use CLReshapeKernel |
| 55 | if(axis != 3) |
| 56 | { |
| 57 | auto reshape_kernel_ptr = support::cpp14::make_unique<CLReshapeLayerKernel>(); |
| 58 | reshape_kernel_ptr->configure(input, &_input_flattened); |
| 59 | _flatten_kernel_ptr = std::move(reshape_kernel_ptr); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | auto flatten_kernel_ptr = support::cpp14::make_unique<CLFlattenLayerKernel>(); |
| 64 | flatten_kernel_ptr->configure(input, &_input_flattened); |
| 65 | _flatten_kernel_ptr = std::move(flatten_kernel_ptr); |
| 66 | } |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 67 | |
| 68 | // We need to init the output tensor here. Indeed, the reshape kernel expects |
| 69 | // both tensors to be already initialized |
| 70 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | } |
| 72 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 73 | void CLSoftmaxLayer::configure(const ICLTensor *input, ICLTensor *output, float beta, size_t axis) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | { |
Georgios Pinitas | ee8be2d | 2017-11-22 12:53:45 +0000 | [diff] [blame] | 75 | // Perform validation step |
| 76 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 77 | ARM_COMPUTE_ERROR_THROW_ON(CLSoftmaxLayer::validate(input->info(), output->info(), beta, axis)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 79 | // We don't need flattening only in the case the input is 2D and axis is 1 |
| 80 | _needs_flattening = axis != 1; |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 81 | |
| 82 | // If we are dealing with a 4D tensor, we will: |
| 83 | // - Flatten the input, so that we end up with a [width*height*depth] * batches 2D tensor |
| 84 | // - Execute all the pipeline (reduction + normalization) on the flattened tensor |
| 85 | // - Reshape the flattened output into the real output |
| 86 | if(_needs_flattening) |
| 87 | { |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 88 | // Add to the memory manager _input_flattened |
| 89 | _memory_group.manage(&_input_flattened); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 90 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 91 | // Cofigure _flatten_kernel and _input_flattened |
| 92 | configure_reshape_input_kernel(input, output, axis); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // We want to deal with a 2D input. Either it is the flattened version of the original input (4D case) |
| 96 | // or it is the original input case (2D case) |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 97 | const ICLTensor *input_2D = (_needs_flattening ? &_input_flattened : input); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 98 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | // Create intermediate tensors shapes |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 100 | TensorInfo input_info = input_2D->info()->clone()->reset_padding().set_is_resizable(true); |
| 101 | DataType tmp_data_type = is_data_type_quantized_asymmetric(input_2D->info()->data_type()) ? DataType::S32 : input_2D->info()->data_type(); |
| 102 | TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type)); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 103 | _tmp.allocator()->init(tensor_info_tmp); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 105 | TensorShape max_sum_shape = input_2D->info()->tensor_shape(); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 106 | max_sum_shape.set(0, 1); |
Georgios Pinitas | ee8be2d | 2017-11-22 12:53:45 +0000 | [diff] [blame] | 107 | _max.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape)); |
| 108 | _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] | 109 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 110 | // Set GPU target to kernels |
| 111 | _max_shift_exp_sum_kernel.set_target(CLScheduler::get().target()); |
| 112 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 113 | // Manage intermediate buffers |
| 114 | _memory_group.manage(&_tmp); |
| 115 | _memory_group.manage(&_max); |
| 116 | _memory_group.manage(&_sum); |
| 117 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 118 | // Configure kernels |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 119 | _max_shift_exp_sum_kernel.configure(input_2D, &_max, &_tmp, &_sum, beta); |
| 120 | |
| 121 | if(_needs_flattening) |
| 122 | { |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 123 | // Add to the memory manager _output_flattened |
| 124 | _memory_group.manage(&_output_flattened); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 125 | |
| 126 | // The normalization kernel stores the result in a flat output tensor |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 127 | _norm_kernel.configure(&_tmp, &_sum, &_output_flattened, beta); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 128 | |
| 129 | // Reshape the flat output into a the requested (4D) output |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 130 | _reshape_kernel.configure(&_output_flattened, output); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 131 | |
| 132 | // Allocate the intermediate flat tensors |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 133 | _input_flattened.allocator()->allocate(); |
| 134 | _output_flattened.allocator()->allocate(); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 135 | } |
| 136 | else |
| 137 | { |
| 138 | // Softmax 2D case |
| 139 | _norm_kernel.configure(&_tmp, &_sum, output, beta); |
| 140 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | |
| 142 | // Allocate intermediate buffers |
| 143 | _tmp.allocator()->allocate(); |
| 144 | _max.allocator()->allocate(); |
| 145 | _sum.allocator()->allocate(); |
| 146 | } |
| 147 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 148 | Status CLSoftmaxLayer::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, size_t axis) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 149 | { |
Georgios Pinitas | ee8be2d | 2017-11-22 12:53:45 +0000 | [diff] [blame] | 150 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 151 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported"); |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 152 | ARM_COMPUTE_UNUSED(beta); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 153 | |
| 154 | // Create intermediate tensor info |
| 155 | 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] | 156 | 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] | 157 | |
| 158 | TensorShape max_sum_shape = input->tensor_shape(); |
| 159 | max_sum_shape.set(0, 1); |
Michele Di Giorgio | 5cb3773 | 2018-06-08 18:07:08 +0100 | [diff] [blame] | 160 | TensorInfo tensor_info_max(input->clone()->set_tensor_shape(max_sum_shape).set_is_resizable(true)); |
| 161 | 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] | 162 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 163 | const bool needs_flattening = (axis != 1); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 164 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 165 | if(needs_flattening) |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 166 | { |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 167 | const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input, axis); |
| 168 | TensorInfo tensor_info_flat(input->clone()->set_tensor_shape(shape_flatten).set_is_resizable(true)); |
| 169 | |
| 170 | if(axis != 3) |
| 171 | { |
| 172 | ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayerKernel::validate(input, &tensor_info_flat)); |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | ARM_COMPUTE_RETURN_ON_ERROR(CLFlattenLayerKernel::validate(input, &tensor_info_flat)); |
| 177 | } |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 180 | ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DMaxShiftExpSumKernel::validate(input, &tensor_info_max, &tensor_info_tmp, &tensor_info_sum)); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 181 | ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DNormKernel::validate(&tensor_info_tmp, &tensor_info_sum, output)); |
| 182 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 183 | if(needs_flattening) |
| 184 | { |
| 185 | const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input); |
| 186 | TensorInfo tensor_info_flat(input->clone()->set_tensor_shape(shape_flatten).set_is_resizable(true)); |
| 187 | } |
| 188 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 189 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 192 | void CLSoftmaxLayer::run() |
| 193 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 194 | _memory_group.acquire(); |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 195 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 196 | if(_needs_flattening) |
| 197 | { |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 198 | CLScheduler::get().enqueue(*_flatten_kernel_ptr, false); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 199 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 200 | |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 201 | CLScheduler::get().enqueue(_max_shift_exp_sum_kernel, false); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 202 | CLScheduler::get().enqueue(_norm_kernel, !_needs_flattening); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 203 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 204 | if(_needs_flattening) |
| 205 | { |
| 206 | CLScheduler::get().enqueue(_reshape_kernel, true); |
| 207 | } |
| 208 | |
| 209 | // Relase intermediate buffers |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 210 | _memory_group.release(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 211 | } |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 212 | |
| 213 | } // namespace arm_compute |