Ë
    ˆ\;jþ/  ã                   ó`   — d dl Z d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 g Z
	 d	d„Z	 d	d„Zy)
é    N)Ú_C_ops)Úcheck_variable_and_dtype)ÚVariable)ÚLayerHelper)Úin_dynamic_modec           	      ó  — |�|�dnd}t        «       r"t        j                  | ||||«      \  }}}	|||	fS t        | ddd«       t        |ddd«       t        |dd	d«       |rt        |d
d	d«       t        |dd	d«       t	        di t        «       ¤Ž}
|
j                  | j                  ¬«      }|
j                  | j                  ¬«      }|
j                  | j                  ¬«      }	|
j                  d| |||r|nd|r|nddœ|||	dœ¬«       |||	fS )aš  

    Reindex Graph API.

    This API is mainly used in Graph Learning domain, which should be used
    in conjunction with `paddle.geometric.sample_neighbors` API. And the main purpose
    is to reindex the ids information of the input nodes, and return the
    corresponding graph edges after reindex.

    Take input nodes x = [0, 1, 2] as an example. If we have neighbors = [8, 9, 0, 4, 7, 6, 7], and count = [2, 3, 2],
    then we know that the neighbors of 0 is [8, 9], the neighbors of 1 is [0, 4, 7], and the neighbors of 2 is [6, 7].
    Then after graph_reindex, we will have 3 different outputs: reindex_src: [3, 4, 0, 5, 6, 7, 6], reindex_dst: [0, 0, 1, 1, 1, 2, 2]
    and out_nodes: [0, 1, 2, 8, 9, 4, 7, 6]. We can see that the numbers in `reindex_src` and `reindex_dst` is the corresponding index
    of nodes in `out_nodes`.

    Note:
        The number in x should be unique, otherwise it would cause potential errors. We will reindex all the nodes from 0.

    Args:
        x (Tensor): The input nodes which we sample neighbors for. The available
                    data type is int32, int64.
        neighbors (Tensor): The neighbors of the input nodes `x`. The data type
                            should be the same with `x`.
        count (Tensor): The neighbor count of the input nodes `x`. And the
                        data type should be int32.
        value_buffer (Tensor, optional): Value buffer for hashtable. The data type should be int32,
                                    and should be filled with -1. Only useful for gpu version. Default is None.
        index_buffer (Tensor, optional): Index buffer for hashtable. The data type should be int32,
                                    and should be filled with -1. Only useful for gpu version.
                                    `value_buffer` and `index_buffer` should be both not None
                                    if you want to speed up by using hashtable buffer. Default is None.
        name (str, optional): Name for the operation (optional, default is None).
                              For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        - reindex_src (Tensor), the source node index of graph edges after reindex.

        - reindex_dst (Tensor), the destination node index of graph edges after reindex.

        - out_nodes (Tensor), the index of unique input nodes and neighbors before reindex, where we put the input nodes `x` in the front, and put neighbor nodes in the back.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = [0, 1, 2]
            >>> neighbors = [8, 9, 0, 4, 7, 6, 7]
            >>> count = [2, 3, 2]
            >>> x = paddle.to_tensor(x, dtype="int64")
            >>> neighbors = paddle.to_tensor(neighbors, dtype="int64")
            >>> count = paddle.to_tensor(count, dtype="int32")
            >>> reindex_src, reindex_dst, out_nodes = paddle.geometric.reindex_graph(x, neighbors, count)
            >>> print(reindex_src.numpy())
            [3 4 0 5 6 7 6]
            >>> print(reindex_dst.numpy())
            [0 0 1 1 1 2 2]
            >>> print(out_nodes.numpy())
            [0 1 2 8 9 4 7 6]

    NTFÚX©Úint32Úint64Úgraph_reindexÚ	NeighborsÚCountr   ÚHashTable_ValueÚHashTable_Index©Údtype©r	   r   r   r   r   ©ÚReindex_SrcÚReindex_DstÚ	Out_Nodes©ÚtypeÚinputsÚoutputs)Úreindex_graph)	r   r   r   r   r   ÚlocalsÚ"create_variable_for_type_inferencer   Ú	append_op©ÚxÚ	neighborsÚcountÚvalue_bufferÚindex_bufferÚnameÚuse_buffer_hashtableÚreindex_srcÚreindex_dstÚ	out_nodesÚhelpers              úaG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/geometric/reindex.pyr   r      sW  € ðB Ð(¨\Ð-E‰È5ð ô ÔÜ.4×.BÑ.BØØØØØó/
Ñ+ˆ�[ )ð ˜K¨Ð2Ð2ä˜Q Ð%7¸ÔIÜØ�;Ð 2°Oôô ˜U G¨g¸ÔHáÜ ØÐ+¨g¸ô	
ô 	!ØÐ+¨g¸ô	
ô Ñ5¬F«HÑ5€FØ×;Ñ;À!Ç'Á'Ð;ÓJ€KØ×;Ñ;À!Ç'Á'Ð;ÓJ€KØ×9Ñ9ÀÇÁÐ9ÓH€IØ
×ÑØàØ"ØÙ/C™|ÈÙ/C™|Èñ
ð 'Ø&Ø"ñ
ð ô ð ˜ YÐ.Ð.ó    c           	      ó~  — |�|�dnd}t        «       rPt        j                  |d¬«      }t        j                  |d¬«      }t        j                  | ||||«      \  }}}	|||	fS t        |t        «      r|g}t        |t        «      r|g}t        j                  |d¬«      }t        j                  |d¬«      }t        | ddd«       t        |d	dd
«       t        |ddd
«       |rt        |ddd
«       t        |ddd
«       t        di t        «       ¤Ž}
|
j                  | j                  ¬«      }|
j                  | j                  ¬«      }|
j                  | j                  ¬«      }	t        j                  |d¬«      }t        j                  |d¬«      }|
j                  d
| |||r|nd|r|nddœ|||	dœ¬«       |||	fS )a5  

    Reindex HeterGraph API.

    This API is mainly used in Graph Learning domain, which should be used
    in conjunction with `paddle.geometric.sample_neighbors` API. And the main purpose
    is to reindex the ids information of the input nodes, and return the
    corresponding graph edges after reindex.

    Take input nodes x = [0, 1, 2] as an example. For graph A, suppose we have neighbors = [8, 9, 0, 4, 7, 6, 7], and count = [2, 3, 2],
    then we know that the neighbors of 0 is [8, 9], the neighbors of 1 is [0, 4, 7], and the neighbors of 2 is [6, 7]. For graph B,
    suppose we have neighbors = [0, 2, 3, 5, 1], and count = [1, 3, 1], then we know that the neighbors of 0 is [0], the neighbors of 1 is [2, 3, 5],
    and the neighbors of 3 is [1]. We will get following outputs: reindex_src: [3, 4, 0, 5, 6, 7, 6, 0, 2, 8, 9, 1], reindex_dst: [0, 0, 1, 1, 1, 2, 2, 0, 1, 1, 1, 2]
    and out_nodes: [0, 1, 2, 8, 9, 4, 7, 6, 3, 5].

    Note:
        The number in x should be unique, otherwise it would cause potential errors. We support multi-edge-types neighbors reindexing in reindex_heter_graph api. We will reindex all the nodes from 0.

    Args:
        x (Tensor): The input nodes which we sample neighbors for. The available
                    data type is int32, int64.
        neighbors (list|tuple): The neighbors of the input nodes `x` from different graphs.
                                The data type should be the same with `x`.
        count (list|tuple): The neighbor counts of the input nodes `x` from different graphs.
                            And the data type should be int32.
        value_buffer (Tensor, optional): Value buffer for hashtable. The data type should be int32,
                                    and should be filled with -1. Only useful for gpu version. Default is None.
        index_buffer (Tensor, optional): Index buffer for hashtable. The data type should be int32,
                                    and should be filled with -1. Only useful for gpu version.
                                    `value_buffer` and `index_buffer` should be both not None
                                    if you want to speed up by using hashtable buffer. Default is None.
        name (str, optional): Name for the operation (optional, default is None).
                              For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        - reindex_src (Tensor), the source node index of graph edges after reindex.

        - reindex_dst (Tensor), the destination node index of graph edges after reindex.

        - out_nodes (Tensor), the index of unique input nodes and neighbors before reindex,
                              where we put the input nodes `x` in the front, and put neighbor
                              nodes in the back.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = [0, 1, 2]
            >>> neighbors_a = [8, 9, 0, 4, 7, 6, 7]
            >>> count_a = [2, 3, 2]
            >>> x = paddle.to_tensor(x, dtype="int64")
            >>> neighbors_a = paddle.to_tensor(neighbors_a, dtype="int64")
            >>> count_a = paddle.to_tensor(count_a, dtype="int32")
            >>> neighbors_b = [0, 2, 3, 5, 1]
            >>> count_b = [1, 3, 1]
            >>> neighbors_b = paddle.to_tensor(neighbors_b, dtype="int64")
            >>> count_b = paddle.to_tensor(count_b, dtype="int32")
            >>> neighbors = [neighbors_a, neighbors_b]
            >>> count = [count_a, count_b]
            >>> reindex_src, reindex_dst, out_nodes = paddle.geometric.reindex_heter_graph(x, neighbors, count)
            >>> print(reindex_src.numpy())
            [3 4 0 5 6 7 6 0 2 8 9 1]
            >>> print(reindex_dst.numpy())
            [0 0 1 1 1 2 2 0 1 1 1 2]
            >>> print(out_nodes.numpy())
            [0 1 2 8 9 4 7 6 3 5]

    NTFr   )Úaxisr	   r
   Úheter_graph_reindexr   r   r   r   r   r   r   r   r   r   )Úreindex_heter_graph)r   ÚpaddleÚconcatr   r   Ú
isinstancer   r   r   r   r   r   r    r!   s              r-   r2   r2   ‹   sÚ  € ðR Ð(¨\Ð-E‰È5ð ô ÔÜ—M‘M )°!Ô4ˆ	Ü—‘˜e¨!Ô,ˆÜ.4×.BÑ.BØØØØØó/
Ñ+ˆ�[ )ð ˜K¨Ð2Ð2ä�)œXÔ&Ø�Kˆ	Ü�%œÔ"Ø�ˆä—‘˜i¨aÔ0€IÜ�M‰M˜% aÔ(€Eä˜Q Ð%7Ð9NÔOÜØ�;Ð 2°Oôô ˜U G¨g¸ÔHáÜ ØÐ+¨g¸ô	
ô 	!ØÐ+¨g¸ô	
ô Ñ;´&³(Ñ;€FØ×;Ñ;À!Ç'Á'Ð;ÓJ€KØ×;Ñ;À!Ç'Á'Ð;ÓJ€KØ×9Ñ9ÀÇÁÐ9ÓH€IÜ—‘˜i¨aÔ0€IÜ�M‰M˜% aÔ(€EØ
×ÑØàØ"ØÙ/C™|ÈÙ/C™|Èñ
ð 'Ø&Ø"ñ
ð ô ð ˜ YÐ.Ð.r.   )NNN)r3   r   Úpaddle.base.data_feederr   Úpaddle.base.frameworkr   Úpaddle.base.layer_helperr   Úpaddle.frameworkr   Ú__all__r   r2   © r.   r-   Ú<module>r<      s9   ðó Ý Ý <Ý *Ý 0Ý ,à
€ð EIóo/ðf EIôC/r.   