giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 Arm Limited. |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +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/core/CL/kernels/CLElementwiseOperationKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/runtime/CL/CLScheduler.h" |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/functions/CLPReluLayer.h" |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 29 | #include "support/MemorySupport.h" |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace |
| 34 | { |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 35 | void configure_border_handler(const CLCompileContext &compile_context, CLFillBorderKernel &border_handler, BorderSize border_size, ITensorInfo *input1, ITensorInfo *input2, const ITensorInfo *output) |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 36 | { |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 37 | if(output->dimension(0) > 1) |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 38 | { |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 39 | ITensorInfo *broadcasted_info = (input1->dimension(0) == 1) ? input1 : input2; |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 40 | |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 41 | if(broadcasted_info->dimension(0) == 1) |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 42 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 43 | border_handler.configure(compile_context, broadcasted_info, border_size, BorderMode::REPLICATE); |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | } |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 47 | |
| 48 | ITensorPack select_border_input(ITensorPack &tensors) |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 49 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 50 | ITensorPack pack; |
| 51 | if(tensors.get_tensor(TensorType::ACL_DST)->info()->dimension(0) > 1) |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 52 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 53 | if(tensors.get_const_tensor(TensorType::ACL_SRC_1)->info()->dimension(0) == 1) |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 54 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 55 | pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(TensorType::ACL_SRC_1)); |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 56 | } |
| 57 | else |
| 58 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 59 | pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(TensorType::ACL_SRC_0)); |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 60 | } |
| 61 | } |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 62 | return pack; |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 63 | } |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 64 | } // namespace |
| 65 | |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 66 | namespace experimental |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 67 | { |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 68 | CLPReluLayer::CLPReluLayer() |
| 69 | : _border_handler() |
| 70 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 73 | void CLPReluLayer::configure(const CLCompileContext &compile_context, ITensorInfo *input, ITensorInfo *alpha, ITensorInfo *output) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 74 | { |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 75 | auto k = arm_compute::support::cpp14::make_unique<CLArithmeticOperationKernel>(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 76 | k->configure(compile_context, ArithmeticOperation::PRELU, input, alpha, output); |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 77 | _kernel = std::move(k); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 78 | configure_border_handler(compile_context, _border_handler, _kernel->border_size(), input, alpha, output); |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | Status CLPReluLayer::validate(const ITensorInfo *input, const ITensorInfo *alpha, const ITensorInfo *output) |
| 82 | { |
| 83 | return CLArithmeticOperationKernel::validate(ArithmeticOperation::PRELU, input, alpha, output); |
| 84 | } |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 85 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 86 | void CLPReluLayer::run(ITensorPack &tensors) |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 87 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 88 | auto border_pack = select_border_input(tensors); |
| 89 | CLScheduler::get().enqueue_op(_border_handler, border_pack); |
| 90 | ICLOperator::run(tensors); |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 91 | } |
| 92 | } // namespace experimental |
| 93 | |
| 94 | struct CLPReluLayer::Impl |
| 95 | { |
| 96 | const ICLTensor *src_0{ nullptr }; |
| 97 | const ICLTensor *src_1{ nullptr }; |
| 98 | ICLTensor *dst{ nullptr }; |
| 99 | std::unique_ptr<experimental::CLPReluLayer> op{ nullptr }; |
| 100 | }; |
| 101 | |
| 102 | CLPReluLayer::CLPReluLayer() |
| 103 | : _impl(support::cpp14::make_unique<Impl>()) |
| 104 | { |
| 105 | } |
| 106 | CLPReluLayer::CLPReluLayer(CLPReluLayer &&) = default; |
| 107 | CLPReluLayer &CLPReluLayer::operator=(CLPReluLayer &&) = default; |
| 108 | CLPReluLayer::~CLPReluLayer() = default; |
| 109 | |
| 110 | void CLPReluLayer::configure(ICLTensor *input, ICLTensor *alpha, ICLTensor *output) |
| 111 | { |
| 112 | configure(CLKernelLibrary::get().get_compile_context(), input, alpha, output); |
| 113 | } |
| 114 | |
| 115 | void CLPReluLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *alpha, ICLTensor *output) |
| 116 | { |
| 117 | _impl->src_0 = input; |
| 118 | _impl->src_1 = alpha; |
| 119 | _impl->dst = output; |
| 120 | _impl->op = arm_compute::support::cpp14::make_unique<experimental::CLPReluLayer>(); |
| 121 | _impl->op->configure(compile_context, input->info(), alpha->info(), output->info()); |
| 122 | } |
| 123 | |
| 124 | Status CLPReluLayer::validate(const ITensorInfo *input, const ITensorInfo *alpha, const ITensorInfo *output) |
| 125 | { |
| 126 | return experimental::CLPReluLayer::validate(input, alpha, output); |
| 127 | } |
| 128 | |
| 129 | void CLPReluLayer::run() |
| 130 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 131 | ITensorPack pack; |
| 132 | pack.add_tensor(TensorType::ACL_SRC_0, _impl->src_0); |
| 133 | pack.add_tensor(TensorType::ACL_SRC_1, _impl->src_1); |
| 134 | pack.add_tensor(TensorType::ACL_DST, _impl->dst); |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 135 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 136 | _impl->op->run(pack); |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 137 | } |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 138 | } // namespace arm_compute |