
    g\0                         d dl Z d dlZd dlZd dlZ ej        e          Ze j        ddfdZ	e j
        Z
ddZe j        ddfdZddZdd	Zdd
Z	 	 ddZddZdS )    NFc                    t                               d| j        |fz             t          j                    }| j        \  }}t	          j        |          }	t	          j        |||	          }
t	          j        ||          }|dk    rt	          j                    }|rNt                               d|z             t	          j	                    }||_
        t	          j        |||          }d}|D ]}|j        d         }|                    |           |                    | |          \  }}||z  }|
                    ||           |                                 ||z  }t                               d|t          j                    |z
  fz             |
                                 t                               dt          j                    |z
  |fz             |
j        |
j        fS )	zComputes the exact KNN search results for a dataset that possibly
    does not fit in RAM but for which we have an iterator that
    returns it block by block.
    z%knn_ground_truth queries size %s k=%dkeep_maxr   running on %d GPUscongpur   %d db elements, %.3f szGT time: %.3f s (%d vectors))LOGinfoshapetimefaissis_similarity_metric
ResultHeap	IndexFlatget_num_gpusGpuMultipleClonerOptionsshardindex_cpu_to_all_gpusaddsearch
add_resultresetfinalizeDI)xqdb_iteratorkmetric_typer   r
   t0nqdr   rhindexr	   i0xbinir   r   s                     ]/var/www/html/mpstechhub/venv/lib/python3.11/site-packages/faiss/contrib/exhaustive_search.pyknn_ground_truthr,      s   
 HH4!}DEEE	BHEB)+66H		"a(	3	3	3BOA{++Erzz!## E%,---+--+EbtDDD 
B D DYq\		#||B""1	R
a
b)Rr1A,BBCCCCKKMMMHH+ty{{R/?.DDEEE4:       c                    | j         \  }}t          |t          j                  }t          j        |j                  }|rt          |          nt          |          }t          |j	        |          }	t                              d| d|	 d| d|            t          j                    }
|                    | |	          \  }}t          j                    |
z
  }|r |dz  dk     sJ |                    d          }d}d	}|5|s|d	d	|	d
z
  f         |k     }n|d	d	|	d
z
  f         |k    }|                                dk    rt                              d|                                z             t          j                    }
t          |t           j                  rK|}|rt          j        |dz            }nt          j        ||j                  }|                    |           |                    | |         |          \  }}}|r|                    d          }t          j                    |
z
  }t                              d           t          j                    }
|rt          j        nt          j        } |||	||          }	 t          j        } ||          |_         ||          |_        |S ||          |_         ||          |_         ||                    d                    |_         ||          |_        t!          j         |d
z   d          }|!                     ||                     |d         }t!          j         ||j"                  }t!          j         |d          }|#                     ||           ||                     t          j                    |
z
  } t                              d|dd|dd| dd           |||fS )aA  GPU does not support range search, so we emulate it with
    knn search + fallback to CPU index.

    The index_cpu can either be:
    - a CPU index that supports range search
    - a numpy table, that will be used to construct a Flat index if needed.
    - None. In that case, at most gpu_k results will be returned
    zGPU search z queries with k=z is_binary_index=z
 keep_max=   i   int16r   N   zCPU search remain %dcombineTint64dtyper   c                 ,    g | ]}t          |          S  len).0dis     r+   
<listcomp>z$range_search_gpu.<locals>.<listcomp>   s     9 9 9RR 9 9 9r-   ztimes z.3fzs s))r   
isinstancer   IndexBinaryr   r"   intfloatminntotalr   debugr   r   astypesumnpndarrayIndexBinaryFlatr   r   range_searchCombinerRangeKNNint16CombinerRangeKNNfloatswig_ptrr   r   maskD_remainview
lim_remainI_remainemptycompute_sizesr6   write_resultrange	METRIC_L2appendcumsumhstack)!r   r2	index_gpu	index_cpugpu_kr$   r%   is_binary_indexr   r!   r#   r   r   t1t2rR   rO   xbrP   rS   CombinerRangeKNNcombinerspL_resnresD_resI_resnrinvl0l1t3s!                                    r+   range_search_gpurq   :   s    HEB E,=>>O))*?@@H#	2RrBIe$$AIINbNNNNNoNNN(NNNP P P	BB""DAq	r	B 1uu}}}}HHW	
BJ 	$QQQAX;#DDQQQAX;#D88::>>II,txxzz9:::B)RZ00 "" J % 5a!e < <II %93H I IIb!!!-6-C-CBtHb-Q-Q*J( 4#??733r!BIIi	B (7 	$### 
  Ar844H#!^RUU
