blob: 0665fa3a2990e0427d3157e9d21eb67de80c786f [file] [log] [blame]
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +00001/*
ramelg018a164882022-04-07 02:42:52 +01002 * Copyright (c) 2021-2022 Arm Limited.
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +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
25#pragma once
26
ramelg018a164882022-04-07 02:42:52 +010027#include "depthwise.hpp"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000028
29#include <cstddef>
30#include <functional>
31
32using arm_gemm::Nothing;
33
34namespace arm_conv {
35namespace depthwise {
36
37template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing>
38struct DepthwiseImplementation
39{
40 const DepthwiseMethod method;
41 const char *name;
42 std::function<bool(const DepthwiseArgs &, const OutputStage &)> is_supported;
43 std::function<uint64_t(const DepthwiseArgs &, const OutputStage &)> cycle_estimate;
44 std::function<DepthwiseCommon<TInput, TWeight, TOutput> *(const DepthwiseArgs &, const OutputStage &)> initialise;
45
46 bool get_is_supported(const DepthwiseArgs &args, const OutputStage &os) const
47 {
48 return (is_supported == nullptr) ? true : is_supported(args, os);
49 }
50
51 uint64_t get_cycle_estimate(const DepthwiseArgs &args, const OutputStage &os) const
52 {
53 return (cycle_estimate == nullptr) ? 0 : cycle_estimate(args, os);
54 }
55
56 DepthwiseCommon<TInput, TWeight, TOutput> *get_instance(const DepthwiseArgs &args, const OutputStage &os) const
57 {
58 return initialise(args, os);
59 }
60};
61
62template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing>
63const DepthwiseImplementation<TInput, TWeight, TOutput, OutputStage> *depthwise_implementation_list();
64
65template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing>
66bool find_implementation(
67 const DepthwiseArgs &args,
68 const OutputStage &os,
69 const DepthwiseImplementation<TInput, TWeight, TOutput, OutputStage> * &selected
70)
71{
72 selected = nullptr;
73 uint64_t best_cycle_estimate = UINT64_MAX;
74
75 const auto *impl = depthwise_implementation_list<TInput, TWeight, TOutput, OutputStage>();
76 for (; impl->method != DepthwiseMethod::DEFAULT; impl++)
77 {
78 const bool has_cfg = (args.config != nullptr);
79 const auto &cfg = args.config;
80
81 if (
82 !impl->get_is_supported(args, os) || // Problem is unsupported
83 (has_cfg && cfg->method != DepthwiseMethod::DEFAULT && cfg->method != impl->method) ||
84 (has_cfg && cfg->filter != "" && !std::strstr(impl->name, cfg->filter.c_str()))
85 )
86 {
87 continue;
88 }
89
90 const auto cycle_estimate = impl->get_cycle_estimate(args, os);
91
92 if (cycle_estimate == 0)
93 {
94 selected = impl;
95 break;
96 }
97
98 if (selected == nullptr || cycle_estimate < best_cycle_estimate)
99 {
100 selected = impl;
101 best_cycle_estimate = cycle_estimate;
102 }
103 }
104
105 return (selected != nullptr);
106}
107
108template <typename TInput, typename TWeight, typename TOutput, class OutputStage>
109std::vector<KernelDescription> get_compatible_kernels(const DepthwiseArgs &args, const OutputStage &os)
110{
111 std::vector<KernelDescription> kerns;
112
113 // Find the default implementation so we can flag it accordingly
114 const DepthwiseImplementation<TInput, TWeight, TOutput, OutputStage> *default_impl;
115 find_implementation<TInput, TWeight, TOutput, OutputStage>(args, os, default_impl);
116
117 for (auto impl = depthwise_implementation_list<TInput, TWeight, TOutput, OutputStage>();
118 impl->method != DepthwiseMethod::DEFAULT; impl++)
119 {
120 if (!impl->get_is_supported(args, os))
121 {
122 continue;
123 }
124
125 kerns.emplace_back(
126 impl->method, impl->name, impl == default_impl,
127 impl->get_cycle_estimate(args, os)
128 );
129 }
130
131 return kerns;
132}
133
134template <typename TInput, typename TWeight, typename TOutput, class OutputStage>
135UniqueDepthwiseCommon<TInput, TWeight, TOutput> depthwise(const DepthwiseArgs &args, const OutputStage &os)
136{
137 const DepthwiseImplementation<TInput, TWeight, TOutput, OutputStage> *impl = nullptr;
138 const bool success = find_implementation<TInput, TWeight, TOutput, OutputStage>(args, os, impl);
ramelg018a164882022-04-07 02:42:52 +0100139 return UniqueDepthwiseCommon<TInput, TWeight, TOutput>(success ? impl->get_instance(args, os) : nullptr);
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000140}
141
142} // namespace depthwise
143} // namespace arm_conv