
    wfh;?                       d dl mZ d dlZd dlmZmZ d dlmZmZ 	 d dlm	Z	 d dlZd dlmZmZmZmZ d dlmZ d dlmZ d d	lmZ er e       rd d
lmZ  ej4                  e      Z G d de      Zy# e
$ r	 d dlm	Z	 Y \w xY w)    )annotationsN)TYPE_CHECKINGAny)load_onnx_modelload_openvino_model)Self)
AutoConfigAutoModelForMaskedLMAutoTokenizerPretrainedConfig)is_peft_available)find_adapter_config_file)InputModule
PeftConfigc                      e Zd ZU dZdZded<   ddgZded<   d	Zd
ed<   	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZ	 	 	 	 	 	 	 	 	 	 ddZ		 	 	 	 	 	 	 	 	 	 ddZ
ddZ	 d	 	 	 	 	 ddZddZddZdd dZe	 	 	 	 	 	 	 	 	 	 d!	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d"d       Ze	 	 	 	 	 	 	 	 	 	 d!	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d#d       Ze	 	 	 	 	 	 d$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d% fd       Z xZS )&MLMTransformerax  
    MLMTransformer adapts a Masked Language Model (MLM) for sparse encoding applications.

    This class extends the Transformer class to work specifically with models that have a
    MLM head (like BERT, RoBERTa, etc.) and is designed to be used with SpladePooling
    for creating SPLADE sparse representations.

    MLMTransformer accesses the MLM prediction head to get vocabulary logits for each token,
    which are later used by SpladePooling to create sparse lexical representations.

    Args:
        model_name_or_path: Hugging Face models name
            (https://huggingface.co/models)
        max_seq_length: Truncate any inputs longer than max_seq_length
        model_args: Keyword arguments passed to the Hugging Face
            MLMTransformers model
        tokenizer_args: Keyword arguments passed to the Hugging Face
            MLMTransformers tokenizer
        config_args: Keyword arguments passed to the Hugging Face
            MLMTransformers config
        cache_dir: Cache dir for Hugging Face MLMTransformers to store/load
            models
        do_lower_case: If true, lowercases the input (independent if the
            model is cased or not)
        tokenizer_name_or_path: Name or path of the tokenizer. When
            None, then model_name_or_path is used
        backend: Backend used for model inference. Can be only `torch` for now for this class.
    sentence_bert_config.jsonstrconfig_file_namemax_seq_lengthdo_lower_casez	list[str]config_keysTboolsave_in_rootc
           	        t         |           || _        |	| _        |i }|i }|i }| j	                  |||	|      \  | _        }
 | j                  d|| j
                  |	|
|d| |	d|vr||d<   t        j                  ||n|fd|i|| _	        || _
        |ht        | j
                  d      rQt        | j                  d      r:t        | j
                  j                  | j                  j                        | _
        y y y y )N)model_name_or_path	cache_dirbackendconfig_args)r   configr   is_peft_modelr   model_max_lengthr   max_position_embeddings )super__init__r   r   _load_configr!   _load_modelr   from_pretrained	tokenizerr   hasattrminr$   r#   )selfr   r   
model_argstokenizer_argsr    r   r   tokenizer_name_or_pathr   r"   	__class__s              x/home/chris/cleankitchens-env/lib/python3.12/site-packages/sentence_transformers/sparse_encoder/models/MLMTransformer.pyr'   zMLMTransformer.__init__<   sE    	*J!NK%)%6%61YPWep &7 &
"] 	 	
1;;'	
 	
 %*<N*R1?N-.&66'='I#Oa

 
 -!t{{$=>74>>[mCn&)$++*M*Mt~~OnOn&o# Do> "    c           
     @   t        |||j                  d      |j                  d      |j                  dd            	 Dt               st        d      |dk7  rt	        d      d	d
lm}  |j                  |fi |d|idfS t        j                  |fi |d|idfS )a  Loads the transformers or PEFT configuration

        Args:
            model_name_or_path (str): The model name on Hugging Face (e.g. 'naver/splade-cocondenser-ensembledistil')
                or the path to a local model directory.
            cache_dir (str | None): The cache directory to store the model configuration.
            backend (str): Backend used for model inference. Can be only `torch` for now for this class.
            config_args (dict[str, Any]): Keyword arguments passed to the Hugging Face Transformers config.

        Returns:
            tuple[PretrainedConfig, bool]: The model configuration and a boolean indicating whether the model is a PEFT model.
        tokenrevisionlocal_files_onlyF)r   r6   r7   r8   zgLoading a PEFT model requires installing the `peft` package. You can install it via `pip install peft`.torcha  PEFT models can currently only be loaded with the `torch` backend. To use other backends, load the model with `backend="torch"`, call `model.transformers_model.merge_and_unload()`, save that model with `model.save_pretrained()` and then load the model with the desired backend.r   r   r   T)	r   getr   	Exception
