o
    + i2                     @  s$  d dl mZ d dlmZmZmZmZ d dlZd dlmZ ddl	m
Z
mZ ddlmZ ddlmZ dd	lmZmZmZ erBd d
lmZ g ZedZed+ddZed,ddZdd Zed-ddZed.ddZdd Ze	d/d0ddZed1d!dZed2d"dZ	d/d#dZ	d/d3d)d*ZdS )4    )annotations)TYPE_CHECKINGAnyTypeVaroverloadN)_typing   )
check_typecheck_variable_and_dtype)in_pir_mode)Variable)LayerHelpercorein_dynamic_mode)SequenceTarray	list[Any]returnintc                 C     d S N r   r   r   _/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/tensor/array.pyarray_length#      r   paddle.Tensorc                 C  r   r   r   r   r   r   r   r   '   r   c                 C  s   t  rt| tsJ dt| S t r(t| tjjr|  s"t	dtj
| S t| tr5| jtjjjkr9t	dtdi t }|jdd}d|_|jdd| gid	|gid
 |S )a6  
    This OP is used to get the length of the input array.

    Args:
        array (list|Tensor): The input array that will be used to compute the length. In dynamic mode, ``array`` is a Python list. But in static graph mode, array is a Tensor whose VarType is DENSE_TENSOR_ARRAY.

    Returns:
        Tensor, 0-D Tensor with shape [], which is the length of array.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> arr = paddle.tensor.create_array(dtype='float32')
            >>> x = paddle.full(shape=[3, 3], fill_value=5, dtype="float32")
            >>> i = paddle.zeros(shape=[1], dtype="int32")

            >>> arr = paddle.tensor.array_write(x, i, array=arr)

            >>> arr_len = paddle.tensor.array_length(arr)
            >>> print(arr_len)
            1
    9The 'array' in array_write must be a list in dygraph mode8array should be tensor array variable in array_length Opr   int64dtypeTZlod_array_lengthXOuttypeZinputsZoutputsN)r   )r   
isinstancelistlenr   paddlepirValueis_dense_tensor_array_type	TypeError_pir_opsr   r   r&   r   VarDescVarTypeDENSE_TENSOR_ARRAYr   locals"create_variable_for_type_inferenceZstop_gradient	append_op)r   helpertmpr   r   r   r   +   s:   list[T]ic                 C  r   r   r   r   r9   r   r   r   
array_readf   r   r;   c                 C  r   r   r   r:   r   r   r   r;   j   r   c                 C  s   t  r(t| tsJ dt|tsJ d|jdgksJ d|d}| | S t rAt| tjj	r6| 
 s:tdtj| |S t|ddgd	 tdi t }t| tr^| jtjjjkrbtd
|j| jd}|jd| g|gdd|gid |S )a  
    This OP is used to read data at the specified position from the input array.

    Case:

    .. code-block:: text

        Input:
            The shape of first three tensors are [1], and that of the last one is [1,2]:
                array = ([0.6], [0.1], [0.3], [0.4, 0.2])
            And:
                i = [3]

        Output:
            output = [0.4, 0.2]

    Args:
        array (list|Tensor): The input array. In dynamic mode, ``array`` is a Python list. But in static graph mode, array is a Tensor whose ``VarType`` is ``DENSE_TENSOR_ARRAY``.
        i (Tensor): 1-D Tensor, whose shape is [1] and dtype is int64. It represents the
            specified read position of ``array``.

    Returns:
        Tensor, A Tensor that is read at the specified position of ``array``.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> arr = paddle.tensor.create_array(dtype="float32")
            >>> x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32")
            >>> i = paddle.zeros(shape=[1], dtype="int32")

            >>> arr = paddle.tensor.array_write(x, i, array=arr)

            >>> item = paddle.tensor.array_read(arr, i)
            >>> print(item.numpy())
            [[5. 5. 5.]]
    z6The 'array' in array_read must be list in dygraph modez<The index 'i' in array_read must be Variable in dygraph mode   4The shape of index 'i' should be [1] in dygraph moder   r   r9   r    r;   %array should be tensor array variabler!   Zread_from_arrayr#   Ir$   r%   N)r;   )r   r'   r(   r   shapeitemr   r*   r+   r,   r-   r.   r/   r;   r
   r   r3   r&   r   r0   r1   r2   r4   r"   r5   )r   r9   r6   outr   r   r   r;   n   sD   (
xNonelist[Any] | paddle.Tensorc                 C  r   r   r   rD   r9   r   r   r   r   array_write      rH   list[paddle.Tensor]c                 C  r   r   r   rG   r   r   r   rH      rI   c                 C  r   r   r   rG   r   r   r   rH      rI   c                 C  s  t  rSt| tsJ dt|tsJ d|jdgksJ d|d}|du r-t| j}t|ts6J d|t|ks@J d|t|k rL| ||< |S |	|  |S t
 rt|d	d
