
    OhD                         S SK r S SKrS SKrS SKrS SKJr  S SKrS SKrS SKJ	r	  S SK
r
S SKrS SKrS SKrS SKJr   " S S5      r " S S5      rS	\R&                  \R(                  S S S
SSSSSS SS4S jrg)    N)
ThreadPool)Queue)get_invlistc                   n    \ rS rSrSr  SS jrS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rSrg)BigBatchSearcher   z
Object that manages all the data related to the computation
except the actual within-bucket matching and the organization of the
computation (parallel or not)
c                     X@l         / U l        X l        Xl        XPl        [
        R                  " UR                  5      n[
        R                  " [        U5      X6S9U l
        S/S-  U l        [        R                  " 5       =U l        U l        g )N)keep_maxr      )verbosetictocxqindexuse_float16faissis_similarity_metricmetric_type
ResultHeaplenrht_accutime	t_displayt0)selfr   r   kr   r   r
   s          V/var/www/html/shao/venv/lib/python3.13/site-packages/faiss/contrib/big_batch_search.py__init__BigBatchSearcher.__init__   so     
&--e.?.?@""3r7AAcAg#'99;.    c                 8    [         R                   " 5       U l        g N)r   	t_accu_t0r   s    r   start_t_accuBigBatchSearcher.start_t_accu0   s    r    c                 r    U R                   U==   [        R                  " 5       U R                  -
  -  ss'   g r"   )r   r   r#   )r   ns     r   stop_t_accuBigBatchSearcher.stop_t_accu3   s#    A$))+66r    c                 t    U[         R                   " 5       4U l        U R                  S:  a  [        USSS9  g g )Nr   Tendflush)r   r   r   print)r   names     r   ticBigBatchSearcher.tic6   s1    TYY[)<<!$D- r    c                     U R                   u  p[        R                  " 5       U-
  nU R                  S:  a  [        U SUS S35        U$ )Nr   z: .3fz s)r   r   r   r0   )r   r1   r   dts       r   tocBigBatchSearcher.toc;   sE    ;;YY[2<<!TF"RHB'(	r    c                    U R                   S:X  d<  U R                   S:X  a-  US:  a'  [        R                  " 5       U R                  S-   :  a  g [        R                  " 5       U R                  -
  n[	        SUS SU SU R
                  R                   S	U R                  S
   S SU R                  S   S SU R                  S   S SU R                  S   S SU R                  S   S SU R                  S   S S[        R                  " X R
                  R                  -  US-   -  U-
  S9 S[        R                  " 5        3U R                   S::  a  SOSSS9  [        R                  " 5       U l        g )N      i  g      ?[z.1fz	 s] list /z times prep q r   r5   z prep b z comp z res    z	 wait in    z
 wait out    z eta )secondsz mem r,   
Tr-   )r   r   r   r   r0   r   nlistr   datetime	timedeltar   get_mem_usage_kb)r   lts      r   reportBigBatchSearcher.reportB   s_   <<1LLADTYY[4>>C+?? IIK$''!#wis!DJJ$4$4#5 6 KKN3/xAs7K LKKN3'uT[[^C,@ A{{1~c* +As+ ,%%a

0@0@.@!A#.Fq.HIJ K))+,. *
	
 r    c                    U R                  S5        Sn[        U R                  5      n[        R                  " X R
                  R                  4SS9n[        SX!5       H\  n[        X$U-   5      nU R
                  R                  R                  U R                  XE U R
                  R                  5      u  pgXsXE& M^     U R                  5         X0l        g )Nzcoarse quantizationi   int32)dtyper   )r2   r   r   npemptyr   nproberangemin	quantizersearchr7   q_assign)r   bsnqrU   i0i1q_dis_i
q_assign_is           r   coarse_quantization$BigBatchSearcher.coarse_quantizationW   s    &'\88R!2!237C2"BRb!B"&**"6"6"="=

 1 1#3G )RO # 	
 r    c                    U R                  S5        U R                  nUS-  n[        R                  " U R                  U R                  R
                  S-   SS9U l        U R                  R                  5       U l        U R                  S:  a  [        SU R                  S   5        U R                  SS  U l        U ?U R                  5         g )Nzbucket sortr:      )nbucketntr   z  number of -1s:)r2   rU   r   matrix_bucket_sort_inplacer   rC   bucket_limsravel	query_idsr   r0   r7   )r   rU   s     r   reorder_assignBigBatchSearcher.reorder_assigne   s    ==A ;;MM4::#3#3a#7B@,,.<<!$d&6&6q&9:++AB/M
