blob: e03bd13284ca04621f8fd081f73349adf5ba6db9 [file] [log] [blame]
giuros011e6e1b82019-05-14 16:12:53 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
giuros011e6e1b82019-05-14 16:12:53 +01003 *
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 Spyrouad7515d2020-07-24 00:02:23 +010027#include "arm_compute/runtime/CL/CLScheduler.h"
giuros011e6e1b82019-05-14 16:12:53 +010028#include "arm_compute/runtime/CL/functions/CLPReluLayer.h"
Matthew Bentham92046462020-03-07 22:15:55 +000029#include "support/MemorySupport.h"
giuros011e6e1b82019-05-14 16:12:53 +010030
31namespace arm_compute
32{
33namespace
34{
Michalis Spyrouad7515d2020-07-24 00:02:23 +010035void configure_border_handler(const CLCompileContext &compile_context, CLFillBorderKernel &border_handler, BorderSize border_size, ITensorInfo *input1, ITensorInfo *input2, const ITensorInfo *output)
giuros011e6e1b82019-05-14 16:12:53 +010036{
Michalis Spyrouad7515d2020-07-24 00:02:23 +010037 if(output->dimension(0) > 1)
giuros011e6e1b82019-05-14 16:12:53 +010038 {
Michalis Spyrouad7515d2020-07-24 00:02:23 +010039 ITensorInfo *broadcasted_info = (input1->dimension(0) == 1) ? input1 : input2;
giuros011e6e1b82019-05-14 16:12:53 +010040
Michalis Spyrouad7515d2020-07-24 00:02:23 +010041 if(broadcasted_info->dimension(0) == 1)
giuros011e6e1b82019-05-14 16:12:53 +010042 {
Manuel Bottini2b84be52020-04-08 10:15:51 +010043 border_handler.configure(compile_context, broadcasted_info, border_size, BorderMode::REPLICATE);
giuros011e6e1b82019-05-14 16:12:53 +010044 }
45 }
46}
Georgios Pinitas0499dff2020-07-31 22:21:38 +010047
48ITensorPack select_border_input(ITensorPack &tensors)
Michalis Spyrouad7515d2020-07-24 00:02:23 +010049{
Georgios Pinitas0499dff2020-07-31 22:21:38 +010050 ITensorPack pack;
51 if(tensors.get_tensor(TensorType::ACL_DST)->info()->dimension(0) > 1)
Michalis Spyrouad7515d2020-07-24 00:02:23 +010052 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +010053 if(tensors.get_const_tensor(TensorType::ACL_SRC_1)->info()->dimension(0) == 1)
Michalis Spyrouad7515d2020-07-24 00:02:23 +010054 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +010055 pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(TensorType::ACL_SRC_1));
Michalis Spyrouad7515d2020-07-24 00:02:23 +010056 }
57 else
58 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +010059 pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(TensorType::ACL_SRC_0));
Michalis Spyrouad7515d2020-07-24 00:02:23 +010060 }
61 }
Georgios Pinitas0499dff2020-07-31 22:21:38 +010062 return pack;
Michalis Spyrouad7515d2020-07-24 00:02:23 +010063}
giuros011e6e1b82019-05-14 16:12:53 +010064} // namespace
65
Michalis Spyrouad7515d2020-07-24 00:02:23 +010066namespace experimental
giuros011e6e1b82019-05-14 16:12:53 +010067{
Michalis Spyrouad7515d2020-07-24 00:02:23 +010068CLPReluLayer::CLPReluLayer()
69 : _border_handler()
70{
Manuel Bottini2b84be52020-04-08 10:15:51 +010071}
72
Michalis Spyrouad7515d2020-07-24 00:02:23 +010073void CLPReluLayer::configure(const CLCompileContext &compile_context, ITensorInfo *input, ITensorInfo *alpha, ITensorInfo *output)
Manuel Bottini2b84be52020-04-08 10:15:51 +010074{
giuros011e6e1b82019-05-14 16:12:53 +010075 auto k = arm_compute::support::cpp14::make_unique<CLArithmeticOperationKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +010076 k->configure(compile_context, ArithmeticOperation::PRELU, input, alpha, output);
giuros011e6e1b82019-05-14 16:12:53 +010077 _kernel = std::move(k);
Manuel Bottini2b84be52020-04-08 10:15:51 +010078 configure_border_handler(compile_context, _border_handler, _kernel->border_size(), input, alpha, output);
giuros011e6e1b82019-05-14 16:12:53 +010079}
80
81Status CLPReluLayer::validate(const ITensorInfo *input, const ITensorInfo *alpha, const ITensorInfo *output)
82{
83 return CLArithmeticOperationKernel::validate(ArithmeticOperation::PRELU, input, alpha, output);
84}
Michalis Spyrouad7515d2020-07-24 00:02:23 +010085
Georgios Pinitas0499dff2020-07-31 22:21:38 +010086void CLPReluLayer::run(ITensorPack &tensors)
Michalis Spyrouad7515d2020-07-24 00:02:23 +010087{
Georgios Pinitas0499dff2020-07-31 22:21:38 +010088 auto border_pack = select_border_input(tensors);
89 CLScheduler::get().enqueue_op(_border_handler, border_pack);
90 ICLOperator::run(tensors);
Michalis Spyrouad7515d2020-07-24 00:02:23 +010091}
92} // namespace experimental
93
94struct 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
102CLPReluLayer::CLPReluLayer()
103 : _impl(support::cpp14::make_unique<Impl>())
104{
105}
106CLPReluLayer::CLPReluLayer(CLPReluLayer &&) = default;
107CLPReluLayer &CLPReluLayer::operator=(CLPReluLayer &&) = default;
108CLPReluLayer::~CLPReluLayer() = default;
109
110void CLPReluLayer::configure(ICLTensor *input, ICLTensor *alpha, ICLTensor *output)
111{
112 configure(CLKernelLibrary::get().get_compile_context(), input, alpha, output);
113}
114
115void 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
124Status CLPReluLayer::validate(const ITensorInfo *input, const ITensorInfo *alpha, const ITensorInfo *output)
125{
126 return experimental::CLPReluLayer::validate(input, alpha, output);
127}
128
129void CLPReluLayer::run()
130{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100131 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 Spyrouad7515d2020-07-24 00:02:23 +0100135
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100136 _impl->op->run(pack);
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100137}
giuros011e6e1b82019-05-14 16:12:53 +0100138} // namespace arm_compute