Compute Library
 23.08
depthwise_implementation.hpp
Go to the documentation of this file.
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 
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  auto impl = initialise(args, os);
59  impl->set_name(std::string(name));
60  return impl;
61  }
62 };
63 
64 /**
65  * \relates DepthwiseImplementation
66  */
67 template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing>
68 const DepthwiseImplementation<TInput, TWeight, TOutput, OutputStage> *depthwise_implementation_list();
69 
70 template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing>
72  const DepthwiseArgs &args,
73  const OutputStage &os,
75 )
76 {
77  selected = nullptr;
78  uint64_t best_cycle_estimate = UINT64_MAX;
79 
80  const auto *impl = depthwise_implementation_list<TInput, TWeight, TOutput, OutputStage>();
81  for (; impl->method != DepthwiseMethod::DEFAULT; impl++)
82  {
83  const bool has_cfg = (args.config != nullptr);
84  const auto &cfg = args.config;
85 
86  if (
87  !impl->get_is_supported(args, os) || // Problem is unsupported
88  (has_cfg && cfg->method != DepthwiseMethod::DEFAULT && cfg->method != impl->method) ||
89  (has_cfg && cfg->filter != "" && !std::strstr(impl->name, cfg->filter.c_str()))
90  )
91  {
92  continue;
93  }
94 
95  const auto cycle_estimate = impl->get_cycle_estimate(args, os);
96 
97  if (cycle_estimate == 0)
98  {
99  selected = impl;
100  break;
101  }
102 
103  if (selected == nullptr || cycle_estimate < best_cycle_estimate)
104  {
105  selected = impl;
106  best_cycle_estimate = cycle_estimate;
107  }
108  }
109 
110  return (selected != nullptr);
111 }
112 
113 template <typename TInput, typename TWeight, typename TOutput, class OutputStage>
114 std::vector<KernelDescription> get_compatible_kernels(const DepthwiseArgs &args, const OutputStage &os)
115 {
116  std::vector<KernelDescription> kerns;
117 
118  // Find the default implementation so we can flag it accordingly
120  find_implementation<TInput, TWeight, TOutput, OutputStage>(args, os, default_impl);
121 
122  for (auto impl = depthwise_implementation_list<TInput, TWeight, TOutput, OutputStage>();
123  impl->method != DepthwiseMethod::DEFAULT; impl++)
124  {
125  if (!impl->get_is_supported(args, os))
126  {
127  continue;
128  }
129 
130  kerns.emplace_back(
131  impl->method, impl->name, impl == default_impl,
132  impl->get_cycle_estimate(args, os)
133  );
134  }
135 
136  return kerns;
137 }
138 
139 template <typename TInput, typename TWeight, typename TOutput, class OutputStage>
140 UniqueDepthwiseCommon<TInput, TWeight, TOutput> depthwise(const DepthwiseArgs &args, const OutputStage &os)
141 {
143  const bool success = find_implementation<TInput, TWeight, TOutput, OutputStage>(args, os, impl);
144  return UniqueDepthwiseCommon<TInput, TWeight, TOutput>(success ? impl->get_instance(args, os) : nullptr);
145 }
146 
147 } // namespace depthwise
148 } // namespace arm_conv
GemmTuner.args
args
Definition: GemmTuner.py:679
arm_conv::depthwise::depthwise
template UniqueDepthwiseCommon< float > depthwise(const DepthwiseArgs &, const Nothing &)
arm_conv::depthwise::find_implementation
bool find_implementation(const DepthwiseArgs &args, const OutputStage &os, const DepthwiseImplementation< TInput, TWeight, TOutput, OutputStage > *&selected)
Definition: depthwise_implementation.hpp:71
arm_conv::depthwise::DepthwiseImplementation::get_cycle_estimate
uint64_t get_cycle_estimate(const DepthwiseArgs &args, const OutputStage &os) const
Definition: depthwise_implementation.hpp:51
arm_conv::depthwise::DepthwiseImplementation::initialise
std::function< DepthwiseCommon< TInput, TWeight, TOutput > *(const DepthwiseArgs &, const OutputStage &)> initialise
Definition: depthwise_implementation.hpp:44
arm_conv::depthwise::DepthwiseImplementation::method
const DepthwiseMethod method
Definition: depthwise_implementation.hpp:40
arm_gemm::Nothing
Definition: arm_gemm.hpp:211
arm_conv::depthwise::DepthwiseImplementation::get_instance
DepthwiseCommon< TInput, TWeight, TOutput > * get_instance(const DepthwiseArgs &args, const OutputStage &os) const
Definition: depthwise_implementation.hpp:56
arm_conv::depthwise::DepthwiseImplementation::cycle_estimate
std::function< uint64_t(const DepthwiseArgs &, const OutputStage &)> cycle_estimate
Definition: depthwise_implementation.hpp:43
arm_conv::depthwise::DepthwiseImplementation
Definition: depthwise_implementation.hpp:38
arm_conv::depthwise::DepthwiseImplementation::is_supported
std::function< bool(const DepthwiseArgs &, const OutputStage &)> is_supported
Definition: depthwise_implementation.hpp:42
arm_conv::depthwise::depthwise
UniqueDepthwiseCommon< TInput, TWeight, TOutput > depthwise(const DepthwiseArgs &args, const OutputStage &os)
Definition: depthwise_implementation.hpp:140
arm_conv::depthwise::DepthwiseImplementation::name
const char * name
Definition: depthwise_implementation.hpp:41
arm_conv::depthwise::depthwise_implementation_list
const DepthwiseImplementation< float > * depthwise_implementation_list()
Definition: depthwise_fp32.cpp:530
arm_conv::depthwise::get_compatible_kernels
std::vector< KernelDescription > get_compatible_kernels(const DepthwiseArgs &args, const OutputStage &os)
Definition: depthwise_implementation.hpp:114
arm_conv
Definition: addressing.cpp:30
arm_conv::depthwise::DepthwiseImplementation::get_is_supported
bool get_is_supported(const DepthwiseArgs &args, const OutputStage &os) const
Definition: depthwise_implementation.hpp:46