CUDNN Frontend API  8.3.0
cudnn_frontend_EngineFallbackList.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #pragma once
24 
25 #include <cudnn.h>
26 #include <numeric>
27 
28 namespace cudnn_frontend {
29 
30 auto static get_fallback_engine_list(cudnnBackendDescriptorType_t mode, const std::string &opGraphTag) -> std::vector<int> {
31  auto major_version = cudnnGetVersion() / 1000;
32 
33  auto minor_version = (cudnnGetVersion() / 100) % 10;
34  if (major_version >= 8) {
35  if (minor_version <= 2) {
40  if (mode == CUDNN_BACKEND_OPERATION_CONVOLUTION_FORWARD_DESCRIPTOR) {
41  if (opGraphTag.find("bias") == std::string::npos) {
42  std::vector<int> engine_list(50);
43  std::iota(engine_list.begin(), engine_list.end(), 0);
44  return engine_list;
45  } else {
46  return {11,0};
47  }
48  } else if (mode == CUDNN_BACKEND_OPERATION_CONVOLUTION_BACKWARD_DATA_DESCRIPTOR) {
49  std::vector<int> engine_list(61);
50  std::iota(engine_list.begin(), engine_list.end(), 0);
51  return engine_list;
52  } else if (mode == CUDNN_BACKEND_OPERATION_CONVOLUTION_BACKWARD_FILTER_DESCRIPTOR) {
53  return {0, 1, 20};
54  } else {
55  return {};
56  }
57  } else {
58  if (mode == CUDNN_BACKEND_OPERATION_CONVOLUTION_FORWARD_DESCRIPTOR) {
59  if (opGraphTag.find("bias") == std::string::npos) {
60  return {0, 1, 28};
61  } else {
62  return {};
63  }
64  } else if (mode == CUDNN_BACKEND_OPERATION_CONVOLUTION_BACKWARD_DATA_DESCRIPTOR) {
65  return {0, 1, 25};
66  } else if (mode == CUDNN_BACKEND_OPERATION_CONVOLUTION_BACKWARD_FILTER_DESCRIPTOR) {
67  return {0, 1, 20};
68  } else {
69  return {};
70  }
71  }
72  } else {
73  return {};
74  }
75 }
76 
78  public:
80 
81  std::string
82  describe() const override {
83  std::stringstream ss;
84  ss << "CUDNN_BACKEND_FALLBACK ENGINES :";
85  return ss.str();
86  }
87 
88  auto
89  getFallbackList() -> std::vector<ManagedOpaqueDescriptor> & {
90  return m_engine_configs;
91  }
92 
93  ~EngineFallbackList_v8() = default;
94 
96  : BackendDescriptor(from.get_desc(), from.get_status(), from.get_error()),
97  opGraph(from.opGraph),
98  mode(from.mode),
99  num_ops(from.num_ops),
100  opGraphTag(from.opGraphTag) {
101  m_engine_configs.swap(from.m_engine_configs);
102  }
103 
104  private:
105  EngineFallbackList_v8() = default;
108  operator=(EngineFallbackList_v8 const &) = delete;
109 
111  cudnnBackendDescriptorType_t mode;
112  uint64_t num_ops;
113  std::vector<ManagedOpaqueDescriptor> m_engine_configs;
114  std::string opGraphTag;
115 };
116 
121  public:
126  auto
129  m_fallback_list.opGraph = opGraph_.get_desc();
130  m_fallback_list.opGraphTag = opGraph_.getTag();
131  m_fallback_list.num_ops = opGraph_.getOpCount();
132  return *this;
133  }
134  auto
135  setOperation(cudnnBackendDescriptorType_t mode) -> EngineFallbackListBuilder_v8 & {
136  m_fallback_list.mode = mode;
137  return *this;
138  }
144  build() {
145  if (m_fallback_list.opGraph == nullptr) {
146  set_error_and_throw_exception(&m_fallback_list,
147  CUDNN_STATUS_BAD_PARAM,
148  "CUDNN_BACKEND_ENGINEHEUR_DESCRIPTOR: Check and Set the "
149  "CUDNN_ATTR_ENGINEHEUR_OPERATION_GRAPH field for heuristic");
150  return std::move(m_fallback_list);
151  };
152  auto fallback_engine_list = get_fallback_engine_list(m_fallback_list.mode, m_fallback_list.opGraphTag);
153  for (std::uint32_t i = 0; i < fallback_engine_list.size(); i++) {
154 #ifndef NV_CUDNN_DISABLE_EXCEPTION
155  try {
156 #endif
157  auto engine = cudnn_frontend::EngineBuilder_v8()
158  .setGlobalEngineIdx(fallback_engine_list[i])
159  .setOperationGraph(m_fallback_list.opGraph)
160  .build();
161  auto engine_config = cudnn_frontend::EngineConfigBuilder_v8().setEngine(engine).build();
162  m_fallback_list.m_engine_configs.emplace_back(engine_config.get_desc());
163 #ifndef NV_CUDNN_DISABLE_EXCEPTION
164  } catch (cudnn_frontend::cudnnException &e) {
165  continue;
166  }
167 #endif
168  }
169  getLogger() << "[cudnn_frontend] " << m_fallback_list << std::endl;
170  return std::move(m_fallback_list);
171  }
172 
173  explicit EngineFallbackListBuilder_v8() = default;
174  ~EngineFallbackListBuilder_v8() = default;
178  operator=(EngineFallbackListBuilder_v8 const &) = delete;
179 
180  private:
182 };
183 }
static auto get_fallback_engine_list(cudnnBackendDescriptorType_t mode, const std::string &opGraphTag) -> std::vector< int >
ConditionalStreamer & getLogger()
static void set_error_and_throw_exception(BackendDescriptor const *desc, cudnnStatus_t status, const char *message)
auto setGlobalEngineIdx(int64_t idx_) -> EngineBuilder_v8 &
Set engine index for the engine.
std::string describe() const override
Return a string describing the backend Descriptor.
ManagedOpaqueDescriptor get_desc() const
Returns a copy of underlying managed descriptor.
auto setEngine(Engine_v8 const &engine_) -> EngineConfigBuilder_v8 &
Set engine for the EngineConfig_v8.
auto setOperation(cudnnBackendDescriptorType_t mode) -> EngineFallbackListBuilder_v8 &
cudnnStatus_t get_status() const
Current status of the descriptor.
std::shared_ptr< OpaqueBackendPointer > ManagedOpaqueDescriptor
const char * get_error() const
Diagonistic error message if any.
auto setOperationGraph(OperationGraph_v8 &opGraph_) -> EngineFallbackListBuilder_v8 &
Set operationGraph for the engine (opGraph is not destroyed)
EngineFallbackList_v8 & operator=(EngineFallbackList_v8 const &)=delete
auto getFallbackList() -> std::vector< ManagedOpaqueDescriptor > &
std::vector< ManagedOpaqueDescriptor > m_engine_configs