未验证 提交 51331098 编写于 作者: R Roc 提交者: GitHub

Comp hardswish (#51003)

* add composite op hard swish

* add test grad

* update apis calling

* update date range

* add ut

* tune off cinn for 0-d shape

* skip cinn
上级 b535d6ce
......@@ -2155,6 +2155,7 @@ class TestHardSwish(TestActivation):
self.op_type = 'hard_swish'
self.init_dtype()
self.init_shape()
self.prim_op_type = "comp"
self.python_api = paddle.nn.functional.hardswish
np.random.seed(1024)
......@@ -2170,18 +2171,42 @@ class TestHardSwish(TestActivation):
self.inputs = {'X': x}
self.attrs = {'threshold': threshold, 'scale': scale, 'offset': offset}
self.outputs = {'Out': out}
self.enable_cinn = False
def init_shape(self):
self.shape = [10, 12]
def test_check_grad(self):
self.check_grad(['X'], 'Out', check_eager=True)
self.check_grad(['X'], 'Out', check_eager=True, check_prim=True)
def test_check_output(self):
self.check_output(check_eager=True)
self.check_output(check_eager=True, check_prim=True)
class TestHardSwish_ZeroDim(TestHardSwish):
def setUp(self):
super().setUp()
self.enable_cinn = False
def init_shape(self):
self.shape = []
class TestHardSwishFP16(TestHardSwish):
def setUp(self):
super().setUp()
self.only_prim = True
self.enable_cinn = False
def init_dtype(self):
self.dtype = np.float16
class TestHardSwish_ZeroDim_FP16(TestHardSwishFP16):
def setUp(self):
super().setUp()
self.enable_cinn = False
def init_shape(self):
self.shape = []
......
......@@ -261,6 +261,31 @@ def bernoulli(shape, dtype, p, seed=0):
)
@REGISTER_COMPOSITE('hard_swish')
def hard_swish_composite(x):
"""define composite rule of op hard_swish.
offset=3, threshold=6, scale=6
out = minimum(
maxmum(x + offset, 0), threshold
) * x / scale
"""
offset = 3.0
threshold = 6.0
scale = 6.0
res = (
minimum(
maximum(
x + full(x.shape, offset, dtype=x.dtype),
full(x.shape, 0.0, dtype=x.dtype),
),
full(x.shape, threshold, dtype=x.dtype),
)
* x
/ full(x.shape, scale, dtype=x.dtype)
)
return res
@REGISTER_COMPOSITE('silu')
def silu_composite(x):
"""
......
......@@ -60,6 +60,8 @@ from paddle.tensor import uniform # noqa: F401
from paddle.tensor import zeros # noqa: F401
from paddle.tensor.creation import assign # noqa: F401
from paddle.tensor.manipulation import cast # noqa: F401
from paddle.tensor.math import maximum # noqa: F401
from paddle.tensor.math import minimum # noqa: F401
"""
math_op = [
......@@ -87,6 +89,8 @@ math_op = [
'logit',
'max',
'min',
'minimum',
'maximum'
]
trigonometric_op = [
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册