Ë
    –\;jë/  ã                   óh   — 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„ Zd„ Zdd	„Zdd
„Zy)é    Né   )Ú
check_typeÚcheck_variable_and_dtype)Úin_pir_mode)ÚVariable)ÚLayerHelperÚcoreÚin_dynamic_modec                 óR  — t        «       r"t        | t        «      sJ d«       ‚t        | «      S t	        «       r^t        | t
        j                  j                  «      r| j                  «       st        d«      ‚t
        j                  j                  | «      S t        | t        «      r1| j                  t        j                  j                   j"                  k7  rt        d«      ‚t%        d
i t'        «       ¤Ž}|j)                  d¬«      }d|_        |j-                  dd| gid|gi¬	«       |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 modeú8array should be tensor array vairable in array_length OpÚint64©ÚdtypeTÚlod_array_lengthÚXÚOut©ÚtypeÚinputsÚoutputs)Úarray_length)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_inferenceÚstop_gradientÚ	append_op)ÚarrayÚhelperÚtmps      ú\G:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/tensor/array.pyr   r      s  € ô2 ÔÜØ”4ô
ð 	GàFó	Gð 
ô �5‹zÐÜ	Œä˜5¤&§*¡*×"5Ñ"5Ô6Ø×3Ñ3Ô5äØJóð ô �‰×+Ñ+¨EÓ2Ð2ô ˜5¤(Ô+Ø�z‰zœTŸ\™\×1Ñ1×BÑBÒBäØJóð ô Ñ8¬v«xÑ8ˆØ×7Ñ7¸gÐ7ÓFˆØ ˆÔØ×ÑØ#Ø˜%˜�>Ø˜S˜E�Nð 	ô 	
ð
 ˆ
ó    c                 óî  — t        «       r[t        | t        «      sJ d«       ‚t        |t        «      sJ d«       ‚|j                  dgk(  sJ d«       ‚|j                  d«      }| |   S t        «       r_t        | t        j                  j                  «      r| j                  «       st        d«      ‚t        j                  j                  | |«      S t        |ddgd	«       t        di t!        «       ¤Ž}t        | t        «      r1| j"                  t$        j&                  j(                  j*                  k7  rt        d
«      ‚|j-                  | j.                  ¬«      }|j1                  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 ``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   Úread_from_array©r   ÚIr   r   )r2   )r
   r   r   r   ÚshapeÚitemr   r   r   r   r   r    r!   r2   r   r   r%   r   r	   r"   r#   r$   r&   r   r(   )r)   r1   r*   Úouts       r,   r2   r2   V   sy  € ôP ÔÜØ”4ô
ð 	DàCó	Dð 
ô ØŒxô
ð 	JàIó	Jð 
ð �w‰wØð
ò 
ð 	BàAó	Bð 
ð �F‰F�1‹IˆØ�Q‰xˆÜ	Œä˜5¤&§*¡*×"5Ñ"5Ô6Ø×3Ñ3Ô5äØJóð ô �‰×)Ñ)¨%°Ó3Ð3ä   C¨'¨°LÔAÜÑ6¬V«XÑ6ˆä˜5¤(Ô+Ø�z‰zœTŸ\™\×1Ñ1×BÑBÒBäÐCÓDÐDØ×7Ñ7¸e¿k¹kÐ7ÓJˆØ×ÑØ"Ø˜¨ sÑ+Ø˜S˜E�Nð 	ô 	
ð
 ˆ
r-   c                 ó>  — t        «       rÁt        | t        «      sJ d«       ‚t        |t        «      sJ d«       ‚|j                  dgk(  sJ d«       ‚|j	                  d«      }|€t        | j                  «      }t        |t        «      sJ d«       ‚|t        |«      k  sJ d«       ‚|t        |«      k  r| ||<   |S |j                  | «       |S t        «       rÚt        |dd	gd
«       t        | t        j                  j                  «      st        dt!        | «      › d�«      ‚|�?t        |t        j                  j                  «      r|j#                  «       st        d«      ‚|€)t        j$                  j                  | j                  «      }t        j$                  j'                  || |«      }|S t        |dd	gd
«       t)        | dt        d
«       t+        di t-        «       ¤Ž}|�Lt        |t        «      r1|j                   t.        j0                  j2                  j4                  k7  rt        d«      ‚|€M|j7                  |j8                  › d�t.        j0                  j2                  j4                  | j                  ¬«      }|j;                  d| g|gdœd|gi¬«       |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/   r0   r   r   zNThe index 'i' should not be greater than the length of 'array' in dygraph moder1   r   Úarray_writez'x should be pir.OpResult, but recevied Ú.r3   Úxz7array should be tensor array vairable in array_write Opú.out©Únamer   r   Úwrite_to_arrayr5   r   r   )r;   )r
   r   r   r7   r8   Úcreate_arrayr   r   r   Úappendr   r   r   r   r   r    r   r   r!   Úarray_write_r   r   r%   r	   r"   r#   r$   Úcreate_variabler@   r(   )r=   r1   r)   r*   s       r,   r;   r;   ¤   s©  € ô@ ÔÜØŒxô
