Ë
    ¯Œj\  ã                   ó  — d dl Zd dl­ d dlZd dlZd„ Zd„ Zed fd„Z	dd„Z
dd„ZeZdd„Zd	„ ZeZd d
„Zd„ Zd„ ZeZd!d„ZeZd!d„Z G d„ d«      Zd"d„Z G d„ d«      Zedfd„Zd#d„Z G d„ d«      Z G d„ de«      Zd„ ZeZ d„ Ze!Z"d$d„Z!y)%é    N)Ú*c                 ó´  — t        j                  | d¬«      } | j                  \  }}t        j                  ||fd¬«      }t        j                  ||fd¬«      }t	        j
                  «       }t        |«      |_        t        |«      |_        ||_	        ||_
        |j                  «        |j                  |t        | «      «       |j                  «        ||fS )zPreturn k smallest values (and their indices) of the lines of a
    float32 arrayÚfloat32©ÚdtypeÚint64)ÚnpÚascontiguousarrayÚshapeÚzerosÚfaissÚfloat_maxheap_array_tÚswig_ptrÚidsÚvalÚnhÚkÚheapifyÚaddnÚreorder©Úarrayr   ÚmÚnÚIÚDÚhas          ú^/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/faiss/extra_wrappers.pyÚkminr      óª   € ô × Ñ  ¨iÔ8€EØ�;‰;�D€A€qÜ
�‰�!�Q�˜wÔ'€AÜ
�‰�!�Q�˜yÔ)€AÜ	×	$Ñ	$Ó	&€BÜ�a‹[€B„FÜ�a‹[€B„FØ€B„EØ€B„DØ‡J�J„LØ‡G�GˆAŒx˜‹ÔØ‡J�J„LØˆaˆ4€Kó    c                 ó´  — t        j                  | d¬«      } | j                  \  }}t        j                  ||fd¬«      }t        j                  ||fd¬«      }t	        j
                  «       }t        |«      |_        t        |«      |_        ||_	        ||_
        |j                  «        |j                  |t        | «      «       |j                  «        ||fS )zOreturn k largest values (and their indices) of the lines of a
    float32 arrayr   r   r   )r	   r
   r   r   r   Úfloat_minheap_array_tr   r   r   r   r   r   r   r   r   s          r   Úkmaxr$   +   r    r!   c                 óÞ  — t        j                  | d¬«      } t        j                  |d¬«      }| j                  \  }}|j                  \  }}||k(  sJ ‚t        j                  ||fd¬«      }|t        k(  r-t        ||t        | «      |t        |«      t        |«      «       |S |t        k(  r| |j                  z  |dd |S t        ||t        | «      |t        |«      ||t        |«      «       |S )zJcompute the whole pairwise distance matrix between two sets of
    vectorsr   r   N)
r	   r
   r   ÚemptyÚ	METRIC_L2Úpairwise_L2sqrr   ÚMETRIC_INNER_PRODUCTÚTÚpairwise_extra_distances)	ÚxqÚxbÚmetricÚ
metric_argÚnqÚdÚnbÚd2Údiss	            r   Úpairwise_distancesr5   =   sæ   € ô 
×	Ñ	˜b¨	Ô	2€BÜ	×	Ñ	˜b¨	Ô	2€BØ�H‰H�E€BˆØ�X‰X�F€BˆØ�Š7€Nˆ7Ü
�(‰(�B˜�8 9Ô
-€CØ”ÒÜ�q˜"œh r›l¨B´¸³¼hÀs»mÔLð €Jð 
Ô'Ò	'Ø�b—d‘d‘ˆ‰Aˆð €Jô 	!ØØÜ�R‹LØÜ�R‹LØØÜ�S‹Mô		
ð €Jr!   c                 ót   — t        j                  | d¬«      }t        t        |«      |j                  |«       |S ©Nr   r   )r	   r&   Ú
float_randr   Úsize©r   ÚseedÚress      r   Úrandr=   X   s+   € Ü
�(‰(�1˜IÔ
&€CÜŒx˜‹}˜cŸh™h¨Ô-Ø€Jr!   c                 ó¾   — t        j                  | d¬«      }|€"t        t        |«      |j                  |«       |S t        t        |«      |j                  ||«       |S ©Nr   r   )r	   r&   Ú
int64_randr   r9   Úint64_rand_max)r   r;   Úvmaxr<   s       r   ÚrandintrC   ^   sO   € Ü
�(‰(�1˜GÔ
$€CØ€|Ü”8˜C“= #§(¡(¨DÔ1ð €Jô 	”x “} c§h¡h°°dÔ;Ø€Jr!   c                 ót   — t        j                  | d¬«      }t        t        |«      |j                  |«       |S r7   )r	   r&   Úfloat_randnr   r9   r:   s      r   ÚrandnrF   j   s+   € Ü
�(‰(�1˜IÔ
&€CÜ”˜“˜sŸx™x¨Ô.Ø€Jr!   c                 ó  — | j                  d«      } | j                  dk(  rt        | j                  t	        | «      «      S | j
                  \  }}t        j                  |d¬«      }t        ||t	        | «      t	        |«      «       |S )z<compute a checksum for quick-and-dirty comparisons of arraysÚuint8é   Úuint64r   )	ÚviewÚndimÚbvec_checksumr9   r   r   r	   r   Úbvecs_checksum)Úar   r1   Úcss       r   ÚchecksumrQ   p   sg   € à	�‰ˆw‹€AØ‡v�v�‚{Ü˜QŸV™V¤X¨a£[Ó1Ð1Ø�7‰7�D€A€qÜ	�‰�!˜8Ô	$€BÜ�1�aœ !›¤h¨r£lÔ3Ø€Ir!   c                 óf   — t        j                  | |fd¬«      }t        | |t        |«      |«       |S r7   )r	   r&   Úrand_smooth_vectors_cr   )r   r1   r;   r<   s       r   Úrand_smooth_vectorsrT   ~   s-   € Ü
