Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +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/CL/functions/CLSoftmaxLayer.h" |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 25 | #include "arm_compute/core/CL/CLHelpers.h" |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Helpers.h" |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/KernelDescriptors.h" |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 29 | #include "arm_compute/core/Types.h" |
| 30 | #include "arm_compute/core/Utils.h" |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 31 | #include "src/core/helpers/MemoryHelpers.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 32 | #include "src/gpu/cl/kernels/ClSoftmaxKernel.h" |
| 33 | #include "src/gpu/cl/operators/ClPermute.h" |
| 34 | #include "src/gpu/cl/operators/ClSoftmax.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 38 | using OperatorType = opencl::ClSoftmax; |
| 39 | |
| 40 | template <bool IS_LOG> |
| 41 | struct CLSoftmaxLayerGeneric<IS_LOG>::Impl |
| 42 | { |
| 43 | const ICLTensor *src{ nullptr }; |
| 44 | ICLTensor *dst{ nullptr }; |
| 45 | std::unique_ptr<OperatorType> op{ nullptr }; |
| 46 | MemoryGroup memory_group{}; |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 47 | ITensorPack run_pack{}; |
| 48 | WorkspaceData<CLTensor> workspace_tensors{}; |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 51 | template <bool IS_LOG> |
| 52 | CLSoftmaxLayerGeneric<IS_LOG>::CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager) |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 53 | : _impl(std::make_unique<Impl>()) |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 54 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 55 | _impl->memory_group = MemoryGroup(std::move(memory_manager)); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 58 | template <bool IS_LOG> |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 59 | CLSoftmaxLayerGeneric<IS_LOG>::~CLSoftmaxLayerGeneric() = default; |
| 60 | |
| 61 | template <bool IS_LOG> |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 62 | void CLSoftmaxLayerGeneric<IS_LOG>::configure(const ICLTensor *input, ICLTensor *output, float beta, int32_t axis) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | { |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 64 | configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | template <bool IS_LOG> |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 68 | void CLSoftmaxLayerGeneric<IS_LOG>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, float beta, int32_t axis) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 69 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 70 | _impl->src = input; |
| 71 | _impl->dst = output; |
| 72 | _impl->op = std::make_unique<OperatorType>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 74 | SoftmaxKernelInfo softmax_info{ beta, IS_LOG, input->info()->data_type(), axis }; |
| 75 | _impl->op->configure(compile_context, *input->info(), *output->info(), softmax_info); |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 76 | |
| 77 | _impl->run_pack = { { TensorType::ACL_SRC, _impl->src }, { TensorType::ACL_DST, _impl->dst } }; |
| 78 | _impl->workspace_tensors = manage_workspace<CLTensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 81 | template <bool IS_LOG> |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 82 | Status CLSoftmaxLayerGeneric<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, int32_t axis) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 83 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 84 | SoftmaxKernelInfo softmax_info{ beta, IS_LOG, input->data_type(), axis }; |
| 85 | return OperatorType::validate(*input, *output, softmax_info); |
| 86 | } |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 87 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 88 | template <bool IS_LOG> |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 89 | void CLSoftmaxLayerGeneric<IS_LOG>::run() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 91 | // Acquire all the temporaries |
| 92 | MemoryGroupResourceScope scope_mg(_impl->memory_group); |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 93 | ARM_COMPUTE_ERROR_ON_NULLPTR(_impl->src, _impl->dst); |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 94 | _impl->op->run(_impl->run_pack); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | } |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 96 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 97 | template class CLSoftmaxLayerGeneric<false>; |
| 98 | template class CLSoftmaxLayerGeneric<true>; |
| 99 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 100 | } // namespace arm_compute |