@@ -71,9 +71,8 @@ struct EnforceNotMet : public std::exception {
71
71
}
72
72
}
73
73
74
- template <typename ... ARGS>
75
- EnforceNotMet (const char * f, int l, ARGS... args) {
76
- Init (string::Sprintf (args...), f, l);
74
+ EnforceNotMet (const std::string& str, const char * f, int l) {
75
+ Init (str, f, l);
77
76
}
78
77
79
78
const char * what () const noexcept override { return err_str_.c_str (); }
@@ -142,68 +141,56 @@ struct EOFException : public std::exception {
142
141
143
142
inline bool is_error (bool stat) { return !stat; }
144
143
145
- template <typename ... Args>
146
- inline typename std::enable_if<sizeof ...(Args) != 0 , void >::type throw_on_error (
147
- bool stat, const Args&... args) {
144
+ inline void throw_on_error (bool stat, const std::string& msg) {
148
145
#ifndef REPLACE_ENFORCE_GLOG
149
- throw std::runtime_error (string::Sprintf (args...) );
146
+ throw std::runtime_error (msg );
150
147
#else
151
- LOG (FATAL) << string::Sprintf (args...) ;
148
+ LOG (FATAL) << msg ;
152
149
#endif
153
150
}
154
151
155
152
#ifdef PADDLE_WITH_CUDA
156
153
157
- inline bool is_error (cudaError_t e) { return UNLIKELY (e) ; }
154
+ inline bool is_error (cudaError_t e) { return e != cudaSuccess ; }
158
155
159
- template <typename ... Args>
160
- inline typename std::enable_if<sizeof ...(Args) != 0 , void >::type throw_on_error (
161
- cudaError_t e, const Args&... args) {
156
+ inline void throw_on_error (cudaError_t e, const std::string& msg) {
162
157
#ifndef REPLACE_ENFORCE_GLOG
163
- throw thrust::system_error (e, thrust::cuda_category (),
164
- string::Sprintf (args...));
158
+ throw thrust::system_error (e, thrust::cuda_category (), msg);
165
159
#else
166
- LOG (FATAL) << string::Sprintf (args...) ;
160
+ LOG (FATAL) << msg ;
167
161
#endif
168
162
}
169
163
170
164
inline bool is_error (curandStatus_t stat) {
171
165
return stat != CURAND_STATUS_SUCCESS;
172
166
}
173
167
174
- template <typename ... Args>
175
- inline typename std::enable_if<sizeof ...(Args) != 0 , void >::type throw_on_error (
176
- curandStatus_t stat, const Args&... args) {
168
+ inline void throw_on_error (curandStatus_t stat, const std::string& msg) {
177
169
#ifndef REPLACE_ENFORCE_GLOG
178
170
throw thrust::system_error (cudaErrorLaunchFailure, thrust::cuda_category (),
179
- string::Sprintf (args...) );
171
+ msg );
180
172
#else
181
- LOG (FATAL) << string::Sprintf (args...) ;
173
+ LOG (FATAL) << msg ;
182
174
#endif
183
175
}
184
176
185
177
inline bool is_error (cudnnStatus_t stat) {
186
178
return stat != CUDNN_STATUS_SUCCESS;
187
179
}
188
180
189
- template <typename ... Args>
190
- inline typename std::enable_if<sizeof ...(Args) != 0 , void >::type throw_on_error (
191
- cudnnStatus_t stat, const Args&... args) {
181
+ inline void throw_on_error (cudnnStatus_t stat, const std::string& msg) {
192
182
#ifndef REPLACE_ENFORCE_GLOG
193
- throw std::runtime_error (platform::dynload::cudnnGetErrorString (stat) +
194
- string::Sprintf (args...));
183
+ throw std::runtime_error (platform::dynload::cudnnGetErrorString (stat) + msg);
195
184
#else
196
- LOG (FATAL) << string::Sprintf (args...) ;
185
+ LOG (FATAL) << platform::dynload::cudnnGetErrorString (stat) << msg ;
197
186
#endif
198
187
}
199
188
200
189
inline bool is_error (cublasStatus_t stat) {
201
190
return stat != CUBLAS_STATUS_SUCCESS;
202
191
}
203
192
204
- template <typename ... Args>
205
- inline typename std::enable_if<sizeof ...(Args) != 0 , void >::type throw_on_error (
206
- cublasStatus_t stat, const Args&... args) {
193
+ inline void throw_on_error (cublasStatus_t stat, const std::string& msg) {
207
194
std::string err;
208
195
if (stat == CUBLAS_STATUS_NOT_INITIALIZED) {
209
196
err = " CUBLAS: not initialized, " ;
@@ -225,87 +212,45 @@ inline typename std::enable_if<sizeof...(Args) != 0, void>::type throw_on_error(
225
212
err = " CUBLAS: license error, " ;
226
213
}
227
214
#ifndef REPLACE_ENFORCE_GLOG
228
- throw std::runtime_error (err + string::Sprintf (args...) );
215
+ throw std::runtime_error (err + msg );
229
216
#else
230
- LOG (FATAL) << err << string::Sprintf (args...) ;
217
+ LOG (FATAL) << err << msg ;
231
218
#endif
232
219
}
233
220
234
221
#if !defined(__APPLE__) && !defined(_WIN32)
235
- template <typename ... Args>
236
- inline typename std::enable_if<sizeof ...(Args) != 0 , void >::type throw_on_error (
237
- ncclResult_t stat, const Args&... args) {
238
- if (stat == ncclSuccess) {
239
- return ;
240
- } else {
222
+ inline bool is_error (ncclResult_t nccl_result) {
223
+ return nccl_result != ncclSuccess;
224
+ }
225
+
226
+ inline void throw_on_error (ncclResult_t stat, const std::string& msg) {
241
227
#ifndef REPLACE_ENFORCE_GLOG
242
- throw std::runtime_error (platform::dynload::ncclGetErrorString (stat) +
243
- string::Sprintf (args...));
228
+ throw std::runtime_error (platform::dynload::ncclGetErrorString (stat) + msg);
244
229
#else
245
- LOG (FATAL) << platform::dynload::ncclGetErrorString (stat)
246
- << string::Sprintf (args...);
230
+ LOG (FATAL) << platform::dynload::ncclGetErrorString (stat) << msg;
247
231
#endif
248
- }
249
232
}
250
233
#endif // __APPLE__ and windows
251
234
#endif // PADDLE_WITH_CUDA
252
235
253
- template <typename T>
254
- inline void throw_on_error (T e) {
255
- throw_on_error (e, " " );
256
- }
257
-
258
- #define PADDLE_THROW (...) \
259
- throw ::paddle::platform::EnforceNotMet (__FILE__, __LINE__, __VA_ARGS__)
260
-
261
- #define __PADDLE_THROW_ERROR_I (_, _9, _8, _7, _6, _5, _4, _3, _2, X_, ...) X_;
262
-
263
- #define __THROW_ON_ERROR_ONE_ARG (COND, ARG ) \
264
- ::paddle::platform::throw_on_error (COND, ::paddle::string::Sprintf(ARG));
265
-
266
- #ifdef _WIN32
267
- #define __PADDLE_THROW_ON_ERROR (COND, ...) \
268
- __THROW_ON_ERROR_ONE_ARG (COND, __VA_ARGS__)
269
- #else // _WIN32
270
- #define __PADDLE_THROW_ON_ERROR (COND, ...) \
271
- __PADDLE_THROW_ERROR_I ( \
272
- __VA_ARGS__, ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
273
- ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
274
- ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
275
- ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
276
- ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
277
- ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
278
- ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
279
- ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \
280
- __THROW_ON_ERROR_ONE_ARG(COND, __VA_ARGS__))
281
- #endif // _WIN32
282
-
283
- #define __PADDLE_UNARY_COMPARE (COND, ...) \
284
- do { \
285
- auto __cond = COND; \
286
- if (UNLIKELY (::paddle::platform::is_error (__cond))) { \
287
- __PADDLE_THROW_ON_ERROR (__cond, __VA_ARGS__); \
288
- } \
236
+ #define PADDLE_THROW (...) \
237
+ throw ::paddle::platform::EnforceNotMet ( \
238
+ ::paddle::string::Sprintf (__VA_ARGS__), __FILE__, __LINE__)
239
+
240
+ #define PADDLE_ENFORCE (COND, ...) \
241
+ do { \
242
+ auto __cond__ = (COND); \
243
+ if (UNLIKELY (::paddle::platform::is_error (__cond__))) { \
244
+ try { \
245
+ ::paddle::platform::throw_on_error ( \
246
+ __cond__, ::paddle::string::Sprintf(__VA_ARGS__)); \
247
+ } catch (...) { \
248
+ throw ::paddle::platform::EnforceNotMet (std::current_exception (), \
249
+ __FILE__, __LINE__); \
250
+ } \
251
+ } \
289
252
} while (0 )
290
253
291
- #ifndef REPLACE_ENFORCE_GLOG
292
- #define __PADDLE_ENFORCE_I (COND, ...) \
293
- do { \
294
- try { \
295
- __PADDLE_UNARY_COMPARE (COND, __VA_ARGS__); \
296
- } catch (...) { \
297
- throw ::paddle::platform::EnforceNotMet (std::current_exception (), \
298
- __FILE__, __LINE__); \
299
- } \
300
- } while (0 )
301
-
302
- #else
303
- #define __PADDLE_ENFORCE_I (COND, ...) __PADDLE_UNARY_COMPARE(COND, __VA_ARGS__);
304
- #endif // REPLACE_ENFORCE_GLOG
305
-
306
- #define __PADDLE_ENFORCE (__args ) __PADDLE_ENFORCE_I __args
307
- #define PADDLE_ENFORCE (...) __PADDLE_ENFORCE((__VA_ARGS__))
308
-
309
254
#define PADDLE_THROW_EOF () \
310
255
do { \
311
256
throw ::paddle::platform::EOFException (" There is no next data." , __FILE__, \
0 commit comments