From 42e47f03f0eeb3202990e4ad1e9d34d62ae5cb37 Mon Sep 17 00:00:00 2001 From: fangfangssj <1135470306@qq.com> Date: Wed, 29 Oct 2025 15:54:04 +0000 Subject: [PATCH 1/4] sink tanh --- paddle/phi/ops/yaml/python_api_info.yaml | 6 +++ python/paddle/_paddle_docs.py | 40 +++++++++++++++++ python/paddle/tensor/math.py | 55 +----------------------- 3 files changed, 47 insertions(+), 54 deletions(-) diff --git a/paddle/phi/ops/yaml/python_api_info.yaml b/paddle/phi/ops/yaml/python_api_info.yaml index 8855abbc7e2a14..e7e105a75050a0 100644 --- a/paddle/phi/ops/yaml/python_api_info.yaml +++ b/paddle/phi/ops/yaml/python_api_info.yaml @@ -184,6 +184,7 @@ x : [input] args_mapper : func : GeluMapper + - op : sum name : [paddle.sum, paddle.Tensor.sum] args_alias: @@ -192,3 +193,8 @@ func : SumPreProcess(x, axis) args_mapper : func : ArgSumMapper + +- op : tanh + name : [paddle.tanh, paddle.Tensor.tanh, paddle.nn.functional.tanh] + args_alias: + use_default_mapping : True diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index 4c9efab1645a3b..c9b21b93ca952d 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -2245,6 +2245,46 @@ def dot( """, ) +add_doc_and_signature( + "tanh", + r""" + Tanh Activation Operator. + + .. math:: + out = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} + + .. note:: + Alias Support: + 1. The parameter name ``input`` can be used as an alias for ``x``. + + Args: + x (Tensor): Input of Tanh operator, an N-D Tensor, with data type bfloat16, float32, float64, + float16, uint8, int8, int16, int32, int64. Alias: ``input``. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + out (Tensor|None, optional): The output tensor. + + Returns: + Output of Tanh operator, a Tensor with same data type and shape as input + (integer types are autocasted into float32). + + Examples: + + .. code-block:: python + + >>> import paddle + + >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3]) + >>> out = paddle.tanh(x) + >>> out + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [-0.37994900, -0.19737528, 0.09966799, 0.29131261]) +""", + """ +def tanh( + x: Tensor, name: str | None = None, *, out: Tensor | None = None +) -> Tensor +""", +) # lubingxin # chenhuangrun diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 64bcdd4efa288b..fdd6e893663309 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -40,6 +40,7 @@ sign, sin, sum, + tanh, ) from paddle.base.libpaddle import DataType from paddle.common_ops_import import VarDesc, dygraph_utils @@ -4454,60 +4455,6 @@ def prod( return out -def tanh(x: Tensor, name: str | None = None) -> Tensor: - r""" - Tanh Activation Operator. - - .. math:: - out = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} - - Args: - x (Tensor): Input of Tanh operator, an N-D Tensor, with data type bfloat16, float32, float64, - float16, uint8, int8, int16, int32, int64. - name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. - - Returns: - Output of Tanh operator, a Tensor with same data type and shape as input - (integer types are autocasted into float32). - - Examples: - - .. code-block:: python - - >>> import paddle - - >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3]) - >>> out = paddle.tanh(x) - >>> out - Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, - [-0.37994900, -0.19737528, 0.09966799, 0.29131261]) - """ - if in_dynamic_or_pir_mode(): - return _C_ops.tanh(x) - else: - check_variable_and_dtype( - x, - 'x', - [ - 'uint16', - 'float16', - 'float32', - 'float64', - 'uint8', - 'int8', - 'int16', - 'int32', - 'int64', - ], - 'tanh', - ) - check_type(x, 'x', (Variable), 'tanh') - helper = LayerHelper('tanh', **locals()) - out = helper.create_variable_for_type_inference(x.dtype) - helper.append_op(type='tanh', inputs={'X': x}, outputs={'Out': out}) - return out - - @inplace_apis_in_dygraph_only def tanh_(x: Tensor, name: str | None = None) -> Tensor: r""" From 50e2a892fa7acd77ab0b1f94f04da2300935d247 Mon Sep 17 00:00:00 2001 From: fangfangssj <1135470306@qq.com> Date: Fri, 31 Oct 2025 03:44:47 +0000 Subject: [PATCH 2/4] fix --- test/standalone_executor/test_standalone_custom_event.py | 2 ++ test/standalone_executor/test_standalone_custom_stream.py | 2 ++ test/standalone_executor/test_standalone_executor.py | 1 + 3 files changed, 5 insertions(+) diff --git a/test/standalone_executor/test_standalone_custom_event.py b/test/standalone_executor/test_standalone_custom_event.py index bf629dac44fd92..b6cd92583e580b 100644 --- a/test/standalone_executor/test_standalone_custom_event.py +++ b/test/standalone_executor/test_standalone_custom_event.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import unittest import paddle @@ -23,6 +24,7 @@ split_program, ) +os.environ['FLAGS_enable_pir_api'] = '1' paddle.enable_static() diff --git a/test/standalone_executor/test_standalone_custom_stream.py b/test/standalone_executor/test_standalone_custom_stream.py index 50da25fc1ffe27..1bfc8b1227b0a7 100644 --- a/test/standalone_executor/test_standalone_custom_stream.py +++ b/test/standalone_executor/test_standalone_custom_stream.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import sys import unittest @@ -22,6 +23,7 @@ import paddle from paddle.base import core +os.environ['FLAGS_enable_pir_api'] = '1' paddle.enable_static() diff --git a/test/standalone_executor/test_standalone_executor.py b/test/standalone_executor/test_standalone_executor.py index ae2c766f28e717..4503e763219c70 100644 --- a/test/standalone_executor/test_standalone_executor.py +++ b/test/standalone_executor/test_standalone_executor.py @@ -26,6 +26,7 @@ from paddle.base import core from paddle.profiler import profiler +os.environ['FLAGS_enable_pir_api'] = '1' paddle.enable_static() From af506b946b4bda2d6c2ceed163a1ac6b39abda51 Mon Sep 17 00:00:00 2001 From: fangfangssj <1135470306@qq.com> Date: Wed, 5 Nov 2025 15:12:21 +0000 Subject: [PATCH 3/4] fix Ut --- python/paddle/_paddle_docs.py | 31 ++++++++++++++----- test/legacy_test/test_activation_op.py | 1 + .../test_standalone_custom_event.py | 14 ++++----- .../test_standalone_custom_stream.py | 6 ++-- .../test_standalone_executor.py | 9 +++--- 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index 6ea761cc326704..ea0e1664954bbd 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -2317,23 +2317,31 @@ def dot( add_doc_and_signature( "tanh", r""" + Tanh Activation Operator. + .. math:: out = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} + .. note:: Alias Support: 1. The parameter name ``input`` can be used as an alias for ``x``. + Args: x (Tensor): Input of Tanh operator, an N-D Tensor, with data type bfloat16, float32, float64, float16, uint8, int8, int16, int32, int64. Alias: ``input``. name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. - out (Tensor|None, optional): The output tensor. + out (Tensor|None, optional): The output tensor. Default: None. + Returns: Output of Tanh operator, a Tensor with same data type and shape as input (integer types are autocasted into float32). + Examples: .. code-block:: python + >>> import paddle + >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3]) >>> out = paddle.tanh(x) >>> out @@ -2342,7 +2350,7 @@ def dot( """, """ def tanh( - x: Tensor, name: str | None = None, *, out: Tensor | None = None + x: Tensor, *, out: Tensor | None = None, name: str | None = None, ) -> Tensor """, ) @@ -2364,7 +2372,7 @@ def tanh( x (Tensor): Input of Exp operator, an N-D Tensor, with data type int32, int64, bfloat16, float16, float32, float64, complex64 or complex128. Alias: ``input``. name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. - out (Tensor|None, optional): The output tensor. + out (Tensor|None, optional): The output tensor. Default: None. Returns: Tensor. Output of Exp operator, a Tensor with shape same as input. @@ -2404,7 +2412,7 @@ def exp( x (Tensor): Input of Expm1 operator, an N-D Tensor, with data type int32, int64, bfloat16, float16, float32, float64, complex64 or complex128. Alias: ``input``. name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. - out (Tensor|None, optional): The output tensor. + out (Tensor|None, optional): The output tensor. Default: None. Returns: Tensor. Output of Expm1 operator, a Tensor with shape same as input. @@ -2527,10 +2535,16 @@ def diagonal( out.shape = [4] out.data = [1., -1., 3., 1.] + .. note:: + Alias Support: + 1. The parameter name ``input`` can be used as an alias for ``x``. + Args: x (Tensor): Input of Round operator, an N-D Tensor, with data type bfloat16, int32, int64, float32, float64, float16, complex64 or complex128. + Alias: ``input``. decimals(int): Rounded decimal place (default: 0). name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + out (Tensor|None, optional): The output tensor. Default: None. Returns: Tensor. Output of Round operator, a Tensor with shape same as input. @@ -2562,12 +2576,15 @@ def round( out = |x| + .. note:: + Alias Support: + 1. The parameter name ``input`` can be used as an alias for ``x``. + Args: x (Tensor): The input Tensor with data type int32, int64, float16, float32, float64, complex64 and complex128. + Alias: ``input``. name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. - - Keyword args: - out (Tensor|None, optional): The output tensor. + out (Tensor|None, optional): The output tensor. Default: None. Returns: Tensor.A Tensor with the same data type and shape as :math:`x`. diff --git a/test/legacy_test/test_activation_op.py b/test/legacy_test/test_activation_op.py index e85d7358fca7c5..7cf5bf22ada927 100644 --- a/test/legacy_test/test_activation_op.py +++ b/test/legacy_test/test_activation_op.py @@ -6167,6 +6167,7 @@ class TestActivationAPI_Compatibility(unittest.TestCase): ("paddle.exp", np.exp, {'min_val': -1.0, 'max_val': 1.0}), ("paddle.expm1", np.expm1, {'min_val': -1.0, 'max_val': 1.0}), ("paddle.round", np.round, {'min_val': -5.0, 'max_val': 5.0}), + ("paddle.tanh", np.tanh, {'min_val': -1.0, 'max_val': 1.0}), ] def setUp(self): diff --git a/test/standalone_executor/test_standalone_custom_event.py b/test/standalone_executor/test_standalone_custom_event.py index b6cd92583e580b..61eb5feb1ebf07 100644 --- a/test/standalone_executor/test_standalone_custom_event.py +++ b/test/standalone_executor/test_standalone_custom_event.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import paddle @@ -24,7 +23,6 @@ split_program, ) -os.environ['FLAGS_enable_pir_api'] = '1' paddle.enable_static() @@ -42,12 +40,12 @@ def build_program(): matmul_out = data @ weight bias = paddle.ones([1024, 2048], dtype='float32', name='bias') add_out = paddle.add(matmul_out, bias, name='add_out') - # add_out -> [sub] -> sub_out -> [tanh] -> tanh_out + # add_out -> [sub] -> sub_out -> [silu] -> silu_out sub_out = paddle.subtract(add_out, data, name='sub_out') - tanh_out = paddle.tanh(sub_out, name='tanh_out') + silu_out = paddle.nn.functional.silu(sub_out, name='silu_out') bias_1 = paddle.add(bias, sub_out, name='bias_1') - out_before = paddle.tanh(bias_1, name='out_before') - out_last = paddle.subtract(tanh_out, data, name='out_last') + out_before = paddle.nn.functional.silu(bias_1, name='out_before') + out_last = paddle.subtract(silu_out, data, name='out_last') out_last2 = out_last @ weight out = paddle.add(out_before, out_last2, name='out') @@ -66,9 +64,9 @@ class TestManualEvent(unittest.TestCase): | | | | | elementwise_sub(s1) | | | | | - | tanh(s1) elementwise_add(s1) + | silu(s1) elementwise_add(s1) | | | - elementwise_sub(s1) tanh(s1) + elementwise_sub(s1) silu(s1) | | matmul_v2(s1) | | | ---split prog---- diff --git a/test/standalone_executor/test_standalone_custom_stream.py b/test/standalone_executor/test_standalone_custom_stream.py index 1bfc8b1227b0a7..86b53989c26ee8 100644 --- a/test/standalone_executor/test_standalone_custom_stream.py +++ b/test/standalone_executor/test_standalone_custom_stream.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import sys import unittest @@ -23,7 +22,6 @@ import paddle from paddle.base import core -os.environ['FLAGS_enable_pir_api'] = '1' paddle.enable_static() @@ -37,9 +35,9 @@ class TestCustomStream(unittest.TestCase): | | | | | elementwise_sub(cpu) | | | | | - | tanh(cpu) elementwise_add(s2) + | silu(cpu) elementwise_add(s2) | | | - elementwise_sub(s1) tanh(s2) + elementwise_sub(s1) silu(s2) | | elementwise_add(s2) | diff --git a/test/standalone_executor/test_standalone_executor.py b/test/standalone_executor/test_standalone_executor.py index 4503e763219c70..478e100fecdd56 100644 --- a/test/standalone_executor/test_standalone_executor.py +++ b/test/standalone_executor/test_standalone_executor.py @@ -26,7 +26,6 @@ from paddle.base import core from paddle.profiler import profiler -os.environ['FLAGS_enable_pir_api'] = '1' paddle.enable_static() @@ -45,15 +44,15 @@ def build_program(): bias = paddle.ones([4, 64], dtype='float32', name='bias') add_out = paddle.add(matmul_out, bias, name='add_out') - # add_out -> [memcpy_d2h] -> add_out' -> [sub] -> sub_out -> [tanh] -> tanh_out + # add_out -> [memcpy_d2h] -> add_out' -> [sub] -> sub_out -> [silu] -> silu_out with paddle.static.device_guard('cpu'): sub_out = paddle.subtract(add_out, data, name='sub_out') - tanh_out = paddle.tanh(sub_out, name='tanh_out') + silu_out = paddle.nn.functional.silu(sub_out, name='silu_out') with paddle.static.device_guard('gpu'): bias_1 = paddle.add(bias, sub_out, name='bias_1') - out_before = paddle.tanh(bias_1, name='out_before') - out_last = paddle.subtract(tanh_out, data, name='out_last') + out_before = paddle.nn.functional.silu(bias_1, name='out_before') + out_last = paddle.subtract(silu_out, data, name='out_last') out = paddle.add(out_before, out_last, name='out') mean = paddle.mean(out, name='mean_out') From e959587d9da4703eafe97cd0bb203d4a165c487c Mon Sep 17 00:00:00 2001 From: fangfangssj <1135470306@qq.com> Date: Thu, 6 Nov 2025 06:31:40 +0000 Subject: [PATCH 4/4] fix win UT --- test/legacy_test/test_weight_decay.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/legacy_test/test_weight_decay.py b/test/legacy_test/test_weight_decay.py index 27c0efea3c81a8..a0d48f13eeeeee 100644 --- a/test/legacy_test/test_weight_decay.py +++ b/test/legacy_test/test_weight_decay.py @@ -62,9 +62,9 @@ def bow_net( bow = paddle.static.nn.sequence_lod.sequence_pool( input=emb, pool_type='sum' ) - bow_tanh = paddle.tanh(bow) - fc_1 = paddle.static.nn.fc(x=bow_tanh, size=hid_dim, activation="tanh") - fc_2 = paddle.static.nn.fc(x=fc_1, size=hid_dim2, activation="tanh") + bow_silu = paddle.nn.functional.silu(bow) + fc_1 = paddle.static.nn.fc(x=bow_silu, size=hid_dim, activation="silu") + fc_2 = paddle.static.nn.fc(x=fc_1, size=hid_dim2, activation="silu") prediction = paddle.static.nn.fc( x=[fc_2], size=class_dim, activation="softmax" )