HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
common.h
Go to the documentation of this file.
1 /**
2  * Copyright (c) 2016-present, Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 // Portions Copyright (c) Microsoft Corporation
17 
18 #pragma once
19 
20 #include <climits>
21 #include <cstring>
22 #include <algorithm>
23 #include <chrono>
24 #include <functional>
25 #include <memory>
26 #include <numeric>
27 #include <set>
28 #include <sstream>
29 #include <string>
30 #include <type_traits>
31 #include <unordered_map>
32 #include <utility>
33 #include <vector>
34 
36 #include "core/common/exceptions.h"
38 #include "core/common/status.h"
39 
40 namespace onnxruntime {
41 
42 using TimePoint = std::chrono::high_resolution_clock::time_point;
43 
44 #ifdef _WIN32
45 #define ORT_UNUSED_PARAMETER(x) (x)
46 #else
47 #define ORT_UNUSED_PARAMETER(x) (void)(x)
48 #endif
49 
50 #ifndef ORT_HAVE_ATTRIBUTE
51 #ifdef __has_attribute
52 #define ORT_HAVE_ATTRIBUTE(x) __has_attribute(x)
53 #else
54 #define ORT_HAVE_ATTRIBUTE(x) 0
55 #endif
56 #endif
57 
58 // ORT_ATTRIBUTE_UNUSED
59 //
60 // Prevents the compiler from complaining about or optimizing away variables
61 // that appear unused on Linux
62 #if ORT_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
63 #undef ORT_ATTRIBUTE_UNUSED
64 #define ORT_ATTRIBUTE_UNUSED __attribute__((__unused__))
65 #else
66 #define ORT_ATTRIBUTE_UNUSED
67 #endif
68 
69 #ifdef ORT_NO_EXCEPTIONS
70 // Print the given final message, the message must be a null terminated char*
71 // ORT will abort after printing the message.
72 // For Android, will print to Android system log
73 // For other platforms, will print to stderr
74 void PrintFinalMessage(const char* msg);
75 #endif
76 
77 // macro to explicitly ignore the return value from a function call so Code Analysis doesn't complain
78 #define ORT_IGNORE_RETURN_VALUE(fn) \
79  static_cast<void>(fn)
80 
81 std::vector<std::string> GetStackTrace();
82 // these is a helper function that gets defined by platform/Telemetry
83 void LogRuntimeError(uint32_t session_id, const common::Status& status, const char* file,
84  const char* function, uint32_t line);
85 
86 // __PRETTY_FUNCTION__ isn't a macro on gcc, so use a check for _MSC_VER
87 // so we only define it as one for MSVC
88 #if (_MSC_VER && !defined(__PRETTY_FUNCTION__))
89 #define __PRETTY_FUNCTION__ __FUNCTION__
90 #endif
91 
92 // Capture where a message is coming from. Use __FUNCTION__ rather than the much longer __PRETTY_FUNCTION__
93 #define ORT_WHERE ::onnxruntime::CodeLocation(__FILE__, __LINE__, static_cast<const char*>(__FUNCTION__))
94 
95 #define ORT_WHERE_WITH_STACK \
96  ::onnxruntime::CodeLocation(__FILE__, __LINE__, static_cast<const char*>(__PRETTY_FUNCTION__), ::onnxruntime::GetStackTrace())
97 
98 #ifdef ORT_NO_EXCEPTIONS
99 
100 #define ORT_TRY if (true)
101 #define ORT_CATCH(x) else if (false)
102 #define ORT_RETHROW
103 
104 // In order to ignore the catch statement when a specific exception (not ... ) is caught and referred
105 // in the body of the catch statements, it is necessary to wrap the body of the catch statement into
106 // a lambda function. otherwise the exception referred will be undefined and cause build break
107 #define ORT_HANDLE_EXCEPTION(func)
108 
109 // Throw an exception with optional message.
110 // NOTE: The arguments get streamed into a string via ostringstream::operator<<
111 // DO NOT use a printf format string, as that will not work as you expect.
112 #define ORT_THROW(...) \
113  do { \
114  ::onnxruntime::PrintFinalMessage( \
115  ::onnxruntime::OnnxRuntimeException( \
116  ORT_WHERE_WITH_STACK, ::onnxruntime::MakeString(__VA_ARGS__)) \
117  .what()); \
118  abort(); \
119  } while (false)
120 
121 // Just in order to mark things as not implemented. Do not use in final code.
122 #define ORT_NOT_IMPLEMENTED(...) \
123  do { \
124  ::onnxruntime::PrintFinalMessage( \
125  ::onnxruntime::NotImplementedException(::onnxruntime::MakeString(__VA_ARGS__)) \
126  .what()); \
127  abort(); \
128  } while (false)
129 
130 // Check condition.
131 // NOTE: The arguments get streamed into a string via ostringstream::operator<<
132 // DO NOT use a printf format string, as that will not work as you expect.
133 #define ORT_ENFORCE(condition, ...) \
134  do { \
135  if (!(condition)) { \
136  ::onnxruntime::PrintFinalMessage( \
137  ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, #condition, \
138  ::onnxruntime::MakeString(__VA_ARGS__)) \
139  .what()); \
140  abort(); \
141  } \
142  } while (false)
143 
144 #define ORT_THROW_EX(ex, ...) \
145  do { \
146  ::onnxruntime::PrintFinalMessage( \
147  ::onnxruntime::MakeString(#ex, "(", ::onnxruntime::MakeString(__VA_ARGS__), ")").c_str()); \
148  abort(); \
149  } while (false)
150 
151 #else
152 
153 #define ORT_TRY try
154 #define ORT_CATCH(x) catch (x)
155 #define ORT_RETHROW throw;
156 
157 #define ORT_HANDLE_EXCEPTION(func) func()
158 
159 // Throw an exception with optional message.
160 // NOTE: The arguments get streamed into a string via ostringstream::operator<<
161 // DO NOT use a printf format string, as that will not work as you expect.
162 #define ORT_THROW(...) \
163  throw ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, ::onnxruntime::MakeString(__VA_ARGS__))
164 
165 // Just in order to mark things as not implemented. Do not use in final code.
166 #define ORT_NOT_IMPLEMENTED(...) \
167  throw ::onnxruntime::NotImplementedException(::onnxruntime::MakeString(__VA_ARGS__))
168 
169 // Check condition.
170 // NOTE: The arguments get streamed into a string via ostringstream::operator<<
171 // DO NOT use a printf format string, as that will not work as you expect.
172 #define ORT_ENFORCE(condition, ...) \
173  do { \
174  if (!(condition)) { \
175  throw ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, #condition, \
176  ::onnxruntime::MakeString(__VA_ARGS__)); \
177  } \
178  } while (false)
179 
180 #define ORT_THROW_EX(ex, ...) \
181  throw ex(__VA_ARGS__)
182 
183 #endif
184 
185 #define ORT_MAKE_STATUS(category, code, ...) \
186  ::onnxruntime::common::Status(::onnxruntime::common::category, \
187  ::onnxruntime::common::code, \
188  ::onnxruntime::MakeString(__VA_ARGS__))
189 
190 // Check condition. if met, return status.
191 #define ORT_RETURN_IF(condition, ...) \
192  do { \
193  if (condition) { \
194  return ::onnxruntime::common::Status(::onnxruntime::common::ONNXRUNTIME, \
195  ::onnxruntime::common::FAIL, \
196  ::onnxruntime::MakeString(ORT_WHERE.ToString(), " ", __VA_ARGS__)); \
197  } \
198  } while (false)
199 
200 // Check condition. if not met, return status.
201 #define ORT_RETURN_IF_NOT(condition, ...) \
202  ORT_RETURN_IF(!(condition), __VA_ARGS__)
203 
204 // Macros to disable the copy and/or move ctor and assignment methods
205 // These are usually placed in the private: declarations for a class.
206 
207 #define ORT_DISALLOW_COPY(TypeName) TypeName(const TypeName&) = delete
208 
209 #define ORT_DISALLOW_ASSIGNMENT(TypeName) TypeName& operator=(const TypeName&) = delete
210 
211 #define ORT_DISALLOW_COPY_AND_ASSIGNMENT(TypeName) \
212  ORT_DISALLOW_COPY(TypeName); \
213  ORT_DISALLOW_ASSIGNMENT(TypeName)
214 
215 #define ORT_DISALLOW_MOVE(TypeName) \
216  TypeName(TypeName&&) = delete; \
217  TypeName& operator=(TypeName&&) = delete
218 
219 #define ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(TypeName) \
220  ORT_DISALLOW_COPY_AND_ASSIGNMENT(TypeName); \
221  ORT_DISALLOW_MOVE(TypeName)
222 
223 #define ORT_RETURN_IF_ERROR_SESSIONID(expr, session_id) \
224  do { \
225  auto _status = (expr); \
226  if ((!_status.IsOK())) { \
227  ::onnxruntime::LogRuntimeError(session_id, _status, __FILE__, static_cast<const char*>(__FUNCTION__), __LINE__); \
228  return _status; \
229  } \
230  } while (0)
231 
232 #define ORT_RETURN_IF_ERROR_SESSIONID_(expr) ORT_RETURN_IF_ERROR_SESSIONID(expr, session_id_)
233 #define ORT_RETURN_IF_ERROR(expr) ORT_RETURN_IF_ERROR_SESSIONID(expr, 0)
234 
235 #define ORT_THROW_IF_ERROR(expr) \
236  do { \
237  auto _status = (expr); \
238  if ((!_status.IsOK())) { \
239  ::onnxruntime::LogRuntimeError(0, _status, __FILE__, static_cast<const char*>(__FUNCTION__), __LINE__); \
240  ORT_THROW(_status); \
241  } \
242  } while (0)
243 
244 // use this macro when cannot early return
245 #define ORT_CHECK_AND_SET_RETVAL(expr) \
246  do { \
247  if (retval.IsOK()) { \
248  retval = (expr); \
249  } \
250  } while (0)
251 
252 inline long long TimeDiffMicroSeconds(TimePoint start_time) {
253  auto end_time = std::chrono::high_resolution_clock::now();
254  return std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();
255 }
256 
257 inline long long TimeDiffMicroSeconds(TimePoint start_time, TimePoint end_time) {
258  return std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();
259 }
260 
261 struct null_type {};
262 inline std::string ToUTF8String(const std::string& s) { return s; }
263 #ifdef _WIN32
264 /**
265  * Convert a wide character string to a UTF-8 string
266  */
267 std::string ToUTF8String(const std::wstring& s);
268 
269 std::wstring ToWideString(const std::string& s);
270 inline std::wstring ToWideString(const std::wstring& s) { return s; }
271 #else
272 inline std::string ToWideString(const std::string& s) { return s; }
273 #endif
274 
275 constexpr size_t kMaxStrLen = 2048;
276 
277 // Returns whether `key` is in `container`.
278 // Like C++20's map/set contains() member function.
279 template <typename Key, typename... OtherContainerArgs,
280  template <typename...> typename AssociativeContainer,
281  typename LookupKey>
282 inline bool Contains(const AssociativeContainer<Key, OtherContainerArgs...>& container, LookupKey&& key) {
283  return container.find(std::forward<LookupKey>(key)) != container.end();
284 }
285 
286 } // namespace onnxruntime
bool Contains(const AssociativeContainer< Key, OtherContainerArgs...> &container, LookupKey &&key)
Definition: common.h:282
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
long long TimeDiffMicroSeconds(TimePoint start_time)
Definition: common.h:252
GLdouble s
Definition: glad.h:3009
std::chrono::high_resolution_clock::time_point TimePoint
Definition: common.h:42
std::string ToWideString(const std::string &s)
Definition: common.h:272
void LogRuntimeError(uint32_t session_id, const common::Status &status, const char *file, const char *function, uint32_t line)
std::string ToUTF8String(const std::string &s)
Definition: common.h:262
constexpr size_t kMaxStrLen
Definition: common.h:275
std::vector< std::string > GetStackTrace()
GLint GLsizei count
Definition: glcorearb.h:405