o
    pi.                     @  s   d dl mZ d dlmZ d dlZd dlZd dlmZ d dlm	Z	 ddl
mZmZ ddlmZmZ dd	lmZ dd
lmZmZ ddlmZmZ erNd dlmZ g Zd#ddZd#ddZe	ddgid$ddZd$ddZd$ddZd%d&dd Zd%d&d!d"Z dS )'    )annotations)TYPE_CHECKINGN)_C_ops)ParamAliasDecorator   )
check_typecheck_variable_and_dtype)in_dynamic_or_pir_modeuse_pir_api)Variable)LayerHelpercore   )_complex_to_real_dtypeassign)Tensorinputr   returnc                 C  s4   t | dttjjfd t| j}tt	|d}|S )a%  

    Returns the number of dimensions for a tensor, which is a 0-D int32 Tensor.

    Args:
        input (Tensor): The input Tensor with shape of :math:`[N_1, N_2, ..., N_k]`, the data type is arbitrary.

    Returns:
        Tensor, the output data type is int32.: The 0-D tensor with the dimensions of the input Tensor.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> input = paddle.rand((3, 100, 100))
            >>> rank = paddle.rank(input)
            >>> print(rank.numpy())
            3
    r   int32)
r   r   paddlepirValuelenshaper   nparray)r   Zndimsout r   ^/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/tensor/attribute.pyrank%   s   
r   c                 C  sl   t  rt| }d|_|S t| dg dd tdi t }|jdd}|jdd| id|idd	 d|_|S )a  
    Get the shape of the input.

    .. code-block:: text

        Case1:
            Given N-D Tensor:
                input = [ [1, 2, 3, 4], [5, 6, 7, 8] ]

            Then:
                input.shape = [2, 4]

        Case2:
            Given SelectedRows:
                input.rows = [0, 4, 19]
                input.height = 20
                input.value = [ [1, 2], [3, 4], [5, 6] ]  # inner tensor
            Then:
                input.shape = [3, 2]

    Args:
        input (Tensor): The input can be N-D Tensor or SelectedRows with data type bool, bfloat16, float16, float32, float64, int32, int64.
                          If input variable is type of SelectedRows, returns the shape of it's inner tensor.

    Returns:
        Tensor: The shape of the input variable.

    Examples:
        .. code-block:: python

            >>> import numpy as np
            >>> import paddle
            >>> paddle.enable_static()

            >>> inputs = paddle.static.data(name="x", shape=[3, 100, 100], dtype="float32")
            >>> output = paddle.shape(inputs)

            >>> exe = paddle.static.Executor(paddle.CPUPlace())
            >>> exe.run(paddle.static.default_startup_program())

            >>> img = np.ones((3, 100, 100)).astype(np.float32)

            >>> res = exe.run(paddle.static.default_main_program(), feed={'x': img}, fetch_list=[output])
            >>> print(res)
            [array([  3, 100, 100], dtype=int64)]
    Tr   )booluint16Zfloat16Zfloat32Zfloat64r   Zint64	complex64
complex128r!   Zfloat8_e4m3fnZfloat8_e5m2r   r   dtypeZInputOut)typeinputsoutputsstop_gradientN)r   )	r	   r   Zshape64r*   r   r   locals"create_variable_for_type_inference	append_op)r   r   helperr   r   r   r   A   s(   /
r   xr    c                 C  sj   t | tjtjjtjjfstdt|  | j	}|t
jjjkp2|t
jjjkp2|t
jjkp2|t
jjk}|S )am  Return whether x is a tensor of complex data type(complex64 or complex128).


    .. note::
    Alias Support: The parameter name ``input`` can be used as an alias for ``x``.
    For example, ``input=tensor_x`` is equivalent to ``x=tensor_x``.

    Args:
        x (Tensor): The input tensor.
        input: An alias for ``x`` , with identical behavior.

    Returns:
        bool: True if the data type of the input is complex data type, otherwise false.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = paddle.to_tensor([1 + 2j, 3 + 4j])
            >>> print(paddle.is_complex(x))
            True

            >>> x = paddle.to_tensor([1.1, 1.2])
            >>> print(paddle.is_complex(x))
            False

            >>> x = paddle.to_tensor([1, 2, 3])
            >>> print(paddle.is_complex(x))
            False
    )Expected Tensor, but received type of x: )
isinstancer   r   staticr   r   r   	TypeErrorr'   r%   r   VarDescVarTypeZ	COMPLEX64Z
COMPLEX128DataType)r/   r%   Zis_complex_dtyper   r   r   
is_complex   s   !

