blob: 3d968b84e5fe572794b04d8078b08055f6d55233 [file] [log] [blame]
Michele Di Giorgiod556d7b2020-10-27 10:56:31 +00001/*
2 * Copyright (c) 2021 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#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;
42 std::function<PoolingCommon<TInput, TOutput, OutputStage> *(const PoolingArgs &, const OutputStage &)> initialise;
43
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
54 PoolingCommon<TInput, TOutput, OutputStage> *get_instance(const PoolingArgs &args, const OutputStage &os) const
55 {
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>
95UniquePoolingCommon<TInput, TOutput, OutputStage> pooling(const PoolingArgs &args, const OutputStage &os)
96{
97 const PoolingImplementation<TInput, TOutput, OutputStage> *impl = nullptr;
98 const bool success = find_implementation<TInput, TOutput, OutputStage>(args, os, impl);
99 return UniquePoolingCommon<TInput, TOutput, OutputStage>(success ? impl->get_instance(args, os) : nullptr);
100}
101
102} // namespace pooling
103} // namespace arm_conv