Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
cc387159
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看板
未验证
提交
cc387159
编写于
12月 16, 2020
作者:
C
ceci3
提交者:
GitHub
12月 16, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add pad and concat double grad (#29549)
* add constant pad double grad
上级
f13c3a9c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
115 addition
and
1 deletion
+115
-1
paddle/fluid/operators/concat_op.cc
paddle/fluid/operators/concat_op.cc
+16
-0
paddle/fluid/operators/pad3d_op.cc
paddle/fluid/operators/pad3d_op.cc
+18
-0
paddle/fluid/operators/pad_op.cc
paddle/fluid/operators/pad_op.cc
+16
-1
python/paddle/fluid/tests/unittests/test_nn_grad.py
python/paddle/fluid/tests/unittests/test_nn_grad.py
+65
-0
未找到文件。
paddle/fluid/operators/concat_op.cc
浏览文件 @
cc387159
...
@@ -201,6 +201,20 @@ class ConcatGradOpMaker : public framework::SingleGradOpMaker<T> {
...
@@ -201,6 +201,20 @@ class ConcatGradOpMaker : public framework::SingleGradOpMaker<T> {
}
}
};
};
template
<
typename
T
>
class
ConcatDoubleGradOpMaker
:
public
framework
::
SingleGradOpMaker
<
T
>
{
public:
using
framework
::
SingleGradOpMaker
<
T
>::
SingleGradOpMaker
;
protected:
void
Apply
(
GradOpPtr
<
T
>
grad_op
)
const
override
{
grad_op
->
SetType
(
"concat"
);
grad_op
->
SetInput
(
"X"
,
this
->
OutputGrad
(
framework
::
GradVarName
(
"X"
)));
grad_op
->
SetOutput
(
"Out"
,
this
->
InputGrad
(
framework
::
GradVarName
(
"Out"
)));
grad_op
->
SetAttrMap
(
this
->
Attrs
());
}
};
}
// namespace operators
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
...
@@ -209,6 +223,8 @@ REGISTER_OPERATOR(concat, ops::ConcatOp, ops::ConcatOpMaker,
...
@@ -209,6 +223,8 @@ REGISTER_OPERATOR(concat, ops::ConcatOp, ops::ConcatOpMaker,
ops
::
ConcatGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
ConcatGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
ConcatGradOpMaker
<
paddle
::
imperative
::
OpBase
>
);
ops
::
ConcatGradOpMaker
<
paddle
::
imperative
::
OpBase
>
);
REGISTER_OPERATOR
(
concat_grad
,
ops
::
ConcatOpGrad
,
REGISTER_OPERATOR
(
concat_grad
,
ops
::
ConcatOpGrad
,
ops
::
ConcatDoubleGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
ConcatDoubleGradOpMaker
<
paddle
::
imperative
::
OpBase
>
,
ops
::
ConcatOpGradNoNeedBufferVarInferer
);
ops
::
ConcatOpGradNoNeedBufferVarInferer
);
REGISTER_OP_CPU_KERNEL
(
REGISTER_OP_CPU_KERNEL
(
concat
,
ops
::
ConcatKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
concat
,
ops
::
ConcatKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
...
...
paddle/fluid/operators/pad3d_op.cc
浏览文件 @
cc387159
...
@@ -893,6 +893,22 @@ class Pad3dOpGradMaker : public framework::SingleGradOpMaker<T> {
...
@@ -893,6 +893,22 @@ class Pad3dOpGradMaker : public framework::SingleGradOpMaker<T> {
}
}
};
};
template
<
typename
T
>
class
Pad3dOpDoubleGradMaker
:
public
framework
::
SingleGradOpMaker
<
T
>
{
public:
using
framework
::
SingleGradOpMaker
<
T
>::
SingleGradOpMaker
;
void
Apply
(
GradOpPtr
<
T
>
grad_op
)
const
override
{
if
(
this
->
HasInput
(
"Paddings"
))
{
grad_op
->
SetInput
(
"Paddings"
,
this
->
Input
(
"Paddings"
));
}
grad_op
->
SetType
(
"pad3d"
);
grad_op
->
SetInput
(
"X"
,
this
->
OutputGrad
(
framework
::
GradVarName
(
"X"
)));
grad_op
->
SetOutput
(
"Out"
,
this
->
InputGrad
(
framework
::
GradVarName
(
"Out"
)));
grad_op
->
SetAttrMap
(
this
->
Attrs
());
}
};
DECLARE_NO_NEED_BUFFER_VARS_INFERER
(
Pad3dOpGradNoNeedBufferVarsInferer
,
"X"
);
DECLARE_NO_NEED_BUFFER_VARS_INFERER
(
Pad3dOpGradNoNeedBufferVarsInferer
,
"X"
);
}
// namespace operators
}
// namespace operators
...
@@ -904,6 +920,8 @@ REGISTER_OPERATOR(pad3d, ops::Pad3dOp, ops::Pad3dOpMaker,
...
@@ -904,6 +920,8 @@ REGISTER_OPERATOR(pad3d, ops::Pad3dOp, ops::Pad3dOpMaker,
ops
::
Pad3dOpGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
Pad3dOpGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
Pad3dOpGradMaker
<
paddle
::
imperative
::
OpBase
>
);
ops
::
Pad3dOpGradMaker
<
paddle
::
imperative
::
OpBase
>
);
REGISTER_OPERATOR
(
pad3d_grad
,
ops
::
Pad3dOpGrad
,
REGISTER_OPERATOR
(
pad3d_grad
,
ops
::
Pad3dOpGrad
,
ops
::
Pad3dOpDoubleGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
Pad3dOpDoubleGradMaker
<
paddle
::
imperative
::
OpBase
>
,
ops
::
Pad3dOpGradNoNeedBufferVarsInferer
);
ops
::
Pad3dOpGradNoNeedBufferVarsInferer
);
REGISTER_OP_CPU_KERNEL
(
pad3d
,
ops
::
Pad3dCPUKernel
<
float
>
,
REGISTER_OP_CPU_KERNEL
(
pad3d
,
ops
::
Pad3dCPUKernel
<
float
>
,
ops
::
Pad3dCPUKernel
<
double
>
,
ops
::
Pad3dCPUKernel
<
int
>
,
ops
::
Pad3dCPUKernel
<
double
>
,
ops
::
Pad3dCPUKernel
<
int
>
,
...
...
paddle/fluid/operators/pad_op.cc
浏览文件 @
cc387159
...
@@ -142,6 +142,19 @@ class PadOpGradMaker : public framework::SingleGradOpMaker<T> {
...
@@ -142,6 +142,19 @@ class PadOpGradMaker : public framework::SingleGradOpMaker<T> {
}
}
};
};
template
<
typename
T
>
class
PadOpDoubleGradMaker
:
public
framework
::
SingleGradOpMaker
<
T
>
{
public:
using
framework
::
SingleGradOpMaker
<
T
>::
SingleGradOpMaker
;
void
Apply
(
GradOpPtr
<
T
>
grad_op
)
const
override
{
grad_op
->
SetType
(
"pad"
);
grad_op
->
SetInput
(
"X"
,
this
->
OutputGrad
(
framework
::
GradVarName
(
"X"
)));
grad_op
->
SetOutput
(
"Out"
,
this
->
InputGrad
(
framework
::
GradVarName
(
"Out"
)));
grad_op
->
SetAttrMap
(
this
->
Attrs
());
}
};
}
// namespace operators
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
...
@@ -150,7 +163,9 @@ namespace ops = paddle::operators;
...
@@ -150,7 +163,9 @@ namespace ops = paddle::operators;
REGISTER_OPERATOR
(
pad
,
ops
::
PadOp
,
ops
::
PadOpMaker
,
REGISTER_OPERATOR
(
pad
,
ops
::
PadOp
,
ops
::
PadOpMaker
,
ops
::
PadOpGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
PadOpGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
PadOpGradMaker
<
paddle
::
imperative
::
OpBase
>
);
ops
::
PadOpGradMaker
<
paddle
::
imperative
::
OpBase
>
);
REGISTER_OPERATOR
(
pad_grad
,
ops
::
PadOpGrad
);
REGISTER_OPERATOR
(
pad_grad
,
ops
::
PadOpGrad
,
ops
::
PadOpDoubleGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
PadOpDoubleGradMaker
<
paddle
::
imperative
::
OpBase
>
);
REGISTER_OP_CPU_KERNEL
(
REGISTER_OP_CPU_KERNEL
(
pad
,
ops
::
PadKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
pad
,
ops
::
PadKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
PadKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
ops
::
PadKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
...
...
python/paddle/fluid/tests/unittests/test_nn_grad.py
浏览文件 @
cc387159
...
@@ -394,5 +394,70 @@ class TestTransposeDoubleGradCheckCase1(unittest.TestCase):
...
@@ -394,5 +394,70 @@ class TestTransposeDoubleGradCheckCase1(unittest.TestCase):
self
.
func
(
p
)
self
.
func
(
p
)
class
TestConstantPadDoubleGradCheck
(
unittest
.
TestCase
):
@
prog_scope
()
def
func
(
self
,
place
):
x_shape
=
[
2
,
3
,
4
,
5
]
pad
=
[
1
,
1
,
1
,
1
]
eps
=
0.005
dtype
=
np
.
float64
x
=
layers
.
data
(
'x'
,
x_shape
,
False
,
dtype
)
x
.
persistable
=
True
out
=
paddle
.
nn
.
functional
.
pad
(
x
,
pad
)
x_arr
=
np
.
random
.
uniform
(
-
1
,
1
,
x_shape
).
astype
(
dtype
)
gradient_checker
.
double_grad_check
(
[
x
],
out
,
x_init
=
x_arr
,
place
=
place
,
eps
=
eps
)
def
test_grad
(
self
):
places
=
[
fluid
.
CPUPlace
()]
if
core
.
is_compiled_with_cuda
():
places
.
append
(
fluid
.
CUDAPlace
(
0
))
for
p
in
places
:
self
.
func
(
p
)
class
TestConstantPadDoubleGradCheckCase1
(
TestConstantPadDoubleGradCheck
):
@
prog_scope
()
def
func
(
self
,
place
):
x_shape
=
[
2
,
3
,
4
,
5
]
pad
=
[
1
,
0
,
1
,
0
,
1
,
0
,
1
,
0
]
dtype
=
np
.
float64
x
=
layers
.
data
(
'x'
,
x_shape
,
False
,
dtype
)
x
.
persistable
=
True
out
=
paddle
.
nn
.
functional
.
pad
(
x
,
pad
)
x_arr
=
np
.
random
.
uniform
(
-
1
,
1
,
x_shape
).
astype
(
dtype
)
gradient_checker
.
double_grad_check
([
x
],
out
,
x_init
=
x_arr
,
place
=
place
)
class
TestConcatDoubleGradCheck
(
unittest
.
TestCase
):
@
prog_scope
()
def
func
(
self
,
place
):
x_shape
=
[
2
,
3
,
4
,
5
]
pad
=
[
1
,
1
,
1
,
1
]
dtype
=
np
.
float64
x1
=
layers
.
data
(
'x'
,
x_shape
,
False
,
dtype
)
x2
=
layers
.
data
(
'x'
,
x_shape
,
False
,
dtype
)
x1
.
persistable
=
True
x2
.
persistable
=
True
out
=
paddle
.
concat
([
x1
,
x2
],
axis
=
0
)
x2_arr
=
np
.
random
.
uniform
(
-
1
,
1
,
x_shape
).
astype
(
dtype
)
x1_arr
=
np
.
random
.
uniform
(
-
1
,
1
,
x_shape
).
astype
(
dtype
)
gradient_checker
.
double_grad_check
(
[
x1
,
x2
],
out
,
x_init
=
[
x1_arr
,
x2_arr
],
place
=
place
)
def
test_grad
(
self
):
places
=
[
fluid
.
CPUPlace
()]
if
core
.
is_compiled_with_cuda
():
places
.
append
(
fluid
.
CUDAPlace
(
0
))
for
p
in
places
:
self
.
func
(
p
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录