You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

exception.h 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * \file src/core/include/megbrain/exception.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. * implied.
  11. */
  12. #pragma once
  13. #include "megbrain_build_config.h"
  14. #include <memory>
  15. #include <stdexcept>
  16. #include <string>
  17. #if MGB_ENABLE_EXCEPTION
  18. #define MGB_IF_EXCEPTION(x...) x
  19. #else
  20. #define MGB_IF_EXCEPTION(x...)
  21. #endif
  22. #if (defined(__GNUC__) && !defined(__ANDROID__) && !defined(ANDROID) && \
  23. !defined(__APPLE__))
  24. #include <cxxabi.h> // for abi::__forced_unwind
  25. #define __MGB_HANDLE_FORCED_UNWIND MGB_CATCH(abi::__forced_unwind&, { throw; })
  26. #else
  27. #define __MGB_HANDLE_FORCED_UNWIND
  28. #endif
  29. /*!
  30. * \brief catch all exceptions and store in an exception_ptr; usually used in
  31. * worker threads
  32. *
  33. * This macro should be inserted after a try block
  34. * \param _scope_msg const char* type text to describe where this exception is
  35. * caught
  36. */
  37. #define MGB_CATCH_ALL_EXCEPTION(_scope_msg, _ptr) \
  38. MGB_CATCH(std::exception& _exc, { \
  39. mgb_log_error("caught exception in %s; what(): %s", _scope_msg, _exc.what()); \
  40. _ptr = std::current_exception(); \
  41. }) \
  42. __MGB_HANDLE_FORCED_UNWIND \
  43. MGB_CATCH(..., { \
  44. mgb_log_error("caught unknown exception in %s", _scope_msg); \
  45. _ptr = std::current_exception(); \
  46. }) \
  47. do { \
  48. } while (0)
  49. /*!
  50. * \brief catch all exceptions in a class destructor and log error and abort
  51. *
  52. * \param _scope_msg const char* type text to describe where this exception is
  53. * caught
  54. */
  55. #define MGB_HANDLE_EXCEPTION_DTOR(_scope_msg) \
  56. MGB_CATCH(std::exception& _exc, { \
  57. mgb_log_error( \
  58. "abort due to exception in %s; what(): %s", _scope_msg, _exc.what()); \
  59. abort(); \
  60. }) \
  61. MGB_CATCH(..., { \
  62. mgb_log_error("abort due to unknown exception in %s", _scope_msg); \
  63. }) \
  64. do { \
  65. } while (0)
  66. namespace mgb {
  67. //! the most general MegBrain exception type; also base class for all megbrain
  68. //! exceptions
  69. class MegBrainError : public std::exception {
  70. protected:
  71. std::string m_msg;
  72. public:
  73. /*!
  74. * \brief base class for extra information to be associated with an
  75. * exception
  76. */
  77. class ExtraInfo {
  78. public:
  79. virtual ~ExtraInfo() = default;
  80. };
  81. MegBrainError(const std::string& msg) : m_msg(msg) { init(); }
  82. const char* what() const noexcept override { return m_msg.c_str(); }
  83. /*!
  84. * \brief get associated extra info, or nullptr
  85. */
  86. const ExtraInfo* extra_info() const { return m_extra_info.get(); }
  87. /*!
  88. * \brief set extra info
  89. */
  90. template <typename T>
  91. MegBrainError& extra_info(T&& ptr) {
  92. m_extra_info = ptr;
  93. return *this;
  94. }
  95. ~MegBrainError() noexcept = default;
  96. private:
  97. std::shared_ptr<ExtraInfo> m_extra_info;
  98. MGE_WIN_DECLSPEC_FUC void init();
  99. };
  100. //! base class for system error: error caused by uncontrollable environment
  101. class SystemError : public MegBrainError {
  102. public:
  103. using MegBrainError::MegBrainError;
  104. };
  105. /*!
  106. * \brief exception to be thrown if failing to allocate memory
  107. */
  108. class MemAllocError : public SystemError {
  109. public:
  110. using SystemError::SystemError;
  111. };
  112. class CudaError final : public SystemError {
  113. public:
  114. /*!
  115. * \brief get extra info for current cuda status, to be appended in
  116. * error message
  117. */
  118. static std::string get_cuda_extra_info();
  119. CudaError(const std::string& msg);
  120. };
  121. class EnFlameError final : public SystemError {
  122. public:
  123. EnFlameError(const std::string& msg);
  124. };
  125. class AtlasError final : public SystemError {
  126. public:
  127. AtlasError(const std::string& msg);
  128. };
  129. class ROCmError final : public SystemError {
  130. public:
  131. /*!
  132. * \brief get extra info for current rocm status, to be appended in
  133. * error message
  134. */
  135. static std::string get_rocm_extra_info();
  136. ROCmError(const std::string& msg);
  137. };
  138. class CnrtError final : public SystemError {
  139. public:
  140. /*!
  141. * \brief get extra info for current cnrt status, to be appended in
  142. * error message
  143. */
  144. static std::string get_cnrt_extra_info();
  145. CnrtError(const std::string& msg);
  146. };
  147. class CndevError final : public SystemError {
  148. public:
  149. CndevError(const std::string& msg);
  150. };
  151. class CnmlError final : public SystemError {
  152. public:
  153. CnmlError(const std::string& msg);
  154. };
  155. class AssertionError final : public MegBrainError {
  156. public:
  157. using MegBrainError::MegBrainError;
  158. };
  159. //! datatype conversion error
  160. class ConversionError final : public MegBrainError {
  161. public:
  162. using MegBrainError::MegBrainError;
  163. };
  164. class TensorCopyOverlapError final : public MegBrainError {
  165. public:
  166. using MegBrainError::MegBrainError;
  167. };
  168. class TensorReshapeError final : public MegBrainError {
  169. public:
  170. using MegBrainError::MegBrainError;
  171. };
  172. class SerializationError final : public MegBrainError {
  173. public:
  174. using MegBrainError::MegBrainError;
  175. };
  176. class MegDNNError final : public MegBrainError {
  177. public:
  178. using MegBrainError::MegBrainError;
  179. };
  180. //! megbrain internal error; should be treated as a bug
  181. class InternalError final : public MegBrainError {
  182. public:
  183. using MegBrainError::MegBrainError;
  184. };
  185. class TimeoutError final : public MegBrainError {
  186. public:
  187. using MegBrainError::MegBrainError;
  188. };
  189. } // namespace mgb
  190. namespace mgb {
  191. bool has_uncaught_exception();
  192. } // namespace mgb
  193. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台