ð 	PàOó	Pð 
ô ØŒxô
ð 	KàJó	Kð 
ð �w‰wØð
ò 
ð 	BàAó	Bð 
ð �F‰F�1‹IˆØˆ=Ü  §¡Ó)ˆEÜØ”4ô
ð 	GàFó	Gð 
ð ”CØó
ò 
ð 	\à[ó	\ð 
ð Œs�5‹zŠ>ØˆE�!‰Hð ˆð �L‰L˜ŒOØˆÜ	ŒÜ   C¨'¨°MÔBÜ˜!œVŸZ™Z×0Ñ0Ô1ÜØ9¼$¸q»'¸À!ÐDóð ð Ðä˜u¤f§j¡j×&9Ñ&9Ô:Ø×7Ñ7Ô9äÐ GÓHÐHØˆ=Ü—O‘O×0Ñ0°·±Ó9ˆEä—‘×,Ñ,¨U°A°qÓ9ˆØˆä   C¨'¨°MÔBÜ�1�cœH }Ô5ÜÑ7¬f«hÑ7ˆØÐä˜u¤hÔ/Ø—:‘:¤§¡×!5Ñ!5×!FÑ!FÒFäØMóð ð ˆ=Ø×*Ñ*ØŸ™�} DÐ)Ü—\‘\×)Ñ)×:Ñ:Ø—g‘gð +ó ˆEð
 	×ÑØ!Ø˜ A 3Ñ'Ø˜U˜GÐ$ð 	ô 	
ð
 ˆr-   c                 ó°  — g }|�Dt        |t        t        f«      s#t        dj	                  t        |«      «      «      ‚t        |«      }|D ]O  }t        |t        t        j                  j                  f«      rŒ.t        dj	                  t        |«      «      «      ‚ t        «       r|S t        «       r¯t        | t        j                  j                  t        j                  f«      s)t        j                   j"                  j%                  | «      } t        j&                  j)                  | «      }|D ],  }t        j&                  j+                  ||t-        |«      «      }Œ. |S t/        di t1        «       ¤Ž}|j3                  |j4                  › d�t        j                  j                  j6                  | ¬«      }|D ]  }t9        |t-        |«      |¬«       Œ |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.]]

    zDRequire type(initialized_list) should be list/tuple, but received {}zUAll values in `initialized_list` should be Variable or pir.OpResult, but recevied {}.r>   r?   )r=   r1   r)   )r)   )r   r   Útupler    Úformatr   r   r   r   r   r
   r   r	   r"   r#   ÚDataTypeÚbaseÚ	frameworkÚconvert_np_dtype_to_dtype_r!   rB   rD   r   r   r%   rE   r@   r$   r;   )r   Úinitialized_listr)   Úvalr9   r*   Útensor_arrays          r,   rB   rB     sŒ  € ð< €EØÐ#ÜÐ*¬T´5¨MÔ:ÜØV×]Ñ]ÜÐ)Ó*óóð ô
 Ð%Ó&ˆó ˆÜ˜#¤¬&¯*©*×*=Ñ*=Ð>Õ?ÜØg×nÑnÜ˜“Ióóð ð ô ÔØˆÜ	ŒÜ˜%¤$§,¡,×"6Ñ"6¼¿¹Ð!FÔGÜ—K‘K×)Ñ)×DÑDÀUÓKˆEÜ�o‰o×*Ñ*¨5Ó1ˆÛˆCÜ—/‘/×.Ñ.¨s°C¼ÀcÓ9JÓK‰Cð àˆ
äÑ1¬«Ñ1ˆØ×-Ñ-Ø—K‘K�= Ð%Ü—‘×%Ñ%×6Ñ6Øð .ó 
ˆó ˆCÜ˜#¤¨lÓ!;À<ÖPð ð Ðr-   )N)r   Úbase.data_feederr   r   Úbase.frameworkr   Úcommon_ops_importr   rK   r   r	   r
   Ú__all__r   r2   r;   rB   © r-   r,   Ú<module>rU      s8   ðó" ç CÝ (Ý (ß :Ñ :à
€ò8òvKó\`ôFEr-   