§
    æ˜ºh^  ã                   ó˜   — d dl mZmZ d dlmZmZ d dlmZ d dlm	Z	m
Z
mZmZ d dlZ ej        e¦  «        Z G d„ dej        ¦  «        ZdS )é    )ÚnnÚTensor)ÚIterableÚDict)ÚSentenceTransformer)Ú
AutoConfigÚAutoTokenizerÚAutoModelForCausalLMÚPreTrainedModelNc                   ód   ‡ — e Zd ZdZddededefˆ fd„Zd„ Zd	e	e
eef                  d
efd„Zˆ xZS )ÚDenoisingAutoEncoderLossaÇ  
    This loss expects as input a batch consisting of damaged sentences and the corresponding original ones.
    The data generation process has already been implemented in readers/DenoisingAutoEncoderReader.py
    During training, the decoder reconstructs the original sentences from the encoded sentence embeddings.
    Here the argument 'decoder_name_or_path' indicates the pretrained model (supported by Huggingface) to be used as the decoder.
    Since decoding process is included, here the decoder should have a class called XXXLMHead (in the context of Huggingface's Transformers).
    Flag 'tie_encoder_decoder' indicates whether to tie the trainable parameters of encoder and decoder,
    which is shown beneficial to model performance while limiting the amount of required memory.
    Only when the encoder and decoder are from the same architecture, can the flag 'tie_encoder_decoder' works.
    For more information, please refer to the TSDAE paper.
    NTÚmodelÚdecoder_name_or_pathÚtie_encoder_decoderc                 óž  •— t          t          | ¦  «                             ¦   «          || _        |j        | _        |d         j        j        j        }|€|s
J d¦   «         ‚|r|rt           
                    d¦  «         |}t          j        |¦  «        | _        t          | j        t          | j        ¦  «        ¦  «         | _        t#          j        |¦  «        }d|_        d|_        d|i}	 t)          j        |fi |¤Ž| _        n2# t,          $ r%}t                               d|› d�¦  «         |‚d}~ww xY w|d         j        j        j        |j        k    s
J d	¦   «         ‚| j        j        €6| j        j        | j        _        | j        j        j        | j        j        _        t;          t          j        |¦  «        ¦  «        t;          | j        ¦  «        k    rt           
                    d
¦  «         |rÖ| j        r
J d¦   «         ‚t;          | j        ¦  «        t;          | j        ¦  «        k    rR| j        | _        | j                             t;          | j        ¦  «        ¦  «         t           
                    d¦  «         | j        j        }tA          j!        |d         j        | j        j"        |         | j        j        ¦  «         dS dS )a  
        :param model: SentenceTransformer model
        :param decoder_name_or_path: Model name or path for initializing a decoder (compatible with Huggingface's Transformers)
        :param tie_encoder_decoder: whether to tie the trainable parameters of encoder and decoder
        r   NzOMust indicate the decoder_name_or_path argument when tie_encoder_decoder=False!zHWhen tie_encoder_decoder=True, the decoder_name_or_path will be invalid.TÚconfigzModel name or path "zc" does not support being as a decoder. Please make sure the decoder model has an "XXXLMHead" class.zHidden sizes do not match!znWARNING: The vocabulary of the encoder has been changed. One might need to change the decoder vocabulary, too.z@The tokenizers should be the same when tie_encoder_decoder=True.zˆSince the encoder vocabulary has been changed and --tie_encoder_decoder=True, now the new vocabulary has also been used for the decoder.)#Úsuperr   Ú__init__ÚencoderÚ	tokenizerÚtokenizer_encoderÚ
auto_modelr   Ú_name_or_pathÚloggerÚwarningr	   Úfrom_pretrainedÚtokenizer_decoderÚ
isinstanceÚtypeÚneed_retokenizationr   Ú
is_decoderÚadd_cross_attentionr
   ÚdecoderÚ
