
    i\0                         S SK r S SKrS SKrS SKr\R
                  " \5      r\ R                  SS4S jr	\ R                  r
SS jr\ R                  SS4S jrSS jrSS jrSS	 jr  SS
 jrSS jrg)    NFc                    [         R                  SU R                  U4-  5        [        R                  " 5       nU R                  u  px[        R
                  " U5      n	[        R                  " XrU	S9n
[        R                  " X5      nUS:X  a  [        R                  " 5       nU(       aH  [         R                  SU-  5        [        R                  " 5       nXLl
        [        R                  " XUS9nSnU H  nUR                  S   nUR                  U5        UR                  X5      u  nnUU-  nU
R                  UU5        UR                  5         X-  n[         R                  SU[        R                  " 5       U-
  4-  5        M     U
R!                  5         [         R                  S[        R                  " 5       U-
  U4-  5        U
R"                  U
R$                  4$ )	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                     o/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/faiss/contrib/exhaustive_search.pyknn_ground_truthr,      sv   
 HH4!}DE	BHHEB))+6H			"(	3BOOA+Erz!!#%,-++-++EtD 
BYYq\		#||B"1	R
a
)Rr1A,BBC  KKMHH+tyy{R/?.DDE44:    c                    U R                   u  pV[        U[        R                  5      n[        R                  " UR
                  5      nU(       a  [        U5      O
[        U5      n[        UR                  U5      n	[        R                  SU SU	S SUS SUS 35        [        R                  " 5       n
UR                  X	5      u  p[        R                  " 5       U
-
  nU(       a  US-  S:  d   eUR                  S5      nS	nS
nUGb1  U(       d  US
S
2U	S-
  4   U:  nOUS
S
2U	S-
  4   U:  nUR                  5       S	:  a  [        R                  SUR                  5       -  5        [        R                  " 5       n
[        U[         R"                  5      (       aT  UnU(       a  [        R$                  " US-  5      nO [        R&                  " XbR
                  5      nUR)                  U5        UR+                  U U   U5      u  nnnU(       a  UR                  S5      n[        R                  " 5       U
-
  n[        R                  S5        [        R                  " 5       n
U(       a  [        R,                  O[        R.                  nU" XYX5      n [        R0                  nU" U5      Ul        U" U5      Ul        UbC  U" W5      Ul        U" W5      Ul        U" UR;                  S5      5      Ul        U" W5      Ul        [         R@                  " US-   SS9nURC                  U" U5      5        US   n[         R@                  " UURD                  S9n[         R@                  " USS9nURG                  U" U5      U" U5      5        [        R                  " 5       U
-
  n![        R                  SUS SUS SU!S S35        UUU4$ )a)  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combineint64dtyper   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lenhstack)"r   r2	index_gpu	index_cpugpu_kr$   r%   is_binary_indexr   r!   r#   r   r   t1t2rK   rH   xbrI   rL   CombinerRangeKNNcombinerspL_resnresD_resI_resnrinvl0l1dit3s"                                     r+   range_search_gpurl   :   s&    HHEB E,=,=>O)))*?*?@H#RrBIe$AII
bT*!u,>oa-@(1NP	BB"DA	r	B1uu}}HHW	
BJQAX;#DQAX;#D88:>II,txxz9:B)RZZ00" % 5 5a!e <I %3H3H IIb!-6-C-CBtHb-Q*J(#??73r!BIIi	B (7#### 
  r4H^^U
U
!tHHM "8H"$Z__W%=">H "8Haw/r%y)RyQWW-W-biE3( 
r	BIIr#hbC2c(!45%r-   c                 d   U R                   u  pg[        R                  " 5       n[        R                  " U SS9n [        R
                  " Xs5      n	US:X  a  [        R                  " 5       nU(       aH  [        R                  SU-  5        [        R                  " 5       n
XJl
        [        R                  " XUS9nSn[        U5       Vs/ s H  n/ PM     nn[        U5       Vs/ s H  n/ PM     nnU GH  nUR                   S   nUS:  a3  WR                  U5        [        XUU5      u  nnnUR                  5         O6U	R                  U5        U	R!                  X5      u  nnnU	R                  5         UU-  n[        U5       HF  nUU   UUS-      nnUU:  d  M  UU   R#                  UUU 5        UU   R#                  UUU 5        MH     UU-  n[        R                  SU[        R                  " 5       U-
  4-  5        GM     [        R$                  " SS	S9n[        R$                  " SSS9nU Vs/ s H!  nU/ :w  a  [        R&                  " U5      OUPM#     nnU Vs/ s H!  nU/ :w  a  [        R&                  " U5      OUPM#     nnU Vs/ s H  n[)        U5      PM     nn[)        U5      U:X  d   e[        R$                  " US-   S
S9n[        R*                  " U5      USS& U[        R&                  " U5      [        R&                  " U5      4$ s  snf s  snf s  snf s  snf s  snf )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   r2   r   r4   uint64N)r   r   rA   ascontiguousarrayr   r   r   r   r   r   r   r   rP   r   rl   r   rD   rR   zerosrU   rT   rS   )r   r    	thresholdr"   r   r
   r$   r%   r#   r'   r	   rW   r(   _ir   r   r)   r*   lims_iDiIijrh   ri   empty_Iempty_Drf   sizeslimss                                r+   range_ground_truthr|      s    HHEB	B			b		2BOOA+Erz!!#%,-++-//4H	 
BBi iiA Bi iiA YYq\!8MM#-bYLNFBOOIIcN"//>NFBKKM
brAAYq1uBBw!Br"I&!Br"I&	 
 	b)Rr1A,BBC# & hhq(Ghhq	*G9:;A!r'"))A,w
.A;9:;A!r'"))A,w
.A;QSVQEu:88BF(+DyyDH1ryy|++= 	! . 	<;s   L#L7(L#%(L(L-c                     U(       a  X:  nOX:  n[         R                  " U 5      nSn[        U 5       H*  u  p[        U	5      n	XWXy-    R	                  5       Xh'   Xy-  nM,     XaU   X%   4$ )zselect a set of results r   )rA   
zeros_like	enumerater:   r@   )
rb   disidsthreshr   rH   new_nresorf   re   s
             r+   threshold_radius_nresr      so    ||}}T"H	A4WQVn((*	 ! Y	))r-   c                     U(       a  X:  nOX:  n[         R                  " U 5      n[        U 5      S-
  n[        U5       H)  nX   XS-      pXh   XYU
 R	                  5       -   XhS-   '   M+     XaU   X%   4$ )z<restrict range-search results to those below a given radius r2   )rA   r~   rT   rP   r@   )r{   r   r   r   r   rH   new_limsnrf   rh   ri   s              r+   threshold_radiusr      s{    ||}}T"HD	AA1X$1u+B"+(99Q  Y	))r-   c           	      <   [         R                  " U  VVs/ s H  u  p4o4PM	     snn5      n[        U5      U:  d   eU(       a)  UR                  [        U5      U-
  S-
  5        USU-
     nOUR                  U5        XQ   nUR                  S:X  a  [        U5      nO[        U5      n[        R                  SU-  5        Sn[        U 5       H+  u  nu  pn
[        XXUS9u  pn
U[        U5      -  nXU
4X'   M-     [        R                  SU-  5        Xg4$ s  snnf )zfind radius that reduces number of results to target_nres, and
applies it in-place to the result batches used in
range_search_max_resultsr2   r   rn   z   setting radius to %sr   r   z.   updated previous results, new nb results %d)rA   rU   rT   	partitionr6   r;   r:   r   r>   r   r   )res_batchestarget_nresr   _r   alldisradiustotresrf   rb   r   s              r+   apply_maxresr      s    YY[9[	[9:Fv;$$$V{2Q67[()%$||y vVII'&01F(5Ds.sX73#c(C	  6
 II>GH>+ :s   D
c           
         [        U [        R                  5      nUc  Uc   e[        SU-  5      nUc  Uc   e[        US-  5      nUS:X  a  [        R                  " 5       nU(       aI  [
        R                  SU-  5        [        R                  " 5       n	XYl        [        R                  " X	US9n
OSn
[        R                  " 5       nS=pS=n=nn/ nU GH~  n[        R                  " 5       n[
        R                  S[        U5       S	35        U
(       a  [        UX*U 5      u  nnnOU R                  UU5      u  nnnUS
S USS -
  nU[        U5      -  nU[        U5      -  n[        R                  " 5       nU(       a  UR                  S5      nU[        U5      -  nUR!                  UUU45        UbF  X:  aA  [
        R                  SX4-  5        [#        UUU R$                  [        R&                  :H  S9u  p/[        R                  " 5       nUUU-
  -  nUUU-
  -  n[
        R                  S[        R                  " 5       U-
  X4-  5        GM     [
        R                  SXX4-  5        U(       a-  X:  a(  [#        UUU R$                  [        R&                  :H  S9u  p/[(        R*                  " U VVVs/ s H	  u  nnnUPM     snnn5      n[(        R*                  " U VVVs/ s H	  u  nnnUPM     snnn5      n[(        R*                  " U VVVs/ s H	  u  nnnUPM     snnn5      n[(        R,                  " [        U5      S
-   SS9n[(        R.                  " U5      US
S& UUUU4$ s  snnnf s  snnnf s  snnnf )zPerforms 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 %gro   r5   )r8   r   r9   r:   r   r   r   r   r   r   r   r>   rT   rl   rD   r?   rR   r   r"   METRIC_INNER_PRODUCTrA   rU   rq   rS   ) r'   query_iteratorr   max_resultsmin_resultsr   r
   clip_to_minrZ   r	   rW   t_startt_searcht_post_processqtotr   
raw_totresr   xqir#   rt   ru   rv   nres_ir[   r\   dis_iids_irb   r   r   r{   s                                    r+   range_search_max_resultsr     sB    !(9(9:O&&&#+,&&&++,rz!!#%,-++-//4H		iikG !!H!""D"6JKYY[		Js3xj12-c6eLNFB"//V<NFBfSbk)c"g
CYY[7#B#b'FB+,"v';HHD*+ ,)[**e.H.HHNF YY[BG"r'!		;IIK'!4?1 1 	2= B HHLfP6 	6
 v+%&&%*D*DD

 99E!5fEFD
)){C{3vueU{C
DC
)){C{3vueU{C
DC88CIM2DyyDH4c!! FCCs   =M4,M;Nc              #      #    [        U 5      nUnSnXS:  a*  XXT-    nUv   XB:  a  US-  nU[        U5      -  nXS:  a  M)  gg7f)zproduces batches of progressively increasing sizes. This is useful to
adjust the search radius progressively without overflowing with
intermediate results r      N)rT   )r   start_bsmax_bsr$   bsrf   r   s          r+   exponential_query_iteratorr   c  sQ      
RB	B	A
&16l	;!GB	SX &s   <A A)i   )F)NNFr   F)    i N  )r   r   numpyrA   logging	getLogger__name__r   rQ   r,   knnrl   r|   r   r   r   r   r    r-   r+   <module>r      s       !5:__EXZ $N ii
[| @E"3,l**: <@>CY"xr-   