blob: 63579155cb107b0228a5bcb6d6815cc2f1d659ed [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/PoolingLayer.h"
25
26#include "arm_compute/runtime/CL/CLTensor.h"
27#include "arm_compute/runtime/CL/functions/CLPoolingLayer.h"
28#include "arm_compute/runtime/NEON/functions/NEPoolingLayer.h"
29#include "arm_compute/runtime/Tensor.h"
30#include "support/ToolchainSupport.h"
31#include "utils/TypePrinter.h"
32
33using namespace arm_compute::graph;
34
35namespace
36{
Georgios Pinitasff421f22017-10-04 16:53:58 +010037template <typename PoolingType, typename TensorType, TargetHint target_hint>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010038std::unique_ptr<arm_compute::IFunction> instantiate_function(arm_compute::ITensor *input, arm_compute::ITensor *output, const PoolingLayerInfo &pool_info)
Anthony Barbier2a07e182017-08-04 18:20:27 +010039{
40 auto pool = arm_compute::support::cpp14::make_unique<PoolingType>();
41 pool->configure(
42 dynamic_cast<TensorType *>(input),
43 dynamic_cast<TensorType *>(output),
44 pool_info);
45
46 return std::move(pool);
47}
48
Georgios Pinitasff421f22017-10-04 16:53:58 +010049template <TargetHint target_hint>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010050std::unique_ptr<arm_compute::IFunction> instantiate(arm_compute::ITensor *input, arm_compute::ITensor *output, const PoolingLayerInfo &pool_info);
Anthony Barbier2a07e182017-08-04 18:20:27 +010051
52template <>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010053std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::OPENCL>(arm_compute::ITensor *input, arm_compute::ITensor *output, const PoolingLayerInfo &pool_info)
Anthony Barbier2a07e182017-08-04 18:20:27 +010054{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010055 return instantiate_function<arm_compute::CLPoolingLayer, arm_compute::ICLTensor, TargetHint::OPENCL>(input, output, pool_info);
Anthony Barbier2a07e182017-08-04 18:20:27 +010056}
57
58template <>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010059std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::NEON>(arm_compute::ITensor *input, arm_compute::ITensor *output, const PoolingLayerInfo &pool_info)
Anthony Barbier2a07e182017-08-04 18:20:27 +010060{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010061 return instantiate_function<arm_compute::NEPoolingLayer, arm_compute::ITensor, TargetHint::NEON>(input, output, pool_info);
Anthony Barbier2a07e182017-08-04 18:20:27 +010062}
63} // namespace
64
65PoolingLayer::PoolingLayer(const PoolingLayerInfo pool_info)
66 : _pool_info(pool_info)
67{
68}
69
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010070std::unique_ptr<arm_compute::IFunction> PoolingLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output)
Anthony Barbier2a07e182017-08-04 18:20:27 +010071{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010072 ARM_COMPUTE_ERROR_ON(input == nullptr || input->tensor() == nullptr);
73 ARM_COMPUTE_ERROR_ON(output == nullptr || output->tensor() == nullptr);
74
Anthony Barbier2a07e182017-08-04 18:20:27 +010075 std::unique_ptr<arm_compute::IFunction> func;
Georgios Pinitasff421f22017-10-04 16:53:58 +010076 _target_hint = ctx.hints().target_hint();
Anthony Barbier2a07e182017-08-04 18:20:27 +010077
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010078 arm_compute::ITensor *in = input->tensor();
79 arm_compute::ITensor *out = output->tensor();
80
Georgios Pinitasff421f22017-10-04 16:53:58 +010081 if(_target_hint == TargetHint::OPENCL)
Anthony Barbier2a07e182017-08-04 18:20:27 +010082 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010083 func = instantiate<TargetHint::OPENCL>(in, out, _pool_info);
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010084 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLPoolingLayer");
Anthony Barbier2a07e182017-08-04 18:20:27 +010085 }
86 else
87 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010088 func = instantiate<TargetHint::NEON>(in, out, _pool_info);
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010089 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEPoolingLayer");
Anthony Barbier2a07e182017-08-04 18:20:27 +010090 }
91
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010092 ARM_COMPUTE_LOG_GRAPH_INFO(" Data Type: " << in->info()->data_type()
93 << " Input shape: " << in->info()->tensor_shape()
94 << " Output shape: " << out->info()->tensor_shape()
95 << " Pooling info: " << _pool_info << std::endl);
Michalis Spyroue4720822017-10-02 17:44:52 +010096
Anthony Barbier2a07e182017-08-04 18:20:27 +010097 return func;
98}