ValueErrorÚerrorÚhidden_sizeÚ	pad_tokenÚ	eos_tokenÚeos_token_idÚpad_token_idÚlenÚresize_token_embeddingsÚbase_model_prefixr   Ú_tie_encoder_decoder_weightsÚ_modules)
Úselfr   r   r   Úencoder_name_or_pathÚdecoder_configÚkwargs_decoderÚeÚdecoder_base_model_prefixÚ	__class__s
            €ú}/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/sentence_transformers/losses/DenoisingAutoEncoderLoss.pyr   z!DenoisingAutoEncoderLoss.__init__   sý  ø€ õ 	Õ&¨Ñ-Ô-×6Ò6Ñ8Ô8Ð8ØˆŒØ!&¤ˆÔà$ QœxÔ2Ô9ÔGÐØÐ'à#ðað aà`ñaô að að ð 	8Ø#ð kÝ—’ÐiÑjÔjÐjØ#7Ð å!.Ô!>Ð?SÑ!TÔ!TˆÔÝ'1°$Ô2HÍ$ÈtÔOeÑJfÔJfÑ'gÔ'gÐ#gˆÔ å#Ô3Ð4HÑIÔIˆØ$(ˆÔ!Ø-1ˆÔ*Ø" NÐ3ˆð	Ý/Ô?Ð@TÐgÐgÐXfÐgÐgˆDŒLˆLøÝð 	ð 	ð 	Ý�LŠLð aÐ';ð  að  að  añô ð ð ˆGøøøøð		øøøð
 �QŒxÔ"Ô)Ô5¸Ô9SÒSÐSÐSÐUqÑSÔSÐSØÔ!Ô+Ð3à/3Ô/EÔ/OˆDÔ"Ô,Ø/3¬|Ô/BÔ/OˆDŒLÔÔ,å�}Ô,Ð-AÑBÔBÑCÔCÅsÈ4ÔKaÑGbÔGbÒbÐbÝ�NŠNð Añô ð ð ð 	ØÔ/ÐsÐsÐ1sÑsÔsÐsÝ�4Ô)Ñ*Ô*­c°$Ô2HÑ.IÔ.IÒIÐIØ)-Ô)?�Ô&Ø”×4Ò4µS¸Ô9OÑ5PÔ5PÑQÔQÐQÝ—’ð _ñô ð ð )-¬Ô(FÐ%ÝÔ8Ø�a”Ô# T¤\Ô%:Ð;TÔ%UÐW[ÔWcÔWuñô ð ð ð ð	ð 	s   Ã.D Ä
D5Ä D0Ä0D5c                 ó¸   — |d         }|j         }| j                             |dd¬¦  «        }|                      |dddd ¬¦  «                             |¦  «        }|S )NÚ	input_idsT)Úskip_special_tokensÚclean_up_tokenization_spacesÚlongest_firstÚpt)ÚpaddingÚ
truncationÚreturn_tensorsÚ
max_length)Údevicer   Úbatch_decoder   Úto)r0   Úsentence_featuresr9   rB   Úsentences_decodedÚretokenizeds         r7   Ú
retokenizez#DenoisingAutoEncoderLoss.retokenizeQ   sy   € Ø% kÔ2ˆ	ØÔ!ˆØ Ô2×?Ò?Ø¨4Èdð @ñ 
ô 
Ðð ×,Ò,Ø t¸ÐX\Ðimð -ñ 
ô 
ç
Š"ˆV‰*Œ*ð 	ð Ðó    rE   Úlabelsc           
      ót  — t          |¦  «        \  }}| j        r|                      |¦  «        }|                      |¦  «        d         }|d         j        d         }|d                              ¦   «         d d …d |dz
  …f         }|d         d d …dd …f         }|                      |d d |d d …d f         |d         d d …dd…f         d d d¬¦  «        }	|	d         }
t          j        | j	        j
        ¬¦  «        } ||
                     d	|
j        d	         ¦  «        |                     d	¦  «        ¦  «        }|S )
NÚsentence_embeddingr9   é   Úattention_maskr   F)r9   Úinputs_embedsrN   Úencoder_hidden_statesÚencoder_attention_maskrJ   Úreturn_dictÚ	use_cache)Úignore_indexéÿÿÿÿ)Útupler    rH   r   ÚshapeÚcloner#   r   ÚCrossEntropyLossr   r*   ÚviewÚreshape)r0   rE   rJ   Úsource_featuresÚtarget_featuresÚrepsÚtarget_lengthÚdecoder_input_idsÚ	label_idsÚdecoder_outputsÚ	lm_logitsÚce_loss_fctÚlosss                r7   Úforwardz DenoisingAutoEncoderLoss.forward\   s^  € Ý+0Ð1BÑ+CÔ+CÑ(ˆ˜ØÔ#ð 	?ð #Ÿošo¨oÑ>Ô>ˆOØ�|Š|˜OÑ,Ô,Ð-AÔBˆð (¨Ô4Ô:¸1Ô=ˆØ+¨KÔ8×>Ò>Ñ@Ô@ÀÀÀÐDWÀmÐVWÑFWÐDWÐAWÔXÐØ# KÔ0°°°°A°B°B°Ô7ˆ	ð Ÿ,š,Ø'ØØØ"& q q q¨$ w¤-Ø#2Ð3CÔ#DÀQÀQÀQÈÈ!ÈÀVÔ#LØØØð 'ñ 	
ô 	
ˆð $ AÔ&ˆ	ÝÔ)°tÔ7MÔ7ZÐ[Ñ[Ô[ˆØˆ{˜9Ÿ>š>¨"¨i¬o¸bÔ.AÑBÔBÀI×DUÒDUÐVXÑDYÔDYÑZÔZˆØˆrI   )NT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚstrÚboolr   rH   r   r   r   rf   Ú__classcell__)r6   s   @r7   r   r   
   s£   ø€ € € € € ð
ð 
ð8ð 8Ð1ð 8Èð 8Ðjnð 8ð 8ð 8ð 8ð 8ð 8ðt	ð 	ð 	ð¨°$°s¸F°{Ô2CÔ)Dð Èfð ð ð ð ð ð ð ð rI   r   )Útorchr   r   Útypingr   r   Úsentence_transformersr   Útransformersr   r	   r
   r   ÚloggingÚ	getLoggerrg   r   ÚModuler   © rI   r7   ú<module>rv      sÆ   ðØ Ð Ð Ð Ð Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !Ð !Ð !Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YØ €€€à	ˆÔ	˜8Ñ	$Ô	$€ðoð oð oð oð o˜rœyñ oô oð oð oð orI   