gd t| tjjsotdt|  d|durt|tjjr~| std|du rtj| j}|jtjjjjkrt| |j} tj|| | |S t|d	d
gd t| dtd tdi t }|durt|tr|jtjjjkrtd|du r|j |j! dtjjj| jd}|j"d| g|gdd|gid |S )a2  
    This OP writes the input ``x`` into the i-th position of the ``array`` returns the modified array.
    If ``array`` is none, a new array will be created and returned.

    Args:
        x (Tensor): The input data to be written into array. It's multi-dimensional
            Tensor. Data type: float32, float64, int32, int64 and bool.
        i (Tensor): 0-D Tensor with shape [], which represents the position into which
            ``x`` is written.
        array (list|Tensor, optional): The array into which ``x`` is written. The default value is None,
            when a new array will be created and returned as a result. In dynamic mode, ``array`` is a Python list.
            But in static graph mode, array is a Tensor whose ``VarType`` is ``DENSE_TENSOR_ARRAY``.

    Returns:
        list|Tensor, The input ``array`` after ``x`` is written into.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> arr = paddle.tensor.create_array(dtype="float32")
            >>> x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32")
            >>> i = paddle.zeros(shape=[1], dtype="int32")

            >>> arr = paddle.tensor.array_write(x, i, array=arr)

            >>> item = paddle.tensor.array_read(arr, i)
            >>> print(item.numpy())
            [[5. 5. 5.]]
    zBThe input data 'x' in array_write must be Variable in dygraph modez=The index 'i' in array_write must be Variable in dygraph moder<   r=   r   Nr   zNThe index 'i' should not be greater than the length of 'array' in dygraph moder9   r    rH   z$x should be pir.Value, but received .r>   rD   z7array should be tensor array variable in array_write Op.outnamer&   r"   Zwrite_to_arrayr?   r$   r%   )rH   )#r   r'   r   rA   rB   create_arrayr"   r(   r)   appendr   r
   r*   r+   r,   r.   r&   r-   r/   base	libpaddleDataType	UNDEFINEDcastarray_write_r	   r   r3   r   r0   r1   r2   create_variablerN   r5   )rD   r9   r   r6   r   r   r   rH      s|   $



r"   _typing.DTypeLikeinitialized_listSequence[paddle.Tensor] | None#paddle.Tensor | list[paddle.Tensor]c                 C  s.  g }|durt |ttfstdt| t|}|D ]}t |ttjjfs1tdt| dqt	 r7|S t
 rpt | tjjtjfsKtjj| } tj| }|D ]}| tjjjjkrct|| }tj||t| qS|S td	i t }|j|j dtjjj| d}|D ]}t|t||d q|S )
aT  
    This OP creates an array. It is used as the input of :ref:`api_paddle_tensor_array_array_read` and
    :ref:`api_paddle_tensor_array_array_write`.

    Args:
        dtype (str): The data type of the elements in the array. Support data type: float32, float64, int32, int64 and bool.
        initialized_list(list): Used to initialize as default value for created array.
                    All values in initialized list should be a Tensor.

    Returns:
        list|Tensor, An empty array. In dynamic mode, ``array`` is a Python list. But in static graph mode, array is a Tensor
        whose ``VarType`` is ``DENSE_TENSOR_ARRAY``.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> arr = paddle.tensor.create_array(dtype="float32")
            >>> x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32")
            >>> i = paddle.zeros(shape=[1], dtype="int32")

            >>> arr = paddle.tensor.array_write(x, i, array=arr)

            >>> item = paddle.tensor.array_read(arr, i)
            >>> print(item.numpy())
            [[5. 5. 5.]]

    NzBRequire type(initialized_list) should be list/tuple, but received zOAll values in `initialized_list` should be Variable or pir.Value, but received rK   r   rL   rM   rG   r   )r'   r(   tupler.   r&   r   r*   r+   r,   r   r   r   r0   r1   rS   rQ   	frameworkZconvert_np_dtype_to_dtype_r/   rO   rR   rT   rU   rV   r   r   r3   rW   rN   r2   rH   )r"   rY   r   valrC   r6   Ztensor_arrayr   r   r   rO   5  sB   !
rO   )r   r   r   r   )r   r   r   r   )r   r8   r9   r   r   r   )r   r   r9   r   r   r   r   )rD   r   r9   r   r   rE   r   rF   )rD   r   r9   r   r   rJ   r   rJ   )rD   r   r9   r   r   r   r   r   )r"   rX   rY   rZ   r   r[   )
__future__r   typingr   r   r   r   r*   r   Zbase.data_feederr	   r
   Zbase.frameworkr   Zcommon_ops_importr   r]   r   r   r   collections.abcr   __all__r   r   r;   rH   rO   r   r   r   r   <module>   sB   ;N
i