�(‰(�A�q�6 Ô
+€CÜ˜!˜Q¤¨£¨tÔ4Ø€Jr!   c                 óZ  — t        j                  | d¬«      } t        j                  |d¬«      }| j                  d   }|j                  d   |k(  sJ ‚| j                  d   |j                  d   }}d}t        |«      D ]+  }|t	        |t        | |   «      |t        ||   «      «      z  }Œ- |S )z;size of intersection between each line of two result tablesr   r   r   rI   )r	   r
   r   ÚrangeÚranklist_intersection_sizer   )ÚI1ÚI2r   Úk1Úk2ÚninterÚis          r   Úeval_intersectionr^   „   s§   € ä	×	Ñ	˜b¨Ô	0€BÜ	×	Ñ	˜b¨Ô	0€BØ
�‰�‰€AØ�8‰8�A‰;˜!ÒÐÐØ�X‰X�a‰[˜"Ÿ(™( 1™+ˆ€BØ€FÜ�1ŽXˆØÔ,Ø”˜˜A™“ ¤X¨b°©e£_ó
ñ 	
‰ð ð €Mr!   c                 ód   — t        | j                  d   | j                  d   t        | «      «       y )NrI   r   )Úfvec_renorm_L2r   r   ©Úxs    r   Únormalize_L2rc   “   s"   € Ü�1—7‘7˜1‘:˜qŸw™w q™z¬8°A«;Õ7r!   c           	      ó®  — t        j                  | d¬«      } |€t        | j                  «       dz   «      }t        j                  |dz   d¬«      }t        j                  | j
                  d¬«      }t        | j
                  t        j                  | j                  d«      «      |t        j                  |«      t        j                  |«      |«       ||fS )a  Perform a bucket sort on a table of integers.

    Parameters
    ----------
    tab : array_like
        elements to sort, max value nbucket - 1
    nbucket : integer
        number of buckets, None if unknown
    nt : integer
        number of threads to use (0 = use unthreaded codepath)

    Returns
    -------
    lims : array_like
        cumulative sum of bucket sizes (size vmax + 1)
    perm : array_like
        perm[lims[i] : lims[i + 1]] contains the indices of bucket #i
        (size tab.size)
    r   r   rI   rJ   )
r	   r
   ÚintÚmaxr&   r9   Úbucket_sort_cr   r   rK   )ÚtabÚnbucketÚntÚlimsÚperms        r   Úbucket_sortrm   š   s¥   € ô( ×
Ñ
˜s¨'Ô
2€CØ€Ü�c—g‘g“i !‘mÓ$ˆÜ�8‰8�G˜a‘K wÔ/€DÜ�8‰8�C—H‘H GÔ,€DÜØ�‰Ü�‰�s—x‘x Ó)Ó*ØÜ�‰�tÓÜ�‰�tÓØ
ôð �ˆ:Ðr!   c           	      ó@  — | j                   dk(  s| j                   dk(  sJ ‚| j                  \  }}|€t        | j                  «       dz   «      }t	        j
                  |dz   d¬«      }t        ||t        j                  | «      |t        j                  |«      |«       |S )a®  Perform a bucket sort on a matrix, recording the original
    row of each element.

    Parameters
    ----------
    tab : array_like
        array of size (N, ncol) that contains the bucket ids, maximum
        value nbucket - 1.
        On output, it the elements are shuffled such that the flat array
        tab.ravel()[lims[i] : lims[i + 1]] contains the row numbers
        of each bucket entry.
    nbucket : integer
        number of buckets (the maximum value in tab should be nbucket - 1)
    nt : integer
        number of threads to use (0 = use unthreaded codepath)

    Returns
    -------
    lims : array_like
        cumulative sum of bucket sizes (size vmax + 1)
    Úint32r   rI   r   )	r   r   re   rf   r	   r&   Úmatrix_bucket_sort_inplace_cr   r   )rh   ri   rj   ÚnrowÚncolrk   s         r   Úmatrix_bucket_sort_inplacers   Á   s‰   € ð, �9‰9˜Ò 3§9¡9°Ò#7Ð7Ð7Ø—‘�J€Dˆ$Ø€Ü�c—g‘g“i !‘mÓ$ˆÜ�8‰8�G˜a‘K wÔ/€DÜ Øˆd”E—N‘N 3Ó'¨´%·.±.ÀÓ2FÈôð €Kr!   c                   ó*   — e Zd ZdZdd„Zd„ Zd„ Zd„ Zy)Ú
