Ë
    –\;jI-  ã                   ó¢   — d dl Zd dlZd dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZ d dlmZmZ ddgZd	„ Zd
„ Zd„ Zd„ Z	 dd„Ze	 dd„«       Zy)é    N)Ú_C_opsÚin_dynamic_mode)Úconvert_dtype)Ú_current_expected_placeÚ_get_paddle_placeÚcoreÚdygraph_only)ÚLayerHelper)ÚmaxÚ	to_tensorÚsparse_coo_tensorÚsparse_csr_tensorc                 ó€   — |r;t        |«      t        | j                  «      k7  r| j                  t        |«      «      S | S )N)r   ÚdtypeÚastype)Údatar   s     ú_G:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/sparse/creation.pyÚ_handle_dtyper   #   s4   € ÙÜ˜Ó¤=°·±Ó#<Ò<Ø—;‘;œ}¨UÓ3Ó4Ð4Ø€Kó    c                 ó  — t        | j                  «      dk(  sJ ‚t        | d¬«      }|dz   }|j                  «       }t        |j                  «      dkD  r#t	        j
                  ||j                  dd  «      }t        |«      S )Né   é   )Úaxis)ÚlenÚshaper   ÚnumpyÚnpÚappendÚlist)ÚindicesÚvaluesÚlenss      r   Ú_infer_dense_shaper#   *   so   € Üˆw�}‰}Ó Ò"Ð"Ð"Üˆw˜QÔ€DØ�!‰8€DØ�:‰:‹<€DÜ
ˆ6�<‰<Ó˜1ÒÜ�y‰y˜˜vŸ|™|¨A¨BÐ/Ó0ˆÜ�‹:Ðr   c                 óÞ   — t        | «      } | €t        «       } | S t        | t        j                  t        j
                  t        j                  t        j                  f«      st        d«      ‚| S )Nz^'place' must be any of paddle.Place, paddle.CPUPlace, paddle.CUDAPinnedPlace, paddle.CUDAPlace)	r   r   Ú
isinstancer   ÚPlaceÚCPUPlaceÚCUDAPinnedPlaceÚ	CUDAPlaceÚ
ValueError)Úplaces    r   Ú
_get_placer,   4   sc   € Ü˜eÓ$€EØ€}Ü'Ó)ˆð €Lô Ø”—
‘
œDŸM™M¬4×+?Ñ+?ÄÇÁÐPôô Øló
ð 	
ð €Lr   c                 óš   — | t         j                  t         j                  t         j                  t         j                  fvrt        d«      ‚y )NzDthe dtype of indices must be 'int8' or 'int16' or 'int32' or 'int64')ÚpaddleÚint8Úint16Úint32Úint64Ú	TypeError)r   s    r   Ú_check_indices_dtyper4   A   s9   € Ø”V—[‘[¤&§,¡,´·±¼f¿l¹lÐKÑKÜØRó
ð 	
ð Lr   c           	      ó´  — t        «       �rt        |«      }t        | t        j                  j
                  «      st        | d|d¬«      } t        |t        j                  j
                  «      st        ||||«      }t        | j                  «      dk7  rt        d«      ‚| j                  d   }| j                  d   }t        | j                  «       ||j                  d   k7  r(t        dj                  ||j                  d   «      «      ‚t        |j                  «      dz
  }| j                  j                  |«      s| j                  |d	«      } |j                  j                  |«      s|j                  |d	«      }t!        ||«      }||_        t%        | |«      }	|€|	}nWt'        |«      }||	k  rt        d
|	› d|› �«      ‚t        |«      ||z   k7  r%t        dj                  ||t        |«      «      «      ‚t)        j*                  || |«      S d}
|| dœ}|d   €d|d<   d|i}t-        |
«      }|j/                  |«      }|j1                  |
|d|i|¬«       |S )a&  
    Constructs a sparse ``paddle.Tensor`` in coordinate format according to the indices
    and values of the specified non-zero elements.

    Args:
        indices(list|tuple|ndarray|Tensor): the indices of non-zero elements.
            Can be a list, tuple, numpy\.ndarray, paddle\.Tensor. The indices must be 2-D.
        values(list|tuple|ndarray|Tensor): Initial values for the tensor.
            Can be a scalar, list, tuple, numpy\.ndarray, paddle\.Tensor.
        shape(list|tuple, optional): The shape of the sparse tensor also represents the shape of
            original dense tensor. If not provided the smallest shape will be inferred to
            hold all elements.
        dtype(str|np.dtype, optional): The desired data type of returned tensor. Can be 'bool' , 'float16' ,
            'float32' , 'float64' , 'int8' , 'int16' , 'int32' , 'int64' , 'uint8',
            'complex64' , 'complex128'. Default: None, infers dtype from ``data``
            except for python float number which gets dtype from ``get_default_type`` .
        place(CPUPlace|CUDAPinnedPlace|CUDAPlace|str, optional): The place to allocate Tensor. Can be
            CPUPlace, CUDAPinnedPlace, CUDAPlace. Default: None, means global place. If ``place`` is
            string, It can be ``cpu``, ``gpu:x`` and ``gpu_pinned``, where ``x`` is the index of the GPUs.
        stop_gradient(bool, optional): Whether to block the gradient propagation of Autograd. Default: True.

    Returns:
        Tensor: A Tensor constructed from ``indices`` and ``values`` .

    Examples:

        .. code-block:: python

            >>> import paddle

            >>> indices = [[0, 1, 2], [1, 2, 0]]
            >>> values = [1.0, 2.0, 3.0]
            >>> dense_shape = [3, 3]
            >>> coo = paddle.sparse.sparse_coo_tensor(indices, values, dense_shape)
            >>> print(coo)
            Tensor(shape=[3, 3], dtype=paddle.float32, place=Place(cpu), stop_gradient=True,
                   indices=[[0, 1, 2],
                            [1, 2, 0]],
                   values=[1., 2., 3.])
    NT©r   r+   Ústop_gradientr   z'indices' must be 2-D.r   r   zKthe indices and values must have same number of non-zero, but get {} and {}Fzthe minimun shape required is z
