Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
b4a4ae1b
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b4a4ae1b
编写于
9月 26, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add comments
上级
9e5de167
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
18 addition
and
10 deletion
+18
-10
paddle/pybind/protobuf.cc
paddle/pybind/protobuf.cc
+18
-10
未找到文件。
paddle/pybind/protobuf.cc
浏览文件 @
b4a4ae1b
...
@@ -85,6 +85,7 @@ namespace pybind {
...
@@ -85,6 +85,7 @@ namespace pybind {
using
namespace
paddle
::
framework
;
// NOLINT
using
namespace
paddle
::
framework
;
// NOLINT
// convert between std::vector and protobuf repeated.
template
<
typename
T
>
template
<
typename
T
>
inline
std
::
vector
<
T
>
RepeatedToVector
(
inline
std
::
vector
<
T
>
RepeatedToVector
(
const
google
::
protobuf
::
RepeatedField
<
T
>
&
repeated_field
)
{
const
google
::
protobuf
::
RepeatedField
<
T
>
&
repeated_field
)
{
...
@@ -104,6 +105,7 @@ inline void VectorToRepeated(const std::vector<T> &vec,
...
@@ -104,6 +105,7 @@ inline void VectorToRepeated(const std::vector<T> &vec,
}
}
}
}
// Specialize vector<bool>.
template
<
typename
RepeatedField
>
template
<
typename
RepeatedField
>
inline
void
VectorToRepeated
(
const
std
::
vector
<
bool
>
&
vec
,
inline
void
VectorToRepeated
(
const
std
::
vector
<
bool
>
&
vec
,
RepeatedField
*
repeated_field
)
{
RepeatedField
*
repeated_field
)
{
...
@@ -118,13 +120,16 @@ class OpDescBind;
...
@@ -118,13 +120,16 @@ class OpDescBind;
class
BlockDescBind
;
class
BlockDescBind
;
class
VarDescBind
;
class
VarDescBind
;
// Each Protobuf Message, we provide a XXXBind class. In that class, we optimize
// read/write speed. Only when we want the protobuf message, the local changes
// will be synchronized (by `Sync` method).
class
VarDescBind
{
class
VarDescBind
{
public:
public:
explicit
VarDescBind
(
const
std
::
string
&
name
)
{
desc_
.
set_name
(
name
);
}
explicit
VarDescBind
(
const
std
::
string
&
name
)
{
desc_
.
set_name
(
name
);
}
VarDesc
*
Proto
()
{
return
&
desc_
;
}
VarDesc
*
Proto
()
{
return
&
desc_
;
}
py
::
bytes
Name
()
{
return
desc_
.
name
();
}
py
::
bytes
Name
()
const
{
return
desc_
.
name
();
}
void
SetShape
(
const
std
::
vector
<
int64_t
>
&
dims
)
{
void
SetShape
(
const
std
::
vector
<
int64_t
>
&
dims
)
{
VectorToRepeated
(
dims
,
desc_
.
mutable_lod_tensor
()
->
mutable_dims
());
VectorToRepeated
(
dims
,
desc_
.
mutable_lod_tensor
()
->
mutable_dims
());
...
@@ -134,11 +139,13 @@ public:
...
@@ -134,11 +139,13 @@ public:
desc_
.
mutable_lod_tensor
()
->
set_data_type
(
data_type
);
desc_
.
mutable_lod_tensor
()
->
set_data_type
(
data_type
);
}
}
std
::
vector
<
int64_t
>
Shape
()
{
std
::
vector
<
int64_t
>
Shape
()
const
{
return
RepeatedToVector
(
desc_
.
lod_tensor
().
dims
());
return
RepeatedToVector
(
desc_
.
lod_tensor
().
dims
());
}
}
framework
::
DataType
DataType
()
{
return
desc_
.
lod_tensor
().
data_type
();
}
framework
::
DataType
DataType
()
const
{
return
desc_
.
lod_tensor
().
data_type
();
}
private:
private:
VarDesc
desc_
;
VarDesc
desc_
;
...
@@ -283,16 +290,16 @@ public:
...
@@ -283,16 +290,16 @@ public:
void
SetBlockAttr
(
const
std
::
string
&
name
,
BlockDescBind
&
block
);
void
SetBlockAttr
(
const
std
::
string
&
name
,
BlockDescBind
&
block
);
int
GetBlock
Attr
(
const
std
::
string
&
name
)
const
{
Attribute
Get
Attr
(
const
std
::
string
&
name
)
const
{
auto
it
=
attrs_
.
find
(
name
);
auto
it
=
attrs_
.
find
(
name
);
PADDLE_ENFORCE
(
it
!=
attrs_
.
end
(),
"Attribute %s is not found"
,
name
);
PADDLE_ENFORCE
(
it
!=
attrs_
.
end
(),
"Attribute %s is not found"
,
name
);
return
boost
::
get
<
BlockDesc
*>
(
it
->
second
)
->
idx
()
;
return
it
->
second
;
}
}
Attribute
Get
Attr
(
const
std
::
string
&
name
)
const
{
int
GetBlock
Attr
(
const
std
::
string
&
name
)
const
{
auto
it
=
attrs_
.
find
(
name
);
auto
it
=
attrs_
.
find
(
name
);
PADDLE_ENFORCE
(
it
!=
attrs_
.
end
(),
"Attribute %s is not found"
,
name
);
PADDLE_ENFORCE
(
it
!=
attrs_
.
end
(),
"Attribute %s is not found"
,
name
);
return
it
->
second
;
return
boost
::
get
<
BlockDesc
*>
(
it
->
second
)
->
idx
()
;
}
}
private:
private:
...
@@ -312,7 +319,7 @@ public:
...
@@ -312,7 +319,7 @@ public:
BlockDescBind
(
const
BlockDescBind
&
o
)
=
delete
;
BlockDescBind
(
const
BlockDescBind
&
o
)
=
delete
;
BlockDescBind
&
operator
=
(
const
BlockDescBind
&
o
)
=
delete
;
BlockDescBind
&
operator
=
(
const
BlockDescBind
&
o
)
=
delete
;
int32_t
id
()
const
{
return
desc_
->
idx
();
}
int32_t
ID
()
const
{
return
desc_
->
idx
();
}
int32_t
Parent
()
const
{
return
desc_
->
parent_idx
();
}
int32_t
Parent
()
const
{
return
desc_
->
parent_idx
();
}
...
@@ -410,7 +417,7 @@ public:
...
@@ -410,7 +417,7 @@ public:
BlockDescBind
*
AppendBlock
(
const
BlockDescBind
&
parent
)
{
BlockDescBind
*
AppendBlock
(
const
BlockDescBind
&
parent
)
{
auto
*
b
=
prog_
->
add_blocks
();
auto
*
b
=
prog_
->
add_blocks
();
b
->
set_parent_idx
(
parent
.
id
());
b
->
set_parent_idx
(
parent
.
ID
());
b
->
set_idx
(
prog_
->
blocks_size
()
-
1
);
b
->
set_idx
(
prog_
->
blocks_size
()
-
1
);
blocks_
.
emplace_back
(
new
BlockDescBind
(
this
,
b
));
blocks_
.
emplace_back
(
new
BlockDescBind
(
this
,
b
));
return
blocks_
.
back
().
get
();
return
blocks_
.
back
().
get
();
...
@@ -454,6 +461,7 @@ void OpDescBind::SetBlockAttr(const std::string &name, BlockDescBind &block) {
...
@@ -454,6 +461,7 @@ void OpDescBind::SetBlockAttr(const std::string &name, BlockDescBind &block) {
this
->
attrs_
[
name
]
=
desc
;
this
->
attrs_
[
name
]
=
desc
;
}
}
// Bind Methods
void
BindProgramDesc
(
py
::
module
&
m
)
{
void
BindProgramDesc
(
py
::
module
&
m
)
{
py
::
class_
<
ProgramDescBind
>
(
m
,
"ProgramDesc"
,
""
)
py
::
class_
<
ProgramDescBind
>
(
m
,
"ProgramDesc"
,
""
)
.
def_static
(
"instance"
,
.
def_static
(
"instance"
,
...
@@ -481,7 +489,7 @@ void BindProgramDesc(py::module &m) {
...
@@ -481,7 +489,7 @@ void BindProgramDesc(py::module &m) {
void
BindBlockDesc
(
py
::
module
&
m
)
{
void
BindBlockDesc
(
py
::
module
&
m
)
{
py
::
class_
<
BlockDescBind
>
(
m
,
"BlockDesc"
,
""
)
py
::
class_
<
BlockDescBind
>
(
m
,
"BlockDesc"
,
""
)
.
def_property_readonly
(
"id"
,
&
BlockDescBind
::
id
)
.
def_property_readonly
(
"id"
,
&
BlockDescBind
::
ID
)
.
def_property_readonly
(
"parent"
,
&
BlockDescBind
::
Parent
)
.
def_property_readonly
(
"parent"
,
&
BlockDescBind
::
Parent
)
.
def
(
"append_op"
,
.
def
(
"append_op"
,
&
BlockDescBind
::
AppendOp
,
&
BlockDescBind
::
AppendOp
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录