Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7f6b5044
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
提交
7f6b5044
编写于
8月 20, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make OpInfoMap as a class
* Add Get/Has methods to OpInfoMap * Add PADDLE_ENFORCE for OpInfo to get field.
上级
59b3df31
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
84 addition
and
47 deletion
+84
-47
paddle/framework/grad_op_builder.cc
paddle/framework/grad_op_builder.cc
+6
-14
paddle/framework/op_info.cc
paddle/framework/op_info.cc
+4
-5
paddle/framework/op_info.h
paddle/framework/op_info.h
+60
-1
paddle/framework/op_registry.cc
paddle/framework/op_registry.cc
+3
-5
paddle/framework/op_registry.h
paddle/framework/op_registry.h
+2
-2
paddle/framework/operator.cc
paddle/framework/operator.cc
+2
-10
paddle/framework/pybind.cc
paddle/framework/pybind.cc
+7
-10
未找到文件。
paddle/framework/grad_op_builder.cc
浏览文件 @
7f6b5044
...
...
@@ -24,9 +24,9 @@ static void TransOpArg(const OperatorBase* src_op, const OpArgType& src_type,
const
auto
&
src_inout
=
src_type
==
OpArgType
::
IN
?
src_op
->
Inputs
()
:
src_op
->
Outputs
();
auto
&
dst_inout
=
*
vars
;
const
OpProto
*
proto
=
OpInfoMap
().
at
(
src_op
->
Type
()).
proto_
;
auto
&
proto
=
OpInfoMap
::
Instance
().
Get
(
src_op
->
Type
()).
Proto
()
;
const
auto
&
src_arg_list
=
src_type
==
OpArgType
::
IN
?
proto
->
inputs
()
:
proto
->
outputs
();
src_type
==
OpArgType
::
IN
?
proto
.
inputs
()
:
proto
.
outputs
();
for
(
const
auto
&
arg
:
src_arg_list
)
{
if
(
arg
.
not_in_gradient
()
&&
!
is_grad
)
continue
;
const
std
::
string
src_name
=
arg
.
name
();
...
...
@@ -40,14 +40,8 @@ static void TransOpArg(const OperatorBase* src_op, const OpArgType& src_type,
}
OperatorBase
*
BuildGradOp
(
const
OperatorBase
*
op
)
{
auto
it
=
OpInfoMap
().
find
(
op
->
Type
());
PADDLE_ENFORCE
(
it
!=
OpInfoMap
().
end
(),
"'%s' has not been registered."
,
op
->
Type
());
PADDLE_ENFORCE
(
it
->
second
.
proto_
!=
nullptr
,
"'%s' has no OpProto."
,
op
->
Type
());
std
::
string
grad_op_type
=
it
->
second
.
grad_op_type_
;
PADDLE_ENFORCE
(
!
grad_op_type
.
empty
(),
"'%s' has no gradient operator."
,
op
->
Type
());
auto
&
info
=
OpInfoMap
::
Instance
().
Get
(
op
->
Type
());
PADDLE_ENFORCE
(
info
.
HasGradientOp
());
VariableNameMap
inputs
;
VariableNameMap
outputs
;
...
...
@@ -56,10 +50,8 @@ OperatorBase* BuildGradOp(const OperatorBase* op) {
TransOpArg
(
op
,
OpArgType
::
OUT
,
true
,
&
inputs
);
// OG
TransOpArg
(
op
,
OpArgType
::
IN
,
true
,
&
outputs
);
// IG
it
=
OpInfoMap
().
find
(
grad_op_type
);
PADDLE_ENFORCE
(
it
!=
OpInfoMap
().
end
(),
"'%s' has not been registered."
,
grad_op_type
);
return
it
->
second
.
creator_
(
grad_op_type
,
inputs
,
outputs
,
op
->
Attrs
());
auto
&
grad_info
=
OpInfoMap
::
Instance
().
Get
(
info
.
grad_op_type_
);
return
grad_info
.
Creator
()(
info
.
grad_op_type_
,
inputs
,
outputs
,
op
->
Attrs
());
}
}
// namespace framework
...
...
paddle/framework/op_info.cc
浏览文件 @
7f6b5044
...
...
@@ -17,12 +17,11 @@
namespace
paddle
{
namespace
framework
{
static
std
::
unordered_map
<
std
::
string
,
const
paddle
::
framework
::
OpInfo
>*
g_op_info_map
=
nullptr
;
std
::
unordered_map
<
std
::
string
,
const
paddle
::
framework
::
OpInfo
>&
OpInfoMap
()
{
static
OpInfoMap
*
g_op_info_map
=
nullptr
;
OpInfoMap
&
OpInfoMap
::
Instance
()
{
if
(
g_op_info_map
==
nullptr
)
{
g_op_info_map
=
new
std
::
unordered_map
<
std
::
string
,
const
paddle
::
framework
::
OpInfo
>
();
g_op_info_map
=
new
OpInfoMap
();
}
return
*
g_op_info_map
;
}
...
...
paddle/framework/op_info.h
浏览文件 @
7f6b5044
...
...
@@ -34,9 +34,68 @@ struct OpInfo {
std
::
string
grad_op_type_
;
OpProto
*
proto_
;
OpAttrChecker
*
checker_
;
bool
HasOpProtoAndChecker
()
const
{
return
proto_
!=
nullptr
&&
checker_
!=
nullptr
;
}
const
OpProto
&
Proto
()
const
{
PADDLE_ENFORCE_NOT_NULL
(
proto_
,
"Operator Proto has not been registered"
);
PADDLE_ENFORCE
(
proto_
->
IsInitialized
(),
"Operator Proto must be initialized in op info"
);
return
*
proto_
;
}
const
OpAttrChecker
&
Checker
()
const
{
PADDLE_ENFORCE_NOT_NULL
(
checker_
,
"Operator Checker has not been registered"
);
return
*
checker_
;
}
const
OpCreator
&
Creator
()
const
{
PADDLE_ENFORCE_NOT_NULL
(
creator_
,
"Operator Creator has not been registered"
);
return
creator_
;
}
bool
HasGradientOp
()
const
{
return
!
grad_op_type_
.
empty
();
}
};
extern
std
::
unordered_map
<
std
::
string
,
const
OpInfo
>&
OpInfoMap
();
class
OpInfoMap
{
public:
static
OpInfoMap
&
Instance
();
OpInfoMap
(
const
OpInfoMap
&
o
)
=
delete
;
OpInfoMap
(
OpInfoMap
&&
o
)
=
delete
;
OpInfoMap
&
operator
=
(
const
OpInfoMap
&
o
)
=
delete
;
OpInfoMap
&
operator
=
(
OpInfoMap
&&
o
)
=
delete
;
bool
Has
(
const
std
::
string
&
op_type
)
const
{
return
map_
.
find
(
op_type
)
!=
map_
.
end
();
}
void
Insert
(
const
std
::
string
&
type
,
const
OpInfo
&
info
)
{
PADDLE_ENFORCE
(
!
Has
(
type
),
"Operator %s has been registered"
,
type
);
map_
.
insert
({
type
,
info
});
}
const
OpInfo
&
Get
(
const
std
::
string
&
type
)
const
{
auto
it
=
map_
.
find
(
type
);
PADDLE_ENFORCE
(
it
!=
map_
.
end
(),
"Operator %s are not found"
,
type
);
return
it
->
second
;
}
template
<
typename
Callback
>
void
IterAllInfo
(
Callback
callback
)
{
for
(
auto
&
it
:
map_
)
{
callback
(
it
.
first
,
it
.
second
);
}
}
private:
OpInfoMap
()
=
default
;
std
::
unordered_map
<
std
::
string
,
const
OpInfo
>
map_
;
};
}
// namespace framework
}
// namespace paddle
paddle/framework/op_registry.cc
浏览文件 @
7f6b5044
...
...
@@ -22,11 +22,9 @@ namespace framework {
std
::
unique_ptr
<
OperatorBase
>
OpRegistry
::
CreateOp
(
const
std
::
string
&
type
,
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
AttributeMap
attrs
)
{
auto
it
=
OpInfoMap
().
find
(
type
);
PADDLE_ENFORCE
(
it
!=
OpInfoMap
().
end
(),
"Operator '%s' has not been registered."
,
type
);
it
->
second
.
checker_
->
Check
(
attrs
);
auto
op
=
it
->
second
.
creator_
(
type
,
inputs
,
outputs
,
attrs
);
auto
&
info
=
OpInfoMap
::
Instance
().
Get
(
type
);
info
.
Checker
().
Check
(
attrs
);
auto
op
=
info
.
Creator
()(
type
,
inputs
,
outputs
,
attrs
);
return
std
::
unique_ptr
<
OperatorBase
>
(
op
);
}
...
...
paddle/framework/op_registry.h
浏览文件 @
7f6b5044
...
...
@@ -35,7 +35,7 @@ class OpRegistry {
template
<
typename
OpType
,
typename
ProtoMakerType
,
typename
GradOpType
>
static
void
RegisterOp
(
const
std
::
string
&
op_type
,
const
std
::
string
&
grad_op_type
)
{
PADDLE_ENFORCE
(
OpInfoMap
().
count
(
op_type
)
==
0
,
PADDLE_ENFORCE
(
!
OpInfoMap
::
Instance
().
Has
(
op_type
)
,
"'%s' is registered more than once."
,
op_type
);
OpInfo
op_info
;
op_info
.
creator_
=
[](
...
...
@@ -59,7 +59,7 @@ class OpRegistry {
op_info
.
proto_
=
nullptr
;
op_info
.
checker_
=
nullptr
;
}
OpInfoMap
().
insert
(
std
::
make_pair
(
op_type
,
op_info
)
);
OpInfoMap
::
Instance
().
Insert
(
op_type
,
op_info
);
// register gradient op
if
(
!
grad_op_type
.
empty
())
{
RegisterOp
<
GradOpType
,
NOPMaker
,
NOP
>
(
grad_op_type
,
""
);
...
...
paddle/framework/operator.cc
浏览文件 @
7f6b5044
...
...
@@ -141,18 +141,10 @@ std::vector<std::string> OperatorBase::OutputVars(bool has_intermediate) const {
}
return
ret_val
;
}
auto
it
=
OpInfoMap
().
find
(
type_
);
PADDLE_ENFORCE
(
it
!=
OpInfoMap
().
end
(),
"Operator %s not registered, cannot figure out intermediate outputs"
,
type_
);
PADDLE_ENFORCE
(
it
->
second
.
proto_
!=
nullptr
,
"Operator %s has no OpProto, cannot figure out intermediate outputs"
,
type_
);
auto
&
info
=
OpInfoMap
::
Instance
().
Get
(
Type
());
// get all OpProto::Var for outputs
for
(
auto
&
o
:
i
t
->
second
.
proto_
->
outputs
())
{
for
(
auto
&
o
:
i
nfo
.
Proto
().
outputs
())
{
// ignore all intermediate output
if
(
o
.
intermediate
())
continue
;
auto
out
=
outputs_
.
find
(
o
.
name
());
...
...
paddle/framework/pybind.cc
浏览文件 @
7f6b5044
...
...
@@ -138,19 +138,16 @@ All parameter, weight, gradient are variables in Paddle.
//! @note: Be careful! PyBind will return std::string as an unicode, not
//! Python str. If you want a str object, you should cast them in Python.
m
.
def
(
"get_all_op_protos"
,
[]()
->
std
::
vector
<
py
::
bytes
>
{
auto
&
op_info_map
=
OpInfoMap
();
std
::
vector
<
py
::
bytes
>
ret_values
;
for
(
auto
it
=
op_info_map
.
begin
();
it
!=
op_info_map
.
end
();
++
it
)
{
const
OpProto
*
proto
=
it
->
second
.
proto_
;
if
(
proto
==
nullptr
)
{
continue
;
}
PADDLE_ENFORCE
(
proto
->
IsInitialized
(),
"OpProto must all be initialized"
);
OpInfoMap
::
Instance
().
IterAllInfo
([
&
ret_values
](
const
std
::
string
&
type
,
const
OpInfo
&
info
)
{
if
(
!
info
.
HasOpProtoAndChecker
())
return
;
std
::
string
str
;
PADDLE_ENFORCE
(
proto
->
SerializeToString
(
&
str
),
PADDLE_ENFORCE
(
info
.
Proto
().
SerializeToString
(
&
str
),
"Serialize OpProto Error. This could be a bug of Paddle."
);
ret_values
.
push_back
(
py
::
bytes
(
str
)
);
}
ret_values
.
emplace_back
(
str
);
}
);
return
ret_values
;
});
m
.
def_submodule
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录