RUU
!BtHHHM "8H"$"Z__W%=%=">">H "8Haw///rr%yy)))RyQW---W---bbiiE333( 
r	BII4r44444424444555%r-   c                    | j         \  }}t          j                    }t          j        | d          } t	          j        ||          }	|dk    rt	          j                    }|rNt                              d|z             t	          j	                    }
||
_
        t	          j        |	|
|          }d}d t          |          D             }d t          |          D             }|D ]G}|j         d         }|dk    r@|                    |           t          | |||          \  }}}|                                 nC|	                    |           |	                    | |          \  }}}|	                                 ||z  }t          |          D ]a}||         ||d	z            }}||k    rF||                             |||                    ||                             |||                    b||z  }t                              d
|t          j                    |z
  fz             It          j        dd          t          j        dd          fd|D             }fd|D             }d |D             }t'          |          |k    sJ t          j        |d	z   d          }t          j        |          |d	d<   |t          j        |          t          j        |          fS )zComputes the range-search search results for a dataset that possibly
    does not fit in RAM but for which we have an iterator that
    returns it block by block.
    float32r5   r   r   r   r   c                     g | ]}g S r8   r8   r;   _is     r+   r=   z&range_ground_truth.<locals>.<listcomp>             r-   c                     g | ]}g S r8   r8   ru   s     r+   r=   z&range_ground_truth.<locals>.<listcomp>   rw   r-   r2   r   r4   c                 H    g | ]}|g k    rt          j        |          nS r8   rH   r[   )r;   rl   empty_Ds     r+   r=   z&range_ground_truth.<locals>.<listcomp>   -    ;;;A!r''")A,,,w;;;r-   c                 H    g | ]}|g k    rt          j        |          nS r8   rz   )r;   rl   empty_Is     r+   r=   z&range_ground_truth.<locals>.<listcomp>   r|   r-   c                 ,    g | ]}t          |          S r8   r9   )r;   rl   s     r+   r=   z&range_ground_truth.<locals>.<listcomp>   s    SVVr-   uint64N)r   r   rH   ascontiguousarrayr   r   r   r   r   r   r   r   rW   r   rq   r   rK   rY   zerosr:   rZ   r[   )r   r    	thresholdr"   r   r
   r$   r%   r#   r'   r	   r]   r(   r   r   r)   r*   lims_iDiIijrn   ro   sizeslimsr{   r~   s                            @@r+   range_ground_truthr      s    HEB	B		b		2	2	2BOA{++Erzz!## I%,---+--/"4HHH	 
B  eBii   A  eBii   A D DYq\!88MM#-b)YLLNFBOOIIcNNN"//I>>NFBKKMMM
br 	' 	'AAYq1uBBww!Br"uI&&&!Br"uI&&&
b)Rr1A,BBCCCChq(((Ghq	***G;;;;;;;A;;;;;;;AQEu::8BF(+++DyDH1ry||++r-   c                    |r||k    }n||k     }t          j        |           }d}t          |           D ];\  }}	t          |	          }	||||	z                                            ||<   ||	z  }<|||         ||         fS )z select a set of results r   )rH   
zeros_like	enumeraterA   rG   )
rh   disidsthreshr   rO   new_nresorl   rk   s
             r+   threshold_radius_nresr      s     V|V|}T""H	A4  2WW1QV8n((**	RSYD	))r-   c                 .   |r||k    }n||k     }t          j        |           }t          |           dz
  }t          |          D ]@}| |         | |dz            }
}	||         ||	|
                                         z   ||dz   <   A|||         ||         fS )z= restrict range-search results to those below a given radius r2   )rH   r   r:   rW   rG   )r   r   r   r   r   rO   new_limsnrl   rn   ro   s              r+   threshold_radiusr      s     V|V|}T""HD		AA1XX : :a$q1u+B"1+RU(9(99QSYD	))r-   c                 x   t          j        d | D                       }t          |          |k    sJ |r4|                    t          |          |z
  dz
             |d|z
           }n|                    |           ||         }|j        dk    rt          |          }nt          |          }t                              d|z             d}t          |           D ];\  }\  }}}	t          |||	||          \  }}}	|t          |          z  }|||	f| |<   <t                              d|z             ||fS )	zfind radius that reduces number of results to target_nres, and
    applies it in-place to the result batches used in
    range_search_max_resultsc                     g | ]\  }}}|	S r8   r8   )r;   _r   s      r+   r=   z apply_maxres.<locals>.<listcomp>   s    999	3999r-   r2   r   rs   z   setting radius to %sr   r   z.   updated previous results, new nb results %d)rH   r[   r:   	partitionr6   rB   rA   r   rE   r   r   )