, but get zVthe number of dimensions(len(shape) must be sparse_dim({}) + dense_dim({}), but get {}Úsparse_sparse_coo_tensor)r!   r    éÿÿÿÿr   Úout)ÚtypeÚinputsÚoutputsÚattrs)r   r,   r%   r   ÚeagerÚTensorr   r   r   r*   r4   r   Úformatr+   Ú_equalsÚ_copy_tor   r7   r#   r   r   r8   r
   Ú)create_sparse_variable_for_type_inferenceÚ	append_op)r    r!   r   r   r+   r7   ÚnnzÚ
sparse_dimÚ	dense_dimÚ	min_shapeÚop_typer<   r>   Úhelperr:   s                  r   r   r   H   sQ  € ôX ÕÜ˜5Ó!ˆä˜'¤4§:¡:×#4Ñ#4Ô5ÜØ˜t¨5ÀôˆGô ˜&¤$§*¡*×"3Ñ"3Ô4Ü˜v u¨e°]ÓCˆFÜˆw�}‰}Ó Ò"ÜÐ5Ó6Ð6à�m‰m˜AÑˆØ—]‘] 1Ñ%ˆ
ä˜WŸ]™]Ô+à�&—,‘,˜q‘/Ò!ÜØ]×dÑdØ˜Ÿ™ a™óóð ô ˜Ÿ™Ó%¨Ñ)ˆ	à�}‰}×$Ñ$ UÔ+Ø×&Ñ& u¨eÓ4ˆGà�|‰|×#Ñ# EÔ*Ø—_‘_ U¨EÓ2ˆFÜ˜v uÓ-ˆØ,ˆÔä& w°Ó7ˆ	àˆ=Ø‰Eä˜“KˆEØ�yÒ Ü Ø4°Y°K¸zÈ%ÈÐQóð ô �5‹z˜Z¨)Ñ3Ò3Ü Øl×sÑsØ" I¬s°5«zóóð ô ×.Ñ.¨v°wÀÓFÐFð -ˆØ"¨wÑ7ˆØ�‰8ÐØˆE�!‰HØ˜%Ð ˆÜ˜WÓ%ˆØ×>Ñ>¸uÓEˆØ×ÑØ °%¸°ÀUð 	ô 	
ð ˆ
r   c                 óò  — t        |«      }t        | t        j                  j                  «      st        | d|d¬«      } t        |t        j                  j                  «      st        |d|d¬«      }t        |t        j                  j                  «      st        ||||«      }t        | j                  «       t        |j                  «       t        |«      dk7  rt        |«      dk7  rt        d|› �«      ‚|t        |«      dz
     }| j                  j                  |«      s| j                  |d«      } |j                  j                  |«      s|j                  |d«      }|j                  j                  |«      s|j                  |d«      }t        ||«      }||_        t        | j                  «      dk7  s0t        |j                  «      dk7  st        |j                  «      dk7  rt        d	«      ‚t        |«      t        |«      k7  rt        d
