
    i#                     \    S SK rS SK7  S
S jrSS jrSS jrSS\SSS S 4S jrS\S4S	 jrg)    N)*c                    Uc  [        [        U 5      5      n[        5       n[        5       n[	        X05       H'  u  pgUR                  U5        UR                  U5        M)     [        U[        5      (       a  [        XEX5      $ [        XEX5      $ )zbuilds the C++ vectors for the GPU indices and the
resources. Handles the case where the resources are assigned to
the list of GPUs )
rangelenGpuResourcesVectorInt32Vectorzip	push_back
isinstanceIndexBinary index_binary_cpu_to_gpu_multipleindex_cpu_to_gpu_multiple)	resourcesindexcogpusvresvdeviress           b/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/faiss/gpu_wrappers.pyindex_cpu_to_gpu_multiple_pyr      sx     |S^$D=Dd&qs ' %%%/EFF(U??    c                     [        XS US9nU$ )N)r   r   ngpu)index_cpu_to_gpus_list)r   r   r   	index_gpus       r   index_cpu_to_all_gpusr   &   s    &u$TJIr   c                     Uc  US:X  a  [        [        5       5      nOUc  US:w  a  [        U5      nU Vs/ s H  n[        5       PM     nn[        XPX5      nU$ s  snf )zHere 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
r   )r   get_num_gpusStandardGpuResourcesr   )r   r   r   r   _r   r   s          r   r   r   +   s^    
 	42:\^$
,TRZT{+/
04a!4C
0,SBI 1s   AFc                    UR                   u  pUR                  R                  (       a  SnOAUR                  R                  (       a  UR                  nSnO[
        R                  " USS9nSn[        U5      nUR                  [
        R                  :X  a  [        nO0UR                  [
        R                  :X  a  [        nO[        S5      eUR                   u  nnUU:X  d   eUR                  R                  (       a  SnOAUR                  R                  (       a  UR                  nSnO[
        R                  " USS9nSn[        U5      nUR                  [
        R                  :X  a  [        nO0UR                  [
        R                  :X  a  [        nO[        S5      eUc%  [
        R                  " X4[
        R                  S9nO3UR                   X4:X  d   eUR                  [
        R                  :X  d   e[        U5      nUc%  [
        R                  " X4[
        R                  S9nOUR                   X4:X  d   e[        U5      nUR                  [
        R                  :X  a  [         nODUR                  UR                  s=:X  a  [
        R"                  :X  a
  O  O[$        nO[        S5      e['        5       nUUl        UUl        UUl        UUl        UUl        UUl        UUl        UUl        UUl        UUl        UUl        UUl        UUl         UUl!        UUl"        UUl#        U	S:  d  U
S:  a  [I        U UX5        XE4$ [K        U U5        XE4$ )	ac  
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 float16zI must be i64 or i32r   )&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use_cuvsbfKnn_tilingbfKnn)r   xqxbr<   DIr;   rI   rJ   vectorsMemoryLimitqueriesMemoryLimitnqdxq_row_majorxq_ptrxq_typenbd2xb_row_majorxb_ptrxb_typeD_ptrI_ptrI_typeargss                            r   knn_gpura   ;   s   X HHEB	xx			TT!!"I6b\F	xx2::&	RZZ	&/00XXFB7N7	xx			TT!!"I6b\F	xx2::&	RZZ	&788yHHbWBJJ/ww2'!!!ww"**$$$QKEyHHbWBHH-ww2'!!!QKEww"(($	
AGG	'rxx	'$.//DDKDFDIDL'DDODODL'DDNDODDO DDKDM A!3a!7S$ 2G 4K 	c44Kr   c                    UR                   u  pgUR                  R                  (       a  SnO5UR                  R                  (       a  UR                  nSnO[        S5      e[        U5      n	UR                  [        R                  :X  a  [        n
O<UR                  [        R                  :X  a  [        n
O[        R                  " USS9nSnUR                   u  pX:X  d   eUR                  R                  (       a  SnOAUR                  R                  (       a  UR                  nSnO[        R                  " USS9nSn[        U5      nUR                  [        R                  :X  a  [        nO0UR                  [        R                  :X  a  [        nO[        S5      eUc%  [        R                  " Xk4[        R                  S9nO3UR                   Xk4:X  d   eUR                  [        R                  :X  d   e[        U5      n[        5       nUUl        SUl        UUl        UUl        UUl        UUl        UUl        U	Ul        UUl        W
Ul        UUl        UUl        UUl        [;        U U5        U$ )a  
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(   r   )r)   r*   r+   r,   r-   r4   r0   r'   r.   r%   r1   r2   r3   r/   r5   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rI   rL   )r   rM   rN   rO   r;   rI   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r`   s                     r   pairwise_distance_gpurc      s   < HHEB	xx			TTCE 	E b\F	xx2::&	RZZ	&!!"I6XXFB7N7	xx			TT!!"I6b\F	xx2::&	RZZ	&788yHHbXRZZ0ww2("""ww"**$$$QKEDDKDFDIDL'DDODODL'DDNDODDK 
#tHr   )NN)Nr   )NNr   )	numpyr.   faiss.loaderr   r   r   	METRIC_L2ra   rc    r   r   <module>rh      sK     @"
  #d9RRWlm  CD CP *.i br   