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.5 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. kLiteUninitializedObj = kLite | (0x0FFFFFFF & -9), /**< Object is not initialized. */
  75. kLiteFileError = kLite | (0x0FFFFFFF & -10), /**< Invalid file. */
  76. // Executor error code, range: [-100,-200)
  77. kLiteOutOfTensorRange = kLite | (0x0FFFFFFF & -100), /**< Failed to check range. */
  78. kLiteInputTensorError = kLite | (0x0FFFFFFF & -101), /**< Failed to check input tensor. */
  79. kLiteReentrantError = kLite | (0x0FFFFFFF & -102), /**< Exist executor running. */
  80. // Graph error code, range: [-200,-300)
  81. kLiteGraphFileError = kLite | (0x0FFFFFFF & -200), /**< Failed to verify graph file. */
  82. // Node error code, range: [-300,-400)
  83. kLiteNotFindOp = kLite | (0x0FFFFFFF & -300), /**< Failed to find operator. */
  84. kLiteInvalidOpName = kLite | (0x0FFFFFFF & -301), /**< Invalid operator name. */
  85. kLiteInvalidOpAttr = kLite | (0x0FFFFFFF & -302), /**< Invalid operator attr. */
  86. kLiteOpExecuteFailure = kLite | (0x0FFFFFFF & -303), /**< Failed to execution operator. */
  87. // Tensor error code, range: [-400,-500)
  88. kLiteFormatError = kLite | (0x0FFFFFFF & -400), /**< Failed to checking tensor format. */
  89. // InferShape error code, range: [-500,-600)
  90. kLiteInferError = kLite | (0x0FFFFFFF & -500), /**< Failed to infer shape. */
  91. kLiteInferInvalid = kLite | (0x0FFFFFFF & -501), /**< Invalid infer shape before runtime. */
  92. // User input param error code, range: [-600, 700)
  93. kLiteInputParamInvalid = kLite | (0x0FFFFFFF & -600), /**< Invalid input param by user. */
  94. };
  95. class MS_API Status {
  96. public:
  97. Status();
  98. inline Status(enum StatusCode status_code, const std::string &status_msg = ""); // NOLINT(runtime/explicit)
  99. inline Status(const StatusCode code, int line_of_code, const char *file_name, const std::string &extra = "");
  100. ~Status() = default;
  101. enum StatusCode StatusCode() const;
  102. inline std::string ToString() const;
  103. int GetLineOfCode() const;
  104. inline std::string GetErrDescription() const;
  105. inline std::string SetErrDescription(const std::string &err_description);
  106. MS_API friend std::ostream &operator<<(std::ostream &os, const Status &s);
  107. bool operator==(const Status &other) const;
  108. bool operator==(enum StatusCode other_code) const;
  109. bool operator!=(const Status &other) const;
  110. bool operator!=(enum StatusCode other_code) const;
  111. explicit operator bool() const;
  112. explicit operator int() const;
  113. static Status OK();
  114. bool IsOk() const;
  115. bool IsError() const;
  116. static inline std::string CodeAsString(enum StatusCode c);
  117. private:
  118. // api without std::string
  119. Status(enum StatusCode status_code, const std::vector<char> &status_msg);
  120. Status(const enum StatusCode code, int line_of_code, const char *file_name, const std::vector<char> &extra);
  121. std::vector<char> ToCString() const;
  122. std::vector<char> GetErrDescriptionChar() const;
  123. std::vector<char> SetErrDescription(const std::vector<char> &err_description);
  124. static std::vector<char> CodeAsCString(enum StatusCode c);
  125. struct Data;
  126. std::shared_ptr<Data> data_;
  127. };
  128. Status::Status(enum StatusCode status_code, const std::string &status_msg)
  129. : Status(status_code, StringToChar(status_msg)) {}
  130. Status::Status(const enum StatusCode code, int line_of_code, const char *file_name, const std::string &extra)
  131. : Status(code, line_of_code, file_name, StringToChar(extra)) {}
  132. std::string Status::ToString() const { return CharToString(ToCString()); }
  133. std::string Status::GetErrDescription() const { return CharToString(GetErrDescriptionChar()); }
  134. std::string Status::SetErrDescription(const std::string &err_description) {
  135. return CharToString(SetErrDescription(StringToChar(err_description)));
  136. }
  137. std::string Status::CodeAsString(enum StatusCode c) { return CharToString(CodeAsCString(c)); }
  138. } // namespace mindspore
  139. #endif // MINDSPORE_INCLUDE_API_STATUS_H