Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
夏有凉风,冬有雪
Paddle
提交
e3a642e0
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,发现更多精彩内容 >>
提交
e3a642e0
编写于
9月 30, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Extract BaseClass of grad_op_desc_maker and add some common method
上级
d9e3c4ff
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
139 addition
and
12 deletion
+139
-12
paddle/framework/details/op_registry.h
paddle/framework/details/op_registry.h
+5
-1
paddle/framework/grad_op_desc_maker.h
paddle/framework/grad_op_desc_maker.h
+115
-0
paddle/framework/op_desc.h
paddle/framework/op_desc.h
+18
-4
paddle/framework/op_info.h
paddle/framework/op_info.h
+1
-7
未找到文件。
paddle/framework/details/op_registry.h
浏览文件 @
e3a642e0
...
...
@@ -14,6 +14,7 @@
#pragma once
#include "paddle/framework/grad_op_desc_maker.h"
#include "paddle/framework/op_info.h"
#include "paddle/framework/op_proto_maker.h"
#include "paddle/framework/operator.h"
...
...
@@ -96,7 +97,10 @@ struct OpInfoFiller<T, kOpProtoAndCheckerMaker> {
template
<
typename
T
>
struct
OpInfoFiller
<
T
,
kGradOpDescMaker
>
{
void
operator
()(
const
char
*
op_type
,
OpInfo
*
info
)
const
{
info
->
grad_op_maker_
=
new
T
();
info
->
grad_op_maker_
=
[](
const
OpDescBind
&
fwd_op
)
{
T
maker
(
fwd_op
);
return
maker
();
};
}
};
}
// namespace details
...
...
paddle/framework/grad_op_desc_maker.h
0 → 100644
浏览文件 @
e3a642e0
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/framework/op_desc.h"
#include "paddle/framework/operator.h"
namespace
paddle
{
namespace
framework
{
class
GradOpDescMakerBase
{
public:
explicit
GradOpDescMakerBase
(
const
OpDescBind
&
fwd_op
)
:
fwd_op_
(
fwd_op
)
{}
virtual
~
GradOpDescMakerBase
()
=
default
;
virtual
std
::
vector
<
OpDescBind
>
operator
()()
const
=
0
;
protected:
static
std
::
vector
<
std
::
string
>
ToGradNames
(
const
std
::
vector
<
std
::
string
>&
var_names
)
{
std
::
vector
<
std
::
string
>
ret_val
;
ret_val
.
reserve
(
var_names
.
size
());
std
::
transform
(
var_names
.
begin
(),
var_names
.
end
(),
std
::
back_inserter
(
ret_val
),
GradVarName
);
return
ret_val
;
}
std
::
vector
<
std
::
string
>
InputGrad
(
const
std
::
string
&
name
)
const
{
return
ToGradNames
(
fwd_op_
.
Input
(
name
));
}
std
::
vector
<
std
::
string
>
OutputGrad
(
const
std
::
string
&
name
)
const
{
return
ToGradNames
(
fwd_op_
.
Output
(
name
));
}
std
::
vector
<
std
::
string
>
InputParamNames
()
const
{
return
this
->
fwd_op_
.
InputParamNames
();
}
std
::
vector
<
std
::
string
>
OutputParamNames
()
const
{
return
this
->
fwd_op_
.
OutputParamNames
();
}
std
::
vector
<
std
::
string
>
Input
(
const
std
::
string
&
name
)
const
{
return
fwd_op_
.
Input
(
name
);
}
std
::
vector
<
std
::
string
>
Output
(
const
std
::
string
&
name
)
const
{
return
fwd_op_
.
Output
(
name
);
}
const
std
::
unordered_map
<
std
::
string
,
Attribute
>&
Attrs
()
const
{
return
fwd_op_
.
GetAttrMap
();
}
const
Attribute
&
GetAttr
(
const
std
::
string
&
name
)
const
{
auto
&
map
=
fwd_op_
.
GetAttrMap
();
auto
it
=
map
.
find
(
name
);
PADDLE_ENFORCE
(
it
!=
map
.
end
(),
"Cannot find attribute %s"
,
name
);
return
it
->
second
;
}
std
::
string
ForwardOpType
()
const
{
return
this
->
fwd_op_
.
Type
();
}
private:
const
OpDescBind
&
fwd_op_
;
};
class
SingleGradOpDescMaker
:
public
GradOpDescMakerBase
{
public:
std
::
vector
<
OpDescBind
>
operator
()()
const
{
return
{
this
->
Apply
()};
}
protected:
virtual
OpDescBind
Apply
()
const
=
0
;
};
class
DefaultGradOpDescMaker
:
public
SingleGradOpDescMaker
{
protected:
virtual
OpDescBind
Apply
()
const
{
OpDescBind
grad
;
grad
.
SetType
(
this
->
GradOpType
());
for
(
auto
&
input_param
:
this
->
InputParamNames
())
{
grad
.
SetInput
(
input_param
,
this
->
Input
(
input_param
));
grad
.
SetOutput
(
GradVarName
(
input_param
),
this
->
InputGrad
(
input_param
));
}
for
(
auto
&
output_param
:
this
->
OutputParamNames
())
{
grad
.
SetInput
(
output_param
,
this
->
Output
(
output_param
));
grad
.
SetInput
(
GradVarName
(
output_param
),
this
->
OutputGrad
(
output_param
));
}
grad
.
SetAttrMap
(
this
->
Attrs
());
return
grad
;
}
virtual
std
::
string
GradOpType
()
const
{
return
this
->
ForwardOpType
()
+
"_grad"
;
}
};
}
// namespace framework
}
// namespace paddle
paddle/framework/op_desc.h
浏览文件 @
e3a642e0
...
...
@@ -60,17 +60,31 @@ class OpDescBind {
void
SetBlockAttr
(
const
std
::
string
&
name
,
BlockDescBind
&
block
);
// Only be used in C++
void
SetAttrMap
(
const
std
::
unordered_map
<
std
::
string
,
Attribute
>
&
attr_map
);
Attribute
GetAttr
(
const
std
::
string
&
name
)
const
;
int
GetBlockAttr
(
const
std
::
string
&
name
)
const
;
//
O
nly be used in C++
//
The following methods should o
nly be used in C++
const
std
::
unordered_map
<
std
::
string
,
Attribute
>
&
GetAttrMap
()
const
;
void
SetAttrMap
(
const
std
::
unordered_map
<
std
::
string
,
Attribute
>
&
attr_map
);
std
::
vector
<
std
::
string
>
InputParamNames
()
const
{
return
MapKeys
(
inputs_
);
}
std
::
vector
<
std
::
string
>
OutputParamNames
()
const
{
return
MapKeys
(
outputs_
);
}
private:
template
<
typename
MapType
>
static
std
::
vector
<
typename
MapType
::
key_type
>
MapKeys
(
const
MapType
&
map
)
{
std
::
vector
<
typename
MapType
::
key_type
>
ret_val
;
ret_val
.
reserve
(
map
.
size
());
std
::
transform
(
map
.
begin
(),
map
.
end
(),
ret_val
.
begin
(),
[](
const
typename
MapType
::
value_type
&
pair
)
{
return
pair
.
first
;
});
return
ret_val
;
}
struct
SetAttrDescVisitor
:
public
boost
::
static_visitor
<
void
>
{
explicit
SetAttrDescVisitor
(
OpDesc
::
Attr
*
attr
)
:
attr_
(
attr
)
{}
mutable
OpDesc
::
Attr
*
attr_
;
...
...
paddle/framework/op_info.h
浏览文件 @
e3a642e0
...
...
@@ -29,16 +29,10 @@ using OpCreator = std::function<OperatorBase*(
const
std
::
string
&
/*type*/
,
const
VariableNameMap
&
/*inputs*/
,
const
VariableNameMap
&
/*outputs*/
,
const
AttributeMap
&
/*attrs*/
)
>
;
class
GradOpDescMakerBase
{
public:
virtual
~
GradOpDescMakerBase
()
=
default
;
virtual
std
::
vector
<
OpDescBind
>
operator
()(
const
OpDescBind
&
)
const
=
0
;
};
struct
OpInfo
{
OpCreator
creator_
;
std
::
string
grad_op_type_
;
GradOpDescMakerBase
*
grad_op_maker_
{
nullptr
}
;
std
::
function
<
std
::
vector
<
OpDescBind
>
(
const
OpDescBind
&
)
>
grad_op_maker_
;
OpProto
*
proto_
{
nullptr
};
OpAttrChecker
*
checker_
{
nullptr
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录