• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022-2022 Huawei Technologies Co., Ltd.  All rights reserved.
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 
17 /*!
18  * \file error_util.h
19  * \brief
20  */
21 #ifndef CUSTOMIZE_OP_PROTO_UTILS_ERROR_UTIL_H_
22 #define CUSTOMIZE_OP_PROTO_UTILS_ERROR_UTIL_H_
23 
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 #include "error_code.h"
28 #include "graph/operator.h"
29 #include "op_proto/utils/op_log.h"
30 
31 #define AICPU_INFER_SHAPE_CALL_ERR_REPORT(op_name, err_msg)   \
32   do {                                                        \
33     OP_LOGE_WITHOUT_REPORT(op_name, "%s", get_cstr(err_msg)); \
34   } while (0)
35 
36 #define AICPU_INFER_SHAPE_INNER_ERR_REPORT(op_name, err_msg)  \
37   do {                                                        \
38     OP_LOGE_WITHOUT_REPORT(op_name, "%s", get_cstr(err_msg)); \
39   } while (0)
40 
41 #define VECTOR_INFER_SHAPE_INNER_ERR_REPORT(op_name, err_msg) \
42   do {                                                        \
43     OP_LOGE_WITHOUT_REPORT(op_name, "%s", get_cstr(err_msg)); \
44   } while (0)
45 
46 #define INFER_AXIS_TYPE_ERR_REPORT(op_name, err_msg, ...)    \
47   do {                                                       \
48     OP_LOGE_WITHOUT_REPORT(op_name, err_msg, ##__VA_ARGS__); \
49   } while (0)
50 
51 #define VECTOR_FUSION_INNER_ERR_REPORT(op_name, err_msg, ...) \
52   do {                                                        \
53     OP_LOGE_WITHOUT_REPORT(op_name, err_msg, ##__VA_ARGS__);  \
54   } while (0)
55 
56 #define VECTOR_CHECK_NULLPTR_RETURN_WITH_REPORT(op_name, ptr, ret, err_msg, ...) \
57   do {                                                                           \
58     if ((ptr) == nullptr) {                                                      \
59       VECTOR_FUSION_INNER_ERR_REPORT(op_name, err_msg, ##__VA_ARGS__);           \
60       return (ret);                                                              \
61     }                                                                            \
62   } while (0)
63 
64 #define CUBE_INNER_ERR_REPORT(op_name, err_msg, ...)         \
65   do {                                                       \
66     OP_LOGE_WITHOUT_REPORT(op_name, err_msg, ##__VA_ARGS__); \
67   } while (0)
68 
69 #define CUBE_CALL_ERR_REPORT(op_name, err_msg, ...)          \
70   do {                                                       \
71     OP_LOGE_WITHOUT_REPORT(op_name, err_msg, ##__VA_ARGS__); \
72   } while (0)
73 
74 #define CUBE_INNER_ERR_REPORT_PLUGIN(op_name, err_msg, ...)  \
75   do {                                                       \
76     OP_LOGE_WITHOUT_REPORT(op_name, err_msg, ##__VA_ARGS__); \
77   } while (0)
78 
79 #define CUBE_CALL_ERR_REPORT_PLUGIN(op_name, err_msg, ...)   \
80   do {                                                       \
81     OP_LOGE_WITHOUT_REPORT(op_name, err_msg, ##__VA_ARGS__); \
82   } while (0)
83 
84 namespace optiling {
85 #define VECTOR_INNER_ERR_REPORT_TILIING(op_name, err_msg, ...) \
86   do {                                                         \
87     OP_LOGE_WITHOUT_REPORT(op_name, err_msg, ##__VA_ARGS__);   \
88   } while (0)
89 
90 #define OP_TILING_CHECK(cond, log_func, expr) \
91   do {                                        \
92     if (cond) {                               \
93       log_func;                               \
94       expr;                                   \
95     }                                         \
96   } while (0)
97 }  // namespace optiling
98 
99 namespace ge {
100 /*
101  * get debug string of vector
102  * param[in] v vector
103  * return vector's debug string
104  */
105 template <typename T>
DebugString(const std::vector<T> & v)106 std::string DebugString(const std::vector<T> &v) {
107   std::ostringstream oss;
108   oss << "[";
109   if (v.size() > 0) {
110     for (size_t i = 0; i < v.size() - 1; ++i) {
111       oss << v[i] << ", ";
112     }
113     oss << v[v.size() - 1];
114   }
115   oss << "]";
116   return oss.str();
117 }
118 
119 template <typename T>
DebugString(const std::vector<std::pair<T,T>> & v)120 std::string DebugString(const std::vector<std::pair<T, T>> &v) {
121   std::ostringstream oss;
122   oss << "[";
123   for (size_t i = 0; i < v.size(); ++i) {
124     if (i != 0) {
125       oss << ", ";
126     }
127     oss << "(" << v[i].first << ", " << v[i].second << ")";
128   }
129   oss << "]";
130   return oss.str();
131 }
132 
133 inline std::ostream &operator<<(std::ostream &os, const ge::Operator &op) { return os << get_op_info(op); }
134 
135 /*
136  * str cat util function
137  * param[in] params need concat to string
138  * return concatted string
139  */
140 template <typename T>
ConcatString(const T & arg)141 std::string ConcatString(const T &arg) {
142   std::ostringstream oss;
143   oss << arg;
144   return oss.str();
145 }
146 
147 template <typename T, typename... Ts>
ConcatString(const T & arg,const Ts &...arg_left)148 std::string ConcatString(const T &arg, const Ts &... arg_left) {
149   std::ostringstream oss;
150   oss << arg;
151   oss << ConcatString(arg_left...);
152   return oss.str();
153 }
154 
155 template <typename T>
Shape2String(const T & shape)156 std::string Shape2String(const T &shape) {
157   std::ostringstream oss;
158   oss << "[";
159   if (shape.GetDimNum() > 0) {
160     for (size_t i = 0; i < shape.GetDimNum() - 1; ++i) {
161       oss << shape.GetDim(i) << ", ";
162     }
163     oss << shape.GetDim(shape.GetDimNum() - 1);
164   }
165   oss << "]";
166   return oss.str();
167 }
168 
169 std::string GetViewErrorCodeStr(ge::ViewErrorCode errCode);
170 
171 std::string GetShapeErrMsg(uint32_t index, const std::string &wrong_shape, const std::string &correct_shape);
172 
173 std::string GetAttrValueErrMsg(const std::string &attr_name, const std::string &wrong_val,
174                                const std::string &correct_val);
175 
176 std::string GetAttrSizeErrMsg(const std::string &attr_name, const std::string &wrong_size,
177                               const std::string &correct_size);
178 
179 std::string GetInputInvalidErrMsg(const std::string &param_name);
180 std::string GetShapeSizeErrMsg(uint32_t index, const std::string &wrong_shape_size,
181                                const std::string &correct_shape_size);
182 
183 std::string GetInputFormatNotSupportErrMsg(const std::string &param_name, const std::string &expected_format_list,
184                                            const std::string &data_format);
185 
186 std::string GetInputDtypeNotSupportErrMsg(const std::string &param_name, const std::string &expected_dtype_list,
187                                           const std::string &data_dtype);
188 
189 std::string GetInputDTypeErrMsg(const std::string &param_name, const std::string &expected_dtype,
190                                 const std::string &data_dtype);
191 
192 std::string GetInputFormatErrMsg(const std::string &param_name, const std::string &expected_format,
193                                  const std::string &data_format);
194 
195 std::string SetAttrErrMsg(const std::string &param_name);
196 std::string UpdateParamErrMsg(const std::string &param_name);
197 
198 template <typename T>
199 std::string GetParamOutRangeErrMsg(const std::string &param_name, const T &real_value, const T &min, const T &max);
200 
201 std::string OtherErrMsg(const std::string &error_detail);
202 }  // namespace ge
203 
204 #endif  // CUSTOMIZE_OP_PROTO_UTILS_ERROR_UTIL_H_
205