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.

status.h 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  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. #ifndef MINDSPORE_INCLUDE_API_STATUS_H
  17. #define MINDSPORE_INCLUDE_API_STATUS_H
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include <ostream>
  22. #include <climits>
  23. #include "include/api/dual_abi_helper.h"
  24. #include "include/api/types.h"
  25. namespace mindspore {
  26. enum CompCode : uint32_t {
  27. kCore = 0x00000000u,
  28. kMD = 0x10000000u,
  29. kME = 0x20000000u,
  30. kMC = 0x30000000u,
  31. kLite = 0xF0000000u,
  32. };
  33. enum StatusCode : uint32_t {
  34. kSuccess = 0,
  35. // Core
  36. kCoreFailed = kCore | 0x1,
  37. // MD
  38. kMDOutOfMemory = kMD | 1,
  39. kMDShapeMisMatch = kMD | 2,
  40. kMDInterrupted = kMD | 3,
  41. kMDNoSpace = kMD | 4,
  42. kMDPyFuncException = kMD | 5,
  43. kMDDuplicateKey = kMD | 6,
  44. kMDPythonInterpreterFailure = kMD | 7,
  45. kMDTDTPushFailure = kMD | 8,
  46. kMDFileNotExist = kMD | 9,
  47. kMDProfilingError = kMD | 10,
  48. kMDBoundingBoxOutOfBounds = kMD | 11,
  49. kMDBoundingBoxInvalidShape = kMD | 12,
  50. kMDSyntaxError = kMD | 13,
  51. kMDTimeOut = kMD | 14,
  52. kMDBuddySpaceFull = kMD | 15,
  53. kMDNetWorkError = kMD | 16,
  54. kMDNotImplementedYet = kMD | 17,
  55. // Make this error code the last one. Add new error code above it.
  56. kMDUnexpectedError = kMD | 127,
  57. // ME
  58. kMEFailed = kME | 0x1,
  59. kMEInvalidInput = kME | 0x2,
  60. // MC
  61. kMCFailed = kMC | 0x1,
  62. kMCDeviceError = kMC | 0x2,
  63. kMCInvalidInput = kMC | 0x3,
  64. kMCInvalidArgs = kMC | 0x4,
  65. // Lite // Common error code, range: [-1, -100)
  66. kLiteError = kLite | (0x0FFFFFFF & -1), /**< Common error code. */
  67. kLiteNullptr = kLite | (0x0FFFFFFF & -2), /**< NULL pointer returned.*/
  68. kLiteParamInvalid = kLite | (0x0FFFFFFF & -3), /**< Invalid parameter.*/
  69. kLiteNoChange = kLite | (0x0FFFFFFF & -4), /**< No change. */
  70. kLiteSuccessExit = kLite | (0x0FFFFFFF & -5), /**< No error but exit. */
  71. kLiteMemoryFailed = kLite | (0x0FFFFFFF & -6), /**< Fail to create memory. */
  72. kLiteNotSupport = kLite | (0x0FFFFFFF & -7), /**< Fail to support. */
  73. kLiteThreadPoolError = kLite | (0x0FFFFFFF & -8), /**< Error occur in thread pool. */
  74. // Executor error code, range: [-100,-200)
  75. kLiteOutOfTensorRange = kLite | (0x0FFFFFFF & -100), /**< Failed to check range. */
  76. kLiteInputTensorError = kLite | (0x0FFFFFFF & -101), /**< Failed to check input tensor. */
  77. kLiteReentrantError = kLite | (0x0FFFFFFF & -102), /**< Exist executor running. */
  78. // Graph error code, range: [-200,-300)
  79. kLiteGraphFileError = kLite | (0x0FFFFFFF & -200), /**< Failed to verify graph file. */
  80. // Node error code, range: [-300,-400)
  81. kLiteNotFindOp = kLite | (0x0FFFFFFF & -300), /**< Failed to find operator. */
  82. kLiteInvalidOpName = kLite | (0x0FFFFFFF & -301), /**< Invalid operator name. */
  83. kLiteInvalidOpAttr = kLite | (0x0FFFFFFF & -302), /**< Invalid operator attr. */
  84. kLiteOpExecuteFailure = kLite | (0x0FFFFFFF & -303), /**< Failed to execution operator. */
  85. // Tensor error code, range: [-400,-500)
  86. kLiteFormatError = kLite | (0x0FFFFFFF & -400), /**< Failed to checking tensor format. */
  87. // InferShape error code, range: [-500,-600)
  88. kLiteInferError = kLite | (0x0FFFFFFF & -500), /**< Failed to infer shape. */
  89. kLiteInferInvalid = kLite | (0x0FFFFFFF & -501), /**< Invalid infer shape before runtime. */
  90. // User input param error code, range: [-600, 700)
  91. kLiteInputParamInvalid = kLite | (0x0FFFFFFF & -600), /**< Invalid input param by user. */
  92. };
  93. class MS_API Status {
  94. public:
  95. Status();
  96. inline Status(enum StatusCode status_code, const std::string &status_msg = ""); // NOLINT(runtime/explicit)
  97. inline Status(const StatusCode code, int line_of_code, const char *file_name, const std::string &extra = "");
  98. ~Status() = default;
  99. enum StatusCode StatusCode() const;
  100. inline std::string ToString() const;
  101. int GetLineOfCode() const;
  102. inline std::string GetErrDescription() const;
  103. inline std::string SetErrDescription(const std::string &err_description);
  104. friend std::ostream &operator<<(std::ostream &os, const Status &s);
  105. bool operator==(const Status &other) const;
  106. bool operator==(enum StatusCode other_code) const;
  107. bool operator!=(const Status &other) const;
  108. bool operator!=(enum StatusCode other_code) const;
  109. explicit operator bool() const;
  110. explicit operator int() const;
  111. static Status OK();
  112. bool IsOk() const;
  113. bool IsError() const;
  114. static inline std::string CodeAsString(enum StatusCode c);
  115. private:
  116. // api without std::string
  117. explicit Status(enum StatusCode status_code, const std::vector<char> &status_msg);
  118. Status(const enum StatusCode code, int line_of_code, const char *file_name, const std::vector<char> &extra);
  119. std::vector<char> ToCString() const;
  120. std::vector<char> GetErrDescriptionChar() const;
  121. std::vector<char> SetErrDescription(const std::vector<char> &err_description);
  122. static std::vector<char> CodeAsCString(enum StatusCode c);
  123. struct Data;
  124. std::shared_ptr<Data> data_;
  125. };
  126. Status::Status(enum StatusCode status_code, const std::string &status_msg)
  127. : Status(status_code, StringToChar(status_msg)) {}
  128. Status::Status(const enum StatusCode code, int line_of_code, const char *file_name, const std::string &extra)
  129. : Status(code, line_of_code, file_name, StringToChar(extra)) {}
  130. std::string Status::ToString() const { return CharToString(ToCString()); }
  131. std::string Status::GetErrDescription() const { return CharToString(GetErrDescriptionChar()); }
  132. std::string Status::SetErrDescription(const std::string &err_description) {
  133. return CharToString(SetErrDescription(StringToChar(err_description)));
  134. }
  135. std::string Status::CodeAsString(enum StatusCode c) { return CharToString(CodeAsCString(c)); }
  136. } // namespace mindspore
  137. #endif // MINDSPORE_INCLUDE_API_STATUS_H