ResultHeapz_Accumulate query results from a sliced dataset. The final result will
    be in self.D, self.I.c                 óz  — t        j                  ||fd¬«      | _        t        j                  ||fd¬«      | _        ||c| _        | _        |rt        «       }n
t        «       }||_        ||_        t        | j                  «      |_
        t        | j                  «      |_        |j                  «        || _        y)z›
        nq: number of query vectors,
        k: number of results per query
        keep_max: keep the top-k maximum values instead of the minima
        r   r   r   N)r	   r   r   r   r0   r   r#   r   r   r   r   r   r   Úheaps)Úselfr0   r   Úkeep_maxrw   s        r   Ú__init__zResultHeap.__init__ë   s�   € ô —‘˜2˜q˜'¨Ô1ˆŒÜ—‘˜2˜q˜'¨Ô3ˆŒØ˜aˆˆŒ�”ÙÜ)Ó+‰Eä)Ó+ˆEØˆŒØˆŒÜ˜TŸV™VÓ$ˆŒ	Ü˜TŸV™VÓ$ˆŒ	Ø�‰ŒØˆ�
r!   c                 ó&  — |j                   \  }}t        j                  |d¬«      }t        j                  |d¬«      }|j                   ||fk(  sJ ‚|| j                  k(  sJ ‚| j                  j                  |t        |«      t        |«      |«       y)z›
        Add results for all heaps
        D, I should be of size (nh, nres)
        D, I do not need to be in a particular order (heap or sorted)
        r   r   r   N)r   r	   r
   r0   rw   Úaddn_with_idsr   )rx   r   r   r0   Úkds        r   Ú
add_resultzResultHeap.add_resultÿ   s{   € ð —‘‰ˆˆBÜ× Ñ  ¨)Ô4ˆÜ× Ñ  ¨'Ô2ˆØ�w‰w˜2˜r˜(Ò"Ð"Ð"Ø�T—W‘WŠ}Ðˆ}Ø�
‰
× Ñ  ¤X¨a£[´(¸1³+¸rÕBr!   c           	      óú  — |j                   \  }}|t        |«      k(  sJ ‚|j                  dk(  r|j                   |j                   k(  s!|j                  dk(  r|j                   |fk(  sJ ‚t        j                  |d¬«      }t        j                  |d¬«      }t        j                  |d¬«      }|j                  dk(  rdn|}| j
                  j                  |t        |«      |t        |«      t        |«      |«       y)z·
        Add results for a subset of heaps.
        D, I should hold results for all the subset
        as a special case, if I is 1D, then all ids are assumed to be the same
        é   rI   r   r   r   r   N)r   ÚlenrL   r	   r
   rw   Úaddn_query_subset_with_idsr   )rx   Úsubsetr   r   Únsubsetr}   Ú	id_strides          r   Úadd_result_subsetzResultHeap.add_result_subset  sÖ   € ð —g‘g‰ˆ�Øœ#˜f›+Ò%Ð%Ð%à�F‰F�aŠKØ—‘˜1Ÿ7™7Ò"Ø�v‰v˜Š{Ø—‘˜B˜5Ò ð		
ð!ô
 × Ñ  ¨)Ô4ˆÜ× Ñ  ¨'Ô2ˆÜ×%Ñ% f°GÔ<ˆØŸ™ 1š‘A¨"ˆ	Ø�
‰
×-Ñ-Ø”X˜fÓ% r¬8°A«;¼À»ÀYõ	
r!   c                 ó8   — | j                   j                  «        y ©N)rw   r   ©rx   s    r   ÚfinalizezResultHeap.finalize"  s   € Ø�
‰
×ÑÕr!   N©F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rz   r~   r†   rŠ   © r!   r   ru   ru   ç   s   „ ñóò(Cò
ó,r!   ru   c                 ól  — |j                   | j                   k(  sJ ‚| j                   \  }}}t        j                  ||f| j                  ¬«      }t        j                  ||f|j                  ¬«      }|rt        nt
        } ||||t        | «      t        |«      t        |«      t        |«      «       ||fS )zá
    Merge a set of sorted knn-results obtained from different shards in a
    dataset
    Dall and Iall are of size (nshard, nq, k) each D[i, j] should be sorted
    returns D, I of size (nq, k) as the merged result set
    r   )r   r	   r&   r   Úmerge_knn_results_CMaxÚmerge_knn_results_CMinr   )	ÚDallÚIallry   Únshardr   r   ÚDnewÚInewÚfuncs	            r   Úmerge_knn_resultsrš   &  s�   € ð �:‰:˜Ÿ™Ò#Ð#Ð#Ø—:‘:�L€FˆAˆqÜ�8‰8�Q˜�F $§*¡*Ô-€DÜ�8‰8�Q˜�F $§*¡*Ô-€DÙ%-Õ!Ô3I€DÙØ	Ø	ØÜ�‹Ü�‹Ü�‹Ü�‹ôð �ˆ:Ðr!   c                   ó   — e Zd Zd„ Zd„ Zd„ Zy)ÚMapInt64ToInt64c                 ó,  — t        t        j                  |«      «      | _        |d| j                  z  k(  sJ d«       ‚|| _        t        j
                  |dfd¬«      | _        t        j                  | j                  t        | j                  «      «       y )Nr€   zneed power of 2 capacityr   r   )
