CUDNN API  8
cudnn_frontend_Engine.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 <algorithm>
26 #include <array>
27 #include <functional>
28 #include <memory>
29 #include <sstream>
30 #include <utility>
31 #include <vector>
32 
33 #include <cudnn.h>
34 #include <cudnn_backend.h>
35 
37 #include "cudnn_frontend_utils.h"
38 
39 namespace cudnn_frontend {
40 
52 class Engine_v8 : public BackendDescriptor {
53  private:
54  Engine_v8() = default;
55  Engine_v8(Engine_v8 const &) = delete;
56  Engine_v8 &
57  operator=(Engine_v8 const &) = delete;
58 
62  class Knob {
63  public:
64  Knob(cudnnBackendKnobType_t type_, int64_t max, int64_t min, int64_t stride_)
65  : knobType(type_), maxValue(max), minValue(min), stride(stride_) {}
66 
67  std::string
68  describe() const {
69  std::stringstream ss;
70  ss << "Knob:" << knobType;
71  ss << " Min: " << minValue;
72  ss << " Max: " << maxValue;
73  ss << " Stride: " << stride;
74  return ss.str();
75  }
76 
77  void
78  setChoice(uint64_t val_) {
79  choice = val_;
80  }
81 
82  const int64_t
83  getChoice() const {
84  return choice;
85  }
86 
87  const cudnnBackendKnobType_t
88  getKnobType() const {
89  return knobType;
90  }
91 
92  const int64_t
93  getMinValue() const {
94  return minValue;
95  }
96 
97  const int64_t
98  getMaxValue() const {
99  return minValue;
100  }
101 
102  const int64_t
103  getStride() const {
104  return stride;
105  }
106 
107  private:
108  cudnnBackendKnobType_t knobType = CUDNN_KNOB_TYPE_COUNTS;
109  int64_t maxValue = 0, minValue = 0, stride = 0;
110  int64_t choice = 0;
111  };
112 
114  int64_t idx = -1;
115  int64_t numKnobs = 0;
116  std::array<ManagedOpaqueDescriptor, CUDNN_KNOB_TYPE_COUNTS> bKnobs = {};
117  std::vector<Knob> knobs;
118  std::string opGraphTag;
119 
121  void
123  cudnnStatus_t status;
124  for (auto i = 0; i < numKnobs; i++) {
125  auto bKnob = bKnobs[i]->get_backend_descriptor();
126  cudnnBackendKnobType_t type;
127  int64_t maxValue, minValue, stride, elemCount;
128  status =
129  cudnnBackendGetAttribute(bKnob, CUDNN_ATTR_KNOB_INFO_TYPE, CUDNN_TYPE_KNOB_TYPE, 1, &elemCount, &type);
130  if (status != CUDNN_STATUS_SUCCESS) {
132  status,
133  "CUDNN_BACKEND_ENGINE_DESCRIPTOR: CUDNN_BACKEND_KNOB_INFO_DESCRIPTOR "
134  "GetAttribute CUDNN_ATTR_KNOB_INFO_TYPE failed");
135  }
136  status = cudnnBackendGetAttribute(
137  bKnob, CUDNN_ATTR_KNOB_INFO_MAXIMUM_VALUE, CUDNN_TYPE_INT64, 1, &elemCount, &maxValue);
138  if (status != CUDNN_STATUS_SUCCESS) {
140  status,
141  "CUDNN_BACKEND_ENGINE_DESCRIPTOR: CUDNN_BACKEND_KNOB_INFO_DESCRIPTOR "
142  "GetAttribute CUDNN_ATTR_KNOB_INFO_MAXIMUM_VALUE Failed");
143  }
144  status = cudnnBackendGetAttribute(
145  bKnob, CUDNN_ATTR_KNOB_INFO_MINIMUM_VALUE, CUDNN_TYPE_INT64, 1, &elemCount, &minValue);
146  if (status != CUDNN_STATUS_SUCCESS) {
148  status,
149  "CUDNN_BACKEND_ENGINE_DESCRIPTOR: CUDNN_BACKEND_KNOB_INFO_DESCRIPTOR "
150  "GetAttribute CUDNN_ATTR_KNOB_INFO_MINIMUM_VALUE Failed");
151  }
152  status =
153  cudnnBackendGetAttribute(bKnob, CUDNN_ATTR_KNOB_INFO_STRIDE, CUDNN_TYPE_INT64, 1, &elemCount, &stride);
154  if (status != CUDNN_STATUS_SUCCESS) {
156  status,
157  "CUDNN_BACKEND_ENGINE_DESCRIPTOR: CUDNN_BACKEND_KNOB_INFO_DESCRIPTOR "
158  "GetAttribute CUDNN_ATTR_KNOB_INFO_STRIDE Failed");
159  }
160  knobs.emplace_back(Knob(type, maxValue, minValue, stride));
161  }
162  }
163 
164  public:
165  friend class EngineBuilder_v8;
166  std::string
167  describe() const override {
168  std::stringstream ss;
169  ss << "CUDNN_BACKEND_ENGINE_DESCRIPTOR :";
170  ss << " ID: " << idx;
171  ss << " Has " << numKnobs << " knobs";
172  return ss.str();
173  }
175  : BackendDescriptor(from.get_desc(), from.get_status(), from.get_error()),
176  opGraph(from.opGraph),
177  idx(from.idx),
178  opGraphTag(from.opGraphTag) {
179  cudnnStatus_t status;
180  for (uint64_t i = 0; i < bKnobs.size(); i++) {
181  bKnobs[i] = make_shared_backend_pointer(CUDNN_BACKEND_KNOB_INFO_DESCRIPTOR);
182  if (bKnobs[i]->is_good() == false) {
183  status = bKnobs[i]->get_status();
185  this,
186  status,
187  "CUDNN_BACKEND_ENGINE_DESCRIPTOR: CUDNN_BACKEND_KNOB_INFO_DESCRIPTOR cudnnCreate Failed");
188  }
189  }
190 
191  std::array<cudnnBackendDescriptor_t, CUDNN_KNOB_TYPE_COUNTS> bKnobs_ =
192  {};
193  for (auto i = 0; i < bKnobs.size(); i++) {
194  bKnobs_[i] = bKnobs[i]->get_backend_descriptor();
195  }
196  status = cudnnBackendGetAttribute(pointer->get_backend_descriptor(),
197  CUDNN_ATTR_ENGINE_KNOB_INFO,
198  CUDNN_TYPE_BACKEND_DESCRIPTOR,
199  CUDNN_KNOB_TYPE_COUNTS,
200  &numKnobs,
201  bKnobs_.data());
202  if (status != CUDNN_STATUS_SUCCESS) {
204  this, status, "CUDNN_BACKEND_ENGINE_DESCRIPTOR: GetAttribute CUDNN_ATTR_ENGINE_KNOB_INFO Query Failed");
205  }
206  buildKnobs();
207  }
208  ~Engine_v8() = default;
209 
210  std::string const &
211  getTag() const {
212  return opGraphTag;
213  }
214 
216  std::vector<Knob> &
218  return knobs;
219  }
220 
222  std::vector<Knob> const &
224  return knobs;
225  }
226 };
227 
232  public:
237  auto
240  m_engine.opGraph = opGraph_.get_desc();
241  m_engine.opGraphTag = opGraph_.getTag();
242  return *this;
243  }
245  auto
246  setOperationGraph(cudnnBackendDescriptor_t desc_) -> EngineBuilder_v8 & {
247  // TBD
248  return *this;
249  }
251  auto
253  m_engine.opGraph = desc_;
254  return *this;
255  }
257  auto
259  m_engine.idx = idx_;
260  return *this;
261  }
264  Engine_v8 &&
267  build() {
268  if (m_engine.idx < 0) {
270  &m_engine,
271  CUDNN_STATUS_BAD_PARAM,
272  "CUDNN_BACKEND_ENGINE_DESCRIPTOR: Check and Set the CUDNN_ATTR_ENGINE_GLOBAL_INDEX to valid value");
273  return std::move(m_engine);
274  }
275  if (m_engine.opGraph == nullptr) {
277  &m_engine,
278  CUDNN_STATUS_BAD_PARAM,
279  "CUDNN_BACKEND_ENGINE_DESCRIPTOR: Check and Set CUDNN_ATTR_ENGINE_OPERATION_GRAPH to valid value");
280  return std::move(m_engine);
281  }
282 
283  // Create a descriptor. Memory allocation happens here.
284  auto status = m_engine.initialize_managed_backend_pointer(CUDNN_BACKEND_ENGINE_DESCRIPTOR);
285  if (status != CUDNN_STATUS_SUCCESS) {
287  &m_engine, status, "CUDNN_BACKEND_ENGINE_DESCRIPTOR: cudnnCreate Descriptor Failed");
288  return std::move(m_engine);
289  }
290 
291  status = cudnnBackendSetAttribute(m_engine.pointer->get_backend_descriptor(),
292  CUDNN_ATTR_ENGINE_OPERATION_GRAPH,
293  CUDNN_TYPE_BACKEND_DESCRIPTOR,
294  1,
295  &(m_engine.opGraph->get_backend_descriptor()));
296  if (status != CUDNN_STATUS_SUCCESS) {
298  &m_engine,
299  status,
300  "CUDNN_BACKEND_ENGINE_DESCRIPTOR: SetAttribute CUDNN_ATTR_ENGINE_OPERATION_GRAPH Failed");
301  return std::move(m_engine);
302  }
303 
304  status = cudnnBackendSetAttribute(m_engine.pointer->get_backend_descriptor(),
305  CUDNN_ATTR_ENGINE_GLOBAL_INDEX,
306  CUDNN_TYPE_INT64,
307  1,
308  &m_engine.idx);
309  if (status != CUDNN_STATUS_SUCCESS) {
311  &m_engine,
312  status,
313  "CUDNN_BACKEND_ENGINE_DESCRIPTOR: SetAttribute CUDNN_ATTR_ENGINE_GLOBAL_INDEX Failed");
314  return std::move(m_engine);
315  }
316 
317  // Finalizing the descriptor
318  status = cudnnBackendFinalize(m_engine.pointer->get_backend_descriptor());
319  if (status != CUDNN_STATUS_SUCCESS) {
320  set_error_and_throw_exception(&m_engine, status, "CUDNN_BACKEND_ENGINE_DESCRIPTOR: cudnnFinalize Failed");
321  return std::move(m_engine);
322  }
323 
324  return std::move(m_engine);
325  }
326 
327  explicit EngineBuilder_v8() = default;
328  ~EngineBuilder_v8() = default;
329  EngineBuilder_v8(EngineBuilder_v8 &&) = delete;
330  EngineBuilder_v8(EngineBuilder_v8 const &) = delete;
332  operator=(EngineBuilder_v8 const &) = delete;
333 
334  private:
336 };
337 }
int64_t choice
Choice set by the user.
Engine_v8 & operator=(Engine_v8 const &)=delete
auto setOperationGraph(ManagedOpaqueDescriptor desc_) -> EngineBuilder_v8 &
Set operationGraph for the engine.
int64_t stride
min, max and stride of the knob value
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 const & getTag() const
int64_t idx
Global Index of the engine for the given operationGraph.
static ManagedOpaqueDescriptor make_shared_backend_pointer(cudnnBackendDescriptorType_t type)
void buildKnobs()
Called from the constructor builds the internal knobs vector.
ManagedOpaqueDescriptor opGraph
ManagedOpaqueDescriptor get_desc() const
Returns a copy of underlying managed descriptor.
std::array< ManagedOpaqueDescriptor, CUDNN_KNOB_TYPE_COUNTS > bKnobs
Opaque pointer to the backend knobs.
const cudnnBackendKnobType_t getKnobType() const
auto setOperationGraph(OperationGraph_v8 const &opGraph_) -> EngineBuilder_v8 &
Set operationGraph for the engine.
auto setOperationGraph(cudnnBackendDescriptor_t desc_) -> EngineBuilder_v8 &
Set operationGraph for the engine.
cudnnStatus_t get_status() const
Current status of the descriptor.
std::vector< Knob > const & getFinalizedKnobs() const
Returns a final vector of knobs. Used in EngineConfigBuilder.
std::vector< Knob > & getSupportedKnobs()
Returns a vector of knobs to the user for modification.
std::shared_ptr< OpaqueBackendPointer > ManagedOpaqueDescriptor
std::string describe() const override
Return a string describing the backend Descriptor.
const char * get_error() const
Diagonistic error message if any.
Knob(cudnnBackendKnobType_t type_, int64_t max, int64_t min, int64_t stride_)
cudnnStatus_t status
Shared pointer of the OpaqueBackendPointer.
int64_t numKnobs
Count of the backend knobs in the engine.