Compute Library
 22.08
depthwise_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 
25 #pragma once
26 
27 #include "depthwise.hpp"
28 
29 #include <cstddef>
30 #include <functional>
31 
32 using arm_gemm::Nothing;
33 
34 namespace arm_conv {
35 namespace depthwise {
36 
37 template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing>
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 
62 template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing>
64 
65 template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing>
67  const DepthwiseArgs &args,
68  const OutputStage &os,
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 
108 template <typename TInput, typename TWeight, typename TOutput, class OutputStage>
109 std::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
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 
134 template <typename TInput, typename TWeight, typename TOutput, class OutputStage>
135 UniqueDepthwiseCommon<TInput, TWeight, TOutput> depthwise(const DepthwiseArgs &args, const OutputStage &os)
136 {
138  const bool success = find_implementation<TInput, TWeight, TOutput, OutputStage>(args, os, impl);
139 
140  if(success)
141  {
142  auto i = impl->get_instance(args, os);
143  i->set_name(impl->name);
144  return UniqueDepthwiseCommon<TInput, TWeight, TOutput>(i);
145  }
146  return nullptr;
147 }
148 
149 } // namespace depthwise
150 } // namespace arm_conv
std::function< bool(const DepthwiseArgs &, const OutputStage &)> is_supported
const DepthwiseImplementation< float > * depthwise_implementation_list()
std::function< DepthwiseCommon< TInput, TWeight, TOutput > *(const DepthwiseArgs &, const OutputStage &)> initialise
std::function< uint64_t(const DepthwiseArgs &, const OutputStage &)> cycle_estimate
bool find_implementation(const DepthwiseArgs &args, const OutputStage &os, const DepthwiseImplementation< TInput, TWeight, TOutput, OutputStage > *&selected)
bool get_is_supported(const DepthwiseArgs &args, const OutputStage &os) const
std::vector< KernelDescription > get_compatible_kernels(const DepthwiseArgs &args, const OutputStage &os)
UniqueDepthwiseCommon< TInput, TWeight, TOutput > depthwise(const DepthwiseArgs &args, const OutputStage &os)
DepthwiseCommon< TInput, TWeight, TOutput > * get_instance(const DepthwiseArgs &args, const OutputStage &os) const
template UniqueDepthwiseCommon< float > depthwise(const DepthwiseArgs &, const Nothing &)
uint64_t get_cycle_estimate(const DepthwiseArgs &args, const OutputStage &os) const