Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
马赛克医院
Paddle
提交
d11430e0
P
Paddle
项目概览
马赛克医院
/
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d11430e0
编写于
9月 13, 2017
作者:
D
dangqingqing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use the inheritance in the definition of LoDTensor.
上级
6d2e87f9
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
52 addition
and
66 deletion
+52
-66
paddle/framework/lod_tensor.h
paddle/framework/lod_tensor.h
+3
-7
paddle/framework/lod_tensor_test.cc
paddle/framework/lod_tensor_test.cc
+20
-25
paddle/framework/lod_tensor_test.cu
paddle/framework/lod_tensor_test.cu
+2
-4
paddle/framework/tensor_impl.h
paddle/framework/tensor_impl.h
+4
-0
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+7
-18
paddle/pybind/tensor_py.h
paddle/pybind/tensor_py.h
+1
-0
python/paddle/v2/framework/tests/test_tensor.py
python/paddle/v2/framework/tests/test_tensor.py
+15
-12
未找到文件。
paddle/framework/lod_tensor.h
浏览文件 @
d11430e0
...
...
@@ -51,16 +51,13 @@ bool operator==(const LoD& a, const LoD& b);
* LoDTensor (Level of details Tensor)
* see https://en.wikipedia.org/wiki/Level_of_details for reference.
*/
class
LoDTensor
{
class
LoDTensor
:
public
Tensor
{
public:
LoDTensor
()
{}
LoDTensor
(
const
LoD
&
lod
,
Tensor
*
t
)
:
lod_
(
lod
),
tensor_
(
t
)
{}
void
set_lod
(
const
LoD
&
lod
)
{
lod_
=
lod
;
}
void
set_tensor
(
Tensor
*
tensor
)
{
tensor_
=
tensor
;
}
explicit
LoDTensor
(
const
LoD
&
lod
)
:
lod_
(
lod
)
{}
Tensor
&
tensor
()
{
return
*
tensor_
;
}
void
set_lod
(
const
LoD
&
lod
)
{
lod_
=
lod
;
}
LoD
lod
()
{
return
lod_
;
}
...
...
@@ -104,7 +101,6 @@ class LoDTensor {
private:
LoD
lod_
;
Tensor
*
tensor_
;
// not owned
};
}
// namespace framework
}
// namespace paddle
paddle/framework/lod_tensor_test.cc
浏览文件 @
d11430e0
...
...
@@ -36,69 +36,64 @@ class LoDTensorTester : public ::testing::Test {
ASSERT_EQ
(
lod
.
size
(),
3UL
);
tensor
.
Resize
({
20
/*batch size*/
,
128
/*dim*/
});
lod_tensor_
.
Resize
({
20
/*batch size*/
,
128
/*dim*/
});
// malloc memory
tensor
.
mutable_data
<
float
>
(
place
);
lod_tensor_
.
mutable_data
<
float
>
(
place
);
lod_tensor
.
set_lod
(
lod
);
lod_tensor
.
set_tensor
(
&
tensor
);
lod_tensor_
.
set_lod
(
lod
);
}
protected:
platform
::
CPUPlace
place
;
Tensor
tensor
;
LoDTensor
lod_tensor
;
LoDTensor
lod_tensor_
;
};
TEST_F
(
LoDTensorTester
,
NumLevels
)
{
ASSERT_EQ
(
lod_tensor
.
NumLevels
(),
3UL
);
}
TEST_F
(
LoDTensorTester
,
NumLevels
)
{
ASSERT_EQ
(
lod_tensor
_
.
NumLevels
(),
3UL
);
}
TEST_F
(
LoDTensorTester
,
NumElements
)
{
ASSERT_EQ
(
lod_tensor
.
NumElements
(
0
),
2UL
);
ASSERT_EQ
(
lod_tensor
.
NumElements
(
1
),
4UL
);
ASSERT_EQ
(
lod_tensor
.
NumElements
(
2
),
8UL
);
ASSERT_EQ
(
lod_tensor
_
.
NumElements
(
0
),
2UL
);
ASSERT_EQ
(
lod_tensor
_
.
NumElements
(
1
),
4UL
);
ASSERT_EQ
(
lod_tensor
_
.
NumElements
(
2
),
8UL
);
}
TEST_F
(
LoDTensorTester
,
SliceLevels
)
{
// slice 1 level
for
(
size_t
level
=
0
;
level
<
3UL
;
++
level
)
{
LoDTensor
new_lod_tensor
=
lod_tensor
;
LoDTensor
new_lod_tensor
=
lod_tensor
_
;
new_lod_tensor
.
SliceLevels
(
level
,
level
+
1
);
ASSERT_EQ
(
new_lod_tensor
.
NumLevels
(),
1UL
);
ASSERT_EQ
(
new_lod_tensor
.
NumElements
(
0
),
lod_tensor
.
NumElements
(
level
));
ASSERT_EQ
(
new_lod_tensor
.
tensor
().
data
<
float
>
(),
lod_tensor
.
tensor
().
data
<
float
>
());
ASSERT_EQ
(
new_lod_tensor
.
NumElements
(
0
),
lod_tensor_
.
NumElements
(
level
));
ASSERT_EQ
(
new_lod_tensor
.
data
<
float
>
(),
lod_tensor_
.
data
<
float
>
());
}
// slice 2 level
for
(
size_t
level
=
0
;
level
<
2UL
;
++
level
)
{
LoDTensor
new_lod_tensor
=
lod_tensor
;
LoDTensor
new_lod_tensor
=
lod_tensor
_
;
new_lod_tensor
.
SliceLevels
(
level
,
level
+
2
);
ASSERT_EQ
(
new_lod_tensor
.
NumLevels
(),
2UL
);
ASSERT_EQ
(
new_lod_tensor
.
NumElements
(
0
),
lod_tensor
.
NumElements
(
level
));
ASSERT_EQ
(
new_lod_tensor
.
NumElements
(
1
),
lod_tensor
.
NumElements
(
level
+
1
));
ASSERT_EQ
(
new_lod_tensor
.
tensor
().
data
<
float
>
(),
lod_tensor
.
tensor
()
.
data
<
float
>
());
ASSERT_EQ
(
new_lod_tensor
.
NumElements
(
0
),
lod_tensor
_
.
NumElements
(
level
));
ASSERT_EQ
(
new_lod_tensor
.
NumElements
(
1
),
lod_tensor_
.
NumElements
(
level
+
1
));
ASSERT_EQ
(
new_lod_tensor
.
data
<
float
>
(),
lod_tensor_
.
data
<
float
>
());
}
}
TEST_F
(
LoDTensorTester
,
SliceInLevel
)
{
size_t
level
=
0
;
LoDTensor
new_lod_tensor
=
lod_tensor
;
LoDTensor
new_lod_tensor
=
lod_tensor
_
;
new_lod_tensor
.
SliceInLevel
(
level
,
0
,
2
);
EXPECT_EQ
(
new_lod_tensor
.
NumLevels
(),
3UL
);
EXPECT_EQ
(
new_lod_tensor
.
NumElements
(
0
),
2UL
);
EXPECT_EQ
(
new_lod_tensor
.
NumElements
(
1
),
4UL
);
EXPECT_EQ
(
new_lod_tensor
.
NumElements
(
2
),
8UL
);
ASSERT_EQ
(
new_lod_tensor
.
tensor
().
data
<
float
>
(),
lod_tensor
.
tensor
().
data
<
float
>
());
ASSERT_EQ
(
new_lod_tensor
.
data
<
float
>
(),
lod_tensor_
.
data
<
float
>
());
level
=
1
;
new_lod_tensor
=
lod_tensor
;
new_lod_tensor
=
lod_tensor
_
;
new_lod_tensor
.
SliceInLevel
(
level
,
0
,
2
);
ASSERT_EQ
(
new_lod_tensor
.
NumLevels
(),
2UL
);
ASSERT_EQ
(
new_lod_tensor
.
NumElements
(
0
),
2UL
);
ASSERT_EQ
(
new_lod_tensor
.
NumElements
(
1
),
4UL
);
ASSERT_EQ
(
new_lod_tensor
.
tensor
().
data
<
float
>
(),
lod_tensor
.
tensor
().
data
<
float
>
());
ASSERT_EQ
(
new_lod_tensor
.
data
<
float
>
(),
lod_tensor_
.
data
<
float
>
());
}
}
// namespace framework
...
...
paddle/framework/lod_tensor_test.cu
浏览文件 @
d11430e0
...
...
@@ -26,18 +26,16 @@ __global__ void test(size_t* a, int size) {
}
TEST
(
LoDTensor
,
LoDInGPU
)
{
paddle
::
framework
::
Tensor
tensor
;
paddle
::
framework
::
LoDTensor
lod_tensor
;
paddle
::
platform
::
GPUPlace
place
(
0
);
paddle
::
framework
::
LoD
src_lod
;
src_lod
.
push_back
(
std
::
vector
<
size_t
>
{
0
,
2
,
4
,
6
,
8
,
10
,
12
,
14
});
tensor
.
Resize
({
14
,
16
});
tensor
.
mutable_data
<
float
>
(
place
);
lod_
tensor
.
Resize
({
14
,
16
});
lod_
tensor
.
mutable_data
<
float
>
(
place
);
lod_tensor
.
set_lod
(
src_lod
);
lod_tensor
.
set_tensor
(
&
tensor
);
CHECK_EQ
(
lod_tensor
.
lod_element
(
0
,
2
),
4
);
CHECK_EQ
(
lod_tensor
.
lod_element
(
0
,
4
),
8
);
...
...
paddle/framework/tensor_impl.h
浏览文件 @
d11430e0
...
...
@@ -16,6 +16,8 @@ limitations under the License. */
#include "paddle/memory/memcpy.h"
#include "paddle/platform/enforce.h"
#include <glog/logging.h>
namespace
paddle
{
namespace
framework
{
...
...
@@ -53,6 +55,7 @@ inline T* Tensor::mutable_data(DDim dims, platform::Place place) {
template
<
typename
T
>
inline
T
*
Tensor
::
mutable_data
(
platform
::
Place
place
)
{
LOG
(
INFO
)
<<
"------ mutable_data ---- "
;
static_assert
(
std
::
is_pod
<
T
>::
value
,
"T must be POD"
);
PADDLE_ENFORCE_GT
(
numel
(),
0
,
"Tensor's numel must be larger than zero to call "
...
...
@@ -142,6 +145,7 @@ inline Tensor Tensor::Slice(const int& begin_idx, const int& end_idx) const {
}
inline
Tensor
&
Tensor
::
Resize
(
const
DDim
&
dims
)
{
LOG
(
INFO
)
<<
"---- resize -----"
;
dims_
=
dims
;
numel_
=
product
(
dims_
);
return
*
this
;
...
...
paddle/pybind/pybind.cc
浏览文件 @
d11430e0
...
...
@@ -121,27 +121,19 @@ PYBIND11_PLUGIN(core) {
return
self
.
data
<
float
>
()[
offset
];
});
py
::
class_
<
LoDTensor
>
(
m
,
"LoDTensor"
,
R"DOC(LoD(Leval of Ddetails) Tensor.
The tensor and LoD info should be created before creating the LoDTensor, then
call the set_tensor and set_lod functions to set them.
)DOC"
)
.
def
(
"__init__"
,
[](
LoDTensor
&
instance
,
const
std
::
vector
<
std
::
vector
<
size_t
>>
&
lod
,
Tensor
*
t
)
{
py
::
class_
<
LoDTensor
,
Tensor
>
(
m
,
"LoDTensor"
)
.
def
(
"__init__"
,
[](
LoDTensor
&
instance
,
const
std
::
vector
<
std
::
vector
<
size_t
>>
&
lod
)
{
#ifdef PADDLE_ONLY_CPU
new
(
&
instance
)
LoDTensor
(
lod
,
t
);
new
(
&
instance
)
LoDTensor
(
lod
);
#else
paddle
::
framework
::
LoD
new_lod
;
new_lod
.
reserve
(
lod
.
size
());
std
::
copy
(
lod
.
begin
(),
lod
.
end
(),
std
::
back_inserter
(
new_lod
));
new
(
&
instance
)
LoDTensor
(
new_lod
,
t
);
new
(
&
instance
)
LoDTensor
(
new_lod
);
#endif
})
.
def
(
"set_tensor"
,
[](
LoDTensor
&
self
,
Tensor
*
tensor
)
{
self
.
set_tensor
(
tensor
);
})
})
.
def
(
"set_lod"
,
[](
LoDTensor
&
self
,
const
std
::
vector
<
std
::
vector
<
size_t
>>
&
lod
)
{
#ifdef PADDLE_ONLY_CPU
...
...
@@ -153,9 +145,6 @@ call the set_tensor and set_lod functions to set them.
self
.
set_lod
(
new_lod
);
#endif
})
.
def
(
"tensor"
,
[](
LoDTensor
&
self
)
->
Tensor
&
{
return
self
.
tensor
();
},
py
::
return_value_policy
::
reference
)
.
def
(
"lod"
,
[](
LoDTensor
&
self
)
->
std
::
vector
<
std
::
vector
<
size_t
>>
{
#ifdef PADDLE_ONLY_CPU
return
self
.
lod
();
...
...
paddle/pybind/tensor_py.h
浏览文件 @
d11430e0
...
...
@@ -42,6 +42,7 @@ template <size_t I, typename... ARGS>
struct
CastToPyBufferImpl
<
true
,
I
,
ARGS
...
>
{
using
CUR_TYPE
=
typename
std
::
tuple_element
<
I
,
std
::
tuple
<
ARGS
...
>>::
type
;
py
::
buffer_info
operator
()(
framework
::
Tensor
&
tensor
)
{
LOG
(
INFO
)
<<
"---- CastToPyBufferImpl -----"
;
if
(
std
::
type_index
(
typeid
(
CUR_TYPE
))
==
tensor
.
holder_
->
type
())
{
auto
dim_vec
=
framework
::
vectorize
(
tensor
.
dims
());
std
::
vector
<
size_t
>
dims_outside
;
...
...
python/paddle/v2/framework/tests/test_tensor.py
浏览文件 @
d11430e0
...
...
@@ -4,7 +4,7 @@ import numpy
class
TestTensor
(
unittest
.
TestCase
):
def
test_int_tensor
(
self
):
def
not_
test_int_tensor
(
self
):
scope
=
core
.
Scope
()
var
=
scope
.
new_var
(
"test_tensor"
)
place
=
core
.
CPUPlace
()
...
...
@@ -23,7 +23,7 @@ class TestTensor(unittest.TestCase):
self
.
assertEqual
(
1
,
tensor_array_2
[
3
,
9
])
self
.
assertEqual
(
2
,
tensor_array_2
[
19
,
11
])
def
test_float_tensor
(
self
):
def
not_
test_float_tensor
(
self
):
scope
=
core
.
Scope
()
var
=
scope
.
new_var
(
"test_tensor"
)
place
=
core
.
CPUPlace
()
...
...
@@ -47,23 +47,26 @@ class TestTensor(unittest.TestCase):
places
=
[
core
.
CPUPlace
(),
core
.
GPUPlace
(
0
)]
for
place
in
places
:
scope
=
core
.
Scope
()
var
=
scope
.
new_var
(
"test_tensor"
)
#
var = scope.new_var("test_tensor")
var_lod
=
scope
.
new_var
(
"test_lod_tensor"
)
tensor
=
var
.
get_tensor
()
#
tensor = var.get_tensor()
lod_tensor
=
var_lod
.
get_lod_tensor
()
tensor
.
set_dims
([
4
,
4
,
6
])
tensor
.
alloc_int
(
place
)
array
=
numpy
.
array
(
tensor
)
lod_tensor
.
set_dims
([
4
,
4
,
6
])
lod_tensor
.
alloc_int
(
place
)
print
lod_tensor
array
=
numpy
.
array
(
lod_tensor
)
print
"---- array ----"
,
array
array
[
0
,
0
,
0
]
=
3
array
[
3
,
3
,
5
]
=
10
tensor
.
set
(
array
,
place
)
lod_
tensor
.
set
(
array
,
place
)
lod_tensor
.
set_tensor
(
tensor
)
#
lod_tensor.set_tensor(tensor)
lod_tensor
.
set_lod
([[
0
,
2
,
4
]])
lod_v
=
numpy
.
array
(
lod_tensor
.
tensor
())
# lod_v = numpy.array(lod_tensor.tensor())
lod_v
=
numpy
.
array
(
lod_tensor
)
self
.
assertTrue
(
numpy
.
alltrue
(
array
==
lod_v
))
lod
=
lod_tensor
.
lod
()
...
...
@@ -71,7 +74,7 @@ class TestTensor(unittest.TestCase):
self
.
assertEqual
(
2
,
lod
[
0
][
1
])
self
.
assertEqual
(
4
,
lod
[
0
][
2
])
def
test_float_lod_tensor
(
self
):
def
not_
test_float_lod_tensor
(
self
):
places
=
[
core
.
CPUPlace
(),
core
.
GPUPlace
(
0
)]
for
place
in
places
:
scope
=
core
.
Scope
()
...
...
@@ -102,7 +105,7 @@ class TestTensor(unittest.TestCase):
lod
=
lod_tensor
.
lod
()
self
.
assertListEqual
(
lod_py
,
lod
)
def
test_lod_tensor_init
(
self
):
def
not_
test_lod_tensor_init
(
self
):
scope
=
core
.
Scope
()
var
=
scope
.
new_var
(
"test_tensor"
)
place
=
core
.
CPUPlace
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录