Compute Library
 22.11
pooling_implementation.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2022 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 
32 namespace arm_conv {
33 namespace pooling {
34 
35 template <typename TInput, typename TOutput, class OutputStage = Nothing>
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> *(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> *get_instance(const PoolingArgs &args, const OutputStage &os) const
55  {
56  return initialise(args, os);
57  }
58 };
59 
60 template <typename TInput, typename TOutput, class OutputStage = Nothing>
62 
63 template <typename TInput, typename TOutput, class OutputStage = Nothing>
65  const PoolingArgs &args,
66  const OutputStage &os,
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 
94 template <typename TInput, typename TOutput, class OutputStage>
95 UniquePoolingCommon<TInput, TOutput> pooling(const PoolingArgs &args, const OutputStage &os)
96 {
98  const bool success = find_implementation<TInput, TOutput, OutputStage>(args, os, impl);
99  return UniquePoolingCommon<TInput, TOutput>(success ? impl->get_instance(args, os) : nullptr);
100 }
101 
102 template <class Strategy>
103 bool 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));
110 }
111 
112 } // namespace pooling
113 } // namespace arm_conv
bool find_implementation(const PoolingArgs &args, const OutputStage &os, const PoolingImplementation< TInput, TOutput, OutputStage > *&selected)
std::function< uint64_t(const PoolingArgs &, const OutputStage &)> cycle_estimate
template UniquePoolingCommon< float, float > pooling(const PoolingArgs &, const Nothing &)
std::function< PoolingCommon< TInput, TOutput > *(const PoolingArgs &, const OutputStage &)> initialise
uint64_t get_cycle_estimate(const PoolingArgs &args, const OutputStage &os) const
PoolingCommon< TInput, TOutput > * get_instance(const PoolingArgs &args, const OutputStage &os) const
const PoolingImplementation< float, float > * pooling_implementation_list()
std::function< bool(const PoolingArgs &, const OutputStage &)> is_supported
bool get_is_supported(const PoolingArgs &args, const OutputStage &os) const
UniquePoolingCommon< TInput, TOutput > pooling(const PoolingArgs &args, const OutputStage &os)