• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "include/api/status.h"
20 #ifndef ENABLE_ANDROID
21 #include <thread>
22 #endif
23 #include <map>
24 #include <sstream>
25 
26 namespace mindspore {
27 struct Status::Data {
28   enum StatusCode status_code = kSuccess;
29   std::string status_msg;
30   int line_of_code = -1;
31   std::string file_name;
32   std::string err_description;
33 };
34 
Status()35 Status::Status() : data_(std::make_shared<Data>()) {}
36 
Status(enum StatusCode status_code,const std::vector<char> & status_msg)37 Status::Status(enum StatusCode status_code, const std::vector<char> &status_msg) : data_(std::make_shared<Data>()) {
38   if (data_ == nullptr) {
39     return;
40   }
41 
42   data_->err_description = CharToString(status_msg);
43   data_->status_msg = CharToString(status_msg);
44   data_->status_code = status_code;
45 }
46 
Status(enum StatusCode code,int line_of_code,const char * file_name,const std::vector<char> & extra)47 Status::Status(enum StatusCode code, int line_of_code, const char *file_name, const std::vector<char> &extra)
48     : data_(std::make_shared<Data>()) {
49   if (data_ == nullptr) {
50     return;
51   }
52   data_->status_code = code;
53   data_->line_of_code = line_of_code;
54   if (file_name != nullptr) {
55     data_->file_name = file_name;
56   }
57   data_->err_description = CharToString(extra);
58 
59   std::ostringstream ss;
60 #ifndef ENABLE_ANDROID
61 #ifdef DEBUG
62   ss << "Thread ID " << std::this_thread::get_id() << " " << CodeAsString(code) << ". ";
63 #else
64   ss << CodeAsString(code) << ". ";
65 #endif
66   if (!data_->err_description.empty()) {
67     ss << data_->err_description;
68   }
69   ss << "\n";
70 #endif
71 
72   ss << "Line of code : " << line_of_code << "\n";
73   if (file_name != nullptr) {
74     ss << "File         : " << file_name << "\n";
75   }
76   data_->status_msg = ss.str();
77 }
78 
StatusCode() const79 enum StatusCode Status::StatusCode() const {
80   if (data_ == nullptr) {
81     return kSuccess;
82   }
83   return data_->status_code;
84 }
85 
ToCString() const86 std::vector<char> Status::ToCString() const {
87   if (data_ == nullptr) {
88     return std::vector<char>();
89   }
90   return StringToChar(data_->status_msg);
91 }
92 
GetLineOfCode() const93 int Status::GetLineOfCode() const {
94   if (data_ == nullptr) {
95     return -1;
96   }
97   return data_->line_of_code;
98 }
99 
GetErrDescriptionChar() const100 std::vector<char> Status::GetErrDescriptionChar() const {
101   if (data_ == nullptr) {
102     return std::vector<char>();
103   }
104   return StringToChar(data_->err_description);
105 }
106 
CodeAsCString(enum StatusCode c)107 std::vector<char> Status::CodeAsCString(enum StatusCode c) {
108   static std::map<enum StatusCode, std::string> info_map = {{kSuccess, "No error occurs."},
109                                                             // Core
110                                                             {kCoreFailed, "Common error code."},
111                                                             // MD
112                                                             {kMDOutOfMemory, "Out of memory"},
113                                                             {kMDShapeMisMatch, "Shape is incorrect"},
114                                                             {kMDInterrupted, "Interrupted system call"},
115                                                             {kMDNoSpace, "No space left on device"},
116                                                             {kMDPyFuncException, "Exception thrown from PyFunc"},
117                                                             {kMDDuplicateKey, "Duplicate key"},
118                                                             {kMDPythonInterpreterFailure, ""},
119                                                             {kMDTDTPushFailure, "Unexpected error"},
120                                                             {kMDFileNotExist, "Unexpected error"},
121                                                             {kMDProfilingError, "Error encountered while profiling"},
122                                                             {kMDBoundingBoxOutOfBounds, "Unexpected error"},
123                                                             {kMDBoundingBoxInvalidShape, "Unexpected error"},
124                                                             {kMDSyntaxError, "Syntax error"},
125                                                             {kMDTimeOut, "Unexpected error"},
126                                                             {kMDBuddySpaceFull, "BuddySpace full"},
127                                                             {kMDNetWorkError, "Network error"},
128                                                             {kMDNotImplementedYet, "Unexpected error"},
129                                                             {kMDUnexpectedError, "Unexpected error"},
130                                                             // ME
131                                                             {kMEFailed, "Common error code."},
132                                                             {kMEInvalidInput, "Invalid input."},
133                                                             // MC
134                                                             {kMCFailed, "Common error code."},
135                                                             {kMCDeviceError, "Device error."},
136                                                             {kMCInvalidInput, "Invalid input."},
137                                                             {kMCInvalidArgs, "Invalid arguments."},
138                                                             // Lite
139                                                             {kLiteError, "Common error code."},
140                                                             {kLiteNullptr, "NULL pointer returned."},
141                                                             {kLiteParamInvalid, "Invalid parameter."},
142                                                             {kLiteNoChange, "No change."},
143                                                             {kLiteSuccessExit, "No error but exit."},
144                                                             {kLiteMemoryFailed, "Fail to create memory."},
145                                                             {kLiteNotSupport, "Fail to support."},
146                                                             {kLiteThreadPoolError, "Thread pool error."},
147                                                             {kLiteOutOfTensorRange, "Failed to check range."},
148                                                             {kLiteInputTensorError, "Failed to check input tensor."},
149                                                             {kLiteReentrantError, "Exist executor running."},
150                                                             {kLiteGraphFileError, "Failed to verify graph file."},
151                                                             {kLiteNotFindOp, "Failed to find operator."},
152                                                             {kLiteInvalidOpName, "Invalid operator name."},
153                                                             {kLiteInvalidOpAttr, "Invalid operator attr."},
154                                                             {kLiteOpExecuteFailure, "Failed to execution operator."},
155                                                             {kLiteFormatError, "Failed to checking tensor format."},
156                                                             {kLiteInferError, "Failed to infer shape."},
157                                                             {kLiteInferInvalid, "Invalid infer shape before runtime."},
158                                                             {kLiteInputParamInvalid, "Invalid input param by user."}};
159   auto iter = info_map.find(c);
160   return StringToChar(iter == info_map.end() ? "Unknown error" : iter->second);
161 }
162 
operator <<(std::ostream & os,const Status & s)163 std::ostream &operator<<(std::ostream &os, const Status &s) {
164   os << s.ToString();
165   return os;
166 }
167 
SetErrDescription(const std::vector<char> & err_description)168 std::vector<char> Status::SetErrDescription(const std::vector<char> &err_description) {
169   if (data_ == nullptr) {
170     return std::vector<char>();
171   }
172   data_->err_description = CharToString(err_description);
173   std::ostringstream ss;
174 #ifndef ENABLE_ANDROID
175 #ifdef DEBUG
176   ss << "Thread ID " << std::this_thread::get_id() << " " << CodeAsString(data_->status_code) << ". ";
177 #else
178   ss << CodeAsString(data_->status_code) << ". ";
179 #endif
180   if (!data_->err_description.empty()) {
181     ss << data_->err_description;
182   }
183   ss << "\n";
184 #endif
185 
186   if (data_->line_of_code > 0 && !data_->file_name.empty()) {
187     ss << "Line of code : " << data_->line_of_code << "\n";
188     ss << "File         : " << data_->file_name << "\n";
189   }
190   data_->status_msg = ss.str();
191   return StringToChar(data_->status_msg);
192 }
193 
operator ==(const Status & other) const194 bool Status::operator==(const Status &other) const {
195   if (data_ == nullptr && other.data_ == nullptr) {
196     return true;
197   }
198 
199   if (data_ == nullptr || other.data_ == nullptr) {
200     return false;
201   }
202 
203   return data_->status_code == other.data_->status_code;
204 }
205 
operator ==(enum StatusCode other_code) const206 bool Status::operator==(enum StatusCode other_code) const { return StatusCode() == other_code; }
operator !=(const Status & other) const207 bool Status::operator!=(const Status &other) const { return !operator==(other); }
operator !=(enum StatusCode other_code) const208 bool Status::operator!=(enum StatusCode other_code) const { return !operator==(other_code); }
209 
operator bool() const210 Status::operator bool() const { return (StatusCode() == kSuccess); }
operator int() const211 Status::operator int() const { return static_cast<int>(StatusCode()); }
212 
OK()213 Status Status::OK() { return StatusCode::kSuccess; }
IsOk() const214 bool Status::IsOk() const { return (StatusCode() == StatusCode::kSuccess); }
IsError() const215 bool Status::IsError() const { return !IsOk(); }
216 }  // namespace mindspore
217