re   r	   Úlog2Úlog2_capacityÚcapacityr&   rh   r   Úhashtable_int64_to_int64_initr   )rx   r    s     r   rz   zMapInt64ToInt64.__init__E  sv   € Ü ¤§¡¨Ó!2Ó3ˆÔØ˜1˜d×0Ñ0Ñ0Ò0ÐLÐ2LÓLÐ0Ø ˆŒÜ—8‘8˜X q˜M°Ô9ˆŒÜ×+Ñ+Ø×Ñ¤¨¯©Ó 2õ	
r!   c           	      óÔ   — |j                   \  }|j                   |fk(  sJ ‚t        j                  | j                  t	        | j
                  «      |t	        |«      t	        |«      «       y rˆ   )r   r   Úhashtable_int64_to_int64_addrŸ   r   rh   )rx   ÚkeysÚvalsr   s       r   ÚaddzMapInt64ToInt64.addN  sU   € Ø�z‰z‰ˆØ�z‰z˜a˜TÒ!Ð!Ð!Ü×*Ñ*Ø×ÑÜ�T—X‘XÓØÜ�T‹NÜ�T‹Nõ	
r!   c           	      óâ   — |j                   \  }t        j                  |fd¬«      }t        j                  | j
                  t        | j                  «      |t        |«      t        |«      «       |S r?   )r   r	   r&   r   Úhashtable_int64_to_int64_lookuprŸ   r   rh   )rx   r¤   r   r¥   s       r   ÚlookupzMapInt64ToInt64.lookupY  sZ   € Ø�z‰z‰ˆÜ�x‰x˜˜ GÔ,ˆÜ×-Ñ-Ø×ÑÜ�T—X‘XÓØÜ�T‹NÜ�T‹Nô	
ð ˆr!   N)rŒ   r�   rŽ   rz   r¦   r©   r�   r!   r   rœ   rœ   C  s   „ ò
ò	
ó
r!   rœ   ç        c                 ó�  — t        j                  | d¬«      } t        j                  |d¬«      }| j                  \  }}|j                  \  }}||k(  sJ ‚t        j                  ||fd¬«      }	t        j                  ||fd¬«      }
|t        k(  r:t        t        | «      t        |«      ||||t        |
«      t        |	«      «       |
|	fS |t        k(  r:t        t        | «      t        |«      ||||t        |
«      t        |	«      «       |
|	fS t        t        | «      t        |«      ||||||t        |
«      t        |	«      «
       |
|	fS )aÅ  
    Compute the k nearest neighbors of a vector without constructing an index


    Parameters
    ----------
    xq : array_like
        Query vectors, shape (nq, d) where the dimension d is that same as xb
        `dtype` must be float32.
    xb : array_like
        Database vectors, shape (nb, d) where dimension d is the same as xq
        `dtype` must be float32.
    k : int
        Number of nearest neighbors.
    metric : MetricType, optional
        distance measure to use (either METRIC_L2 or METRIC_INNER_PRODUCT)

    Returns
    -------
    D : array_like
        Distances of the nearest neighbors, shape (nq, k)
    I : array_like
        Labels of the nearest neighbors, shape (nq, k)
    r   r   r   )
r	   r
   r   r&   r'   Ú	knn_L2sqrr   r)   Úknn_inner_productÚknn_extra_metrics)r,   r-   r   r.   r/   r0   r1   r2   r3   r   r   s              r   Úknnr¯   k  s3  € ô2 
