Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
d64bedf6
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
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看板
提交
d64bedf6
编写于
9月 30, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Stash
上级
c23af80a
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
25 addition
and
354 deletion
+25
-354
paddle/framework/backward.cc
paddle/framework/backward.cc
+3
-0
paddle/framework/backward_test.cc
paddle/framework/backward_test.cc
+10
-21
paddle/framework/grad_op_builder.cc
paddle/framework/grad_op_builder.cc
+0
-97
paddle/framework/grad_op_builder.h
paddle/framework/grad_op_builder.h
+0
-28
paddle/framework/grad_op_builder_test.cc
paddle/framework/grad_op_builder_test.cc
+0
-201
paddle/framework/op_desc.h
paddle/framework/op_desc.h
+12
-0
paddle/framework/op_registry.cc
paddle/framework/op_registry.cc
+0
-5
paddle/framework/op_registry.h
paddle/framework/op_registry.h
+0
-2
未找到文件。
paddle/framework/backward.cc
浏览文件 @
d64bedf6
...
...
@@ -154,6 +154,9 @@ static std::unique_ptr<OperatorBase> BackwardRecursive(
net
->
InsertOp
(
pos
.
first
+
1
,
std
::
move
(
pos
.
second
));
}
}
else
{
OpDescBind
fwd_desc
;
fwd_desc
.
SetInput
(
forwardOp
.
Inputs
());
std
::
unique_ptr
<
OperatorBase
>
grad_op
(
OpRegistry
::
CreateGradOp
(
forwardOp
));
ForEachVarName
(
grad_op
->
Inputs
(),
[
&
no_grad_names
,
&
net
,
&
grad_op
](
...
...
paddle/framework/backward_test.cc
浏览文件 @
d64bedf6
...
...
@@ -159,16 +159,16 @@ REGISTER_OP_WITHOUT_GRADIENT(fc, f::FcOp, f::FcOpMaker);
REGISTER_OP
(
many_output_op
,
f
::
NOP
,
f
::
ManyOutputOpMaker
,
many_output_op_grad
,
f
::
NOP
);
TEST
(
Backward
,
simple_op_grad
)
{
auto
fwd
=
f
::
OpRegistry
::
CreateOp
(
"rowwise_add"
,
{{
"X"
,
{
"x"
}},
{
"b"
,
{
"b"
}}},
{{
"Out"
,
{
"out"
}}},
{});
ASSERT_NE
(
fwd
,
nullptr
);
auto
gop
=
f
::
OpRegistry
::
CreateGradOp
(
*
fwd
);
ASSERT_EQ
(
1UL
,
gop
->
Inputs
().
size
());
ASSERT_EQ
(
"rowwise_add_grad"
,
gop
->
Type
());
ASSERT_EQ
(
f
::
GradVarName
(
"x"
),
gop
->
Output
(
f
::
GradVarName
(
"X"
)));
ASSERT_EQ
(
f
::
GradVarName
(
"b"
),
gop
->
Output
(
f
::
GradVarName
(
"b"
)));
}
//
TEST(Backward, simple_op_grad) {
//
auto fwd = f::OpRegistry::CreateOp(
//
"rowwise_add", {{"X", {"x"}}, {"b", {"b"}}}, {{"Out", {"out"}}}, {});
//
ASSERT_NE(fwd, nullptr);
//
auto gop = f::OpRegistry::CreateGradOp(*fwd);
//
ASSERT_EQ(1UL, gop->Inputs().size());
//
ASSERT_EQ("rowwise_add_grad", gop->Type());
//
ASSERT_EQ(f::GradVarName("x"), gop->Output(f::GradVarName("X")));
//
ASSERT_EQ(f::GradVarName("b"), gop->Output(f::GradVarName("b")));
//
}
TEST
(
Backward
,
simple_op_not_need_grad
)
{
auto
fwd
=
f
::
OpRegistry
::
CreateOp
(
...
...
@@ -286,17 +286,6 @@ TEST(Backward, net_shared_weight) {
ASSERT_EQ
(
"add"
,
bwd_net
->
ops_
[
2
]
->
Type
());
}
TEST
(
Backward
,
op_register_grad_not_for_network
)
{
auto
fwd
=
f
::
OpRegistry
::
CreateOp
(
"fc"
,
{{
"X"
,
{
"x"
}},
{
"W"
,
{
"w"
}},
{
"b"
,
{
"b"
}}},
{{
"mul_result"
,
{
"mul_out"
}},
{
"add_result"
,
{
"add_out"
}},
{
"Out"
,
{
"out1"
}}},
{{
"temporary_index"
,
std
::
vector
<
int
>
{
0
,
1
}}});
ASSERT_THROW
(
f
::
OpRegistry
::
CreateGradOp
(
*
fwd
),
EnforceNotMet
);
}
TEST
(
Backward
,
op_all_input_are_not_need
)
{
auto
fwd
=
f
::
OpRegistry
::
CreateOp
(
"rowwise_add"
,
{{
"X"
,
{
"x"
}},
{
"b"
,
{
"b"
}}},
{{
"Out"
,
{
"out"
}}},
{});
...
...
paddle/framework/grad_op_builder.cc
已删除
100644 → 0
浏览文件 @
c23af80a
/* 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,
WITHOpArgType::OUT WARRANTIES OR CONDITIONS OF ANY KOpArgType::IND, either
express or implied. See the License for the specific language governing
permissions and limitations under the License. */
#include "paddle/framework/grad_op_builder.h"
#include "paddle/framework/op_registry.h"
namespace
paddle
{
namespace
framework
{
enum
class
OpArgType
{
IN
,
OUT
};
static
void
TransOpArg
(
const
OperatorBase
*
src_op
,
const
OpArgType
&
src_type
,
bool
is_grad
,
VariableNameMap
*
vars
)
{
const
auto
&
src_inout
=
src_type
==
OpArgType
::
IN
?
src_op
->
Inputs
()
:
src_op
->
Outputs
();
auto
&
dst_inout
=
*
vars
;
auto
&
proto
=
OpInfoMap
::
Instance
().
Get
(
src_op
->
Type
()).
Proto
();
const
auto
&
src_arg_list
=
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
();
std
::
string
dst_name
=
is_grad
?
GradVarName
(
src_name
)
:
src_name
;
dst_inout
[
dst_name
].
reserve
(
src_inout
.
at
(
src_name
).
size
());
for
(
auto
&
var_name
:
src_inout
.
at
(
src_name
))
{
std
::
string
s
=
is_grad
?
GradVarName
(
var_name
)
:
var_name
;
dst_inout
[
dst_name
].
emplace_back
(
s
);
}
}
}
OperatorBase
*
BuildGradOp
(
const
OperatorBase
*
op
)
{
auto
&
info
=
OpInfoMap
::
Instance
().
Get
(
op
->
Type
());
PADDLE_ENFORCE
(
info
.
HasGradientOp
());
VariableNameMap
inputs
;
VariableNameMap
outputs
;
TransOpArg
(
op
,
OpArgType
::
IN
,
false
,
&
inputs
);
// I
TransOpArg
(
op
,
OpArgType
::
OUT
,
false
,
&
inputs
);
// O
TransOpArg
(
op
,
OpArgType
::
OUT
,
true
,
&
inputs
);
// OG
TransOpArg
(
op
,
OpArgType
::
IN
,
true
,
&
outputs
);
// IG
auto
&
grad_info
=
OpInfoMap
::
Instance
().
Get
(
info
.
grad_op_type_
);
return
grad_info
.
Creator
()(
info
.
grad_op_type_
,
inputs
,
outputs
,
op
->
Attrs
());
}
static
void
TransOpDescArg
(
const
OpDescBind
*
src_op
,
const
OpArgType
&
src_type
,
bool
is_grad
,
OpDescBind
*
dst_op
,
const
OpArgType
&
dst_type
)
{
PADDLE_ENFORCE
(
dst_op
!=
nullptr
,
"Protobuf desc of gradient op must be initialized first."
);
const
auto
&
proto
=
OpInfoMap
::
Instance
().
Get
(
src_op
->
Type
()).
Proto
();
const
auto
&
src_arg_list
=
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
();
std
::
vector
<
std
::
string
>
vars
=
src_type
==
OpArgType
::
IN
?
src_op
->
Input
(
src_name
)
:
src_op
->
Output
(
src_name
);
if
(
is_grad
)
{
for
(
std
::
string
&
var
:
vars
)
{
var
=
GradVarName
(
var
);
}
}
std
::
string
dst_name
=
is_grad
?
GradVarName
(
src_name
)
:
src_name
;
dst_type
==
OpArgType
::
IN
?
dst_op
->
SetInput
(
dst_name
,
vars
)
:
dst_op
->
SetOutput
(
dst_name
,
vars
);
}
}
void
CompleteGradOpDesc
(
const
OpDescBind
*
forw_op
,
OpDescBind
*
grad_op
)
{
auto
&
info
=
OpInfoMap
::
Instance
().
Get
(
forw_op
->
Type
());
PADDLE_ENFORCE
(
info
.
HasGradientOp
());
grad_op
->
SetType
(
info
.
grad_op_type_
);
TransOpDescArg
(
forw_op
,
OpArgType
::
IN
,
false
,
grad_op
,
OpArgType
::
IN
);
TransOpDescArg
(
forw_op
,
OpArgType
::
OUT
,
false
,
grad_op
,
OpArgType
::
IN
);
TransOpDescArg
(
forw_op
,
OpArgType
::
OUT
,
true
,
grad_op
,
OpArgType
::
IN
);
TransOpDescArg
(
forw_op
,
OpArgType
::
IN
,
true
,
grad_op
,
OpArgType
::
OUT
);
grad_op
->
SetAttrMap
(
forw_op
->
GetAttrMap
());
}
}
// namespace framework
}
// namespace paddle
paddle/framework/grad_op_builder.h
已删除
100644 → 0
浏览文件 @
c23af80a
/* 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
{
OperatorBase
*
BuildGradOp
(
const
OperatorBase
*
op
);
void
CompleteGradOpDesc
(
const
OpDescBind
*
forw_op
,
OpDescBind
*
grad_op
);
}
// namespace framework
}
// namespace paddle
paddle/framework/grad_op_builder_test.cc
已删除
100644 → 0
浏览文件 @
c23af80a
#include "paddle/framework/grad_op_builder.h"
#include <gtest/gtest.h>
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"
USE_OP
(
add
);
namespace
paddle
{
namespace
framework
{
class
MutiInOutOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
MutiInOutOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"In1"
,
"a single input"
);
AddInput
(
"In2_mult"
,
"a multiple input"
).
AsDuplicable
();
AddInput
(
"In3"
,
"another single input"
);
AddOutput
(
"Out1"
,
"a single output"
);
AddOutput
(
"Out2_mult"
,
"a multiple output"
).
AsDuplicable
();
AddComment
(
"test op with multiple inputs and outputs"
);
}
};
class
IOIgnoredOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
IOIgnoredOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"In1"
,
"a single input"
);
AddInput
(
"In2_mult"
,
"a multiple input"
).
AsDuplicable
().
NotInGradient
();
AddInput
(
"In3_mult"
,
"another multiple input"
).
AsDuplicable
();
AddOutput
(
"Out1_mult"
,
"a multiple output"
).
AsDuplicable
();
AddOutput
(
"Out2"
,
"a single output"
).
NotInGradient
();
AddComment
(
"op with inputs and outputs ignored in gradient calculating"
);
}
};
}
// namespace framework
}
// namespace paddle
namespace
f
=
paddle
::
framework
;
TEST
(
GradOpBuilder
,
AddTwo
)
{
std
::
shared_ptr
<
f
::
OperatorBase
>
add_op
(
f
::
OpRegistry
::
CreateOp
(
"add"
,
{{
"X"
,
{
"x"
}},
{
"Y"
,
{
"y"
}}},
{{
"Out"
,
{
"out"
}}},
{}));
std
::
shared_ptr
<
f
::
OperatorBase
>
grad_add_op
=
f
::
OpRegistry
::
CreateGradOp
(
*
add_op
);
EXPECT_EQ
(
grad_add_op
->
Inputs
().
size
(),
4UL
);
EXPECT_EQ
(
grad_add_op
->
Outputs
().
size
(),
2UL
);
EXPECT_EQ
(
grad_add_op
->
Input
(
"X"
),
"x"
);
EXPECT_EQ
(
grad_add_op
->
Input
(
"Y"
),
"y"
);
EXPECT_EQ
(
grad_add_op
->
Input
(
"Out"
),
"out"
);
EXPECT_EQ
(
grad_add_op
->
Input
(
f
::
GradVarName
(
"Out"
)),
f
::
GradVarName
(
"out"
));
EXPECT_EQ
(
grad_add_op
->
Output
(
f
::
GradVarName
(
"X"
)),
f
::
GradVarName
(
"x"
));
EXPECT_EQ
(
grad_add_op
->
Output
(
f
::
GradVarName
(
"Y"
)),
f
::
GradVarName
(
"y"
));
}
REGISTER_OP
(
mult_io
,
f
::
NOP
,
f
::
MutiInOutOpMaker
,
mult_io_grad
,
f
::
NOP
);
REGISTER_OP
(
io_ignored
,
f
::
NOP
,
f
::
IOIgnoredOpMaker
,
io_ignored_grad
,
f
::
NOP
);
TEST
(
GradOpBuilder
,
MutiInOut
)
{
std
::
shared_ptr
<
f
::
OperatorBase
>
test_op
(
f
::
OpRegistry
::
CreateOp
(
"mult_io"
,
{{
"In1"
,
{
"in1"
}},
{
"In2_mult"
,
{
"in2_1"
,
"in2_2"
,
"in2_3"
}},
{
"In3"
,
{
"in3"
}}},
{{
"Out1"
,
{
"out1"
}},
{
"Out2_mult"
,
{
"out2_1"
,
"out2_2"
}}},
{}));
std
::
shared_ptr
<
f
::
OperatorBase
>
grad_test_op
=
f
::
OpRegistry
::
CreateGradOp
(
*
test_op
);
ASSERT_EQ
(
grad_test_op
->
Inputs
().
size
(),
3UL
+
2UL
+
2UL
);
EXPECT_EQ
(
grad_test_op
->
Input
(
"In1"
),
"in1"
);
EXPECT_EQ
(
grad_test_op
->
Inputs
(
"In2_mult"
),
std
::
vector
<
std
::
string
>
({
"in2_1"
,
"in2_2"
,
"in2_3"
}));
EXPECT_EQ
(
grad_test_op
->
Input
(
"In3"
),
"in3"
);
EXPECT_EQ
(
grad_test_op
->
Input
(
"Out1"
),
"out1"
);
EXPECT_EQ
(
grad_test_op
->
Inputs
(
"Out2_mult"
),
std
::
vector
<
std
::
string
>
({
"out2_1"
,
"out2_2"
}));
EXPECT_EQ
(
grad_test_op
->
Input
(
f
::
GradVarName
(
"Out1"
)),
f
::
GradVarName
(
"out1"
));
EXPECT_EQ
(
grad_test_op
->
Inputs
(
f
::
GradVarName
(
"Out2_mult"
)),
std
::
vector
<
std
::
string
>
(
{
f
::
GradVarName
(
"out2_1"
),
f
::
GradVarName
(
"out2_2"
)}));
ASSERT_EQ
(
grad_test_op
->
Outputs
().
size
(),
3UL
);
EXPECT_EQ
(
grad_test_op
->
Output
(
f
::
GradVarName
(
"In1"
)),
f
::
GradVarName
(
"in1"
));
EXPECT_EQ
(
grad_test_op
->
Outputs
(
f
::
GradVarName
(
"In2_mult"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"in2_1"
),
f
::
GradVarName
(
"in2_2"
),
f
::
GradVarName
(
"in2_3"
)}));
EXPECT_EQ
(
grad_test_op
->
Output
(
f
::
GradVarName
(
"In3"
)),
f
::
GradVarName
(
"in3"
));
}
TEST
(
GradOpBuilder
,
IOIgnoredInGradient
)
{
std
::
shared_ptr
<
f
::
OperatorBase
>
test_op
(
f
::
OpRegistry
::
CreateOp
(
"io_ignored"
,
{{
"In1"
,
{
"in1"
}},
{
"In2_mult"
,
{
"in2_1"
,
"in2_2"
}},
{
"In3_mult"
,
{
"in3_1"
,
"in3_2"
}}},
{{
"Out1_mult"
,
{
"out1_1"
,
"out1_2"
}},
{
"Out2"
,
{
"out2"
}}},
{}));
std
::
shared_ptr
<
f
::
OperatorBase
>
grad_test_op
=
f
::
OpRegistry
::
CreateGradOp
(
*
test_op
);
// 'In2' and 'Out2' are ignored in gradient calculating
ASSERT_EQ
(
grad_test_op
->
Inputs
().
size
(),
2UL
+
1UL
+
2UL
);
EXPECT_EQ
(
grad_test_op
->
Input
(
"In1"
),
"in1"
);
EXPECT_EQ
(
grad_test_op
->
Inputs
(
"In3_mult"
),
std
::
vector
<
std
::
string
>
({
"in3_1"
,
"in3_2"
}));
EXPECT_EQ
(
grad_test_op
->
Inputs
(
"Out1_mult"
),
std
::
vector
<
std
::
string
>
({
"out1_1"
,
"out1_2"
}));
EXPECT_EQ
(
grad_test_op
->
Inputs
(
f
::
GradVarName
(
"Out1_mult"
)),
std
::
vector
<
std
::
string
>
(
{
f
::
GradVarName
(
"out1_1"
),
f
::
GradVarName
(
"out1_2"
)}));
EXPECT_EQ
(
grad_test_op
->
Input
(
f
::
GradVarName
(
"Out2"
)),
f
::
GradVarName
(
"out2"
));
ASSERT_EQ
(
grad_test_op
->
Outputs
().
size
(),
3UL
);
EXPECT_EQ
(
grad_test_op
->
Output
(
f
::
GradVarName
(
"In1"
)),
f
::
GradVarName
(
"in1"
));
EXPECT_EQ
(
grad_test_op
->
Outputs
(
f
::
GradVarName
(
"In2_mult"
)),
std
::
vector
<
std
::
string
>
(
{
f
::
GradVarName
(
"in2_1"
),
f
::
GradVarName
(
"in2_2"
)}));
EXPECT_EQ
(
grad_test_op
->
Outputs
(
f
::
GradVarName
(
"In3_mult"
)),
std
::
vector
<
std
::
string
>
(
{
f
::
GradVarName
(
"in3_1"
),
f
::
GradVarName
(
"in3_2"
)}));
}
TEST
(
GradOpDescBuilder
,
MutiInOut
)
{
f
::
OpDescBind
*
forw_op
=
new
f
::
OpDescBind
();
forw_op
->
SetType
(
"mult_io"
);
forw_op
->
SetInput
(
"In1"
,
{
"in1"
});
forw_op
->
SetInput
(
"In2_mult"
,
{
"in2_1"
,
"in2_2"
,
"in2_3"
});
forw_op
->
SetInput
(
"In3"
,
{
"in3"
});
forw_op
->
SetOutput
(
"Out1"
,
{
"out1"
});
forw_op
->
SetOutput
(
"Out2_mult"
,
{
"out2_1"
,
"out2_2"
});
f
::
OpDescBind
*
grad_op
=
new
f
::
OpDescBind
();
f
::
CompleteGradOpDesc
(
forw_op
,
grad_op
);
EXPECT_EQ
(
grad_op
->
Type
(),
"mult_io_grad"
);
ASSERT_EQ
(
grad_op
->
InputNames
().
size
(),
3UL
+
2UL
+
2UL
);
EXPECT_EQ
(
grad_op
->
Input
(
"In1"
),
std
::
vector
<
std
::
string
>
({
"in1"
}));
EXPECT_EQ
(
grad_op
->
Input
(
"In2_mult"
),
std
::
vector
<
std
::
string
>
({
"in2_1"
,
"in2_2"
,
"in2_3"
}));
EXPECT_EQ
(
grad_op
->
Input
(
"In3"
),
std
::
vector
<
std
::
string
>
({
"in3"
}));
EXPECT_EQ
(
grad_op
->
Input
(
"Out1"
),
std
::
vector
<
std
::
string
>
({
"out1"
}));
EXPECT_EQ
(
grad_op
->
Input
(
"Out2_mult"
),
std
::
vector
<
std
::
string
>
({
"out2_1"
,
"out2_2"
}));
EXPECT_EQ
(
grad_op
->
Input
(
f
::
GradVarName
(
"Out1"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"out1"
)}));
EXPECT_EQ
(
grad_op
->
Input
(
f
::
GradVarName
(
"Out2_mult"
)),
std
::
vector
<
std
::
string
>
(
{
f
::
GradVarName
(
"out2_1"
),
f
::
GradVarName
(
"out2_2"
)}));
ASSERT_EQ
(
grad_op
->
OutputNames
().
size
(),
3UL
);
EXPECT_EQ
(
grad_op
->
Output
(
f
::
GradVarName
(
"In1"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"in1"
)}));
EXPECT_EQ
(
grad_op
->
Output
(
f
::
GradVarName
(
"In2_mult"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"in2_1"
),
f
::
GradVarName
(
"in2_2"
),
f
::
GradVarName
(
"in2_3"
)}));
EXPECT_EQ
(
grad_op
->
Output
(
f
::
GradVarName
(
"In3"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"in3"
)}));
delete
forw_op
;
delete
grad_op
;
}
TEST
(
GradOpDescBuilder
,
IOIgnoredInGradient
)
{
f
::
OpDescBind
*
forw_op
=
new
f
::
OpDescBind
();
forw_op
->
SetType
(
"io_ignored"
);
forw_op
->
SetInput
(
"In1"
,
{
"in1"
});
forw_op
->
SetInput
(
"In2_mult"
,
{
"in2_1"
,
"in2_2"
});
forw_op
->
SetInput
(
"In3_mult"
,
{
"in3_1"
,
"in3_2"
});
forw_op
->
SetOutput
(
"Out1_mult"
,
{
"out1_1"
,
"out1_2"
});
forw_op
->
SetOutput
(
"Out2"
,
{
"out2"
});
f
::
OpDescBind
*
grad_op
=
new
f
::
OpDescBind
();
f
::
CompleteGradOpDesc
(
forw_op
,
grad_op
);
EXPECT_EQ
(
grad_op
->
Type
(),
"io_ignored_grad"
);
// 'In2' and 'Out2' are ignored in gradient calculating
ASSERT_EQ
(
grad_op
->
InputNames
().
size
(),
2UL
+
1UL
+
2UL
);
EXPECT_EQ
(
grad_op
->
Input
(
"In1"
),
std
::
vector
<
std
::
string
>
({
"in1"
}));
EXPECT_EQ
(
grad_op
->
Input
(
"In3_mult"
),
std
::
vector
<
std
::
string
>
({
"in3_1"
,
"in3_2"
}));
EXPECT_EQ
(
grad_op
->
Input
(
"Out1_mult"
),
std
::
vector
<
std
::
string
>
({
"out1_1"
,
"out1_2"
}));
EXPECT_EQ
(
grad_op
->
Input
(
f
::
GradVarName
(
"Out1_mult"
)),
std
::
vector
<
std
::
string
>
(
{
f
::
GradVarName
(
"out1_1"
),
f
::
GradVarName
(
"out1_2"
)}));
EXPECT_EQ
(
grad_op
->
Input
(
f
::
GradVarName
(
"Out2"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"out2"
)}));
ASSERT_EQ
(
grad_op
->
OutputNames
().
size
(),
3UL
);
EXPECT_EQ
(
grad_op
->
Output
(
f
::
GradVarName
(
"In1"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"in1"
)}));
EXPECT_EQ
(
grad_op
->
Output
(
f
::
GradVarName
(
"In2_mult"
)),
std
::
vector
<
std
::
string
>
(
{
f
::
GradVarName
(
"in2_1"
),
f
::
GradVarName
(
"in2_2"
)}));
EXPECT_EQ
(
grad_op
->
Output
(
f
::
GradVarName
(
"In3_mult"
)),
std
::
vector
<
std
::
string
>
(
{
f
::
GradVarName
(
"in3_1"
),
f
::
GradVarName
(
"in3_2"
)}));
delete
forw_op
;
delete
grad_op
;
}
\ No newline at end of file
paddle/framework/op_desc.h
浏览文件 @
d64bedf6
...
...
@@ -74,6 +74,18 @@ class OpDescBind {
return
MapKeys
(
outputs_
);
}
void
SetInput
(
const
std
::
unordered_map
<
std
::
string
,
std
::
vector
<
std
::
string
>>
&
input
)
{
this
->
inputs_
=
input
;
this
->
need_update_
=
true
;
}
void
SetOutput
(
const
std
::
unordered_map
<
std
::
string
,
std
::
vector
<
std
::
string
>>
&
output
)
{
this
->
outputs_
=
output
;
this
->
need_update_
=
true
;
}
private:
template
<
typename
MapType
>
static
std
::
vector
<
typename
MapType
::
key_type
>
MapKeys
(
const
MapType
&
map
)
{
...
...
paddle/framework/op_registry.cc
浏览文件 @
d64bedf6
...
...
@@ -52,10 +52,5 @@ std::unique_ptr<OperatorBase> OpRegistry::CreateOp(const OpDesc& op_desc) {
return
CreateOp
(
op_desc
.
type
(),
inputs
,
outputs
,
attrs
);
}
std
::
unique_ptr
<
OperatorBase
>
OpRegistry
::
CreateGradOp
(
const
OperatorBase
&
op
)
{
PADDLE_ENFORCE
(
!
op
.
IsNetOp
(),
"Use framework::Backward to get backward ops"
);
return
std
::
unique_ptr
<
OperatorBase
>
(
BuildGradOp
(
&
op
));
}
}
// namespace framework
}
// namespace paddle
paddle/framework/op_registry.h
浏览文件 @
d64bedf6
...
...
@@ -79,8 +79,6 @@ class OpRegistry {
AttributeMap
attrs
);
static
std
::
unique_ptr
<
OperatorBase
>
CreateOp
(
const
OpDesc
&
op_desc
);
static
std
::
unique_ptr
<
OperatorBase
>
CreateGradOp
(
const
OperatorBase
&
op
);
};
template
<
typename
OpType
,
typename
ProtoMakerType
,
typename
GradOpType
>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录