Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
afeixing77
Paddle
提交
f812de2c
P
Paddle
项目概览
afeixing77
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f812de2c
编写于
7月 15, 2017
作者:
L
liaogang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ENH: unify PADDLE_ENFORCE
上级
48cf64e8
变更
14
显示空白变更内容
内联
并排
Showing
14 changed file
with
39 addition
and
238 deletion
+39
-238
paddle/framework/CMakeLists.txt
paddle/framework/CMakeLists.txt
+0
-1
paddle/framework/attr_checker.h
paddle/framework/attr_checker.h
+1
-1
paddle/framework/enforce.h
paddle/framework/enforce.h
+0
-69
paddle/framework/enforce_test.cc
paddle/framework/enforce_test.cc
+0
-35
paddle/framework/op_registry_test.cc
paddle/framework/op_registry_test.cc
+3
-3
paddle/framework/tensor.h
paddle/framework/tensor.h
+1
-1
paddle/framework/tensor_test.cc
paddle/framework/tensor_test.cc
+1
-1
paddle/memory/detail/system_allocator.cc
paddle/memory/detail/system_allocator.cc
+2
-3
paddle/platform/CMakeLists.txt
paddle/platform/CMakeLists.txt
+2
-0
paddle/platform/cpu_info.cc
paddle/platform/cpu_info.cc
+0
-1
paddle/platform/device_context.h
paddle/platform/device_context.h
+23
-30
paddle/platform/dynload/dynamic_loader.cc
paddle/platform/dynload/dynamic_loader.cc
+1
-1
paddle/platform/error.h
paddle/platform/error.h
+0
-87
paddle/platform/gpu_info.cc
paddle/platform/gpu_info.cc
+5
-5
未找到文件。
paddle/framework/CMakeLists.txt
浏览文件 @
f812de2c
...
...
@@ -5,7 +5,6 @@ nv_test(dim_test SRCS dim_test.cu DEPS ddim)
cc_test
(
tensor_test SRCS tensor_test.cc DEPS ddim
)
cc_test
(
variable_test SRCS variable_test.cc
)
cc_test
(
scope_test SRCS scope_test.cc
)
cc_test
(
enforce_test SRCS enforce_test.cc
)
proto_library
(
attr_type SRCS attr_type.proto
)
proto_library
(
op_proto SRCS op_proto.proto DEPS attr_type
)
cc_test
(
op_proto_test SRCS op_proto_test.cc DEPS op_proto protobuf
)
...
...
paddle/framework/attr_checker.h
浏览文件 @
f812de2c
...
...
@@ -5,7 +5,7 @@
#include <string>
#include <unordered_map>
#include <vector>
#include "paddle/
framework
/enforce.h"
#include "paddle/
platform
/enforce.h"
namespace
paddle
{
namespace
framework
{
...
...
paddle/framework/enforce.h
已删除
100644 → 0
浏览文件 @
48cf64e8
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include <paddle/string/printf.h>
#include <exception>
#include <sstream>
namespace
paddle
{
namespace
framework
{
/**
* @brief Enforce exception. Inherits std::exception
*
* All enforce condition not met, will throw an EnforceNotMet exception.
*/
class
EnforceNotMet
:
public
std
::
exception
{
public:
EnforceNotMet
(
const
std
::
string
&
msg
,
const
char
*
file
,
int
fileline
)
{
std
::
ostringstream
sout
;
sout
<<
msg
<<
" at ["
<<
file
<<
":"
<<
fileline
<<
"];"
;
all_msg_
=
sout
.
str
();
}
const
char
*
what
()
const
noexcept
override
{
return
all_msg_
.
c_str
();
}
private:
std
::
string
all_msg_
;
};
// From https://stackoverflow.com/questions/30130930/
// __buildin_expect is in C++ 11 standard. Since the condition which enforced
// should be true in most situation, it will make the compiler generate faster
// code by adding `UNLIKELY` macro.
#define UNLIKELY(condition) __builtin_expect(static_cast<bool>(condition), 0)
/**
* @brief Throw a EnforceNotMet exception, automatically filled __FILE__ &
* __LINE__
*
* This macro take __VA_ARGS__, user can pass any type if that type can
* serialize to std::ostream
*/
#define PADDLE_THROW(...) \
do { \
throw ::paddle::framework::EnforceNotMet( \
::paddle::string::Sprintf(__VA_ARGS__), __FILE__, __LINE__); \
} while (0)
/**
* @brief Enforce a condition, otherwise throw an EnforceNotMet
*/
#define PADDLE_ENFORCE(condition, ...) \
do { \
if (UNLIKELY(!(condition))) { \
PADDLE_THROW(__VA_ARGS__); \
} \
} while (0)
}
// namespace framework
}
// namespace paddle
paddle/framework/enforce_test.cc
已删除
100644 → 0
浏览文件 @
48cf64e8
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include <gtest/gtest.h>
#include <paddle/framework/enforce.h>
TEST
(
ENFORCE
,
OK
)
{
PADDLE_ENFORCE
(
true
,
"Enforce is ok %d now %f"
,
123
,
0.345
);
size_t
val
=
1
;
const
size_t
limit
=
10
;
PADDLE_ENFORCE
(
val
<
limit
,
"Enforce is OK too"
);
}
TEST
(
ENFORCE
,
FAILED
)
{
bool
in_catch
=
false
;
try
{
PADDLE_ENFORCE
(
false
,
"Enforce is not ok %d at all"
,
123
);
}
catch
(
paddle
::
framework
::
EnforceNotMet
err
)
{
in_catch
=
true
;
std
::
string
msg
=
"Enforce is not ok 123 at all"
;
const
char
*
what
=
err
.
what
();
for
(
size_t
i
=
0
;
i
<
msg
.
length
();
++
i
)
{
ASSERT_EQ
(
what
[
i
],
msg
[
i
]);
}
}
ASSERT_TRUE
(
in_catch
);
}
\ No newline at end of file
paddle/framework/op_registry_test.cc
浏览文件 @
f812de2c
...
...
@@ -91,7 +91,7 @@ TEST(OpRegistry, IllegalAttr) {
try
{
paddle
::
framework
::
OperatorPtr
op
__attribute__
((
unused
))
=
paddle
::
framework
::
OpRegistry
::
CreateOp
(
op_desc
);
}
catch
(
paddle
::
framework
::
EnforceNotMet
err
)
{
}
catch
(
paddle
::
platform
::
EnforceNotMet
err
)
{
caught
=
true
;
std
::
string
msg
=
"larger_than check fail"
;
const
char
*
err_msg
=
err
.
what
();
...
...
@@ -138,7 +138,7 @@ TEST(OpRegistry, CustomChecker) {
try
{
paddle
::
framework
::
OperatorPtr
op
__attribute__
((
unused
))
=
paddle
::
framework
::
OpRegistry
::
CreateOp
(
op_desc
);
}
catch
(
paddle
::
framework
::
EnforceNotMet
err
)
{
}
catch
(
paddle
::
platform
::
EnforceNotMet
err
)
{
caught
=
true
;
std
::
string
msg
=
"Attribute 'test_attr' is required!"
;
const
char
*
err_msg
=
err
.
what
();
...
...
@@ -157,7 +157,7 @@ TEST(OpRegistry, CustomChecker) {
try
{
paddle
::
framework
::
OperatorPtr
op
__attribute__
((
unused
))
=
paddle
::
framework
::
OpRegistry
::
CreateOp
(
op_desc
);
}
catch
(
paddle
::
framework
::
EnforceNotMet
err
)
{
}
catch
(
paddle
::
platform
::
EnforceNotMet
err
)
{
caught
=
true
;
std
::
string
msg
=
"'test_attr' must be even!"
;
const
char
*
err_msg
=
err
.
what
();
...
...
paddle/framework/tensor.h
浏览文件 @
f812de2c
...
...
@@ -18,7 +18,7 @@ limitations under the License. */
#include <cstring>
#include <memory>
#include "paddle/framework/ddim.h"
#include "paddle/
framework
/enforce.h"
#include "paddle/
platform
/enforce.h"
#include "paddle/memory/memory.h"
#include "paddle/platform/place.h"
...
...
paddle/framework/tensor_test.cc
浏览文件 @
f812de2c
...
...
@@ -33,7 +33,7 @@ TEST(Tensor, DataAssert) {
bool
caught
=
false
;
try
{
src_tensor
.
data
<
double
>
();
}
catch
(
paddle
::
framework
::
EnforceNotMet
err
)
{
}
catch
(
paddle
::
platform
::
EnforceNotMet
err
)
{
caught
=
true
;
std
::
string
msg
=
"Tenosr holds no memory. Call Tensor::mutable_data first."
;
...
...
paddle/memory/detail/system_allocator.cc
浏览文件 @
f812de2c
...
...
@@ -14,7 +14,7 @@ limitations under the License. */
#include "paddle/memory/detail/system_allocator.h"
#include "paddle/platform/assert.h"
#include "paddle/platform/e
rror
.h"
#include "paddle/platform/e
nforce
.h"
#include "paddle/platform/gpu_info.h"
#include <stdlib.h> // for malloc and free
...
...
@@ -128,8 +128,7 @@ void GPUAllocator::Free(void* p, size_t size, size_t index) {
// process is terminating, in which case we don't care if
// cudaFree succeeds.
if
(
err
!=
cudaErrorCudartUnloading
)
{
platform
::
throw_on_error
(
err
,
"cudaFree{Host} failed in GPUAllocator::Free."
);
PADDLE_ENFORCE
(
err
,
"cudaFree{Host} failed in GPUAllocator::Free."
);
}
}
...
...
paddle/platform/CMakeLists.txt
浏览文件 @
f812de2c
...
...
@@ -8,6 +8,8 @@ cc_test(place_test SRCS place_test.cc DEPS place glog gflags)
add_subdirectory
(
dynload
)
cc_test
(
enforce_test SRCS enforce_test.cc
)
IF
(
WITH_GPU
)
set
(
GPU_CTX_DEPS dynload_cuda dynamic_loader
)
ELSE
()
...
...
paddle/platform/cpu_info.cc
浏览文件 @
f812de2c
...
...
@@ -22,7 +22,6 @@ limitations under the License. */
#endif
#include "gflags/gflags.h"
#include "paddle/platform/error.h"
DEFINE_double
(
fraction_of_cpu_memory_to_use
,
1
,
"Default use 100% of CPU memory for PaddlePaddle,"
...
...
paddle/platform/device_context.h
浏览文件 @
f812de2c
...
...
@@ -11,7 +11,7 @@ limitations under the License. */
#pragma once
#include "paddle/
framework
/enforce.h"
#include "paddle/
platform
/enforce.h"
#ifndef PADDLE_ONLY_CPU
#include "paddle/platform/dynload/cublas.h"
#include "paddle/platform/dynload/cudnn.h"
...
...
@@ -74,8 +74,7 @@ class CUDADeviceContext : public DeviceContext {
public:
explicit
CUDADeviceContext
(
const
GPUPlace
gpu_place
)
:
gpu_place_
(
gpu_place
)
{
GPUPlaceGuard
guard
(
gpu_place_
);
paddle
::
platform
::
throw_on_error
(
cudaStreamCreate
(
&
stream_
),
"cudaStreamCreate failed"
);
PADDLE_ENFORCE
(
cudaStreamCreate
(
&
stream_
),
"cudaStreamCreate failed"
);
eigen_stream_
.
reset
(
new
Eigen
::
CudaStreamDevice
(
&
stream_
));
eigen_device_
.
reset
(
new
Eigen
::
GpuDevice
(
eigen_stream_
.
get
()));
}
...
...
@@ -86,7 +85,7 @@ class CUDADeviceContext : public DeviceContext {
}
void
Wait
()
{
paddle
::
platform
::
throw_on_error
(
cudaStreamSynchronize
(
stream_
),
PADDLE_ENFORCE
(
cudaStreamSynchronize
(
stream_
),
"cudaStreamSynchronize failed"
);
}
...
...
@@ -97,11 +96,10 @@ class CUDADeviceContext : public DeviceContext {
cublasHandle_t
cublas_handle
()
{
if
(
!
blas_handle_
)
{
GPUPlaceGuard
guard
(
gpu_place_
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
cublasCreate
(
&
blas_handle_
)
==
CUBLAS_STATUS_SUCCESS
,
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
cublasCreate
(
&
blas_handle_
),
"cublasCreate failed"
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
cublasSetStream
(
blas_handle_
,
stream_
)
==
CUBLAS_STATUS_SUCCESS
,
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
cublasSetStream
(
blas_handle_
,
stream_
)
,
"cublasSetStream failed"
);
}
return
blas_handle_
;
...
...
@@ -110,11 +108,10 @@ class CUDADeviceContext : public DeviceContext {
cudnnHandle_t
cudnn_handle
()
{
if
(
!
dnn_handle_
)
{
GPUPlaceGuard
guard
(
gpu_place_
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
cudnnCreate
(
&
dnn_handle_
)
==
CUDNN_STATUS_SUCCESS
,
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
cudnnCreate
(
&
dnn_handle_
),
"cudnnCreate failed"
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
cudnnSetStream
(
dnn_handle_
,
stream_
)
==
CUDNN_STATUS_SUCCESS
,
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
cudnnSetStream
(
dnn_handle_
,
stream_
)
,
"cudnnSetStream failed"
);
}
return
dnn_handle_
;
...
...
@@ -124,15 +121,14 @@ class CUDADeviceContext : public DeviceContext {
if
(
!
rand_generator_
)
{
GPUPlaceGuard
guard
(
gpu_place_
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
curandCreateGenerator
(
&
rand_generator_
,
CURAND_RNG_PSEUDO_DEFAULT
)
==
CURAND_STATUS_SUCCESS
,
&
rand_generator_
,
CURAND_RNG_PSEUDO_DEFAULT
),
"curandCreateGenerator failed"
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
curandSetPseudoRandomGeneratorSeed
(
rand_generator_
,
random_seed_
)
==
CURAND_STATUS_SUCCESS
,
rand_generator_
,
random_seed_
),
"curandSetPseudoRandomGeneratorSeed failed"
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
curandSetStream
(
rand_generator_
,
stream_
)
==
CURAND_STATUS_SUCCESS
,
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
curandSetStream
(
rand_generator_
,
stream_
)
,
"curandSetStream failed"
);
}
return
rand_generator_
;
...
...
@@ -141,26 +137,23 @@ class CUDADeviceContext : public DeviceContext {
~
CUDADeviceContext
()
{
Wait
();
if
(
blas_handle_
)
{
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
cublasDestroy
(
blas_handle_
)
==
CUBLAS_STATUS_SUCCESS
,
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
cublasDestroy
(
blas_handle_
),
"cublasDestroy failed"
);
}
if
(
dnn_handle_
)
{
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
cudnnDestroy
(
dnn_handle_
)
==
CUDNN_STATUS_SUCCESS
,
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
cudnnDestroy
(
dnn_handle_
),
"cudnnDestroy failed"
);
}
if
(
rand_generator_
)
{
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
curandDestroyGenerator
(
rand_generator_
)
==
CURAND_STATUS_SUCCESS
,
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
curandDestroyGenerator
(
rand_generator_
)
,
"curandDestroyGenerator failed"
);
}
eigen_stream_
.
reset
();
eigen_device_
.
reset
();
paddle
::
platform
::
throw_on_error
(
cudaStreamDestroy
(
stream_
),
"cudaStreamDestroy failed"
);
PADDLE_ENFORCE
(
cudaStreamDestroy
(
stream_
),
"cudaStreamDestroy failed"
);
}
private:
...
...
paddle/platform/dynload/dynamic_loader.cc
浏览文件 @
f812de2c
...
...
@@ -19,7 +19,7 @@ limitations under the License. */
#include <string>
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "paddle/
framework
/enforce.h"
#include "paddle/
platform
/enforce.h"
DEFINE_string
(
cudnn_dir
,
""
,
"Specify path for loading libcudnn.so. For instance, "
...
...
paddle/platform/error.h
已删除
100644 → 0
浏览文件 @
48cf64e8
#pragma once
#include <sstream>
#include <stdexcept>
#include <string>
#ifndef PADDLE_ONLY_CPU
#include <cublas_v2.h>
#include <cudnn.h>
#include <curand.h>
#include <thrust/system/cuda/error.h>
#include <thrust/system_error.h>
#endif // PADDLE_ONLY_CPU
namespace
paddle
{
namespace
platform
{
#ifndef PADDLE_ONLY_CPU
inline
void
throw_on_error
(
cudaError_t
e
,
const
char
*
message
)
{
if
(
e
)
{
throw
thrust
::
system_error
(
e
,
thrust
::
cuda_category
(),
message
);
}
}
inline
void
throw_on_error
(
curandStatus_t
stat
,
const
char
*
message
)
{
if
(
stat
!=
CURAND_STATUS_SUCCESS
)
{
throw
thrust
::
system_error
(
cudaErrorLaunchFailure
,
thrust
::
cuda_category
(),
message
);
}
}
inline
void
throw_on_error
(
cudnnStatus_t
stat
,
const
char
*
message
)
{
std
::
stringstream
ss
;
if
(
stat
==
CUDNN_STATUS_SUCCESS
)
{
return
;
}
else
{
ss
<<
cudnnGetErrorString
(
stat
);
ss
<<
", "
<<
message
;
throw
std
::
runtime_error
(
ss
.
str
());
}
}
inline
void
throw_on_error
(
cublasStatus_t
stat
,
const
char
*
message
)
{
std
::
stringstream
ss
;
if
(
stat
==
CUBLAS_STATUS_SUCCESS
)
{
return
;
}
else
if
(
stat
==
CUBLAS_STATUS_NOT_INITIALIZED
)
{
ss
<<
"CUBLAS: not initialized"
;
}
else
if
(
stat
==
CUBLAS_STATUS_ALLOC_FAILED
)
{
ss
<<
"CUBLAS: alloc failed"
;
}
else
if
(
stat
==
CUBLAS_STATUS_INVALID_VALUE
)
{
ss
<<
"CUBLAS: invalid value"
;
}
else
if
(
stat
==
CUBLAS_STATUS_ARCH_MISMATCH
)
{
ss
<<
"CUBLAS: arch mismatch"
;
}
else
if
(
stat
==
CUBLAS_STATUS_MAPPING_ERROR
)
{
ss
<<
"CUBLAS: mapping error"
;
}
else
if
(
stat
==
CUBLAS_STATUS_EXECUTION_FAILED
)
{
ss
<<
"CUBLAS: execution failed"
;
}
else
if
(
stat
==
CUBLAS_STATUS_INTERNAL_ERROR
)
{
ss
<<
"CUBLAS: internal error"
;
}
else
if
(
stat
==
CUBLAS_STATUS_NOT_SUPPORTED
)
{
ss
<<
"CUBLAS: not supported"
;
}
else
if
(
stat
==
CUBLAS_STATUS_LICENSE_ERROR
)
{
ss
<<
"CUBLAS: license error"
;
}
ss
<<
", "
<<
message
;
throw
std
::
runtime_error
(
ss
.
str
());
}
inline
void
throw_on_error
(
cublasStatus_t
stat
)
{
const
char
*
message
=
""
;
throw_on_error
(
stat
,
message
);
}
#endif // PADDLE_ONLY_CPU
inline
void
throw_on_error
(
int
stat
,
const
char
*
message
)
{
if
(
stat
)
{
throw
std
::
runtime_error
(
message
+
(
", stat = "
+
std
::
to_string
(
stat
)));
}
}
}
// namespace platform
}
// namespace paddle
paddle/platform/gpu_info.cc
浏览文件 @
f812de2c
...
...
@@ -14,7 +14,7 @@ limitations under the License. */
#include "paddle/platform/gpu_info.h"
#include "gflags/gflags.h"
#include "paddle/platform/e
rror
.h"
#include "paddle/platform/e
nforce
.h"
DEFINE_double
(
fraction_of_gpu_memory_to_use
,
0.95
,
"Default use 95% of GPU memory for PaddlePaddle,"
...
...
@@ -25,7 +25,7 @@ namespace platform {
int
GetDeviceCount
()
{
int
count
;
throw_on_error
(
PADDLE_ENFORCE
(
cudaGetDeviceCount
(
&
count
),
"cudaGetDeviceCount failed in paddle::platform::GetDeviceCount"
);
return
count
;
...
...
@@ -33,19 +33,19 @@ int GetDeviceCount() {
int
GetCurrentDeviceId
()
{
int
device_id
;
throw_on_error
(
PADDLE_ENFORCE
(
cudaGetDevice
(
&
device_id
),
"cudaGetDevice failed in paddle::platform::GetCurrentDeviceId"
);
return
device_id
;
}
void
SetDeviceId
(
int
id
)
{
throw_on_error
(
cudaSetDevice
(
id
),
PADDLE_ENFORCE
(
cudaSetDevice
(
id
),
"cudaSetDevice failed in paddle::platform::SetDeviceId"
);
}
void
GpuMemoryUsage
(
size_t
&
available
,
size_t
&
total
)
{
throw_on_error
(
cudaMemGetInfo
(
&
available
,
&
total
),
PADDLE_ENFORCE
(
cudaMemGetInfo
(
&
available
,
&
total
),
"cudaMemGetInfo failed in paddle::platform::GetMemoryUsage"
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录