
    ^Nj                     (   U d dl mZmZmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZmZ d dlmZmZ d d	lmZmZ  ed
dddd ed      d       eddddd ed      d      gZee   ed<    G d deee         Z G d dee         Zy)    )AnyIterableSequenceTypeN)OnnxProvider)OnnxOutputContext)Device)define_cache_dir)SparseEmbeddingSparseTextEmbeddingBase)OnnxTextModelTextEmbeddingWorker)SparseModelDescriptionModelSourcezprithivida/Splade_PP_en_v1i:w  z9Independent Implementation of SPLADE++ Model for English.z
apache-2.0g/$?zQdrant/Splade_PP_en_v1)hfz
model.onnx)model
vocab_sizedescriptionlicense
size_in_GBsources
model_filezprithvida/Splade_PP_en_v1supported_splade_modelsc                   f    e Zd Zdededee   fdZ	 ddeee   z  de	dede	fdZ
edee   fd       Zd	d	d	ej                   d	d
d	d	fdeded	z  de	d	z  dee   d	z  deez  dee	   d	z  dede	d	z  ded	z  def fdZddZ	 	 ddeee   z  de	de	d	z  dedee   f
dZedeee      fd       Z xZS )SpladePPoutputkwargsreturnc              +   x  K   |j                   t        d      t        j                  dt        j                  |j
                  d      z         }|t        j                  |j                   d      z  }t        j                  |d      }|D ])  }|j                         d   }||   }t        ||       + y w)Nz<attention_mask must be provided for document post-processing   r   )axis)valuesindices)
attention_mask
ValueErrornplogmaximummodel_outputexpand_dimsmaxnonzeror   )selfr   r   relu_logweighted_logscores
row_scoresr$   s           k/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastembed/sparse/splade_pp.py_post_process_onnx_outputz"SpladePP._post_process_onnx_output&   s        ([\\66!bjj)<)<a@@A"..1F1FR"PP1- !J ((*1-G(F!AA !s   B8B:texts
batch_sizec                 ,     | j                   |fd|i|S )Nr6   )_token_count)r.   r5   r6   r   s       r3   token_countzSpladePP.token_count9   s!     !t  H:HHH    c                     t         S )zLists the supported models.

        Returns:
            list[SparseModelDescription]: A list of SparseModelDescription objects containing the model information.
        )r   clss    r3   _list_supported_modelszSpladePP._list_supported_models>   s
     '&r:   NF
model_name	cache_dirthreads	providerscuda
device_ids	lazy_load	device_idspecific_model_pathc
                    t        |   |||fi |
 || _        || _        | j	                  |
      | _        || _        || _        d| _        ||| _        n | j                  | j                  d   | _        | j                  |      | _
        t        t        |            | _        |	| _        | j                  | j                  | j                  | j                   | j                        | _        | j                  s| j%                          yy)a  
        Args:
            model_name (str): The name of the model to use.
            cache_dir (str, optional): The path to the cache directory.
                                       Can be set using the `FASTEMBED_CACHE_PATH` env variable.
                                       Defaults to `fastembed_cache` in the system's temp directory.
            threads (int, optional): The number of threads single onnxruntime session can use. Defaults to None.
            providers (Optional[Sequence[OnnxProvider]], optional): The list of onnxruntime providers to use.
                Mutually exclusive with the `cuda` and `device_ids` arguments. Defaults to None.
            cuda (Union[bool, Device], optional): Whether to use cuda for inference. Mutually exclusive with `providers`
                Defaults to Device.
            device_ids (Optional[list[int]], optional): The list of device ids to use for data parallel processing in
                workers. Should be used with `cuda` equals to `True`, `Device.AUTO` or `Device.CUDA`, mutually exclusive
                with `providers`. Defaults to None.
            lazy_load (bool, optional): Whether to load the model during class initialization or on demand.
                Should be set to True when using multiple-gpu and parallel encoding. Defaults to False.
            device_id (Optional[int], optional): The device id to use for loading the model in the worker process.
            specific_model_path (Optional[str], optional): The specific path to the onnx model dir if it should be imported from somewhere else

        Raises:
            ValueError: If the model_name is not in the format <org>/<model> e.g. BAAI/bge-base-en.
        Nr   )local_files_onlyrG   )super__init__rB   rE   _select_exposed_session_options_extra_session_optionsrD   rC   rF   _get_model_descriptionmodel_descriptionstrr
   r@   _specific_model_pathdownload_model_local_files_only
