o
    #j.                     @   sp   d dl 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 g Zdd Zd	d
 ZdddZdddZdS )    N   )
check_typecheck_variable_and_dtype)in_pir_mode)Variable)LayerHelpercorein_dynamic_modec                 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 )a5  
    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 LOD_TENSOR_ARRAY.

    Returns:
        Tensor: 1-D Tensor with shape [1], 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 vairable in array_length Oparray_lengthint64dtypeTZlod_array_lengthXOuttypeZinputsZoutputsN)r   )r	   
isinstancelistlenr   paddlepirOpResultis_dense_tensor_array_type	TypeError_pir_opsr   r   r   r   VarDescVarTypeLOD_TENSOR_ARRAYr   locals"create_variable_for_type_inferenceZstop_gradient	append_op)arrayhelpertmp r&   T/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/tensor/array.pyr      s>   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 ``LOD_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   ir   
array_read%array should be tensor array vairabler   Zread_from_arrayr   Ir   r   N)r+   )r	   r   r   r   shapeitemr   r   r   r   r   r   r   r+   r   r   r    r   r   r   r   r   r!   r   r"   )r#   r*   r$   outr&   r&   r'   r+   V   sP   (
r+   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}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 )a>  
    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 or LoDTensor. Data type: float32, float64, int32, int64 and bool.
        i (Tensor): 1-D Tensor with shape [1], 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 ``LOD_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 moder*   r   array_writez'x should be pir.OpResult, but recevied .r,   xz7array should be tensor array vairable in array_write Op.outnamer   r   Zwrite_to_arrayr-   r   r   )r2   )r	   r   r   r/   r0   create_arrayr   r   r   appendr   r   r   r   r   r   r   r   r   array_write_r   r   r    r   r   r   r   create_variabler7   r"   )r4   r*   r#   r$   r&   r&   r'   r2      s    



r2   c                 C   s  g }|durt |ttfstdt|t|}|D ]}t |ttjj	fs0tdt|qt
 r6|S t rat | tjjtjfsJtjj| } tj| }|D ]}tj||t|}qR|S tdi t }|j|j dtjjj| d}|D ]}t|t||d qz|S )	aR  
    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 ``LOD_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.]]

    NzDRequire type(initialized_list) should be list/tuple, but received {}zUAll values in `initialized_list` should be Variable or pir.OpResult, but recevied {}.r#   r5   r6   )r4   r*   r#   )r#   )r   r   tupler   formatr   r   r   r   r   r	   r   r   r   r   ZDataTypebase	frameworkZconvert_np_dtype_to_dtype_r   r8   r:   r   r   r    r;   r7   r   r2   )r   Zinitialized_listr#   valr1   r$   Ztensor_arrayr&   r&   r'   r8     sF   
r8   )N)r   Zbase.data_feederr   r   Zbase.frameworkr   Zcommon_ops_importr   r?   r   r   r	   __all__r   r+   r2   r8   r&   r&   r&   r'   <module>   s   ;
Nc