From d5065878201d7d07ed4b0916edf95adc43299c21 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Wed, 12 Jun 2024 12:31:12 +0000 Subject: [PATCH 01/16] p1 --- python/paddle/base/data_feeder.py | 3 +- python/paddle/tensor/creation.py | 233 ++++++++++++++++++++---------- 2 files changed, 160 insertions(+), 76 deletions(-) diff --git a/python/paddle/base/data_feeder.py b/python/paddle/base/data_feeder.py index c914c79202287..248120a637af2 100644 --- a/python/paddle/base/data_feeder.py +++ b/python/paddle/base/data_feeder.py @@ -17,6 +17,7 @@ import numpy as np from paddle import pir +from paddle._typing.dtype_like import DTypeLike from ..pir import Value from ..pir.core import _PADDLE_PIR_DTYPE_2_NUMPY_DTYPE, ParameterMeta @@ -89,7 +90,7 @@ def convert_uint16_to_float(data): return np.reshape(new_data, data.shape) -def convert_dtype(dtype): +def convert_dtype(dtype: DTypeLike) -> str: if isinstance(dtype, core.VarDesc.VarType): if dtype in _PADDLE_DTYPE_2_NUMPY_DTYPE: return _PADDLE_DTYPE_2_NUMPY_DTYPE[dtype] diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 6ef53a757ee23..f11e37ee456e4 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -25,6 +25,7 @@ DTypeLike, NestedNumbericSequence, PlaceLike, + ShapeLike, TensorLike, ) from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only @@ -54,7 +55,7 @@ __all__ = [] -def _complex_to_real_dtype(dtype): +def _complex_to_real_dtype(dtype: DTypeLike) -> DTypeLike: if dtype == core.VarDesc.VarType.COMPLEX64: return core.VarDesc.VarType.FP32 elif dtype == core.VarDesc.VarType.COMPLEX128: @@ -67,7 +68,7 @@ def _complex_to_real_dtype(dtype): return dtype -def _real_to_complex_dtype(dtype): +def _real_to_complex_dtype(dtype: DTypeLike) -> DTypeLike: if dtype == core.VarDesc.VarType.FP32: return core.VarDesc.VarType.COMPLEX64 elif dtype == core.VarDesc.VarType.FP64: @@ -81,13 +82,18 @@ def _real_to_complex_dtype(dtype): def create_global_var( - shape, value, dtype, persistable=False, force_cpu=False, name=None -): + shape: ShapeLike, + value: float, + dtype: DTypeLike, + persistable: bool = False, + force_cpu: bool = False, + name: str | None = None, +) -> paddle.Tensor: """ This function creates a new tensor variable with value in the global block(block 0). Args: - shape (list[int]|tuple[int]): Shape of the variable + shape (ShapeLike): Shape of the variable value (float): The value of the variable. The new created variable will be filled with it. dtype (str): Data type of the variable @@ -95,7 +101,7 @@ def create_global_var( Default: False force_cpu (bool, optional): Force this variable to be on CPU. Default: False - name (str, optional): For detailed information, please refer to + name(str|None, optional): For detailed information, please refer to :ref:`api_guide_Name` . Usually name is no need to set and None by default. Returns: @@ -162,8 +168,13 @@ def create_global_var( def create_parameter( - shape, dtype, name=None, attr=None, is_bias=False, default_initializer=None -): + shape: ShapeLike, + dtype: DTypeLike, + name: str | None = None, + attr: ParamAttr | None = None, + is_bias: bool = False, + default_initializer: paddle.nn.initializer.Initializer | None = None, +) -> paddle.Tensor: """ This function creates a parameter. The parameter is a learnable variable, which can have gradient, and can be optimized. @@ -174,15 +185,15 @@ def create_parameter( Args: shape (list of int): Shape of the parameter dtype (str): Data type of the parameter. It can be set as 'float16', 'float32', 'float64'. - name (str, optional): For detailed information, please refer to + name(str|None, optional): For detailed information, please refer to :ref:`api_guide_Name` . Usually name is no need to set and None by default. - attr (ParamAttr, optional): Attribute object of the specified argument. For detailed information, please refer to + attr (ParamAttr|None, optional): Attribute object of the specified argument. For detailed information, please refer to :ref:`api_paddle_ParamAttr` None by default, which means that ParamAttr will be initialized as it is. is_bias (bool, optional): This can affect which default initializer is chosen when default_initializer is None. If is_bias, initializer.Constant(0.0) will be used. Otherwise, Xavier() will be used. - default_initializer (Initializer, optional): Initializer for the parameter + default_initializer (Initializer|None, optional): Initializer for the parameter Returns: The created parameter. @@ -243,7 +254,9 @@ def create_parameter( ) -def create_tensor(dtype, name=None, persistable=False): +def create_tensor( + dtype: DTypeLike, name: str | None = None, persistable: bool = False +) -> paddle.Tensor: """ Create a variable, which will hold a Tensor with data type dtype. @@ -285,7 +298,13 @@ def create_tensor(dtype, name=None, persistable=False): ) -def linspace(start, stop, num, dtype=None, name=None): +def linspace( + start: float | paddle.Tensor, + stop: float | paddle.Tensor, + num: int | paddle.Tensor, + dtype: DTypeLike | None = None, + name: str | None = None, +) -> paddle.Tensor: r""" Return fixed number of evenly spaced values within a given interval. Note: no gradient calculation is performed. @@ -298,7 +317,7 @@ def linspace(start, stop, num, dtype=None, name=None): or a 0-D Tensor with data type int32. dtype(np.dtype|str, optional): The data type of output tensor, it could be int32, int64, float32 and float64. Default: if None, the data type is float32. - name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: the output data type will be float32, float64. The 1-D tensor with fixed number of evenly spaced values, \ @@ -405,7 +424,14 @@ def linspace(start, stop, num, dtype=None, name=None): return out -def logspace(start, stop, num, base=10.0, dtype=None, name=None): +def logspace( + start: float | paddle.Tensor, + stop: float | paddle.Tensor, + num: int | paddle.Tensor, + base: float | paddle.Tensor = 10.0, + dtype: DTypeLike | None = None, + name: str | None = None, +) -> paddle.Tensor: r""" Return fixed number of logarithmical-evenly spaced values within the interval \ :math:`[base^{start}, base^{stop}]`. @@ -427,7 +453,7 @@ def logspace(start, stop, num, base=10.0, dtype=None, name=None): float32 or float64. dtype(np.dtype|str, optional): The data type of output tensor, it could be \ int32, int64, float32 or float64. Default: if None, the data type is float32. \ - name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: The output data type will be float32, float64. The 1-D tensor with \ @@ -558,14 +584,21 @@ def logspace(start, stop, num, base=10.0, dtype=None, name=None): return out -def _to_tensor_non_static(data, dtype=None, place=None, stop_gradient=True): - def _handle_tensor_dtype(tensor, dtype): +def _to_tensor_non_static( + data: TensorLike, + dtype: DTypeLike | None = None, + place: PlaceLike | None = None, + stop_gradient: bool = True, +) -> paddle.Tensor: + def _handle_tensor_dtype( + tensor: paddle.Tensor, dtype: DTypeLike + ) -> paddle.Tensor: if dtype: if convert_dtype(dtype) != convert_dtype(tensor.dtype): return tensor.astype(convert_dtype(dtype)) return tensor - def _handle_np_dtype(ndarray, dtype): + def _handle_np_dtype(ndarray: np.ndarray, dtype: DTypeLike) -> np.ndarray: if dtype: if convert_dtype(dtype) != convert_dtype(ndarray.dtype): # should not ndarray.astype('uint16') directly, data bits is wrong @@ -658,7 +691,11 @@ def _handle_np_dtype(ndarray, dtype): ) -def _to_tensor_static(data, dtype=None, stop_gradient=None): +def _to_tensor_static( + data: TensorLike, + dtype: DTypeLike | None = None, + stop_gradient: bool = True, +) -> paddle.Tensor: if isinstance(data, (Variable, paddle.pir.Value)): output = data if dtype is not None and dtype != data.dtype: @@ -813,7 +850,12 @@ def to_tensor( return _to_tensor_static(data, dtype, stop_gradient) -def full_like(x, fill_value, dtype=None, name=None): +def full_like( + x: paddle.Tensor, + fill_value: bool | float, + dtype: DTypeLike | None = None, + name: str | None = None, +) -> paddle.Tensor: """ This function creates a tensor filled with ``fill_value`` which has identical shape of ``x`` and ``dtype``. @@ -825,7 +867,7 @@ def full_like(x, fill_value, dtype=None, name=None): dtype(np.dtype|str, optional): The data type of output. The data type can be one of bool, float16, float32, float64, int32, int64. The default value is None, which means the output data type is the same as input. - name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: Tensor which is created according to ``x``, ``fill_value`` and ``dtype``. @@ -896,7 +938,14 @@ def full_like(x, fill_value, dtype=None, name=None): return out -def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None): +def fill_constant( + shape: ShapeLike, + dtype: DTypeLike, + value: float | paddle.Tensor, + force_cpu: bool = False, + out: paddle.Tensor | None = None, + name: str | None = None, +) -> paddle.Tensor: if in_dynamic_or_pir_mode(): place = _current_expected_place() if force_cpu: @@ -926,11 +975,9 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None): out = _C_ops.full(shape, value, dtype, place) out.stop_gradient = True return out - - if out is not None: - _C_ops.full_(out, shape, value, dtype, place) - out.stop_gradient = True - return out + _C_ops.full_(out, shape, value, dtype, place) + out.stop_gradient = True + return out else: attrs = {'force_cpu': force_cpu} @@ -996,7 +1043,9 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None): return out -def ones(shape, dtype=None, name=None): +def ones( + shape: ShapeLike, dtype: DTypeLike | None = None, name: str | None = None +) -> paddle.Tensor: """ Create a Tensor of specified :attr:`shape` and :attr:`dtype` and fill it with 1. @@ -1006,7 +1055,7 @@ def ones(shape, dtype=None, name=None): If ``shape`` is an Tensor, it should be an 1-D Tensor which represents a list. dtype (np.dtype|str, optional): Data type of output Tensor, it should be one of bool, float16, float32, float64, int32 and int64. If it is set to None, the data type will be float32. - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: A Tensor of data type :attr:`dtype` with shape :attr:`shape` and all elements are 1. @@ -1044,7 +1093,9 @@ def ones(shape, dtype=None, name=None): return fill_constant(value=1.0, shape=shape, dtype=dtype, name=name) -def ones_like(x, dtype=None, name=None): +def ones_like( + x: paddle.Tensor, dtype: DTypeLike | None = None, name: str | None = None +) -> paddle.Tensor: """ Returns a Tensor filled with the value 1, with the same shape and data type (use ``dtype`` if ``dtype`` is not None) as ``x``. @@ -1056,7 +1107,7 @@ def ones_like(x, dtype=None, name=None): output tensor. Supported data types: bool, float16, float32, float64, int32, int64. If ``dtype`` is None, the data type is the same as ``x``. Default is None. - name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: A Tensor filled with the value 1, with the same shape and @@ -1079,7 +1130,9 @@ def ones_like(x, dtype=None, name=None): return full_like(x=x, fill_value=1, dtype=dtype, name=name) -def zeros(shape, dtype=None, name=None): +def zeros( + shape: ShapeLike, dtype: DTypeLike | None = None, name: str | None = None +) -> paddle.Tensor: """ Creates a tensor of specified :attr:`shape` and :attr:`dtype`, and fills it with 0. @@ -1089,7 +1142,7 @@ def zeros(shape, dtype=None, name=None): If ``shape`` is an Tensor, it should be an 1-D Tensor which represents a list. dtype(np.dtype|str, optional): Data type of output Tensor, it supports bool, float16, float32, float64, int32 and int64. Default: if None, the data type is float32. - name(str, optional): The default value is None. Normally there is no need for user to set this + name(str|None, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -1128,7 +1181,9 @@ def zeros(shape, dtype=None, name=None): return fill_constant(value=0.0, shape=shape, dtype=dtype, name=name) -def zeros_like(x, dtype=None, name=None): +def zeros_like( + x: paddle.Tensor, dtype: DTypeLike | None = None, name: str | None = None +) -> paddle.Tensor: """ Returns a Tensor filled with the value 0, with the same shape and data type (use ``dtype`` if ``dtype`` is not None) as ``x``. @@ -1140,7 +1195,7 @@ def zeros_like(x, dtype=None, name=None): output tensor. Supported data types: bool, float16, float32, float64, int32, int64. If ``dtype`` is None, the data type is the same as ``x``. Default is None. - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: A Tensor filled with the value 0, with the same shape and @@ -1164,19 +1219,24 @@ def zeros_like(x, dtype=None, name=None): return full_like(x=x, fill_value=0, dtype=dtype, name=name) -def eye(num_rows, num_columns=None, dtype=None, name=None): +def eye( + num_rows: int, + num_columns: int | None = None, + dtype: DTypeLike | None = None, + name: str | None = None, +) -> paddle.Tensor: """ This function constructs 2-D Tensor with ones on the diagonal and zeros elsewhere. Args: num_rows(int): the number of rows in each batch Tensor. - num_columns(int, optional): the number of columns in each batch Tensor. + num_columns(int|None, optional): the number of columns in each batch Tensor. If None, default: num_rows. dtype(np.dtype|str, optional): The data type of the returned Tensor. It should be int32, int64, float16, float32, float64, complex64, complex128. Default: if None, the data type is float32. - name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: An identity Tensor or LoDTensor of shape [num_rows, num_columns]. @@ -1252,7 +1312,12 @@ def _check_attr(attr, message): return out -def full(shape, fill_value, dtype=None, name=None): +def full( + shape: ShapeLike, + fill_value: bool | float | paddle.Tensor, + dtype: DTypeLike | None = None, + name: str | None = None, +) -> paddle.Tensor: """ Return a Tensor with the ``fill_value`` which size is same as ``shape``. @@ -1266,7 +1331,7 @@ def full(shape, fill_value, dtype=None, name=None): dtype(np.dtype|str, optional): Data type of the output Tensor which can be float16, float32, float64, int32, int64, if dtype is `None`, the data type of created Tensor is `float32`. - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: Tensor which is created according to ``shape``, ``fill_value`` and ``dtype``. @@ -1314,7 +1379,13 @@ def full(shape, fill_value, dtype=None, name=None): return fill_constant(shape=shape, dtype=dtype, value=fill_value, name=name) -def arange(start=0, end=None, step=1, dtype=None, name=None): +def arange( + start: float | paddle.Tensor = 0, + end: float | paddle.Tensor | None = None, + step: float | paddle.Tensor = 1, + dtype: DTypeLike | None = None, + name: str | None = None, +): """ Returns a 1-D Tensor with spaced values within a given interval. @@ -1341,7 +1412,7 @@ def arange(start=0, end=None, step=1, dtype=None, name=None): dtype(str|np.dtype, optional): The data type of the output tensor. Supported data types: int32, int64, float32, float64. If ``dtype`` is None, the data type is float32. Default is None. - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: A 1-D Tensor with values from the interval [``start``, ``end``) @@ -1496,7 +1567,7 @@ def _tril_triu_op(helper): return out -def tril(x, diagonal=0, name=None): +def tril(x, diagonal=0, name: str | None = None): r""" Returns the lower triangular part of a matrix (2-D tensor) or batch of matrices :attr:`x`, the other elements of the result tensor are set @@ -1513,7 +1584,7 @@ def tril(x, diagonal=0, name=None): the main diagonal. The main diagonal are the set of indices :math:`\{(i, i)\}` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]` where :math:`d_{1}, d_{2}` are the dimensions of the matrix. - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: Results of lower triangular operation by the specified diagonal of input tensor x, @@ -1561,7 +1632,7 @@ def tril(x, diagonal=0, name=None): @inplace_apis_in_dygraph_only -def tril_(x, diagonal=0, name=None): +def tril_(x, diagonal=0, name: str | None = None): r""" Inplace version of ``tril`` API, the output Tensor will be inplaced with input ``x``. Please refer to :ref:`api_paddle_tril`. @@ -1571,7 +1642,7 @@ def tril_(x, diagonal=0, name=None): return _C_ops.tril_(x, diagonal) -def triu(x, diagonal=0, name=None): +def triu(x, diagonal=0, name: str | None = None): r""" Return the upper triangular part of a matrix (2-D tensor) or batch of matrices :attr:`x`, the other elements of the result tensor are set to 0. @@ -1588,7 +1659,7 @@ def triu(x, diagonal=0, name=None): the main diagonal. The main diagonal are the set of indices :math:`\{(i, i)\}` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]` where :math:`d_{1}, d_{2}` are the dimensions of the matrix. - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: Results of upper triangular operation by the specified diagonal of input tensor x, @@ -1638,7 +1709,7 @@ def triu(x, diagonal=0, name=None): @inplace_apis_in_dygraph_only -def triu_(x, diagonal=0, name=None): +def triu_(x, diagonal=0, name: str | None = None): r""" Inplace version of ``triu`` API, the output Tensor will be inplaced with input ``x``. Please refer to :ref:`api_paddle_triu`. @@ -1841,7 +1912,7 @@ def __check_input(input, offset, dim1, dim2): return out -def diagflat(x, offset=0, name=None): +def diagflat(x, offset=0, name: str | None = None): """ If ``x`` is a vector (1-D tensor), a 2-D square tensor with the elements of ``x`` as the diagonal is returned. @@ -1859,7 +1930,7 @@ def diagflat(x, offset=0, name=None): Args: x (Tensor): The input tensor. It can be any shape. Its data type should be float16, float32, float64, int32, int64. offset (int, optional): The diagonal offset. A positive value represents superdiagonal, 0 represents the main diagonal, and a negative value represents subdiagonal. Default: 0 (main diagonal). - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor, a square matrix. The output data type is the same as input data type. @@ -1975,7 +2046,7 @@ def diagflat(x, offset=0, name=None): return out2 -def diag(x, offset=0, padding_value=0, name=None): +def diag(x, offset=0, padding_value=0, name: str | None = None): """ If ``x`` is a vector (1-D tensor), a 2-D square tensor with the elements of ``x`` as the diagonal is returned. @@ -1993,7 +2064,7 @@ def diag(x, offset=0, padding_value=0, name=None): x (Tensor): The input tensor. Its shape is either 1-D or 2-D. Its data type should be float16, float32, float64, int32, int64, complex64, complex128. offset (int, optional): The diagonal offset. A positive value represents superdiagonal, 0 represents the main diagonal, and a negative value represents subdiagonal. padding_value (int|float, optional): Use this value to fill the area outside the specified diagonal band. Only takes effect when the input is a 1-D Tensor. The default value is 0. - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor, a square matrix or a vector. The output data type is the same as input data type. @@ -2092,7 +2163,9 @@ def diag(x, offset=0, padding_value=0, name=None): return out -def empty(shape, dtype=None, name=None): +def empty( + shape: ShapeLike, dtype: DTypeLike | None = None, name: str | None = None +) -> paddle.Tensor: """ Returns a Tensor with uninitialized data which size is same as ``shape``. @@ -2104,7 +2177,7 @@ def empty(shape, dtype=None, name=None): which can be bool, float16, float32, float64, int32, int64, complex64, complex128 if dtype is `None`, the data type of created Tensor use global default dtype (see ``get_default_dtype`` for details). - name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: Tensor which is created according to ``shape`` and ``dtype``, and is uninitialized. @@ -2229,7 +2302,9 @@ def empty(shape, dtype=None, name=None): return out -def empty_like(x, dtype=None, name=None): +def empty_like( + x: paddle.Tensor, dtype: DTypeLike | None = None, name: str | None = None +) -> paddle.Tensor: """ Returns a Tensor with uninitialized data which has identical shape of ``x`` and ``dtype``. If the ``dtype`` is None, the data type of Tensor is same with ``x``. @@ -2239,7 +2314,7 @@ def empty_like(x, dtype=None, name=None): dtype(np.dtype|str, optional): The data type of output. The data type can be one of bool, float16, float32, float64, int32, int64. The default value is None, which means the output data type is the same as input. - name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: Tensor which is created according to ``x`` and ``dtype``, and is uninitialized. @@ -2339,7 +2414,7 @@ def empty_like(x, dtype=None, name=None): return out -def assign(x, output=None): +def assign(x: TensorLike, output: paddle.Tensor | None = None) -> paddle.Tensor: """ Copy value of the :attr:`x` to the :attr:`output`. @@ -2348,7 +2423,7 @@ def assign(x, output=None): x (Tensor|np.ndarray|list|tuple|scalar): A Tensor, numpy ndarray, tuple/list of scalar, or scalar. Its data type can be float16, float32, float64, int32, int64 or bool. Note: the float64 data will be converted to float32 because of current platform protobuf data limitation. - output (Tensor, optional): A Tensor. If :attr:`output` is None, a new Tensor will be created as :attr:`output`. Default: None. + output (Tensor|None, optional): A Tensor. If :attr:`output` is None, a new Tensor will be created as :attr:`output`. Default: None. Returns: Tensor: A Tensor with the same shape, data type and value as :attr:`x`. @@ -2547,7 +2622,7 @@ def convert_scalar(x): return output -def clone(x, name=None): +def clone(x: paddle.Tensor, name: str | None = None) -> paddle.Tensor: """ Returns a copy of input Tensor. It will always have a Tensor copy. @@ -2555,7 +2630,7 @@ def clone(x, name=None): Parameters: x (Tensor): The input Tensor. - name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor, A Tensor copied from ``input``. @@ -2583,7 +2658,7 @@ def clone(x, name=None): # NOTE(zhiqiu): not public -def _memcpy(input, place=None, output=None): +def _memcpy(input, place=None, output=None) -> paddle.Tensor: """ The OP copies the :attr:`input` to the :attr:`output`. @@ -2667,16 +2742,18 @@ def _memcpy(input, place=None, output=None): return output -def complex(real, imag, name=None): +def complex( + real: paddle.Tensor, imag: paddle.Tensor, name: str | None = None +) -> paddle.Tensor: """Return a complex tensor given the real and image component. Args: real (Tensor): The real component. The data type should be 'float32' or 'float64'. imag (Tensor): The image component. The data type should be the same as ``real``. - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: - Tensor: The output tensor. The data type is 'complex64' or 'complex128', with the same precision as ``real`` and ``imag``. + Tensor, The output tensor. The data type is 'complex64' or 'complex128', with the same precision as ``real`` and ``imag``. Note: ``paddle.complex`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ . @@ -2719,7 +2796,9 @@ def complex(real, imag, name=None): return out -def tril_indices(row, col, offset=0, dtype='int64'): +def tril_indices( + row: int, col: int, offset: int = 0, dtype='int64' +) -> paddle.Tensor: """ Return the indices of the lower triangular part of the 2-D matrix whose row and col is known. Indices are ordered based on row and then columns. @@ -2803,7 +2882,9 @@ def tril_indices(row, col, offset=0, dtype='int64'): return out -def triu_indices(row, col=None, offset=0, dtype='int64'): +def triu_indices( + row: int, col: int | None = None, offset: int = 0, dtype='int64' +) -> paddle.Tensor: """ Return the indices of the upper triangular part of the 2-D matrix whose row and col is known. Indices are ordered based on row and then columns. @@ -2812,7 +2893,7 @@ def triu_indices(row, col=None, offset=0, dtype='int64'): Args: row (int): The input x which is a int number describe the number of row of the matrix. - col (int, optional): The input x which is a int number describe the number of col of the matrix. + col (int|None, optional): The input x which is a int number describe the number of col of the matrix. default value for col is None, then it will be set equal to row, indicting a square matrix. offset (int, optional): The offset to consider, default value is 0. @@ -2882,16 +2963,18 @@ def triu_indices(row, col=None, offset=0, dtype='int64'): return out -def polar(abs, angle, name=None): +def polar( + abs: paddle.Tensor, angle: paddle.Tensor, name: str | None = None +) -> paddle.Tensor: """Return a Cartesian coordinates corresponding to the polar coordinates complex tensor given the ``abs`` and ``angle`` component. Args: abs (Tensor): The abs component. The data type should be 'float32' or 'float64'. angle (Tensor): The angle component. The data type should be the same as ``abs``. - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: - Tensor: The output tensor. The data type is 'complex64' or 'complex128', with the same precision as ``abs`` and ``angle``. + Tensor, The output tensor. The data type is 'complex64' or 'complex128', with the same precision as ``abs`` and ``angle``. Note: ``paddle.polar`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ . @@ -2921,14 +3004,14 @@ def polar(abs, angle, name=None): @dygraph_only -def cauchy_(x, loc=0, scale=1, name=None): +def cauchy_(x: paddle.Tensor, loc=0, scale=1, name: str | None = None): """Fills the tensor with numbers drawn from the Cauchy distribution. Args: x (Tensor): the tensor will be filled, The data type is float32 or float64. loc (scalar, optional): Location of the peak of the distribution. The data type is float32 or float64. scale (scalar, optional): The half-width at half-maximum (HWHM). The data type is float32 or float64. Must be positive values. - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: input tensor with numbers drawn from the Cauchy distribution. @@ -2955,14 +3038,14 @@ def cauchy_(x, loc=0, scale=1, name=None): @dygraph_only -def geometric_(x, probs, name=None): +def geometric_(x: paddle.Tensor, probs, name: str | None = None): """Fills the tensor with numbers drawn from the Geometric distribution. Args: x (Tensor): the tensor will be filled, The data type is float32 or float64. probs (Real|Tensor): Probability parameter. The value of probs must be positive. When the parameter is a tensor, probs is probability of success for each trial. - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor: input tensor with numbers drawn from the Geometric distribution. From 02be84e67b7bf41f534364b26cf54df563bd8b80 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Wed, 12 Jun 2024 13:17:05 +0000 Subject: [PATCH 02/16] p2 --- python/paddle/tensor/creation.py | 56 +++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index f11e37ee456e4..c6f57b562dc6c 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -16,6 +16,7 @@ import math import re +from typing import Sequence, overload import numpy as np @@ -1385,7 +1386,7 @@ def arange( step: float | paddle.Tensor = 1, dtype: DTypeLike | None = None, name: str | None = None, -): +) -> paddle.Tensor: """ Returns a 1-D Tensor with spaced values within a given interval. @@ -1518,7 +1519,7 @@ def arange( return out -def _tril_triu_op(helper): +def _tril_triu_op(helper: LayerHelper) -> paddle.Tensor: """Base op of tril_op and triu_op""" op_type = helper.layer_type x = helper.kwargs.get('x', None) @@ -1567,7 +1568,9 @@ def _tril_triu_op(helper): return out -def tril(x, diagonal=0, name: str | None = None): +def tril( + x: paddle.Tensor, diagonal: int = 0, name: str | None = None +) -> paddle.Tensor: r""" Returns the lower triangular part of a matrix (2-D tensor) or batch of matrices :attr:`x`, the other elements of the result tensor are set @@ -1632,7 +1635,9 @@ def tril(x, diagonal=0, name: str | None = None): @inplace_apis_in_dygraph_only -def tril_(x, diagonal=0, name: str | None = None): +def tril_( + x: paddle.Tensor, diagonal: int = 0, name: str | None = None +) -> paddle.Tensor | None: r""" Inplace version of ``tril`` API, the output Tensor will be inplaced with input ``x``. Please refer to :ref:`api_paddle_tril`. @@ -1642,7 +1647,9 @@ def tril_(x, diagonal=0, name: str | None = None): return _C_ops.tril_(x, diagonal) -def triu(x, diagonal=0, name: str | None = None): +def triu( + x: paddle.Tensor, diagonal: int = 0, name: str | None = None +) -> paddle.Tensor: r""" Return the upper triangular part of a matrix (2-D tensor) or batch of matrices :attr:`x`, the other elements of the result tensor are set to 0. @@ -1709,7 +1716,9 @@ def triu(x, diagonal=0, name: str | None = None): @inplace_apis_in_dygraph_only -def triu_(x, diagonal=0, name: str | None = None): +def triu_( + x: paddle.Tensor, diagonal: int = 0, name: str | None = None +) -> paddle.Tensor | None: r""" Inplace version of ``triu`` API, the output Tensor will be inplaced with input ``x``. Please refer to :ref:`api_paddle_triu`. @@ -1719,6 +1728,18 @@ def triu_(x, diagonal=0, name: str | None = None): return _C_ops.triu_(x, diagonal) +@overload +def meshgrid( + args: Sequence[paddle.Tensor], name: str | None = None +) -> paddle.Tensor: + ... + + +@overload +def meshgrid(*args: paddle.Tensor, name: str | None = None) -> paddle.Tensor: + ... + + def meshgrid(*args, **kwargs): """ @@ -1793,7 +1814,9 @@ def meshgrid(*args, **kwargs): return out -def diag_embed(input, offset=0, dim1=-2, dim2=-1): +def diag_embed( + input: TensorLike, offset: int = 0, dim1: int = -2, dim2: int = -1 +) -> paddle.Tensor: """ Creates a tensor whose diagonals of certain 2D planes (specified by dim1 and dim2) are filled by ``input``. By default, a 2D plane formed by the last two dimensions @@ -1912,7 +1935,9 @@ def __check_input(input, offset, dim1, dim2): return out -def diagflat(x, offset=0, name: str | None = None): +def diagflat( + x: paddle.Tensor, offset: int = 0, name: str | None = None +) -> paddle.Tensor: """ If ``x`` is a vector (1-D tensor), a 2-D square tensor with the elements of ``x`` as the diagonal is returned. @@ -2046,7 +2071,12 @@ def diagflat(x, offset=0, name: str | None = None): return out2 -def diag(x, offset=0, padding_value=0, name: str | None = None): +def diag( + x: paddle.Tensor, + offset: int = 0, + padding_value: int = 0, + name: str | None = None, +) -> paddle.Tensor: """ If ``x`` is a vector (1-D tensor), a 2-D square tensor with the elements of ``x`` as the diagonal is returned. @@ -3004,7 +3034,9 @@ def polar( @dygraph_only -def cauchy_(x: paddle.Tensor, loc=0, scale=1, name: str | None = None): +def cauchy_( + x: paddle.Tensor, loc=0, scale=1, name: str | None = None +) -> paddle.Tensor: """Fills the tensor with numbers drawn from the Cauchy distribution. Args: @@ -3038,7 +3070,9 @@ def cauchy_(x: paddle.Tensor, loc=0, scale=1, name: str | None = None): @dygraph_only -def geometric_(x: paddle.Tensor, probs, name: str | None = None): +def geometric_( + x: paddle.Tensor, probs, name: str | None = None +) -> paddle.Tensor: """Fills the tensor with numbers drawn from the Geometric distribution. Args: From 574daefbe71e1ca8766c30d538449498f0488551 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <46243324+zrr1999@users.noreply.github.com> Date: Wed, 12 Jun 2024 23:20:05 +0800 Subject: [PATCH 03/16] Update python/paddle/tensor/creation.py Co-authored-by: Nyakku Shigure --- python/paddle/tensor/creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index c6f57b562dc6c..1d88384b56d47 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -102,7 +102,7 @@ def create_global_var( Default: False force_cpu (bool, optional): Force this variable to be on CPU. Default: False - name(str|None, optional): For detailed information, please refer to + name (str|None, optional): For detailed information, please refer to :ref:`api_guide_Name` . Usually name is no need to set and None by default. Returns: From af7ce189d8981b368125e35812f9b4e3d863cd08 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <46243324+zrr1999@users.noreply.github.com> Date: Wed, 12 Jun 2024 23:20:19 +0800 Subject: [PATCH 04/16] Update python/paddle/tensor/creation.py Co-authored-by: Nyakku Shigure --- python/paddle/tensor/creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 1d88384b56d47..433dbfd809301 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -599,7 +599,7 @@ def _handle_tensor_dtype( return tensor.astype(convert_dtype(dtype)) return tensor - def _handle_np_dtype(ndarray: np.ndarray, dtype: DTypeLike) -> np.ndarray: + def _handle_np_dtype(ndarray: npt.NDArray[Any], dtype: DTypeLike) -> npt.NDArray[Any]: if dtype: if convert_dtype(dtype) != convert_dtype(ndarray.dtype): # should not ndarray.astype('uint16') directly, data bits is wrong From 55e138774204f3f307326deff0e14460566e92e2 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <46243324+zrr1999@users.noreply.github.com> Date: Wed, 12 Jun 2024 23:20:39 +0800 Subject: [PATCH 05/16] Update python/paddle/tensor/creation.py Co-authored-by: Nyakku Shigure --- python/paddle/tensor/creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 433dbfd809301..e0942e0630afc 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -316,7 +316,7 @@ def linspace( or a 0-D Tensor with data type int32, int64, float32 or float64. num(int|Tensor): The input :attr:`num` is given num of the sequence. It is an int, \ or a 0-D Tensor with data type int32. - dtype(np.dtype|str, optional): The data type of output tensor, it could be + dtype(str|paddle.dtype|np.dtype|None, optional): The data type of output tensor, it could be int32, int64, float32 and float64. Default: if None, the data type is float32. name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. From 6c2d03c76165f925119e871660289000dae368b0 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Wed, 12 Jun 2024 15:21:59 +0000 Subject: [PATCH 06/16] fix --- python/paddle/tensor/creation.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index e0942e0630afc..4240edc8bd607 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -16,9 +16,10 @@ import math import re -from typing import Sequence, overload +from typing import Any, Sequence, overload import numpy as np +import numpy.typing as npt import paddle from paddle import _C_ops @@ -94,7 +95,7 @@ def create_global_var( This function creates a new tensor variable with value in the global block(block 0). Args: - shape (ShapeLike): Shape of the variable + shape (list[int]|tuple[int]): Shape of the variable value (float): The value of the variable. The new created variable will be filled with it. dtype (str): Data type of the variable @@ -599,7 +600,9 @@ def _handle_tensor_dtype( return tensor.astype(convert_dtype(dtype)) return tensor - def _handle_np_dtype(ndarray: npt.NDArray[Any], dtype: DTypeLike) -> npt.NDArray[Any]: + def _handle_np_dtype( + ndarray: npt.NDArray[Any], dtype: DTypeLike + ) -> npt.NDArray[Any]: if dtype: if convert_dtype(dtype) != convert_dtype(ndarray.dtype): # should not ndarray.astype('uint16') directly, data bits is wrong From 1e005bc610fb6c1bc3e6a06a3682109635203f2c Mon Sep 17 00:00:00 2001 From: Nyakku Shigure Date: Wed, 12 Jun 2024 23:28:29 +0800 Subject: [PATCH 07/16] Update python/paddle/tensor/creation.py --- python/paddle/tensor/creation.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 4240edc8bd607..f67f45b605793 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -3074,13 +3074,15 @@ def cauchy_( @dygraph_only def geometric_( - x: paddle.Tensor, probs, name: str | None = None + x: paddle.Tensor, + probs: float | paddle.Tensor, + name: str | None = None, ) -> paddle.Tensor: """Fills the tensor with numbers drawn from the Geometric distribution. Args: x (Tensor): the tensor will be filled, The data type is float32 or float64. - probs (Real|Tensor): Probability parameter. + probs (float|Tensor): Probability parameter. The value of probs must be positive. When the parameter is a tensor, probs is probability of success for each trial. name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. From 749694ed8ba23f1776f1a53635b732c5dbb0e3c9 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Thu, 13 Jun 2024 04:03:04 +0000 Subject: [PATCH 08/16] fix --- python/paddle/tensor/creation.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index f67f45b605793..17de648335c66 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -29,6 +29,7 @@ PlaceLike, ShapeLike, TensorLike, + Numberic, ) from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only @@ -1734,13 +1735,13 @@ def triu_( @overload def meshgrid( args: Sequence[paddle.Tensor], name: str | None = None -) -> paddle.Tensor: - ... +) -> paddle.Tensor: ... @overload -def meshgrid(*args: paddle.Tensor, name: str | None = None) -> paddle.Tensor: - ... +def meshgrid( + *args: paddle.Tensor, name: str | None = None +) -> paddle.Tensor: ... def meshgrid(*args, **kwargs): @@ -3038,7 +3039,10 @@ def polar( @dygraph_only def cauchy_( - x: paddle.Tensor, loc=0, scale=1, name: str | None = None + x: paddle.Tensor, + loc: Numberic = 0, + scale: Numberic = 1, + name: str | None = None, ) -> paddle.Tensor: """Fills the tensor with numbers drawn from the Cauchy distribution. From 73cabcaef4c7d54bd2fc75d023a8fe20a7b26aba Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Thu, 13 Jun 2024 05:32:48 +0000 Subject: [PATCH 09/16] fix --- python/paddle/tensor/creation.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 17de648335c66..228e6e700266d 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -25,11 +25,12 @@ from paddle import _C_ops from paddle._typing import ( DTypeLike, + DynamicShapeLike, NestedNumbericSequence, + Numberic, PlaceLike, ShapeLike, TensorLike, - Numberic, ) from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only @@ -1136,7 +1137,9 @@ def ones_like( def zeros( - shape: ShapeLike, dtype: DTypeLike | None = None, name: str | None = None + shape: DynamicShapeLike, + dtype: DTypeLike | None = None, + name: str | None = None, ) -> paddle.Tensor: """ Creates a tensor of specified :attr:`shape` and :attr:`dtype`, and fills it with 0. @@ -1735,13 +1738,13 @@ def triu_( @overload def meshgrid( args: Sequence[paddle.Tensor], name: str | None = None -) -> paddle.Tensor: ... +) -> paddle.Tensor: + ... @overload -def meshgrid( - *args: paddle.Tensor, name: str | None = None -) -> paddle.Tensor: ... +def meshgrid(*args: paddle.Tensor, name: str | None = None) -> paddle.Tensor: + ... def meshgrid(*args, **kwargs): From 18a84a71dd78287db929876fd4b6c5918b37665f Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Thu, 13 Jun 2024 07:02:29 +0000 Subject: [PATCH 10/16] fix --- python/paddle/_typing/shape.py | 14 +++++++------- python/paddle/tensor/creation.py | 3 +-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/python/paddle/_typing/shape.py b/python/paddle/_typing/shape.py index 235bfd6157c9b..ff7e3b92b7f72 100644 --- a/python/paddle/_typing/shape.py +++ b/python/paddle/_typing/shape.py @@ -13,26 +13,26 @@ # limitations under the License. from __future__ import annotations -from typing import TYPE_CHECKING, List, Tuple, Union +from typing import TYPE_CHECKING, List, Sequence, Tuple, Union from typing_extensions import TypeAlias if TYPE_CHECKING: from .. import Tensor -DynamicShapeLike: TypeAlias = Union[ - Tuple[Union[int, "Tensor", None], ...], - List[Union[int, "Tensor", None]], + +_DynamicShapeLike: TypeAlias = Union[ + Sequence[Union[int, "Tensor", None]], "Tensor", ] -ShapeLike: TypeAlias = Union[ - Tuple[int, ...], - List[int], +_StaticShapeLike: TypeAlias = Union[ + Sequence[int], "Tensor", ] +ShapeLike: TypeAlias = Union[_DynamicShapeLike, _StaticShapeLike] # for size parameters, eg, kernel_size, stride ... Size1: TypeAlias = Union[int, Tuple[int], List[int]] diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 228e6e700266d..be2372ee35ed0 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -25,7 +25,6 @@ from paddle import _C_ops from paddle._typing import ( DTypeLike, - DynamicShapeLike, NestedNumbericSequence, Numberic, PlaceLike, @@ -1137,7 +1136,7 @@ def ones_like( def zeros( - shape: DynamicShapeLike, + shape: ShapeLike, dtype: DTypeLike | None = None, name: str | None = None, ) -> paddle.Tensor: From 32024b1be053f3ee9b2c8b77b875080c888ab5bf Mon Sep 17 00:00:00 2001 From: SigureMo Date: Thu, 13 Jun 2024 18:54:21 +0800 Subject: [PATCH 11/16] remove re-export for DynamicShapeLike --- python/paddle/_typing/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/_typing/__init__.py b/python/paddle/_typing/__init__.py index 131a71a9520c5..a405d495161d6 100644 --- a/python/paddle/_typing/__init__.py +++ b/python/paddle/_typing/__init__.py @@ -44,7 +44,6 @@ # Shape from .shape import ( - DynamicShapeLike as DynamicShapeLike, ShapeLike as ShapeLike, Size1 as Size1, Size2 as Size2, From 21a021e10a0ba714c4d2fe5b64becd1a14ad77cd Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Fri, 14 Jun 2024 05:50:27 +0000 Subject: [PATCH 12/16] fix --- python/paddle/tensor/creation.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index be2372ee35ed0..dae25bd396dc5 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -2474,9 +2474,9 @@ def assign(x: TensorLike, output: paddle.Tensor | None = None) -> paddle.Tensor: [[2.5 2.5] [2.5 2.5] [2.5 2.5]] - >>> array = np.array([[1, 1], - ... [3, 4], - ... [1, 3]]).astype(np.int64) + >>> array: npt.NDArray[Any] = np.array([[1, 1], [3, 4], [1, 3]]).astype( + ... np.int64 + ... ) >>> result1 = paddle.zeros(shape=[3, 3], dtype='float32') >>> paddle.assign(array, result1) >>> print(result1.numpy()) @@ -2685,9 +2685,9 @@ def clone(x: paddle.Tensor, name: str | None = None) -> paddle.Tensor: >>> y = clone_x**3 >>> y.backward() - >>> print(clone_x.grad.numpy()) + >>> print(clone_x.grad.numpy()) # type: ignore [3. 3.] - >>> print(x.grad.numpy()) + >>> print(x.grad.numpy()) # type: ignore [3. 3.] """ return x.clone() From 187c73bc907c2f9643cb79ddc3f7d8cccf12e57d Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Fri, 14 Jun 2024 07:14:20 +0000 Subject: [PATCH 13/16] add import --- python/paddle/tensor/creation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index dae25bd396dc5..d096291e1022e 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -2469,6 +2469,7 @@ def assign(x: TensorLike, output: paddle.Tensor | None = None) -> paddle.Tensor: >>> import paddle >>> import numpy as np + >>> import numpy.typing as npt >>> data = paddle.full(shape=[3, 2], fill_value=2.5, dtype='float64') >>> print(data.numpy()) [[2.5 2.5] From 5b4c453284b575180bd34e6809b7596b1eb91367 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Fri, 14 Jun 2024 15:02:15 +0000 Subject: [PATCH 14/16] fix --- python/paddle/tensor/creation.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index d096291e1022e..a6cff75981668 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -2469,15 +2469,14 @@ def assign(x: TensorLike, output: paddle.Tensor | None = None) -> paddle.Tensor: >>> import paddle >>> import numpy as np - >>> import numpy.typing as npt >>> data = paddle.full(shape=[3, 2], fill_value=2.5, dtype='float64') >>> print(data.numpy()) [[2.5 2.5] [2.5 2.5] [2.5 2.5]] - >>> array: npt.NDArray[Any] = np.array([[1, 1], [3, 4], [1, 3]]).astype( + >>> array = np.array([[1, 1], [3, 4], [1, 3]]).astype( ... np.int64 - ... ) + ... ) # type: ignore >>> result1 = paddle.zeros(shape=[3, 3], dtype='float32') >>> paddle.assign(array, result1) >>> print(result1.numpy()) From aec08942c61e38464d8ecb2f4b206dbe8667f9f6 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 14 Jun 2024 17:54:02 +0000 Subject: [PATCH 15/16] remove print --- python/paddle/tensor/creation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index a6cff75981668..d7920d310cf89 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -2685,9 +2685,9 @@ def clone(x: paddle.Tensor, name: str | None = None) -> paddle.Tensor: >>> y = clone_x**3 >>> y.backward() - >>> print(clone_x.grad.numpy()) # type: ignore + >>> clone_x.grad.numpy() # type: ignore [3. 3.] - >>> print(x.grad.numpy()) # type: ignore + >>> x.grad.numpy() # type: ignore [3. 3.] """ return x.clone() From e9ed77b9923cb00207328ac35d7145c7d88487e5 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 14 Jun 2024 18:42:09 +0000 Subject: [PATCH 16/16] Revert "remove print" This reverts commit aec08942c61e38464d8ecb2f4b206dbe8667f9f6. --- python/paddle/tensor/creation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index d7920d310cf89..a6cff75981668 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -2685,9 +2685,9 @@ def clone(x: paddle.Tensor, name: str | None = None) -> paddle.Tensor: >>> y = clone_x**3 >>> y.backward() - >>> clone_x.grad.numpy() # type: ignore + >>> print(clone_x.grad.numpy()) # type: ignore [3. 3.] - >>> x.grad.numpy() # type: ignore + >>> print(x.grad.numpy()) # type: ignore [3. 3.] """ return x.clone()