×	Ñ	˜b¨	Ô	2€BÜ	×	Ñ	˜b¨	Ô	2€BØ�H‰H�E€BˆØ�X‰X�F€BˆØ�Š7€Nˆ7ä
�‰�"�a� Ô(€AÜ
�‰�"�a� 	Ô*€Aà”ÒÜÜ�R‹Lœ( 2›,¨¨2¨r°1´h¸q³kÄ8ÈAÃ;ô	
ð* ˆaˆ4€Kð% 
Ô'Ò	'ÜÜ�R‹Lœ( 2›,¨¨2¨r°1´h¸q³kÄ8ÈAÃ;ô	
ð" ˆaˆ4€Kô 	Ü�R‹LÜ�R‹LØØØØØØÜ�Q‹KÜ�Q‹Kô	
ð ˆaˆ4€Kr!   c                 óÜ  — | j                   \  }}|j                   \  }}||k(  sJ ‚t        j                  ||fd¬«      }t        j                  ||fd¬«      }	|dk(  ršt        j                  «       }
||
_        ||
_        t        j                  |	«      |
_        t        j                  |«      |
_	        t        j                  |
t        j                  | «      t        j                  |«      ||d«       ||	fS |dk(  rlt        j                  t        j                  | «      t        j                  |«      ||||t        j                  |«      t        j                  |	«      «       ||	fS t        ‚)a²  
    Compute the k nearest neighbors of a set of vectors without constructing
    an index.

    Parameters
    ----------
    xq : array_like
        Query vectors, shape (nq, d) where d is the number of bits / 8
        `dtype` must be uint8.
    xb : array_like
        Database vectors, shape (nb, d) where d is the number of bits / 8
        `dtype` must be uint8.
    k : int
        Number of nearest neighbors.
    variant : string
        Function variant to use, either "mc" (counter) or "hc" (heap)

    Returns
    -------
    D : array_like
        Distances of the nearest neighbors, shape (nq, k)
    I : array_like
        Labels of the nearest neighbors, shape (nq, k)
    ro   r   r   ÚhcrI   Úmc)r   r	   r&   r   Úint_maxheap_array_tr   r   r   r   r   Úhammings_knn_hcÚhammings_knn_mcÚNotImplementedError)r,   r-   r   Úvariantr0   r1   r2   r3   r   r   Úheaps              r   Úknn_hammingr¹   ¦  s2  € ð4 �H‰H�E€BˆØ�X‰X�F€BˆØ�Š7€Nˆ7Ü
�‰�"�a� Ô(€AÜ
�‰�"�a� Ô(€Aà�$‚Ü×(Ñ(Ó*ˆØˆŒØˆŒÜ—>‘> !Ó$ˆŒÜ—>‘> !Ó$ˆŒÜ×ÑØ”%—.‘. Ó$¤e§n¡n°RÓ&8¸"¸aÀô	
ð  ˆaˆ4€Kð 
�DŠÜ×ÑÜ�N‰N˜2ÓÜ�N‰N˜2ÓØØØØÜ�N‰N˜1ÓÜ�N‰N˜1Óô		
ð ˆaˆ4€Kô "Ð!r!   c                   ó2   — e Zd ZdZd„ Zd„ Zdd„Zd	d„Zd„ Zy)
ÚKmeansuÉ  Object that performs k-means clustering and manages the centroids.
    The `Kmeans` class is essentially a wrapper around the C++ `Clustering`
    object.

    Parameters
    ----------
    d : int
       dimension of the vectors to cluster
    k : int
       number of clusters
    gpu: bool or int, optional
       False: don't use GPU
       True: use all GPUs
       number: use this many GPUs
    progressive_dim_steps:
        use a progressive dimension clustering (with that number of steps)

    Subsequent parameters are fields of the Clustring object. The most
    important are:

    niter: int, optional
       clustering iterations
    nredo: int, optional
       redo clustering this many times and keep best
    verbose: bool, optional
    spherical: bool, optional
       do we want normalized centroids?
    int_centroids: bool, optional
       round centroids coordinates to integer
    seed: int, optional
       seed for the random number generator
    init_method: ClusteringInitMethod, optional
       centroid initialization method:
       - ClusteringInitMethod_RANDOM: uniform random sampling (default)
       - ClusteringInitMethod_KMEANS_PLUS_PLUS: k-means++ DÂ²-weighted sampling,
         selects centroids with probability proportional to squared distance
         from existing centroids. Better quality but O(nkd) complexity.
       - ClusteringInitMethod_AFK_MC2: Assumption-Free K-MCÂ², MCMC-based
         approximation using Metropolis-Hastings. Good quality with lower
         complexity than k-means++ for large k.
    afkmc2_chain_length: int, optional
       chain length for AFK-MCÂ² initialization (default 50). Longer chains
       give better approximation to k-means++ but are slower.

    c                 ót  — || _         | j                  |«       d| _        d|v rt        «       | _        nt        «       | _        |j                  «       D ]S  \  }}|dk(  r|dk(  s|dk(  r
t        «       }|| _        Œ't        | j                  |«       t        | j                  ||«       ŒU | j                  «        y)zºd: input dimension, k: nb of centroids. Additional
        parameters are passed on the ClusteringParameters object,
        including niter=25, verbose=False, spherical = False
        FÚprogressive_dim_stepsÚgpuTéÿÿÿÿN)r1   Úresetr¾   Ú"ProgressiveDimClusteringParametersÚcpÚClusteringParametersÚitemsÚget_num_gpusÚgetattrÚsetattrÚ	set_index)rx   r1   r   ÚkwargsÚvs        r   rz   zKmeans.__init__  s›   € ð
 ˆŒØ�
‰
�1ŒØˆŒØ" fÑ,Ü8Ó:ˆD�Gä*Ó,ˆDŒGØ—L‘L–N‰DˆAˆqØ�EŠzØ˜’9  R¢Ü$›�AØ�•ô ˜Ÿ™ Ô#Ü˜Ÿ™  AÕ&ð #ð 	�‰Õr!   c                 ó¨  — | j                   }| j                  j                  t        k(  ru| j                  j                  rt        |«      | _        nt        |«      | _        | j                  r1t        j                  | j                  | j                  ¬«      | _        y y | j                  rt        | j                  ¬«      }n
t        «       }|| _        y )N)Úngpu)r1   rÂ   Ú	__class__rÃ   Ú	sphericalÚIndexFlatIPÚindexÚIndexFlatL2r¾   r   Úindex_cpu_to_all_gpusÚGpuProgressiveDimIndexFactoryÚProgressiveDimIndexFactoryÚfac)rx   r1   rÕ   s      r   rÈ   zKmeans.set_index+  s’   € Ø�F‰FˆØ�7‰7×ÑÔ 4Ò4Ø�w‰w× Ò Ü(¨›^�•
ä(¨›^�”
Ø�xŠxÜ"×8Ñ8Ø—J‘J T§X¡Xô�•
ð ð
 �xŠxÜ3¸¿¹ÔB‘ä0Ó2�ØˆD�Hr!   Nc                 óR   — |�t        |«      | _        d| _        d| _        d| _        y)zeprepare k-means object to perform a new clustering, possibly
        with another number of centroidsN)re   r   Ú	centroidsÚobjÚiteration_stats)rx   r   s     r   rÀ   zKmeans.reset=  s*   € ð ˆ=Ü˜“VˆDŒFØˆŒØˆŒØ#ˆÕr!   c                 óæ  — t        j                  |d¬«      }|j                  \  }}|| j                  k(  sJ ‚| j                  j
                  t        k(  r…t        || j                  | j                  «      }|�D|j                  \  }}||k(  sJ ‚t        j                  |j                  «       |j                  «       |j                  || j                  |«       ng|�J ‚|�J ‚| j                  j                  rJ ‚t!        || j                  | j                  «      }|j                  |t#        |«      | j$                  «       t        j&                  |j                  «      }	|	j)                  | j                  |«      | _        |j*                  }
