blob: 78320cec44aa95e2380bea7efddf30abb7f2b880 [file] [log] [blame]
Michele Di Giorgiod556d7b2020-10-27 10:56:31 +00001/*
ramelg01c827e992022-04-08 03:52:28 +01002 * Copyright (c) 2021-2022 Arm Limited.
Michele Di Giorgiod556d7b2020-10-27 10:56:31 +00003 *
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#pragma once
25
26#include "pooling.hpp"
27
28#include <cstddef>
29#include <functional>
30#include <cstring>
31
32namespace arm_conv {
33namespace pooling {
34
35template <typename TInput, typename TOutput, class OutputStage = Nothing>
36struct PoolingImplementation
37{
38 const PoolingMethod method;
39 const char * name;
40 std::function<bool(const PoolingArgs &, const OutputStage &)> is_supported;
41 std::function<uint64_t(const PoolingArgs &, const OutputStage &)> cycle_estimate;
ramelg01c827e992022-04-08 03:52:28 +010042 std::function<PoolingCommon<TInput, TOutput> *(const PoolingArgs &, const OutputStage &)> initialise;
Michele Di Giorgiod556d7b2020-10-27 10:56:31 +000043
44 bool get_is_supported(const PoolingArgs &args, const OutputStage &os) const
45 {
46 return (is_supported == nullptr) ? true : is_supported(args, os);
47 }
48
49 uint64_t get_cycle_estimate(const PoolingArgs &args, const OutputStage &os) const
50 {
51 return (cycle_estimate == nullptr) ? 0 : cycle_estimate(args, os);
52 }
53
ramelg01c827e992022-04-08 03:52:28 +010054 PoolingCommon<TInput, TOutput> *get_instance(const PoolingArgs &args, const OutputStage &os) const
Michele Di Giorgiod556d7b2020-10-27 10:56:31 +000055 {
56 return initialise(args, os);
57 }
58};
59
60template <typename TInput, typename TOutput, class OutputStage = Nothing>
61const PoolingImplementation<TInput, TOutput, OutputStage> *pooling_implementation_list();
62
63template <typename TInput, typename TOutput, class OutputStage = Nothing>
64bool find_implementation(
65 const PoolingArgs &args,
66 const OutputStage &os,
67 const PoolingImplementation<TInput, TOutput, OutputStage> * &selected
68)
69{
70 // For now, return the first valid implementation
71 const auto *impl = pooling_implementation_list<TInput, TOutput, OutputStage>();
72 for (; impl->method != PoolingMethod::DEFAULT; impl++)
73 {
74 if (args.config != nullptr)
75 {
76 // Apply filters provided by the configuration
77 const auto cfg = args.config;
78
79 if (cfg->filter != "" && !std::strstr(impl->name, cfg->filter.c_str()))
80 {
81 continue;
82 }
83 }
84
85 if (impl->get_is_supported(args, os))
86 {
87 selected = impl;
88 return true;
89 }
90 }
91 return false;
92}
93
94template <typename TInput, typename TOutput, class OutputStage>
ramelg01c827e992022-04-08 03:52:28 +010095UniquePoolingCommon<TInput, TOutput> pooling(const PoolingArgs &args, const OutputStage &os)
Michele Di Giorgiod556d7b2020-10-27 10:56:31 +000096{
97 const PoolingImplementation<TInput, TOutput, OutputStage> *impl = nullptr;
98 const bool success = find_implementation<TInput, TOutput, OutputStage>(args, os, impl);
ramelg01c827e992022-04-08 03:52:28 +010099 return UniquePoolingCommon<TInput, TOutput>(success ? impl->get_instance(args, os) : nullptr);
100}
101
102template <class Strategy>
103bool is_supported(const PoolingArgs &args, const Nothing &)
104{
105 return ((args.pool_type == Strategy::pooling_type) &&
106 (args.pool_window.rows == Strategy::pool_rows) &&
107 (args.pool_window.cols == Strategy::pool_cols) &&
108 (args.pool_stride.rows == Strategy::stride_rows) &&
109 (args.pool_stride.cols == Strategy::stride_cols));
Michele Di Giorgiod556d7b2020-10-27 10:56:31 +0000110}
111
112} // namespace pooling
113} // namespace arm_conv