Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0665dc97
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看板
提交
0665dc97
编写于
7月 11, 2017
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add more test
上级
ca39515e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
119 addition
and
23 deletion
+119
-23
paddle/framework/tensor.h
paddle/framework/tensor.h
+1
-1
paddle/framework/tensor_test.cc
paddle/framework/tensor_test.cc
+118
-22
未找到文件。
paddle/framework/tensor.h
浏览文件 @
0665dc97
...
...
@@ -57,7 +57,7 @@ class Tensor {
void
ShareDataFrom
(
const
Tensor
&
src
)
{
PADDLE_ENFORCE
(
src
.
holder_
!=
nullptr
,
"
Tenosr 'src' has not been initialized
."
);
"
Can not share data from an uninitialized tensor
."
);
holder_
=
src
.
holder_
;
dims_
=
src
.
dims_
;
offset_
=
src
.
offset_
;
...
...
paddle/framework/tensor_test.cc
浏览文件 @
0665dc97
...
...
@@ -15,15 +15,27 @@
#include <gtest/gtest.h>
#include <string>
TEST
(
Tensor
,
ASSERT
)
{
paddle
::
framework
::
Tensor
cpu_tensor
;
TEST
(
Tensor
,
Dims
)
{
using
namespace
paddle
::
framework
;
using
namespace
paddle
::
platform
;
Tensor
tt
(
make_ddim
({
2
,
3
,
4
}));
DDim
dims
=
tt
.
dims
();
ASSERT_EQ
(
arity
(
dims
),
3
);
for
(
int
i
=
0
;
i
<
3
;
++
i
)
{
EXPECT_EQ
(
i
+
2
,
dims
[
i
]);
}
}
TEST
(
Tensor
,
DataAssert
)
{
paddle
::
framework
::
Tensor
src_tensor
;
bool
caught
=
false
;
try
{
const
double
*
p
__attribute__
((
unused
))
=
cpu
_tensor
.
data
<
double
>
();
src
_tensor
.
data
<
double
>
();
}
catch
(
paddle
::
framework
::
EnforceNotMet
err
)
{
caught
=
true
;
std
::
string
msg
=
"Tensor::data must be called after Tensor::mutable_data."
;
std
::
string
msg
=
"Tenosr has not been initialized. Call Tensor::mutable_data first."
;
const
char
*
what
=
err
.
what
();
for
(
size_t
i
=
0
;
i
<
msg
.
length
();
++
i
)
{
ASSERT_EQ
(
what
[
i
],
msg
[
i
]);
...
...
@@ -32,54 +44,138 @@ TEST(Tensor, ASSERT) {
ASSERT_TRUE
(
caught
);
}
/*
mutable_data() is not tested
at present
/*
following tests are not available
at present
because Memory::Alloc() and Memory::Free() have not been ready.
TEST(Tensor, MutableData) {
using namespace paddle::framework;
using namespace paddle::platform;
{
Tensor
cpu
_tensor;
Tensor
src
_tensor;
float* p1 = nullptr;
float* p2 = nullptr;
// initialization
p1 =
cpu
_tensor.mutable_data<float>(make_ddim({1, 2, 3}), CPUPlace());
p1 =
src
_tensor.mutable_data<float>(make_ddim({1, 2, 3}), CPUPlace());
EXPECT_NE(p1, nullptr);
// set
cpu
_tensor a new dim with large size
// set
src
_tensor a new dim with large size
// momery is supposed to be re-allocated
p2 =
cpu_tensor.mutable_data<float>(make_ddim({3, 4}
));
p2 =
src_tensor.mutable_data<float>(make_ddim({3, 4}), CPUPlace(
));
EXPECT_NE(p2, nullptr);
EXPECT_NE(p1, p2);
// set
cpu
_tensor a new dim with same size
// set
src
_tensor a new dim with same size
// momery block is supposed to be unchanged
p1 =
cpu_tensor.mutable_data<float>(make_ddim({2, 2, 3}
));
p1 =
src_tensor.mutable_data<float>(make_ddim({2, 2, 3}), CPUPlace(
));
EXPECT_EQ(p1, p2);
// set
cpu
_tensor a new dim with smaller size
// set
src
_tensor a new dim with smaller size
// momery block is supposed to be unchanged
p2 =
cpu_tensor.mutable_data<float>(make_ddim({2, 2}
));
p2 =
src_tensor.mutable_data<float>(make_ddim({2, 2}), CPUPlace(
));
EXPECT_EQ(p1, p2);
}
{
Tensor
gpu
_tensor;
Tensor
src
_tensor;
float* p1 = nullptr;
float* p2 = nullptr;
// initialization
p1 =
gpu
_tensor.mutable_data<float>(make_ddim({1, 2, 3}), GPUPlace());
p1 =
src
_tensor.mutable_data<float>(make_ddim({1, 2, 3}), GPUPlace());
EXPECT_NE(p1, nullptr);
// set
gpu
_tensor a new dim with large size
// set
src
_tensor a new dim with large size
// momery is supposed to be re-allocated
p2 =
gpu_tensor.mutable_data<float>(make_ddim({3, 4}
));
p2 =
src_tensor.mutable_data<float>(make_ddim({3, 4}), GPUPlace(
));
EXPECT_NE(p2, nullptr);
EXPECT_NE(p1, p2);
// set
gpu
_tensor a new dim with same size
// set
src
_tensor a new dim with same size
// momery block is supposed to be unchanged
p1 =
gpu_tensor.mutable_data<float>(make_ddim({2, 2, 3}
));
p1 =
src_tensor.mutable_data<float>(make_ddim({2, 2, 3}), GPUPlace(
));
EXPECT_EQ(p1, p2);
// set
gpu
_tensor a new dim with smaller size
// set
src
_tensor a new dim with smaller size
// momery block is supposed to be unchanged
p2 =
gpu_tensor.mutable_data<float>(make_ddim({2, 2}
));
p2 =
src_tensor.mutable_data<float>(make_ddim({2, 2}), GPUPlace(
));
EXPECT_EQ(p1, p2);
}
}
*/
TEST(Tensor, ShareDataFrom) {
using namespace paddle::framework;
using namespace paddle::platform;
{
Tensor src_tensor;
Tensor dst_tensor;
// Try to share data form uninitialized tensor
bool caught = false;
try {
dst_tensor.ShareDataFrom(src_tensor);
} catch (EnforceNotMet err) {
caught = true;
std::string msg = "Can not share data from an uninitialized tensor.";
const char* what = err.what();
for (size_t i = 0; i < msg.length(); ++i) {
ASSERT_EQ(what[i], msg[i]);
}
}
ASSERT_TRUE(caught);
src_tensor.mutable_data<int>(make_ddim({2, 3, 4}), CPUPlace());
dst_tensor.ShareDataFrom(src_tensor);
ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>());
}
{
Tensor src_tensor;
Tensor dst_tensor;
src_tensor.mutable_data<int>(make_ddim({2, 3, 4}), GPUPlace());
dst_tensor.ShareDataFrom(src_tensor);
ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>());
}
}
TEST(Tensor, Slice) {
using namespace paddle::framework;
using namespace paddle::platform;
{
Tensor src_tensor;
src_tensor.mutable_data<int>(make_ddim({5, 3, 4}), CPUPlace());
Tensor slice_tensor = src_tensor.Slice(1, 3);
DDim slice_dims = slice_tensor.dims();
ASSERT_EQ(arity(slice_dims), 3);
EXPECT_EQ(slice_dims[0], 2);
EXPECT_EQ(slice_dims[1], 3);
EXPECT_EQ(slice_dims[2], 4);
uintptr_t src_data_address =
reinterpret_cast<uintptr_t>(src_tensor.data<int>());
uintptr_t src_mutable_data_address = reinterpret_cast<uintptr_t>(
src_tensor.mutable_data<int>(src_tensor.dims(), CPUPlace()));
uintptr_t slice_data_address =
reinterpret_cast<uintptr_t>(slice_tensor.data<int>());
uintptr_t slice_mutable_data_address = reinterpret_cast<uintptr_t>(
slice_tensor.mutable_data<int>(slice_tensor.dims(), CPUPlace()));
EXPECT_EQ(src_data_address, src_mutable_data_address);
EXPECT_EQ(slice_data_address, slice_mutable_data_address);
EXPECT_EQ(src_data_address + 3 * 4 * 1 * sizeof(int), slice_data_address);
}
{
Tensor src_tensor;
src_tensor.mutable_data<double>(make_ddim({6, 9}), GPUPlace());
Tensor slice_tensor = src_tensor.Slice(2, 6);
DDim slice_dims = slice_tensor.dims();
ASSERT_EQ(arity(slice_dims), 2);
EXPECT_EQ(slice_dims[0], 4);
EXPECT_EQ(slice_dims[1], 9);
uintptr_t src_data_address =
reinterpret_cast<uintptr_t>(src_tensor.data<double>());
uintptr_t src_mutable_data_address = reinterpret_cast<uintptr_t>(
src_tensor.mutable_data<double>(src_tensor.dims(), GPUPlace()));
uintptr_t slice_data_address =
reinterpret_cast<uintptr_t>(slice_tensor.data<double>());
uintptr_t slice_mutable_data_address = reinterpret_cast<uintptr_t>(
slice_tensor.mutable_data<double>(slice_tensor.dims(), GPUPlace()));
EXPECT_EQ(src_data_address, src_mutable_data_address);
EXPECT_EQ(slice_data_address, slice_mutable_data_address);
EXPECT_EQ(src_data_address + 9 * 2 * sizeof(double), slice_data_address);
}
}
*/
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录