
    |j2                    r   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rd d
lmZ g Z ed          Zed'd            Zed(d            Zd Zed)d            Zed*d            Zd Ze	 d+d,d            Zed-d            Zed.d            Z	 d+d Z	 d+d/d&ZdS )0    )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                    d S N r   s    c/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/tensor/array.pyarray_lengthr   #   s    +.3    paddle.Tensorc                    d S r   r   r   s    r   r   r   '   s    9<r   c                   t                      r.t          | t                    s
J d            t          |           S t	                      rat          | t
          j        j                  r|                                 st          d          t
          j
                            |           S t          | t                    r| j        t          j        j        j        k    rt          d          t%          di t'                      }|                    d          }d|_        |                    dd| gid	|gi
           |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dtypeTlod_array_lengthXOuttypeinputs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_inferencestop_gradient	append_op)r   helpertmps      r   r   r   +   s[   2  %&& 	
 	
G	
 	
& 5zz	 5&*"233	3355	 J   ++E222 5(++	zT\1DDDJ   88vxx8877g7FF #%>SEN 	 	
 	
 	

 
r   list[T]ic                    d S r   r   r   r@   s     r   
array_readrC   f   s    7:sr   c                    d S r   r   rB   s     r   rC   rC   j   s    ILr   c                6   t                      rqt          | t                    s
J d            t          |t                    s
J d            |j        dgk    s
J d            |                    d          }| |         S t                      rbt          | t          j        j	                  r| 
                                st          d          t          j                            | |          S t          |ddgd	           t          di t!                      }t          | t                    r| j        t$          j        j        j        k    rt          d
          |                    | j                  }|                    d| g|gdd|gi           |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"   r@   r#   rC   %array should be tensor array variabler$   read_from_arrayr'   Ir(   r)   N)rC   )r   r-   r.   r   shapeitemr   r0   r1   r2   r3   r4   r5   rC   r   r   r9   r*   r   r6   r7   r8   r:   r%   r<   )r   r@   r=   outs       r   rC   rC   n   s   P  #%&& 	
 	
D	
 	
& !X&& 	
 	
J	
 	
& w1#~~~B ~~ FF1IIQx	 5&*"233	3355	 J   ))%333 C'LAAA66VXX665(++	EzT\1DDDCDDD77ek7JJ"s++SEN 	 	
 	
 	

 
r   xNonelist[Any] | paddle.Tensorc                    d S r   r   rO   r@   r   s      r   array_writerT      s	     !$r   list[paddle.Tensor]c                    d S r   r   rS   s      r   rT   rT      s	     #r   c                    d S r   r   rS   s      r   rT   rT      s	     Cr   c                   t                      rt          | t                    s
J d            t          |t                    s
J d            |j        dgk    s
J d            |                    d          }|t          | j                  }t          |t                    s
J d            |t          |          k    s
J d            |t          |          k     r| ||<   n|	                    |            |S t                      rt          |d	d
gd           t          | t          j        j                  s t          dt!          |            d          |Bt          |t          j        j                  r|                                st          d          |$t          j                            | j                  }|j        t          j        j        j        j        k    rt          j        | |j                  } t          j                            || |           |S t          |d	d
gd           t3          | dt          d           t5          di t7                      }|Ct          |t                    r|j        t8          j        j        j        k    rt          d          |9|                     |j!         dt8          j        j        j        | j                  }|"                    d| g|gdd|gi           |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 moderF   rG   r   Nr!   zNThe index 'i' should not be greater than the length of 'array' in dygraph moder@   r#   rT   z$x should be pir.Value, but received .rH   rO   z7array should be tensor array variable in array_write Op.outnamer*   r%   write_to_arrayrJ   r(   r)   )rT   )#r   r-   r   rL   rM   create_arrayr%   r.   r/   appendr   r   r0   r1   r2   r4   r*   r3   r5   base	libpaddleDataType	UNDEFINEDcastarray_write_r
   r   r9   r   r6   r7   r8   create_variabler\   r<   )rO   r@   r   r=   s       r   rT   rT      s$   H  @!X&& 	
 	
P	
 	
& !X&& 	
 	
K	
 	
& w1#~~~B ~~ FF1II= ))E%&& 	
 	
G	
 	
& CJJ\  s5zz>>E!HHLLOOO	 ( C'MBBB!VZ-.. 	OM477MMMNNNufj&677I7799I   GHHH=O0099E;&+/8BBBAu{++A$$UAq111 C'MBBB1cH}55577fhh77uh//:!5!HHHM   =**)))\)<g +  E
 	!A3''UG$ 	 	
 	
 	

 r   r%   _typing.DTypeLikeinitialized_listSequence[paddle.Tensor] | None#paddle.Tensor | list[paddle.Tensor]c                   g }|Jt          |t          t          f          st          dt	          |                     t          |          }|D ]H}t          |t
          t          j        j        f          s t          dt	          |           d          It                      r|S t                      rt          | t          j        j        t          j        f          s$t          j        j                            |           } t          j                            |           }|D ]d}| t          j        j        j        j        k    rt          j        ||           }t          j                            ||t1          |                     e|S t3          d	i t5                      }|                    |j         dt          j        j        j        |           }|D ]!}t=          |t1          |          |           "|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 rY   r   rZ   r[   rS   r   )r-   r.   tupler4   r*   r   r0   r1   r2   r   r   r   r6   r7   rb   r`   	frameworkconvert_np_dtype_to_dtype_r5   r^   ra   rc   rd   re   r   r   r9   rf   r\   r8   rT   )r%   rh   r   valrN   r=   tensor_arrays          r   r^   r^   5  s   B E#*T5M:: 	mUYZjUkUkmm   %&&   #&**:;<< 	nbfgjbkbknnn  	
  	 %$,"6!FGG 	LK)DDUKKEo**511 	F 	FC-6@@@k#u--O((c<3D3DEEEE
1111&,&<&<K%%%%8 '= '
 '
  	Q 	QC#l!;!;<PPPPPr   )r   r   r   r   )r   r   r   r   )r   r?   r@   r   r   r   )r   r   r@   r   r   r   r   )rO   r   r@   r   r   rP   r   rQ   )rO   r   r@   r   r   rU   r   rU   )rO   r   r@   r   r   r   r   r   )r%   rg   rh   ri   r   rj   )
__future__r   typingr   r   r   r   r0   r   base.data_feederr
   r   base.frameworkr   common_ops_importr   rm   r   r   r   collections.abcr   __all__r   r   rC   rT   r^   r   r   r   <module>rx      s    # " " " " " 8 8 8 8 8 8 8 8 8 8 8 8        C C C C C C C C ( ( ( ( ( ( ( ( ( ( ( ( : : : : : : : : : : )((((((
GCLL 
 . . . 
 . 
 < < < 
 <8 8 8v 
 : : : 
 : 
 L L L 
 LK K K\ 
6:$ $ $ $ 
$
 
   

 
   
 d d d dR 8<F F F F F F Fr   