Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
TonyTonyFun
Paddle
提交
5419f16b
P
Paddle
项目概览
TonyTonyFun
/
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,发现更多精彩内容 >>
提交
5419f16b
编写于
9月 25, 2017
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add unittests
上级
f9f910a3
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
79 addition
and
7 deletion
+79
-7
paddle/pybind/protobuf.cc
paddle/pybind/protobuf.cc
+44
-5
python/paddle/v2/framework/tests/test_protobuf_descs.py
python/paddle/v2/framework/tests/test_protobuf_descs.py
+35
-2
未找到文件。
paddle/pybind/protobuf.cc
浏览文件 @
5419f16b
...
...
@@ -99,7 +99,7 @@ template <typename T, typename RepeatedField>
inline
void
VectorToRepeated
(
const
std
::
vector
<
T
>
&
vec
,
RepeatedField
*
repeated_field
)
{
repeated_field
->
Reserve
(
vec
.
size
());
for
(
auto
&
elem
:
vec
)
{
for
(
const
auto
&
elem
:
vec
)
{
*
repeated_field
->
Add
()
=
elem
;
}
}
...
...
@@ -124,18 +124,23 @@ public:
VarDesc
*
Proto
()
{
return
&
desc_
;
}
py
::
bytes
Name
()
{
return
desc_
.
name
();
}
void
SetShape
(
const
std
::
vector
<
int64_t
>
&
dims
)
{
VectorToRepeated
(
dims
,
desc_
.
mutable_lod_tensor
()
->
mutable_dims
());
}
void
SetDataType
(
int
type_id
)
{
desc_
.
mutable_lod_tensor
()
->
set_data_type
(
static_cast
<
DataType
>
(
type_id
));
desc_
.
mutable_lod_tensor
()
->
set_data_type
(
static_cast
<
enum
DataType
>
(
type_id
));
}
std
::
vector
<
int64_t
>
Shape
()
{
return
RepeatedToVector
(
desc_
.
lod_tensor
().
dims
());
}
int
DataType
()
{
return
desc_
.
lod_tensor
().
data_type
();
}
private:
VarDesc
desc_
;
};
...
...
@@ -322,6 +327,22 @@ public:
return
var
;
}
VarDescBind
*
Var
(
py
::
bytes
name_bytes
)
const
{
std
::
string
name
=
name_bytes
;
auto
it
=
vars_
.
find
(
name
);
PADDLE_ENFORCE
(
it
!=
vars_
.
end
(),
"Can not find variable %s in current block."
,
name
);
return
it
->
second
.
get
();
}
std
::
vector
<
VarDescBind
*>
AllVars
()
const
{
std
::
vector
<
VarDescBind
*>
res
;
for
(
const
auto
&
p
:
vars_
)
{
res
.
push_back
(
p
.
second
.
get
());
}
return
res
;
}
BlockDescBind
*
ParentBlock
()
const
;
OpDescBind
*
AppendOp
()
{
...
...
@@ -336,6 +357,14 @@ public:
return
ops_
.
front
().
get
();
}
std
::
vector
<
OpDescBind
*>
AllOps
()
const
{
std
::
vector
<
OpDescBind
*>
res
;
for
(
const
auto
&
op
:
ops_
)
{
res
.
push_back
(
op
.
get
());
}
return
res
;
}
void
Sync
()
{
if
(
need_update_
)
{
auto
&
op_field
=
*
this
->
desc_
->
mutable_ops
();
...
...
@@ -461,16 +490,26 @@ void BindBlockDesc(py::module &m) {
.
def
(
"prepend_op"
,
&
BlockDescBind
::
PrependOp
,
py
::
return_value_policy
::
reference
)
.
def
(
"new_var"
,
&
BlockDescBind
::
NewVar
,
.
def
(
"new_var"
,
&
BlockDescBind
::
NewVar
,
py
::
return_value_policy
::
reference
)
.
def
(
"var"
,
&
BlockDescBind
::
Var
,
py
::
return_value_policy
::
reference
)
.
def
(
"all_vars"
,
&
BlockDescBind
::
AllVars
,
py
::
return_value_policy
::
reference
)
.
def
(
"all_ops"
,
&
BlockDescBind
::
AllOps
,
py
::
return_value_policy
::
reference
);
}
void
BindVarDsec
(
py
::
module
&
m
)
{
py
::
class_
<
VarDescBind
>
(
m
,
"VarDesc"
,
""
)
.
def
(
"name"
,
&
VarDescBind
::
Name
,
py
::
return_value_policy
::
reference
)
.
def
(
"set_shape"
,
&
VarDescBind
::
SetShape
)
.
def
(
"set_data_type"
,
&
VarDescBind
::
SetDataType
)
.
def
(
"shape"
,
&
VarDescBind
::
Shape
);
.
def
(
"shape"
,
&
VarDescBind
::
Shape
,
py
::
return_value_policy
::
reference
)
.
def
(
"data_type"
,
&
VarDescBind
::
DataType
,
py
::
return_value_policy
::
reference
);
}
void
BindOpDesc
(
py
::
module
&
m
)
{
...
...
python/paddle/v2/framework/tests/test_protobuf_descs.py
浏览文件 @
5419f16b
...
...
@@ -57,7 +57,7 @@ class TestOpDesc(unittest.TestCase):
class
TestProgramDesc
(
unittest
.
TestCase
):
def
test_instance
(
self
):
program_desc
=
core
.
ProgramDesc
.
instance
()
program_desc
=
core
.
ProgramDesc
.
__create_program_desc__
()
self
.
assertIsNotNone
(
program_desc
)
del
program_desc
program_desc
=
core
.
ProgramDesc
.
instance
()
...
...
@@ -84,7 +84,7 @@ class TestProgramDesc(unittest.TestCase):
class
TestVarDesc
(
unittest
.
TestCase
):
def
test_shape
(
self
):
program_desc
=
core
.
ProgramDesc
.
instance
()
program_desc
=
core
.
ProgramDesc
.
__create_program_desc__
()
block
=
program_desc
.
block
(
0
)
var
=
block
.
new_var
(
'my_var'
)
src_shape
=
[
3
,
2
,
10
,
8
]
...
...
@@ -92,6 +92,39 @@ class TestVarDesc(unittest.TestCase):
res_shape
=
var
.
shape
()
self
.
assertEqual
(
src_shape
,
res_shape
)
def
test_data_type
(
self
):
program_desc
=
core
.
ProgramDesc
.
__create_program_desc__
()
block
=
program_desc
.
block
(
0
)
var
=
block
.
new_var
(
'my_var'
)
var
.
set_data_type
(
2
)
self
.
assertEqual
(
2
,
var
.
data_type
)
class
TestBlockDesc
(
unittest
.
TestCase
):
def
test_add_var
(
self
):
prog
=
core
.
ProgramDesc
.
__create_program_desc__
()
self
.
assertIsNotNone
(
prog
)
block
=
prog
.
block
(
0
)
self
.
assertIsNotNone
(
block
)
var1
=
block
.
new_var
(
"var1"
)
var2
=
block
.
new_var
(
"var2"
)
var3
=
block
.
new_var
(
"var3"
)
all_vars
=
block
.
all_vars
()
self
.
assertEqual
(
set
(
all_vars
),
set
([
var1
,
var2
,
var3
]))
var2_re
=
block
.
var
(
"var2"
)
self
.
assertEqual
(
var2_re
,
var2
)
def
test_add_op
(
self
):
prog
=
core
.
ProgramDesc
.
__create_program_desc__
()
self
.
assertIsNotNone
(
prog
)
block
=
prog
.
block
(
0
)
self
.
assertIsNotNone
(
block
)
op1
=
block
.
append_op
()
op2
=
block
.
append_op
()
op0
=
block
.
prepend_op
()
all_ops
=
block
.
all_ops
()
self
.
assertEqual
(
all_ops
,
[
op0
,
op1
,
op2
])
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录