Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
0ac79a38
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
694
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0ac79a38
编写于
7月 26, 2017
作者:
D
dongzhihong
浏览文件
操作
浏览文件
下载
差异文件
Merge remote-tracking branch 'reyoung/feature/backward' into feature/backward
上级
fa7cbfde
f77c63b8
变更
14
显示空白变更内容
内联
并排
Showing
14 changed file
with
356 addition
and
55 deletion
+356
-55
paddle/framework/CMakeLists.txt
paddle/framework/CMakeLists.txt
+1
-0
paddle/framework/backward.cc
paddle/framework/backward.cc
+21
-2
paddle/framework/backward_test.cc
paddle/framework/backward_test.cc
+180
-0
paddle/framework/grad_op_builder.cc
paddle/framework/grad_op_builder.cc
+9
-10
paddle/framework/grad_op_builder.h
paddle/framework/grad_op_builder.h
+2
-2
paddle/framework/grad_op_builder_test.cc
paddle/framework/grad_op_builder_test.cc
+1
-1
paddle/framework/op_registry.h
paddle/framework/op_registry.h
+35
-33
paddle/framework/op_registry_test.cc
paddle/framework/op_registry_test.cc
+2
-3
paddle/framework/operator.h
paddle/framework/operator.h
+3
-0
paddle/framework/operator_test.cc
paddle/framework/operator_test.cc
+2
-2
paddle/operators/fc_op.cc
paddle/operators/fc_op.cc
+2
-2
paddle/operators/fill_zeros_like_op.cc
paddle/operators/fill_zeros_like_op.cc
+58
-0
paddle/operators/fill_zeros_like_op.cu
paddle/operators/fill_zeros_like_op.cu
+6
-0
paddle/operators/fill_zeros_like_op.h
paddle/operators/fill_zeros_like_op.h
+34
-0
未找到文件。
paddle/framework/CMakeLists.txt
浏览文件 @
0ac79a38
...
@@ -33,3 +33,4 @@ cc_library(net SRCS net.cc DEPS op_registry)
...
@@ -33,3 +33,4 @@ cc_library(net SRCS net.cc DEPS op_registry)
cc_test
(
net_op_test SRCS net_op_test.cc DEPS net add_op mul_op sigmoid_op softmax_op fc_op
)
cc_test
(
net_op_test SRCS net_op_test.cc DEPS net add_op mul_op sigmoid_op softmax_op fc_op
)
cc_library
(
backward SRCS backward.cc DEPS net
)
cc_library
(
backward SRCS backward.cc DEPS net
)
cc_test
(
backward_test SRCS backward_test.cc DEPS backward
)
paddle/framework/backward.cc
浏览文件 @
0ac79a38
...
@@ -12,8 +12,9 @@
...
@@ -12,8 +12,9 @@
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
limitations under the License. */
limitations under the License. */
#include <paddle/framework/backward.h>
#include "paddle/framework/backward.h"
#include <paddle/framework/net.h>
#include "paddle/framework/net.h"
#include "paddle/framework/op_registry.h"
namespace
paddle
{
namespace
paddle
{
namespace
framework
{
namespace
framework
{
...
@@ -105,6 +106,24 @@ static void DeDuplicate(NetOp* net, std::unordered_se)
...
@@ -105,6 +106,24 @@ static void DeDuplicate(NetOp* net, std::unordered_se)
//! TODO(dzh)
//! TODO(dzh)
}
else
{
}
else
{
//! TODO(fjy)
//! TODO(fjy)
std
::
shared_ptr
<
OperatorBase
>
grad_op
=
OpRegistry
::
CreateGradOp
(
forwardOp
);
for
(
std
::
string
&
grad_input
:
grad_op
->
inputs_
)
{
if
(
no_grad_names
.
count
(
grad_input
))
{
std
::
string
prefix
=
grad_input
.
substr
(
0
,
grad_input
.
size
()
-
OperatorBase
::
GRAD_VAR_SUFFIX
().
size
());
grad_input
=
prefix
+
OperatorBase
::
ZERO_VAR_SUFFIX
();
std
::
vector
<
std
::
string
>
fill_zeros_in
=
{
prefix
};
std
::
vector
<
std
::
string
>
fill_zeros_out
=
{
grad_input
};
net
.
AddOp
(
OpRegistry
::
CreateOp
(
"fill_zeros_like"
,
fill_zeros_in
,
fill_zeros_out
,
AttributeMap
()));
}
}
for
(
std
::
string
&
grad_output
:
grad_op
->
output_
)
{
if
(
no_grad_names
.
count
(
grad_output
))
{
grad_output
=
OperatorBase
::
EMPTY_VAR_NAME
();
}
}
net
.
AddOp
(
grad_op
);
}
}
net
->
CompleteAddOp
();
net
->
CompleteAddOp
();
...
...
paddle/framework/backward_test.cc
0 → 100644
浏览文件 @
0ac79a38
/* 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. */
#include "paddle/framework/backward.h"
#include <gtest/gtest.h>
#include "paddle/framework/net.h"
#include "paddle/framework/op_registry.h"
namespace
paddle
{
namespace
framework
{
class
EmptyOp
:
public
OperatorBase
{
public:
void
InferShape
(
const
std
::
shared_ptr
<
Scope
>
&
scope
)
const
override
{}
void
Run
(
const
std
::
shared_ptr
<
Scope
>
&
scope
,
const
platform
::
DeviceContext
&
dev_ctx
)
const
override
{}
};
class
RowWiseAddOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
RowWiseAddOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input X of Add"
).
IgnoreGradient
();
AddInput
(
"b"
,
"Bias of Add"
).
IgnoreGradient
();
AddOutput
(
"Out"
,
"Out of Add"
).
IgnoreGradient
();
AddComment
(
"Add Op"
);
}
};
class
MulOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
MulOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"A"
,
"A"
);
AddInput
(
"B"
,
"B"
);
AddOutput
(
"Out"
,
"Out"
);
AddComment
(
"Mul"
);
}
};
class
SigmoidOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
SigmoidOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"X"
);
AddOutput
(
"Y"
,
"Y"
);
AddComment
(
"Sigmoid"
);
}
};
class
FcOp
:
public
NetOp
{
public:
void
Init
()
override
{
AddOp
(
OpRegistry
::
CreateOp
(
"mul"
,
{
Input
(
"X"
),
Input
(
"W"
)},
{
Output
(
"before_act"
)},
{}));
auto
b_name
=
Input
(
"b"
);
if
(
b_name
!=
EMPTY_VAR_NAME
())
{
AddOp
(
OpRegistry
::
CreateOp
(
"rowwise_add"
,
{
Output
(
"before_act"
),
b_name
},
{
Output
(
"before_act"
)},
{}));
}
AddOp
(
OpRegistry
::
CreateOp
(
"sigmoid"
,
{
Output
(
"before_act"
)},
{
Output
(
"Out"
)},
{}));
CompleteAddOp
(
false
);
}
};
class
FcOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
FcOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"x"
);
AddInput
(
"W"
,
"w"
);
AddInput
(
"b"
,
"b"
);
AddOutput
(
"before_act"
,
"before act"
).
SetTemporary
();
AddOutput
(
"Out"
,
""
);
AddComment
(
""
);
}
};
class
ManyOutputOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
ManyOutputOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"x"
,
"x"
);
AddOutput
(
"y"
,
"y"
);
AddOutput
(
"z"
,
"z"
);
AddComment
(
""
);
}
};
class
FillZeroOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
FillZeroOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"x"
,
"x"
);
AddOutput
(
"out"
,
"out"
);
AddComment
(
""
);
}
};
}
// namespace framework
}
// namespace paddle
namespace
f
=
paddle
::
framework
;
using
EnforceNotMet
=
paddle
::
platform
::
EnforceNotMet
;
REGISTER_OP
(
rowwise_add
,
f
::
EmptyOp
,
f
::
RowWiseAddOpMaker
);
REGISTER_GRADIENT_OP
(
rowwise_add
,
rowwise_add_grad
,
f
::
EmptyOp
);
REGISTER_OP
(
mul
,
f
::
EmptyOp
,
f
::
MulOpMaker
);
REGISTER_GRADIENT_OP
(
mul
,
mul_grad
,
f
::
EmptyOp
);
REGISTER_OP
(
sigmoid
,
f
::
EmptyOp
,
f
::
SigmoidOpMaker
);
REGISTER_GRADIENT_OP
(
sigmoid
,
sigmoid_grad
,
f
::
EmptyOp
);
REGISTER_OP
(
fc
,
f
::
FcOp
,
f
::
FcOpMaker
);
REGISTER_OP
(
many_output_op
,
f
::
EmptyOp
,
f
::
ManyOutputOpMaker
);
REGISTER_GRADIENT_OP
(
many_output_op
,
many_output_op_grad
,
f
::
EmptyOp
);
REGISTER_OP
(
fill_zeros_like
,
f
::
EmptyOp
,
f
::
FillZeroOpMaker
);
TEST
(
Backward
,
simple_grad
)
{
auto
fwd
=
f
::
OpRegistry
::
CreateOp
(
"rowwise_add"
,
{
"X"
,
"b"
},
{
"Out"
},
{});
ASSERT_NE
(
fwd
,
nullptr
);
auto
gop
=
f
::
OpRegistry
::
CreateGradOp
(
*
fwd
);
ASSERT_EQ
(
"Out"
+
f
::
OperatorBase
::
GRAD_VAR_SUFFIX
(),
gop
->
inputs_
[
0
]);
ASSERT_EQ
(
"rowwise_add_grad"
,
gop
->
type_
);
ASSERT_EQ
(
"X"
+
f
::
OperatorBase
::
GRAD_VAR_SUFFIX
(),
gop
->
outputs_
[
0
]);
ASSERT_EQ
(
"b"
+
f
::
OperatorBase
::
GRAD_VAR_SUFFIX
(),
gop
->
outputs_
[
1
]);
// LOG(INFO) << gop->Output("X" + "@GRAD");
}
TEST
(
Backward
,
not_for_network
)
{
auto
fwd
=
f
::
OpRegistry
::
CreateOp
(
"fc"
,
{
"X"
,
"W"
,
"b"
},
{
"Out"
,
"tmp_out"
},
{{
"temporary_index"
,
std
::
vector
<
int
>
{
1
}}});
ASSERT_THROW
(
f
::
OpRegistry
::
CreateGradOp
(
*
fwd
),
EnforceNotMet
);
}
TEST
(
Backward
,
all_input_are_not_need
)
{
auto
fwd
=
f
::
OpRegistry
::
CreateOp
(
"rowwise_add"
,
{
"X"
,
"b"
},
{
"Out"
},
{});
auto
backward
=
f
::
Backward
(
*
fwd
,
{
"X"
,
"b"
});
ASSERT_TRUE
(
backward
->
IsNetOp
());
auto
net
=
static_cast
<
f
::
NetOp
*>
(
backward
.
get
());
ASSERT_TRUE
(
net
->
ops_
.
empty
());
}
TEST
(
Backward
,
all_output_are_not_need
)
{
auto
fwd
=
f
::
OpRegistry
::
CreateOp
(
"rowwise_add"
,
{
"X"
,
"b"
},
{
"Out"
},
{});
auto
backward
=
f
::
Backward
(
*
fwd
,
{
"Out"
});
ASSERT_TRUE
(
backward
->
IsNetOp
());
auto
net
=
static_cast
<
f
::
NetOp
*>
(
backward
.
get
());
ASSERT_TRUE
(
net
->
ops_
.
empty
());
}
TEST
(
Backward
,
part_of_output_are_not_need
)
{
auto
fwd
=
f
::
OpRegistry
::
CreateOp
(
"many_output_op"
,
{
"X"
},
{
"Y"
,
"Z"
},
{});
auto
backward
=
f
::
Backward
(
*
fwd
,
{
"Z"
});
ASSERT_TRUE
(
backward
->
IsNetOp
());
auto
net
=
static_cast
<
f
::
NetOp
*>
(
backward
.
get
());
ASSERT_EQ
(
net
->
ops_
.
size
(),
2
);
auto
&
fill_zero
=
*
net
->
ops_
[
0
];
ASSERT_EQ
(
"fill_zeros_like"
,
fill_zero
.
type_
);
ASSERT_EQ
(
1
,
fill_zero
.
inputs_
.
size
());
ASSERT_EQ
(
"Z"
,
fill_zero
.
inputs_
[
0
]);
ASSERT_EQ
(
1
,
fill_zero
.
outputs_
.
size
());
ASSERT_EQ
(
"Z@ZERO"
,
fill_zero
.
outputs_
[
0
]);
auto
&
d_many_out
=
*
net
->
ops_
[
1
];
ASSERT_EQ
(
"many_output_op_grad"
,
d_many_out
.
type_
);
ASSERT_EQ
(
1
+
2
+
2
,
d_many_out
.
inputs_
.
size
());
// I/O/OG
ASSERT_EQ
(
"Z@ZERO"
,
d_many_out
.
Input
(
"z@GRAD"
));
}
\ No newline at end of file
paddle/framework/grad_op_builder.cc
浏览文件 @
0ac79a38
...
@@ -20,7 +20,7 @@ namespace framework {
...
@@ -20,7 +20,7 @@ namespace framework {
OperatorBase
*
GradOpBuilder
::
Build
()
{
OperatorBase
*
GradOpBuilder
::
Build
()
{
BuildOpInOutArgList
();
BuildOpInOutArgList
();
std
::
string
grad_op_type
=
OpRegistry
::
grad_ops
().
at
(
op_
->
type_
);
std
::
string
grad_op_type
=
OpRegistry
::
grad_ops
().
at
(
op_
.
type_
);
OperatorBase
*
grad_op
=
OpRegistry
::
op_creators
().
at
(
grad_op_type
)();
OperatorBase
*
grad_op
=
OpRegistry
::
op_creators
().
at
(
grad_op_type
)();
grad_op
->
type_
=
grad_op_type
;
grad_op
->
type_
=
grad_op_type
;
CompleteGradOp
(
grad_op
);
CompleteGradOp
(
grad_op
);
...
@@ -39,15 +39,15 @@ OpInOutArg* GradOpBuilder::BuildArg(const VarProto& var,
...
@@ -39,15 +39,15 @@ OpInOutArg* GradOpBuilder::BuildArg(const VarProto& var,
}
}
void
GradOpBuilder
::
BuildOpInOutArgList
()
{
void
GradOpBuilder
::
BuildOpInOutArgList
()
{
const
OpProto
&
op_proto
=
OpRegistry
::
protos
().
at
(
op_
->
type_
);
const
OpProto
&
op_proto
=
OpRegistry
::
protos
().
at
(
op_
.
type_
);
const
auto
&
var_map
=
*
(
OpRegistry
::
VarIndexMaps
().
at
(
op_
->
type_
));
const
auto
&
var_map
=
*
(
OpRegistry
::
VarIndexMaps
().
at
(
op_
.
type_
));
const
std
::
vector
<
int
>&
in_format
=
const
std
::
vector
<
int
>&
in_format
=
op_
->
attrs_
.
count
(
"input_format"
)
op_
.
attrs_
.
count
(
"input_format"
)
?
op_
->
GetAttr
<
std
::
vector
<
int
>>
(
"input_format"
)
?
op_
.
GetAttr
<
std
::
vector
<
int
>>
(
"input_format"
)
:
std
::
vector
<
int
>
();
:
std
::
vector
<
int
>
();
const
std
::
vector
<
int
>&
out_format
=
const
std
::
vector
<
int
>&
out_format
=
op_
->
attrs_
.
count
(
"output_format"
)
op_
.
attrs_
.
count
(
"output_format"
)
?
op_
->
GetAttr
<
std
::
vector
<
int
>>
(
"output_format"
)
?
op_
.
GetAttr
<
std
::
vector
<
int
>>
(
"output_format"
)
:
std
::
vector
<
int
>
();
:
std
::
vector
<
int
>
();
for
(
const
auto
&
var
:
op_proto
.
inputs
())
{
for
(
const
auto
&
var
:
op_proto
.
inputs
())
{
arg_list_
.
emplace_back
(
arg_list_
.
emplace_back
(
...
@@ -70,8 +70,7 @@ void GradOpBuilder::AddArgIntoGradOp(const OpInOutArg* arg,
...
@@ -70,8 +70,7 @@ void GradOpBuilder::AddArgIntoGradOp(const OpInOutArg* arg,
}
}
(
*
varmap
)[
var_name
]
=
idx
++
;
(
*
varmap
)[
var_name
]
=
idx
++
;
size_t
pre_sz
=
in_out
.
size
();
size_t
pre_sz
=
in_out
.
size
();
auto
base_it
=
auto
base_it
=
arg
->
type_
==
IN
?
op_
.
inputs_
.
begin
()
:
op_
.
outputs_
.
begin
();
arg
->
type_
==
IN
?
op_
->
inputs_
.
begin
()
:
op_
->
outputs_
.
begin
();
std
::
copy
(
base_it
+
arg
->
begin_idx_
,
base_it
+
arg
->
end_idx_
,
std
::
copy
(
base_it
+
arg
->
begin_idx_
,
base_it
+
arg
->
end_idx_
,
std
::
back_inserter
(
in_out
));
std
::
back_inserter
(
in_out
));
if
(
is_grad
)
{
if
(
is_grad
)
{
...
@@ -83,7 +82,7 @@ void GradOpBuilder::AddArgIntoGradOp(const OpInOutArg* arg,
...
@@ -83,7 +82,7 @@ void GradOpBuilder::AddArgIntoGradOp(const OpInOutArg* arg,
}
}
void
GradOpBuilder
::
CompleteGradOp
(
OperatorBase
*
grad_op
)
const
{
void
GradOpBuilder
::
CompleteGradOp
(
OperatorBase
*
grad_op
)
const
{
grad_op
->
attrs_
=
op_
->
attrs_
;
grad_op
->
attrs_
=
op_
.
attrs_
;
grad_op
->
attrs_
.
erase
(
"input_format"
);
grad_op
->
attrs_
.
erase
(
"input_format"
);
grad_op
->
attrs_
.
erase
(
"output_format"
);
grad_op
->
attrs_
.
erase
(
"output_format"
);
VarIndexMap
*
grad_varmap
=
new
VarIndexMap
();
VarIndexMap
*
grad_varmap
=
new
VarIndexMap
();
...
...
paddle/framework/grad_op_builder.h
浏览文件 @
0ac79a38
...
@@ -29,7 +29,7 @@ class GradOpBuilder {
...
@@ -29,7 +29,7 @@ class GradOpBuilder {
using
VarIndexMap
=
std
::
unordered_map
<
std
::
string
,
int
>
;
using
VarIndexMap
=
std
::
unordered_map
<
std
::
string
,
int
>
;
public:
public:
GradOpBuilder
(
const
OperatorBase
*
op
)
:
op_
(
op
)
{}
GradOpBuilder
(
const
OperatorBase
&
op
)
:
op_
(
op
)
{}
OperatorBase
*
Build
();
OperatorBase
*
Build
();
private:
private:
...
@@ -40,7 +40,7 @@ class GradOpBuilder {
...
@@ -40,7 +40,7 @@ class GradOpBuilder {
std
::
vector
<
int
>&
format
,
VarIndexMap
*
varmap
,
int
&
idx
,
std
::
vector
<
int
>&
format
,
VarIndexMap
*
varmap
,
int
&
idx
,
bool
is_grad
)
const
;
bool
is_grad
)
const
;
void
CompleteGradOp
(
OperatorBase
*
grad_op
)
const
;
void
CompleteGradOp
(
OperatorBase
*
grad_op
)
const
;
const
OperatorBase
*
op_
;
const
OperatorBase
&
op_
;
std
::
vector
<
std
::
shared_ptr
<
OpInOutArg
>>
arg_list_
;
std
::
vector
<
std
::
shared_ptr
<
OpInOutArg
>>
arg_list_
;
};
};
...
...
paddle/framework/grad_op_builder_test.cc
浏览文件 @
0ac79a38
...
@@ -11,7 +11,7 @@ namespace framework {
...
@@ -11,7 +11,7 @@ namespace framework {
TEST
(
GradOpBuilder
,
AddTwo
)
{
TEST
(
GradOpBuilder
,
AddTwo
)
{
std
::
shared_ptr
<
OperatorBase
>
add_op
(
std
::
shared_ptr
<
OperatorBase
>
add_op
(
OpRegistry
::
CreateOp
(
"add_two"
,
{
"x"
,
"y"
},
{
"out"
},
{}));
OpRegistry
::
CreateOp
(
"add_two"
,
{
"x"
,
"y"
},
{
"out"
},
{}));
std
::
shared_ptr
<
OperatorBase
>
grad_add_op
=
OpRegistry
::
CreateGradOp
(
add_op
);
std
::
shared_ptr
<
OperatorBase
>
grad_add_op
=
OpRegistry
::
CreateGradOp
(
*
add_op
);
EXPECT_EQ
(
static_cast
<
int
>
(
grad_add_op
->
inputs_
.
size
()),
4
);
EXPECT_EQ
(
static_cast
<
int
>
(
grad_add_op
->
inputs_
.
size
()),
4
);
EXPECT_EQ
(
static_cast
<
int
>
(
grad_add_op
->
outputs_
.
size
()),
2
);
EXPECT_EQ
(
static_cast
<
int
>
(
grad_add_op
->
outputs_
.
size
()),
2
);
EXPECT_EQ
(
grad_add_op
->
Input
(
"X"
),
"x"
);
EXPECT_EQ
(
grad_add_op
->
Input
(
"X"
),
"x"
);
...
...
paddle/framework/op_registry.h
浏览文件 @
0ac79a38
...
@@ -86,43 +86,46 @@ class OpProtoAndCheckerMaker {
...
@@ -86,43 +86,46 @@ class OpProtoAndCheckerMaker {
}
}
protected:
protected:
void
AddInput
(
const
std
::
string
&
name
,
const
std
::
string
&
comment
,
struct
VariableBuilder
{
bool
multiple
=
false
,
bool
ignore_gradient
=
false
)
{
VarProto
*
var_
;
auto
input
=
proto_
->
mutable_inputs
()
->
Add
()
;
std
::
function
<
void
()
>
on_multiple_
;
*
input
->
mutable_name
()
=
name
;
std
::
function
<
void
()
>
on_temporary_
;
*
input
->
mutable_comment
()
=
comment
;
input
->
set_ignore_gradient
(
ignore_gradient
);
VariableBuilder
&
SetMultiple
()
{
input
->
set_multiple
(
multipl
e
);
var_
->
set_multiple
(
tru
e
);
if
(
multiple
)
{
on_multiple_
();
SetHasMultipleInput
()
;
return
*
this
;
}
}
VariableBuilder
&
SetTemporary
()
{
PADDLE_ENFORCE
(
bool
(
on_temporary_
),
"Cannot set temporary"
);
var_
->
set_temporary
(
true
);
on_temporary_
();
return
*
this
;
}
}
void
AddInputs
(
const
std
::
string
&
name
,
const
std
::
string
&
comment
,
VariableBuilder
&
IgnoreGradient
()
{
bool
ignore_gradient
=
false
)
{
var_
->
set_ignore_gradient
(
true
);
AddInput
(
name
,
comment
,
true
,
ignore_gradient
)
;
return
*
this
;
}
}
};
void
AddOutput
(
const
std
::
string
&
name
,
const
std
::
string
&
comment
,
VariableBuilder
AddInput
(
const
std
::
string
&
name
,
bool
temporary
=
false
,
bool
multiple
=
false
,
const
std
::
string
&
comment
)
{
bool
ignore_gradient
=
false
)
{
auto
input
=
proto_
->
mutable_inputs
()
->
Add
();
*
input
->
mutable_name
()
=
name
;
*
input
->
mutable_comment
()
=
comment
;
return
VariableBuilder
{
input
,
[
=
]
{
this
->
SetHasMultipleInput
();
},
nullptr
};
}
VariableBuilder
AddOutput
(
const
std
::
string
&
name
,
const
std
::
string
&
comment
)
{
auto
output
=
proto_
->
mutable_outputs
()
->
Add
();
auto
output
=
proto_
->
mutable_outputs
()
->
Add
();
*
output
->
mutable_name
()
=
name
;
*
output
->
mutable_name
()
=
name
;
*
output
->
mutable_comment
()
=
comment
;
*
output
->
mutable_comment
()
=
comment
;
output
->
set_ignore_gradient
(
ignore_gradient
);
return
VariableBuilder
{
output
,
[
=
]
{
this
->
SetHasMultipleOutput
();
},
output
->
set_multiple
(
multiple
);
[
=
]
{
this
->
SetHasTemporaryOutput
();
}};
if
(
multiple
)
{
SetHasMultipleOutput
();
}
output
->
set_temporary
(
temporary
);
if
(
temporary
)
{
SetHasTemporaryOutput
();
}
}
void
AddOutputs
(
const
std
::
string
&
name
,
const
std
::
string
&
comment
,
bool
temporary
=
false
,
bool
ignore_gradient
=
false
)
{
AddOutput
(
name
,
comment
,
temporary
,
true
,
ignore_gradient
);
}
}
template
<
typename
T
>
template
<
typename
T
>
...
@@ -300,11 +303,10 @@ class OpRegistry {
...
@@ -300,11 +303,10 @@ class OpRegistry {
return
CreateOp
(
op_desc
.
type
(),
inputs
,
outputs
,
attrs
);
return
CreateOp
(
op_desc
.
type
(),
inputs
,
outputs
,
attrs
);
}
}
static
std
::
shared_ptr
<
OperatorBase
>
CreateGradOp
(
static
std
::
shared_ptr
<
OperatorBase
>
CreateGradOp
(
const
OperatorBase
&
op
)
{
std
::
shared_ptr
<
OperatorBase
>
op
)
{
PADDLE_ENFORCE
(
!
op
.
IsNetOp
(),
PADDLE_ENFORCE
(
!
op
->
IsNetOp
(),
"Use framework::Backward to get backward ops"
);
"Use framework::Backward to get backward ops"
);
GradOpBuilder
builder
(
op
.
get
()
);
GradOpBuilder
builder
(
op
);
std
::
shared_ptr
<
OperatorBase
>
grad_op
(
builder
.
Build
());
std
::
shared_ptr
<
OperatorBase
>
grad_op
(
builder
.
Build
());
grad_op
->
Init
();
grad_op
->
Init
();
return
grad_op
;
return
grad_op
;
...
...
paddle/framework/op_registry_test.cc
浏览文件 @
0ac79a38
...
@@ -36,9 +36,8 @@ class MyTestOpProtoAndCheckerMaker : public OpProtoAndCheckerMaker {
...
@@ -36,9 +36,8 @@ class MyTestOpProtoAndCheckerMaker : public OpProtoAndCheckerMaker {
public:
public:
MyTestOpProtoAndCheckerMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
MyTestOpProtoAndCheckerMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInputs
(
"input"
,
"input of cosine op"
);
AddInput
(
"input"
,
"input of cosine op"
).
SetMultiple
();
AddOutput
(
"output"
,
"output of cosine op"
,
AddOutput
(
"output"
,
"output of cosine op"
).
SetTemporary
();
/*temporary*/
true
);
auto
my_checker
=
[](
int
i
)
{
auto
my_checker
=
[](
int
i
)
{
PADDLE_ENFORCE
(
i
%
2
==
0
,
"'test_attr' must be even!"
);
PADDLE_ENFORCE
(
i
%
2
==
0
,
"'test_attr' must be even!"
);
};
};
...
...
paddle/framework/operator.h
浏览文件 @
0ac79a38
...
@@ -67,6 +67,9 @@ class OperatorBase {
...
@@ -67,6 +67,9 @@ class OperatorBase {
/// e.g. Variable "x@GRAD" is the gradient of varibale "x".
/// e.g. Variable "x@GRAD" is the gradient of varibale "x".
static
std
::
string
GRAD_VAR_SUFFIX
()
{
return
"@GRAD"
;
}
static
std
::
string
GRAD_VAR_SUFFIX
()
{
return
"@GRAD"
;
}
/// Variables with this suffix are supposed to be filled up with zeros.
static
std
::
string
ZERO_VAR_SUFFIX
()
{
return
"@ZERO"
;
}
virtual
~
OperatorBase
()
{}
virtual
~
OperatorBase
()
{}
template
<
typename
T
>
template
<
typename
T
>
...
...
paddle/framework/operator_test.cc
浏览文件 @
0ac79a38
...
@@ -137,9 +137,9 @@ class OpKernelTestMultiInputsProtoAndCheckerMaker
...
@@ -137,9 +137,9 @@ class OpKernelTestMultiInputsProtoAndCheckerMaker
OpKernelTestMultiInputsProtoAndCheckerMaker
(
OpProto
*
proto
,
OpKernelTestMultiInputsProtoAndCheckerMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
s
(
"xs"
,
"inputs of test op"
);
AddInput
(
"xs"
,
"inputs of test op"
).
SetMultiple
(
);
AddInput
(
"k"
,
"input of test op"
);
AddInput
(
"k"
,
"input of test op"
);
AddOutput
s
(
"ys"
,
"outputs of test op"
);
AddOutput
(
"ys"
,
"outputs of test op"
).
SetMultiple
(
);
AddAttr
<
float
>
(
"scale"
,
"scale of cosine op"
)
AddAttr
<
float
>
(
"scale"
,
"scale of cosine op"
)
.
SetDefault
(
1.0
)
.
SetDefault
(
1.0
)
.
LargerThan
(
0.0
);
.
LargerThan
(
0.0
);
...
...
paddle/operators/fc_op.cc
浏览文件 @
0ac79a38
...
@@ -50,8 +50,8 @@ public:
...
@@ -50,8 +50,8 @@ public:
AddInput
(
"b"
,
"the bias of fc operator"
);
AddInput
(
"b"
,
"the bias of fc operator"
);
AddOutput
(
"Y"
,
"the output of fc operator"
);
AddOutput
(
"Y"
,
"the output of fc operator"
);
AddOutput
(
AddOutput
(
"before_act"
,
"the before activation output of fc operator"
)
"before_act"
,
"the before activation output of fc operator"
,
true
);
.
SetTemporary
(
);
AddAttr
<
std
::
string
>
(
"activation"
,
"The activation key for fc layer"
)
AddAttr
<
std
::
string
>
(
"activation"
,
"The activation key for fc layer"
)
.
SetDefault
(
"sigmoid"
)
.
SetDefault
(
"sigmoid"
)
.
InEnum
({
"sigmoid"
,
"softmax"
});
.
InEnum
({
"sigmoid"
,
"softmax"
});
...
...
paddle/operators/fill_zeros_like_op.cc
0 → 100644
浏览文件 @
0ac79a38
/* 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. */
#include "paddle/operators/fill_zeros_like_op.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/tensor.h"
namespace
paddle
{
namespace
operators
{
class
FillZerosLike
:
public
framework
::
OperatorWithKernel
{
protected:
void
InferShape
(
const
std
::
vector
<
const
framework
::
Tensor
*>
&
inputs
,
const
std
::
vector
<
framework
::
Tensor
*>
&
outputs
)
const
override
{
PADDLE_ENFORCE
(
inputs
.
size
()
==
1
,
"Input size of FillZerosLike must be one."
);
PADDLE_ENFORCE
(
outputs
.
size
()
==
1
,
"Output size of AddOp must be one."
);
PADDLE_ENFORCE
(
inputs
[
0
]
!=
nullptr
&&
outputs
[
0
]
!=
nullptr
,
"Outputs of FillZerosLike must all be set."
);
outputs
[
0
]
->
Resize
(
inputs
[
0
]
->
dims
());
}
};
class
FillZerosLikeOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
FillZerosLikeOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
framework
::
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"Src"
,
"The input of fill-zeros-like op."
);
AddOutput
(
"Dst"
,
"The varibale will be filled up with zeros."
);
AddComment
(
R"DOC(
Fill up a vriable with zeros.
The output will have the same size with input.
)DOC"
)
}
};
}
// namespace operators
}
// namespace paddle
REGISTER_OP
(
fill_zeros_like
,
paddle
::
operators
::
FillZerosLikeOp
,
paddle
::
operators
::
FillZerosLikeOpMaker
);
EGISTER_OP_CPU_KERNEL
(
fill_zeros_like
,
paddle
::
operators
::
FillZerosLikeKernal
<
paddle
::
platform
::
CPUPlace
,
float
>
);
\ No newline at end of file
paddle/operators/fill_zeros_like_op.cu
0 → 100644
浏览文件 @
0ac79a38
#include "paddle/framework/op_registry.h"
#include "paddle/operators/fill_zeros_like_op.h"
REGISTER_OP_GPU_KERNEL
(
fill_zeros_like
,
paddle
::
operators
::
FillZerosLikeKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
\ No newline at end of file
paddle/operators/fill_zeros_like_op.h
0 → 100644
浏览文件 @
0ac79a38
/* 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 "glog/logging.h"
#include "paddle/framework/eigen.h"
#include "paddle/framework/operator.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
Place
,
typename
T
>
class
FillZerosLikeKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
KernelContext
&
context
)
const
override
{
auto
*
output
=
context
.
Output
(
0
)
->
GetMutable
<
framework
::
Tensor
>
();
output
->
mutable_data
<
T
>
(
context
.
GetPlace
());
framework
::
EigenVector
<
T
>::
Flatten
(
*
output
).
setZero
();
}
};
}
// namespace operators
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录