Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
urhero
Paddle
提交
fddaf0c4
P
Paddle
项目概览
urhero
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fddaf0c4
编写于
10月 02, 2017
作者:
Y
Yu Yang
提交者:
GitHub
10月 02, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4562 from reyoung/feature/simplify_sum_op
Feature/simplify sum op
上级
e4d20110
b3e479da
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
20 addition
and
58 deletion
+20
-58
paddle/framework/grad_op_builder_test.cc
paddle/framework/grad_op_builder_test.cc
+0
-22
paddle/operators/CMakeLists.txt
paddle/operators/CMakeLists.txt
+5
-1
paddle/operators/sum_op.cc
paddle/operators/sum_op.cc
+15
-14
paddle/operators/sum_op.cu
paddle/operators/sum_op.cu
+0
-2
paddle/operators/sum_op.h
paddle/operators/sum_op.h
+0
-19
未找到文件。
paddle/framework/grad_op_builder_test.cc
浏览文件 @
fddaf0c4
...
...
@@ -39,28 +39,6 @@ class IOIgnoredOpMaker : public OpProtoAndCheckerMaker {
namespace
f
=
paddle
::
framework
;
TEST
(
GradOpBuilder
,
AddTwo
)
{
std
::
shared_ptr
<
f
::
OperatorBase
>
add_op
(
f
::
OpRegistry
::
CreateOp
(
"sum"
,
{{
"X"
,
{
"x"
,
"y"
}}},
{{
"Out"
,
{
"out"
}}},
{}));
std
::
shared_ptr
<
f
::
OperatorBase
>
grad_add_op
=
f
::
OpRegistry
::
CreateGradOp
(
*
add_op
);
EXPECT_EQ
(
grad_add_op
->
Inputs
().
size
(),
1UL
);
EXPECT_EQ
(
grad_add_op
->
Outputs
().
size
(),
1UL
);
EXPECT_EQ
(
grad_add_op
->
Input
(
f
::
GradVarName
(
"Out"
)),
f
::
GradVarName
(
"out"
));
auto
&
outputs
=
grad_add_op
->
Outputs
(
f
::
GradVarName
(
"X"
));
EXPECT_EQ
(
2UL
,
outputs
.
size
());
auto
in_output
=
[
&
outputs
](
const
std
::
string
&
name
)
{
for
(
auto
&
output_name
:
outputs
)
{
if
(
output_name
==
name
)
return
true
;
}
return
false
;
};
EXPECT_TRUE
(
in_output
(
f
::
GradVarName
(
"x"
)));
EXPECT_TRUE
(
in_output
(
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
);
...
...
paddle/operators/CMakeLists.txt
浏览文件 @
fddaf0c4
...
...
@@ -103,12 +103,16 @@ set(DEPS_OPS
recurrent_op
cond_op
cross_entropy_op
softmax_with_cross_entropy_op
)
softmax_with_cross_entropy_op
sum_op
)
op_library
(
recurrent_op SRCS recurrent_op.cc rnn/recurrent_op_utils.cc
DEPS framework_proto tensor net_op
)
op_library
(
cond_op SRCS cond_op.cc DEPS framework_proto tensor operator net_op
)
op_library
(
cross_entropy_op DEPS cross_entropy
)
op_library
(
softmax_with_cross_entropy_op DEPS cross_entropy softmax
)
op_library
(
sum_op DEPS net_op
)
list
(
REMOVE_ITEM GENERAL_OPS
${
DEPS_OPS
}
)
foreach
(
src
${
GENERAL_OPS
}
)
...
...
paddle/operators/sum_op.cc
浏览文件 @
fddaf0c4
...
...
@@ -11,6 +11,7 @@ limitations under the License. */
#include "paddle/operators/sum_op.h"
#include <vector>
#include "paddle/operators/net_op.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -57,21 +58,23 @@ or not. But the output only shares the LoD with the first input.
}
};
class
SumGradOp
:
public
framework
::
OperatorWithKernel
{
class
SumGradOp
:
public
NetOp
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
SumGradOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
NetOp
(
type
,
inputs
,
outputs
,
attrs
)
{
auto
&
x_grad_names
=
Outputs
(
framework
::
GradVarName
(
"X"
));
auto
out_grad_name
=
this
->
Input
(
framework
::
GradVarName
(
"Out"
));
protected:
void
InferShape
(
framework
::
InferShapeContextBase
*
ctx
)
const
override
{
auto
out_grad_dims
=
ctx
->
GetInputDim
(
framework
::
GradVarName
(
"Out"
));
auto
x_grad_names
=
ctx
->
Outputs
(
framework
::
GradVarName
(
"X"
));
size_t
x_length
=
x_grad_names
.
size
();
std
::
vector
<
framework
::
DDim
>
x_grad_dims
;
x_grad_dims
.
reserve
(
x_length
);
for
(
size_t
i
=
0
;
i
<
x_length
;
++
i
)
{
x_grad_dims
.
push_back
(
out_grad_dims
);
framework
::
AttributeMap
grad_attrs
;
grad_attrs
[
"scale"
]
=
1.0
f
;
for
(
auto
&
x_grad_name
:
x_grad_names
)
{
AppendOp
(
framework
::
OpRegistry
::
CreateOp
(
"scale"
,
{{
"X"
,
{
out_grad_name
}}},
{{
"Out"
,
{
x_grad_name
}}},
grad_attrs
));
}
ctx
->
SetOutputsDim
(
framework
::
GradVarName
(
"X"
),
x_grad_dims
);
CompleteAddOp
(
false
);
}
};
...
...
@@ -81,5 +84,3 @@ class SumGradOp : public framework::OperatorWithKernel {
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
sum
,
ops
::
SumOp
,
ops
::
SumOpMaker
,
sum_grad
,
ops
::
SumGradOp
);
REGISTER_OP_CPU_KERNEL
(
sum
,
ops
::
SumKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
sum_grad
,
ops
::
SumGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/sum_op.cu
浏览文件 @
fddaf0c4
...
...
@@ -14,5 +14,3 @@ limitations under the License. */
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
sum
,
ops
::
SumKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
sum_grad
,
ops
::
SumGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/sum_op.h
浏览文件 @
fddaf0c4
...
...
@@ -42,24 +42,5 @@ class SumKernel : public framework::OpKernel<T> {
}
};
template
<
typename
Place
,
typename
T
>
class
SumGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
input
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
outs
=
context
.
MultiOutput
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
for
(
auto
out
:
outs
)
{
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
}
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
auto
in
=
EigenVector
<
T
>::
Flatten
(
*
input
);
for
(
auto
out
:
outs
)
{
auto
result
=
EigenVector
<
T
>::
Flatten
(
*
out
);
result
.
device
(
place
)
=
in
;
}
}
};
}
// namespace operators
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录