Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
1480720f
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看板
提交
1480720f
编写于
7月 17, 2017
作者:
F
fengjiayi
提交者:
GitHub
7月 17, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2891 from Canpio/dev_enable_tensor_test
Enable tensor test
上级
df707d04
1cd14f66
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
46 addition
and
27 deletion
+46
-27
paddle/framework/tensor.h
paddle/framework/tensor.h
+34
-21
paddle/framework/tensor_test.cc
paddle/framework/tensor_test.cc
+12
-6
未找到文件。
paddle/framework/tensor.h
浏览文件 @
1480720f
...
@@ -29,8 +29,6 @@ class Tensor {
...
@@ -29,8 +29,6 @@ class Tensor {
public:
public:
Tensor
()
:
numel_
(
0
),
offset_
(
0
)
{}
Tensor
()
:
numel_
(
0
),
offset_
(
0
)
{}
Tensor
&
operator
=
(
const
Tensor
&
src
)
=
delete
;
template
<
typename
T
>
template
<
typename
T
>
const
T
*
data
()
const
{
const
T
*
data
()
const
{
CheckDims
<
T
>
();
CheckDims
<
T
>
();
...
@@ -39,13 +37,13 @@ class Tensor {
...
@@ -39,13 +37,13 @@ class Tensor {
}
}
template
<
typename
T
>
template
<
typename
T
>
T
*
mutable_data
(
DDim
dims
,
p
addle
::
p
latform
::
Place
place
)
{
T
*
mutable_data
(
DDim
dims
,
platform
::
Place
place
)
{
set_dims
(
dims
);
set_dims
(
dims
);
return
mutable_data
<
T
>
(
place
);
return
mutable_data
<
T
>
(
place
);
}
}
template
<
typename
T
>
template
<
typename
T
>
T
*
mutable_data
(
p
addle
::
p
latform
::
Place
place
)
{
T
*
mutable_data
(
platform
::
Place
place
)
{
PADDLE_ENFORCE
(
numel_
>
0
,
PADDLE_ENFORCE
(
numel_
>
0
,
"Tensor::numel_ must be larger than zero to call "
"Tensor::numel_ must be larger than zero to call "
"Tensor::mutable_data. Call Tensor::set_dim first."
);
"Tensor::mutable_data. Call Tensor::set_dim first."
);
...
@@ -53,7 +51,23 @@ class Tensor {
...
@@ -53,7 +51,23 @@ class Tensor {
!
(
holder_
->
place
()
==
!
(
holder_
->
place
()
==
place
)
/* some versions of boost::variant don't have operator!= */
place
)
/* some versions of boost::variant don't have operator!= */
||
holder_
->
size
()
<
numel_
*
sizeof
(
T
)
+
offset_
)
{
||
holder_
->
size
()
<
numel_
*
sizeof
(
T
)
+
offset_
)
{
holder_
.
reset
(
new
PlaceholderImpl
<
T
>
(
place
,
numel_
*
sizeof
(
T
)));
#ifdef __CUDACC__
switch
(
place
.
which
())
{
case
0
:
holder_
.
reset
(
new
PlaceholderImpl
<
T
,
platform
::
GPUPlace
>
(
boost
::
get
<
platform
::
GPUPlace
>
(
place
),
numel_
*
sizeof
(
T
)));
break
;
case
1
:
holder_
.
reset
(
new
PlaceholderImpl
<
T
,
platform
::
CPUPlace
>
(
boost
::
get
<
platform
::
CPUPlace
>
(
place
),
numel_
*
sizeof
(
T
)));
break
;
}
#else
holder_
.
reset
(
new
PlaceholderImpl
<
T
,
platform
::
CPUPlace
>
(
boost
::
get
<
platform
::
CPUPlace
>
(
place
),
numel_
*
sizeof
(
T
)));
#endif
offset_
=
0
;
offset_
=
0
;
}
}
return
reinterpret_cast
<
T
*>
(
reinterpret_cast
<
uintptr_t
>
(
holder_
->
ptr
())
+
return
reinterpret_cast
<
T
*>
(
reinterpret_cast
<
uintptr_t
>
(
holder_
->
ptr
())
+
...
@@ -69,7 +83,7 @@ class Tensor {
...
@@ -69,7 +83,7 @@ class Tensor {
}
}
template
<
typename
T
>
template
<
typename
T
>
void
CopyFrom
(
const
Tensor
&
src
,
p
addle
::
p
latform
::
Place
dst_place
)
{
void
CopyFrom
(
const
Tensor
&
src
,
platform
::
Place
dst_place
)
{
PADDLE_ENFORCE
(
platform
::
is_cpu_place
(
src
.
holder_
->
place
())
&&
PADDLE_ENFORCE
(
platform
::
is_cpu_place
(
src
.
holder_
->
place
())
&&
platform
::
is_cpu_place
(
dst_place
),
platform
::
is_cpu_place
(
dst_place
),
"Tensor::CopyFrom only support CPU now."
);
"Tensor::CopyFrom only support CPU now."
);
...
@@ -119,38 +133,37 @@ class Tensor {
...
@@ -119,38 +133,37 @@ class Tensor {
struct
Placeholder
{
struct
Placeholder
{
virtual
~
Placeholder
()
{}
virtual
~
Placeholder
()
{}
virtual
void
*
ptr
()
const
=
0
;
virtual
void
*
ptr
()
const
=
0
;
virtual
p
addle
::
p
latform
::
Place
place
()
const
=
0
;
virtual
platform
::
Place
place
()
const
=
0
;
virtual
size_t
size
()
const
=
0
;
virtual
size_t
size
()
const
=
0
;
};
};
template
<
typename
T
>
template
<
typename
T
,
typename
PlaceType
>
struct
PlaceholderImpl
:
public
Placeholder
{
struct
PlaceholderImpl
:
public
Placeholder
{
private:
private:
template
<
typename
PType
>
class
Deleter
{
class
Deleter
{
public:
public:
Deleter
(
platform
::
Place
place
)
:
place_
(
place
)
{}
Deleter
(
PType
place
)
:
place_
(
place
)
{}
void
operator
()(
T
*
ptr
)
{
void
operator
()(
T
*
ptr
)
{
memory
::
Free
(
place_
,
static_cast
<
void
*>
(
ptr
));
}
paddle
::
memory
::
Free
(
place_
,
static_cast
<
void
*>
(
ptr
));
}
private:
private:
paddle
::
platform
::
Plac
e
place_
;
PTyp
e
place_
;
};
};
public:
public:
PlaceholderImpl
(
paddle
::
platform
::
Plac
e
place
,
size_t
size
)
PlaceholderImpl
(
PlaceTyp
e
place
,
size_t
size
)
:
ptr_
(
static_cast
<
T
*>
(
paddle
::
memory
::
Alloc
(
place
,
size
)),
:
ptr_
(
static_cast
<
T
*>
(
memory
::
Alloc
(
place
,
size
)),
Deleter
(
place
)),
Deleter
<
PlaceType
>
(
place
)),
place_
(
place
),
place_
(
place
),
size_
(
size
)
{}
size_
(
size
)
{}
virtual
void
*
ptr
()
const
{
return
static_cast
<
void
*>
(
ptr_
.
get
());
}
virtual
void
*
ptr
()
const
{
return
static_cast
<
void
*>
(
ptr_
.
get
());
}
virtual
size_t
size
()
const
{
return
size_
;
}
virtual
size_t
size
()
const
{
return
size_
;
}
virtual
p
addle
::
p
latform
::
Place
place
()
const
{
return
place_
;
}
virtual
platform
::
Place
place
()
const
{
return
place_
;
}
std
::
unique_ptr
<
T
,
Deleter
>
ptr_
;
std
::
unique_ptr
<
T
,
Deleter
<
PlaceType
>
>
ptr_
;
p
addle
::
p
latform
::
Place
place_
;
// record the place of ptr_.
platform
::
Place
place_
;
// record the place of ptr_.
size_t
size_
;
// size of the memory block.
size_t
size_
;
// size of the memory block.
};
};
template
<
typename
T
>
template
<
typename
T
>
...
@@ -166,7 +179,7 @@ class Tensor {
...
@@ -166,7 +179,7 @@ class Tensor {
DDim
dims_
;
DDim
dims_
;
size_t
numel_
;
// cache of `product(dims_)`
size_t
numel_
;
// cache of `product(dims_)`
size_t
offset_
;
// marks the begin of tensor data area.
size_t
offset_
;
// marks the begin of tensor data area.
};
};
// namespace framework
}
// namespace framework
}
// namespace framework
}
// namespace paddle
}
// namespace paddle
paddle/framework/tensor_test.cc
浏览文件 @
1480720f
...
@@ -47,7 +47,7 @@ TEST(Tensor, DataAssert) {
...
@@ -47,7 +47,7 @@ TEST(Tensor, DataAssert) {
/* following tests are not available at present
/* following tests are not available at present
because Memory::Alloc() and Memory::Free() have not been ready.
because Memory::Alloc() and Memory::Free() have not been ready.
*/
TEST
(
Tensor
,
MutableData
)
{
TEST
(
Tensor
,
MutableData
)
{
using
namespace
paddle
::
framework
;
using
namespace
paddle
::
framework
;
using
namespace
paddle
::
platform
;
using
namespace
paddle
::
platform
;
...
@@ -72,7 +72,7 @@ TEST(Tensor, MutableData) {
...
@@ -72,7 +72,7 @@ TEST(Tensor, MutableData) {
p2
=
src_tensor
.
mutable_data
<
float
>
(
make_ddim
({
2
,
2
}),
CPUPlace
());
p2
=
src_tensor
.
mutable_data
<
float
>
(
make_ddim
({
2
,
2
}),
CPUPlace
());
EXPECT_EQ
(
p1
,
p2
);
EXPECT_EQ
(
p1
,
p2
);
}
}
#ifdef __CUDACC__
{
{
Tensor
src_tensor
;
Tensor
src_tensor
;
float
*
p1
=
nullptr
;
float
*
p1
=
nullptr
;
...
@@ -94,6 +94,7 @@ TEST(Tensor, MutableData) {
...
@@ -94,6 +94,7 @@ TEST(Tensor, MutableData) {
p2
=
src_tensor
.
mutable_data
<
float
>
(
make_ddim
({
2
,
2
}),
GPUPlace
());
p2
=
src_tensor
.
mutable_data
<
float
>
(
make_ddim
({
2
,
2
}),
GPUPlace
());
EXPECT_EQ
(
p1
,
p2
);
EXPECT_EQ
(
p1
,
p2
);
}
}
#endif
}
}
TEST
(
Tensor
,
ShareDataFrom
)
{
TEST
(
Tensor
,
ShareDataFrom
)
{
...
@@ -108,9 +109,11 @@ TEST(Tensor, ShareDataFrom) {
...
@@ -108,9 +109,11 @@ TEST(Tensor, ShareDataFrom) {
dst_tensor
.
ShareDataFrom
<
float
>
(
src_tensor
);
dst_tensor
.
ShareDataFrom
<
float
>
(
src_tensor
);
}
catch
(
EnforceNotMet
err
)
{
}
catch
(
EnforceNotMet
err
)
{
caught
=
true
;
caught
=
true
;
std::string msg = "Tenosr holds no memory. Call Tensor::mutable_data
std
::
string
msg
=
first."; const char* what = err.what(); for (size_t i = 0; i < msg.length();
"Tenosr holds no memory. Call Tensor::mutable_data first."
;
++i) { ASSERT_EQ(what[i], msg[i]);
const
char
*
what
=
err
.
what
();
for
(
size_t
i
=
0
;
i
<
msg
.
length
();
++
i
)
{
ASSERT_EQ
(
what
[
i
],
msg
[
i
]);
}
}
}
}
ASSERT_TRUE
(
caught
);
ASSERT_TRUE
(
caught
);
...
@@ -120,6 +123,7 @@ first."; const char* what = err.what(); for (size_t i = 0; i < msg.length();
...
@@ -120,6 +123,7 @@ first."; const char* what = err.what(); for (size_t i = 0; i < msg.length();
ASSERT_EQ
(
src_tensor
.
data
<
int
>
(),
dst_tensor
.
data
<
int
>
());
ASSERT_EQ
(
src_tensor
.
data
<
int
>
(),
dst_tensor
.
data
<
int
>
());
}
}
#ifdef __CUDACC__
{
{
Tensor
src_tensor
;
Tensor
src_tensor
;
Tensor
dst_tensor
;
Tensor
dst_tensor
;
...
@@ -127,6 +131,7 @@ first."; const char* what = err.what(); for (size_t i = 0; i < msg.length();
...
@@ -127,6 +131,7 @@ first."; const char* what = err.what(); for (size_t i = 0; i < msg.length();
dst_tensor
.
ShareDataFrom
<
int
>
(
src_tensor
);
dst_tensor
.
ShareDataFrom
<
int
>
(
src_tensor
);
ASSERT_EQ
(
src_tensor
.
data
<
int
>
(),
dst_tensor
.
data
<
int
>
());
ASSERT_EQ
(
src_tensor
.
data
<
int
>
(),
dst_tensor
.
data
<
int
>
());
}
}
#endif
}
}
TEST
(
Tensor
,
Slice
)
{
TEST
(
Tensor
,
Slice
)
{
...
@@ -155,6 +160,7 @@ TEST(Tensor, Slice) {
...
@@ -155,6 +160,7 @@ TEST(Tensor, Slice) {
EXPECT_EQ
(
src_data_address
+
3
*
4
*
1
*
sizeof
(
int
),
slice_data_address
);
EXPECT_EQ
(
src_data_address
+
3
*
4
*
1
*
sizeof
(
int
),
slice_data_address
);
}
}
#ifdef __CUDACC__
{
{
Tensor
src_tensor
;
Tensor
src_tensor
;
src_tensor
.
mutable_data
<
double
>
(
make_ddim
({
6
,
9
}),
GPUPlace
());
src_tensor
.
mutable_data
<
double
>
(
make_ddim
({
6
,
9
}),
GPUPlace
());
...
@@ -176,6 +182,7 @@ TEST(Tensor, Slice) {
...
@@ -176,6 +182,7 @@ TEST(Tensor, Slice) {
EXPECT_EQ
(
slice_data_address
,
slice_mutable_data_address
);
EXPECT_EQ
(
slice_data_address
,
slice_mutable_data_address
);
EXPECT_EQ
(
src_data_address
+
9
*
2
*
sizeof
(
double
),
slice_data_address
);
EXPECT_EQ
(
src_data_address
+
9
*
2
*
sizeof
(
double
),
slice_data_address
);
}
}
#endif
}
}
TEST
(
Tensor
,
CopyFrom
)
{
TEST
(
Tensor
,
CopyFrom
)
{
...
@@ -203,4 +210,3 @@ TEST(Tensor, CopyFrom) {
...
@@ -203,4 +210,3 @@ TEST(Tensor, CopyFrom) {
EXPECT_EQ
(
dst_ptr
[
i
],
slice_ptr
[
i
]);
EXPECT_EQ
(
dst_ptr
[
i
],
slice_ptr
[
i
]);
}
}
}
}
*/
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录