Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
e3f5fdcc
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
e3f5fdcc
编写于
7月 25, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make PADDLE_ENFORCE and PADDLE_THROW catchable
* Use EnforceNotMet to unify all exception types.
上级
0c2790f7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
38 addition
and
32 deletion
+38
-32
paddle/platform/enforce.h
paddle/platform/enforce.h
+37
-31
paddle/platform/enforce_test.cc
paddle/platform/enforce_test.cc
+1
-1
未找到文件。
paddle/platform/enforce.h
浏览文件 @
e3f5fdcc
...
@@ -36,6 +36,21 @@ limitations under the License. */
...
@@ -36,6 +36,21 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
platform
{
namespace
platform
{
struct
EnforceNotMet
:
public
std
::
exception
{
std
::
exception_ptr
exp_
;
std
::
string
err_str_
;
EnforceNotMet
(
std
::
exception_ptr
e
,
const
char
*
f
,
int
l
)
:
exp_
(
e
)
{
try
{
std
::
rethrow_exception
(
exp_
);
}
catch
(
const
std
::
exception
&
exp
)
{
err_str_
=
string
::
Sprintf
(
"%s at [%s:%d]"
,
exp
.
what
(),
f
,
l
);
}
}
const
char
*
what
()
const
noexcept
{
return
err_str_
.
c_str
();
}
};
// Because most enforce conditions would evaluate to true, we can use
// Because most enforce conditions would evaluate to true, we can use
// __builtin_expect to instruct the C++ compiler to generate code that
// __builtin_expect to instruct the C++ compiler to generate code that
// always forces branch prediction of true.
// always forces branch prediction of true.
...
@@ -52,9 +67,7 @@ template <typename... Args>
...
@@ -52,9 +67,7 @@ template <typename... Args>
inline
typename
std
::
enable_if
<
sizeof
...(
Args
)
!=
0
,
void
>::
type
throw_on_error
(
inline
typename
std
::
enable_if
<
sizeof
...(
Args
)
!=
0
,
void
>::
type
throw_on_error
(
int
stat
,
const
Args
&
...
args
)
{
int
stat
,
const
Args
&
...
args
)
{
if
(
UNLIKELY
(
!
(
stat
)))
{
if
(
UNLIKELY
(
!
(
stat
)))
{
throw
std
::
runtime_error
(
throw
std
::
runtime_error
(
string
::
Sprintf
(
args
...));
string
::
Sprintf
(
args
...)
+
string
::
Sprintf
(
" at [%s:%s];"
,
__FILE__
,
__LINE__
));
}
}
}
}
...
@@ -64,12 +77,8 @@ template <typename... Args>
...
@@ -64,12 +77,8 @@ template <typename... Args>
inline
typename
std
::
enable_if
<
sizeof
...(
Args
)
!=
0
,
void
>::
type
throw_on_error
(
inline
typename
std
::
enable_if
<
sizeof
...(
Args
)
!=
0
,
void
>::
type
throw_on_error
(
cudaError_t
e
,
const
Args
&
...
args
)
{
cudaError_t
e
,
const
Args
&
...
args
)
{
if
(
UNLIKELY
(
e
))
{
if
(
UNLIKELY
(
e
))
{
// clang-format off
throw
thrust
::
system_error
(
e
,
thrust
::
cuda_category
(),
throw
thrust
::
system_error
(
string
::
Sprintf
(
args
...));
e
,
thrust
::
cuda_category
(),
string
::
Sprintf
(
args
...)
+
string
::
Sprintf
(
" at [%s:%s];"
,
__FILE__
,
__LINE__
));
// clang-format on
}
}
}
}
...
@@ -77,12 +86,8 @@ template <typename... Args>
...
@@ -77,12 +86,8 @@ template <typename... Args>
inline
typename
std
::
enable_if
<
sizeof
...(
Args
)
!=
0
,
void
>::
type
throw_on_error
(
inline
typename
std
::
enable_if
<
sizeof
...(
Args
)
!=
0
,
void
>::
type
throw_on_error
(
curandStatus_t
stat
,
const
Args
&
...
args
)
{
curandStatus_t
stat
,
const
Args
&
...
args
)
{
if
(
stat
!=
CURAND_STATUS_SUCCESS
)
{
if
(
stat
!=
CURAND_STATUS_SUCCESS
)
{
// clang-format off
throw
thrust
::
system_error
(
cudaErrorLaunchFailure
,
thrust
::
cuda_category
(),
throw
thrust
::
system_error
(
string
::
Sprintf
(
args
...));
cudaErrorLaunchFailure
,
thrust
::
cuda_category
(),
string
::
Sprintf
(
args
...)
+
string
::
Sprintf
(
" at [%s:%s];"
,
__FILE__
,
__LINE__
));
// clang-format on
}
}
}
}
...
@@ -92,12 +97,8 @@ inline typename std::enable_if<sizeof...(Args) != 0, void>::type throw_on_error(
...
@@ -92,12 +97,8 @@ inline typename std::enable_if<sizeof...(Args) != 0, void>::type throw_on_error(
if
(
stat
==
CUDNN_STATUS_SUCCESS
)
{
if
(
stat
==
CUDNN_STATUS_SUCCESS
)
{
return
;
return
;
}
else
{
}
else
{
// clang-format off
throw
std
::
runtime_error
(
platform
::
dynload
::
cudnnGetErrorString
(
stat
)
+
throw
std
::
runtime_error
(
string
::
Sprintf
(
args
...));
platform
::
dynload
::
cudnnGetErrorString
(
stat
)
+
string
::
Sprintf
(
args
...)
+
string
::
Sprintf
(
" at [%s:%s];"
,
__FILE__
,
__LINE__
));
// clang-format on
}
}
}
}
...
@@ -126,22 +127,27 @@ inline typename std::enable_if<sizeof...(Args) != 0, void>::type throw_on_error(
...
@@ -126,22 +127,27 @@ inline typename std::enable_if<sizeof...(Args) != 0, void>::type throw_on_error(
}
else
if
(
stat
==
CUBLAS_STATUS_LICENSE_ERROR
)
{
}
else
if
(
stat
==
CUBLAS_STATUS_LICENSE_ERROR
)
{
err
=
"CUBLAS: license error, "
;
err
=
"CUBLAS: license error, "
;
}
}
throw
std
::
runtime_error
(
err
+
string
::
Sprintf
(
args
...)
+
throw
std
::
runtime_error
(
err
+
string
::
Sprintf
(
args
...));
string
::
Sprintf
(
" at [%s:%s];"
,
__FILE__
,
__LINE__
));
}
}
#endif // PADDLE_ONLY_CPU
#endif // PADDLE_ONLY_CPU
#define PADDLE_THROW(...) \
#define PADDLE_THROW(...) \
do { \
do { \
throw std::runtime_error( \
throw ::paddle::platform::EnforceNotMet( \
string::Sprintf(__VA_ARGS__) + \
std::make_exception_ptr( \
string::Sprintf(" at [%s:%s];", __FILE__, __LINE__)); \
std::runtime_error(string::Sprintf(__VA_ARGS__))), \
__FILE__, __LINE__); \
} while (0)
} while (0)
#define PADDLE_ENFORCE(...) \
#define PADDLE_ENFORCE(...) \
do { \
do { \
::paddle::platform::throw_on_error(__VA_ARGS__); \
try { \
::paddle::platform::throw_on_error(__VA_ARGS__); \
} catch (...) { \
throw ::paddle::platform::EnforceNotMet(std::current_exception(), \
__FILE__, __LINE__); \
} \
} while (0)
} while (0)
}
// namespace platform
}
// namespace platform
...
...
paddle/platform/enforce_test.cc
浏览文件 @
e3f5fdcc
...
@@ -23,7 +23,7 @@ TEST(ENFORCE, FAILED) {
...
@@ -23,7 +23,7 @@ TEST(ENFORCE, FAILED) {
bool
in_catch
=
false
;
bool
in_catch
=
false
;
try
{
try
{
PADDLE_ENFORCE
(
false
,
"Enforce is not ok %d at all"
,
123
);
PADDLE_ENFORCE
(
false
,
"Enforce is not ok %d at all"
,
123
);
}
catch
(
const
std
::
runtime_error
&
error
)
{
}
catch
(
paddle
::
platform
::
EnforceNotMet
error
)
{
// your error handling code here
// your error handling code here
in_catch
=
true
;
in_catch
=
true
;
std
::
string
msg
=
"Enforce is not ok 123 at all"
;
std
::
string
msg
=
"Enforce is not ok 123 at all"
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录