1 /**
2 * Copyright 2020-2022 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/visible.h"
26 namespace mindspore {
27 enum CompCode : uint32_t {
28 kCore = 0x00000000u,
29 kMD = 0x10000000u,
30 kME = 0x20000000u,
31 kMC = 0x30000000u,
32 kLite = 0xF0000000u,
33 };
34
35 enum StatusCode : uint32_t {
36 kSuccess = 0,
37 // Core
38 kCoreFailed = kCore | 0x1,
39
40 // MD
41 kMDOutOfMemory = kMD | 1,
42 kMDShapeMisMatch = kMD | 2,
43 kMDInterrupted = kMD | 3,
44 kMDNoSpace = kMD | 4,
45 kMDPyFuncException = kMD | 5,
46 kMDDuplicateKey = kMD | 6,
47 kMDPythonInterpreterFailure = kMD | 7,
48 kMDTDTPushFailure = kMD | 8,
49 kMDFileNotExist = kMD | 9,
50 kMDProfilingError = kMD | 10,
51 kMDBoundingBoxOutOfBounds = kMD | 11,
52 kMDBoundingBoxInvalidShape = kMD | 12,
53 kMDSyntaxError = kMD | 13,
54 kMDTimeOut = kMD | 14,
55 kMDBuddySpaceFull = kMD | 15,
56 kMDNetWorkError = kMD | 16,
57 kMDNotImplementedYet = kMD | 17,
58 // Make this error code the last one. Add new error code above it.
59 kMDUnexpectedError = kMD | 127,
60
61 // ME
62 kMEFailed = kME | 0x1,
63 kMEInvalidInput = kME | 0x2,
64
65 // MC
66 kMCFailed = kMC | 0x1,
67 kMCDeviceError = kMC | 0x2,
68 kMCInvalidInput = kMC | 0x3,
69 kMCInvalidArgs = kMC | 0x4,
70
71 // Lite // Common error code, range: [-1, -100)
72 kLiteError = kLite | (0x0FFFFFFF & -1), /**< Common error code. */
73 kLiteNullptr = kLite | (0x0FFFFFFF & -2), /**< NULL pointer returned.*/
74 kLiteParamInvalid = kLite | (0x0FFFFFFF & -3), /**< Invalid parameter.*/
75 kLiteNoChange = kLite | (0x0FFFFFFF & -4), /**< No change. */
76 kLiteSuccessExit = kLite | (0x0FFFFFFF & -5), /**< No error but exit. */
77 kLiteMemoryFailed = kLite | (0x0FFFFFFF & -6), /**< Fail to create memory. */
78 kLiteNotSupport = kLite | (0x0FFFFFFF & -7), /**< Fail to support. */
79 kLiteThreadPoolError = kLite | (0x0FFFFFFF & -8), /**< Error occur in thread pool. */
80 kLiteUninitializedObj = kLite | (0x0FFFFFFF & -9), /**< Object is not initialized. */
81 kLiteFileError = kLite | (0x0FFFFFFF & -10), /**< Invalid file. */
82 kLiteServiceDeny = kLite | (0x0FFFFFFF & -11), /**< Denial of service. */
83 kLiteModelRebuild = kLite | (0x0FFFFFFF & -12), /**< Model has been built. */
84
85 // Executor error code, range: [-100,-200)
86 kLiteOutOfTensorRange = kLite | (0x0FFFFFFF & -100), /**< Failed to check range. */
87 kLiteInputTensorError = kLite | (0x0FFFFFFF & -101), /**< Failed to check input tensor. */
88 kLiteReentrantError = kLite | (0x0FFFFFFF & -102), /**< Exist executor running. */
89 kLiteLLMWaitProcessTimeOut = kLite | (0x0FFFFFFF & -103), /**< Wait to be processed time out. */
90 kLiteLLMKVCacheNotExist = kLite | (0x0FFFFFFF & -104), /**< KV Cache not exist. */
91 kLiteLLMRepeatRequest = kLite | (0x0FFFFFFF & -105), /**< repeat request. */
92 kLiteLLMRequestAlreadyCompleted = kLite | (0x0FFFFFFF & -106), /**< request already complete!. */
93 kLiteLLMEngineFinalized = kLite | (0x0FFFFFFF & -107), /**< llm engine finalized. */
94 kLiteLLMNotYetLink = kLite | (0x0FFFFFFF & -108), /**< decoder cluster is no link with prompt. */
95 kLiteLLMAlreadyLink = kLite | (0x0FFFFFFF & -109), /**< decoder cluster is already linked with prompt cluster! */
96 kLiteLLMLinkFailed = kLite | (0x0FFFFFFF & -110), /**< decoder cluster link with prompt cluster failed! */
97 kLiteLLMUnlinkFailed = kLite | (0x0FFFFFFF & -111), /**< decoder cluster unlink with prompt cluster failed */
98 kLiteLLMNofiryPromptUnlinkFailed =
99 kLite | (0x0FFFFFFF & -112), /**< decoder cluster notify prompt cluster do unlink failed */
100 kLiteLLMClusterNumExceedLimit = kLite | (0x0FFFFFFF & -113), /**< cluster num exceed limit */
101 kLiteLLMProcessingLink = kLite | (0x0FFFFFFF & -114), /**< link is current processing. */
102 kLiteLLMOutOfMemory = kLite | (0x0FFFFFFF & -115), /**< device out of memory. */
103 kLiteLLMPrefixAlreadyExist = kLite | (0x0FFFFFFF & -116), /**< Prefix has already existed. */
104 kLiteLLMPrefixNotExist = kLite | (0x0FFFFFFF & -117), /**< Prefix does not exist. */
105 kLiteLLMSeqLenOverLimit = kLite | (0x0FFFFFFF & -118), /**< Sequence length exceed limit. */
106 kLiteLLMNoFreeBlock = kLite | (0x0FFFFFFF & -119), /**< No free block. */
107 kLiteLLMBlockOutOfMemory = kLite | (0x0FFFFFFF & -120), /**< Block is out of memory. */
108
109 // Graph error code, range: [-200,-300)
110 kLiteGraphFileError = kLite | (0x0FFFFFFF & -200), /**< Failed to verify graph file. */
111
112 // Node error code, range: [-300,-400)
113 kLiteNotFindOp = kLite | (0x0FFFFFFF & -300), /**< Failed to find operator. */
114 kLiteInvalidOpName = kLite | (0x0FFFFFFF & -301), /**< Invalid operator name. */
115 kLiteInvalidOpAttr = kLite | (0x0FFFFFFF & -302), /**< Invalid operator attr. */
116 kLiteOpExecuteFailure = kLite | (0x0FFFFFFF & -303), /**< Failed to execution operator. */
117
118 // Tensor error code, range: [-400,-500)
119 kLiteFormatError = kLite | (0x0FFFFFFF & -400), /**< Failed to checking tensor format. */
120
121 // InferShape error code, range: [-500,-600)
122 kLiteInferError = kLite | (0x0FFFFFFF & -500), /**< Failed to infer shape. */
123 kLiteInferInvalid = kLite | (0x0FFFFFFF & -501), /**< Invalid infer shape before runtime. */
124
125 // User input param error code, range: [-600, 700)
126 kLiteInputParamInvalid = kLite | (0x0FFFFFFF & -600), /**< Invalid input param by user. */
127 };
128
129 class MS_API Status {
130 public:
131 Status();
132 inline Status(enum StatusCode status_code, const std::string &status_msg = ""); // NOLINT(runtime/explicit)
133 inline Status(const StatusCode code, int line_of_code, const char *file_name, const std::string &extra = "");
134
135 ~Status() = default;
136
137 enum StatusCode StatusCode() const;
138 inline std::string ToString() const;
139
140 int GetLineOfCode() const;
141 inline std::string GetFileName() const;
142 inline std::string GetErrDescription() const;
143 inline std::string SetErrDescription(const std::string &err_description);
144 inline void SetStatusMsg(const std::string &status_msg);
145
146 MS_API friend std::ostream &operator<<(std::ostream &os, const Status &s);
147
148 bool operator==(const Status &other) const;
149 bool operator==(enum StatusCode other_code) const;
150 bool operator!=(const Status &other) const;
151 bool operator!=(enum StatusCode other_code) const;
152
153 explicit operator bool() const;
154 explicit operator int() const;
155
156 static Status OK();
157
158 bool IsOk() const;
159
160 bool IsError() const;
161
162 static inline std::string CodeAsString(enum StatusCode c);
163
164 private:
165 // api without std::string
166 Status(enum StatusCode status_code, const std::vector<char> &status_msg);
167 Status(enum StatusCode code, int line_of_code, const char *file_name, const std::vector<char> &extra);
168 std::vector<char> ToCString() const;
169 std::vector<char> GetFileNameChar() const;
170 std::vector<char> GetErrDescriptionChar() const;
171 std::vector<char> SetErrDescription(const std::vector<char> &err_description);
172 void SetStatusMsgChar(const std::vector<char> &status_msg);
173 static std::vector<char> CodeAsCString(enum StatusCode c);
174
175 struct Data;
176 std::shared_ptr<Data> data_;
177 };
178
Status(enum StatusCode status_code,const std::string & status_msg)179 Status::Status(enum StatusCode status_code, const std::string &status_msg)
180 : Status(status_code, StringToChar(status_msg)) {}
Status(const enum StatusCode code,int line_of_code,const char * file_name,const std::string & extra)181 Status::Status(const enum StatusCode code, int line_of_code, const char *file_name, const std::string &extra)
182 : Status(code, line_of_code, file_name, StringToChar(extra)) {}
ToString()183 std::string Status::ToString() const { return CharToString(ToCString()); }
GetFileName()184 std::string Status::GetFileName() const { return CharToString(GetFileNameChar()); }
GetErrDescription()185 std::string Status::GetErrDescription() const { return CharToString(GetErrDescriptionChar()); }
SetErrDescription(const std::string & err_description)186 std::string Status::SetErrDescription(const std::string &err_description) {
187 return CharToString(SetErrDescription(StringToChar(err_description)));
188 }
SetStatusMsg(const std::string & status_msg)189 void Status::SetStatusMsg(const std::string &status_msg) { SetStatusMsgChar(StringToChar(status_msg)); }
CodeAsString(enum StatusCode c)190 std::string Status::CodeAsString(enum StatusCode c) { return CharToString(CodeAsCString(c)); }
191 } // namespace mindspore
192 #endif // MINDSPORE_INCLUDE_API_STATUS_H
193