o
    rh                     @   s^   d dl Zd dlZdd Zdd Zdd Zdd	 Zd
d ZdddZdd Z	dd Z
dd ZdS )    Nc           
   
   C   s2  t | } | |}tj|dd}d }}zm| |}|dkr*t t |||j | 	|}| j
t jjkrAtj|| j
fdd}n| j}| j}|| d | }	tj|	|| |fdd}|dkrjt t |||j W |duru| || |dur| || ||fS |dur| || |dur| || w w )zy returns the inverted lists content as a pair of (list_ids, list_codes).
    The codes are reshaped to a proper size
    int64dtypeNr   uint8   )faissdowncast_InvertedLists	list_sizenpzerosget_idsmemcpyswig_ptrnbytes	get_codes	code_sizeInvertedListsINVALID_CODE_SIZEn_per_block
block_sizerelease_idsrelease_codes)
invlistsllslist_idsidscodes
list_codesnpbbsls_round r"   Y/var/www/html/alpaca_bot/venv/lib/python3.10/site-packages/faiss/contrib/inspect_tools.pyget_invlist
   s8   



r$   c                    s"   t j fddt jD ddS )z1 return the array of sizes of the inverted lists c                    s   g | ]}  |qS r"   )r	   .0ir   r"   r#   
<listcomp>+   s    z%get_invlist_sizes.<locals>.<listcomp>r   r   )r
   arrayrangenlistr(   r"   r(   r#   get_invlist_sizes)   s
   r-   c                 C   s*   | j jD ]}t| dt| |  qdS )z3 list values all fields of an object known to SWIG z = N)	__class____swig_getmethods__printgetattr)objnamer"   r"   r#   print_object_fields1   s   r4   c                 C   s    t | j}|| j| j| jS )z% return the PQ centroids as an array )r   vector_to_array	centroidsreshapeMksubdsub)pqcenr"   r"   r#   get_pq_centroids8   s   r=   c                 C   s,   t | j}t | j| j| j}||fS )zo extract matrix + bias from the PCA object
    works for any linear transform (OPQ, random rotation, etc.)
    )r   r5   bAr7   d_outd_in)pcar>   r?   r"   r"   r#   get_LinearTransform_matrix>   s   rC   c                 C   sn   | j \}}|dur|j |fksJ t|||du}t|  |j |dur.t||j d|_|  |S )zA make a linear transform from a matrix and a bias term (optional)NT)	shaper   LinearTransformcopy_array_to_vectorravelr?   r>   
is_trainedset_is_orthonormal)r?   r>   r@   rA   ltr"   r"   r#   make_LinearTransform_matrixG   s   
rK   c                    s<   t | jd| jt | j  fddt| jD S )z. return to codebooks of an additive quantizer c                    s$   g | ]} |  |d    qS )r   r"   r%   co	codebooksr"   r#   r)   Y   s    z4get_additive_quantizer_codebooks.<locals>.<listcomp>)r   r5   rO   r7   dcodebook_offsetsr+   r8   )aqr"   rM   r#    get_additive_quantizer_codebooksU   s
   rS   c                 C   s"   t | jd}|| j| jS )z1 copy and return the data matrix in an IndexFlat float32)r   r5   r   viewr7   ntotalrP   )indexxbr"   r"   r#   get_flat_data_   s   rY   c                 C   s:   |   }tj|j|jfdd}tt||j|j	 |S )zf get the neighbor list for the vectors stored in the NSG structure, as
    a N-by-K matrix of indices int32r   )
get_final_graphr
   r   NKr   r   r   datar   )nsggraph	neighborsr"   r"   r#   get_NSG_neighborse   s   rb   )N)numpyr
   r   r$   r-   r4   r=   rC   rK   rS   rY   rb   r"   r"   r"   r#   <module>   s   
	
