Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
06736921
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
未验证
提交
06736921
编写于
5月 30, 2023
作者:
R
RedContritio
提交者:
GitHub
5月 30, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support auto generate for activation_op hardswish (#53989)
上级
e531bb02
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
49 addition
and
58 deletion
+49
-58
paddle/fluid/operators/activation_op.cc
paddle/fluid/operators/activation_op.cc
+16
-46
paddle/phi/api/yaml/op_compat.yaml
paddle/phi/api/yaml/op_compat.yaml
+2
-1
paddle/phi/api/yaml/static_backward.yaml
paddle/phi/api/yaml/static_backward.yaml
+12
-0
paddle/phi/api/yaml/static_ops.yaml
paddle/phi/api/yaml/static_ops.yaml
+11
-0
paddle/phi/ops/compat/activation_sig.cc
paddle/phi/ops/compat/activation_sig.cc
+0
-9
python/paddle/incubate/autograd/composite_rules.py
python/paddle/incubate/autograd/composite_rules.py
+1
-1
python/paddle/nn/functional/activation.py
python/paddle/nn/functional/activation.py
+7
-1
未找到文件。
paddle/fluid/operators/activation_op.cc
浏览文件 @
06736921
...
@@ -83,22 +83,22 @@ class ActivationGradOpMaker : public framework::SingleGradOpMaker<T> {
...
@@ -83,22 +83,22 @@ class ActivationGradOpMaker : public framework::SingleGradOpMaker<T> {
}
}
}
}
};
};
class
HardSwishCompositeGradOpMaker
:
public
prim
::
CompositeGradOpMakerBase
{
//
class HardSwishCompositeGradOpMaker : public prim::CompositeGradOpMakerBase {
public:
//
public:
using
prim
::
CompositeGradOpMakerBase
::
CompositeGradOpMakerBase
;
//
using prim::CompositeGradOpMakerBase::CompositeGradOpMakerBase;
protected:
//
protected:
void
Apply
()
override
{
//
void Apply() override {
paddle
::
Tensor
x
=
this
->
GetSingleForwardInput
(
"X"
);
//
paddle::Tensor x = this->GetSingleForwardInput("X");
paddle
::
Tensor
out_grad
=
this
->
GetSingleOutputGrad
(
"Out"
);
//
paddle::Tensor out_grad = this->GetSingleOutputGrad("Out");
paddle
::
Tensor
dx
=
this
->
GetSingleInputGrad
(
"X"
);
//
paddle::Tensor dx = this->GetSingleInputGrad("X");
auto
*
dx_ptr
=
this
->
GetOutputPtr
(
&
dx
);
//
auto* dx_ptr = this->GetOutputPtr(&dx);
std
::
string
dx_name
=
this
->
GetOutputName
(
dx
);
//
std::string dx_name = this->GetOutputName(dx);
VLOG
(
6
)
<<
"Runing hardswish_grad composite func"
;
//
VLOG(6) << "Runing hardswish_grad composite func";
prim
::
hardswish_grad
<
prim
::
DescTensor
>
(
x
,
out_grad
,
dx_ptr
);
//
prim::hardswish_grad<prim::DescTensor>(x, out_grad, dx_ptr);
this
->
RecoverOutputName
(
dx
,
dx_name
);
//
this->RecoverOutputName(dx, dx_name);
}
//
}
};
//
};
phi
::
KernelKey
GetKernelType
(
const
framework
::
ExecutionContext
&
ctx
,
phi
::
KernelKey
GetKernelType
(
const
framework
::
ExecutionContext
&
ctx
,
const
framework
::
OperatorWithKernel
&
oper
,
const
framework
::
OperatorWithKernel
&
oper
,
...
@@ -217,32 +217,6 @@ Mish Activation Operator.
...
@@ -217,32 +217,6 @@ Mish Activation Operator.
}
}
};
};
class
HardSwishOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
void
Make
()
override
{
AddInput
(
"X"
,
"Input of HardSwish operator"
);
AddOutput
(
"Out"
,
"Output of HardSwish operator"
);
AddAttr
<
float
>
(
"threshold"
,
"The threshold parameter of HardSwish operator"
)
.
SetDefault
(
6.0
f
);
AddAttr
<
float
>
(
"scale"
,
"The scale parameter of HardSwish operator"
)
.
SetDefault
(
6.0
f
);
AddAttr
<
float
>
(
"offset"
,
"The offset parameter of HardSwish operator"
)
.
SetDefault
(
3.0
f
);
AddComment
(
R"DOC(
HardSwish Activation Operator.
The hard version of swish(https://arxiv.org/pdf/1905.02244.pdf).
$$out = \frac{x * (min(max(0, x+offset), threshold))}{scale}$$
The threshold and scale should be positive. The offset can be either positive or negative.
The default parameters are set according to the above reference.
It is recommended to use the defaults for this activation.
)DOC"
);
}
};
template
<
ActBwdOpFwdDeps
kDepValue
>
template
<
ActBwdOpFwdDeps
kDepValue
>
class
ActivationOpDoubleGrad
:
public
framework
::
OperatorWithKernel
{
class
ActivationOpDoubleGrad
:
public
framework
::
OperatorWithKernel
{
public:
public:
...
@@ -432,10 +406,6 @@ FOR_EACH_ACTIVATION_OP(REGISTER_ACTIVATION_OP);
...
@@ -432,10 +406,6 @@ FOR_EACH_ACTIVATION_OP(REGISTER_ACTIVATION_OP);
REGISTER_ACTIVATION_CPU_KERNEL
(
soft_relu
,
SoftRelu
)
REGISTER_ACTIVATION_CPU_KERNEL
(
soft_relu
,
SoftRelu
)
REGISTER_ACTIVATION_OP
(
mish
,
Mish
,
MishFunctor
,
MishGradFunctor
);
REGISTER_ACTIVATION_OP
(
mish
,
Mish
,
MishFunctor
,
MishGradFunctor
);
REGISTER_ACTIVATION_OP_WITH_COMP
(
hard_swish
,
HardSwish
,
HardSwishFunctor
,
HardSwishGradFunctor
);
REGISTER_ACTIVATION_OP
(
swish
,
Swish
,
SwishFunctor
,
SwishGradFunctor
);
REGISTER_ACTIVATION_OP
(
swish
,
Swish
,
SwishFunctor
,
SwishGradFunctor
);
/* ========================== register checkpoint ===========================*/
/* ========================== register checkpoint ===========================*/
...
...
paddle/phi/api/yaml/op_compat.yaml
浏览文件 @
06736921
...
@@ -1100,9 +1100,10 @@
...
@@ -1100,9 +1100,10 @@
x
:
X
x
:
X
outputs
:
outputs
:
out
:
Out
out
:
Out
backward
:
hard
_swish_grad
backward
:
hard
swish_grad (hard_swish_grad)
extra
:
extra
:
attrs
:
[
bool use_mkldnn = false
]
attrs
:
[
bool use_mkldnn = false
]
manual_signature
:
[
hardswish
]
-
op
:
hardtanh (brelu)
-
op
:
hardtanh (brelu)
backward
:
hardtanh_grad (brelu_grad)
backward
:
hardtanh_grad (brelu_grad)
...
...
paddle/phi/api/yaml/static_backward.yaml
浏览文件 @
06736921
...
@@ -43,6 +43,18 @@
...
@@ -43,6 +43,18 @@
func
:
frobenius_norm_grad
func
:
frobenius_norm_grad
param
:
[
x
,
out
,
out_grad
,
axis
,
keepdim
,
reduce_all
]
param
:
[
x
,
out
,
out_grad
,
axis
,
keepdim
,
reduce_all
]
-
backward_op
:
hardswish_grad
forward
:
hardswish (Tensor x, float threshold = 6.0f, float scale = 6.0f, float offset = 3.0f) -> Tensor(out)
args
:
(Tensor x, Tensor out_grad)
output
:
Tensor(x_grad)
infer_meta
:
func
:
UnchangedInferMeta
param
:
[
x
]
kernel
:
func
:
hardswish_grad
param
:
[
x
,
out_grad
]
inplace
:
(out_grad -> x_grad)
-
backward_op
:
relu6_grad
-
backward_op
:
relu6_grad
forward
:
relu6 (Tensor x, float threshold = 6.0f) -> Tensor(out)
forward
:
relu6 (Tensor x, float threshold = 6.0f) -> Tensor(out)
args
:
(Tensor out, Tensor out_grad)
args
:
(Tensor out, Tensor out_grad)
...
...
paddle/phi/api/yaml/static_ops.yaml
浏览文件 @
06736921
...
@@ -180,6 +180,17 @@
...
@@ -180,6 +180,17 @@
backend
:
x
backend
:
x
force_backend
:
force_cpu
force_backend
:
force_cpu
-
op
:
hardswish
args
:
(Tensor x, float threshold = 6.0f, float scale = 6.0f, float offset = 3.0f)
output
:
Tensor(out)
infer_meta
:
func
:
UnchangedInferMeta
param
:
[
x
]
kernel
:
func
:
hardswish
param
:
[
x
]
backward
:
hardswish_grad
-
op
:
less_equal
-
op
:
less_equal
args
:
(Tensor x, Tensor y, int axis = -1, bool force_cpu=false)
args
:
(Tensor x, Tensor y, int axis = -1, bool force_cpu=false)
output
:
Tensor(out)
output
:
Tensor(out)
...
...
paddle/phi/ops/compat/activation_sig.cc
浏览文件 @
06736921
...
@@ -47,11 +47,6 @@ KernelSignature SwishGradOpArgumentMapping(
...
@@ -47,11 +47,6 @@ KernelSignature SwishGradOpArgumentMapping(
return
KernelSignature
(
"swish_grad"
,
{
"X"
,
"Out@GRAD"
},
{},
{
"X@GRAD"
});
return
KernelSignature
(
"swish_grad"
,
{
"X"
,
"Out@GRAD"
},
{},
{
"X@GRAD"
});
}
}
KernelSignature
HardSwishGradOpArgumentMapping
(
const
ArgumentMappingContext
&
ctx
UNUSED
)
{
return
KernelSignature
(
"hardswish_grad"
,
{
"X"
,
"Out@GRAD"
},
{},
{
"X@GRAD"
});
}
KernelSignature
HardSwishOpArgumentMapping
(
KernelSignature
HardSwishOpArgumentMapping
(
const
ArgumentMappingContext
&
ctx
UNUSED
)
{
const
ArgumentMappingContext
&
ctx
UNUSED
)
{
return
KernelSignature
(
"hardswish"
,
{
"X"
},
{},
{
"Out"
});
return
KernelSignature
(
"hardswish"
,
{
"X"
},
{},
{
"Out"
});
...
@@ -65,12 +60,8 @@ KernelSignature SwishOpArgumentMapping(
...
@@ -65,12 +60,8 @@ KernelSignature SwishOpArgumentMapping(
}
// namespace phi
}
// namespace phi
PD_REGISTER_BASE_KERNEL_NAME
(
hard_swish
,
hardswish
);
PD_REGISTER_BASE_KERNEL_NAME
(
hard_swish
,
hardswish
);
PD_REGISTER_BASE_KERNEL_NAME
(
hard_swish_grad
,
hardswish_grad
);
PD_REGISTER_ARG_MAPPING_FN
(
mish_grad
,
phi
::
MishGradOpArgumentMapping
);
PD_REGISTER_ARG_MAPPING_FN
(
mish_grad
,
phi
::
MishGradOpArgumentMapping
);
PD_REGISTER_ARG_MAPPING_FN
(
hard_swish_grad
,
phi
::
HardSwishGradOpArgumentMapping
);
PD_REGISTER_ARG_MAPPING_FN
(
hard_swish
,
phi
::
HardSwishOpArgumentMapping
);
PD_REGISTER_ARG_MAPPING_FN
(
hard_swish
,
phi
::
HardSwishOpArgumentMapping
);
PD_REGISTER_ARG_MAPPING_FN
(
swish_grad
,
phi
::
SwishGradOpArgumentMapping
);
PD_REGISTER_ARG_MAPPING_FN
(
swish_grad
,
phi
::
SwishGradOpArgumentMapping
);
PD_REGISTER_ARG_MAPPING_FN
(
swish
,
phi
::
SwishOpArgumentMapping
);
PD_REGISTER_ARG_MAPPING_FN
(
swish
,
phi
::
SwishOpArgumentMapping
);
python/paddle/incubate/autograd/composite_rules.py
浏览文件 @
06736921
...
@@ -433,9 +433,9 @@ def hard_swish_composite(x):
...
@@ -433,9 +433,9 @@ def hard_swish_composite(x):
maxmum(x + offset, 0), threshold
maxmum(x + offset, 0), threshold
) * x / scale
) * x / scale
"""
"""
offset
=
3.0
threshold
=
6.0
threshold
=
6.0
scale
=
6.0
scale
=
6.0
offset
=
3.0
full_shape
=
x
.
shape
if
len
(
x
.
shape
)
==
0
else
[
1
]
full_shape
=
x
.
shape
if
len
(
x
.
shape
)
==
0
else
[
1
]
res
=
(
res
=
(
minimum
(
minimum
(
...
...
python/paddle/nn/functional/activation.py
浏览文件 @
06736921
...
@@ -393,10 +393,16 @@ def hardswish(x, name=None):
...
@@ -393,10 +393,16 @@ def hardswish(x, name=None):
x
,
'x'
,
[
'float16'
,
'uint16'
,
'float32'
,
'float64'
],
'hardswish'
x
,
'x'
,
[
'float16'
,
'uint16'
,
'float32'
,
'float64'
],
'hardswish'
)
)
threshold
=
6.0
scale
=
6.0
offset
=
3.0
helper
=
LayerHelper
(
'hardswish'
,
**
locals
())
helper
=
LayerHelper
(
'hardswish'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
helper
.
append_op
(
type
=
'hard_swish'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
}
type
=
'hard_swish'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
},
attrs
=
{
'threshold'
:
threshold
,
'scale'
:
scale
,
'offset'
:
offset
},
)
)
return
out
return
out
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录