Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. |
| 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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/cpu/operators/CpuSoftmax.h" |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/TensorInfo.h" |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Validate.h" |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | |
ramelg01 | 3ae3d88 | 2021-09-12 23:07:47 +0100 | [diff] [blame] | 32 | #include "src/common/utils/Log.h" |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 33 | #include "src/core/helpers/MemoryHelpers.h" |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 34 | #include "src/core/helpers/SoftmaxHelpers.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 35 | #include "src/cpu/kernels/CpuSoftmaxKernel.h" |
| 36 | #include "src/cpu/utils/CpuAuxTensorHandler.h" |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 37 | |
| 38 | using namespace arm_compute::experimental; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace cpu |
| 43 | { |
| 44 | template <bool IS_LOG> |
| 45 | CpuSoftmaxGeneric<IS_LOG>::CpuSoftmaxGeneric() |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 46 | : _permute_input(), |
| 47 | _permute_output(), |
| 48 | _max_kernel(), |
| 49 | _softmax_kernel(), |
| 50 | _max(), |
| 51 | _tmp(), |
| 52 | _input_permuted(), |
| 53 | _output_permuted(), |
| 54 | _needs_permute(false), |
| 55 | _aux_mem(InternalTensorIdx::COUNT) |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
| 59 | template <bool IS_LOG> |
| 60 | void CpuSoftmaxGeneric<IS_LOG>::configure(const ITensorInfo *src, ITensorInfo *dst, float beta, int32_t axis) |
| 61 | { |
| 62 | // Perform validation step |
| 63 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 64 | ARM_COMPUTE_ERROR_THROW_ON(CpuSoftmaxGeneric::validate(src, dst, beta, axis)); |
ramelg01 | 3ae3d88 | 2021-09-12 23:07:47 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_LOG_PARAMS(src, dst, beta, axis); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 66 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 67 | const unsigned int actual_axis = |
| 68 | static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(src->num_dimensions()))); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 69 | |
| 70 | _needs_permute = actual_axis > 0; |
| 71 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 72 | if (_needs_permute) |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 73 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 74 | _permute_input.configure(src, &_input_permuted, |
| 75 | softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis)); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // We want to deal with a 2D input. Either it is the permuted version of the original input (4D case) |
| 79 | // or it is the original input case (2D case) |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 80 | const ITensorInfo *tmp_input = (_needs_permute ? &_input_permuted : src); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 81 | |
| 82 | // Create intermediate tensors shapes |
| 83 | TensorShape max_sum_shape = tmp_input->tensor_shape(); |
| 84 | max_sum_shape.set(0, 1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 85 | const TensorInfo input_info = tmp_input->clone()->reset_padding().set_is_resizable(true); |
| 86 | DataType tmp_data_type = |
| 87 | is_data_type_quantized_asymmetric(tmp_input->data_type()) ? DataType::F32 : tmp_input->data_type(); |
| 88 | TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type)); |
| 89 | TensorInfo max_info(tmp_input->clone()->set_tensor_shape(max_sum_shape)); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 90 | |
| 91 | // Init intermediate tensors |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 92 | _max = TensorInfo(max_info); |
| 93 | _tmp = TensorInfo(tensor_info_tmp); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 94 | |
| 95 | // Configure kernels |
| 96 | auto mk = std::make_unique<kernels::CpuLogits1DMaxKernel>(); |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 97 | mk->configure(tmp_input, &_max); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 98 | _max_kernel = std::move(mk); |
| 99 | |
| 100 | auto sm = std::make_unique<kernels::CpuLogits1DSoftmaxKernel<IS_LOG>>(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 101 | if (_needs_permute) |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 102 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 103 | // The normalization kernel stores the result in a permuted output tensor |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 104 | sm->configure(tmp_input, &_max, &_output_permuted, beta, &_tmp); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 105 | |
| 106 | // Re-permute the permuted output into the requested (4D) output |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | _permute_output.configure(&_output_permuted, dst, |
| 108 | softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis)); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 109 | } |
| 110 | else |
| 111 | { |
| 112 | // Softmax 2D case |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 113 | sm->configure(tmp_input, &_max, dst, beta, &_tmp); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 114 | } |
| 115 | _softmax_kernel = std::move(sm); |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 116 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 117 | _aux_mem[InternalTensorIdx::MAX] = |
| 118 | MemoryInfo(offset_int_vec(InternalTensorIdx::MAX), MemoryLifetime::Temporary, _max.total_size()); |
| 119 | _aux_mem[InternalTensorIdx::TMP] = |
| 120 | MemoryInfo(offset_int_vec(InternalTensorIdx::TMP), MemoryLifetime::Temporary, _tmp.total_size()); |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 121 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 122 | _aux_mem[InternalTensorIdx::PERMUTED_SRC] = MemoryInfo(offset_int_vec(InternalTensorIdx::PERMUTED_SRC), |
| 123 | MemoryLifetime::Temporary, _input_permuted.total_size()); |
| 124 | _aux_mem[InternalTensorIdx::PERMUTED_DST] = MemoryInfo(offset_int_vec(InternalTensorIdx::PERMUTED_DST), |
| 125 | MemoryLifetime::Temporary, _output_permuted.total_size()); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | template <bool IS_LOG> |
| 129 | Status CpuSoftmaxGeneric<IS_LOG>::validate(const ITensorInfo *src, const ITensorInfo *dst, float beta, int32_t axis) |
| 130 | { |
| 131 | // Perform validation step |
| 132 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 133 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->num_dimensions() > 4, "Only up to 4 dimensions are supported"); |
| 134 | ARM_COMPUTE_UNUSED(beta); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 135 | ARM_COMPUTE_RETURN_ERROR_ON(axis < static_cast<int32_t>(-src->num_dimensions()) || |
| 136 | static_cast<int32_t>(src->num_dimensions()) <= axis); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 137 | |
| 138 | // Create intermediate tensor info |
| 139 | DataType tmp_data_type = src->data_type(); |
| 140 | const TensorInfo tensor_info_tmp(src->clone()->set_data_type(tmp_data_type).set_is_resizable(true)); |
| 141 | |
| 142 | TensorShape max_sum_shape = src->tensor_shape(); |
| 143 | max_sum_shape.set(0, 1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 144 | const TensorInfo tensor_info_max_sum(src->clone() |
| 145 | ->set_tensor_shape(max_sum_shape) |
| 146 | .set_data_type(tmp_data_type) |
| 147 | .set_quantization_info(src->quantization_info()) |
| 148 | .set_is_resizable(true)); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 149 | const TensorInfo dont_care; |
| 150 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 151 | const unsigned int actual_axis = |
| 152 | static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(src->num_dimensions()))); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 153 | |
| 154 | const bool needs_permute = actual_axis > 0; |
| 155 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 156 | if (needs_permute) |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 157 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 158 | const PermutationVector permutation_vector = |
| 159 | softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis); |
| 160 | const TensorShape permuted_shape = |
| 161 | misc::shape_calculator::compute_permutation_output_shape(*src, permutation_vector); |
| 162 | TensorInfo input_permuted(src->clone()->set_tensor_shape(permuted_shape)); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 163 | ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(src, &input_permuted, permutation_vector)); |
| 164 | TensorInfo output_permuted(dst->clone()->set_tensor_shape(permuted_shape)); |
| 165 | ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(&output_permuted, dst, permutation_vector)); |
| 166 | } |
| 167 | |
| 168 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuLogits1DMaxKernel::validate(src, &tensor_info_max_sum)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 169 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuLogits1DSoftmaxKernel<IS_LOG>::validate( |
| 170 | &tensor_info_tmp, &tensor_info_max_sum, dst, beta, &dont_care)); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 171 | |
| 172 | return Status{}; |
| 173 | } |
| 174 | |
| 175 | template <bool IS_LOG> |
| 176 | void CpuSoftmaxGeneric<IS_LOG>::run(ITensorPack &tensors) |
| 177 | { |
| 178 | ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided"); |
| 179 | |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 180 | auto src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 181 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
| 182 | |
Georgios Pinitas | 1fa27ad | 2021-07-22 01:25:57 +0100 | [diff] [blame] | 183 | CpuAuxTensorHandler tmp(offset_int_vec(InternalTensorIdx::TMP), _tmp, tensors, true); |
| 184 | CpuAuxTensorHandler max(offset_int_vec(InternalTensorIdx::MAX), _max, tensors, true); |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 185 | |
Georgios Pinitas | 1fa27ad | 2021-07-22 01:25:57 +0100 | [diff] [blame] | 186 | CpuAuxTensorHandler input_permuted(offset_int_vec(InternalTensorIdx::PERMUTED_SRC), _input_permuted, tensors, true); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 187 | CpuAuxTensorHandler output_permuted(offset_int_vec(InternalTensorIdx::PERMUTED_DST), _output_permuted, tensors, |
| 188 | true); |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 189 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 190 | ITensorPack max_pack; |
| 191 | ITensorPack softmax_pack; |
| 192 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 193 | if (_needs_permute) |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 194 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 195 | ITensorPack permute_in_pack = {{TensorType::ACL_SRC, src}, {TensorType::ACL_DST, input_permuted.get()}}; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 196 | _permute_input.run(permute_in_pack); |
| 197 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 198 | max_pack = {{TensorType::ACL_SRC, input_permuted.get()}, {TensorType::ACL_DST, max.get()}}; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 199 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 200 | softmax_pack = {{TensorType::ACL_SRC_0, input_permuted.get()}, |
| 201 | {TensorType::ACL_SRC_1, max.get()}, |
| 202 | {TensorType::ACL_DST_0, output_permuted.get()}, |
| 203 | {TensorType::ACL_DST_1, tmp.get()}}; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 204 | } |
| 205 | else |
| 206 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 207 | max_pack = {{TensorType::ACL_SRC, src}, {TensorType::ACL_DST, max.get()}}; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 208 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 209 | softmax_pack = {{TensorType::ACL_SRC_0, src}, |
| 210 | {TensorType::ACL_SRC_1, max.get()}, |
| 211 | {TensorType::ACL_DST_0, dst}, |
| 212 | {TensorType::ACL_DST_1, tmp.get()}}; |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 213 | } |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 214 | |
| 215 | NEScheduler::get().schedule_op(_max_kernel.get(), Window::DimY, _max_kernel->window(), max_pack); |
| 216 | NEScheduler::get().schedule_op(_softmax_kernel.get(), Window::DimY, _softmax_kernel->window(), softmax_pack); |
| 217 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 218 | if (_needs_permute) |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 219 | { |
| 220 | ITensorPack permute_out_pack; |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 221 | permute_out_pack.add_tensor(TensorType::ACL_SRC, output_permuted.get()); |
| 222 | permute_out_pack.add_tensor(TensorType::ACL_DST, dst); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 223 | _permute_output.run(permute_out_pack); |
| 224 | } |
| 225 | } |
| 226 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 227 | template <bool IS_LOG> |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 228 | experimental::MemoryRequirements CpuSoftmaxGeneric<IS_LOG>::workspace() const |
| 229 | { |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 230 | return _aux_mem; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | template class CpuSoftmaxGeneric<false>; |
| 234 | template class CpuSoftmaxGeneric<true>; |
| 235 | } // namespace cpu |
| 236 | } // namespace arm_compute |