o
    rh                     @   sV   d dl Zd dlZd dlmZ dddZdddZdddZd	d
 Zdd Z	dd Z
dS )    N)get_invlist_sizesc                 C   s~   |j \}}|j |fksJ t| tjr|d9 }|| jksJ |dur/|j |fks*J t|}| |t||t| dS )zP
    Add elements to an IVF index, where the assignment is already computed
       N)shape
isinstancefaissIndexBinaryIVFdswig_ptradd_core)	index_ivfxaidsnr    r   U/var/www/html/alpaca_bot/venv/lib/python3.10/site-packages/faiss/contrib/ivf_tools.pyadd_preassigned   s   

r   c           	      C   s   t | tjr%| j dksJ dt| jd}||}t| j	} |j
\}}t | tjr7|d9 }d}nd}|| jks@J |j
|| jfksJJ |du rYtj|| jf|d}n
|j
|| jfkscJ | ||||S )	z
    Perform a search in the IVF index, with predefined lists to search into.
    Supports indexes with pretransforms (as opposed to the
    IndexIVF.search_preassigned, that cannot be applied with pretransform).
       z"chain must have only one componentr   r   int32float32Ndtype)r   r   IndexPreTransformchainsizedowncast_VectorTransformatapplydowncast_indexindexr   r   r   nprobenpzerossearch_preassigned)	r   xqklist_nos
coarse_dis	transformr   r   dis_typer   r   r   r#      s    

r#   c                 C   s   |j \}}t| tjr|d9 }d}nd}|du r#tj|| jf|d}n
|j || jfks-J || jks4J |j || jfks>J t|}tj	}	| 
||	|||	||	|| t|j|d  }
t|
d }t|j| }t|j| }|
||fS )z[
    Perform a range search in the IVF index, with predefined lists to
    search into
    r   r   r   Nr   r   )r   r   r   r   r!   emptyr    r   RangeSearchResultr	   range_search_preassigned_crev_swig_ptrlimscopyint	distanceslabels)r   r   radiusr&   r'   r   r   r)   resspr/   num_resultsdistindicesr   r   r   range_search_preassigned<   s,   



r:   c                 C   s   |j dkr| j }|| || n|j | jksJ | j}d| _t| j}|j	
| || _t| dr?| j| |S |g| _|S )zQ replace the IVF quantizer with a flat quantizer and return the
    old quantizerr   Freferenced_objects)ntotal	quantizerreconstruct_ntrainaddnlist
own_fieldsr   r   thisownhasattrr;   append)r   new_quantizer	centroidsold_ownold_quantizerr   r   r   replace_ivf_quantizerb   s   



rK   c                 C   s   |j \}| j|ksJ t| j}|j| jksJ tj|dd}tj||d}t	|tj
|tdks5J || t| j}|t| dS )z Apply some permutation to the inverted lists, and modify the quantizer
    entries accordingly.
    Perm is an array of size nlist, where old_index = perm[new_index]
    int64r   )	minlengthN)r   rA   r   r   r=   r<   r!   ascontiguousarraybincountallonesr1   permute_entriesdowncast_InvertedListsinvlistspermute_invlistsr	   )r   permrA   r=   bcrT   r   r   r   rU   z   s   
rU   c                 C   s"   t | j}t|}t| | d S N)r   rT   r!   argsortrU   )r   invlist_sizesrV   r   r   r   sort_invlists_by_size   s   

r[   rX   )numpyr!   r   faiss.contrib.inspect_toolsr   r   r#   r:   rK   rU   r[   r   r   r   r   <module>   s   


&