t-        |
j/                  «       «      D �cg c]  }|
j1                  |«      ‘Œ }
}t        j2                  |
D �cg c]  }|j4                  ‘Œ c}«      | _        dj7                  «       }|
D ��cg c]  }|D �ci c]  }|t9        ||«      “Œ c}‘Œ c}}| _        | j4                  j.                  dkD  r| j4                  d   S dS c c}w c c}w c c}w c c}}w )a  Perform k-means clustering.
        On output of the function call:

        - the centroids are in the centroids field of size (`k`, `d`).

        - the objective value at each iteration is in the array obj (size
          `niter`)

        - detailed optimization statistics are in the array iteration_stats.

        Parameters
        ----------
        x : array_like
            Training vectors, shape (n, d), `dtype` must be float32 and n should
            be larger than the number of clusters `k`.
        weights : array_like
            weight associated to each vector, shape `n`
        init_centroids : array_like
            initial set of centroids, shape (n, d)

        Returns
        -------
        final_obj: float
            final optimization objective

        r   r   ú,obj time time_search imbalance_factor nsplitr   r¿   rª   )r	   r
   r   r1   rÂ   rÍ   rÃ   Ú
Clusteringr   r   Úcopy_array_to_vectorÚravelr×   ÚtrainrÐ   rÎ   ÚProgressiveDimClusteringr   rÕ   Úvector_float_to_arrayÚreshaperÙ   rV   r9   Úatr   rØ   ÚsplitrÆ   )rx   rb   ÚweightsÚinit_centroidsr   r1   ÚclusÚncr3   r×   Ústatsr]   ÚstÚstat_fieldsÚfields                  r   rß   zKmeans.trainF  s
  € ô6 × Ñ  ¨)Ô4ˆØ�w‰w‰ˆˆ1Ø�D—F‘FŠ{Ðˆ{à�7‰7×ÑÔ 4Ò4ä˜a §¡¨¯©Ó1ˆDØÐ)Ø'×-Ñ-‘��BØ˜Q’w��wÜ×*Ñ*Ø"×(Ñ(Ó*¨D¯N©Nôð �J‰J�q˜$Ÿ*™* gÕ.ð �?Ð"�?Ø!Ð)Ð)Ð)Ø—w‘w×(Ò(Ð(Ð(Ü+¨A¨t¯v©v°t·w±wÓ?ˆDØ�J‰J�qœ( 1›+ t§x¡xÔ0ä×/Ñ/°·±Ó?ˆ	à"×*Ñ*¨4¯6©6°1Ó5ˆŒØ×$Ñ$ˆÜ&+¨E¯J©J«LÔ&9Ó:Ñ&9 �—‘˜!•Ð&9ˆÐ:Ü—8‘8©eÓ4©e¨˜RŸV›V¨eÑ4Ó5ˆŒàD×JÑJÓLˆáKPô 
ÙKPÀR±KÓ@±K¨5ˆU”G˜B Ó&Ñ&°KÓ@È5ò 
ˆÔð  $Ÿx™xŸ}™}¨qÒ0ˆt�x‰x˜‰|Ð9°cÐ9ùò ;ùÚ4ùò Aùó 
s$   Æ(IÇI#È		I-ÈI(È&I-É(I-c                 óR  — t        j                  |d¬«      }| j                  €J d«       ‚| j                  j	                  «        | j                  j                  | j                  «       | j                  j                  |d«      \  }}|j                  «       |j                  «       fS )Nr   r   zshould train before assigningrI   )r	   r
   r×   rÐ   rÀ   r¦   ÚsearchrÞ   )rx   rb   r   r   s       r   ÚassignzKmeans.assign„  s   € Ü× Ñ  ¨)Ô4ˆØ�~‰~Ð)ÐJÐ+JÓJÐ)Ø�
‰
×ÑÔØ�
‰
�‰�t—~‘~Ô&Ø�z‰z× Ñ   AÓ&‰ˆˆ1Ø�w‰w‹y˜!Ÿ'™'›)Ð#Ð#r!   rˆ   ©NN)	rŒ   r�   rŽ   r�   rz   rÈ   rÀ   rß   rï   r�   r!   r   r»   r»   ä  s"   „ ñ,ò\ò0ó$$ó<:ó|$r!   r»   c                   ó$   — e Zd ZdZd„ Zd„ Zdd„Zy)ÚSuperKmeansa  Drop-in replacement for `Kmeans` that runs `SuperKMeans` (ADSampling +
    PDX progressive pruning) instead of `Clustering`. Same `centroids`, `obj`,
    `iteration_stats`, and `assign()` surface; additionally exposes
    `gemm_pruning_rates`.

    kwargs are forwarded to `SuperKMeansParameters`. Fields not present on it
    (e.g. `spherical`, `int_centroids`, `nredo`, `frozen_centroids`,
    `init_method`, `update_index`, `early_stop_threshold`,
    `progressive_dim_steps`, `gpu`) raise `AttributeError`.
    c                 ó
  — || _         | j                  |«       d| _        t        «       | _        |j                  «       D ]2  \  }}t        | j                  |«       t        | j                  ||«       Œ4 | j                  «        y )NF)	r1   rÀ   r¾   ÚSuperKMeansParametersrÂ   rÄ   rÆ   rÇ   rÈ   )rx   r1   r   rÉ   ÚkeyrÊ   s         r   rz   zSuperKmeans.__init__™  sc   € ØˆŒØ�
‰
�1ŒØˆŒÜ'Ó)ˆŒØ—l‘l–n‰FˆC�Ü�D—G‘G˜SÔ!Ü�D—G‘G˜S !Õ$ð %ð 	�‰Õr!   c                 ó8   — t        | j                  «      | _        y rˆ   )rÑ   r1   rÐ   r‰   s    r   rÈ   zSuperKmeans.set_index£  s   € Ü  §¡Ó(ˆ�
r!   Nc                 ó¤  — |�J d«       ‚|�J d«       ‚t        j                  |d¬«      }|j                  \  }}|| j                  k(  sJ ‚t	        || j
                  | j                  «      }|j                  |«       t        j                  |j                  «      }|j                  | j
                  |«      | _
        |j                  }t        |j                  «       «      D �	cg c]  }	|j                  |	«      ‘Œ }}	t        j                   |D �
