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