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