CUDNN API  8
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) -> 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 == 0) {
36  if (mode == CUDNN_BACKEND_OPERATION_CONVOLUTION_FORWARD_DESCRIPTOR) {
37  return {0, 1, 28};
38  } else if (mode == CUDNN_BACKEND_OPERATION_CONVOLUTION_BACKWARD_DATA_DESCRIPTOR) {
39  return {0, 1, 25};
40  } else if (mode == CUDNN_BACKEND_OPERATION_CONVOLUTION_BACKWARD_FILTER_DESCRIPTOR) {
41  return {0, 1, 20};
42  } else {
43  return {};
44  }
45  } else if (minor_version == 1) {
46  if (mode == CUDNN_BACKEND_OPERATION_CONVOLUTION_FORWARD_DESCRIPTOR) {
47  std::vector<int> engine_list(50);
48  std::iota(engine_list.begin(), engine_list.end(), 0);
49  return engine_list;
50  } else if (mode == CUDNN_BACKEND_OPERATION_CONVOLUTION_BACKWARD_DATA_DESCRIPTOR) {
51  std::vector<int> engine_list(61);
52  std::iota(engine_list.begin(), engine_list.end(), 0);
53  return engine_list;
54  } else if (mode == CUDNN_BACKEND_OPERATION_CONVOLUTION_BACKWARD_FILTER_DESCRIPTOR) {
55  return {0, 1, 20};
56  } else {
57  return {};
58  }
59  } else {
60  return {};
61  }
62  } else {
63  return {};
64  }
65 }
66 
68  public:
70 
71  std::string
72  describe() const override {
73  std::stringstream ss;
74  ss << "CUDNN_BACKEND_FALLBACK ENGINES :";
75  return ss.str();
76  }
77 
78  auto
79  getFallbackList() -> std::vector<ManagedOpaqueDescriptor> & {
80  return m_engine_configs;
81  }
82 
83  ~EngineFallbackList_v8() = default;
84 
86  : BackendDescriptor(from.get_desc(), from.get_status(), from.get_error()),
87  mode(from.mode),
88  opGraph(from.opGraph),
89  opGraphTag(from.opGraphTag) {
90  m_engine_configs.swap(from.m_engine_configs);
91  }
92 
93  private:
94  EngineFallbackList_v8() = default;
97  operator=(EngineFallbackList_v8 const &) = delete;
98 
100  cudnnBackendDescriptorType_t mode;
101  std::vector<ManagedOpaqueDescriptor> m_engine_configs;
102  std::string opGraphTag;
103 };
104 
109  public:
114  auto
117  m_fallback_list.opGraph = opGraph_.get_desc();
118  m_fallback_list.opGraphTag = opGraph_.getTag();
119  return *this;
120  }
121  auto
122  setOperation(cudnnBackendDescriptorType_t mode) -> EngineFallbackListBuilder_v8 & {
123  m_fallback_list.mode = mode;
124  return *this;
125  }
131  build() {
132  if (m_fallback_list.opGraph == nullptr) {
133  set_error_and_throw_exception(&m_fallback_list,
134  CUDNN_STATUS_BAD_PARAM,
135  "CUDNN_BACKEND_ENGINEHEUR_DESCRIPTOR: Check and Set the "
136  "CUDNN_ATTR_ENGINEHEUR_OPERATION_GRAPH field for heuristic");
137  return std::move(m_fallback_list);
138  };
139  auto fallback_engine_list = get_fallback_engine_list(m_fallback_list.mode);
140  for (auto i = 0; i < fallback_engine_list.size(); i++) {
141 #ifndef NV_CUDNN_DISABLE_EXCEPTION
142  try {
143 #endif
144  auto engine = cudnn_frontend::EngineBuilder_v8()
145  .setGlobalEngineIdx(fallback_engine_list[i])
146  .setOperationGraph(m_fallback_list.opGraph)
147  .build();
148  auto engine_config = cudnn_frontend::EngineConfigBuilder_v8().setEngine(engine).build();
149  m_fallback_list.m_engine_configs.emplace_back(engine_config.get_desc());
150 #ifndef NV_CUDNN_DISABLE_EXCEPTION
152  continue;
153  }
154 #endif
155  }
156  return std::move(m_fallback_list);
157  }
158 
159  explicit EngineFallbackListBuilder_v8() = default;
160  ~EngineFallbackListBuilder_v8() = default;
164  operator=(EngineFallbackListBuilder_v8 const &) = delete;
165 
166  private:
168 };
169 }
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)
static auto get_fallback_engine_list(cudnnBackendDescriptorType_t mode) -> std::vector< int >
EngineFallbackList_v8 & operator=(EngineFallbackList_v8 const &)=delete
auto getFallbackList() -> std::vector< ManagedOpaqueDescriptor > &
std::vector< ManagedOpaqueDescriptor > m_engine_configs