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" |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 31 | #include "src/core/gpu/cl/kernels/ClSoftmaxKernel.h" |
| 32 | #include "src/runtime/gpu/cl/operators/ClPermute.h" |
| 33 | #include "src/runtime/gpu/cl/operators/ClSoftmax.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 35 | namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 37 | using OperatorType = opencl::ClSoftmax; |
| 38 | |
| 39 | template <bool IS_LOG> |
| 40 | struct CLSoftmaxLayerGeneric<IS_LOG>::Impl |
| 41 | { |
| 42 | const ICLTensor *src{ nullptr }; |
| 43 | ICLTensor *dst{ nullptr }; |
| 44 | std::unique_ptr<OperatorType> op{ nullptr }; |
| 45 | MemoryGroup memory_group{}; |
| 46 | std::vector<std::pair<TensorType, std::unique_ptr<CLTensor>>> workspace_tensors{}; |
| 47 | }; |
| 48 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 49 | template <bool IS_LOG> |
| 50 | CLSoftmaxLayerGeneric<IS_LOG>::CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager) |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 51 | : _impl(std::make_unique<Impl>()) |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 52 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 53 | _impl->memory_group = MemoryGroup(std::move(memory_manager)); |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 56 | template <bool IS_LOG> |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 57 | CLSoftmaxLayerGeneric<IS_LOG>::~CLSoftmaxLayerGeneric() = default; |
| 58 | |
| 59 | template <bool IS_LOG> |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 60 | 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] | 61 | { |
morgolock | 9c7fed8 | 2020-08-05 12:30:56 +0100 | [diff] [blame] | 62 | configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | template <bool IS_LOG> |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 66 | 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] | 67 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 68 | _impl->src = input; |
| 69 | _impl->dst = output; |
| 70 | _impl->op = std::make_unique<OperatorType>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 72 | SoftmaxKernelInfo softmax_info{ beta, IS_LOG, input->info()->data_type(), axis }; |
| 73 | _impl->op->configure(compile_context, *input->info(), *output->info(), softmax_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 76 | template <bool IS_LOG> |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 77 | 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] | 78 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 79 | SoftmaxKernelInfo softmax_info{ beta, IS_LOG, input->data_type(), axis }; |
| 80 | return OperatorType::validate(*input, *output, softmax_info); |
| 81 | } |
SiCong Li | d004a7a | 2020-05-28 15:26:41 +0100 | [diff] [blame] | 82 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 83 | template <bool IS_LOG> |
| 84 | void CLSoftmaxLayerGeneric<IS_LOG>::allocate_workspace() |
| 85 | { |
| 86 | const auto memory_requirements = _impl->op->workspace(); |
| 87 | std::for_each(memory_requirements.begin(), memory_requirements.end(), [this](const experimental::MemoryInfo & memory_info) |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 88 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 89 | auto tensor_info = TensorInfo{ TensorShape(memory_info.size), 1, DataType::U8 }; |
| 90 | _impl->workspace_tensors.emplace_back(memory_info.type, std::make_unique<CLTensor>()); |
| 91 | auto tensor = _impl->workspace_tensors.back().second.get(); |
| 92 | ARM_COMPUTE_ERROR_ON_NULLPTR(tensor); |
| 93 | _impl->memory_group.manage(tensor); |
| 94 | tensor->allocator()->init(tensor_info); |
| 95 | tensor->allocator()->allocate(); |
| 96 | }); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 99 | template <bool IS_LOG> |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 100 | void CLSoftmaxLayerGeneric<IS_LOG>::run() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 102 | allocate_workspace(); |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 103 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 104 | // Acquire all the temporaries |
| 105 | MemoryGroupResourceScope scope_mg(_impl->memory_group); |
| 106 | |
| 107 | ARM_COMPUTE_ERROR_ON_NULLPTR(_impl->src, _impl->dst); |
| 108 | |
| 109 | ITensorPack pack; |
| 110 | pack.add_tensor(TensorType::ACL_SRC, _impl->src); |
| 111 | pack.add_tensor(TensorType::ACL_DST, _impl->dst); |
| 112 | |
| 113 | std::for_each(_impl->workspace_tensors.begin(), _impl->workspace_tensors.end(), [&pack](std::pair<TensorType, std::unique_ptr<CLTensor>> &wt) |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 114 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 115 | auto tensor = wt.second.get(); |
| 116 | ARM_COMPUTE_ERROR_ON_NULLPTR(tensor); |
| 117 | pack.add_tensor(wt.first, tensor); |
| 118 | }); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 119 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame^] | 120 | _impl->op->run(pack); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | } |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 122 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 123 | template class CLSoftmaxLayerGeneric<false>; |
| 124 | template class CLSoftmaxLayerGeneric<true>; |
| 125 | |
Giuseppe Rossini | 87e896a | 2018-08-24 10:24:12 +0100 | [diff] [blame] | 126 | } // namespace arm_compute |