Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
977e9fcb
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看板
未验证
提交
977e9fcb
编写于
5月 18, 2019
作者:
L
lvmengsi
提交者:
GitHub
5月 18, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support elementwise_sub double backward (#17476)
add elementwise_sub_grad_grad op for backward of backward calculation
上级
75cda4d9
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
145 addition
and
5 deletion
+145
-5
paddle/fluid/operators/elementwise/elementwise_sub_op.cc
paddle/fluid/operators/elementwise/elementwise_sub_op.cc
+49
-1
paddle/fluid/operators/elementwise/elementwise_sub_op.cu
paddle/fluid/operators/elementwise/elementwise_sub_op.cu
+10
-0
paddle/fluid/operators/elementwise/elementwise_sub_op.h
paddle/fluid/operators/elementwise/elementwise_sub_op.h
+28
-0
python/paddle/fluid/tests/unittests/test_nn_grad.py
python/paddle/fluid/tests/unittests/test_nn_grad.py
+58
-4
未找到文件。
paddle/fluid/operators/elementwise/elementwise_sub_op.cc
浏览文件 @
977e9fcb
...
...
@@ -13,10 +13,48 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/operators/elementwise/elementwise_sub_op.h"
#include <memory>
#include <string>
#include "paddle/fluid/operators/elementwise/elementwise_op.h"
namespace
paddle
{
namespace
operators
{
class
ElementwiseSubDoubleGradDescMaker
:
public
framework
::
SingleGradOpDescMaker
{
public:
using
framework
::
SingleGradOpDescMaker
::
SingleGradOpDescMaker
;
protected:
std
::
unique_ptr
<
framework
::
OpDesc
>
Apply
()
const
override
{
std
::
unique_ptr
<
framework
::
OpDesc
>
op
(
new
framework
::
OpDesc
());
op
->
SetType
(
"elementwise_sub_grad_grad"
);
op
->
SetInput
(
"Y"
,
Input
(
"Y"
));
op
->
SetInput
(
"DOut"
,
Input
(
framework
::
GradVarName
(
"Out"
)));
op
->
SetInput
(
"DDX"
,
OutputGrad
(
framework
::
GradVarName
(
"X"
)));
op
->
SetInput
(
"DDY"
,
OutputGrad
(
framework
::
GradVarName
(
"Y"
)));
op
->
SetAttrMap
(
Attrs
());
op
->
SetOutput
(
"DDOut"
,
InputGrad
(
framework
::
GradVarName
(
"Out"
)));
return
op
;
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_ELEMWISE_GRAD_MAKER
(
elementwise_sub
,
Sub
);
REGISTER_ELEMWISE_EXPLICIT_OP
(
elementwise_sub
,
"Sub"
,
"Out = X - Y"
);
REGISTER_ELEMWISE_EXPLICIT_OP_WITHOUT_GRAD
(
elementwise_sub
,
"Sub"
,
"Out = X - Y"
);
REGISTER_OPERATOR
(
elementwise_sub_grad
,
ops
::
ElementwiseOpExplicitGrad
,
ops
::
ElementwiseGradOpInplace
,
ops
::
ElementwiseGradNoBufVarsInference
,
ops
::
ElementwiseSubDoubleGradDescMaker
);
REGISTER_OPERATOR
(
elementwise_sub_grad_grad
,
ops
::
ElementwiseOpDoubleGradWithoutDXDY
);
REGISTER_OP_CPU_KERNEL
(
elementwise_sub
,
...
...
@@ -30,3 +68,13 @@ REGISTER_OP_CPU_KERNEL(
ops
::
ElementwiseSubGradKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
ops
::
ElementwiseSubGradKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
,
ops
::
ElementwiseSubGradKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
);
REGISTER_OP_CPU_KERNEL
(
elementwise_sub_grad_grad
,
ops
::
ElementwiseSubDoubleGradKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
ElementwiseSubDoubleGradKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
ops
::
ElementwiseSubDoubleGradKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
,
ops
::
ElementwiseSubDoubleGradKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
);
paddle/fluid/operators/elementwise/elementwise_sub_op.cu
浏览文件 @
977e9fcb
...
...
@@ -33,3 +33,13 @@ REGISTER_OP_CUDA_KERNEL(
ops
::
ElementwiseSubGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int
>
,
ops
::
ElementwiseSubGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int64_t
>
);
REGISTER_OP_CUDA_KERNEL
(
elementwise_sub_grad_grad
,
ops
::
ElementwiseSubDoubleGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
ElementwiseSubDoubleGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
,
ops
::
ElementwiseSubDoubleGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int
>
,
ops
::
ElementwiseSubDoubleGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int64_t
>
);
paddle/fluid/operators/elementwise/elementwise_sub_op.h
浏览文件 @
977e9fcb
...
...
@@ -68,5 +68,33 @@ class ElementwiseSubGradKernel : public ElemwiseGradKernel<T> {
ctx
,
*
x
,
*
y
,
*
out
,
*
dout
,
axis
,
dx
,
dy
,
SubGradDX
<
T
>
(),
SubGradDY
<
T
>
());
}
};
template
<
typename
DeviceContext
,
typename
T
>
class
ElementwiseSubDoubleGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
using
Tensor
=
framework
::
Tensor
;
auto
*
y
=
ctx
.
Input
<
Tensor
>
(
"Y"
);
auto
*
dout
=
ctx
.
Input
<
Tensor
>
(
"DOut"
);
auto
*
ddx
=
ctx
.
Input
<
Tensor
>
(
"DDX"
);
auto
*
ddy
=
ctx
.
Input
<
Tensor
>
(
"DDY"
);
auto
*
ddout
=
ctx
.
Output
<
Tensor
>
(
"DDOut"
);
// DDOut = ddx - ddy
if
(
ddout
)
{
Tensor
ddx_safe
,
ddy_safe
;
GetDoubleGradSafeTensor
<
DeviceContext
,
T
>
(
ctx
,
dout
,
ddx
,
&
ddx_safe
);
GetDoubleGradSafeTensor
<
DeviceContext
,
T
>
(
ctx
,
y
,
ddy
,
&
ddy_safe
);
ddout
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
int
axis
=
ctx
.
Attr
<
int
>
(
"axis"
);
ElementwiseComputeEx
<
SubFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
ddx_safe
,
&
ddy_safe
,
axis
,
SubFunctor
<
T
>
(),
ddout
);
}
}
};
}
// namespace operators
}
// namespace paddle
python/paddle/fluid/tests/unittests/test_nn_grad.py
浏览文件 @
977e9fcb
...
...
@@ -169,7 +169,7 @@ class TestElementwiseMulDoubleGradCheck(unittest.TestCase):
@
prog_scope
()
def
func
(
self
,
place
):
# the shape of input variable shoule be clearly specified, not inlcude -1.
shape
=
[
7
,
9
]
shape
=
[
2
,
3
,
5
,
7
]
eps
=
0.005
dtype
=
np
.
float64
...
...
@@ -219,7 +219,7 @@ class TestElementwiseMulBroadcastDoubleGradCheck(unittest.TestCase):
@
prog_scope
()
def
func
(
self
,
place
):
# the shape of input variable shoule be clearly specified, not inlcude -1.
shape
=
[
7
,
9
]
shape
=
[
2
,
3
,
5
,
7
]
eps
=
0.005
dtype
=
np
.
float64
...
...
@@ -246,7 +246,7 @@ class TestElementwiseAddDoubleGradCheck(unittest.TestCase):
@
prog_scope
()
def
func
(
self
,
place
):
# the shape of input variable shoule be clearly specified, not inlcude -1.
shape
=
[
7
,
9
]
shape
=
[
2
,
3
,
5
,
7
]
eps
=
0.005
dtype
=
np
.
float64
...
...
@@ -273,7 +273,7 @@ class TestElementwiseAddBroadcastDoubleGradCheck(unittest.TestCase):
@
prog_scope
()
def
func
(
self
,
place
):
# the shape of input variable shoule be clearly specified, not inlcude -1.
shape
=
[
7
,
9
]
shape
=
[
2
,
3
,
5
,
7
]
eps
=
0.005
dtype
=
np
.
float64
...
...
@@ -296,6 +296,60 @@ class TestElementwiseAddBroadcastDoubleGradCheck(unittest.TestCase):
self
.
func
(
p
)
class
TestElementwiseSubDoubleGradCheck
(
unittest
.
TestCase
):
@
prog_scope
()
def
func
(
self
,
place
):
# the shape of input variable shoule be clearly specified, not inlcude -1.
shape
=
[
2
,
3
,
5
,
7
]
eps
=
0.005
dtype
=
np
.
float64
x
=
layers
.
data
(
'x'
,
shape
,
False
,
dtype
)
y
=
layers
.
data
(
'y'
,
shape
,
False
,
dtype
)
x
.
persistable
=
True
y
.
persistable
=
True
out
=
layers
.
elementwise_sub
(
x
,
y
)
x_arr
=
np
.
random
.
uniform
(
-
1
,
1
,
shape
).
astype
(
dtype
)
y_arr
=
np
.
random
.
uniform
(
-
1
,
1
,
shape
).
astype
(
dtype
)
gradient_checker
.
double_grad_check
(
[
x
,
y
],
out
,
x_init
=
[
x_arr
,
y_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
TestElementwiseSubBroadcastDoubleGradCheck
(
unittest
.
TestCase
):
@
prog_scope
()
def
func
(
self
,
place
):
# the shape of input variable shoule be clearly specified, not inlcude -1.
shape
=
[
2
,
3
,
5
,
7
]
eps
=
0.005
dtype
=
np
.
float64
x
=
layers
.
data
(
'x'
,
shape
,
False
,
dtype
)
y
=
layers
.
data
(
'y'
,
shape
[:
-
1
],
False
,
dtype
)
x
.
persistable
=
True
y
.
persistable
=
True
out
=
layers
.
elementwise_sub
(
x
,
y
,
axis
=
0
)
x_arr
=
np
.
random
.
uniform
(
-
1
,
1
,
shape
).
astype
(
dtype
)
y_arr
=
np
.
random
.
uniform
(
-
1
,
1
,
shape
[:
-
1
]).
astype
(
dtype
)
gradient_checker
.
double_grad_check
(
[
x
,
y
],
out
,
x_init
=
[
x_arr
,
y_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
TestMulDoubleGradCheck
(
unittest
.
TestCase
):
@
prog_scope
()
def
func
(
self
,
place
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录