| 1 |
/* |
| 2 |
* Copyright (c) 2021-2023 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> |
| 36 |
struct 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> *(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 |
/** |
| 61 |
* \relates PoolingImplementation |
| 62 |
*/ |
| 63 |
template <typename TInput, typename TOutput, class OutputStage = Nothing> |
| 64 |
const PoolingImplementation<TInput, TOutput, OutputStage> *pooling_implementation_list(); |
| 65 |
|
| 66 |
template <typename TInput, typename TOutput, class OutputStage = Nothing> |
| 67 |
bool find_implementation( |
| 68 |
const PoolingArgs &args, |
| 69 |
const OutputStage &os, |
| 70 |
const PoolingImplementation<TInput, TOutput, OutputStage> * &selected |
| 71 |
) |
| 72 |
{ |
| 73 |
// For now, return the first valid implementation |
| 74 |
const auto *impl = pooling_implementation_list<TInput, TOutput, OutputStage>(); |
| 75 |
for (; impl->method != PoolingMethod::DEFAULT; impl++) |
| 76 |
{ |
| 77 |
if (args.config != nullptr) |
| 78 |
{ |
| 79 |
// Apply filters provided by the configuration |
| 80 |
const auto cfg = args.config; |
| 81 |
|
| 82 |
if (cfg->filter != "" && !std::strstr(impl->name, cfg->filter.c_str())) |
| 83 |
{ |
| 84 |
continue; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (impl->get_is_supported(args, os)) |
| 89 |
{ |
| 90 |
selected = impl; |
| 91 |
return true; |
| 92 |
} |
| 93 |
} |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
template <typename TInput, typename TOutput, class OutputStage> |
| 98 |
UniquePoolingCommon<TInput, TOutput> pooling(const PoolingArgs &args, const OutputStage &os) |
| 99 |
{ |
| 100 |
const PoolingImplementation<TInput, TOutput, OutputStage> *impl = nullptr; |
| 101 |
const bool success = find_implementation<TInput, TOutput, OutputStage>(args, os, impl); |
| 102 |
return UniquePoolingCommon<TInput, TOutput>(success ? impl->get_instance(args, os) : nullptr); |
| 103 |
} |
| 104 |
|
| 105 |
template <class Strategy> |
| 106 |
bool is_supported(const PoolingArgs &args, const Nothing &) |
| 107 |
{ |
| 108 |
return ((args.pool_type == Strategy::pooling_type) && |
| 109 |
(args.pool_window.rows == Strategy::pool_rows) && |
| 110 |
(args.pool_window.cols == Strategy::pool_cols) && |
| 111 |
(args.pool_stride.rows == Strategy::stride_rows) && |
| 112 |
(args.pool_stride.cols == Strategy::stride_cols)); |
| 113 |
} |
| 114 |
|
| 115 |
} // namespace pooling |
| 116 |
} // namespace arm_conv |