Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
71ab8ae9
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
71ab8ae9
编写于
1月 15, 2021
作者:
W
whs
提交者:
GitHub
1月 15, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support double backward rsqrt (#29589) (#30431)
上级
ae75affd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
176 addition
and
1 deletion
+176
-1
paddle/fluid/operators/activation_op.cc
paddle/fluid/operators/activation_op.cc
+48
-0
paddle/fluid/operators/activation_op.cu
paddle/fluid/operators/activation_op.cu
+14
-0
paddle/fluid/operators/activation_op.h
paddle/fluid/operators/activation_op.h
+90
-1
python/paddle/fluid/tests/unittests/test_activation_nn_grad.py
...n/paddle/fluid/tests/unittests/test_activation_nn_grad.py
+24
-0
未找到文件。
paddle/fluid/operators/activation_op.cc
浏览文件 @
71ab8ae9
...
...
@@ -886,6 +886,25 @@ class SqrtDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
}
};
// rsqrt Grad: dx = -0.5 * dy * y * y * y
// rsqrt GradGrad: ddy = -0.5 * ddx * y * y * y, dy = (3/y) * ddx
template
<
typename
T
>
class
RsqrtDoubleGradMaker
:
public
::
paddle
::
framework
::
SingleGradOpMaker
<
T
>
{
public:
using
::
paddle
::
framework
::
SingleGradOpMaker
<
T
>::
SingleGradOpMaker
;
protected:
void
Apply
(
GradOpPtr
<
T
>
op
)
const
override
{
op
->
SetType
(
"rsqrt_grad_grad"
);
op
->
SetInput
(
"Out"
,
this
->
Input
(
"Out"
));
op
->
SetInput
(
"DX"
,
this
->
Output
(
framework
::
GradVarName
(
"X"
)));
op
->
SetInput
(
"DDX"
,
this
->
OutputGrad
(
framework
::
GradVarName
(
"X"
)));
op
->
SetAttrMap
(
this
->
Attrs
());
op
->
SetOutput
(
"DOut"
,
this
->
InputGrad
(
"Out"
));
op
->
SetOutput
(
"DDOut"
,
this
->
InputGrad
(
framework
::
GradVarName
(
"Out"
)));
}
};
// square Grad: dx=2x*dy
// square GradGrad: ddy=2x*ddx, dx=2dy*ddx
template
<
typename
T
>
...
...
@@ -1157,6 +1176,35 @@ REGISTER_OP_CPU_KERNEL(
ops
::
SqrtGradGradFunctor
<
plat
::
float16
>>
);
/* ========================================================================== */
/* =========================== rsqrt register =============================
*/
REGISTER_OPERATOR
(
rsqrt
,
ops
::
ActivationOp
,
ops
::
RsqrtOpMaker
,
ops
::
ActivationOpInferVarType
,
ops
::
ActivationGradOpMaker
<
ops
::
RsqrtGradFunctor
<
float
>::
FwdDeps
(),
paddle
::
framework
::
OpDesc
>
,
ops
::
ActivationGradOpMaker
<
ops
::
RsqrtGradFunctor
<
float
>::
FwdDeps
(),
paddle
::
imperative
::
OpBase
>
,
ops
::
ActFwdInplaceInferer
);
REGISTER_OPERATOR
(
rsqrt_grad
,
ops
::
ActivationOpGrad
,
ops
::
ActivationGradOpInplaceInferer
,
ops
::
RsqrtDoubleGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
RsqrtDoubleGradMaker
<
paddle
::
imperative
::
OpBase
>
);
REGISTER_OPERATOR
(
rsqrt_grad_grad
,
ops
::
ActivationOpDoubleGrad
<
ops
::
RsqrtGradGradFunctor
<
float
>::
FwdDeps
()
>
,
ops
::
ActivationDoubleGradOpInplaceInferer
);
REGISTER_ACTIVATION_CPU_KERNEL
(
rsqrt
,
Rsqrt
,
RsqrtFunctor
,
RsqrtGradFunctor
);
REGISTER_OP_CPU_KERNEL
(
rsqrt_grad_grad
,
ops
::
RsqrtDoubleGradKernel
<
plat
::
CPUDeviceContext
,
ops
::
RsqrtGradGradFunctor
<
float
>>
,
ops
::
RsqrtDoubleGradKernel
<
plat
::
CPUDeviceContext
,
ops
::
RsqrtGradGradFunctor
<
double
>>
,
ops
::
RsqrtDoubleGradKernel
<
plat
::
CPUDeviceContext
,
ops
::
RsqrtGradGradFunctor
<
plat
::
float16
>>
);
/* ========================================================================== */
/* ========================== square register ============================ */
REGISTER_OPERATOR
(
square
,
ops
::
ActivationOp
,
ops
::
SquareOpMaker
,
...
...
paddle/fluid/operators/activation_op.cu
浏览文件 @
71ab8ae9
...
...
@@ -85,6 +85,20 @@ REGISTER_OP_CUDA_KERNEL(
ops
::
SqrtGradGradFunctor
<
plat
::
float16
>>
);
/* ========================================================================== */
/* =========================== rsqrt register =============================
*/
REGISTER_ACTIVATION_CUDA_KERNEL
(
rsqrt
,
Rsqrt
,
RsqrtFunctor
,
RsqrtGradFunctor
);
REGISTER_OP_CUDA_KERNEL
(
rsqrt_grad_grad
,
ops
::
RsqrtDoubleGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
ops
::
RsqrtGradGradFunctor
<
float
>>
,
ops
::
RsqrtDoubleGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
ops
::
RsqrtGradGradFunctor
<
double
>>
,
ops
::
RsqrtDoubleGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
ops
::
RsqrtGradGradFunctor
<
plat
::
float16
>>
);
/* ========================================================================== */
/* =========================== square register ============================ */
REGISTER_OP_CUDA_KERNEL
(
square
,
...
...
paddle/fluid/operators/activation_op.h
100755 → 100644
浏览文件 @
71ab8ae9
...
...
@@ -1610,6 +1610,35 @@ struct SqrtGradGradFunctor : public BaseActivationFunctor<T> {
static
constexpr
ActBwdOpFwdDeps
FwdDeps
()
{
return
kDepOut
;
}
};
template
<
typename
T
>
struct
RsqrtGradGradFunctor
:
public
BaseActivationFunctor
<
T
>
{
template
<
typename
Device
>
void
operator
()(
const
Device
&
dev
,
const
framework
::
Tensor
*
Out
,
const
framework
::
Tensor
*
ddX
,
framework
::
Tensor
*
ddOut
,
framework
::
Tensor
*
dOut
,
const
framework
::
Tensor
*
dX
)
const
{
auto
*
d
=
dev
.
eigen_device
();
auto
ddx
=
framework
::
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
ddX
,
"Input"
,
"DDX"
,
"RsqrtGradGrad"
));
auto
out
=
framework
::
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
Out
,
"Output"
,
"Out"
,
"RsqrtGradGrad"
));
// rsqrt GradGrad: ddy = -0.5 * ddx * y * y * y, dy = (3/y) * dx * ddx
if
(
dOut
)
{
auto
dx
=
framework
::
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
dX
,
"Output"
,
"DX"
,
"RsqrtGradGrad"
));
auto
dout
=
framework
::
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
dOut
,
"Output"
,
"DOut"
,
"RsqrtGradGrad"
));
dout
.
device
(
*
d
)
=
(
static_cast
<
T
>
(
3.0
)
/
out
)
*
dx
*
ddx
;
}
if
(
ddOut
)
{
auto
ddout
=
framework
::
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
ddOut
,
"Output"
,
"DDOut"
,
"RsqrtGradGrad"
));
ddout
.
device
(
*
d
)
=
ddx
*
static_cast
<
T
>
(
-
0.5
)
*
out
*
out
*
out
;
}
}
static
constexpr
ActBwdOpFwdDeps
FwdDeps
()
{
return
kDepOut
;
}
};
template
<
typename
T
>
struct
SquareGradGradFunctor
:
public
BaseActivationFunctor
<
T
>
{
template
<
typename
Device
>
...
...
@@ -1795,6 +1824,67 @@ class SqrtDoubleGradKernel
}
};
// rsqrt Grad: dx = -0.5 * dy * y * y * y
// rsqrt GradGrad: ddy = -0.5 * ddx * y * y * y, dy = (3 / y) * dx * ddx
template
<
typename
DeviceContext
,
typename
Functor
>
class
RsqrtDoubleGradKernel
:
public
framework
::
OpKernel
<
typename
Functor
::
ELEMENT_TYPE
>
{
public:
using
T
=
typename
Functor
::
ELEMENT_TYPE
;
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
const
framework
::
Tensor
*
Out
,
*
dX
,
*
ddX
;
Out
=
dX
=
ddX
=
nullptr
;
framework
::
Tensor
*
ddOut
,
*
dOut
;
ddOut
=
dOut
=
nullptr
;
// extract ddx(input), ddout(output)
auto
ddx_var
=
ctx
.
InputVar
(
"DDX"
);
auto
ddo_var
=
ctx
.
OutputVar
(
"DDOut"
);
PADDLE_ENFORCE_NOT_NULL
(
ddx_var
,
platform
::
errors
::
NotFound
(
"Cannot get input Variable DDX, variable name = %s"
,
ctx
.
InputName
(
"DDX"
)));
ddX
=
ctx
.
Input
<
framework
::
Tensor
>
(
"DDX"
);
if
(
ddo_var
)
{
ddOut
=
ctx
.
Output
<
framework
::
Tensor
>
(
"DDOut"
);
}
PADDLE_ENFORCE_NOT_NULL
(
ddX
,
platform
::
errors
::
NotFound
(
"Cannot get input Variable DDX, variable name = %s"
,
ctx
.
InputName
(
"DDX"
)));
// extract out(input), dout(output)
auto
out_var
=
ctx
.
InputVar
(
"Out"
);
PADDLE_ENFORCE_NOT_NULL
(
out_var
,
platform
::
errors
::
NotFound
(
"Cannot get input Variable Out, variable name = %s"
,
ctx
.
InputName
(
"Out"
)));
auto
dout_var
=
ctx
.
OutputVar
(
"DOut"
);
Out
=
ctx
.
Input
<
framework
::
Tensor
>
(
"Out"
);
if
(
dout_var
)
{
dOut
=
ctx
.
Output
<
framework
::
Tensor
>
(
"DOut"
);
}
// extract dx(input)
auto
dx_var
=
ctx
.
InputVar
(
"DX"
);
PADDLE_ENFORCE_NOT_NULL
(
dx_var
,
platform
::
errors
::
NotFound
(
"Cannot get input Variable DX, variable name = %s"
,
ctx
.
InputName
(
"DX"
)));
if
(
dx_var
)
{
dX
=
ctx
.
Input
<
framework
::
Tensor
>
(
"DX"
);
}
if
(
dOut
)
dOut
->
mutable_data
<
T
>
(
Out
->
dims
(),
ctx
.
GetPlace
());
if
(
ddOut
)
ddOut
->
mutable_data
<
T
>
(
Out
->
dims
(),
ctx
.
GetPlace
());
auto
&
place
=
ctx
.
template
device_context
<
DeviceContext
>();
Functor
functor
;
functor
(
place
,
Out
,
ddX
,
ddOut
,
dOut
,
dX
);
}
};
template
<
typename
DeviceContext
,
typename
Functor
>
class
PowKernel
:
public
framework
::
OpKernel
<
typename
Functor
::
ELEMENT_TYPE
>
{
public:
...
...
@@ -1938,7 +2028,6 @@ struct LogGradGradFunctor : public BaseActivationFunctor<T> {
__macro(tanh, Tanh, TanhFunctor, TanhGradFunctor); \
__macro(atan, Atan, AtanFunctor, AtanGradFunctor); \
__macro(softshrink, SoftShrink, SoftShrinkFunctor, SoftShrinkGradFunctor); \
__macro(rsqrt, Rsqrt, RsqrtFunctor, RsqrtGradFunctor); \
__macro(ceil, Ceil, CeilFunctor, ZeroGradFunctor); \
__macro(floor, Floor, FloorFunctor, ZeroGradFunctor); \
__macro(cos, Cos, CosFunctor, CosGradFunctor); \
...
...
python/paddle/fluid/tests/unittests/test_activation_nn_grad.py
浏览文件 @
71ab8ae9
...
...
@@ -125,6 +125,30 @@ class TestSqrtDoubleGradCheck(unittest.TestCase):
self
.
func
(
p
)
class
TestRsqrtDoubleGradCheck
(
unittest
.
TestCase
):
@
prog_scope
()
def
func
(
self
,
place
):
shape
=
[
2
,
3
,
7
,
9
]
eps
=
0.0001
dtype
=
np
.
float64
x
=
layers
.
data
(
'x'
,
shape
,
False
,
dtype
)
x
.
persistable
=
True
y
=
layers
.
rsqrt
(
x
)
x_arr
=
np
.
random
.
uniform
(
0.1
,
1
,
shape
).
astype
(
dtype
)
gradient_checker
.
double_grad_check
(
[
x
],
y
,
x_init
=
x_arr
,
place
=
place
,
eps
=
eps
)
def
test_grad
(
self
):
places
=
[
fluid
.
CPUPlace
()]
if
core
.
is_compiled_with_cuda
():
places
=
[
fluid
.
CUDAPlace
(
0
)]
for
p
in
places
:
self
.
func
(
p
)
class
TestSquareDoubleGradCheck
(
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录