Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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" |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 25 | #include "arm_compute/core/Validate.h" |
| 26 | #include "arm_compute/runtime/Tensor.h" |
| 27 | #include "src/core/cpu/kernels/CpuSoftmaxKernel.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 28 | #include "src/core/helpers/SoftmaxHelpers.h" |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 29 | #include "src/runtime/cpu/operators/CpuSoftmax.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> |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 34 | struct NESoftmaxLayerGeneric<IS_LOG>::Impl |
| 35 | { |
| 36 | const ITensor *src{ nullptr }; |
| 37 | ITensor *dst{ nullptr }; |
| 38 | Tensor max{ nullptr }; |
| 39 | Tensor tmp{ nullptr }; |
| 40 | Tensor input_permuted{ nullptr }; |
| 41 | Tensor output_permuted{ nullptr }; |
| 42 | std::unique_ptr<cpu::CpuSoftmaxGeneric<IS_LOG>> op{ nullptr }; |
| 43 | }; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 44 | |
| 45 | template <bool IS_LOG> |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 46 | NESoftmaxLayerGeneric<IS_LOG>::NESoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager) |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 47 | : _memory_group(std::move(memory_manager)), _impl(std::make_unique<Impl>()) |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 51 | template <bool IS_LOG> |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 52 | NESoftmaxLayerGeneric<IS_LOG>::NESoftmaxLayerGeneric(NESoftmaxLayerGeneric &&) = default; |
| 53 | template <bool IS_LOG> |
| 54 | NESoftmaxLayerGeneric<IS_LOG> &NESoftmaxLayerGeneric<IS_LOG>::operator=(NESoftmaxLayerGeneric &&) = default; |
| 55 | template <bool IS_LOG> |
| 56 | NESoftmaxLayerGeneric<IS_LOG>::~NESoftmaxLayerGeneric() = default; |
| 57 | |
| 58 | template <bool IS_LOG> |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 59 | 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] | 60 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 63 | _impl->src = input; |
| 64 | _impl->dst = output; |
| 65 | _impl->op = std::make_unique<cpu::CpuSoftmaxGeneric<IS_LOG>>(); |
| 66 | _impl->op->configure(input->info(), output->info(), beta, axis); |
Sheri Zhang | 1f567af | 2020-05-05 11:47:36 +0100 | [diff] [blame] | 67 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 68 | const unsigned int actual_axis = static_cast<unsigned int>(wrap_around(axis, static_cast<int32_t>(input->info()->num_dimensions()))); |
| 69 | const bool needs_permute = actual_axis > 0; |
| 70 | if(needs_permute) |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 71 | { |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 72 | // Add to the memory manager _input_permuted |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 73 | auto permute_input = std::make_unique<cpu::CpuPermute>(); |
| 74 | _memory_group.manage(&_impl->input_permuted); |
| 75 | permute_input->configure(input->info(), _impl->input_permuted.info(), softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis)); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 76 | } |
| 77 | |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 78 | // We want to deal with a 2D input. Either it is the permuted version of the original input (4D case) |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 79 | // or it is the original input case (2D case) |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 80 | ITensor *tmp_input = (needs_permute ? &_impl->input_permuted : input); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 81 | |
| 82 | // Create intermediate tensors shapes |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 83 | const TensorInfo input_info = tmp_input->info()->clone()->reset_padding().set_is_resizable(true); |
| 84 | DataType tmp_data_type = is_data_type_quantized_asymmetric(tmp_input->info()->data_type()) ? DataType::F32 : tmp_input->info()->data_type(); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 85 | 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] | 86 | |
| 87 | // Init intermediate tensors |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 88 | TensorShape max_sum_shape = tmp_input->info()->tensor_shape(); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 89 | max_sum_shape.set(0, 1); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 90 | _impl->max.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape)); |
| 91 | _impl->tmp.allocator()->init(tensor_info_tmp); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 92 | |
| 93 | // Manage intermediate buffers |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 94 | _memory_group.manage(&_impl->max); |
| 95 | _memory_group.manage(&_impl->tmp); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 97 | // Configure kernels |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 98 | auto max_kernel = std::make_unique<cpu::kernels::CpuLogits1DMaxKernel>(); |
| 99 | auto softmax_kernel = std::make_unique<cpu::kernels::CpuLogits1DSoftmaxKernel<IS_LOG>>(); |
| 100 | max_kernel->configure(tmp_input->info(), _impl->max.info()); |
| 101 | |
| 102 | if(needs_permute) |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 103 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 104 | auto permute_output = std::make_unique<cpu::CpuPermute>(); |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 105 | // Add to the memory manager _output_permuted |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 106 | _memory_group.manage(&_impl->output_permuted); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 107 | |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 108 | // The normalization kernel stores the result in a permuted output tensor |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 109 | softmax_kernel->configure(tmp_input->info(), _impl->max.info(), _impl->output_permuted.info(), beta, _impl->tmp.info()); |
| 110 | _impl->input_permuted.allocator()->allocate(); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 111 | |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 112 | // Re-permute the permuted output into the requested (4D) output |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 113 | permute_output->configure(_impl->output_permuted.info(), output->info(), softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis)); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 114 | |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 115 | // Allocate the intermediate permuted tensors |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 116 | _impl->output_permuted.allocator()->allocate(); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 117 | } |
| 118 | else |
| 119 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 120 | softmax_kernel->configure(tmp_input->info(), _impl->max.info(), output->info(), beta, _impl->tmp.info()); |
Manuel Bottini | 678d83a | 2019-01-07 16:05:36 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Allocate intermediate buffers |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 124 | _impl->max.allocator()->allocate(); |
| 125 | _impl->tmp.allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 128 | template <bool IS_LOG> |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 129 | 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] | 130 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 131 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 132 | ARM_COMPUTE_RETURN_ON_ERROR(cpu::CpuSoftmaxGeneric<IS_LOG>::validate(input, output, beta, axis)); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 133 | return Status{}; |
| 134 | } |
| 135 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 136 | template <bool IS_LOG> |
| 137 | void NESoftmaxLayerGeneric<IS_LOG>::run() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 139 | MemoryGroupResourceScope scope_mg(_memory_group); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 140 | ITensorPack pack; |
| 141 | pack.add_tensor(TensorType::ACL_SRC, _impl->src); |
| 142 | pack.add_tensor(TensorType::ACL_DST, _impl->dst); |
| 143 | pack.add_tensor(TensorType::ACL_INT_0, &_impl->tmp); |
| 144 | pack.add_tensor(TensorType::ACL_INT_1, &_impl->max); |
| 145 | pack.add_tensor(TensorType::ACL_INT_2, &_impl->input_permuted); |
| 146 | pack.add_tensor(TensorType::ACL_INT_3, &_impl->output_permuted); |
| 147 | _impl->op->run(pack); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | } |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 149 | |
| 150 | template class NESoftmaxLayerGeneric<false>; |
| 151 | template class NESoftmaxLayerGeneric<true>; |
| 152 | |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 153 | } // namespace arm_compute |