res_batchestarget_nresr   alldisradiustotresrl   rh   r   r   s
             r+   apply_maxresr      sa    Y99[999::Fv;;$$$$ %V{2Q6777[()%%%$|y  vVII'&0111F(55 ( (D#s.#sFX7 7 7c3#c((sCAII>GHHH6>r-   c                    t          | t          j                  }||J t          d|z            }||J t          |dz            }|dk    rt          j                    }|rOt
                              d|z             t          j                    }	||	_        t          j	        | |	|          }
nd}
t          j
                    }dx}}dx}x}}g }|D ]}t          j
                    }t
                              dt          |           d	           |
rt          |||
|           \  }}}n|                     ||          \  }}}|d
d         |dd         z
  }|t          |          z  }|t          |          z  }t          j
                    }|r|                    d          }|t          |          z  }|                    |||f           |M||k    rGt
                              d||fz             t#          ||| j        t          j        k              \  }}t          j
                    }|||z
  z  }|||z
  z  }t
                              dt          j
                    |z
  ||fz             t
                              d||||fz             |r.||k    r(t#          ||| j        t          j        k              \  }}t)          j        d |D                       }t)          j        d |D                       }t)          j        d |D                       }t)          j        t          |          d
z   d          }t)          j        |          |d
d<   ||||fS )a  Performs a range search with many queries (given by an iterator)
    and adjusts the threshold on-the-fly so that the total results
    table does not grow larger than max_results.

    If ngpu != 0, the function moves the index to this many GPUs to
    speed up search.
    Ng?g      ?r   r   r   r   z
searching z vectorsr2   r1   z-too many results %d > %d, scaling back radiusr   z'   [%.3f s] %d queries done, %d resultszBsearch done in %.3f s + %.3f s, total %d results, end threshold %gc                     g | ]\  }}}|	S r8   r8   r;   nres_idis_iids_is       r+   r=   z,range_search_max_results.<locals>.<listcomp>Y  s    EEE!5fEEEr-   c                     g | ]\  }}}|	S r8   r8   r   s       r+   r=   z,range_search_max_results.<locals>.<listcomp>Z      CCC3vueUCCCr-   c                     g | ]\  }}}|	S r8   r8   r   s       r+   r=   z,range_search_max_results.<locals>.<listcomp>[  r   r-   r   r5   )r?   r   r@   rA   r   r   r   r   r   r   r   rE   r:   rq   rK   rF   rY   r   r"   METRIC_INNER_PRODUCTrH   r[   r   rZ   )r'   query_iteratorr   max_resultsmin_resultsr   r
   clip_to_minr`   r	   r]   t_startt_searcht_post_processqtotr   
raw_totresr   xqir#   r   r   r   r   ra   rb   rh   r   r   r   s                                 r+   range_search_max_resultsr     s    !(9::O&&&#+,,&&&++,,rzz!## %,---+--/"4HHH			ikkG !!H~!""D"6JK 2 2Y[[		1s3xx111222 	=-c69eLLNFB"//V<<NFBfSbSk)c"gg
CY[[ 	$7##B#b''FB+,,,"v';';HHDk*+ , , ,)[*e.HH  NFF Y[[BG"r'!		;IKK'!4?1 1 	2 	2 	2 	2 HHLnffP6 	6  
  
v++%&%*DD
 
 

 9EEEEEFFD
)CC{CCC
D
DC
)CC{CCC
D
DC8CIIM222DyDH4c!!r-        N  c              #      K   t          |           }|}d}||k     r6| |||z            }|V  ||k     r|dz  }|t          |          z  }||k     4dS dS )z produces batches of progressively increasing sizes. This is useful to
    adjust the search radius progressively without overflowing with
    intermediate results r      Nr9   )r   start_bsmax_bsr$   bsrl   r   s          r+   exponential_query_iteratorr   c  st       
RB	B	A
b&&1r6l			;;!GB	SXX b&&&&&&r-   )r.   )F)NNFr   F)r   r   )r   r   numpyrH   logging	getLogger__name__r   rX   r,   knnrq   r   r   r   r   r   r   r8   r-   r+   <module>r      s          g!!5:_EXZ $ $ $ $N i
[ [ [ [| @E"3, 3, 3, 3,l* * * ** * * *   : <@>CY" Y" Y" Y"x     r-   