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