a
    ^g#                     @   sZ   d dl Zd dlT dddZdddZddd	Zddedd
d d fddZdedfddZdS )    N)*c                 C   sr   |du rt t| }t }t }t|| D ]\}}|| || q*t|tr`t||||S t	||||S d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lenZGpuResourcesVectorZInt32VectorzipZ	push_back
isinstanceZIndexBinaryZ index_binary_cpu_to_gpu_multipleZindex_cpu_to_gpu_multiple)	resourcesindexcogpusZvresZvdevires r   `/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/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 r4|dkr4t |}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   )ZStandardGpuResources).0_r   r   r   
<listcomp>4       z*index_cpu_to_gpus_list.<locals>.<listcomp>)r   Zget_num_gpusr   )r   r	   r
   r   r   r   r   r   r   r   +   s    r   Fc                 C   sZ  |j \}}|jjrd}n&|jjr,|j}d}ntj|dd}d}t|}|jtj	krXt
}n|jtjkrjt}ntd|j \}}||ksJ |jjrd}n&|jjr|j}d}ntj|dd}d}t|}|jtj	krt
}n|jtjkrt}ntd|du rtj||ftj	d}n&|j ||fks$J |jtj	ks6J t|}|du r^tj||ftjd}n|j ||fksrJ t|}|jtjkrt}n0|j|j  krtjkrn nt}ntdt }||_||_||_||_||_||_||_||_||_||_||_||_||_ ||_!||_"||_#|	d	ks8|
d	krHt$| ||	|
 n
t%| | ||fS )
a  
    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
    vectorsMemoryLimit: int, optional
    queriesMemoryLimit: int, optional
        Memory limits for vectors and queries.
        If not 0, the GPU will use at most this amount of memory
        for vectors and queries respectively.
        Vectors are broken up into chunks of size vectorsMemoryLimit,
        and queries are broken up into chunks of size queriesMemoryLimit,
        including the memory required for the results.

    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 i32r   )&shapeflagsc_contiguousf_contiguousTnpascontiguousarrayswig_ptrr   r   DistanceDataType_F32float16DistanceDataType_F16	TypeErroremptyZint64ZIndicesDataType_I64Zint32ZIndicesDataType_I32GpuDistanceParamsmetrickdimsvectorsvectorsRowMajor
vectorType
numVectorsqueriesqueriesRowMajor	queryType
numQueriesoutDistancesZ
outIndicesZoutIndicesTypedeviceuse_cuvsZbfKnn_tilingbfKnn)r   xqxbr,   DIr+   r7   r8   ZvectorsMemoryLimitZqueriesMemoryLimitnqdxq_row_majorxq_ptrxq_typenbd2xb_row_majorxb_ptrxb_typeD_ptrZI_ptrZI_typeargsr   r   r   knn_gpu;   s    ,



"
rJ   c                 C   s  |j \}}|jjrd}n|jjr,|j}d}ntdt|}	|jtj	krNt
}
n$|jtjkr`t}
ntj|dd}d}|j \}}||ksJ |jjrd}n&|jjr|j}d}ntj|dd}d}t|}|jtj	krt
}n|jtjkrt}ntd|du rtj||ftj	d}n&|j ||fks$J |jtj	ks6J 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)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r9   )r   r:   r;   r<   r+   r7   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   r   r   r   pairwise_distance_gpu   sh    



rK   )NN)Nr   )NNr   )	numpyr"   Zfaiss.loaderr   r   r   Z	METRIC_L2rJ   rK   r   r   r   r   <module>   s   


 	