From fb7b961aa022c67eeb4cd08a0799be1c6d6ddc8f Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Wed, 19 Jun 2024 11:29:05 +0800 Subject: [PATCH] [Typing][A-9] Add type annotations for `paddle/tensor/ops.py` (#65249) --- .../paddle/tensor/layer_function_generator.py | 13 ++- python/paddle/tensor/ops.py | 97 ++++++++++--------- 2 files changed, 60 insertions(+), 50 deletions(-) diff --git a/python/paddle/tensor/layer_function_generator.py b/python/paddle/tensor/layer_function_generator.py index 1efa6558a112f3..f7b84209cf6f52 100644 --- a/python/paddle/tensor/layer_function_generator.py +++ b/python/paddle/tensor/layer_function_generator.py @@ -11,8 +11,10 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations import re +from typing import TYPE_CHECKING from paddle import _C_ops, _legacy_C_ops @@ -27,6 +29,9 @@ in_dynamic_or_pir_mode, ) +if TYPE_CHECKING: + from paddle import Tensor + __all__ = [] @@ -46,7 +51,7 @@ def _convert_(name): return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() -def generate_layer_fn(op_type): +def generate_layer_fn(op_type: str): """Register the Python layer for an Operator. Args: @@ -124,7 +129,7 @@ def infer_and_check_dtype(op_proto, *args, **kwargs): dtype = core.VarDesc.VarType.FP32 return dtype - def func(*args, **kwargs): + def func(*args, **kwargs) -> Tensor: helper = LayerHelper(op_type, **kwargs) dtype = infer_and_check_dtype(op_proto, *args, **kwargs) @@ -160,7 +165,7 @@ def func(*args, **kwargs): return func -def generate_activation_fn(op_type): +def generate_activation_fn(op_type: str): """Register the Python layer for an Operator without Attribute. Args: @@ -171,7 +176,7 @@ def generate_activation_fn(op_type): """ - def func(x, name=None): + def func(x, name: str | None = None) -> Tensor: if in_dynamic_or_pir_mode(): if hasattr(_C_ops, op_type): op = getattr(_C_ops, op_type) diff --git a/python/paddle/tensor/ops.py b/python/paddle/tensor/ops.py index 812cab3fafe541..b28d46fea5aee9 100644 --- a/python/paddle/tensor/ops.py +++ b/python/paddle/tensor/ops.py @@ -11,7 +11,9 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations +from typing import TYPE_CHECKING from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only @@ -24,6 +26,9 @@ generate_layer_fn, ) +if TYPE_CHECKING: + from paddle import Tensor + __inplace_unary_func__ = [ 'exp_', 'sqrt_', @@ -64,7 +69,7 @@ globals()[_OP] = _func -def abs(x, name=None): +def abs(x: Tensor, name: str | None = None) -> Tensor: """ Perform elementwise abs for input `x`. @@ -74,7 +79,7 @@ def abs(x, name=None): Args: x (Tensor): The input Tensor with data type int32, int64, float16, float32 and float64. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor.A Tensor with the same data type and shape as :math:`x`. @@ -93,7 +98,7 @@ def abs(x, name=None): return generate_activation_fn('abs')(x, name) -def acos(x, name=None): +def acos(x: Tensor, name: str | None = None) -> Tensor: """ Acos Activation Operator. @@ -102,7 +107,7 @@ def acos(x, name=None): Args: x (Tensor): Input of Acos operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Acos operator, a Tensor with shape same as input. @@ -140,7 +145,7 @@ def acos(x, name=None): return out -def acosh(x, name=None): +def acosh(x: Tensor, name: str | None = None) -> Tensor: """ Acosh Activation Operator. @@ -149,7 +154,7 @@ def acosh(x, name=None): Args: x (Tensor): Input of Acosh operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Acosh operator, a Tensor with shape same as input. @@ -187,7 +192,7 @@ def acosh(x, name=None): return out -def asin(x, name=None): +def asin(x: Tensor, name: str | None = None) -> Tensor: """ Arcsine Operator. @@ -196,7 +201,7 @@ def asin(x, name=None): Args: x (Tensor): Input of Asin operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Same shape and dtype as input. @@ -234,7 +239,7 @@ def asin(x, name=None): return out -def asinh(x, name=None): +def asinh(x: Tensor, name: str | None = None) -> Tensor: """ Asinh Activation Operator. @@ -243,7 +248,7 @@ def asinh(x, name=None): Args: x (Tensor): Input of Asinh operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Asinh operator, a Tensor with shape same as input. @@ -281,7 +286,7 @@ def asinh(x, name=None): return out -def atan(x, name=None): +def atan(x: Tensor, name: str | None = None) -> Tensor: """ Arctangent Operator. @@ -290,7 +295,7 @@ def atan(x, name=None): Args: x (Tensor): Input of Atan operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Same shape and dtype as input x. @@ -328,7 +333,7 @@ def atan(x, name=None): return out -def atanh(x, name=None): +def atanh(x: Tensor, name: str | None = None) -> Tensor: """ Atanh Activation Operator. @@ -337,7 +342,7 @@ def atanh(x, name=None): Args: x (Tensor): Input of Atan operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Atanh operator, a Tensor with shape same as input. @@ -375,7 +380,7 @@ def atanh(x, name=None): return out -def ceil(x, name=None): +def ceil(x: Tensor, name: str | None = None) -> Tensor: """ Ceil Operator. Computes ceil of x element-wise. @@ -385,7 +390,7 @@ def ceil(x, name=None): Args: x (Tensor): Input of Ceil operator, an N-D Tensor, with data type float32, float64 or float16. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Ceil operator, a Tensor with shape same as input. @@ -413,7 +418,7 @@ def ceil(x, name=None): return out -def cos(x, name=None): +def cos(x: Tensor, name: str | None = None) -> Tensor: """ Cosine Operator. Computes cosine of x element-wise. @@ -424,7 +429,7 @@ def cos(x, name=None): Args: x (Tensor): Input of Cos operator, an N-D Tensor, with data type float32, float64 or float16. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Cos operator, a Tensor with shape same as input. @@ -455,7 +460,7 @@ def cos(x, name=None): return out -def cosh(x, name=None): +def cosh(x: Tensor, name: str | None = None) -> Tensor: """ Cosh Activation Operator. @@ -466,7 +471,7 @@ def cosh(x, name=None): Args: x (Tensor): Input of Cosh operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Cosh operator, a Tensor with shape same as input. @@ -504,7 +509,7 @@ def cosh(x, name=None): return out -def exp(x, name=None): +def exp(x: Tensor, name: str | None = None) -> Tensor: """ Computes exp of x element-wise with a natural number `e` as the base. @@ -514,7 +519,7 @@ def exp(x, name=None): Args: x (Tensor): Input of Exp operator, an N-D Tensor, with data type int32, int64, float16, float32, float64, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Exp operator, a Tensor with shape same as input. @@ -554,7 +559,7 @@ def exp(x, name=None): return out -def expm1(x, name=None): +def expm1(x: Tensor, name: str | None = None) -> Tensor: """ Expm1 Operator. Computes expm1 of x element-wise with a natural number :math:`e` as the base. @@ -564,7 +569,7 @@ def expm1(x, name=None): Args: x (Tensor): Input of Expm1 operator, an N-D Tensor, with data type int32, int64, float16, float32, float64, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Expm1 operator, a Tensor with shape same as input. @@ -604,7 +609,7 @@ def expm1(x, name=None): return out -def floor(x, name=None): +def floor(x: Tensor, name: str | None = None) -> Tensor: """ Floor Activation Operator. Computes floor of x element-wise. @@ -614,7 +619,7 @@ def floor(x, name=None): Args: x (Tensor): Input of Floor operator, an N-D Tensor, with data type float32, float64 or float16. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Floor operator, a Tensor with shape same as input. @@ -642,7 +647,7 @@ def floor(x, name=None): return out -def reciprocal(x, name=None): +def reciprocal(x: Tensor, name: str | None = None) -> Tensor: """ Reciprocal Activation Operator. @@ -652,7 +657,7 @@ def reciprocal(x, name=None): Args: x (Tensor): Input of Reciprocal operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Reciprocal operator, a Tensor with shape same as input. @@ -682,7 +687,7 @@ def reciprocal(x, name=None): return out -def round(x, name=None): +def round(x: Tensor, name: str | None = None) -> Tensor: """ Round the values in the input to the nearest integer value. @@ -699,7 +704,7 @@ def round(x, name=None): Args: x (Tensor): Input of Round operator, an N-D Tensor, with data type float32, float64 or float16. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Round operator, a Tensor with shape same as input. @@ -727,7 +732,7 @@ def round(x, name=None): return out -def rsqrt(x, name=None): +def rsqrt(x: Tensor, name: str | None = None) -> Tensor: """ Rsqrt Activation Operator. @@ -738,7 +743,7 @@ def rsqrt(x, name=None): Args: x (Tensor): Input of Rsqrt operator, an N-D Tensor, with data type float32, float64 or float16. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Rsqrt operator, a Tensor with shape same as input. @@ -766,7 +771,7 @@ def rsqrt(x, name=None): return out -def sigmoid(x, name=None): +def sigmoid(x: Tensor, name: str | None = None) -> Tensor: """ Sigmoid Activation. @@ -775,7 +780,7 @@ def sigmoid(x, name=None): Args: x (Tensor): Input of Sigmoid operator, an N-D Tensor, with data type float16, float32, float64, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Sigmoid operator, a Tensor with shape same as input. @@ -814,7 +819,7 @@ def sigmoid(x, name=None): return out -def sin(x, name=None): +def sin(x: Tensor, name: str | None = None) -> Tensor: """ Sine Activation Operator. @@ -823,7 +828,7 @@ def sin(x, name=None): Args: x (Tensor): Input of Sin operator, an N-D Tensor, with data type float32, float64 or float16. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Sin operator, a Tensor with shape same as input. @@ -861,7 +866,7 @@ def sin(x, name=None): return out -def sinh(x, name=None): +def sinh(x: Tensor, name: str | None = None) -> Tensor: """ Sinh Activation Operator. @@ -870,7 +875,7 @@ def sinh(x, name=None): Args: x (Tensor): Input of Sinh operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Sinh operator, a Tensor with shape same as input. @@ -908,7 +913,7 @@ def sinh(x, name=None): return out -def sqrt(x, name=None): +def sqrt(x: Tensor, name: str | None = None) -> Tensor: """ Sqrt Activation Operator. @@ -917,7 +922,7 @@ def sqrt(x, name=None): Args: x (Tensor): Input of Sqrt operator, an N-D Tensor, with data type float32, float64 or float16. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Sqrt operator, a Tensor with shape same as input. @@ -948,7 +953,7 @@ def sqrt(x, name=None): return out -def square(x, name=None): +def square(x: Tensor, name: str | None = None) -> Tensor: """ Square each elements of the inputs. @@ -957,7 +962,7 @@ def square(x, name=None): Args: x (Tensor): Input of Square operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Square operator, a Tensor with shape same as input. @@ -996,7 +1001,7 @@ def square(x, name=None): return out -def tan(x, name=None): +def tan(x: Tensor, name: str | None = None) -> Tensor: """ Tangent Operator. Computes tangent of x element-wise. @@ -1007,7 +1012,7 @@ def tan(x, name=None): Args: x (Tensor): Input of Tan operator, an N-D Tensor, with data type float32, float64 or float16. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor. Output of Tan operator, a Tensor with shape same as input. @@ -1045,7 +1050,7 @@ def tan(x, name=None): return out -def erf(x, name=None): +def erf(x: Tensor, name: str | None = None) -> Tensor: r""" The error function. For more details, see `Error function `_. @@ -1056,7 +1061,7 @@ def erf(x, name=None): Args: x (Tensor): The input tensor, it's data type should be float32, float64. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor: The output of Erf, dtype: float32 or float64, the same as the input, shape: the same as the input.