Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
c1feb27f
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看板
提交
c1feb27f
编写于
9月 03, 2017
作者:
Y
Yu Yang
提交者:
GitHub
9月 03, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3831 from reyoung/feature/fix_empty_input_and_output
Make operator Input/Output can return nullptr
上级
11697a5a
13b43279
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
76 addition
and
43 deletion
+76
-43
paddle/framework/backward_test.cc
paddle/framework/backward_test.cc
+3
-3
paddle/framework/op_info.h
paddle/framework/op_info.h
+12
-2
paddle/framework/operator.cc
paddle/framework/operator.cc
+37
-16
paddle/framework/operator.h
paddle/framework/operator.h
+24
-22
未找到文件。
paddle/framework/backward_test.cc
浏览文件 @
c1feb27f
...
...
@@ -127,8 +127,8 @@ class FillZeroOpMaker : public OpProtoAndCheckerMaker {
public:
FillZeroOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"
x
"
,
"x"
);
AddOutput
(
"
ou
t"
,
"out"
);
AddInput
(
"
Src
"
,
"x"
);
AddOutput
(
"
Ds
t"
,
"out"
);
AddComment
(
""
);
}
};
...
...
@@ -138,7 +138,7 @@ class AddOpMaker : public OpProtoAndCheckerMaker {
AddOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"x"
).
AsDuplicable
();
AddOutput
(
"
Y"
,
"y
"
);
AddOutput
(
"
Out"
,
"out
"
);
AddComment
(
""
);
}
};
...
...
paddle/framework/op_info.h
浏览文件 @
c1feb27f
...
...
@@ -80,9 +80,19 @@ class OpInfoMap {
}
const
OpInfo
&
Get
(
const
std
::
string
&
type
)
const
{
auto
op_info_ptr
=
GetNullable
(
type
);
PADDLE_ENFORCE_NOT_NULL
(
op_info_ptr
,
"Operator %s has not been registered"
,
type
);
return
*
op_info_ptr
;
}
const
OpInfo
*
GetNullable
(
const
std
::
string
&
type
)
const
{
auto
it
=
map_
.
find
(
type
);
PADDLE_ENFORCE
(
it
!=
map_
.
end
(),
"Operator %s are not found"
,
type
);
return
it
->
second
;
if
(
it
==
map_
.
end
())
{
return
nullptr
;
}
else
{
return
&
it
->
second
;
}
}
template
<
typename
Callback
>
...
...
paddle/framework/operator.cc
浏览文件 @
c1feb27f
...
...
@@ -33,12 +33,12 @@ ExecutionContext::GetEigenDevice<platform::GPUPlace, Eigen::GpuDevice>() const {
}
#endif
const
std
::
string
&
OperatorBase
::
Input
(
const
std
::
string
&
name
)
const
{
std
::
string
OperatorBase
::
Input
(
const
std
::
string
&
name
)
const
{
auto
&
ins
=
Inputs
(
name
);
PADDLE_ENFORCE_
EQ
(
ins
.
size
(),
1UL
,
PADDLE_ENFORCE_
LE
(
ins
.
size
(),
1UL
,
"Op %s input %s should contain only one variable"
,
type_
,
name
);
return
ins
[
0
];
return
ins
.
empty
()
?
kEmptyVarName
:
ins
[
0
];
}
const
std
::
vector
<
std
::
string
>&
OperatorBase
::
Inputs
(
...
...
@@ -49,12 +49,12 @@ const std::vector<std::string>& OperatorBase::Inputs(
return
it
->
second
;
}
const
std
::
string
&
OperatorBase
::
Output
(
const
std
::
string
&
name
)
const
{
std
::
string
OperatorBase
::
Output
(
const
std
::
string
&
name
)
const
{
auto
&
outs
=
Outputs
(
name
);
PADDLE_ENFORCE_
EQ
(
outs
.
size
(),
1UL
,
PADDLE_ENFORCE_
LE
(
outs
.
size
(),
1UL
,
"Op %s output %s should contain only one variable"
,
type_
,
name
);
return
outs
[
0
];
return
outs
.
empty
()
?
kEmptyVarName
:
outs
[
0
];
}
const
std
::
vector
<
std
::
string
>&
OperatorBase
::
Outputs
(
...
...
@@ -119,16 +119,8 @@ OperatorBase::OperatorBase(const std::string& type,
const
VariableNameMap
&
outputs
,
const
AttributeMap
&
attrs
)
:
type_
(
type
),
inputs_
(
inputs
),
outputs_
(
outputs
),
attrs_
(
attrs
)
{
static
std
::
atomic
<
size_t
>
gUniqId
(
0UL
);
for
(
auto
&
output
:
outputs_
)
{
for
(
auto
&
output_name
:
output
.
second
)
{
if
(
output_name
==
kTempVarName
)
{
output_name
+=
type_
;
output_name
+=
"@"
;
output_name
+=
std
::
to_string
(
gUniqId
.
fetch_add
(
1
));
}
}
}
GenerateTemporaryNames
();
CheckAllInputOutputSet
();
}
std
::
vector
<
std
::
string
>
OperatorBase
::
OutputVars
(
bool
has_intermediate
)
const
{
...
...
@@ -156,6 +148,35 @@ std::vector<std::string> OperatorBase::OutputVars(bool has_intermediate) const {
return
ret_val
;
}
void
OperatorBase
::
CheckAllInputOutputSet
()
const
{
auto
&
info_map
=
OpInfoMap
::
Instance
();
auto
*
op_info
=
info_map
.
GetNullable
(
Type
());
if
(
op_info
==
nullptr
||
op_info
->
proto_
==
nullptr
)
return
;
for
(
auto
&
in
:
op_info
->
Proto
().
inputs
())
{
PADDLE_ENFORCE
(
inputs_
.
find
(
in
.
name
())
!=
inputs_
.
end
(),
"Type %s's input %s is not set"
,
Type
(),
in
.
name
());
}
for
(
auto
&
out
:
op_info
->
Proto
().
outputs
())
{
PADDLE_ENFORCE
(
outputs_
.
find
(
out
.
name
())
!=
outputs_
.
end
(),
"Type %s's output %s is not set"
,
Type
(),
out
.
name
());
}
}
void
OperatorBase
::
GenerateTemporaryNames
()
{
static
std
::
atomic
<
size_t
>
gUniqId
(
0UL
);
for
(
auto
&
output
:
outputs_
)
{
for
(
auto
&
output_name
:
output
.
second
)
{
if
(
output_name
==
kTempVarName
)
{
output_name
+=
type_
;
output_name
+=
"@"
;
output_name
+=
std
::
to_string
(
gUniqId
.
fetch_add
(
1
));
}
}
}
}
void
OpProtoAndCheckerMaker
::
Validate
()
{
validated_
=
true
;
CheckNoDuplicatedInOutAttrs
();
...
...
paddle/framework/operator.h
浏览文件 @
c1feb27f
...
...
@@ -95,12 +95,12 @@ class OperatorBase {
const
VariableNameMap
&
Inputs
()
const
{
return
inputs_
;
}
const
VariableNameMap
&
Outputs
()
const
{
return
outputs_
;
}
//! Get a input with argument's name described in `op_proto`
const
std
::
string
&
Input
(
const
std
::
string
&
name
)
const
;
std
::
string
Input
(
const
std
::
string
&
name
)
const
;
//! Get a input which has multiple variables.
const
std
::
vector
<
std
::
string
>&
Inputs
(
const
std
::
string
&
name
)
const
;
//! Get a output with argument's name described in `op_proto`
const
std
::
string
&
Output
(
const
std
::
string
&
name
)
const
;
std
::
string
Output
(
const
std
::
string
&
name
)
const
;
//! Get an output which has multiple variables.
//! TODO add a vector_view to prevent memory copy.
const
std
::
vector
<
std
::
string
>&
Outputs
(
const
std
::
string
&
name
)
const
;
...
...
@@ -127,6 +127,10 @@ class OperatorBase {
// IG (Inputs Gradients)
VariableNameMap
outputs_
;
AttributeMap
attrs_
;
private:
void
GenerateTemporaryNames
();
void
CheckAllInputOutputSet
()
const
;
};
// Macro for define a clone method.
...
...
@@ -238,11 +242,13 @@ class InferShapeContext {
}
const
Variable
*
InputVar
(
const
std
::
string
&
name
)
const
{
return
scope_
.
FindVar
(
op_
.
Input
(
name
));
auto
ipt
=
op_
.
Input
(
name
);
return
ipt
==
kEmptyVarName
?
nullptr
:
scope_
.
FindVar
(
ipt
);
}
Variable
*
OutputVar
(
const
std
::
string
&
name
)
const
{
return
scope_
.
FindVar
(
op_
.
Output
(
name
));
auto
opt
=
op_
.
Output
(
name
);
return
opt
==
kEmptyVarName
?
nullptr
:
scope_
.
FindVar
(
opt
);
}
const
std
::
vector
<
const
Variable
*>
MultiInputVar
(
...
...
@@ -250,9 +256,11 @@ class InferShapeContext {
auto
names
=
op_
.
Inputs
(
name
);
std
::
vector
<
const
Variable
*>
res
;
res
.
reserve
(
names
.
size
());
std
::
transform
(
names
.
begin
(),
names
.
end
(),
std
::
back_inserter
(
res
),
[
this
](
const
std
::
string
&
name
)
{
return
scope_
.
FindVar
(
name
);
});
std
::
transform
(
names
.
begin
(),
names
.
end
(),
std
::
back_inserter
(
res
),
[
this
](
const
std
::
string
&
name
)
{
return
name
==
kEmptyVarName
?
nullptr
:
scope_
.
FindVar
(
name
);
});
return
res
;
}
...
...
@@ -260,24 +268,24 @@ class InferShapeContext {
auto
names
=
op_
.
Outputs
(
name
);
std
::
vector
<
const
Variable
*>
res
;
res
.
reserve
(
names
.
size
());
std
::
transform
(
names
.
begin
(),
names
.
end
(),
std
::
back_inserter
(
res
),
[
this
](
const
std
::
string
&
name
)
{
return
scope_
.
FindVar
(
name
);
});
std
::
transform
(
names
.
begin
(),
names
.
end
(),
std
::
back_inserter
(
res
),
[
this
](
const
std
::
string
&
name
)
{
return
name
==
kEmptyVarName
?
nullptr
:
scope_
.
FindVar
(
name
);
});
return
res
;
}
template
<
typename
T
>
const
T
*
Input
(
const
std
::
string
&
name
)
const
{
auto
*
var
=
InputVar
(
name
);
PADDLE_ENFORCE_NOT_NULL
(
var
,
"Input(%s) should not be nullptr"
,
name
);
return
&
var
->
Get
<
T
>
();
return
var
==
nullptr
?
nullptr
:
&
var
->
Get
<
T
>
();
}
template
<
typename
T
>
T
*
Output
(
const
std
::
string
&
name
)
const
{
auto
var
=
OutputVar
(
name
);
PADDLE_ENFORCE_NOT_NULL
(
var
,
"Output(%s) should not be nullptr"
,
name
);
return
var
->
GetMutable
<
T
>
();
return
var
==
nullptr
?
nullptr
:
var
->
GetMutable
<
T
>
();
}
template
<
typename
T
>
...
...
@@ -288,10 +296,7 @@ class InferShapeContext {
std
::
transform
(
names
.
begin
(),
names
.
end
(),
std
::
back_inserter
(
res
),
[
&
](
const
std
::
string
&
sub_name
)
{
auto
var
=
scope_
.
FindVar
(
sub_name
);
PADDLE_ENFORCE_NOT_NULL
(
var
,
"MultiInput(%s:%s) should not be nullptr"
,
name
,
sub_name
);
return
&
var
->
Get
<
T
>
();
return
var
==
nullptr
?
nullptr
:
&
var
->
Get
<
T
>
();
});
return
res
;
}
...
...
@@ -304,10 +309,7 @@ class InferShapeContext {
std
::
transform
(
names
.
begin
(),
names
.
end
(),
std
::
back_inserter
(
res
),
[
&
](
const
std
::
string
&
sub_name
)
{
auto
var
=
scope_
.
FindVar
(
sub_name
);
PADDLE_ENFORCE_NOT_NULL
(
var
,
"MultiOutput(%s:%s) should not be nullptr."
,
name
,
sub_name
);
return
var
->
GetMutable
<
T
>
();
return
var
==
nullptr
?
nullptr
:
var
->
GetMutable
<
T
>
();
});
return
res
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录