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.cc 9.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "include/api/status.h"
  19. #ifndef ENABLE_ANDROID
  20. #include <thread>
  21. #endif
  22. #include <map>
  23. #include <sstream>
  24. namespace mindspore {
  25. struct Status::Data {
  26. enum StatusCode status_code = kSuccess;
  27. std::string status_msg;
  28. int line_of_code = -1;
  29. std::string file_name;
  30. std::string err_description;
  31. };
  32. Status::Status() : data_(std::make_shared<Data>()) {}
  33. Status::Status(enum StatusCode status_code, const std::vector<char> &status_msg) : data_(std::make_shared<Data>()) {
  34. if (data_ == nullptr) {
  35. return;
  36. }
  37. data_->status_msg = CharToString(status_msg);
  38. data_->status_code = status_code;
  39. }
  40. Status::Status(enum StatusCode code, int line_of_code, const char *file_name, const std::vector<char> &extra)
  41. : data_(std::make_shared<Data>()) {
  42. if (data_ == nullptr) {
  43. return;
  44. }
  45. data_->status_code = code;
  46. data_->line_of_code = line_of_code;
  47. if (file_name != nullptr) {
  48. data_->file_name = file_name;
  49. }
  50. data_->err_description = CharToString(extra);
  51. std::ostringstream ss;
  52. #ifndef ENABLE_ANDROID
  53. ss << "Thread ID " << std::this_thread::get_id() << " " << CodeAsString(code) << ". ";
  54. if (!data_->err_description.empty()) {
  55. ss << data_->err_description;
  56. }
  57. ss << "\n";
  58. #endif
  59. ss << "Line of code : " << line_of_code << "\n";
  60. if (file_name != nullptr) {
  61. ss << "File : " << file_name << "\n";
  62. }
  63. data_->status_msg = ss.str();
  64. }
  65. enum StatusCode Status::StatusCode() const {
  66. if (data_ == nullptr) {
  67. return kSuccess;
  68. }
  69. return data_->status_code;
  70. }
  71. std::vector<char> Status::ToCString() const {
  72. if (data_ == nullptr) {
  73. return std::vector<char>();
  74. }
  75. return StringToChar(data_->status_msg);
  76. }
  77. int Status::GetLineOfCode() const {
  78. if (data_ == nullptr) {
  79. return -1;
  80. }
  81. return data_->line_of_code;
  82. }
  83. std::vector<char> Status::GetErrDescriptionChar() const {
  84. if (data_ == nullptr) {
  85. return std::vector<char>();
  86. }
  87. return StringToChar(data_->status_msg);
  88. }
  89. std::vector<char> Status::CodeAsCString(enum StatusCode c) {
  90. static std::map<enum StatusCode, std::string> info_map = {{kSuccess, "No error occurs."},
  91. // Core
  92. {kCoreFailed, "Common error code."},
  93. // MD
  94. {kMDOutOfMemory, "Out of memory"},
  95. {kMDShapeMisMatch, "Shape is incorrect"},
  96. {kMDInterrupted, "Interrupted system call"},
  97. {kMDNoSpace, "No space left on device"},
  98. {kMDPyFuncException, "Exception thrown from PyFunc"},
  99. {kMDDuplicateKey, "Duplicate key"},
  100. {kMDPythonInterpreterFailure, ""},
  101. {kMDTDTPushFailure, "Unexpected error"},
  102. {kMDFileNotExist, "Unexpected error"},
  103. {kMDProfilingError, "Error encountered while profiling"},
  104. {kMDBoundingBoxOutOfBounds, "Unexpected error"},
  105. {kMDBoundingBoxInvalidShape, "Unexpected error"},
  106. {kMDSyntaxError, "Syntax error"},
  107. {kMDTimeOut, "Unexpected error"},
  108. {kMDBuddySpaceFull, "BuddySpace full"},
  109. {kMDNetWorkError, "Network error"},
  110. {kMDNotImplementedYet, "Unexpected error"},
  111. {kMDUnexpectedError, "Unexpected error"},
  112. // ME
  113. {kMEFailed, "Common error code."},
  114. {kMEInvalidInput, "Invalid input."},
  115. // MC
  116. {kMCFailed, "Common error code."},
  117. {kMCDeviceError, "Device error."},
  118. {kMCInvalidInput, "Invalid input."},
  119. {kMCInvalidArgs, "Invalid arguments."},
  120. // Lite
  121. {kLiteError, "Common error code."},
  122. {kLiteNullptr, "NULL pointer returned."},
  123. {kLiteParamInvalid, "Invalid parameter."},
  124. {kLiteNoChange, "No change."},
  125. {kLiteSuccessExit, "No error but exit."},
  126. {kLiteMemoryFailed, "Fail to create memory."},
  127. {kLiteNotSupport, "Fail to support."},
  128. {kLiteThreadPoolError, "Thread pool error."},
  129. {kLiteOutOfTensorRange, "Failed to check range."},
  130. {kLiteInputTensorError, "Failed to check input tensor."},
  131. {kLiteReentrantError, "Exist executor running."},
  132. {kLiteGraphFileError, "Failed to verify graph file."},
  133. {kLiteNotFindOp, "Failed to find operator."},
  134. {kLiteInvalidOpName, "Invalid operator name."},
  135. {kLiteInvalidOpAttr, "Invalid operator attr."},
  136. {kLiteOpExecuteFailure, "Failed to execution operator."},
  137. {kLiteFormatError, "Failed to checking tensor format."},
  138. {kLiteInferError, "Failed to infer shape."},
  139. {kLiteInferInvalid, "Invalid infer shape before runtime."},
  140. {kLiteInputParamInvalid, "Invalid input param by user."}};
  141. auto iter = info_map.find(c);
  142. return StringToChar(iter == info_map.end() ? "Unknown error" : iter->second);
  143. }
  144. std::ostream &operator<<(std::ostream &os, const Status &s) {
  145. os << s.ToString();
  146. return os;
  147. }
  148. std::vector<char> Status::SetErrDescription(const std::vector<char> &err_description) {
  149. if (data_ == nullptr) {
  150. return std::vector<char>();
  151. }
  152. data_->err_description = CharToString(err_description);
  153. std::ostringstream ss;
  154. #ifndef ENABLE_ANDROID
  155. ss << "Thread ID " << std::this_thread::get_id() << " " << CodeAsString(data_->status_code) << ". ";
  156. if (!data_->err_description.empty()) {
  157. ss << data_->err_description;
  158. }
  159. ss << "\n";
  160. #endif
  161. if (data_->line_of_code > 0 && !data_->file_name.empty()) {
  162. ss << "Line of code : " << data_->line_of_code << "\n";
  163. ss << "File : " << data_->file_name << "\n";
  164. }
  165. data_->status_msg = ss.str();
  166. return StringToChar(data_->status_msg);
  167. }
  168. bool Status::operator==(const Status &other) const {
  169. if (data_ == nullptr && other.data_ == nullptr) {
  170. return true;
  171. }
  172. if (data_ == nullptr || other.data_ == nullptr) {
  173. return false;
  174. }
  175. return data_->status_code == other.data_->status_code;
  176. }
  177. bool Status::operator==(enum StatusCode other_code) const { return StatusCode() == other_code; }
  178. bool Status::operator!=(const Status &other) const { return !operator==(other); }
  179. bool Status::operator!=(enum StatusCode other_code) const { return !operator==(other_code); }
  180. Status::operator bool() const { return (StatusCode() == kSuccess); }
  181. Status::operator int() const { return static_cast<int>(StatusCode()); }
  182. Status Status::OK() { return StatusCode::kSuccess; }
  183. bool Status::IsOk() const { return (StatusCode() == StatusCode::kSuccess); }
  184. bool Status::IsError() const { return !IsOk(); }
  185. } // namespace mindspore

A lightweight and high-performance service module that helps MindSpore developers efficiently deploy online inference services in the production environment.