cg c]  }
|
j"                  ‘Œ c}
«      | _        dj%                  «       }|D �
�cg c]  }
|D �ci c]  }|t'        |
|«      “Œ c}‘Œ c}}
| _        t        j                  |j(                  «      | _        | j"                  j                  dkD  r| j"                  d   S dS c c}	w c c}
w c c}w c c}}
w )	Nz$SuperKmeans does not support weightsz+SuperKmeans does not support init_centroidsr   r   rÛ   r   r¿   rª   )r	   r
   r   r1   ÚSuperKMeansr   rÂ   rß   r   Úvector_to_arrayr×   râ   rÙ   rV   r9   rã   r   rØ   rä   rÆ   Úgemm_pruning_rates)rx   rb   rå   ræ   r   r1   Úscr×   ré   r]   rê   rë   rì   s                r   rß   zSuperKmeans.train¦  s�  € ØˆÐFÐ FÓFˆàÐ"ð	9à8ó	9Ø"ä× Ñ  ¨)Ô4ˆØ�w‰w‰ˆˆ1Ø�D—F‘FŠ{Ðˆ{ä˜˜DŸF™F D§G¡GÓ,ˆØ
�‰�Œä×)Ñ)¨"¯,©,Ó7ˆ	Ø"×*Ñ*¨4¯6©6°1Ó5ˆŒØ×"Ñ"ˆÜ&+¨E¯J©J«LÔ&9Ó:Ñ&9 �—‘˜!•Ð&9ˆÐ:Ü—8‘8©eÓ4©e¨˜RŸV›V¨eÑ4Ó5ˆŒØD×JÑJÓLˆáKPô 
ÙKPÀR±KÓ@±K¨5ˆU”G˜B Ó&Ñ&°KÓ@È5ò 
ˆÔô #(×"7Ñ"7¸×8MÑ8MÓ"NˆÔØ#Ÿx™xŸ}™}¨qÒ0ˆt�x‰x˜‰|Ð9°cÐ9ùò ;ùÚ4ùò Aùó 
s$   Ã#F=ÄGÅ	GÅGÅ!GÇGrð   )rŒ   r�   rŽ   r�   rz   rÈ   rß   r�   r!   r   rò   rò   �  s   „ ñ	òò)ô:r!   rò   c                 óJ   — t        | t        j                  j                  «      S rˆ   )Ú
isinstanceÚcollectionsÚabcÚSequencera   s    r   Úis_sequencer  Ä  s   € Ü�aœŸ™×1Ñ1Ó2Ð2r!   c           	      ó  — | j                   \  }}t        j                  | d¬«      } t        |«      rŽt        j                  |d¬«      }|j                   |fk(  sJ ‚t	        |j                  «       dz   dz  «      }t        j                  ||fd¬«      }t        ||t        |«      t        | «      t        |«      |«       |S ||z  dz   dz  }t        j                  ||fd¬«      }t        |||t        | «      t        |«      |«       |S )a>  
    Pack a set integers (i, j) where i=0:n and j=0:M into
    n bitstrings.
    Output is an uint8 array of size (n, code_size), where code_size is
    such that at most 7 bits per code are wasted.

    If nbit is an integer: all entries takes nbit bits.
    If nbit is an array: entry (i, j) takes nbit[j] bits.
    ro   r   é   é   rH   )	r   r	   r
   r  re   Úsumr&   Úpack_bitstrings_cr   )rO   Únbitr   ÚMÚ	code_sizeÚbs         r   Úpack_bitstringsr  Ë  sí   € ð �7‰7�D€A€qÜ
×Ñ˜Q gÔ.€AÜ�4ÔÜ×#Ñ# D°Ô8ˆØ�z‰z˜a˜TÒ!Ð!Ð!Ü˜Ÿ™› a™¨AÑ-Ó.ˆ	Ü�H‰H�a˜�^¨7Ô3ˆÜØˆq”(˜4“.¤(¨1£+¬x¸«{¸Iô	
ð €Hð ˜‘X ‘\ aÑ'ˆ	Ü�H‰H�a˜�^¨7Ô3ˆÜ˜!˜Q ¤h¨q£k´8¸A³;À	ÔJØ€Hr!   c           
      óâ  — | j                   \  }}|€Žt        j                  |d¬«      }t        |«      }t	        |j                  «       dz   dz  «      }||k\  sJ ‚t        j                  ||fd¬«      }t        ||t        |«      t        | «      |t        |«      «       |S |}||z  dz   dz  }||k\  sJ ‚t        j                  ||fd¬«      }t        |||t        | «      |t        |«      «       |S )a   
    Unpack a set integers (i, j) where i=0:n and j=0:M from
    n bitstrings (encoded as uint8s).
    Input is an uint8 array of size (n, code_size), where code_size is
    such that at most 7 bits per code are wasted.

    Two forms:
    - when called with (array, M, nbit): there are M entries of size
      nbit per row
    - when called with (array, nbits): element (i, j) is encoded in
      nbits[j] bits
    ro   r   r  r  )	r   r	   r
   r�   re   r  r&   Úunpack_bitstrings_cr   )r
  Ú
M_or_nbitsr  r   r	  r  Úmin_code_sizerO   s           r   Úunpack_bitstringsr  é  sî   € ð —7‘7�L€A€yØ€|Ü×#Ñ# J°gÔ>ˆÜ�‹IˆÜ˜TŸX™X›Z¨!™^°Ñ1Ó2ˆØ˜MÒ)Ð)Ð)Ü�H‰H�a˜�V 7Ô+ˆÜØˆq”(˜4“.¤(¨1£+¨y¼(À1»+ô	
ð €Hð ˆØ˜T™ A™¨!Ñ+ˆØ˜MÒ)Ð)Ð)Ü�H‰H�a˜�V 7Ô+ˆÜ˜A˜q $¬°«°YÄÈÃÔLØ€Hr!   )é90  )r  N)iÒ  )Nr   r‹   )r±   rˆ   )#Únumpyr	   Úfaiss.loaderr   Úcollections.abcrþ   r   r$   r'   r5   r=   rC   ÚlrandrF   rQ   rT   rS   r^   rc   rm   rg   rs   rp   ru   rš   rœ   r¯   r¹   r»   rò   r  r  r  r  r  r�   r!   r   Ú<module>r     sÝ   ðó ä ã ã òò$ð$ '0¸Aó ó6óð 	€óòð ,Ð óòò8ð €ó!ðH  :Ð ó÷L<ñ <ó~÷: ñ  ðP $°ó 8óv6÷|f$ñ f$ôR/:�&ô /:òn3ð $Ð òð6 (Ð ôr!   