Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
267b218f
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
267b218f
编写于
11月 09, 2022
作者:
C
cyber-pioneer
提交者:
GitHub
11月 09, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add sin triple grad operator (#47753)
上级
f369b2b1
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
169 addition
and
1 deletion
+169
-1
paddle/fluid/eager/auto_code_generator/generator/codegen_utils.py
...luid/eager/auto_code_generator/generator/codegen_utils.py
+2
-0
paddle/phi/api/yaml/backward.yaml
paddle/phi/api/yaml/backward.yaml
+12
-0
paddle/phi/api/yaml/op_compat.yaml
paddle/phi/api/yaml/op_compat.yaml
+1
-1
paddle/phi/kernels/activation_grad_kernel.h
paddle/phi/kernels/activation_grad_kernel.h
+11
-0
paddle/phi/kernels/cpu/activation_grad_kernel.cc
paddle/phi/kernels/cpu/activation_grad_kernel.cc
+9
-0
paddle/phi/kernels/funcs/activation_functor.h
paddle/phi/kernels/funcs/activation_functor.h
+58
-0
paddle/phi/kernels/gpu/activation_grad_kernel.cu
paddle/phi/kernels/gpu/activation_grad_kernel.cu
+10
-0
paddle/phi/kernels/impl/activation_grad_impl.h
paddle/phi/kernels/impl/activation_grad_impl.h
+34
-0
python/paddle/fluid/tests/unittests/test_activation_nn_grad.py
...n/paddle/fluid/tests/unittests/test_activation_nn_grad.py
+32
-0
未找到文件。
paddle/fluid/eager/auto_code_generator/generator/codegen_utils.py
浏览文件 @
267b218f
...
...
@@ -36,6 +36,8 @@ ops_to_fill_zero_for_empty_grads = set(
"batch_norm_double_grad"
,
"tanh_double_grad"
,
"tanh_triple_grad"
,
"sin_double_grad"
,
"sin_triple_grad"
,
"subtract_double_grad"
,
"divide_double_grad"
,
"log_double_grad"
,
...
...
paddle/phi/api/yaml/backward.yaml
浏览文件 @
267b218f
...
...
@@ -661,6 +661,7 @@
param
:
[
x
,
x
]
kernel
:
func
:
sin_double_grad
backward
:
sin_triple_grad
inplace
:
(grad_x_grad -> grad_out_grad)
-
backward_op
:
sin_grad
...
...
@@ -675,6 +676,17 @@
backward
:
sin_double_grad
inplace
:
(out_grad -> x_grad)
-
backward_op
:
sin_triple_grad
forward
:
sin_double_grad (Tensor x, Tensor grad_out_forward, Tensor grad_x_grad_forward) -> Tensor(grad_x), Tensor(grad_out_grad)
args
:
(Tensor x, Tensor grad_out_forward, Tensor grad_x_grad_forward, Tensor grad_x_grad, Tensor grad_out_grad_grad)
output
:
Tensor(x_grad), Tensor(grad_out_forward_grad), Tensor(grad_x_grad_forward_grad)
infer_meta
:
func
:
GeneralTernaryGradInferMeta
param
:
[
x
,
x
,
grad_x_grad_forward
]
kernel
:
func
:
sin_triple_grad
inplace
:
(grad_x_grad_forward -> grad_out_forward_grad)
-
backward_op
:
sinh_grad
forward
:
sinh (Tensor x) -> Tensor(out)
args
:
(Tensor x, Tensor out_grad)
...
...
paddle/phi/api/yaml/op_compat.yaml
浏览文件 @
267b218f
...
...
@@ -831,7 +831,7 @@
attrs
:
[
bool use_mkldnn = false
,
bool use_cudnn = false
]
-
op
:
sin
backward
:
sin_grad, sin_double_grad
backward
:
sin_grad, sin_double_grad
, sin_triple_grad
inputs
:
x
:
X
outputs
:
...
...
paddle/phi/kernels/activation_grad_kernel.h
浏览文件 @
267b218f
...
...
@@ -107,6 +107,17 @@ void TanhTripleGradKernel(const Context& dev_ctx,
DenseTensor
*
d_dout
,
DenseTensor
*
d_ddx
);
template
<
typename
T
,
typename
Context
>
void
SinTripleGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
const
DenseTensor
&
dout
,
const
DenseTensor
&
ddx
,
const
DenseTensor
&
d_dx_new
,
const
DenseTensor
&
d_ddout
,
DenseTensor
*
d_x_new
,
DenseTensor
*
d_dout
,
DenseTensor
*
d_ddx
);
template
<
typename
T
,
typename
Context
>
void
LeakyReluDoubleGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
...
...
paddle/phi/kernels/cpu/activation_grad_kernel.cc
浏览文件 @
267b218f
...
...
@@ -345,6 +345,15 @@ PD_REGISTER_KERNEL(sin_double_grad,
phi
::
dtype
::
float16
,
int
,
int64_t
)
{}
PD_REGISTER_KERNEL
(
sin_triple_grad
,
CPU
,
ALL_LAYOUT
,
phi
::
SinTripleGradKernel
,
float
,
double
,
phi
::
dtype
::
float16
,
int
,
int64_t
)
{}
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
softsign_grad
,
SoftsignGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
sigmoid_grad
,
SigmoidGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
sigmoid_double_grad
,
SigmoidDoubleGradKernel
)
...
...
paddle/phi/kernels/funcs/activation_functor.h
浏览文件 @
267b218f
...
...
@@ -138,6 +138,64 @@ struct SinDoubleGradFunctor : public BaseActivationFunctor<T> {
static
constexpr
ActBwdOpFwdDeps
FwdDeps
()
{
return
kDepX
;
}
};
// 1st reverse grad
// y = sin(x)
// x --> y
// d1x = d1y * cos(x)
//
// 2nd reverse grad
// x, d1y --> d1x
// d2x = -sin(x) * d1y * d2d1x
// d2d1y = cos(x) * d2d1x
//
// 3rd reverse grad
// x, d1y, d2d1x --> d2x, d2d1y
// d3x = -cos(x) * d1y * d2d1x * d3d2x - sin(x) * d2d1x * d3d2d1y
// d3d1y = -sin(x) * d2d1x * d3d2x
// d3d2d1x = -sin(x) * d1y * d3d2x + cos(x) * d3d2d1y
template
<
typename
T
>
struct
SinTripleGradFunctor
:
public
BaseActivationFunctor
<
T
>
{
template
<
typename
Device
>
void
operator
()(
const
Device
&
dev
,
const
DenseTensor
*
X
,
const
DenseTensor
*
ddX
,
const
DenseTensor
*
dOut
,
const
DenseTensor
*
d_DDOut
,
const
DenseTensor
*
d_dx_New
,
DenseTensor
*
d_d_Out
,
DenseTensor
*
d_x_New
,
DenseTensor
*
d_DDx
)
const
{
auto
*
d
=
dev
.
eigen_device
();
auto
x
=
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
X
,
"Input"
,
"x"
,
"SinTripleGrad"
));
auto
d2d1x
=
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
ddX
,
"Input"
,
"d2d1x"
,
"SinTripleGrad"
));
auto
d1y
=
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
dOut
,
"Input"
,
"d1y"
,
"SinTripleGrad"
));
auto
d3d2d1y
=
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
d_DDOut
,
"Input"
,
"d3d2d1y"
,
"SinTripleGrad"
));
auto
d3d2x
=
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
d_dx_New
,
"Input"
,
"d3d2x"
,
"SinTripleGrad"
));
auto
d3x
=
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
d_x_New
,
"Output"
,
"d3x"
,
"SinTripleGrad"
));
d3x
.
device
(
*
d
)
=
-
x
.
unaryExpr
(
Cosine
<
T
>
())
*
d1y
*
d2d1x
*
d3d2x
-
x
.
unaryExpr
(
Sine
<
T
>
())
*
d2d1x
*
d3d2d1y
;
auto
d3d1y
=
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
d_d_Out
,
"Output"
,
"d3d1y"
,
"SinTripleGrad"
));
d3d1y
.
device
(
*
d
)
=
-
x
.
unaryExpr
(
Sine
<
T
>
())
*
d2d1x
*
d3d2x
;
auto
d3d2d1x
=
EigenVector
<
T
>::
Flatten
(
GET_DATA_SAFELY
(
d_DDx
,
"Output"
,
"d3d2d1x"
,
"SinTripleGrad"
));
d3d2d1x
.
device
(
*
d
)
=
-
x
.
unaryExpr
(
Sine
<
T
>
())
*
d1y
*
d3d2x
+
x
.
unaryExpr
(
Cosine
<
T
>
())
*
d3d2d1y
;
}
static
constexpr
ActBwdOpFwdDeps
FwdDeps
()
{
return
ActBwdOpFwdDeps
::
kDepOut
;
}
};
// reciprocal(x) = 1 / x
template
<
typename
T
>
struct
ReciprocalFunctor
:
public
BaseActivationFunctor
<
T
>
{
...
...
paddle/phi/kernels/gpu/activation_grad_kernel.cu
浏览文件 @
267b218f
...
...
@@ -427,6 +427,16 @@ PD_REGISTER_KERNEL(sin_double_grad,
int64_t
,
phi
::
dtype
::
float16
)
{}
PD_REGISTER_KERNEL
(
sin_triple_grad
,
GPU
,
ALL_LAYOUT
,
phi
::
SinTripleGradKernel
,
float
,
double
,
int
,
int64_t
,
phi
::
dtype
::
float16
)
{}
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
softsign_grad
,
SoftsignGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
sigmoid_grad
,
SigmoidGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
sigmoid_double_grad
,
SigmoidDoubleGradKernel
)
...
...
paddle/phi/kernels/impl/activation_grad_impl.h
浏览文件 @
267b218f
...
...
@@ -467,4 +467,38 @@ void SinDoubleGradKernel(const Context& dev_ctx,
functor
(
dev_ctx
,
&
x
,
&
dout
,
&
ddx
,
dx
,
ddout
);
}
template
<
typename
T
,
typename
Context
>
void
SinTripleGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
const
DenseTensor
&
dout
,
const
DenseTensor
&
ddx
,
const
DenseTensor
&
d_dx_new
,
const
DenseTensor
&
d_ddout
,
DenseTensor
*
d_x_new
,
DenseTensor
*
d_dout
,
DenseTensor
*
d_ddx
)
{
if
(
d_dout
)
{
d_dout
->
Resize
(
x
.
dims
());
dev_ctx
.
template
Alloc
<
T
>(
d_dout
);
}
if
(
d_x_new
)
{
d_dout
->
Resize
(
x
.
dims
());
dev_ctx
.
template
Alloc
<
T
>(
d_x_new
);
}
if
(
d_ddx
)
{
d_dout
->
Resize
(
ddx
.
dims
());
dev_ctx
.
template
Alloc
<
T
>(
d_ddx
);
}
funcs
::
SinTripleGradFunctor
<
T
>
functor
;
functor
(
dev_ctx
,
&
x
,
&
ddx
,
&
dout
,
&
d_ddout
,
&
d_dx_new
,
// input
d_dout
,
d_x_new
,
d_ddx
);
// output
}
}
// namespace phi
python/paddle/fluid/tests/unittests/test_activation_nn_grad.py
浏览文件 @
267b218f
...
...
@@ -565,5 +565,37 @@ class TestPowDoubleGradCheck2(unittest.TestCase):
self
.
func
(
p
)
class
TestSinTripleGradCheck
(
unittest
.
TestCase
):
def
sin_wrapper
(
self
,
x
):
return
paddle
.
sin
(
x
[
0
])
@
prog_scope
()
def
func
(
self
,
place
):
shape
=
[
2
,
3
,
7
,
9
]
eps
=
0.0005
dtype
=
np
.
float64
x
=
layers
.
data
(
'x'
,
shape
,
False
,
dtype
=
dtype
)
x
.
persistable
=
True
y
=
layers
.
sin
(
x
)
x_arr
=
np
.
random
.
random
(
shape
).
astype
(
dtype
)
x_arr
[
np
.
abs
(
x_arr
)
<
0.005
]
=
0.002
gradient_checker
.
triple_grad_check
(
[
x
],
y
,
x_init
=
x_arr
,
place
=
place
,
eps
=
eps
)
fluid
.
set_flags
({
"FLAGS_retain_grad_for_all_tensor"
:
True
})
gradient_checker
.
triple_grad_check_for_dygraph
(
self
.
sin_wrapper
,
[
x
],
y
,
x_init
=
x_arr
,
place
=
place
)
fluid
.
set_flags
({
"FLAGS_retain_grad_for_all_tensor"
:
False
})
def
test_grad
(
self
):
paddle
.
enable_static
()
places
=
[
fluid
.
CPUPlace
()]
if
core
.
is_compiled_with_cuda
():
places
.
append
(
fluid
.
CUDAPlace
(
0
))
for
p
in
places
:
self
.
func
(
p
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录