blob: 5cd2a0bcc2a59afe08b718752dfeb19a3cd56756 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
2 * Copyright (c) 2017 ARM Limited.
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/graph/nodes/ActivationLayer.h"
25
Michalis Spyroue4720822017-10-02 17:44:52 +010026#include "arm_compute/core/Logger.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010027#include "arm_compute/runtime/CL/CLTensor.h"
28#include "arm_compute/runtime/CL/functions/CLActivationLayer.h"
29#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
30#include "arm_compute/runtime/Tensor.h"
31#include "support/ToolchainSupport.h"
32#include "utils/TypePrinter.h"
33
34using namespace arm_compute::graph;
35
36namespace
37{
Georgios Pinitasff421f22017-10-04 16:53:58 +010038template <typename ActivationType, typename TensorType, TargetHint target_hint>
Anthony Barbier2a07e182017-08-04 18:20:27 +010039std::unique_ptr<arm_compute::IFunction> instantiate_function(ITensor *input, ITensor *output, const ActivationLayerInfo &activation_info)
40{
41 auto activation = arm_compute::support::cpp14::make_unique<ActivationType>();
42 activation->configure(
43 dynamic_cast<TensorType *>(input),
44 dynamic_cast<TensorType *>(output),
45 activation_info);
46
47 return std::move(activation);
48}
49
Georgios Pinitasff421f22017-10-04 16:53:58 +010050template <TargetHint target_hint>
Anthony Barbier2a07e182017-08-04 18:20:27 +010051std::unique_ptr<arm_compute::IFunction> instantiate(ITensor *input, ITensor *output, const ActivationLayerInfo &activation_info);
52
53template <>
Georgios Pinitasff421f22017-10-04 16:53:58 +010054std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::OPENCL>(ITensor *input, ITensor *output, const ActivationLayerInfo &activation_info)
Anthony Barbier2a07e182017-08-04 18:20:27 +010055{
Georgios Pinitasff421f22017-10-04 16:53:58 +010056 return instantiate_function<arm_compute::CLActivationLayer, arm_compute::CLTensor, TargetHint::OPENCL>(input, output, activation_info);
Anthony Barbier2a07e182017-08-04 18:20:27 +010057}
58
59template <>
Georgios Pinitasff421f22017-10-04 16:53:58 +010060std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::NEON>(ITensor *input, ITensor *output, const ActivationLayerInfo &activation_info)
Anthony Barbier2a07e182017-08-04 18:20:27 +010061{
Georgios Pinitasff421f22017-10-04 16:53:58 +010062 return instantiate_function<arm_compute::NEActivationLayer, arm_compute::Tensor, TargetHint::NEON>(input, output, activation_info);
Anthony Barbier2a07e182017-08-04 18:20:27 +010063}
64} // namespace
65
66ActivationLayer::ActivationLayer(const ActivationLayerInfo activation_info)
67 : _activation_info(activation_info)
68{
69}
70
Georgios Pinitasff421f22017-10-04 16:53:58 +010071std::unique_ptr<arm_compute::IFunction> ActivationLayer::instantiate_node(GraphContext &ctx, ITensor *input, ITensor *output)
Anthony Barbier2a07e182017-08-04 18:20:27 +010072{
73 std::unique_ptr<arm_compute::IFunction> func;
Georgios Pinitasff421f22017-10-04 16:53:58 +010074 _target_hint = ctx.hints().target_hint();
Anthony Barbier2a07e182017-08-04 18:20:27 +010075
Georgios Pinitasff421f22017-10-04 16:53:58 +010076 if(_target_hint == TargetHint::OPENCL)
Anthony Barbier2a07e182017-08-04 18:20:27 +010077 {
Georgios Pinitasff421f22017-10-04 16:53:58 +010078 func = instantiate<TargetHint::OPENCL>(input, output, _activation_info);
Michalis Spyroue4720822017-10-02 17:44:52 +010079 ARM_COMPUTE_LOG("Instantiating CLActivationLayer");
Anthony Barbier2a07e182017-08-04 18:20:27 +010080 }
81 else
82 {
Georgios Pinitasff421f22017-10-04 16:53:58 +010083 func = instantiate<TargetHint::NEON>(input, output, _activation_info);
Michalis Spyroue4720822017-10-02 17:44:52 +010084 ARM_COMPUTE_LOG("Instantiating NEActivationLayer");
Anthony Barbier2a07e182017-08-04 18:20:27 +010085 }
Michalis Spyroue4720822017-10-02 17:44:52 +010086
87 ARM_COMPUTE_LOG(" Data Type: " << input->info()->data_type()
88 << " Input shape: " << input->info()->tensor_shape()
89 << " Output shape: " << output->info()->tensor_shape()
90 << " Activation function: " << _activation_info.activation()
91 << " a: " << _activation_info.a()
92 << " b: " << _activation_info.b()
93 << std::endl);
Anthony Barbier2a07e182017-08-04 18:20:27 +010094 return func;
95}