o
    Ci                      @   sT   d dl Zd dlT dddZdddZddd	Zddedfd
dZdedfddZdS )    N)*c                 C   sZ   |du r
t t| }t }t }t|| D ]\}}|| || qt||||}|S )z builds the C++ vectors for the GPU indices and the
    resources. Handles the case where the resources are assigned to
    the list of GPUs N)rangelenGpuResourcesVectorInt32Vectorzip	push_backindex_cpu_to_gpu_multiple)	resourcesindexcogpusvresvdevires r   j/var/www/html/fyndo/python/python_agents/rag_suite/venv/lib/python3.10/site-packages/faiss/gpu_wrappers.pyindex_cpu_to_gpu_multiple_py   s   
r   c                 C   s   t | |d |d}|S )N)r   r   ngpu)index_cpu_to_gpus_list)r   r   r   	index_gpur   r   r   index_cpu_to_all_gpus$   s   r   c                 C   sT   |du r|dkrt t }n|du r|dkrt |}dd |D }t|| ||}|S )z Here we can pass list of GPU ids as a parameter or ngpu to
    use first n GPU's. gpus mut be a list or None.
    co is a GpuMultipleClonerOptions
    Nr   c                 S   s   g | ]}t  qS r   )StandardGpuResources).0_r   r   r   
<listcomp>2   s    z*index_cpu_to_gpus_list.<locals>.<listcomp>)r   get_num_gpusr   )r   r   r   r   r   r   r   r   r   r   )   s   r   c                 C   s&  |j \}}	|jjrd}
n|jjr|j}d}
n	tj|dd}d}
t|}|jtj	kr,t
}n|jtjkr5t}ntd|j \}}||	ksDJ |jjrKd}n|jjrU|j}d}n	tj|dd}d}t|}|jtj	krkt
}n|jtjkrtt}ntd|du rtj||ftj	d}n|j ||fksJ |jtj	ksJ t|}|du rtj||ftjd}n	|j ||fksJ t|}|jtjkrt}n|j|j  krtjkrn tdt}ntdt }||_||_|	|_||_||_||_||_||_|
|_||_||_||_||_ ||_!||_"t#| | ||fS )	aR  
    Compute the k nearest neighbors of a vector on one GPU without constructing an index

    Parameters
    ----------
    res : StandardGpuResources
        GPU resources to use during computation
    xq : array_like
        Query vectors, shape (nq, d) where d is appropriate for the index.
        `dtype` must be float32.
    xb : array_like
        Database vectors, shape (nb, d) where d is appropriate for the index.
        `dtype` must be float32.
    k : int
        Number of nearest neighbors.
    D : array_like, optional
        Output array for distances of the nearest neighbors, shape (nq, k)
    I : array_like, optional
        Output array for the nearest neighbors, shape (nq, k)
    metric : MetricType, optional
        Distance measure to use (either METRIC_L2 or METRIC_INNER_PRODUCT)
    device: int, optional
        Which CUDA device in the system to run the search on. -1 indicates that
        the current thread-local device state (via cudaGetDevice) should be used
        (can also be set via torch.cuda.set_device in PyTorch)
        Otherwise, an integer 0 <= device < numDevices indicates the GPU on which
        the computation should be run

    Returns
    -------
    D : array_like
        Distances of the nearest neighbors, shape (nq, k)
    I : array_like
        Labels of the nearest neighbors, shape (nq, k)
    TFfloat32dtypezxq must be f32 or f16xb must be float32 or float16NzI must be i64 or i32)$shapeflagsc_contiguousf_contiguousTnpascontiguousarrayswig_ptrr!   r   DistanceDataType_F32float16DistanceDataType_F16	TypeErroremptyint64IndicesDataType_I64int32IndicesDataType_I32GpuDistanceParamsmetrickdimsvectorsvectorsRowMajor
vectorType
numVectorsqueriesqueriesRowMajor	queryType
numQueriesoutDistances
outIndicesoutIndicesTypedevicebfKnn)r   xqxbr6   DIr5   rC   nqdxq_row_majorxq_ptrxq_typenbd2xb_row_majorxb_ptrxb_typeD_ptrI_ptrI_typeargsr   r   r   knn_gpu9   s|   
$

rW   c                 C   s  |j \}}|jjrd}n|jjr|j}d}ntdt|}	|jtj	kr't
}
n|jtjkr0t}
n	tj|dd}d}|j \}}||ksDJ |jjrKd}n|jjrU|j}d}n	tj|dd}d}t|}|jtj	krkt
}n|jtjkrtt}ntd|du rtj||ftj	d}n|j ||fksJ |jtj	ksJ t|}t }||_d|_||_||_||_||_||_|	|_||_|
|_||_||_||_t| | |S )	ak  
    Compute all pairwise distances between xq and xb on one GPU without constructing an index

    Parameters
    ----------
    res : StandardGpuResources
        GPU resources to use during computation
    xq : array_like
        Query vectors, shape (nq, d) where d is appropriate for the index.
        `dtype` must be float32.
    xb : array_like
        Database vectors, shape (nb, d) where d is appropriate for the index.
        `dtype` must be float32.
    D : array_like, optional
        Output array for all pairwise distances, shape (nq, nb)
    metric : MetricType, optional
        Distance measure to use (either METRIC_L2 or METRIC_INNER_PRODUCT)
    device: int, optional
        Which CUDA device in the system to run the search on. -1 indicates that
        the current thread-local device state (via cudaGetDevice) should be used
        (can also be set via torch.cuda.set_device in PyTorch)
        Otherwise, an integer 0 <= device < numDevices indicates the GPU on which
        the computation should be run

    Returns
    -------
    D : array_like
        All pairwise distances, shape (nq, nb)
    TFz5xq matrix should be row (C) or column-major (Fortran)r   r    r"   Nr   )r#   r$   r%   r&   r'   r.   r*   r!   r(   r   r+   r,   r-   r)   r/   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rC   rD   )r   rE   rF   rG   r5   rC   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rV   r   r   r   pairwise_distance_gpu   sh   


rX   )NN)Nr   )NNr   )	numpyr(   faiss.loaderr   r   r   	METRIC_L2rW   rX   r   r   r   r   <module>   s   



|