CUDNN Frontend API  8.3.0
cudnn_frontend_utils.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 #include <exception>
25 #include <string>
26 #include <vector>
27 
28 #include "cudnn_backend_base.h"
29 #include "cudnn_frontend_Logging.h"
30 
31 namespace cudnn_frontend {
32 
34 using feature_vector_t = std::vector<int64_t>;
35 
36 #ifndef NV_CUDNN_DISABLE_EXCEPTION
37 class cudnnException : public std::runtime_error {
38  public:
39  cudnnException(const char *message, cudnnStatus_t status) throw() : std::runtime_error(message) {
40  error_status = status;
41  }
42  virtual const char *
43  what() const throw() {
44  return std::runtime_error::what();
45  }
46  cudnnStatus_t getCudnnStatus() {
47  return error_status;
48  }
49 
50  cudnnStatus_t error_status;
51 };
52 #endif
53 
54 static inline void
55 throw_if(std::function<bool()> expr, const char *message, cudnnStatus_t status) {
56  if (expr()) {
57 #ifndef NV_CUDNN_DISABLE_EXCEPTION
58  throw cudnnException(message, status);
59 #endif
60  }
61 }
62 static inline void
63 throw_if(bool expr, const char *message, cudnnStatus_t status) {
64  if (expr) {
65 #ifndef NV_CUDNN_DISABLE_EXCEPTION
66  throw cudnnException(message, status);
67 #endif
68  }
69 }
70 
71 static inline std::string
72 to_string(cudnnDataType_t type) {
73  switch(type) {
74  case CUDNN_DATA_FLOAT:
75  return std::string("CUDNN_DATA_FLOAT");
76  case CUDNN_DATA_DOUBLE:
77  return std::string("CUDNN_DATA_DOUBLE");
78  case CUDNN_DATA_HALF:
79  return std::string("CUDNN_DATA_HALF");
80  case CUDNN_DATA_INT8:
81  return std::string("CUDNN_DATA_INT8");
82  case CUDNN_DATA_INT32:
83  return std::string("CUDNN_DATA_INT32");
84  case CUDNN_DATA_INT8x4: // x4 and x32 are replaced by vectorized dimension in the v8 API
85  return std::string("CUDNN_DATA_INT8x4");
86  case CUDNN_DATA_UINT8:
87  return std::string("CUDNN_DATA_UINT8");
88  case CUDNN_DATA_UINT8x4: // x4 and x32 are replaced by vectorized dimension in the v8 API
89  return std::string("CUDNN_DATA_UINT8x4");
90  case CUDNN_DATA_INT8x32: // x4 and x32 are replaced by vectorized dimension in the v8 API
91  return std::string("CUDNN_DATA_INT8x32");
92  case CUDNN_DATA_INT64:
93  return std::string("CUDNN_DATA_INT64");
94  case CUDNN_DATA_BFLOAT16:
95  return std::string("CUDNN_DATA_BFLOAT16");
96  }
97  return std::string("");
98 }
99 
100 static inline std::string
101 to_string(cudnnStatus_t status) {
102  switch(status) {
103  case CUDNN_STATUS_SUCCESS:
104  return std::string("CUDNN_STATUS_SUCCESS");
105  case CUDNN_STATUS_NOT_INITIALIZED:
106  return std::string("CUDNN_STATUS_NOT_INITIALIZED");
107  case CUDNN_STATUS_ALLOC_FAILED:
108  return std::string("CUDNN_STATUS_ALLOC_FAILED");
109  case CUDNN_STATUS_BAD_PARAM:
110  return std::string("CUDNN_STATUS_BAD_PARAM");
111  case CUDNN_STATUS_INTERNAL_ERROR:
112  return std::string("CUDNN_STATUS_INTERNAL_ERROR");
113  case CUDNN_STATUS_INVALID_VALUE:
114  return std::string("CUDNN_STATUS_INVALID_VALUE");
115  case CUDNN_STATUS_ARCH_MISMATCH:
116  return std::string("CUDNN_STATUS_ARCH_MISMATCH");
117  case CUDNN_STATUS_MAPPING_ERROR:
118  return std::string("CUDNN_STATUS_MAPPING_ERROR");
119  case CUDNN_STATUS_EXECUTION_FAILED:
120  return std::string("CUDNN_STATUS_EXECUTION_FAILED");
121  case CUDNN_STATUS_NOT_SUPPORTED:
122  return std::string("CUDNN_STATUS_NOT_SUPPORTED");
123  case CUDNN_STATUS_LICENSE_ERROR:
124  return std::string("CUDNN_STATUS_LICENSE_ERROR");
125  case CUDNN_STATUS_RUNTIME_PREREQUISITE_MISSING:
126  return std::string("CUDNN_STATUS_RUNTIME_PREREQUISITE_MISSING");
127  case CUDNN_STATUS_RUNTIME_IN_PROGRESS:
128  return std::string("CUDNN_STATUS_RUNTIME_IN_PROGRESS");
129  case CUDNN_STATUS_RUNTIME_FP_OVERFLOW:
130  return std::string("CUDNN_STATUS_RUNTIME_FP_OVERFLOW");
131  case CUDNN_STATUS_VERSION_MISMATCH:
132  return std::string("CUDNN_STATUS_VERSION_MISMATCH");
133  }
134  return std::string("");
135 }
136 
137 static inline void
138 set_error_and_throw_exception(BackendDescriptor const *desc, cudnnStatus_t status, const char *message) {
139  if (desc != nullptr) {
140  desc->set_status(status);
141  desc->set_error(message);
142  }
143 #ifndef NV_CUDNN_DISABLE_EXCEPTION
144  throw cudnnException(
145  std::string(std::string(message) + std::string(" cudnn_status: ") + to_string(status)).c_str(), status);
146 #endif
147 }
148 
149 }
std::vector< int64_t > feature_vector_t
Detailed feature_vector. Generally the Tensor and Operation properties.
static void throw_if(std::function< bool()> expr, const char *message, cudnnStatus_t status)
void set_status(cudnnStatus_t const status_) const
Set status of the descriptor.
static void set_error_and_throw_exception(BackendDescriptor const *desc, cudnnStatus_t status, const char *message)
virtual const char * what() const
cudnnException(const char *message, cudnnStatus_t status)
void set_error(const char *message) const
Set Diagonistic error message.
static std::string to_string(cudnnDataType_t type)