_model_dirload_onnx_model)r.   r?   r@   rA   rB   rC   rD   rE   rF   rG   r   	__class__s              r3   rK   zSpladePP.__init__G   s    F 	YB6B""&*&J&J6&R# %	 &* &DN__(!__Q/DN!%!<!<Z!H-i89$7!--""NN!33 $ 9 9	 . 
 ~~  " r:   c           	          | j                  | j                  | j                  j                  | j                  | j
                  | j                  | j                  | j                         y )N)	model_dirr   rA   rB   rC   rF   extra_session_options)	_load_onnx_modelrT   rO   r   rA   rB   rC   rF   rM   )r.   s    r3   rU   zSpladePP.load_onnx_model   sP    oo--88LLnnnn"&"="= 	 	
r:   	documentsparallelc              +     K    | j                   d| j                  t        | j                        |||| j                  | j
                  | j                  | j                  | j                  | j                  d|E d{    y7 w)a  
        Encode a list of documents into list of embeddings.
        We use mean pooling with attention so that the model can handle variable-length inputs.

        Args:
            documents: Iterator of documents or single document to embed
            batch_size: Batch size for encoding -- higher values will use more memory, but be faster
            parallel:
                If > 1, data-parallel encoding will be used, recommended for offline encoding of large datasets.
                If 0, use all available cores.
                If None, don't use data-parallel processing, use default onnxruntime threading instead.

        Returns:
            List of embeddings, one per document
        )r?   r@   r[   r6   r\   rB   rC   rD   rI   rG   rY   N )
_embed_documentsr?   rP   r@   rB   rC   rD   rS   rQ   rM   )r.   r[   r6   r\   r   s        r3   embedzSpladePP.embed   s}     , )4(( 
$..)!nn!33 $ 9 9"&"="=
 
 	
 	
s   A;B=B>Bc                     t         S )N)SpladePPEmbeddingWorkerr<   s    r3   _get_worker_classzSpladePP._get_worker_class   s    &&r:   )i   )r   N)   N)__name__
__module____qualname__r   r   r   r   r4   rP   intr9   classmethodlistr   r>   r	   AUTOr   r   boolrK   rU   r`   r   r   rc   __classcell__)rV   s   @r3   r   r   %   s   B'B36B	/	"B( =AI8C=(I69ILOI	I
 't,B'C ' ' !%"37$kk'+ $*.?#?# :?# t	?#
 L)D0?# Vm?# I$?# ?# :?# !4Z?# ?#B	
 #	#
#&#
 #
 *	#

 #
 
/	"#
J '$':?'K"L ' 'r:   r   c                   $    e Zd ZdedededefdZy)rb   r?   r@   r   r   c                      t        d||dd|S )Nr    )r?   r@   rA   r^   )r   )r.   r?   r@   r   s       r3   init_embeddingz&SpladePPEmbeddingWorker.init_embedding   s'     
!
 	
 	
r:   N)re   rf   rg   rP   r   r   rp   r^   r:   r3   rb   rb      s$    
 
 
 
PX 
r:   rb   )typingr   r   r   r   numpyr'   fastembed.commonr   fastembed.common.onnx_modelr   fastembed.common.typesr	   fastembed.common.utilsr
   &fastembed.sparse.sparse_embedding_baser   r   fastembed.text.onnx_text_modelr   r   "fastembed.common.model_descriptionr   r   r   rj   __annotations__r   rb   r^   r:   r3   <module>r{      s    0 0  ) 9 ) 3 N R *O78 )O789 45 ,U'&o(F U'p
1/B 
r:   