«      ‚t        |«      dk(  rp| j                  d   |dz   k7  r(t        dj!                  | j                  d   |«      «      ‚| d   dk7  rt        d«      ‚| d   |j                  d   k7  rKt        d«      ‚| j                  d   |dz   z  dk7  r(t        dj!                  | j                  d   |«      «      ‚t        j                  j#                  | ||||«      S )a<	  
    Constructs a sparse ``paddle.Tensor`` in CSR(Compressed Sparse Row) format according to the
    ``crows``, ``cols`` and ``values``.
    Currently, the crows and cols of each batch must be incrementd.

    Args:
        crows(list|tuple|ndarray|Tensor): 1-D array, each element in the rows represents the
            starting position of the first non-zero element of each row in values.
            Can be a list, tuple, numpy\.ndarray, paddle\.Tensor.
        cols(list|tuple|ndarray|Tensor): 1-D array, the column of non-zero elements.
            Can be a list, tuple, numpy\.ndarray, paddle\.Tensor.
        values(list|tuple|ndarray|Tensor): 1-D array, the non-zero elements.
            Can be a scalar, list, tuple, numpy\.ndarray, paddle\.Tensor.
        shape(list|tuple, optional): The shape of the sparse tensor also represents the shape of
            original dense tensor.
            hold all elements.
        dtype(str|np.dtype, optional): The desired data type of returned tensor. Can be 'bool' , 'float16' ,
            'float32' , 'float64' , 'int8' , 'int16' , 'int32' , 'int64' , 'uint8',
            'complex64' , 'complex128'. Default: None, infers dtype from ``data``
            except for python float number which gets dtype from ``get_default_type`` .
        place(CPUPlace|CUDAPinnedPlace|CUDAPlace|str, optional): The place to allocate Tensor. Can be
            CPUPlace, CUDAPinnedPlace, CUDAPlace. Default: None, means global place. If ``place`` is
            string, It can be ``cpu``, ``gpu:x`` and ``gpu_pinned``, where ``x`` is the index of the GPUs.
        stop_gradient(bool, optional): Whether to block the gradient propagation of Autograd. Default: True.

    Returns:
        Tensor: A Tensor constructed from ``crows``, ``cols`` and ``values`` .

    Examples:

        .. code-block:: python

            >>> import paddle

            >>> crows = [0, 2, 3, 5]
            >>> cols = [1, 3, 2, 0, 1]
            >>> values = [1, 2, 3, 4, 5]
            >>> dense_shape = [3, 4]
            >>> csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape)
            >>> print(csr)
            Tensor(shape=[3, 4], dtype=paddle.int64, place=Place(cpu), stop_gradient=True,
                   crows=[0, 2, 3, 5],
                   cols=[1, 3, 2, 0, 1],
                   values=[1, 2, 3, 4, 5])
    NTr6   r   é   z>SparseCsrTensor only support 2-D or 3-D matrix. but get shape Fr   z-The 'crows', 'cols' and 'values' must be 1-D.z3the length of cols must be same as length of valuesr   zBThe length({}) of crows must be equal to the rows({})+1 of matrix.z the 0th value of crows must be 0r9   z<the last value of crows must be equal the number of non-zerozCThe length({}) of crows must be divisible the rows({})+1 of matrix.)r,   r%   r   r?   r@   r   r4   r   r   r*   r+   rB   rC   r   r7   r   rA   r   )ÚcrowsÚcolsr!   r   r   r+   r7   Úrowss           r   r   r   ¸   s�  € ôd �uÓ€Eä�eœTŸZ™Z×.Ñ.Ô/Ü˜% t°5ÈÔMˆÜ�dœDŸJ™J×-Ñ-Ô.Ü˜ T°ÀdÔKˆÜ�fœdŸj™j×/Ñ/Ô0Ü˜6 5¨%°Ó?ˆä˜Ÿ™Ô%Ü˜Ÿ™Ô$ä
ˆ5ƒz�Q‚œ3˜u›:¨š?ÜØLÈUÈGÐTó
ð 	
ð ”�U“˜a‘Ñ €Dà�;‰;×Ñ˜uÔ%Ø—‘˜u eÓ,ˆà�:‰:×Ñ˜eÔ$Ø�}‰}˜U EÓ*ˆà�<‰<×Ñ Ô&Ø—‘ ¨Ó.ˆÜ˜6 5Ó)€FØ(€FÔä
ˆ5�;‰;Ó˜1Ò¤ D§J¡J£°1Ò 4¼¸F¿L¹LÓ8IÈQÒ8NÜÐHÓIÐIä
ˆ4ƒy”C˜“KÒÜÐNÓOÐOä
ˆ5ƒz�Q‚Ø�;‰;�q‰>˜T A™XÒ%ÜØT×[Ñ[Ø—K‘K ‘N Dóóð ð
 �‰8�qŠ=ÜÐ?Ó@Ð@à�‰9˜Ÿ™ Q™Ò'ÜØNóð ð �;‰;�q‰>˜T A™XÑ&¨!Ò+ÜØU×\Ñ\Ø—K‘K ‘N Dóóð ô �:‰:×'Ñ'Øˆt�V˜U Móð r   )NNNT)NNT)r   r   r.   r   r   Úpaddle.base.data_feederr   Úpaddle.base.frameworkr   r   r   r	   Úpaddle.base.layer_helperr
   Úpaddle.tensorr   r   Ú__all__r   r#   r,   r4   r   r   © r   r   Ú<module>rW      so   ðó ã ß *Ý 1÷ó õ 1ß (ð Øð€òòò
ò
ð HLólð` àFJòmó ñmr   