Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
1d4e938d
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看板
未验证
提交
1d4e938d
编写于
9月 07, 2023
作者:
R
Ruibin Cheung
提交者:
GitHub
9月 07, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[complex] add complex support for silu (#56903)
上级
5ed7c8a0
变更
8
显示空白变更内容
内联
并排
Showing
8 changed file
with
82 addition
and
7 deletion
+82
-7
paddle/phi/common/complex.h
paddle/phi/common/complex.h
+10
-0
paddle/phi/kernels/cpu/activation_grad_kernel.cc
paddle/phi/kernels/cpu/activation_grad_kernel.cc
+1
-1
paddle/phi/kernels/cpu/activation_kernel.cc
paddle/phi/kernels/cpu/activation_kernel.cc
+1
-1
paddle/phi/kernels/funcs/activation_functor.h
paddle/phi/kernels/funcs/activation_functor.h
+36
-0
paddle/phi/kernels/gpu/activation_grad_kernel.cu
paddle/phi/kernels/gpu/activation_grad_kernel.cu
+1
-1
paddle/phi/kernels/gpu/activation_kernel.cu
paddle/phi/kernels/gpu/activation_kernel.cu
+1
-1
python/paddle/nn/functional/activation.py
python/paddle/nn/functional/activation.py
+12
-2
test/legacy_test/test_activation_op.py
test/legacy_test/test_activation_op.py
+20
-1
未找到文件。
paddle/phi/common/complex.h
浏览文件 @
1d4e938d
...
...
@@ -496,6 +496,16 @@ HOSTDEVICE inline complex<T> log(const complex<T>& a) {
#endif
}
template
<
typename
T
>
HOSTDEVICE
inline
complex
<
T
>
exp
(
const
complex
<
T
>&
a
)
{
#if defined(PADDLE_WITH_CUDA_OR_HIP_COMPLEX) && \
(defined(__CUDA_ARCH__) || defined(__HIPCC__))
return
complex
<
T
>
(
thrust
::
exp
(
thrust
::
complex
<
T
>
(
a
)));
#else
return
complex
<
T
>
(
std
::
exp
(
std
::
complex
<
T
>
(
a
)));
#endif
}
template
<
typename
T
>
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
complex
<
T
>&
a
)
{
os
<<
"real:"
<<
a
.
real
<<
" imag:"
<<
a
.
imag
;
...
...
paddle/phi/kernels/cpu/activation_grad_kernel.cc
浏览文件 @
1d4e938d
...
...
@@ -301,7 +301,7 @@ PD_REGISTER_ACTIVATION_GRAD_KERNEL(softshrink_grad, SoftShrinkGradKernel)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
hard_shrink_grad
,
HardShrinkGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
tanh_shrink_grad
,
TanhShrinkGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
elu_grad
,
EluGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
silu_grad
,
SiluGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
_WITH_COMPLEX
(
silu_grad
,
SiluGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
mish_grad
,
MishGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
stanh_grad
,
STanhGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
reciprocal_grad
,
ReciprocalGradKernel
)
...
...
paddle/phi/kernels/cpu/activation_kernel.cc
浏览文件 @
1d4e938d
...
...
@@ -195,7 +195,7 @@ PD_REGISTER_ACTIVATION_KERNEL(hard_shrink, HardShrinkKernel)
PD_REGISTER_ACTIVATION_KERNEL
(
softshrink
,
SoftShrinkKernel
)
PD_REGISTER_ACTIVATION_KERNEL
(
tanh_shrink
,
TanhShrinkKernel
)
PD_REGISTER_ACTIVATION_KERNEL
(
elu
,
EluKernel
)
PD_REGISTER_ACTIVATION_KERNEL
(
silu
,
SiluKernel
)
PD_REGISTER_ACTIVATION_KERNEL
_WITH_COMPLEX
(
silu
,
SiluKernel
)
PD_REGISTER_ACTIVATION_KERNEL
(
mish
,
MishKernel
)
PD_REGISTER_ACTIVATION_KERNEL
(
stanh
,
STanhKernel
)
PD_REGISTER_ACTIVATION_KERNEL
(
reciprocal
,
ReciprocalKernel
)
...
...
paddle/phi/kernels/funcs/activation_functor.h
浏览文件 @
1d4e938d
...
...
@@ -1847,6 +1847,25 @@ struct SiluGradFunctor : public BaseActivationFunctor<T> {
static
constexpr
ActBwdOpFwdDeps
FwdDeps
()
{
return
ActBwdOpFwdDeps
::
kDepX
;
}
};
template
<
typename
T
>
struct
SiluGradFunctor
<
ComplexType
<
T
>>
:
public
BaseActivationFunctor
<
ComplexType
<
T
>>
{
template
<
typename
Device
,
typename
X
,
typename
Out
,
typename
dOut
,
typename
dX
>
void
operator
()(
Device
d
,
X
x
,
Out
out
UNUSED
,
dOut
dout
,
dX
dx
)
const
{
auto
temp1
=
static_cast
<
ComplexType
<
T
>>
(
1
)
+
(
-
x
).
exp
();
// 1+e^(-x)
auto
temp2
=
x
*
(
-
x
).
exp
();
// x*e^(-x)
dx
.
device
(
d
)
=
dout
*
((
static_cast
<
ComplexType
<
T
>>
(
1
)
/
temp1
)
*
(
static_cast
<
ComplexType
<
T
>>
(
1
)
+
(
temp2
/
temp1
)))
.
unaryExpr
(
Conj
<
T
>
());
}
static
constexpr
ActBwdOpFwdDeps
FwdDeps
()
{
return
ActBwdOpFwdDeps
::
kDepX
;
}
};
template
<
typename
T
>
struct
SoftsignFunctor
:
public
BaseActivationFunctor
<
T
>
{
template
<
typename
Device
,
typename
X
,
typename
Out
>
...
...
@@ -3793,6 +3812,23 @@ struct CudaSiluGradFunctor : public BaseActivationFunctor<T> {
static
constexpr
ActBwdOpFwdDeps
FwdDeps
()
{
return
ActBwdOpFwdDeps
::
kDepX
;
}
};
template
<
typename
T
>
struct
CudaSiluGradFunctor
<
ComplexType
<
T
>>
:
public
BaseActivationFunctor
<
ComplexType
<
T
>>
{
ComplexType
<
T
>
one
=
static_cast
<
ComplexType
<
T
>>
(
1.0
f
);
// dx = dout * (1 + exp(-x) + x * exp(-x) / (1 + exp(-x))^2)
__device__
__forceinline__
ComplexType
<
T
>
operator
()(
const
ComplexType
<
T
>
arg_dout
,
const
ComplexType
<
T
>
arg_x
)
const
{
ComplexType
<
T
>
dout
=
static_cast
<
ComplexType
<
T
>>
(
arg_dout
);
ComplexType
<
T
>
x
=
static_cast
<
ComplexType
<
T
>>
(
arg_x
);
ComplexType
<
T
>
temp
=
one
/
(
one
+
exp
(
-
x
));
return
dout
*
conj
(
temp
*
(
one
+
x
*
(
one
-
temp
)));
}
static
constexpr
ActBwdOpFwdDeps
FwdDeps
()
{
return
ActBwdOpFwdDeps
::
kDepX
;
}
};
template
<
typename
T
>
struct
CudaSoftsignFunctor
:
public
BaseActivationFunctor
<
T
>
{
T
one
=
static_cast
<
T
>
(
1.0
f
);
...
...
paddle/phi/kernels/gpu/activation_grad_kernel.cu
浏览文件 @
1d4e938d
...
...
@@ -403,7 +403,7 @@ PD_REGISTER_KERNEL(exp_grad,
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
softshrink_grad
,
SoftShrinkGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
hard_shrink_grad
,
HardShrinkGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
tanh_shrink_grad
,
TanhShrinkGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
silu_grad
,
SiluGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
_WITH_COMPLEX
(
silu_grad
,
SiluGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
elu_grad
,
EluGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
elu_double_grad
,
EluDoubleGradKernel
)
PD_REGISTER_ACTIVATION_GRAD_KERNEL
(
logit_grad
,
LogitCUDAGradKernel
)
...
...
paddle/phi/kernels/gpu/activation_kernel.cu
浏览文件 @
1d4e938d
...
...
@@ -287,7 +287,7 @@ PD_REGISTER_ACTIVATION_KERNEL(hard_shrink, HardShrinkKernel)
PD_REGISTER_ACTIVATION_KERNEL
(
softshrink
,
SoftShrinkKernel
)
PD_REGISTER_ACTIVATION_KERNEL
(
tanh_shrink
,
TanhShrinkKernel
)
PD_REGISTER_ACTIVATION_KERNEL
(
elu
,
EluKernel
)
PD_REGISTER_ACTIVATION_KERNEL
(
silu
,
SiluKernel
)
PD_REGISTER_ACTIVATION_KERNEL
_WITH_COMPLEX
(
silu
,
SiluKernel
)
PD_REGISTER_ACTIVATION_KERNEL
(
softsign
,
SoftsignKernel
)
PD_REGISTER_ACTIVATION_KERNEL
(
sigmoid
,
SigmoidKernel
)
PD_REGISTER_ACTIVATION_KERNEL_WITH_COMPLEX
(
logsigmoid
,
LogSigmoidKernel
)
...
...
python/paddle/nn/functional/activation.py
浏览文件 @
1d4e938d
...
...
@@ -1034,7 +1034,7 @@ def silu(x, name=None):
Where :math:`x` is the input Tensor.
Parameters:
x (Tensor): The input Tensor with data type bfloat16, float16, float32, float64.
x (Tensor): The input Tensor with data type bfloat16, float16, float32, float64
, complex64, complex128
.
name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
Returns:
...
...
@@ -1057,7 +1057,17 @@ def silu(x, name=None):
return
_C_ops
.
silu
(
x
)
else
:
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'uint16'
,
'float32'
,
'float64'
],
'silu'
x
,
'x'
,
[
'float16'
,
'uint16'
,
'float32'
,
'float64'
,
'complex64'
,
'complex128'
,
],
'silu'
,
)
helper
=
LayerHelper
(
"silu"
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
...
...
test/legacy_test/test_activation_op.py
浏览文件 @
1d4e938d
...
...
@@ -384,6 +384,11 @@ class TestSilu(TestActivation):
np
.
random
.
seed
(
1024
)
x
=
np
.
random
.
uniform
(
-
1
,
1
,
self
.
shape
).
astype
(
self
.
dtype
)
if
self
.
dtype
==
np
.
complex64
or
self
.
dtype
==
np
.
complex128
:
x
=
(
np
.
random
.
uniform
(
-
1
,
1
,
self
.
shape
)
+
1j
*
np
.
random
.
uniform
(
-
1
,
1
,
self
.
shape
)
).
astype
(
self
.
dtype
)
out
=
x
/
(
np
.
exp
(
-
x
)
+
1
)
self
.
inputs
=
{
'X'
:
OpTest
.
np_dtype_to_fluid_dtype
(
x
)}
self
.
outputs
=
{
'Out'
:
out
}
...
...
@@ -397,6 +402,10 @@ class TestSilu(TestActivation):
pass
def
test_check_grad
(
self
):
# TODO(BeingGod): set `check_prim=True` when `fill_constant` supports `complex` dtype
if
self
.
dtype
==
np
.
complex64
or
self
.
dtype
==
np
.
complex128
:
self
.
check_grad
([
'X'
],
'Out'
,
check_prim
=
False
)
else
:
self
.
check_grad
([
'X'
],
'Out'
,
check_prim
=
True
)
...
...
@@ -405,6 +414,16 @@ class TestSilu_ZeroDim(TestSilu):
self
.
shape
=
[]
class
TestSilu_Complex64
(
TestSilu
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
complex64
class
TestSilu_Complex128
(
TestSilu
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
complex128
class
TestSiluAPI
(
unittest
.
TestCase
):
# test paddle.nn.Silu, paddle.nn.functional.silu
def
setUp
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录