r7   c                 C  s   t | tjtjjtjjfstdt|  | j	}|t
jjjkpL|t
jjjkpL|t
jjjkpL|t
jjjkpL|t
jjkpL|t
jjkpL|t
jjkpL|t
jjk}|S )aK  
    Returns whether the dtype of `x` is one of paddle.float64, paddle.float32, paddle.float16, and paddle.bfloat16.

    Args:
        x (Tensor): The input tensor.

    Returns:
        bool: True if the dtype of `x` is floating type, otherwise false.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = paddle.arange(1., 5., dtype='float32')
            >>> y = paddle.arange(1, 5, dtype='int32')
            >>> print(paddle.is_floating_point(x))
            True
            >>> print(paddle.is_floating_point(y))
            False
    r0   )r1   r   r   r2   r   r   r   r3   r'   r%   r   r4   r5   ZFP32ZFP64ZFP16ZBF16r6   ZFLOAT32ZFLOAT64ZFLOAT16ZBFLOAT16)r/   r%   Zis_fp_dtyper   r   r   is_floating_point   s*   




r8   c                 C  s   t | tjtjjtjjfstdt|  | j	}d}t
 sC|tjjjkp@|tjjjkp@|tjjjkp@|tjjjkp@|tjjjk}|S |tjjkp`|tjjkp`|tjjkp`|tjjkp`|tjjk}|S )ap  Return whether x is a tensor of integral data type.

    Args:
        x (Tensor): The input tensor.

    Returns:
        bool: True if the data type of the input is integer data type, otherwise false.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = paddle.to_tensor([1 + 2j, 3 + 4j])
            >>> print(paddle.is_integer(x))
            False

            >>> x = paddle.to_tensor([1.1, 1.2])
            >>> print(paddle.is_integer(x))
            False

            >>> x = paddle.to_tensor([1, 2, 3])
            >>> print(paddle.is_integer(x))
            True
    r0   F)r1   r   r   r2   r   r   r   r3   r'   r%   r
   r   r4   r5   ZUINT8ZINT8ZINT16ZINT32ZINT64r6   )r/   r%   Zis_int_dtyper   r   r   
is_integer   s6   



r9   name
str | Nonec                 C  b   t  rt| S t| dddgd td
i t }|jt| d}|j	dd| id|id |S )a  
    Returns a new Tensor containing real values of the input Tensor.

    Args:
        x (Tensor): the input Tensor, its data type could be complex64 or complex128.
        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:
        Tensor: a Tensor containing real values of the input Tensor.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = paddle.to_tensor(
            ...     [[1 + 6j, 2 + 5j, 3 + 4j], [4 + 3j, 5 + 2j, 6 + 1j]])
            >>> print(x)
            Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
            [[(1+6j), (2+5j), (3+4j)],
             [(4+3j), (5+2j), (6+1j)]])

            >>> real_res = paddle.real(x)
            >>> print(real_res)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[1., 2., 3.],
             [4., 5., 6.]])

            >>> real_t = x.real()
            >>> print(real_t)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[1., 2., 3.],
             [4., 5., 6.]])
    r/   r"   r#   realr$   Xr&   r'   r(   r)   N)r=   )
r	   r   r=   r   r   r+   r,   r   input_dtyper-   r/   r:   r.   r   r   r   r   r=         $

r=   c                 C  r<   )a  
    Returns a new tensor containing imaginary values of input tensor.

    Args:
        x (Tensor): the input tensor, its data type could be complex64 or complex128.
        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:
        Tensor: a tensor containing imaginary values of the input tensor.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = paddle.to_tensor(
            ...     [[1 + 6j, 2 + 5j, 3 + 4j], [4 + 3j, 5 + 2j, 6 + 1j]])
            >>> print(x)
            Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
            [[(1+6j), (2+5j), (3+4j)],
             [(4+3j), (5+2j), (6+1j)]])

            >>> imag_res = paddle.imag(x)
            >>> print(imag_res)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[6., 5., 4.],
             [3., 2., 1.]])

            >>> imag_t = x.imag()
            >>> print(imag_t)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[6., 5., 4.],
             [3., 2., 1.]])
    r/   r"   r#   imagr$   r>   r&   r?   N)rC   )
r	   r   rC   r   r   r+   r,   r   r@   r-   rA   r   r   r   rC   P  rB   rC   )r   r   r   r   )r/   r   r   r    )N)r/   r   r:   r;   r   r   )!
__future__r   typingr   numpyr   r   r   Zpaddle.utils.decorator_utilsr   Zbase.data_feederr   r   Zbase.frameworkr	   r
   Zcommon_ops_importr   Z	frameworkr   r   Zcreationr   r   r   __all__r   r   r7   r8   r9   r=   rC   r   r   r   r   <module>   s,   

S
.
(50