o
    Ne!                     @   sX   d dl Z d dlmZ d dlmZ d dlmZ d dl mZmZ g Z						d
dd	Z
dS )    N)LayerHelper)_non_static_mode)check_variable_and_dtype)_C_ops_legacy_C_opsFc                 C   s@  |r
|du r
t d|durdnd}t r2t| ||||d|d|d|\}	}
}|r.|	|
|fS |	|
fS t| dd	d
 t|dd	d
 t|dd	d
 |rPt|dd	d
 |rYt|dd	d
 tdi t }|j| jd}	|j| jd}
|j| jd}|j	d
| |||r|nd|r|ndd|	|
|d|||dd |r|	|
|fS |	|
fS )a  
    Graph Sample Neighbors API.

    This API is mainly used in Graph Learning domain, and the main purpose is to
    provide high performance of graph sampling method. For example, we get the
    CSC(Compressed Sparse Column) format of the input graph edges as `row` and
    `colptr`, so as to convert graph data into a suitable format for sampling.
    `input_nodes` means the nodes we need to sample neighbors, and `sample_sizes`
    means the number of neighbors and number of layers we want to sample.

    Besides, we support fisher-yates sampling in GPU version.

    Args:
        row (Tensor): One of the components of the CSC format of the input graph, and
                      the shape should be [num_edges, 1] or [num_edges]. The available
                      data type is int32, int64.
        colptr (Tensor): One of the components of the CSC format of the input graph,
                         and the shape should be [num_nodes + 1, 1] or [num_nodes + 1].
                         The data type should be the same with `row`.
        input_nodes (Tensor): The input nodes we need to sample neighbors for, and the
                              data type should be the same with `row`.
        sample_size (int): The number of neighbors we need to sample. Default value is -1,
                           which means returning all the neighbors of the input nodes.
        eids (Tensor): The eid information of the input graph. If return_eids is True,
                            then `eids` should not be None. The data type should be the
                            same with `row`. Default is None.
        return_eids (bool): Whether to return eid information of sample edges. Default is False.
        perm_buffer (Tensor): Permutation buffer for fisher-yates sampling. If `use_perm_buffer`
                              is True, then `perm_buffer` should not be None. The data type should
                              be the same with `row`. If not None, we will use fiser-yates sampling
                              to speed up. Only useful for gpu version.
        name (str, optional): Name for the operation (optional, default is None).
                              For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        - out_neighbors (Tensor), the sample neighbors of the input nodes.

        - out_count (Tensor), the number of sampling neighbors of each input node, and the shape
                              should be the same with `input_nodes`.

        - out_eids (Tensor), if `return_eids` is True, we will return the eid information of the
                             sample edges.

    Examples:
        .. code-block:: python

            import paddle
            # edges: (3, 0), (7, 0), (0, 1), (9, 1), (1, 2), (4, 3), (2, 4),
            #        (9, 5), (3, 5), (9, 6), (1, 6), (9, 8), (7, 8)
            row = [3, 7, 0, 9, 1, 4, 2, 9, 3, 9, 1, 9, 7]
            colptr = [0, 2, 4, 5, 6, 7, 9, 11, 11, 13, 13]
            nodes = [0, 8, 1, 2]
            sample_size = 2
            row = paddle.to_tensor(row, dtype="int64")
            colptr = paddle.to_tensor(colptr, dtype="int64")
            nodes = paddle.to_tensor(nodes, dtype="int64")
            out_neighbors, out_count = paddle.geometric.sample_neighbors(row, colptr, nodes, sample_size=sample_size)

    Nz3`eids` should not be None if `return_eids` is True.TFsample_sizereturn_eidsflag_perm_bufferRow)Zint32Zint64graph_sample_neighborsCol_PtrXEidsPerm_Buffersample_neighbors)dtype)r   r   r   r   r   )ZOutZ	Out_CountZOut_Eids)r   r	   r
   )typeZinputsZoutputsattrs)r   )

ValueErrorr   r   r   r   r   localsZ"create_variable_for_type_inferencer   Z	append_op)rowZcolptrZinput_nodesr   Zeidsr	   Zperm_buffernameZuse_perm_bufferZout_neighborsZ	out_countZout_eidshelper r   SD:\Projects\ConvertPro\env\Lib\site-packages\paddle/geometric/sampling/neighbors.pyr      s   F



r   )r   NFNN)ZpaddleZpaddle.fluid.layer_helperr   Zpaddle.fluid.frameworkr   Zpaddle.fluid.data_feederr   r   r   __all__r   r   r   r   r   <module>   s   