o
    "j                     @   sb   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	 e	dddd	d
				dddZ
dS )    )_C_ops)check_variable_and_dtype)LayerHelper)in_dynamic_mode)
deprecatedz2.4.0zpaddle.geometric.reindex_graph   z7paddle.incubate.graph_reindex will be removed in future)ZsinceZ	update_tolevelreasonNFc                 C   s  |r|du s
|du rt dt r"t| ||||\}}}	|||	fS t| ddd t|ddd t|ddd |rGt|d	dd t|d
dd tdi t }
|
j| jd}|
j| jd}|
j| jd}	|
j	d| |||rn|nd|rs|ndd|||	dd |||	fS )a*  

    Graph Reindex API.

    This API is mainly used in Graph Learning domain, which should be used
    in conjunction with `graph_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.

    Notes:
        The number in x should be unique, otherwise it would cause potential errors.
        Besides, we also support multi-edge-types neighbors reindexing. If we have different
        edge_type neighbors for x, we should concatenate all the neighbors and count of x.
        We will reindex all the nodes from 0.

    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].

    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. Default is None.
        index_buffer (Tensor, optional): Index buffer for hashtable. The data type should
                                    be int32, and should be filled with -1. Default is None.
        flag_buffer_hashtable (bool, optional): Whether to use buffer for hashtable to speed up.
                                      Default is False. Only useful for gpu version currently.
        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_e1 = [8, 9, 0, 4, 7, 6, 7]
            >>> count_e1 = [2, 3, 2]
            >>> x = paddle.to_tensor(x, dtype="int64")
            >>> neighbors_e1 = paddle.to_tensor(neighbors_e1, dtype="int64")
            >>> count_e1 = paddle.to_tensor(count_e1, dtype="int32")

            >>> reindex_src, reindex_dst, out_nodes = paddle.incubate.graph_reindex(
            ...     x,
            ...     neighbors_e1,
            ...     count_e1,
            ... )
            >>> print(reindex_src)
            Tensor(shape=[7], dtype=int64, place=Place(cpu), stop_gradient=True,
            [3, 4, 0, 5, 6, 7, 6])
            >>> print(reindex_dst)
            Tensor(shape=[7], dtype=int64, place=Place(cpu), stop_gradient=True,
            [0, 0, 1, 1, 1, 2, 2])
            >>> print(out_nodes)
            Tensor(shape=[8], dtype=int64, place=Place(cpu), stop_gradient=True,
            [0, 1, 2, 8, 9, 4, 7, 6])

            >>> neighbors_e2 = [0, 2, 3, 5, 1]
            >>> count_e2 = [1, 3, 1]
            >>> neighbors_e2 = paddle.to_tensor(neighbors_e2, dtype="int64")
            >>> count_e2 = paddle.to_tensor(count_e2, dtype="int32")

            >>> neighbors = paddle.concat([neighbors_e1, neighbors_e2])
            >>> count = paddle.concat([count_e1, count_e2])
            >>> reindex_src, reindex_dst, out_nodes = paddle.incubate.graph_reindex(x, neighbors, count)
            >>> print(reindex_src)
            Tensor(shape=[12], dtype=int64, place=Place(cpu), stop_gradient=True,
            [3, 4, 0, 5, 6, 7, 6, 0, 2, 8, 9, 1])
            >>> print(reindex_dst)
            Tensor(shape=[12], dtype=int64, place=Place(cpu), stop_gradient=True,
            [0, 0, 1, 1, 1, 2, 2, 0, 1, 1, 1, 2])
            >>> print(out_nodes)
            Tensor(shape=[10], dtype=int64, place=Place(cpu), stop_gradient=True,
            [0, 1, 2, 8, 9, 4, 7, 6, 3, 5])

    NzW`value_buffer` and `index_buffer` should notbe None if `flag_buffer_hashtable` is True.X)int32Zint64graph_reindex	NeighborsCountr   HashTable_ValueHashTable_Index)dtype)r
   r   r   r   r   )ZReindex_SrcZReindex_DstZ	Out_Nodes)typeZinputsZoutputs)r   )

ValueErrorr   r   Zreindex_graphr   r   localsZ"create_variable_for_type_inferencer   Z	append_op)xZ	neighborscountZvalue_bufferZindex_bufferZflag_buffer_hashtablenameZreindex_srcZreindex_dstZ	out_nodeshelper r   h/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/incubate/operators/graph_reindex.pyr      sX   h




r   )NNFN)Zpaddler   Zpaddle.base.data_feederr   Zpaddle.base.layer_helperr   Zpaddle.frameworkr   Zpaddle.utilsr   r   r   r   r   r   <module>   s    
