Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
58fd4c09
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
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看板
未验证
提交
58fd4c09
编写于
4月 21, 2018
作者:
T
Tao Luo
提交者:
GitHub
4月 21, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #10111 from abhinavarora/cpplint_tensor_util_test
Fix CPPLint issues in tensor_util_test
上级
a4d3de00
1c70600d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
56 addition
and
60 deletion
+56
-60
paddle/fluid/framework/tensor_util_test.cc
paddle/fluid/framework/tensor_util_test.cc
+48
-52
paddle/fluid/framework/tensor_util_test.cu
paddle/fluid/framework/tensor_util_test.cu
+8
-8
未找到文件。
paddle/fluid/framework/tensor_util_test.cc
浏览文件 @
58fd4c09
...
...
@@ -105,16 +105,14 @@ TEST(TensorCopy, Tensor) {
}
TEST
(
TensorFromVector
,
Tensor
)
{
using
namespace
paddle
::
framework
;
using
namespace
paddle
::
platform
;
{
std
::
vector
<
int
>
src_vec
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
};
Tensor
cpu_tensor
;
paddle
::
framework
::
Tensor
cpu_tensor
;
// Copy to CPU Tensor
cpu_tensor
.
Resize
(
make_ddim
({
3
,
3
}));
cpu_tensor
.
Resize
(
paddle
::
framework
::
make_ddim
({
3
,
3
}));
auto
cpu_place
=
new
paddle
::
platform
::
CPUPlace
();
TensorFromVector
<
int
>
(
src_vec
,
&
cpu_tensor
);
paddle
::
framework
::
TensorFromVector
<
int
>
(
src_vec
,
&
cpu_tensor
);
// Compare Tensors
const
int
*
cpu_ptr
=
cpu_tensor
.
data
<
int
>
();
...
...
@@ -125,8 +123,8 @@ TEST(TensorFromVector, Tensor) {
}
src_vec
.
erase
(
src_vec
.
begin
(),
src_vec
.
begin
()
+
5
);
cpu_tensor
.
Resize
(
make_ddim
({
2
,
2
}));
TensorFromVector
<
int
>
(
src_vec
,
&
cpu_tensor
);
cpu_tensor
.
Resize
(
paddle
::
framework
::
make_ddim
({
2
,
2
}));
paddle
::
framework
::
TensorFromVector
<
int
>
(
src_vec
,
&
cpu_tensor
);
cpu_ptr
=
cpu_tensor
.
data
<
int
>
();
src_ptr
=
src_vec
.
data
();
ASSERT_NE
(
src_ptr
,
cpu_ptr
);
...
...
@@ -140,23 +138,23 @@ TEST(TensorFromVector, Tensor) {
#ifdef PADDLE_WITH_CUDA
{
std
::
vector
<
int
>
src_vec
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
};
Tensor
cpu_tensor
;
Tensor
gpu_tensor
;
Tensor
dst_tensor
;
paddle
::
framework
::
Tensor
cpu_tensor
;
paddle
::
framework
::
Tensor
gpu_tensor
;
paddle
::
framework
::
Tensor
dst_tensor
;
// Copy to CPU Tensor
cpu_tensor
.
Resize
(
make_ddim
({
3
,
3
}));
auto
cpu_place
=
new
paddle
::
platform
::
CPUPlace
();
CPUDeviceContext
cpu_ctx
(
*
cpu_place
);
TensorFromVector
<
int
>
(
src_vec
,
cpu_ctx
,
&
cpu_tensor
);
paddle
::
platform
::
CPUDeviceContext
cpu_ctx
(
*
cpu_place
);
paddle
::
framework
::
TensorFromVector
<
int
>
(
src_vec
,
cpu_ctx
,
&
cpu_tensor
);
// Copy to GPUTensor
gpu_tensor
.
Resize
(
make_ddim
({
3
,
3
}));
gpu_tensor
.
Resize
(
paddle
::
framework
::
make_ddim
({
3
,
3
}));
auto
gpu_place
=
new
paddle
::
platform
::
CUDAPlace
();
CUDADeviceContext
gpu_ctx
(
*
gpu_place
);
TensorFromVector
<
int
>
(
src_vec
,
gpu_ctx
,
&
gpu_tensor
);
paddle
::
platform
::
CUDADeviceContext
gpu_ctx
(
*
gpu_place
);
paddle
::
framework
::
TensorFromVector
<
int
>
(
src_vec
,
gpu_ctx
,
&
gpu_tensor
);
// Copy from GPU to CPU tensor for comparison
TensorCopy
(
gpu_tensor
,
*
cpu_place
,
gpu_ctx
,
&
dst_tensor
);
paddle
::
framework
::
TensorCopy
(
gpu_tensor
,
*
cpu_place
,
gpu_ctx
,
&
dst_tensor
);
// Sync before Compare Tensors
gpu_ctx
.
Wait
();
...
...
@@ -172,11 +170,11 @@ TEST(TensorFromVector, Tensor) {
src_vec
.
erase
(
src_vec
.
begin
(),
src_vec
.
begin
()
+
5
);
cpu_tensor
.
Resize
(
make_ddim
({
2
,
2
}));
TensorFromVector
<
int
>
(
src_vec
,
cpu_ctx
,
&
cpu_tensor
);
gpu_tensor
.
Resize
(
make_ddim
({
2
,
2
}));
TensorFromVector
<
int
>
(
src_vec
,
gpu_ctx
,
&
gpu_tensor
);
TensorCopy
(
gpu_tensor
,
*
cpu_place
,
gpu_ctx
,
&
dst_tensor
);
cpu_tensor
.
Resize
(
paddle
::
framework
::
make_ddim
({
2
,
2
}));
paddle
::
framework
::
TensorFromVector
<
int
>
(
src_vec
,
cpu_ctx
,
&
cpu_tensor
);
gpu_tensor
.
Resize
(
paddle
::
framework
::
make_ddim
({
2
,
2
}));
paddle
::
framework
::
TensorFromVector
<
int
>
(
src_vec
,
gpu_ctx
,
&
gpu_tensor
);
paddle
::
framework
::
TensorCopy
(
gpu_tensor
,
*
cpu_place
,
gpu_ctx
,
&
dst_tensor
);
// Sync before Compare Tensors
gpu_ctx
.
Wait
();
...
...
@@ -197,18 +195,16 @@ TEST(TensorFromVector, Tensor) {
}
TEST
(
TensorToVector
,
Tensor
)
{
using
namespace
paddle
::
framework
;
using
namespace
paddle
::
platform
;
{
Tensor
src
;
int
*
src_ptr
=
src
.
mutable_data
<
int
>
({
3
,
3
},
CPUPlace
());
paddle
::
framework
::
Tensor
src
;
int
*
src_ptr
=
src
.
mutable_data
<
int
>
({
3
,
3
},
paddle
::
platform
::
CPUPlace
());
for
(
int
i
=
0
;
i
<
3
*
3
;
++
i
)
{
src_ptr
[
i
]
=
i
;
}
CPUPlace
place
;
paddle
::
platform
::
CPUPlace
place
;
std
::
vector
<
int
>
dst
;
TensorToVector
<
int
>
(
src
,
&
dst
);
paddle
::
framework
::
TensorToVector
<
int
>
(
src
,
&
dst
);
for
(
int
i
=
0
;
i
<
3
*
3
;
++
i
)
{
EXPECT_EQ
(
src_ptr
[
i
],
dst
[
i
]);
...
...
@@ -217,13 +213,13 @@ TEST(TensorToVector, Tensor) {
#ifdef PADDLE_WITH_CUDA
{
std
::
vector
<
int
>
src_vec
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
};
Tensor
gpu_tensor
;
CUDAPlace
place
;
CUDADeviceContext
gpu_ctx
(
place
);
TensorFromVector
<
int
>
(
src_vec
,
gpu_ctx
,
&
gpu_tensor
);
paddle
::
framework
::
Tensor
gpu_tensor
;
paddle
::
platform
::
CUDAPlace
place
;
paddle
::
platform
::
CUDADeviceContext
gpu_ctx
(
place
);
paddle
::
framework
::
TensorFromVector
<
int
>
(
src_vec
,
gpu_ctx
,
&
gpu_tensor
);
std
::
vector
<
int
>
dst
;
TensorToVector
<
int
>
(
gpu_tensor
,
gpu_ctx
,
&
dst
);
paddle
::
framework
::
TensorToVector
<
int
>
(
gpu_tensor
,
gpu_ctx
,
&
dst
);
for
(
int
i
=
0
;
i
<
3
*
3
;
++
i
)
{
EXPECT_EQ
(
src_vec
[
i
],
dst
[
i
]);
...
...
@@ -233,54 +229,54 @@ TEST(TensorToVector, Tensor) {
}
TEST
(
TensorContainsNAN
,
CPU
)
{
using
namespace
paddle
::
framework
;
using
namespace
paddle
::
platform
;
{
Tensor
src
;
float
*
buf
=
src
.
mutable_data
<
float
>
({
3
},
CPUPlace
());
paddle
::
framework
::
Tensor
src
;
float
*
buf
=
src
.
mutable_data
<
float
>
({
3
},
paddle
::
platform
::
CPUPlace
());
buf
[
0
]
=
0.0
;
buf
[
1
]
=
NAN
;
buf
[
2
]
=
0.0
;
ASSERT_TRUE
(
TensorContainsNAN
(
src
));
ASSERT_TRUE
(
paddle
::
framework
::
TensorContainsNAN
(
src
));
buf
[
1
]
=
0.0
;
ASSERT_FALSE
(
TensorContainsNAN
(
src
));
ASSERT_FALSE
(
paddle
::
framework
::
TensorContainsNAN
(
src
));
}
{
Tensor
src
;
float16
*
buf
=
src
.
mutable_data
<
float16
>
({
3
},
CPUPlace
());
paddle
::
framework
::
Tensor
src
;
paddle
::
platform
::
float16
*
buf
=
src
.
mutable_data
<
paddle
::
platform
::
float16
>
(
{
3
},
paddle
::
platform
::
CPUPlace
());
buf
[
0
]
=
0.0
;
buf
[
1
].
x
=
0x7fff
;
buf
[
2
]
=
0.0
;
ASSERT_TRUE
(
TensorContainsNAN
(
src
));
ASSERT_TRUE
(
paddle
::
framework
::
TensorContainsNAN
(
src
));
buf
[
1
]
=
0.0
;
ASSERT_FALSE
(
TensorContainsNAN
(
src
));
ASSERT_FALSE
(
paddle
::
framework
::
TensorContainsNAN
(
src
));
}
}
TEST
(
TensorContainsInf
,
CPU
)
{
using
namespace
paddle
::
framework
;
using
namespace
paddle
::
platform
;
{
Tensor
src
;
double
*
buf
=
src
.
mutable_data
<
double
>
({
3
},
CPUPlace
());
paddle
::
framework
::
Tensor
src
;
double
*
buf
=
src
.
mutable_data
<
double
>
({
3
},
paddle
::
platform
::
CPUPlace
());
buf
[
0
]
=
1.0
;
buf
[
1
]
=
INFINITY
;
buf
[
2
]
=
0.0
;
ASSERT_TRUE
(
TensorContainsInf
(
src
));
ASSERT_TRUE
(
paddle
::
framework
::
TensorContainsInf
(
src
));
buf
[
1
]
=
1.0
;
ASSERT_FALSE
(
TensorContainsInf
(
src
));
ASSERT_FALSE
(
paddle
::
framework
::
TensorContainsInf
(
src
));
}
{
Tensor
src
;
float16
*
buf
=
src
.
mutable_data
<
float16
>
({
3
},
CPUPlace
());
paddle
::
framework
::
Tensor
src
;
paddle
::
platform
::
float16
*
buf
=
src
.
mutable_data
<
paddle
::
platform
::
float16
>
(
{
3
},
paddle
::
platform
::
CPUPlace
());
buf
[
0
]
=
1.0
;
buf
[
1
].
x
=
0x7c00
;
buf
[
2
]
=
0.0
;
ASSERT_TRUE
(
TensorContainsInf
(
src
));
ASSERT_TRUE
(
paddle
::
framework
::
TensorContainsInf
(
src
));
buf
[
1
]
=
1.0
;
ASSERT_FALSE
(
TensorContainsInf
(
src
));
ASSERT_FALSE
(
paddle
::
framework
::
TensorContainsInf
(
src
));
}
}
...
...
paddle/fluid/framework/tensor_util_test.cu
浏览文件 @
58fd4c09
...
...
@@ -45,9 +45,8 @@ static __global__ void FillInf(platform::float16* buf) {
}
TEST
(
TensorContainsNAN
,
GPU
)
{
using
namespace
paddle
::
platform
;
CUDAPlace
gpu
(
0
);
auto
&
pool
=
DeviceContextPool
::
Instance
();
paddle
::
platform
::
CUDAPlace
gpu
(
0
);
auto
&
pool
=
paddle
::
platform
::
DeviceContextPool
::
Instance
();
auto
*
cuda_ctx
=
pool
.
GetByPlace
(
gpu
);
{
Tensor
tensor
;
...
...
@@ -58,7 +57,8 @@ TEST(TensorContainsNAN, GPU) {
}
{
Tensor
tensor
;
float16
*
buf
=
tensor
.
mutable_data
<
float16
>
({
3
},
gpu
);
paddle
::
platform
::
float16
*
buf
=
tensor
.
mutable_data
<
paddle
::
platform
::
float16
>
({
3
},
gpu
);
FillNAN
<<<
1
,
1
,
0
,
cuda_ctx
->
stream
()
>>>
(
buf
);
cuda_ctx
->
Wait
();
ASSERT_TRUE
(
TensorContainsNAN
(
tensor
));
...
...
@@ -66,9 +66,8 @@ TEST(TensorContainsNAN, GPU) {
}
TEST
(
TensorContainsInf
,
GPU
)
{
using
namespace
paddle
::
platform
;
CUDAPlace
gpu
(
0
);
auto
&
pool
=
DeviceContextPool
::
Instance
();
paddle
::
platform
::
CUDAPlace
gpu
(
0
);
auto
&
pool
=
paddle
::
platform
::
DeviceContextPool
::
Instance
();
auto
*
cuda_ctx
=
pool
.
GetByPlace
(
gpu
);
{
Tensor
tensor
;
...
...
@@ -79,7 +78,8 @@ TEST(TensorContainsInf, GPU) {
}
{
Tensor
tensor
;
float16
*
buf
=
tensor
.
mutable_data
<
float16
>
({
3
},
gpu
);
paddle
::
platform
::
float16
*
buf
=
tensor
.
mutable_data
<
paddle
::
platform
::
float16
>
({
3
},
gpu
);
FillInf
<<<
1
,
1
,
0
,
cuda_ctx
->
stream
()
>>>
(
buf
);
cuda_ctx
->
Wait
();
ASSERT_TRUE
(
TensorContainsInf
(
tensor
));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录