ValueErrorpeftr   r*   r	   )r.   r   r   r   r    r   s         r3   r(   zMLMTransformer._load_configm   s      %"#!oog.$4!,1CU!K  %&}  '! w 
 (-:--.@eKe[degkkk))*<aaW`achhhr4   c                   |dk(  r9|rdD ]  }|j                  |d        t        j                  |fd|i|| _        y|dk(  rt	        d||dd|| _        y|dk(  rt        d||dd|| _        yt        d	| d
      )a  Loads the transformers or PEFT model into the `auto_model` attribute

        Args:
            model_name_or_path (str): The model name on Hugging Face (e.g. 'naver/splade-cocondenser-ensembledistil')
                or the path to a local model directory.
            config ("PeftConfig" | PretrainedConfig): The model configuration.
            cache_dir (str | None): The cache directory to store the model configuration.
            backend (str): Backend used for model inference. Can be `torch`, `onnx`, or `openvino`.
            is_peft_model (bool): Whether the model is a PEFT model.
            model_args (dict[str, Any]): Keyword arguments passed to the Hugging Face Transformers model.
        r9   )r7   Nr!   onnxz	fill-mask)r   r!   	task_nameopenvinozUnsupported backend 'z6'. `backend` should be `torch`, `onnx`, or `openvino`.r%   )popr
   r*   
auto_modelr   r   r<   )r.   r   r!   r   r"   r/   adapter_only_kwargs          r3   r)   zMLMTransformer._load_model   s    ( g *6 =&NN#5t<= 3BBCUs^dshrsDO- #5% 	DO 
"1 #5% 	DO 4WI=stuur4   c                    |j                         D ci c]  \  }}|dv r|| }}} | j                  di |}	 |j                  |d<   |S c c}}w # t        $ r |d   |d<   Y |S w xY w)zGReturns the MLM head logits for the input features as token embeddings.)	input_idsattention_masktoken_type_idsinputs_embedstoken_embeddingsr   r%   )itemsrC   logitsAttributeError)r.   featureskeyvaluetrans_featuresoutputss         r3   forwardzMLMTransformer.forward   s     'nn.
UXX J
 
 "$//3N3	6+2>>H'( 
  	6+21:H'(	6s   AA A'&A'c           
        i }t        |d   t              r|g}nt        |d   t              r\g }g |d<   |D ]L  }t        t	        |j                                     \  }}|j                  |       |d   j                  |       N |g}n7g g }	}|D ]*  }
|j                  |
d          |	j                  |
d          , ||	g}|D cg c])  }|D cg c]  }t        |      j                          c}+ }}}| j                  r-|D cg c]   }|D cg c]  }|j                          c}" }}}|j                   | j                  ||dd| j                  d       |S c c}w c c}}w c c}w c c}}w )z-Tokenizes a text and maps tokens to token-idsr   	text_keys   longest_firstpt)padding
truncationreturn_tensors
max_length)
isinstancer   dictnextiterrK   appendstripr   lowerupdater+   r   )r.   textsrY   outputto_tokenizelookuptext_keytextbatch1batch2
text_tuplecolss                r3   tokenizezMLMTransformer.tokenize   sr    eAh$ 'Ka$'K"$F; 5!%d6<<>&:!;$""4({#**845 '-KFF# -
jm,jm,- "6*K ALL41A4LL ?JKs3!AGGI3KKKDNN*#..	
  5L 4Ks0   	E$ E.E$	E/E*'E/E$*E/c                B    | j                   j                  j                  S )N)rC   r!   
vocab_sizer.   s    r3    get_sentence_embedding_dimensionz/MLMTransformer.get_sentence_embedding_dimension   s    %%000r4   c                |    dt        | j                         | j                  j                  j                         dS )NzMLMTransformer()architecture))r^   get_config_dictrC   r2   __name__rs   s    r3   __repr__zMLMTransformer.__repr__   s3     d&:&:&<4??KdKdKmKm!n oopqqr4   c                    | j                   j                  ||       | j                  |       | j                  |       y )N)safe_serialization)rC   save_pretrainedsave_tokenizersave_config)r.   output_pathr|   kwargss       r3   savezMLMTransformer.save  s6    ''HZ'[K(%r4   c                N    | j                  |||||||||	|
|      } | dd|i|S )N)r   	subfolderr6   cache_folderr7   r8   trust_remote_codemodel_kwargstokenizer_kwargsconfig_kwargsr   r   r%   )_load_init_kwargs)clsr   r   r6   r   r7   r8   r   r   r   r   r   r   init_kwargss                 r3   loadzMLMTransformer.load  sP    $ ++1%-/%-' , 
 H&8HKHHr4   c                   | j                  ||||||      }|||||d}d|vri |d<   d|vri |d<   d|vri |d<   |d   j                  |       |d   j                  |       |d   j                  |       |r|d   j                  |       |	r|d   j                  |	       |
r|d   j                  |
       i |||dS )N)r   r   r6   r   r7   r8   )r   r6   r7   r8   r   r/   r0   r    )r   r   )load_configrd   )r   r   r   r6   r   r7   r8   r   r   r   r   r   r   r!   
hub_kwargss                  r3   r   z MLMTransformer._load_init_kwargs(  s   $ 1%- ! 
 #  0!2

 v%#%F< 6)')F#$&$&F=! 	|##J/ ''
3}$$Z0 < ''5#$++,<==!((7H&H|HHr4   c           
        |r|gng d}|D ]  }t         
|   |||||||      }	|	s n d	v rd|	d   v r|	d   j                  d       d|	v rd|	d   v r|	d   j                  d       d|	v rd|	d   v r|	d   j                  d       |	S )N)r   zsentence_roberta_config.jsonzsentence_distilbert_config.jsonzsentence_camembert_config.jsonzsentence_albert_config.jsonz sentence_xlm-roberta_config.jsonzsentence_xlnet_config.json)r   r   config_filenamer6   r   r7   r8   r/   r   r0   r    )r&   r   rB   )r   r   r   r   r6   r   r7   r8   config_filenamesr!   r2   s             r3   r   zMLMTransformer.load_configb  s       	  0 	OW(#5# /)!!1 ) F 	 6!&9VL=Q&Q< $$%89v%*=HXAY*Y#$(()<=F"':f]>S'S=!%%&9:r4   )NNNNNFNr9   )r   r   r   z
int | Noner/   dict[str, Any] | Noner0   r   r    r   r   
str | Noner   r   r1   r   r   r   returnNone)
r   r   r   r   r   r   r    dict[str, Any]r   z*tuple[PeftConfig | PretrainedConfig, bool])
r   r   r!   zPeftConfig | PretrainedConfigr   r   r"   r   r   r   )rN   dict[str, torch.Tensor]r   r   )T)re   z.list[str] | list[dict] | list[tuple[str, str]]rY   z
str | boolr   r   )r   int)r   r   )r   r   r|   r   r   r   )
 NNNFFNNNr9   )r   r   r   r   r6   bool | str | Noner   r   r7   r   r8   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r6   r   r   r   r7   r   r8   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   NNNNF)r   r   r   r   r   r   r6   r   r   r   r7   r   r8   r   r   r   )ry   
__module____qualname____doc__r   __annotations__r   r   r'   r(   r)   rS   rp   rt   rz   r   classmethodr   r   r   __classcell__)r2   s   @r3   r   r      sM   : 8c7.@K@L$
 &*,004-1 $#-1/p/p #/p *	/p
 ./p +/p /p /p !+/p /p 
/pb(i"%(i2<(iGJ(iYg(i	3(iT+v+v .+v 	+v
 +v 
+vZ" \`&C&NX&	 &P1r&
 
 #'#'#!&"'.226/3II 	I
 !I !I I I  I ,I 0I -I I  
!I I@ 
 #'#'#!&"'.226/37I7I 	7I
 !7I !7I 7I 7I  7I ,7I 07I -7I 7I  
!7I 7Ir  &*#'#'#!&++ + $	+
 !+ !+ + + 
+ +r4   r   )
__future__r   loggingtypingr   r   sentence_transformers.backendr   r   r   ImportErrortyping_extensionsr9   transformersr	   r
   r   r   transformers.utils.import_utilsr   transformers.utils.peft_utilsr   (sentence_transformers.models.InputModuler   r=   r   	getLoggerry   loggerr   r%   r4   r3   <module>r      sm    "  % N'  Z Z = B @&(			8	$t[ t!  '&'s   A1 1A?>A?