r    c                    [         R                   " 5       nU R                  nU R                  U   U R                  US-      pTU R                  XE nU R                  U   nU R
                  (       a  XsR                  R                  U5      -
  n[         R                   " 5       n[        UR                  U5      u  pU R                  c  U
R                  5       n
OU R                  U
5      n
U R                  (       a"  U
R                  S5      n
UR                  S5      n[         R                   " 5       nU R                  S==   X-
  -  ss'   U R                  S==   X-
  -  ss'   XgX4$ )z3prepare the queries and database items for bucket lr:   float16r   )r   r   rc   re   r   by_residualrS   reconstructr   invlistsdecode_funcrd   r   astyper   )r   rG   r   r   rX   rY   q_subsetxq_lt1list_idsxb_lt2s               r   prepare_bucketBigBatchSearcher.prepare_bucketr   s   YY[

!!!$d&6&6q1u&=B>>"(wwx //55a88DYY[$U^^Q7#::<D##D)D;;y)D;;y)DYY[A"'!A"'!x--r    c                     Uc  g[         R                   " 5       nUc  UnOX4   nU R                  R                  XU5        U R                  S==   [         R                   " 5       U-
  -  ss'   g)z,add the bucket results to the heap structureNr>   )r   r   add_result_subsetr   )r   ro   Drr   Ir   s         r   add_results_to_heap$BigBatchSearcher.add_results_to_heap   sW    9YY[9AA!!(q1A$))+**r    c                     U R                   R                  U R                  R                  U R                  R                  4$ r"   )r   shaper   rP   rC   r$   s    r   sizes_in_checkpoint$BigBatchSearcher.sizes_in_checkpoint   s+    tzz00$**2B2BCCr    c                 .   US-   n[        US5       n[        R                  " U R                  5       UU R                  R
                  U R                  R                  4S.US5        S S S 5        [        R                  " X15        g ! , (       d  f       N%= f)Nz.tmpwb)sizes	completedr   )	openpickledumpr   r   ry   rz   osreplace)r   fnamer   tmpnamefs        r   write_checkpoint!BigBatchSearcher.write_checkpoint   sr    &.'4 AKK!557!*7799dggii0 b ! 	

7" ! s   AB
Bc                 2   [        US5       n[        R                  " U5      nS S S 5        WS   U R                  5       :X  d   eUS   S   U R                  R
                  S S & US   S   U R                  R                  S S & US   $ ! , (       d  f       Nj= f)Nrbr   r   r   r:   r   )r   r   loadr   r   ry   rz   )r   r   r   ckps       r   read_checkpoint BigBatchSearcher.read_checkpoint   s    %!++a.C 7|t7799994y|		!4y|		!; s   B
B)rc   r   rU   re   r   r   r   r#   r   r   r   r   r   N)r   F)__name__
__module____qualname____firstlineno____doc__r   r%   r)   r2   r7   rI   r\   rf   ru   r{   r   r   r   __static_attributes__ r    r   r   r      sQ     	/$%7.
%*!.8
+D
# r    r   c                   V    \ rS rSrSrS\R                  \R                  4S jrS r	Sr
g)BlockComputer   zcomputation within one bucket knn_functionc                    Xl         UR                  [        R                  :X  a2  [        R                  " UR
                  UR                  5      nS nSnGO`UR                  [        R                  :X  a  [        R                  " UR
                  UR                  R                  UR                  R                  UR                  5      nUR                  Ul	        UR                  R                  nSUl        UR                  nOUR                  [        R                  :X  a{  [        R                   " UR
                  UR"                  R$                  UR                  5      nUR"                  Ul        UR"                  R                  nSUl        UR                  nO['        SUR                   S35      eXPl        US:X  a  S OUU l        Xpl        X l        X0l        X@l        g )Nc                 $    U R                  S5      $ )Nfloat32)view)xs    r   <lambda>(BlockComputer.__init__.<locals>.<lambda>   s    AFF9$5r    FTzindex type z not supportedr   )r   	__class__r   IndexIVFFlat	IndexFlatdr   
IndexIVFPQIndexPQpqMnbitsdecode
is_trainedrj   IndexIVFScalarQuantizerIndexScalarQuantizersqqtypeRuntimeError
index_helprm   methodpairwise_distancesknn)r   r   r   r   r   r   rm   rj   s           r   r   BlockComputer.__init__   s]    
??e000%2C2CDJ5KK__ 0 00UXX^^U5F5FHJ!HHJM$--..K$(J!++K__ = ==33):):<J!HHJM$--..K$(J!++KU__,=^LMM$#)W#44+&"4r    c                    U R                   R                  nUR                  S:X  d  UR                  S:X  a  S =pxXx4$ U R                  S:X  ad  [        R
                  " X R                  R                  5        [        U5      U R                  l	        U R                  R                  X5      u  pxXx4$ U R                  S:X  a  U R                  XUS9nS nXx4$ U R                  S:X  a  U R                  " XU4SU0UD6u  pxWW4$ )Nr   r   r   )metricr   r   )r   r   sizer   r   copy_array_to_vectorr   codesr   ntotalrT   r   r   )	r   rp   rs   rr   r   
extra_argsr   ry   rz   s	            r   block_searchBlockComputer.block_search   s    jj,,99>TYY!^LA t [[G#&&t__-B-BC%(]DOO"??))$2DA t [[00'';'GAA t [[N*88DL+LLDA!tr    )rj   rm   r   r   r   r   r   N)r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r      s%    )
 "$77		!Fr    r   r   Fr:   i   r   c                   ^ ^^
^1^2^3 T R                   nUS;   d   eUR                  n[        U5      U-  [        R                  " S5      R
                  -  n[        U5      T-  [        R                  " S5      R
                  [        R                  " S5      R
                  -   -  nUU-   U-   nUS:  a*  [        R                  " SU SU SU S	U S
US-  S S35        [        T UTUUS9m1[        T UUUS9m2T2R                  T1l
        T2R                  T1l        Uc  T1R                  5         OUT1l        T1R                  5         Uc  T R                  n[!        5       nUb  X4ST R                  4:X  d   e["        R$                  R'                  U5      (       aM  [        R                  " SU 35        T1R)                  U5      n[        R                  " S[        U5       35        O[        R                  " S5        US:X  a  [+        X5       H  nT1R-                  U5        T1R/                  U5      u  nnnn[0        R0                  " 5       nT2R3                  UUUT5      u  nnT1R4                  S==   [0        R0                  " 5       U-
  -  ss'   T1R7                  UUUU5        M     GOUS:X  a  U1U 4S jnT1R/                  U5      n Sn![9        S5      n"[+        X5       H  nT1R-                  U5        U"R;                  UU!US-   45      n#U u  nnnnT1R=                  5         T2R3                  UUUT5      u  nnT1R?                  S5        UUUU4n!T1R=                  5         U#RA                  5       n T1R?                  S5        M     T1R6                  " U!6   U"RC                  5         GOS m3U34S jn$S"U14S jjn%U2U
U4S jn&[E        S5      n'[E        S5      n(U$" U&T
ST
[!        5       U(U'5      n)U$" U%U	UUUU'S5      n*[0        R0                  " 5       n+ [        R                  " S5        U(RA                  5       n,U,(       d  GO"U,u  n-n.n/n0nnnnU-U:X  a  SS-    T1R4                  S==   U0-  ss'   T1R4                  S==   U.-  ss'   T1R4                  S==   U/-  ss'   [        R                  " SU- 35        T1R7                  UUUU5        [        R                  " SU- 35        URG                  U-5        T1R-                  U-5        UbY  [0        R0                  " 5       U+-
  U:  a=  [        R                  " S 5        T1RI                  UU5        [0        R0                  " 5       n+GMQ  U*RK                  5         U)RK                  5         T1RM                  S!5        T1RN                  RQ                  5         T1RS                  5         T1RN                  RT                  T1RN                  RV                  4$ )#aG  
Search queries xq in the IVF index, with a search function that collects
batches of query vectors per inverted list. This can be faster than the
regular search indexes.
Supports IVFFlat, IVFPQ and IVFScalarQuantizer.

Supports three computation methods:
method = "index":
    build a flat index and populate it separately for each index
method = "pairwise_distances":
    decompress codes and compute all pairwise distances for the queries
    and index and add result to heap
method = "knn_function":
    decompress codes and compute knn results for the queries

threaded=0: sequential execution
threaded=1: prefetch next bucket while computing the current one
threaded=2: prefetch prefetch_threads buckets at a time.

compute_threads>1: the knn function will get an additional thread_no that
    tells which worker should handle this.

In threaded mode, the computation is tiled with the bucket perparation and
the writeback of results (useful to maximize GPU utilization).

use_float16: convert all matrices to float16 (faster for GPU gemm)

q_assign: override coarse assignment, should be a matrix of size nq * nprobe

checkpointing (only for threaded > 1):
checkpoint: file where the checkpoints are stored
checkpoint_freq: when to perform checkpoinging. Should be a multiple of threaded

start_list, end_list: process only a subset of invlists
)r   r   r   rL   int64r   r   zmemory: queries z assign z result z total z = i   @r5   z GiB)r   r   )r   r   r   Nzrecovering checkpoint: z   already completed: z$no checkpoint: starting from scratchr;   r:   c                 l   > U b  TR                   " U 6   UTR                  :  a  TR                  U5      $ g)zSperform the addition for the previous bucket and
prefetch the next (if applicable) N)r{   rC   ru   )to_addrG   bbsr   s     r   add_results_and_prefetch2big_batch_search.<locals>.add_results_and_prefetcha  s:     !''05;;))!,, r    r?   c           
          [        U5       n[        X#5       Vs/ sH  nX;  d  M
  UR                  U XU4S9PM     n	nU	 H  n
U
R                  5         M     UR	                  5         UR                  5         S S S 5        UR                  S 5        g s  snf ! , (       d  f       N%= f!   [        R                  " 5         [        R                  " 5         e = f)N)args)r   rQ   apply_asyncgetclosejoinput	traceback	print_exc_threadinterrupt_main)task	pool_size
start_taskend_taskr   output_queueinput_queuepooliresrs              r   task_manager_thread-big_batch_search.<locals>.task_manager_thread~  s    	*d "'z!</ "=A-	=4++{; , = "=  /
 ! !JJLIIK +   &/ +*##%&&(s7   B( BBB;B8B( B
B%!B( (-Cc                  `   > [         R                  " TU S9nSUl        UR                  5         U$ )N)targetr   T)	threadingThreaddaemonstart)r   task_managerr   s     r   r   &big_batch_search.<locals>.task_manager  s5    $++*L #'L r    c                   >  [         R                  " SU  35        TR                  U 5      u  p4pVUR                  XXEU45        [         R                  " SU  35        g !   [        R
                  " 5         [        R                  " 5         e = f)NzPrepare start: zPrepare end: )logginginforu   r   r   r   r   r   )task_idr   r   ro   rp   rr   rs   r   s          r   prepare_task&big_batch_search.<locals>.prepare_task  sz    wi89141C1CG1L.  'TT!JK}WI67##%&&(s   AA -Bc           
      h  >  [         R                  " SU  35        Sn [        R                  " 5       n[         R                  " SU  35        UR                  5       n[        R                  " 5       U-
  nUc  UR	                  S 5        OUu  pxpn[         R                  " SU  SU 35        [        R                  " 5       nTS:  a  TR                  XU
TU S9u  pOTR                  XU
T5      u  p[        R                  " 5       U-
  n[         R                  " SU  SU 35        [        R                  " 5       nUR	                  XvX>XX45        [        R                  " 5       U-
  nGMK  [         R                  " S	U  35        g !   [        R                  " 5         [        R                  " 5         e = f)
NzCompute start: r   zCompute input: task zCompute work: task z, centroid r:   )	thread_idzCompute output: task zCompute end: )
r   r   r   r   r   r   r   r   r   r   )r   r   r   
t_wait_outr   input_value	t_wait_incentroidro   rp   rr   rs   ry   rz   	t_computecompcomputation_threadsr   s                  r   compute_task&big_batch_search.<locals>.compute_task  s    wi89
BLL#7y!AB"-//"3K $		b 0I"*#-?J<HLL#6wi{8*!UVB*Q.#00 !w  1  1  $00XqI $		b 0ILL#8	XJ!WXB $$!jXRZ^ "&r!1J1 2 }WI67##%&&(s   F F -F1zWaiting for resultr@   zAdding to heap start: centroid zAdding to heap end: centroid zwriting checkpointzfinalize heapr"   ),rP   nbytesr   rN   rM   itemsizer   r   r   r   rm   rj   r\   rU   rf   rC   setr   pathexistsr   rQ   rI   ru   r   r   r   r{   r   r   r%   r)   r   r   r   addr   r   r2   r   finalizer7   ry   rz   )4r   r   r   r   r   r   r   threadedr   prefetch_threadsr   rU   
checkpointcheckpoint_freq
start_listend_listcrash_atrP   mem_queries
mem_assignmem_resmem_totr   rG   ro   rp   rr   rs   t0iry   rz   r   prefetched_bucketr   r   prefetched_bucket_ar   r   r   prepare_to_compute_queuecompute_to_main_queuecompute_task_managerprepare_task_managert_checkpointvaluer   r   r   r   r   r   r   s4   ` `       `                                      @@@r   big_batch_searchr     sU   h \\FDDDD))KR6!BHHW$5$>$>>J"gk
""
((9

&
&	'G J&0G{{m8J< @YggYc'U2CC1HN	

 r1C -	D &&CO&&CO!;;I%!U[[)999977>>*%%LL2:,?@++J7ILL1#i.1ABCLL?@1} z,AJJqM-0-?-?-B*HdHd))+C$$T41=DAqJJqMTYY[3..M##Ha1= - 
Q	-  ..z:!}z,AJJqM"&"2"2(61q5/#;->*HdHd$$T41=DAqOOAq(A-F 3 7 7 9OOA - 	(

	4	 		!	F $)8  %a+E!$ 
  ,$ 
 yy{LL-.)--/ESXPHiY!Xq8#AJJqMY&MJJqMY&MJJqMZ'MLL:8*EF##Ha1=LL8
CDMM(#JJx %99;-?LL!56((Y?#'99;L+ . 	!!#!!#GGOFFOOGGI6688SVVXXr    )r   r   r   r   multiprocessing.poolr   r   r   queuer   r   rD   numpyrN   r   faiss.contrib.inspect_toolsr   r   r   r   r   r  r   r    r   <module>r     s      	  +        3X  X v5 5t  33IIYr    