§
    ‚ŠtjâL ã            
       óÜ  — d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	Z
d dlZd dlmZ ddlmZ ddlmZmZ ddlmZ dd	lmZmZmZ  e¦   «         rd d
lmZ erddlmZ ddlmZ ddlmZ  G d„ d¦  «        Z G d„ de¦  «        Z  G d„ de ¦  «        Z! G d„ dej"        ¦  «        Z# G d„ dej"        ¦  «        Z$ G d„ d¦  «        Z% G d„ d¦  «        Z& G d„ de!¦  «        Z' G d„ de¦  «        Z( G d„ d e ¦  «        Z) G d!„ d"e ¦  «        Z* G d#„ d$e ¦  «        Z+d%e,e-ef         d&e.d'e/d(e,e-ef         fd)„Z0d%e,e-ef         d&e.d'e/d(e,e-ef         fd*„Z1d%e,e-ef         d&e.d(e,e-ef         fd+„Z2dS ),é    N)ÚIterable)ÚTYPE_CHECKINGÚAnyÚOptionalÚcasté   )Úprune_linear_layer)ÚModelOutputÚis_sklearn_availableé   )ÚGenerationConfig)ÚLogitsProcessorListÚMinLengthLogitsProcessorÚSuppressTokensLogitsProcessor)Ú	roc_curve)ÚPreTrainedModel)ÚPreTrainedTokenizerBasec                   óˆ   — e Zd ZU dZdZeed<   dej        de	ej        ej
        f         fd„Zdej        dej
        defd	„Zd
S )ÚCandidateGeneratorz`Abstract base class for all candidate generators that can be applied during assisted generation.FÚrequires_model_outputsÚ	input_idsÚreturnc                 ó0   — t          | j        › d�¦  «        ‚)ag  
        Fetches the candidates to be tried for the current input.

        Args:
            input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
                Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)

        Return:
            `torch.LongTensor` of shape `(batch_size, candidate_length)` containing the candidate sequences to be
            assessed by the model and, optionally, a `torch.FloatTensor` of shape `(batch_size, candidate_length,
            vocabulary_size)` containing the logits associated to each candidate.
        zT is an abstract class. Only classes inheriting this class can call `get_candidates`.©ÚNotImplementedErrorÚ	__class__)Úselfr   Úkwargss      úi/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/generation/candidate_generator.pyÚget_candidatesz!CandidateGenerator.get_candidates,   s$   € õ "ØŒ~ÐsÐsÐsñ
ô 
ð 	
ó    ÚscoresÚnum_matchesc                 ó0   — t          | j        › d�¦  «        ‚)áâ  
        Updates the candidate generation strategy based on the outcomes.

        Args:
            input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
                Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
            scores (`torch.FloatTensor` of shape `(batch_size, candidate_length, config.vocab_size)`):
                Prediction scores of a language modeling head. These can be logits for each vocabulary when not using
                beam search or log softmax for each vocabulary token when using beam search
            num_matches (`int`):
                The number of matches between the candidate sequences and the model predictions.
        z_ is an abstract class. Only classes inheriting this class can call `update_candidate_strategy`.r   ©r   r   r"   r#   s       r   Úupdate_candidate_strategyz,CandidateGenerator.update_candidate_strategy=   s*   € õ "ØŒ~ð +ð +ð +ñ
ô 
ð 	
r!   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__ÚtorchÚ
LongTensorÚtupleÚFloatTensorr    Úintr'   © r!   r   r   r   '   s“   € € € € € € ØjÐjà#(Ð˜DÐ(Ð(Ñ(ð
¨Ô(8ð 
ÀuÈUÔM]Ð_dÔ_pÐMpÔGqð 
ð 
ð 
ð 
ð"
°5Ô3Cð 
ÈUÔM^ð 
Ðmpð 
ð 
ð 
ð 
ð 
ð 
r!   r   c                   óp  — e Zd ZdZ	 	 ddej        ddddded	ej        dz  d
ed         fd„Z	dej        de
ej        ej        f         fd„Zdej        dej        defd„Zdej        de
eef         fd„Z	 ddej        dededefd„Zdej        dededefd„Zdede
ej        ej        dz  f         fd„ZdS )ÚAssistedCandidateGeneratorar  
    `CandidateGenerator` class to be used for assisted generation and speculative decoding. This class generates
    candidates through the use of a smaller model. Read the following blog post for more information:
    https://huggingface.co/blog/assisted-generation

    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
        assistant_model (`PreTrainedModel`):
            The model to be used for generating candidates. This model should be smaller than the main model.
        generation_config (`~generation.GenerationConfig`, *optional*):
            The generation configuration to be used as base parametrization for the generation call.
        logits_processor (`LogitsProcessorList`):
            An instance of [`LogitsProcessorList`]. List of instances of class derived from [`LogitsProcessor`]
            used to modify the prediction scores of the language modeling head applied at each generation step.
        model_kwargs (`Dict`):
            The keyword arguments that will be passed to the main model, and are used as base inputs for the assistant
            model as well.
        inputs_tensor (`torch.Tensor`, *optional*):
            The model input tensor. In encoder-decoder models, this is the encoder input.
    Nr   Úassistant_modelr   Úgeneration_configr   Úmodel_kwargsÚinputs_tensorÚlogits_processorr   c                 ó‚  — |j         }|                     |¦  «        }|�|                     |¦  «        }|| _        t          j        |j        ¦  «        | _        | j                             ¦   «         } | j        j        di |¤ddi¤Ž | j        j	        | _	        | j        j
        | _
        |j        | j        _        i }	|                     ¦   «         D ]a\  }
}|
dvrXt          |t          j        ¦  «        r'|                     ¦   «                              |¦  «        nt          j        |¦  «        |	|
<   Œbd|	v r|                     ¦   «         s|	d= |j        j        rC|                     || j        j        |	¦  «        \  }}}	|                     ||	|| j        ¦  «        }	nd|v r|d         |	d<   |	| _        |j        j        rd| _        ngd|	v r\d| _        | j                             dt          j        |j        d	         d
f|j         t          j        ¬¦  «        ¦  «        | j        d<   nd| _        |�|nt;          ¦   «         | _        t          j        |¦  «        | _        d| j        _        d| j        _         | j
        | j        _
        d| j        _!        | j        j"        | _#        d | j        _"        d | j        _$        | j        j%        | _&        d | j        _%        d„ | j        D ¦   «         | _        d| j        _'        tQ          ¦   «         r2| j        j
        r(tS          | ¦  «        tT          u rg | _+        g | _,        d S d S d S d S )NÚdefaults_onlyT)Úencoder_outputsÚpast_key_valuesÚlogits_to_keepr=   Údecoder_input_idsr   Údecoder_attention_maskr   r   ©ÚdeviceÚdtypeÚattention_maskc                 ó<   — g | ]}t          |t          ¦  «        °|‘ŒS r3   )Ú
isinstancer   )Ú.0Ú	processors     r   ú
<listcomp>z7AssistedCandidateGenerator.__init__.<locals>.<listcomp>»   s8   € ð !
ð !
ð !
Ø#ÅJÈyÕZrÑDsÔDsð!
Øð!
ð !
ð !
r!   Údynamic_fullr3   )-rC   Útor6   ÚcopyÚdeepcopyr7   Úassistant_generation_configÚ_get_default_generation_paramsÚupdateÚnum_assistant_tokensÚassistant_confidence_thresholdÚeos_token_idÚitemsrG   r.   ÚTensorÚdetachÚ_supports_logits_to_keepÚconfigÚis_encoder_decoderÚ_prepare_model_inputsÚbos_token_idÚ._prepare_encoder_decoder_kwargs_for_generationÚassistant_kwargsÚinput_ids_keyÚgetÚonesÚshapeÚlongr   r:   Úreturn_dict_in_generateÚoutput_scoresÚis_assistantÚ
min_lengthÚmain_model_min_lengthÚmin_new_tokensÚ
max_lengthÚmain_model_max_lengthÚcache_implementationr   Útyper5   ÚprobsÚmatches)r   r   r6   r7   r8   r9   r:   rC   Úglobal_defaultsr^   ÚkeyÚvalueÚmodel_input_names                r   Ú__init__z#AssistedCandidateGenerator.__init__g   s¨  € ð !Ô'ˆØ—L’L Ñ(Ô(ˆ	ØÐ$Ø)×,Ò,¨VÑ4Ô4ˆMð  /ˆÔõ ,0¬=¸Ô9ZÑ+[Ô+[ˆÔ(ØÔ:×YÒYÑ[Ô[ˆØ/ˆÔ(Ô/ÐVÐV°/ÐVÐVÐQUÐVÐVÐVÐVØ$(Ô$DÔ$YˆÔ!Ø.2Ô.NÔ.mˆÔ+ð 9JÔ8VˆÔ(Ô5ð ÐØ&×,Ò,Ñ.Ô.ð 	ð 	‰JˆC�ØÐ@Ð@Ð@å1;¸EÅ5Ä<Ñ1PÔ1PÐj�E—L’L‘N”N×%Ò% fÑ-Ô-Ð-ÕVZÔVcÐdiÑVjÔVjð ! Ñ%øð
 Ð/Ð/Ð/¸×8`Ò8`Ñ8bÔ8bÐ/Ø Ð!1Ð2ð Ô!Ô4ð 	RØ@O×@eÒ@eØ˜tÔ?ÔLÐN^ñAô AÑ=ˆMÐ+Ð-=ð  /×]Ò]ØÐ/Ð1AÀ4ÔCcñ ô  ÐÐð  ,Ð.Ð.Ø2>Ð?PÔ2QÐÐ.Ñ/Ø 0ˆÔð Ô!Ô4ð 	-à!4ˆDÔÐØÐ"2Ð2Ð2à!,ˆDÔØ6:Ô6K×6OÒ6OØ(Ý”
˜IœO¨AÔ.°Ð2¸9Ô;KÕSXÔS]Ð^Ñ^Ô^ñ7ô 7ˆDÔ!Ð"2Ñ3Ð3ð "-ˆDÔð 5EÐ4PÐ 0Ð 0ÕViÑVkÔVkˆÔÝ!%¤Ð/@Ñ!AÔ!AˆÔà9=ˆÔÔ6Ø/3ˆÔÔ,Ø@DÔ@cˆÔÔ=à.2ˆÔÔ+ð &*Ô%;Ô%FˆÔ"Ø,0ˆÔÔ)Ø04ˆÔÔ-Ø%)Ô%;Ô%FˆÔ"Ø,0ˆÔÔ)ð!
ð !
Ø'+Ô'<ð!
ñ !
ô !
ˆÔð
 7EˆÔÔ3õ !Ñ"Ô"ð	àÔ0ÔOð	õ �T‘
”
Õ8Ð8Ð8àˆDŒJØˆDŒLˆLˆLð	ð 	ð 	ð 	ð 9Ð8r!   r   c                 ó  — |                      | j        j        ¦  «        }|                      |¦  «        \  }}|dk    r|dfS |                      |¦  «         |                      |||¦  «        }|                      |¦  «        \  }}||fS )áZ  
        Fetches the candidates to be tried for the current input.

        Args:
            input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
                Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)

        Return:
            `torch.LongTensor` of shape `(batch_size, candidate_length)` containing the candidate sequences to be
            assessed by the model and a `torch.FloatTensor` of shape `(batch_size, candidate_length,
            vocabulary_size)` containing the logits associated to each candidate.
        r   N)rL   r6   rC   Ú_calculate_new_tokensÚ_update_past_and_masksÚ_prepare_generation_argsÚ_generate_candidates)r   r   r   ri   Úmax_new_tokensÚgeneration_argsÚcandidate_idsÚcandidate_logitss           r   r    z)AssistedCandidateGenerator.get_candidatesÊ   s™   € ð —L’L Ô!5Ô!<Ñ=Ô=ˆ	à)-×)CÒ)CÀIÑ)NÔ)NÑ&ˆ˜Ø˜QÒÐØ˜d�?Ð"à×#Ò# IÑ.Ô.Ð.à×7Ò7¸	À>ÐSaÑbÔbˆØ*.×*CÒ*CÀOÑ*TÔ*TÑ'ˆÐ'ØÐ.Ð.Ð.r!   r"   r#   c                 óv  — | j         j        dv rJ|t          |d         ¦  «        dz
  k    r| xj        dz  c_        nt	          d| j        dz
  ¦  «        | _        t          ¦   «         �rI| j         j        �r>t          | ¦  «        t          u �r)| j	         
                    dg|z  ¦  «         t          | j        ¦  «        t          | j	        ¦  «        k    r| j	                             d¦  «         t          | j        ¦  «        t          | j	        ¦  «        z
  }|dk    r| j        | d…= t          | j        ¦  «        dk    rwddh                     | j	        ¦  «        r]t          | j	        | j        ¦  «        \  }}}d|z
  }|d|z  z   }	t          j        |	¦  «        }
||
         }|| j         _        dS dS dS dS dS dS )r%   >   Ú	heuristicÚheuristic_transientr   r   r   Né   é   )rO   Únum_assistant_tokens_scheduleÚlenrR   Úmaxr   rS   rm   r5   ro   Úextendrn   ÚappendÚissubsetr   ÚnpÚargmin)r   r   r"   r#   Úexcess_lengthÚfprÚtprÚ
thresholdsÚfnrÚcostsÚoptimal_threshold_indexÚbest_thresholds               r   r'   z4AssistedCandidateGenerator.update_candidate_strategyã   sã  € ð  Ô+ÔIð N
ð 
ð 
ð
 �c &¨¤)™nœn¨qÑ0Ò0Ð0ØÐ)Ô)¨QÑ.Ð)Ô)Ð)å,/°°4Ô3LÈqÑ3PÑ,QÔ,Q�Ô)õ
 !Ñ"Ô"ñ	aàÔ0ÔOñ	aõ �T‘
”
Õ8Ð8Ñ8ð ŒL×Ò   kÑ 1Ñ2Ô2Ð2Ý�4”:‰Œ¥ T¤\Ñ!2Ô!2Ò2Ð2Ø”×#Ò# AÑ&Ô&Ð&õ   ¤
™OœO­c°$´,Ñ.?Ô.?Ñ?ˆMØ˜qÒ Ð Ø”J ˜~˜˜Ð/õ �D”J‘” !Ò#Ð#¨¨A¨¯ª¸¼Ñ(EÔ(EÐ#å'0°´¸t¼zÑ'JÔ'JÑ$��S˜*Ø˜#‘g�ð ˜a #™g™�õ +-¬)°EÑ*:Ô*:Ð'Ø!+Ð,CÔ!D�àR`�Ô0ÔOÐOÐOð9	að 	að 	að 	að 9Ð8ð $Ð#Ð#Ð#r!   c                 óÊ   — |j         d         }t          t          | j        ¦  «        | j        |z
  dz
  ¦  «        }t          t          || j        |z
  ¦  «        d¦  «        }||fS )zCCalculate the minimum and maximum number of new tokens to generate.éÿÿÿÿr   r   )rb   Úminr2   rR   rk   r†   rh   )r   r   Únew_cur_lenr{   ri   s        r   rw   z0AssistedCandidateGenerator._calculate_new_tokens  sd   € à”o bÔ)ˆÝ�S Ô!:Ñ;Ô;¸TÔ=WÐZeÑ=eÐhiÑ=iÑjÔjˆÝ�S °Ô1KÈkÑ1YÑZÔZÐ\]Ñ^Ô^ˆØ˜~Ð-Ð-r!   r   r   Úremove_from_pkvÚnum_added_tokensc                 óæ  — | j                              dd¦  «        du}|rÑ|j        d         dz
  |z
  }| j         d                              ||z
  ¦  «         t	          | j         |j        d         | j        j        j        ¦  «        | _         t          | j         |j        d         | j        j        j        ¦  «        | _         t          | j         |j        d         ¦  «        | _         d| j
        _        |S )zLUpdate past key values and attention masks for subsequent generation rounds.r>   Nr•   r   )r^   r`   rb   ÚcropÚ_prepare_attention_maskr6   rY   rZ   Ú_prepare_position_idsÚ_prepare_token_type_idsr7   rl   )r   r   r˜   r™   Úhas_past_key_valuesÚnew_cache_sizes         r   rx   z1AssistedCandidateGenerator._update_past_and_masks$  só   € ð #Ô3×7Ò7Ð8IÈ4ÑPÔPÐX\Ð\ÐØð 	?Ø&œ_¨RÔ0°1Ñ4°ÑFˆNØÔ!Ð"3Ô4×9Ò9¸.ÐK[Ñ:[Ñ\Ô\Ð\Ý$;ØÔ% y¤°rÔ':¸DÔ<PÔ<WÔ<jñ%ô %ˆDÔ!õ %:ØÔ% y¤°rÔ':¸DÔ<PÔ<WÔ<jñ%ô %ˆDÔ!õ %<¸DÔ<QÐS\ÔSbÐceÔSfÑ$gÔ$gˆDÔ!ð ;?ˆDÔ"Ô7à"Ð"r!   ri   r{   c           
      ó8   — | j         |d|d|d| j        d| j        iS )z*Prepare arguments for the generation call.ri   r{   r7   r:   )r_   r7   r:   )r   r   ri   r{   s       r   ry   z3AssistedCandidateGenerator._prepare_generation_args:  s0   € ð Ô 	Ø˜nØ˜nØ Ô!7Ø Ô 5ð
ð 	
r!   r|   c                 óH  —  | j         j        di |¤| j        ¤Ž}|j        | j        d<   t	          ¦   «         rÇ| j        j        r»t          | ¦  «        t          u r¥t          j
        |j        d¬¦  «        }t          j        |d¬¦  «        }|j        dt          |j        ¦  «         d…f         }|t          t          |¦  «        ¦  «        |f         }| j                             |                     ¦   «         ¦  «         t          j        |j        d¬¦  «        }|j        }||fS )z7Generate candidate sequences using the assistant model.r>   r   ©Údimr•   Nr   r3   )r6   Úgenerater^   r>   r   rO   rS   rm   r5   r.   Úcatr"   ÚsoftmaxÚ	sequencesr…   Úrangern   r‡   ÚtolistÚstack)	r   r|   Úassistant_outputÚscores_tensorÚscores_softmaxÚidsÚpr~   r}   s	            r   rz   z/AssistedCandidateGenerator._generate_candidatesD  s   € à8˜4Ô/Ô8ÐdÐd¸?ÐdÈdÔNcÐdÐdÐØ3CÔ3SˆÔÐ/Ñ0å Ñ"Ô"ð		*àÔ0ÔOð		*õ �T‘
”
Õ8Ð8Ð8å!œIÐ&6Ô&=À1ÐEÑEÔEˆMÝ"œ]¨=¸bÐAÑAÔAˆNØ"Ô,¨Rµ#Ð6FÔ6MÑ2NÔ2NÐ1NÐ1PÐ1PÐ-PÔQˆCØ�u¥S¨¡X¤X™œ°Ð3Ô4ˆAØŒJ×Ò˜aŸhšh™jœjÑ)Ô)Ð)Ý œ;Ð'7Ô'>ÀAÐFÑFÔFÐØ(Ô2ˆØÐ.Ð.Ð.r!   ©NN)r   r   )r(   r)   r*   r+   r.   r/   ÚdictrV   r   rt   r0   r1   r    r2   r'   rw   r,   rx   ry   rz   r3   r!   r   r5   r5   P   sâ  € € € € € ðð ð8 .2Ø<@ðað aàÔ#ðað +ðað .ð	að
 ðað ”| dÑ*ðað #Ð#8Ô9ðað að að aðF/¨Ô(8ð /ÀuÈUÔM]Ð_dÔ_pÐMpÔGqð /ð /ð /ð /ð28a°5Ô3Cð 8aÈUÔM^ð 8aÐmpð 8að 8að 8að 8aðt.¨uÔ/?ð .ÀEÈ#ÈsÈ(ÄOð .ð .ð .ð .ð ^_ð#ð #ØÔ)ð#Ø<?ð#ØWZð#à	ð#ð #ð #ð #ð,
°%Ô2Bð 
ÐTWð 
Ðilð 
Ðquð 
ð 
ð 
ð 
ð/°Dð /¸UÀ5ÔCSÐUZÔUfÐimÑUmÐCmÔ=nð /ð /ð /ð /ð /ð /r!   r5   c                   ó\  ‡ — e Zd ZdZ	 	 ddej        ddddddd	d
dedej        dz  ded         fˆ fd„Z	e
d„ ¦   «         Ze
d„ ¦   «         Ze
d„ ¦   «         Zd„ Zdej        deej        ej        f         fd„Zdej        deej        ef         fd„Zdej        dej        dej        fd„Zˆ xZS )Ú-AssistedCandidateGeneratorDifferentTokenizersaƒ  
    `CandidateGenerator` class to be used for Universal Assisted Generation (UAD): assisted generation with different tokenizers
    for the assistant and main models. This class generates candidates through the use of a smaller
    model.

    The main model input tokens are re-encoded into assistant model tokens, then candidate tokens are generated in the assistant encoding, which are
    in turn re-encoded into main model candidate tokens. Validation then proceeds as explained above.
    The re-encoding steps involve decoding token ids into text and then encoding the text using a different tokenizer.
    Since re-encoding the tokens may result in tokenization discrepancies, UAD finds the longest common subsequence between the source and target encodings,
    to ensure the new tokens include the correct prompt suffix.

    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
        assistant_model (`PreTrainedModel`):
            The model to be used for generating candidates. This model should be smaller than the main model.
        target_tokenizer (`PreTrainedTokenizerBase`):
            The tokenizer used for the target model.
        assistant_tokenizer (`PreTrainedTokenizerBase`):
            The tokenizer used for the assistant model.
        generation_config (`~generation.GenerationConfig`, *optional*):
            The generation configuration to be used as base parametrization for the generation call.
        logits_processor (`LogitsProcessorList`):
            An instance of [`LogitsProcessorList`]. List of instances of class derived from [`LogitsProcessor`]
            used to modify the prediction scores of the language modeling head applied at each generation step.
        model_kwargs (`Dict`):
            The keyword arguments that will be passed to the main model, and are used as base inputs for the assistant
            model as well.
        inputs_tensor (`torch.Tensor`, *optional*):
            The model input tensor. In encoder-decoder models, this is the encoder input.
    Nr   r6   r   Útarget_tokenizerr   Úassistant_tokenizerr7   r   r8   r9   r:   r   c	                 óÐ   •— t          ¦   «                              ||||||¦  «         || _        || _        d | _        d | _        | j        j        | _        | j        j        | _        d S ©N)	Úsuperrt   rµ   r¶   Úprev_target_ids_lenÚprev_assistant_idsrO   Útarget_lookbehindÚassistant_lookbehind)
r   r   r6   rµ   r¶   r7   r8   r9   r:   r   s
            €r   rt   z6AssistedCandidateGeneratorDifferentTokenizers.__init__x  sl   ø€ õ 	‰Œ×Ò˜ OÐ5FÈÐVcÐeuÑvÔvÐvà 0ˆÔØ#6ˆÔ Ø/3ˆÔ Ø;?ˆÔØ!%Ô!AÔ!SˆÔØ$(Ô$DÔ$YˆÔ!Ð!Ð!r!   c                 óZ  — t          ¦   «         }i }|D �]}t          j        |¦  «        }t          |                     ¦   «         ¦  «        }||v rŒ=|                     |¦  «         d}|dz  }|d         | j        d         k     r¡|d         | j        d         k     rŠt          |                     ¦   «         ¦  «        }|                     |¦  «         | |d         |d         f         dk    r|dz  }|dz  }nn.|d         | j        d         k     r|d         | j        d         k     °Š|||<   �Œ|S )aÆ  
        Calculates the length of the longest diagonal sequence in a given matrix.
        Args:
            input_matrix (torch.Tensor): The input matrix.
            nonzero_idx (torch.Tensor): The indices of the non-zero elements in the matrix.
        Returns:
            dict: A dictionary where the keys are the indices of the non-zero elements and the values are the lengths of the longest diagonal sequences starting from those indices.
        r   r   )Úsetr.   Úcloner0   rª   Úaddrb   )Úinput_matrixÚnonzero_idxÚvisitedÚdiagsÚidxÚ	start_idxÚtuple_start_idxÚcur_diag_lens           r   Ú_get_longest_diag_dictzDAssistedCandidateGeneratorDifferentTokenizers._get_longest_diag_dictŒ  sG  € õ ‘%”%ˆØˆØð 	&ñ 	&ˆCÝœ CÑ(Ô(ˆIÝ# I×$4Ò$4Ñ$6Ô$6Ñ7Ô7ˆOà 'Ð)Ð)Øà�KŠK˜Ñ(Ô(Ð(ØˆLØ˜‰NˆIØ˜A”, Ô!3°AÔ!6Ò6Ð6¸9ÀQ¼<È,ÔJ\Ð]^ÔJ_Ò;_Ð;_Ý"'¨	×(8Ò(8Ñ(:Ô(:Ñ";Ô";�Ø—’˜OÑ,Ô,Ð,à 	¨!¤¨i¸¬lÐ :Ô;¸qÒ@Ð@Ø  AÑ%�LØ ‘N�I�Iàð ˜A”, Ô!3°AÔ!6Ò6Ð6¸9ÀQ¼<È,ÔJ\Ð]^ÔJ_Ò;_Ð;_ð &ˆE�#‰J‰JØˆr!   c                 ó0  — t                                | |                      ¦   «         ¦  «        }t          |                     ¦   «         ¦  «        }t          |                     ¦   «         ¦  «        }t          j        |¦  «        }||         }||         }||fS )a  
        Returns the start index and length of the longest diagonal in the given input.
        Args:
            input_matrix (numpy.ndarray): The input matrix.
        Returns:
            tuple: A tuple containing the start index and length of the longest diagonal.
        )r´   rÊ   ÚnonzeroÚlistÚvaluesÚkeysrŠ   Úargmax)rÂ   rÅ   Údiags_valuesÚ
diags_keysÚ	best_diagÚdiag_start_indexÚdiag_start_lengths          r   Ú_get_longest_diag_indexzEAssistedCandidateGeneratorDifferentTokenizers._get_longest_diag_index°  s‡   € õ >×TÒTØ˜,×.Ò.Ñ0Ô0ñ
ô 
ˆõ ˜EŸLšL™NœNÑ+Ô+ˆÝ˜%Ÿ*š*™,œ,Ñ'Ô'ˆ
Ý”I˜lÑ+Ô+ˆ	Ø% iÔ0ÐØ(¨Ô3ÐØÐ!2Ð2Ð2r!   c                 óä  — | |j         k    }t          j        |¦  «        st          j        |¦  «        }|                     t
          ¦  «        }|                     ¦   «                              ¦   «         sdS t           	                    |¦  «        \  }}|d         |z   }|d         |z   }| j
        d         |z
                       ¦   «         }|dd…||z   d…f         }	|dd…|||z   …f         }
||	|
fS )a¹  
        Input:
            prompt: 2D array of shape (batch_size, prompt_length), represents the original prompt tokens
            prompt_plus_new_tokens: 2D array of shape (batch_size, prompt_length), represents the suffix of the original prompt, with additional new tokens.
        Output:
            discrepancy_length: int, represents the number of tokens that need to be replaced from prompt
            new_tokens_only: 2D array of shape (batch_size, new_token_length), represents the new tokens that are not in prompt
            discrepancy_only: 2D array of shape (batch_size, discrepancy_length), represents the new tokens that are in prompt but not in prompt_plus_new_tokens
        ©NNNr   r   N)ÚTr.   Ú	is_tensorÚtensorrL   r2   ÚanyÚitemr´   rÖ   rb   )ÚpromptÚprompt_plus_new_tokensÚcompare_matÚcompare_mat_intÚlongest_locationÚlongest_diag_lengthÚnew_token_start_indexÚdiscrepancy_with_oldÚdiscrepancy_lengthÚnew_tokens_onlyÚdiscrepancy_onlys              r   Ú_get_tokens_diagz>AssistedCandidateGeneratorDifferentTokenizers._get_tokens_diagÄ  s  € ð Ð 6Ô 8Ò8ˆÝŒ˜{Ñ+Ô+ð 	4Ýœ, {Ñ3Ô3ˆKà%Ÿ.š.­Ñ-Ô-ˆà×"Ò"Ñ$Ô$×)Ò)Ñ+Ô+ð 	$à#Ð#å0]×0uÒ0uØñ1
ô 1
Ñ-ÐÐ-ð !1°Ô 3Ð6IÑ IÐØ/°Ô2Ð5HÑHÐØ$œl¨1œoÐ0DÑD×JÒJÑLÔLÐØ0°°°Ð4IÐL^Ñ4^Ð4`Ð4`Ð1`ÔaˆØ1ØˆAˆAÐ$Ð'<Ð?QÑ'QÐQÐQô
Ðð " ?Ð4DÐDÐDr!   c                 óŽ   — |                      |dd¬¦  «        } ||dd¬¦  «        d         }|                     |j        ¦  «        S )a$  
        Convert token IDs from one tokenizer to another.
        Args:
            input_ids: The input token IDs.
            source_tokenizer: The source tokenizer.
            destination_tokenizer: The destination tokenizer.
        Returns:
            The converted token IDs.
        T©Úskip_special_tokensÚclean_up_tokenization_spacesÚpt©Úadd_special_tokensÚreturn_tensorsr   )ÚdecoderL   rC   )r   r   Úsource_tokenizerÚdestination_tokenizerÚtextÚdest_idss         r   Ú&convert_source_tokens_to_target_tokenszTAssistedCandidateGeneratorDifferentTokenizers.convert_source_tokens_to_target_tokenså  sR   € ð  ×&Ò& yÀdÐimÐ&ÑnÔnˆØ(Ð(¨À$ÐW[Ð\Ñ\Ô\Ð]hÔiˆØ�{Š{˜9Ô+Ñ,Ô,Ð,r!   r   c                 óä  — t          | j        ¦  «        }|dk    r|dfS |                     | j        j        ¦  «        }d}|                      |¦  «        \  }}|| _        t          t          || j	        |j
        d         z
  ¦  «        d¦  «        }|                      ||¦  «         |                      |||¦  «        }| j                             dd¦  «         | j                             dd¦  «          | j        j        di |¤| j        ¤Ž}|                      ||j        ¦  «        }	|j
        d         | _        |j        | j        d<   |j        | _        | j        |	j
        d         k    r|dfS |	dfS )	rv   r   Nr•   rE   Úposition_idsr   r>   r3   )r2   rR   rL   r6   rC   Ú_prepare_assistant_input_idsr»   r†   r–   rh   rb   rx   ry   r^   Úpopr¥   Ú_process_assistant_outputsr¨   rº   r>   )
r   r   r   r{   r˜   Úassistant_input_idsri   r|   r¬   Únew_target_idss
             r   r    z<AssistedCandidateGeneratorDifferentTokenizers.get_candidatesø  s‹  € õ ˜TÔ6Ñ7Ô7ˆØ˜QÒÐØ˜d�?Ð"à—L’L Ô!5Ô!<Ñ=Ô=ˆ	Øˆà/3×/PÒ/PÐQZÑ/[Ô/[Ñ,Ð˜_Ø"5ˆÔå�S °Ô1KÐNaÔNgÐhjÔNkÑ1kÑlÔlÐnoÑpÔpˆà×#Ò#Ð$7¸ÑIÔIÐIØ×7Ò7Ð8KÈ^Ð]kÑlÔlˆØÔ×!Ò!Ð"2°DÑ9Ô9Ð9ØÔ×!Ò! .°$Ñ7Ô7Ð7à8˜4Ô/Ô8ÐdÐd¸?ÐdÈdÔNcÐdÐdÐØ×8Ò8¸ÐDTÔD^Ñ_Ô_ˆð $-¤?°1Ô#5ˆÔ Ø3CÔ3SˆÔÐ/Ñ0Ø"2Ô"<ˆÔàÔ# ~Ô';¸AÔ'>Ò>Ð>Ø˜d�?Ð"à˜tÐ#Ð#r!   c                 óú  — | j         | j        dœ}d}| j        ��?| j        | j        k    �r.| j        | j        z
  } | j        |dd…|d…f         fi |¤Ž}|j        d         }| j        dd…| d…f         }|                      ||¦  «        \  }}	}
| j        }|	�¨|dk    rx|
j        d         dk    rg||
j        d         k    r|
|dd…| d…f<   nG||
j        d         k    r6||
j        d         z
  }|dd…d| …f         }|
|dd…|
j        d          d…f<   |}|	j        d         dk    rt          j	        ||	gd¬¦  «        }n9t          j	        ||gd¬¦  «        }n  | j        |fi |¤Ž}|j        d         | _        ||fS )zIConverts target input IDs to assistant input IDs, handling discrepancies.©ró   rô   r   Nr   r•   r£   )
rµ   r¶   r»   rº   r¼   r÷   rb   ré   r.   r¦   )r   r   Úconvert_kwargsr˜   Ústart_index_in_target_windowÚnew_assistant_idsÚprompt_use_lengthÚ
prompt_useræ   rç   rè   rý   Údiscrepancy_length_diffs                r   rú   zJAssistedCandidateGeneratorDifferentTokenizers._prepare_assistant_input_ids#  s;  € ð !%Ô 5Ø%)Ô%=ð
ð 
ˆð ˆàÔ"Ñ.°4Ô3KÈdÔNdÒ3dÑ3dà+/Ô+CÀdÔF\Ñ+\Ð(à K Ô KØ˜!˜!˜!Ð9Ð:Ð:Ð:Ô;ð!ð !Ø?Mð!ð !Ðð !2Ô 7¸Ô :ÐØÔ0°°°Ð5FÐ4FÐ4GÐ4GÐ1GÔHˆJàDH×DYÒDYØÐ-ñEô EÑAÐ Ð1Að #'Ô"9ÐàÐ*Ø%¨Ò)Ð)Ð.>Ô.DÀQÔ.GÈ!Ò.KÐ.KØ)Ð-=Ô-CÀAÔ-FÒFÐFØGWÐ+¨A¨A¨AÐ0BÐ/BÐ/CÐ/CÐ,CÑDÐDà+Ð.>Ô.DÀQÔ.GÒGÐGØ2DÐGWÔG]Ð^_ÔG`Ñ2`Ð/Ø.AÀ!À!À!ÐE^ÐG^ÐF^ÐE^ÐB^Ô._Ð+ØO_Ð+¨A¨A¨AÐ0@Ô0FÀqÔ0IÐ/IÐ/KÐ/KÐ,KÑLà&8�Oà"Ô(¨Ô+¨aÒ/Ð/Ý*/¬)Ð5HÈ/Ð4ZÐ`bÐ*cÑ*cÔ*cÐ'øõ ',¤iÐ1DÐFWÐ0XÐ^`Ð&aÑ&aÔ&aÐ#Ð#à"M $Ô"MÈiÐ"jÐ"jÐ[iÐ"jÐ"jÐØ'0¤°qÔ'9ˆDÔ$à" OÐ3Ð3r!   Úassistant_sequencesc                 óú  — t          t          j        | j        ¦  «        }|j        d         }|| j        z
  }|                      |dd…|d…f         | j        | j        ¬¦  «        }|j        d         }|dd…| d…f         }|  	                    ||¦  «        \  }	}
}	|}|
�*|
j        d         dk    rt          j
        ||
gd¬¦  «        }nt          j
        ||gd¬¦  «        }| j        �|dd…d| j        …f         }|S )z7Processes assistant outputs to obtain target input IDs.r   Nr   r   r•   r£   )r   r.   r/   r»   rb   r½   r÷   r¶   rµ   ré   r¦   rk   )r   r   r  r»   Únum_prev_assistantÚstart_assistant_look_indexÚnew_target_ids_from_windowÚtarget_prompt_use_lengthÚtarget_prompt_useÚ_Útarget_new_tokens_onlyrþ   s               r   rü   zHAssistedCandidateGeneratorDifferentTokenizers._process_assistant_outputsQ  sE  € õ "¥%Ô"2°DÔ4KÑLÔLÐØ/Ô5°aÔ8ÐØ%7¸$Ô:SÑ%SÐ"à%)×%PÒ%PØ   Ð#=Ð#>Ð#>Ð >Ô?Ø!Ô5Ø"&Ô"7ð &Qñ &
ô &
Ð"ð
 $>Ô#CÀAÔ#FÐ à% a a aÐ*BÐ)BÐ)CÐ)CÐ&CÔDÐà'+×'<Ò'<Ð=NÐPjÑ'kÔ'kÑ$ˆÐ! 1à"ˆà!Ð-Ø%Ô+¨AÔ.°Ò2Ð2Ý!&¤¨NÐ<RÐ+SÐY[Ð!\Ñ!\Ô!\�øõ #œY¨Ð8RÐ'SÐY[Ð\Ñ\Ô\ˆNàÔ%Ð1Ø+¨A¨A¨AÐ/K°Ô1KÐ/KÐ,KÔLˆNàÐr!   r±   )r(   r)   r*   r+   r.   r/   r²   rV   r   rt   ÚstaticmethodrÊ   rÖ   ré   r÷   r0   r1   r    r2   rú   rü   Ú__classcell__©r   s   @r   r´   r´   W  sÈ  ø€ € € € € ðð ðP .2Ø<@ðZð ZàÔ#ðZð +ðZð 4ð	Zð
 7ðZð .ðZð ðZð ”| dÑ*ðZð #Ð#8Ô9ðZð Zð Zð Zð Zð Zð( ð!ð !ñ „\ð!ðF ð3ð 3ñ „\ð3ð& ðEð Eñ „\ðEð@-ð -ð -ð&)$¨Ô(8ð )$ÀuÈUÔM]Ð_dÔ_pÐMpÔGqð )$ð )$ð )$ð )$ðV,4°eÔ6Fð ,4È5ÐQVÔQaÐcfÐQfÔKgð ,4ð ,4ð ,4ð ,4ð\ØÔ)ðØ@EÔ@Pðà	Ô	ðð ð ð ð ð ð ð r!   r´   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )Ú_PruneReindexingLMHeadal  
    A class to prune and reindex the language model head.

    This class prunes the language model head to only include the specified token IDs and reindexes the logits
    to map back to the original vocabulary.

    Args:
        original_lm_head (nn.Module): The original language model head.
        token_ids (list[int]): The list of token IDs to keep.
    c                 ó¬   •— t          ¦   «                              ¦   «          t          ||¦  «                             |j        j        ¦  «        | _        d S r¸   )r¹   rt   r	   rL   ÚweightrD   Úpruned_lm_head)r   Úoriginal_lm_headÚassistant_overlap_token_idsr   s      €r   rt   z_PruneReindexingLMHead.__init__  sL   ø€ Ý‰Œ×ÒÑÔÐÝ0Ð1AÐC^Ñ_Ô_×bÒbØÔ#Ô)ñ
ô 
ˆÔÐÐr!   c                 ó0   — |                       |¦  «        }|S r¸   )r  )r   Úhidden_statesÚpruned_logitss      r   Úforwardz_PruneReindexingLMHead.forward…  s   € Ø×+Ò+¨MÑ:Ô:ˆØÐr!   )r(   r)   r*   r+   rt   r  r  r  s   @r   r  r  s  sQ   ø€ € € € € ð	ð 	ð
ð 
ð 
ð 
ð 
ðð ð ð ð ð ð r!   r  c                   óR   ‡ — e Zd Zdej        fˆ fd„Zdej        dej        fd„Z	ˆ xZ
S )Ú_MapInputEmbeddingÚoriginal_embeddingc                 óŠ   •— t          ¦   «                              ¦   «          || _        |j        | _        || _        d| _        dS )aT  
        Wraps an existing embedding layer and remaps token IDs before lookup.

        Args:
            original_embedding (nn.Embedding): Pre-trained or existing embedding layer.
            assistant_overlap_token_ids (dict): Mapping from original token IDs to new token IDs.
                          Example: {old_id: new_id}
        FN)r¹   rt   r   r  r  Úmap)r   r   r  r   s      €r   rt   z_MapInputEmbedding.__init__‹  s@   ø€ õ 	‰Œ×ÒÑÔÐØ"4ˆÔØ(Ô/ˆŒØ+FˆÔ(ØˆŒˆˆr!   r   r   c                 óÀ   — | j         r:| j        |d                                       d¦  «                             d¦  «        }n	d| _         |}|                      |¦  «        S )z½
        Args:
            input_ids (torch.LongTensor): Tensor of token IDs (batch_size, seq_len).

        Returns:
            torch.FloatTensor: Corresponding input embeddings.
        )r   r•   r   T)r"  r  Ú	unsqueezer   )r   r   Úmy_input_idss      r   r  z_MapInputEmbedding.forwardš  s`   € ð Œ8ð 	%àÔ;¸IÀeÔ<LÔM×WÒWÐXYÑZÔZ×dÒdÐefÑgÔgˆLˆLàˆDŒHØ$ˆLà×&Ò& |Ñ4Ô4Ð4r!   )r(   r)   r*   ÚnnÚ	Embeddingrt   r.   r/   r1   r  r  r  s   @r   r  r  Š  sn   ø€ € € € € ð¨2¬<ð ð ð ð ð ð ð5 Ô!1ð 5°eÔ6Gð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5r!   r  c                   óÞ   — e Zd ZU dZ ed¦  «         Zeed<   dZeed<   	 	 ddd	d
d	dede	d         de
f
d„Zd„ Zd„ Zdee         fd„Zdej        dej        fd„Zdej        dej        fd„ZdS )ÚAssistantToTargetTranslatoraô  
    Translates token ids and logits between assistant and target model vocabularies. This class is used to handle
    vocabulary mismatches when using different tokenizers for the assistant and target models in speculative decoding,
    as introduced in the paper "Lossless Speculative Decoding Algorithms for Heterogeneous Vocabularies"
    (https://huggingface.co/papers/2502.05202).
    It maintains mappings between the two vocabularies and handles token/logit conversion.

    Args:
        target_tokenizer (`PreTrainedTokenizerBase`):
            The tokenizer used by the target (main) model.
        assistant_tokenizer (`PreTrainedTokenizerBase`):
            The tokenizer used by the assistant model.
        target_vocab_size (`int`):
            The size of the target model's vocabulary. If not provided, will be inferred from the target tokenizer.
        assistant_model (Optional[PreTrainedModel], optional): The assistant model to be used. Defaults to None for backward compatibility.
        assistant_prune_lm_head (bool): Whether to prune the assistant model's language model
            head to match the target vocabulary. This is only applicable if `assistant_model` is provided.
            Defaults to False for backward compatibility.
    ÚInfÚFILTER_VALUEr•   ÚSUPPRESS_TOKEN_IDNFrµ   r   r¶   Útarget_vocab_sizer6   r   Úassistant_prune_lm_headc                 óP  — || _         || _        |�|j        nd| _        || _        |                      ¦   «         \  | _        | _        |                      ¦   «         | _	        d | _
        |o|d u| _        t          | j	        ¦  «        dk    �r| j        rØ|�Öt          j        t          | j                             ¦   «         ¦  «        t          j        | j        ¬¦  «        | _        |                     ¦   «         }t)          || j        ¦  «        }~|                     |¦  «         |                     ¦   «         }t/          || j        ¦  «        }	~|                     |	¦  «         |	| _        d S t5          t7          |                      ¦   «         | j        ¦  «        g¦  «        | _
        d S d S )NÚcpur   ©rD   rC   )Ú_target_tokenizerÚ_assistant_tokenizerrC   Ú_assistant_model_devicer-  Ú"_get_assistant_to_target_input_idsÚ_assistant_to_target_input_idsÚtarget_to_assistant_input_idsÚ_get_suppress_input_idsÚ_suppress_input_idsÚlogits_processorsr.  r…   r.   rÛ   rÍ   rÎ   rc   r  Úget_output_embeddingsr  Úset_output_embeddingsÚget_input_embeddingsr  Úset_input_embeddingsÚmap_input_embeddingsr   r   )
r   rµ   r¶   r-  r6   r.  r  r  Úoriginal_input_embeddingsr?  s
             r   rt   z$AssistantToTargetTranslator.__init__Ä  s²  € ð ;KˆÔØ=PˆÔ!ØAPÐA\ Ô'=Ð'=ÐbgˆÔ$Ø&7ˆÔà×3Ò3Ñ5Ô5ñ 	PˆÔ+¨TÔ-Oð /3×.JÒ.JÑ.LÔ.LˆÔ Ø=AˆÔØ'>Ð'^À?ÐZ^ÐC^ˆÔ$ÝˆtÔ'Ñ(Ô(¨1Ò,Ñ,àÔ+ð °Ð0KÝ38´<Ý˜Ô;×BÒBÑDÔDÑEÔEÝœ*ØÔ7ð4ñ 4ô 4�Ô0ð
 $3×#HÒ#HÑ#JÔ#JÐ Ý!7Ð8HÈ$ÔJjÑ!kÔ!k�Ø$Ø×5Ò5°nÑEÔEÐEà,;×,PÒ,PÑ,RÔ,RÐ)Ý'9Ð:SÐUYÔUuÑ'vÔ'vÐ$Ø-Ø×4Ò4Ð5IÑJÔJÐJØ,@�Ô)Ð)Ð)å)<Ý2°4×3OÒ3OÑ3QÔ3QÐSWÔSoÑpÔpÐqñ*ô *�Ô&Ð&Ð&ð' -Ð,r!   c                 ód   — | j         r&t          | j        ¦  «        dk    rd| j        _        dS dS dS )as  
        Disables the mapping of input ids despite the assistant pruning for the language model head being enabled.

        This method is required for the first forward pass of `_MapInputEmbedding` where input ids are already in the assistant vocabulary space. By disabling the mapping, it ensures that the input ids are processed correctly without remapping.

        r   FN)r.  r…   r9  r?  r"  ©r   s    r   Úunmap_input_idsz+AssistantToTargetTranslator.unmap_input_idsí  sE   € ð Ô'ð 	2­C°Ô0HÑ,IÔ,IÈAÒ,MÐ,MØ,1ˆDÔ%Ô)Ð)Ð)ð	2ð 	2Ð,MÐ,Mr!   c                 óR  ‡‡— | j                              ¦   «         }| j                             ¦   «         }d}|                       |d¬¦  «        d         }t          |¦  «        dk    r£| j                              |¦  «        d         d         Š|                      |d¬¦  «        d         }t          |¦  «        dk    rM| j                             |¦  «        d         d         Š‰‰k    r!ˆˆfd„|                     ¦   «         D ¦   «         }t          |                     ¦   «         ¦  «        }t          j	        |dz   f| j
        t          ¬¦  «        }i }|                     ¦   «         D ]&\  }	}
|                     |	¦  «        }|�
|||
<   |
||<   Œ'|                     | j        ¦  «        |fS )	Nú F)rð   r   r   c                 ót   •— i | ]4\  }}|                      ‰¦  «        r|                     ‰‰d ¦  «        n||“Œ5S ©r   )Ú
startswithÚreplace)rH   ÚtokrÆ   Úassistant_space_signÚtarget_space_signs      €€r   ú
<dictcomp>zRAssistantToTargetTranslator._get_assistant_to_target_input_ids.<locals>.<dictcomp>  s_   ø€ ð 'ð 'ð 'ñ %˜C ð  #Ÿ~š~Ð.BÑCÔCð%˜CŸKšKÐ(<Ð>OÐQRÑSÔSÐSà!$Øð'ð 'ð 'r!   r   ©rD   )r2  Ú	get_vocabr3  r…   Úconvert_ids_to_tokensrU   r†   rÎ   r.   Úfullr,  r2   r`   rL   r4  )r   Útarget_vocabÚassistant_vocabÚ	space_strÚtarget_space_idsÚassistant_space_idsÚmax_assistant_indexÚassistant_to_target_input_idsr7  rJ  Úassistant_idÚ	target_idrK  rL  s               @@r   r5  z>AssistantToTargetTranslator._get_assistant_to_target_input_idsû  sÝ  øø€ ØÔ-×7Ò7Ñ9Ô9ˆØÔ3×=Ò=Ñ?Ô?ˆàˆ	Ø×1Ò1°)ÐPUÐ1ÑVÔVÐWbÔcÐÝÐÑ Ô  1Ò$Ð$Ø $Ô 6× LÒ LÐM]Ñ ^Ô ^Ð_`Ô aÐbcÔ dÐà"&×";Ò";¸IÐZ_Ð";Ñ"`Ô"`ÐalÔ"mÐÝÐ&Ñ'Ô'¨!Ò+Ð+Ø'+Ô'@×'VÒ'VÐWjÑ'kÔ'kÐlmÔ'nÐopÔ'qÐ$à$Ð(<Ò<Ð<ð'ð 'ð 'ð 'ð 'ð )8×(=Ò(=Ñ(?Ô(?ð'ñ 'ô '�Oõ " /×"8Ò"8Ñ":Ô":Ñ;Ô;ÐÝ(-¬
Ð4GÈ!Ñ4KÐ3MÈtÔOeÕmpÐ(qÑ(qÔ(qÐ%Ø8:Ð%Ø!0×!6Ò!6Ñ!8Ô!8ð 	Hð 	HÑˆC�Ø$×(Ò(¨Ñ-Ô-ˆIØÐ$Ø>GÐ-¨lÑ;Ø;GÐ-¨iÑ8øØ,×/Ò/°Ô0LÑMÔMÐOlÐlÐlr!   r   c                 óR   — t          j        | j        | j        k    ¦  «        d         S )z`
        Get the input ids that are in the assistant vocab but not in the target vocab.
        r   )r.   Úwherer6  r,  rB  s    r   r8  z3AssistantToTargetTranslator._get_suppress_input_ids  s%   € õ Œ{˜4Ô>À$ÔBXÒXÑYÔYÐZ[Ô\Ð\r!   Úassistant_candidate_idsc                 ó
  — t          |d         ¦  «        |j        d         z
  }|dk    r|S |d| d…f         }| j        r| j        |         }| j        |         }t          j        ||                     d¦  «        fd¬¦  «        S )aS  
        Return the target candidate ids that correspond to the assistant candidate ids.
        Note that we have already the target ids for the prompt and we only need to find the target ids for the new tokens.
        Moreover, assistant ids of the original prompt does not necessarily appear in _assistant_to_target_input_ids.
        r   r   Nr£   )r…   rb   r.  r  r6  r.   r¦   r$  )r   rý   Útarget_input_idsr]  Únum_new_tokensÚlast_candidate_idsÚtransformed_slices          r   Úget_target_idsz*AssistantToTargetTranslator.get_target_ids$  s¦   € õ Ð4°QÔ7Ñ8Ô8Ð;NÔ;TÐUVÔ;WÑWˆØ˜QÒÐØ#Ð#ð "9¸¸^¸OÐ<LÐ<LÐ9LÔ!MÐØÔ+ð Zà%)Ô%EÐFXÔ%YÐ"Ø $Ô CÐDVÔ WÐÝ”9Ð.Ð0A×0KÒ0KÈAÑ0NÔ0NÐOÐUVÐWÑWÔWÐWr!   Úassistant_logitsc                 ó(  — g |j         dd…         ¢| j        ‘R }t          j        || j        | j        ¬¦  «        }| j        | j        k    }| j        |         }| j        r||d|f<   n+|dd| j        j         d         …f         }|d|f         |d|f<   |S )zS
        Return the target logits that correspond to the assistant logits.
        Nr•   ©rC   .r   )	rb   r-  r.   rQ  r+  r4  r6  r,  r.  )r   rd  Útarget_shapeÚtarget_logitsÚassistant_indices_maskÚtarget_logits_supported_indicesÚvalid_assistant_logitss          r   Úget_target_logitsz-AssistantToTargetTranslator.get_target_logits9  sÏ   € ð
 )_Ð*:Ô*@ÀÀ"ÀÔ*EÐ(^ÀtÔG]Ð(^Ð(^ˆÝ+0¬:Ø˜$Ô+°DÔ4Pð,
ñ ,
ô ,
ˆð "&Ô!DÈÔH^Ò!^Ðà*.Ô*MÐNdÔ*eÐ'àÔ'ð 	vØBRˆM˜#Ð>Ð>Ñ?Ð?à%5°cÐ;i¸TÔ=`Ô=fÐghÔ=iÐ;iÐ6iÔ%jÐ"ØBXÐY\Ð^tÐYtÔBuˆM˜#Ð>Ð>Ñ?ØÐr!   ©NF)r(   r)   r*   r+   Úfloatr+  r-   r,  r2   r   r,   rt   rC  r5  rÍ   r8  r.   r/   rc  r1   rl  r3   r!   r   r)  r)  ¬  sC  € € € € € € ðð ð( !˜5 ™<œ<˜-€L�%Ð'Ð'Ñ'ØÐ�sÐÐÑð 8<Ø(-ð'ð 'à3ð'ð 7ð'ð ð	'ð
 "Ð"3Ô4ð'ð "&ð'ð 'ð 'ð 'ðR2ð 2ð 2ð!mð !mð !mðF]¨¨c¬ð ]ð ]ð ]ð ]ðXØNSÔN^ðXà	Ô	ðXð Xð Xð Xð*°%Ô2Cð ÈÔHYð ð ð ð ð ð r!   r)  c                   óˆ   — e Zd ZdZ ej        ¦   «         Ze	 	 ddddddede	d	         d
e
defd„¦   «         Zed„ ¦   «         ZdS )ÚAssistantVocabTranslatorCachez§
    Cache for `AssistantToTargetTranslator` instances. The instances are computed at
    pre-processing time, and this cache allows us to avoid recomputing them.
    NFrµ   r   r¶   r-  r6   r   r.  r   c                 óÖ   — | j                              |¦  «        }|€t          j        ¦   «         }|| j         |<   |                     |¦  «        }|€t	          |||||¦  «        }|||<   |S r¸   )Ú_cacher`   ÚweakrefÚWeakKeyDictionaryr)  )Úclsrµ   r¶   r-  r6   r.  Úassistant_dictÚmappings           r   Úget_translatorz,AssistantVocabTranslatorCache.get_translatorW  s‚   € ð œŸšÐ(8Ñ9Ô9ˆØÐ!Ý$Ô6Ñ8Ô8ˆNØ+9ˆCŒJÐ'Ñ(à ×$Ò$Ð%8Ñ9Ô9ˆØˆ?Ý1Ø Ø#Ø!ØØ'ñô ˆGð 3:ˆNÐ.Ñ/àˆr!   c                 ó¢   — d„ | j         D ¦   «         }|D ]
}| j         |= Œ| j                              ¦   «         D ]}d„ |D ¦   «         }|D ]}||= ŒŒdS )z²
        Clean up dead references in the cache.
        This removes entries where either the target_tokenizer or assistant_tokenizer
        has been garbage collected.
        c                 ó   — g | ]}|­|‘ŒS r¸   r3   ©rH   rq   s     r   rJ   z9AssistantVocabTranslatorCache.cleanup.<locals>.<listcomp>z  s   € Ð>Ð>Ð>˜S°#°+�S°+°+°+r!   c                 ó   — g | ]}|­|‘ŒS r¸   r3   r{  s     r   rJ   z9AssistantVocabTranslatorCache.cleanup.<locals>.<listcomp>€  s   € ÐFÐFÐF ¸#¸+˜¸+¸+¸+r!   N)rr  rÎ   )ru  Ú	dead_keysrq   rv  s       r   Úcleanupz%AssistantVocabTranslatorCache.cleanupr  sŽ   € ð ?Ð> C¤JÐ>Ñ>Ô>ˆ	Øð 	 ð 	 ˆCØ”
˜3��ð "œj×/Ò/Ñ1Ô1ð 	(ð 	(ˆNØFÐF¨ÐFÑFÔFˆIØ ð (ð (�Ø" 3Ð'Ð'ð(ð	(ð 	(r!   rm  )r(   r)   r*   r+   rs  rt  rr  Úclassmethodr2   r   r,   r)  rx  r~  r3   r!   r   rp  rp  O  s¶   € € € € € ðð ð
 'ˆWÔ&Ñ(Ô(€Fàð 8<Ø(-ðð à3ðð 7ðð ð	ð
 "Ð"3Ô4ðð "&ðð 
%ðð ð ñ „[ðð4 ð(ð (ñ „[ð(ð (ð (r!   rp  c                   óú   ‡ — e Zd ZdZ	 	 ddej        ddddddd	d
dededej        dz  de	d         fˆ fd„Z
dej        deej        ej        f         fd„Zddej        dedefˆ fd„Zdej        dej        fd„Zˆ xZS )Ú%UniversalSpeculativeDecodingGeneratorzó
    `CandidateGenerator` class to be used for Universal Speculative Decoding (USD): speculative decoding with different tokenizers
    for the assistant and main models. This class generates candidates through the use of a smaller model.
    Nr   r6   r   rµ   r   r¶   r7   r   r8   Úatm_translatorr9   r:   r   c
           
      ó‚   •— || _         t          ¦   «                              ||||||||	¦  «         d| _        d | _        d S )Nr   )Ú_atm_translatorr¹   rt   Ú_target_seq_len_with_candidatesÚ_prev_assistant_ids)r   r   r6   rµ   r¶   r7   r8   r‚  r9   r:   r   s             €r   rt   z.UniversalSpeculativeDecodingGenerator.__init__‹  sY   ø€ ð  .ˆÔÝ‰Œ×ÒØØØØØØØØñ		
ô 		
ð 		
ð 56ˆÔ,Ø<@ˆÔ Ð Ð r!   r   c                 óV  — |                      | j        j        ¦  «        }|                      |¦  «        \  }}|                      |¦  «        \  }}|dk    r|dfS |                      ||¬¦  «         |                      |||¦  «        }d|d         _        d|d         _        | j	        j
        �| j	        j
        |d<   |                      |¦  «        \  | _        }	| j	                             ||| j        ¦  «        }
|
j        d         | _        | j	                             |	¦  «        }|
|fS )zk
        Simplified version of get_candidates that uses the translator cache for token conversion.
        r   N©r™   Tr7   r:   r•   )rL   r6   rC   rú   rw   rx   ry   re   rd   r„  r:  rz   r†  rc  rb   r…  rl  )r   r   r   r_  rý   r™   ri   r{   r|   Úassistant_candidate_logitsÚtarget_candidate_idsÚtarget_candidate_logitss               r   r    z4UniversalSpeculativeDecodingGenerator.get_candidates§  sQ  € ð %Ÿ<š<¨Ô(<Ô(CÑDÔDÐØ04×0QÒ0QÐRbÑ0cÔ0cÑ-ÐÐ-Ø)-×)CÒ)CÐDTÑ)UÔ)UÑ&ˆ˜à˜QÒÐØ˜d�?Ð"à×#Ò#Ð$7ÐJZÐ#Ñ[Ô[Ð[Ø×7Ò7Ð8KÈ^Ð]kÑlÔlˆð >BˆÐ+Ô,Ô:ØGKˆÐ+Ô,ÔDð ÔÔ1Ð=Ø26Ô2FÔ2XˆOÐ.Ñ/Ø?C×?XÒ?XÐYhÑ?iÔ?iÑ<ˆÔ Ð"<ð  $Ô3×BÒBØÐ!1°4Ô3Kñ 
ô  
Ðð 0DÔ/IÈ"Ô/MˆÔ,Ø"&Ô"6×"HÒ"HÐIcÑ"dÔ"dÐà#Ð%<Ð<Ð<r!   r   rý   r™   c                 ó,  •— | j         €jt          | j        |j        d         | j        j        j        ¦  «        | _        t          | j        |j        d         | j        j        j        ¦  «        | _        t          ¦   «          	                    ||¬¦  «        S )Nr•   rˆ  )
r†  rœ   r^   rb   r6   rY   rZ   r�   r¹   rx   )r   rý   r™   r   s      €r   rx   z<UniversalSpeculativeDecodingGenerator._update_past_and_masksÇ  s�   ø€ ØÔ#Ð+õ %<ØÔ%Ð':Ô'@ÀÔ'DÀdÔFZÔFaÔFtñ%ô %ˆDÔ!õ %:ØÔ%Ð':Ô'@ÀÔ'DÀdÔFZÔFaÔFtñ%ô %ˆDÔ!õ ‰wŒw×-Ò-Ð.AÐTdÐ-ÑeÔeÐer!   r_  c                 ó"  — |j         d         }| j        dk    r|}nd}|dd…| d…f         }d}| j        dk    r7| j        j                             |d                              ¦   «         ¦  «        }|€Y| j                             |dd¬¦  «        }|                      |dd¬	¦  «        d
          	                    | j
        j        ¦  «        }n"t          j        |gg| j
        j        ¬¦  «        }| j        €|}nI| j        dz   |z
  }|dk    r| j        dd…d| …f         | _        t          j        | j        |gd¬¦  «        }| 	                    t          j        ¬¦  «        }| j                             ¦   «          |t%          |d         ¦  «        fS )zM
        Simplified token conversion that only processes new tokens.
        r•   r   r   NTrë   Frî   rï   r   rf  r£   rN  )rb   r…  r„  r7  r`   rÝ   rµ   rò   r¶   rL   r6   rC   r.   rÛ   r†  r¦   rc   rC  r…   )	r   r_  Útarget_seq_lenÚnew_token_countÚtarget_new_idsÚassistant_new_idsÚtarget_new_textrý   Útokens_to_removes	            r   rú   zBUniversalSpeculativeDecodingGenerator._prepare_assistant_input_idsÓ  sØ  € ð
 *Ô/°Ô3ˆØÔ/°1Ò4Ð4Ø,ˆOˆOàˆOØ)¨!¨!¨!¨oÐ-=Ð->Ð->Ð*>Ô?ˆð !ÐØÔ/°!Ò3Ð3à $Ô 4Ô R× VÒ VÐWeÐfgÔWh×WmÒWmÑWoÔWoÑ pÔ pÐØÐ$Ø"Ô3×:Ò:Ø°DÐW[ð ;ñ ô ˆOð !%× 8Ò 8Ø°EÈ$ð !9ñ !ô !àô!çš2˜dÔ2Ô9Ñ:Ô:ð Ðõ !&¤Ð/@Ð.AÐ-BÈ4ÔK_ÔKfÐ gÑ gÔ gÐð Ô#Ð+Ø"3ÐÐà#ÔCÀaÑGÈ.ÑXÐà !Ò#Ð#Ø+/Ô+CÀAÀAÀAÐGYÐIYÐHYÐGYÐDYÔ+Z�Ô(Ý"'¤)¨TÔ-EÐGXÐ,YÐ_aÐ"bÑ"bÔ"bÐØ1×4Ò4½5¼:Ð4ÑFÔFÐØÔ×,Ò,Ñ.Ô.Ð.Ø"¥CÐ(9¸!Ô(<Ñ$=Ô$=Ð=Ð=r!   r±   rG  )r(   r)   r*   r+   r.   r/   r²   r)  rV   r   rt   r0   r1   r    r2   r,   rx   rú   r  r  s   @r   r�  r�  …  ss  ø€ € € € € ðð ð .2Ø<@ðAð AàÔ#ðAð +ðAð 4ð	Að
 7ðAð .ðAð ðAð 4ðAð ”| dÑ*ðAð #Ð#8Ô9ðAð Að Að Að Að Að8=¨Ô(8ð =ÀuÈUÔM]Ð_dÔ_pÐMpÔGqð =ð =ð =ð =ð@
fð 
f¸%Ô:Jð 
fÐ^að 
fÐjnð 
fð 
fð 
fð 
fð 
fð 
fð&>¸UÔ=Mð &>ÐRWÔRbð &>ð &>ð &>ð &>ð &>ð &>ð &>ð &>r!   r�  c                   óÈ   — e Zd ZdZ	 	 	 	 	 	 ddej        dz  deded	ed
ed         dedz  fd„Zdej	        de
ej	        ej        f         fd„Zdej	        dej        defd„ZdS )ÚPromptLookupCandidateGeneratora)  
    `CandidateGenerator` class to be used for prompt lookup generation. This class generates candidates by looking up
    likely continuations in the provided prompt (input_ids) itself.
    Read the following blog post for more information: https://github.com/apoorvumang/prompt-lookup-decoding

    Args:
        eos_token_id (`torch.Tensor`, *optional*):
            The token id of the end of sequence token.
        num_output_tokens (`int`, *optional*, defaults to 10):
            The number of tokens to be output as candidate tokens.
        max_matching_ngram_size (`int`, *optional*, defaults to 2):
            The maximum ngram size to be considered for matching in the prompt
        max_length (`int`, *optional*, defaults to 20):
            The number of total maximum tokens that can be generated. For decoder-only models that includes the
            prompt length. Defaults to 20, which is the max length used as default in generation config.
        logits_processor (`LogitsProcessorList`, *optional*):
            An instance of [`LogitsProcessorList`]. List of instances of class derived from [`LogitsProcessor`]
            used to modify the prediction scores of the language modeling head applied at each generation step. In
            prompt lookup assisted generation, they are not used to manipulate probabilities, but rather to find
            forbidden tokens (p = -inf) and block them from being valid candidates.
        vocab_size (`int`, *optional*):
            The size of the vocabulary. Required if `logits_processor` is provided.
    Né
   r   é   rT   Únum_output_tokensÚmax_matching_ngram_sizerj   r:   r   Ú
vocab_sizec                 ó¤   — || _         || _        || _        || _        || _        || _        | j        dk    s| j         dk    rt          d¦  «        ‚d S )Nr   z4Invalid max_matching_ngram_size or num_output_tokens)r˜  r™  rj   rT   r:   rš  Ú
ValueError)r   rT   r˜  r™  rj   r:   rš  s          r   rt   z'PromptLookupCandidateGenerator.__init__  se   € ð "3ˆÔØ'>ˆÔ$Ø$ˆŒØ(ˆÔØ 0ˆÔØ$ˆŒàÔ'¨1Ò,Ð,°Ô0FÈ!Ò0KÐ0KÝÐSÑTÔTÐTð 1LÐ0Kr!   r   r   c           
      ó8  — |j         \  }}| j        |dz   k    r|dfS d}d}t          t          | j        |dz
  ¦  «        dd¦  «        D �]}|                     d|d¬¦  «        }|d| d…f         }	||	k                         d¬¦  «        }
|
                     d	¬
¦  «        d         }|D �] }||z   }|| j        z   }t          ||| j        ¦  «        }||k     �rq|d||…f         }| j	        �é|}t          j        || j        f|j        t          j        ¬¦  «        }t          |¦  «        D ]—\  }}|  	                    ||¦  «        }|d|f         }|t!          d¦  «         t          j        |j        ¦  «        j        fv r|d|…         } n7t          j        ||d|dz   …                              d¦  «        fd¬¦  «        }Œ˜|j         d         dk    r�Œ+d	}| j        �jt          j        || j        ¦  «        }t          j        |¦  «        }|                     ¦   «         dk    r$|d                              ¦   «         }|d|…         } n�Œ¢|r n�Œ|r|�t3          |¦  «        dk    r|dfS |                     d¦  «        }t          j        ||fd¬¦  «        }|dfS )a¤  
        Fetches the candidates to be tried for the current input.

        Args:
            input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
                Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)

        Return:
            `torch.LongTensor` of shape `(num_candidates, candidate_length)`: The candidate sequences to be tried.
        r   NFr   r•   )Ú	dimensionÚsizeÚstepr   r£   T)Úas_tuplerB   r*  )rb   rj   r©   r–   r™  ÚunfoldÚallrÌ   r˜  r:   r.   ra   rš  rC   Úfloat32Ú	enumeratern  ÚfinforD   r¦   r$  rT   ÚisinÚnumelrÝ   r…   )r   r   r   ÚbszÚinput_lengthÚ
chosen_idsÚmatch_foundÚ
ngram_sizeÚwindowsÚngram_tensorro   Úmatch_indicesrÆ   rÇ   Úend_idxÚsequence_with_candidateÚfake_input_logitsÚcandidate_idxÚnew_candidate_tokenÚfake_output_logitsÚfake_candidate_logitsÚmaskÚmatch_indices_eosÚfirst_eos_indexÚcandidate_input_idss                            r   r    z-PromptLookupCandidateGenerator.get_candidates(  s>  € ð &œOÑˆˆ\ð Œ?˜l¨QÑ.Ò.Ð.Ø˜d�?Ð"àˆ
ØˆÝ¥ DÔ$@À,ÐQRÑBRÑ SÔ SÐUVÐXZÑ[Ô[ð @	ñ @	ˆJà×&Ò&°¸È!Ð&ÑLÔLˆGð % Q¨¨¨¨ _Ô5ˆLð  ,Ò.×3Ò3¸Ð3Ñ:Ô:ˆGð $ŸOšO°T˜OÑ:Ô:¸1Ô=ˆMð
 %ð .ñ .�Ø *Ñ,�	Ø# dÔ&<Ñ<�Ý˜g |°T´_ÑEÔE�à˜wÒ&Ñ&Ø!*¨1¨i¸Ð.?Ð+?Ô!@�Jð Ô,Ð8Ø2;Ð/Ý,1¬JØ  $¤/Ð2¸9Ô;KÕSXÔS`ð-ñ -ô -Ð)õ CLÈJÑBWÔBWð 
"ð 
"Ñ>˜MÐ+>Ø15×1FÒ1FÐG^Ð`qÑ1rÔ1rÐ.Ø4FÀqÐJ]ÐG]Ô4^Ð1à4½%À¹,¼,¸ÍÌÐTiÔToÑHpÔHpÔHtÐ8uÐuÐuØ-7¸¸¸Ô-G 
Ø % å:?¼)Ø%.°
Ð;N¸]ÈQÑ=NÐ;NÔ0O×0YÒ0YÐZ[Ñ0\Ô0\Ð$]Ðcdð;"ñ ;"ô ;"Ð 7Ð 7ð &Ô+¨AÔ.°!Ò3Ð3Ù$à"&�Kð
 Ô(Ð4Ý$œz¨*°dÔ6GÑHÔH˜Ý,1¬M¸$Ñ,?Ô,?Ð)Ø,×2Ò2Ñ4Ô4°qÒ8Ð8Ø.?ÀÔ.B×.GÒ.GÑ.IÔ.I˜OØ)3Ð4D°_Ð4DÔ)E˜JØ�EñS 'ðT ð Ø�ñð ð 	#˜jÐ0µC¸
±O´OÀqÒ4HÐ4HØ˜d�?Ð"ð  ×)Ò)¨!Ñ,Ô,ˆ
Ý#œi¨°JÐ(?ÀQÐGÑGÔGÐà" DÐ(Ð(r!   r"   r#   c                 ó   — dS )r%   Nr3   r&   s       r   r'   z8PromptLookupCandidateGenerator.update_candidate_strategy‡  s	   € ð 	ˆr!   )Nr–  r   r—  NN)r(   r)   r*   r+   r.   rV   r2   r   rt   r/   r0   r1   r    r'   r3   r!   r   r•  r•  ü  s  € € € € € ðð ð4 -1Ø!#Ø'(ØØ<@Ø!%ðUð Uà”l TÑ)ðUð ðUð "%ð	Uð
 ðUð #Ð#8Ô9ðUð ˜$‘JðUð Uð Uð Uð&])¨Ô(8ð ])ÀuÈUÔM]Ð_dÔ_pÐMpÔGqð ])ð ])ð ])ð ])ð~°5Ô3Cð ÈUÔM^ð Ðmpð ð ð ð ð ð r!   r•  c                   óª   ‡ — e Zd ZdZ	 	 ddej        ddddded	ej        dz  d
ed         fˆ fd„Z	dej        de
ej        ej        f         fˆ fd„Zˆ xZS )ÚEarlyExitCandidateGeneratora™  
    `CandidateGenerator` class to be used for assisted generation and speculative decoding. This class generates
    candidates through the use of **the model itself**, exiting early. Can only be used with models that support early
    exit, e.g., `facebook/layerskip-llama3.2-1B`.

    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
        assistant_model (`PreTrainedModel`):
            The original model. This model must support early exit (i.e. is trained to compute logits in earlier
            layers).
        generation_config (`~generation.GenerationConfig`, *optional*):
            The generation configuration to be used as base parametrization for the generation call.
        logits_processor (`LogitsProcessorList`):
            An instance of [`LogitsProcessorList`]. List of instances of class derived from [`LogitsProcessor`]
            used to modify the prediction scores of the language modeling head applied at each generation step.
        model_kwargs (`Dict`):
            The keyword arguments that will be passed to the main model, and are used as base inputs for the assistant
            model as well.
        inputs_tensor (`torch.Tensor`, *optional*):
            The model input tensor. In encoder-decoder models, this is the encoder input.
    Nr   r6   r   r7   r   r8   r9   r:   r   c                 ó�   •— t          ¦   «                              ||||||¬¦  «         | j        j        | _        d | j        _        d S )N)r   r6   r7   r8   r9   r:   )r¹   rt   r7   Úassistant_early_exit)r   r   r6   r7   r8   r9   r:   r   s          €r   rt   z$EarlyExitCandidateGenerator.__init__°  sY   ø€ õ 	‰Œ×ÒØØ+Ø/Ø%Ø'Ø-ð 	ñ 	
ô 	
ð 	
ð %)Ô$:Ô$OˆÔ!Ø6:ˆÔÔ3Ð3Ð3r!   r   c                 óä   •— t          | j        | j        j        ¦  «        }|j        j        }| j        |j        _        t          ¦   «                              |¦  «        \  }}||j        _        ||fS r¸   )Úgetattrr6   Úbase_model_prefixrY   Únum_hidden_layersrÀ  r¹   r    )r   r   r   Ú
base_modelÚoriginal_num_hidden_layersr}   r~   r   s          €r   r    z*EarlyExitCandidateGenerator.get_candidatesÆ  si   ø€ å˜TÔ1°4Ô3GÔ3YÑZÔZˆ
Ø%/Ô%6Ô%HÐ"Ø.2Ô.Gˆ
ÔÔ+Ý*/©'¬'×*@Ò*@ÀÑ*KÔ*KÑ'ˆÐ'Ø.Hˆ
ÔÔ+ØÐ.Ð.Ð.r!   r±   )r(   r)   r*   r+   r.   r/   r²   rV   r   rt   r0   r1   r    r  r  s   @r   r¾  r¾  ˜  sÞ   ø€ € € € € ðð ð: .2Ø<@ð;ð ;àÔ#ð;ð +ð;ð .ð	;ð
 ð;ð ”| dÑ*ð;ð #Ð#8Ô9ð;ð ;ð ;ð ;ð ;ð ;ð,/¨Ô(8ð /ÀuÈUÔM]Ð_dÔ_pÐMpÔGqð /ð /ð /ð /ð /ð /ð /ð /ð /ð /r!   r¾  c                   ó<  ‡ — e Zd ZU dZdZeed<   dddœZee	e
f         ed<   	 	 	 ddej        dd	d
ej        dddedej        dz  ded         deee         z  ej        z  dz  fˆ fd„Zdej        dee	e
f         dedededeej        ej        dz  f         fd„Zˆ xZS )Ú*SinglePositionMultiTokenCandidateGeneratoraÑ  Candidate generator for predicting multiple draft tokens from a single token position using the MTP method.

    The Multi-Token Prediction (MTP) method defines an assisted candidate generator with an assistant model that
    autoregressively drafts multiple candidate tokens from a constant `position_ids` value, the last "seen" token, with
    the goals of reducing assistant model compute, increasing drafted token acceptance rates, and increasing target
    model throughput.

    This capability is enabled by three concrete requirements for assistant models:

    *   **Use KV cache sharing techniques for the entire assistant model** (see Gemma 4) to reduce attention computation
        in the assistant model. This method of cache sharing passes the `key_states` and `value_states` from the main
        model in a dictionary, allowing the assistant model to skip pre-fill entirely, and reducing attention
        computations (e.g., `k_proj`, `k_norm`, `v_proj`, and `v_norm` in the Gemma 4 architecture) as applicable to the
        main and assistant models' architectures. This architectural feature effectively locks the assistant into a
        constant `position_ids` value.
    *   **Concatenate the embedding and hidden states for the seen last token** from the target model as the input to
        the assistant. This adaptation allows the assistant to retain rich context about the preceding (possibly
        drafted) tokens while working from a constant `position_ids` value. For the first token drafted after pre-fill,
        the last seen token will be the last token from the prompt. For subsequent drafting steps, the last seen token
        will be the last token generated by the assistant (within a drafting round) or the last token accepted by the
        target model (between drafting rounds).
    *   **Use cross-attention** to allow Q from the assistant to attend to KV from the main model.

    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
        assistant_model (`PreTrainedModel`):
            The model to be used for generating candidates. This model should be smaller than the main model.
        target_model_input_embeddings (`torch.nn.Embedding`):
            The input embedding table from main model, used to get the embeddings from the last seen token.
        generation_config (`~generation.GenerationConfig`, *optional*):
            The generation configuration to be used as base parametrization for the generation call.
        model_kwargs (`dict`):
            The keyword arguments that will be passed to the main model, and are used as base inputs for the assistant
            model as well.
        inputs_tensor (`torch.Tensor`, *optional*):
            The model input tensor. In encoder-decoder models, this is the encoder input.
        logits_processor (`LogitsProcessorList`, *optional*):
            An instance of [`LogitsProcessorList`]. List of instances of class derived from [`LogitsProcessor`]
            used to modify the prediction scores of the language modeling head applied at each generation step.
        eos_token_id (`int` or `list[int]` or `torch.Tensor`, *optional*):
            The token id of the end of sequence token. If not `None`, only the provided values will be used. If None,
            values will be inferred from the available `generation_config` and `assistant_generation_config`.
    Tr   )Úoutput_hidden_statesÚreturn_shared_kv_statesÚmodel_kwargs_overridesNr   r6   r   Útarget_model_input_embeddingsr7   r   r8   r9   r:   r   rT   c	                 ó>  •— d|j         j        vr+d|j         j        vrt          d|j         j        › d�¦  «        ‚t          ¦   «                              ||||||¦  «         || _        |�€@t          ¦   «         }t          | j        j	        t          ¦  «        r |                     | j        j	        ¦  «         n>t          | j        j	        t          ¦  «        r|                     | j        j	        ¦  «         t          | j        j	        t          ¦  «        r |                     | j        j	        ¦  «         n>t          | j        j	        t          ¦  «        r|                     | j        j	        ¦  «         |r-t          j        t#          |¦  «        t          j        ¬¦  «        nd | _	        d S t          |t          j        ¦  «        s?t          |t          ¦  «        r|g}t          j        |t          j        ¬¦  «        | _	        d S |                     ¦   «         | _	        d S )NÚGemma4AssistantÚGemma4UnifiedAssistantzfExpected assistant_model to be a Gemma4AssistantForCausalLM or Gemma4UnifiedAssistantForCausalLM. Got zÒ This candidate generator requires that the assistant model is able to work from a shared_kv_states dictionary. Currently, only the Gemma4AssistantForCausalLM and Gemma4UnifiedAssistantForCausalLM support this.rN  )r   r(   rœ  r¹   rt   rÌ  r¿   rG   r7   rT   r   rQ   r2   rÁ   rO   r.   rÛ   rÍ   rc   rV   )
r   r   r6   rÌ  r7   r8   r9   r:   rT   r   s
            €r   rt   z3SinglePositionMultiTokenCandidateGenerator.__init__  s  ø€ ð  _Ô%>Ô%GÐGÐGØ(°Ô0IÔ0RÐRÐRåðBð  zIô  zSô  z\ð Bð Bð Bñô ð õ 	‰Œ×Ò˜ OÐ5FÈÐVcÐeuÑvÔvÐvØ-JˆÔ*àÑÝ #¡¤ˆLå˜$Ô0Ô=½xÑHÔHð FØ×#Ò# DÔ$:Ô$GÑHÔHÐHÐHÝ˜DÔ2Ô?ÅÑEÔEð FØ× Ò  Ô!7Ô!DÑEÔEÐEå˜$Ô:ÔGÍÑRÔRð PØ×#Ò# DÔ$DÔ$QÑRÔRÐRÐRÝ˜DÔ<ÔIÍ3ÑOÔOð PØ× Ò  Ô!AÔ!NÑOÔOÐOàVbÐ l¥¤­T°,Ñ-?Ô-?ÅuÄzÐ RÑ RÔ RÐ RÐhlˆDÔÐÐÝ˜L­%¬,Ñ7Ô7ð 	4Ý˜,­Ñ,Ô,ð .Ø ,˜~�Ý %¤¨\ÅÄÐ LÑ LÔ LˆDÔÐÐà ,× 1Ò 1Ñ 3Ô 3ˆDÔÐÐr!   Úmodel_outputsÚis_first_iterationÚn_last_matchesr   c           
      ó$  ‡— |r|dfS t          t          | j        ¦  «        | j        |j        d         z
  dz
  ¦  «        }|dk    r|dfS |� t          |d¦  «        rt          |d¦  «        st          d¦  «        ‚|j        d         }|j        }	|j        d         Šˆfd„|	 	                    ¦   «         D ¦   «         }	|dd…||dz   …f         }|dd…dd…f         }
t          j        |j        d         dz
  ggt          j        | j        j        ¬	¦  «        }t          j        |j        d         t          j        |j        ¬	¦  «        }g }g }t#          |¦  «        D �]ð}|                      |
¦  «        }t          j        ||gd¬
¦  «        }t          j        ¦   «         5  |                      ||                     d¦  «        ||	d¬¦  «        }ddd¦  «         n# 1 swxY w Y   |j                             d¬
¦  «        }
|j        }|                     ¦   «         rŽ|                     d¦  «        }t          j        || j        j        |
¦  «        }
|                     t          j        |                     d¦  «        t          j        |j        ¦  «        |j        ¦  «        ¦  «         n|                     |j        ¦  «         |                     |
¦  «         | j         �nt          j!        |t          j"        |
 #                    d¦  «        | j          $                    |
j        ¦  «        ¦  «        ¦  «        }| %                    ¦   «         r n�Œòt          j        |t          j        |d¬
¦  «        gd¬
¦  «        }t          j        |d¬
¦  «        }||fS )z2Generate draft token candidates using the drafter.Nr   r   r  Úshared_kv_statesz_`model_outputs` cannot be None, and they need to contain `hidden_states` and `shared_kv_states`r•   c           
      ó€   •— i | ]:\  }}||d          dd…dd…d‰…dd…f         |d         dd…dd…d‰…dd…f         f“Œ;S )r   Nr   r3   )rH   ÚkÚvÚcurrent_lengths      €r   rM  zMSinglePositionMultiTokenCandidateGenerator.get_candidates.<locals>.<dictcomp>X  sw   ø€ ð 
ð 
ð 
ÙTXÐTUÐWXˆA��!”�Q�Q�Q˜˜˜˜?˜N˜?¨A¨A¨AÐ-Ô.°°!´°Q°Q°Q¸¸¸¸?¸N¸?ÈAÈAÈAÐ5MÔ0NÐOð
ð 
ð 
r!   r1  r£   rE   F)Úinputs_embedsrE   rù   rÔ  Ú	use_cache)&r–   r2   rR   rk   rb   Úhasattrrœ  r  rÔ  rU   r.   rÛ   rc   r6   rC   Úzerosr,   r©   rÌ  r¦   Úno_gradr`   ÚlogitsrÐ   Úlast_hidden_staterÜ   r$  r\  r7   Úpad_token_idrˆ   Ú
zeros_likerT   Ú
logical_orr§  ÚsqueezerL   r£  )r   r   r8   rÐ  rÑ  rÒ  r   r{   rß  rÔ  Úlast_token_idrù   Úsequence_stoppedÚdrafted_logitsÚdrafted_tokensr  Úlast_token_embeddingrÙ  ÚoutputsÚstoppedr}   r~   rØ  s                         @r   r    z9SinglePositionMultiTokenCandidateGenerator.get_candidates3  s  ø€ ð ð 	#Ø˜d�?Ð"õ �S Ô!:Ñ;Ô;¸TÔ=WÐZcÔZiÐjkÔZlÑ=lÐopÑ=pÑqÔqˆØ˜QÒÐØ˜d�?Ð"ð Ð!Ý˜=¨/Ñ:Ô:ð "å˜=Ð*<Ñ=Ô=ð "õ Øqñô ð ð +8Ô*EÀbÔ*IÐØIVÔIgÐð #œ¨Ô+ˆð
ð 
ð 
ð 
Ø\l×\rÒ\rÑ\tÔ\tð
ñ 
ô 
Ðð
 .¨a¨a¨a°À.ÐSTÑBTÐ1TÐ.TÔUÐØ! ! ! ! R S S &Ô)ˆÝ”| i¤o°aÔ&8¸1Ñ&<Ð%=Ð$>ÅeÄjÐY]ÔYmÔYtÐuÑuÔuˆÝ œ; y¤°qÔ'9ÅÄÐT]ÔTdÐeÑeÔeÐð ˆØˆå�~Ñ&Ô&ð #	ñ #	ˆAØ#'×#EÒ#EÀmÑ#TÔ#TÐ Ý!œIÐ';Ð=NÐ&OÐUWÐXÑXÔXˆMå”‘”ð ð Ø×.Ò.Ø"/Ø#/×#3Ò#3Ð4DÑ#EÔ#EØ!-Ø%5Ø#ð /ñ ô �ðð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð ð $œN×1Ò1°bÐ1Ñ9Ô9ˆMØ 'Ô 9Ðð  ×#Ò#Ñ%Ô%ð 6Ø*×4Ò4°QÑ7Ô7�Ý %¤¨G°TÔ5KÔ5XÐZgÑ hÔ h�Ø×%Ò%Ý”K × 1Ò 1°"Ñ 5Ô 5µuÔ7GÈÌÑ7WÔ7WÐY`ÔYgÑhÔhñô ð ð ð ×%Ò% g¤nÑ5Ô5Ð5à×!Ò! -Ñ0Ô0Ð0ð Ô Ð,Ý#(Ô#3Ø$Ý”J˜}×4Ò4°QÑ7Ô7¸Ô9J×9MÒ9MÈmÔNbÑ9cÔ9cÑdÔdñ$ô $Ð ð $×'Ò'Ñ)Ô)ð Ø�Eùõ œ	 9­e¬i¸ÈAÐ.NÑ.NÔ.NÐ"OÐUVÐWÑWÔWˆÝ œ9 ^¸Ð;Ñ;Ô;ÐØÐ.Ð.Ð.s   Æ.GÇG	ÇG	rØ   )r(   r)   r*   r+   r   r,   r-   rË  r²   Ústrr   r.   r/   r&  r'  rV   r   r2   rÍ   rt   r
   r0   r1   r    r  r  s   @r   rÈ  rÈ  Ð  sŒ  ø€ € € € € € ð+ð +ðZ $(Ð˜DÐ'Ð'Ñ'ð !%Ø#'ð.ð .Ð˜D  c œNð ð ñ ð .2Ø<@Ø>Bð+4ð +4àÔ#ð+4ð +ð+4ð (*¤|ð	+4ð
 .ð+4ð ð+4ð ”| dÑ*ð+4ð #Ð#8Ô9ð+4ð ˜D œI‘o¨¬Ñ4°tÑ;ð+4ð +4ð +4ð +4ð +4ð +4ðZ[/àÔ#ð[/ð ˜3 ˜8”nð[/ð #ð	[/ð
 !ð[/ð ð[/ð 
ˆuÔ Ô!2°TÑ!9Ð9Ô	:ð[/ð [/ð [/ð [/ð [/ð [/ð [/ð [/r!   rÈ  c                   óâ   — e Zd ZU dZeed<   ddiZeee	f         ed<   	 ddddd	d
eee	f         de
d         fd„Zdej        d
eee	f         dedededeej        ej        dz  f         fd„Zd„ ZdS )ÚMTPCandidateGeneratorTr   rÉ  rË  NÚ
main_modelr   r7   r   r8   r:   r   c                 ó¾  ‡	‡
‡‡— ddl mŠ	mŠ
mŠmŠm} ddlm} t          |j	         
                    ¦   «         dd ¦  «        | _        | j        €t          d¦  «        ‚|                     ¦   «         }t          d„ |j        d                              ¦   «         D ¦   «         ¦  «        | _        |                     |d| j        i¬	¦  «        | _         ||j	                             ¦   «         ¬
¦  «        }ˆ	ˆ
fd„|j        D ¦   «         |_        ˆˆfd„|j        D ¦   «         |_        || _        | j                             ¦   «          |j        | _        || _        d| _        d S )Nr   )ÚDynamicLayerÚDynamicSlidingWindowLayerÚ$LinearAttentionAndFullAttentionLayerÚ-LinearAttentionAndSlidingWindowAttentionLayerÚMtpCache)ÚMtpModelÚnum_mtp_layerszgCould not find `num_mtp_layers` in the model config. This model probably has no associated mtp weights.c              3   ó$   K  — | ]}|j         V — Œd S r¸   rf  )rH   Úxs     r   ú	<genexpr>z1MTPCandidateGenerator.__init__.<locals>.<genexpr>¯  s$   è è € ÐPÐP¨˜1œ8ÐPÐPÐPÐPÐPÐPr!   r•   Ú )Ú
device_map)rY   c                 óJ   •— g | ]}t          |¦  «        ‰u r
 ‰¦   «         n|‘Œ S r3   )rm   )rH   Úlayerrð  rñ  s     €€r   rJ   z2MTPCandidateGenerator.__init__.<locals>.<listcomp>µ  sA   ø€ ð 
ð 
ð 
ØV[�d 5™kœkÐ-FÐFÐFˆLˆL‰NŒNˆNÈEð
ð 
ð 
r!   c                 óX   •— g | ]&}t          |¦  «        ‰u r ‰|j        ¬ ¦  «        n|‘Œ'S ))Únumber_of_states)rm   rÿ  )rH   rý  rò  ró  s     €€r   rJ   z2MTPCandidateGenerator.__init__.<locals>.<listcomp>¸  sV   ø€ ð 
ð 
ð 
ð õ �E‰{Œ{ÐKÐKÐKð 1Ð0À%ÔBXÐYÑYÔYÐYàð
ð 
ð 
r!   T)Úcache_utilsrð  rñ  rò  ró  rô  Úmodeling_layersrõ  rÂ  rY   Úget_text_configrö  rœ  Úget_decoderÚnextÚlayersÚ
parametersrC   Úfrom_pretrainedÚ	mtp_modelÚget_mtp_configÚ	mtp_cacheÚactivate_past_recordingÚ	do_sampler:   Úis_main_model_prefill)r   rî  r7   r8   r:   rô  rõ  rÅ  r
  rð  rñ  rò  ró  s            @@@@r   rt   zMTPCandidateGenerator.__init__–  sÕ  øøøø€ ð	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	/Ð.Ð.Ð.Ð.Ð.å% jÔ&7×&GÒ&GÑ&IÔ&IÐK[Ð]aÑbÔbˆÔØÔÐ&Ýðñô ð ð  ×+Ò+Ñ-Ô-ˆ
ÝÐPÐP¨ZÔ->¸rÔ-B×-MÒ-MÑ-OÔ-OÐPÑPÔPÑPÔPˆŒØ!×1Ò1°*È"ÈdÌkÐIZÐ1Ñ[Ô[ˆŒð �H JÔ$5×$DÒ$DÑ$FÔ$FÐGÑGÔGˆ	ð
ð 
ð 
ð 
ð 
Ø_hÔ_oð
ñ 
ô 
ˆ	Ôð
ð 
ð 
ð 
ð 
ð #Ô)ð	
ñ 
ô 
ˆ	Ôð #ˆŒØŒ×.Ò.Ñ0Ô0Ð0ð +Ô4ˆŒØ 0ˆÔà%)ˆÔ"Ð"Ð"r!   r   rÐ  rÑ  rÒ  r   c                 óÒ  — |r|d fS |�t          |d¦  «        st          d¦  «        ‚|j        d         }| j        s|dz   n|j        d         dz
  }|d d …d |…f                              | j        ¦  «        }| j        dk    r1| j        r|| _        n"t          j
        | j        |gd¬¦  «        | _        | j        dk    r¦|| j        dz
  k     r˜| j        s‘| j                             | j         ¦  «         |d d …| | j        z
  d …f         }	|d         d d …| | j        z
  d …f         }
|d         d d …| | j        z
  d …f         }| j        d d …| | j        z
  d …d d …f         }n9|d d …| d …f         }	|d         d d …| d …f         }
|d         d d …| d …f         } | j        d
|	|||
| j        | j        | j        |dœ|¤Ž\  }}}d	| _        t          j
        ||gd¬¦  «        }||fS )Nr  zG`model_outputs` cannot be None, and they need to contain `hiden_states`r•   r   r£   rù   rE   )r   Úlast_hidden_statesrE   rù   r
  r  r:   Úfull_input_idsFr3   )rÛ  rœ  r  r  rb   rL   rC   rö  Úfull_seq_last_hidden_statesr.   r¦   r
  r›   r  r  r:   )r   r   r8   rÐ  rÑ  rÒ  r   r  Únum_last_main_model_tokensÚmtp_input_idsÚmtp_position_idsÚmtp_attention_maskr}   r~   r  s                  r   r    z$MTPCandidateGenerator.get_candidatesÇ  sÖ  € ð ð 	#Ø˜d�?Ð"ð Ð ­°¸Ñ(OÔ(OÐ ÝÐfÑgÔgÐgà+8Ô+FÀrÔ+JÐð @DÔ?YÐ%u ^°aÑ%7Ð%7Ð_hÔ_nÐopÔ_qÐtuÑ_uÐ"ð 0°°°Ð3NÐ4NÐ3NÐ0NÔO×RÒRÐSWÔS^Ñ_Ô_Ðð Ô Ò"Ð"àÔ)ð Ø3E�Ô0Ð0å38´9ØÔ5Ð7IÐJÐPQð4ñ 4ô 4�Ô0ð Ô Ò"Ð" ~¸Ô8KÈaÑ8OÒ'OÐ'OÐX\ÔXrÐ'Oð ŒN×Ò Ô!4Ð 4Ñ5Ô5Ð5ð & a a aÐ*DÐ)DÀtÔGZÑ)ZÐ)\Ð)\Ð&\Ô]ˆMØ+¨NÔ;¸A¸A¸AÐ@ZÐ?ZÐ]aÔ]pÑ?pÐ?rÐ?rÐ<rÔsÐØ!-Ð.>Ô!?ÀÀÀÐD^ÐC^ÐaeÔatÑCtÐCvÐCvÐ@vÔ!wÐØ!%Ô!AØ��Ð.Ð.°Ô1DÑDÐFÐFÈÈÈÐIô"ÐÐð
 & a a aÐ*DÐ)DÐ)EÐ)EÐ&EÔFˆMØ+¨NÔ;¸A¸A¸AÐ@ZÐ?ZÐ?[Ð?[Ð<[Ô\ÐØ!-Ð.>Ô!?ÀÀÀÐD^ÐC^ÐC_ÐC_Ð@_Ô!`Ðà-;¨T¬^ð 
.
Ø#Ø1Ø-Ø)Ø”nØ”nØ!Ô2Ø$ð
.
ð 
.
ð ð
.
ð 
.
Ñ*ˆÐ'¨ð &+ˆÔ"õ œ	 9¨mÐ"<À"ÐEÑEÔEˆØÐ.Ð.Ð.r!   c                 ó   — d S r¸   r3   )r   Úargsr   s      r   r'   z/MTPCandidateGenerator.update_candidate_strategy  s   € àˆr!   r¸   )r(   r)   r*   r   r,   r-   rË  r²   rë  r   r   rt   r.   r/   r
   r2   r0   r1   r    r'   r3   r!   r   rí  rí  ‘  s!  € € € € € € Ø#'Ð˜DÐ'Ð'Ñ'à.DÀdÐ-KÐ˜D  c œNÐKÐKÑKð =Að/*ð /*à%ð/*ð .ð/*ð ˜3 ˜8”nð	/*ð
 #Ð#8Ô9ð/*ð /*ð /*ð /*ðbS/àÔ#ðS/ð ˜3 ˜8”nðS/ð #ð	S/ð
 !ðS/ð ðS/ð 
ˆuÔ Ô!2°TÑ!9Ð9Ô	:ðS/ð S/ð S/ð S/ðjð ð ð ð r!   rí  r8   Ú
new_lengthrZ   r   c                 ó¶  — |rdnd}|| vr| S | |         }||j         d         z
  }|dk     r|dd…d|…f         | |<   nA|dk    r;t          j        ||                     |j         d         |f¦  «        gd¬¦  «        | |<   d| v rl| d         }|dk     r|dd…d|…f         | d<   n·|dk    rE|dd…dd…dd…dd…f                              d|dd¦  «        }t          j        ||gd¬¦  «        | d<   nkd	| v rg| d	         }|dk     r|dd…d|…f         | d	<   nG|dk    rA|dd…dd…dd…f                              d|d¦  «        }t          j        ||gd¬¦  «        | d	<   | S )
zNExpands or crops the model's mask for decoding purposes, to the defined lengthrA   rE   r   r   Nr•   r£   Úcross_attention_maskÚimage_attention_mask)rb   r.   r¦   Únew_onesÚrepeat)r8   r  rZ   Úmask_keyr¸  Úmask_length_diffÚ
cross_maskÚnew_masks           r   rœ   rœ   !  sý  € ð ,>ÐSÐ'Ð'ÐCS€HØ�|Ð#Ð#ØÐà˜Ô!€DØ! D¤J¨q¤MÑ1Ðà˜!ÒÐØ!% a a aÐ):Ð*:Ð):Ð&:Ô!;ˆ�XÑÐØ	˜AÒ	Ð	Ý!&¤¨D°$·-²-ÀÄÈAÄÐP`Ð@aÑ2bÔ2bÐ+cÐikÐ!lÑ!lÔ!lˆ�XÑð  Ð-Ð-à!Ð"8Ô9ˆ
Ø˜aÒÐØ3=¸a¸a¸aÐARÐBRÐARÐ>RÔ3SˆLÐ/Ñ0Ð0Ø Ò!Ð!Ø! ! ! ! R S S¨!¨!¨!¨Q¨Q¨Q ,Ô/×6Ò6°qÐ:JÈAÈqÑQÔQˆHÝ38´9¸jÈ(Ð=SÐYZÐ3[Ñ3[Ô3[ˆLÐ/Ñ0øØ	 <Ð	/Ð	/à!Ð"8Ô9ˆ
Ø˜aÒÐØ3=¸a¸a¸aÐARÐBRÐARÐ>RÔ3SˆLÐ/Ñ0Ð0Ø Ò!Ð!Ø! ! ! ! R S S¨!¨!¨! )Ô,×3Ò3°AÐ7GÈÑKÔKˆHÝ38´9¸jÈ(Ð=SÐYZÐ3[Ñ3[Ô3[ˆLÐ/Ñ0àÐr!   c                 ó–  — |rdnd}|                       |¦  «        €| S | |         }||j        d         z
  }|dk     r|dd…d|…f         | |<   n{|dk    rudg|                     ¦   «         dz
  z  dgz   } t          j        ||j        |j        ¬¦  «        j        |Ž |ddd…f         z   dz   }t          j        ||gd¬	¦  «        }|| |<   | S )
zVExpands or crops the model's position ids for decoding purposes, to the defined lengthÚdecoder_position_idsrù   Nr•   r   r   r1  .r£   )	r`   rb   r¤   r.   ÚarangerD   rC   Úviewr¦   )r8   r  rZ   Úposition_keyÚ	positionsÚposition_length_diffÚrequired_dimÚnext_position_idss           r   r�   r�   E  s  € ð .@ÐSÐ)Ð)À^€LØ×Ò˜Ñ%Ô%Ð-ØÐà˜\Ô*€IØ%¨	¬¸Ô(;Ñ;Ðà˜aÒÐØ%.¨q¨q¨qÐ2GÐ3GÐ2GÐ/GÔ%Hˆ�\Ñ"Ð"Ø	 Ò	!Ð	!à�s˜iŸmšm™oœo°Ñ1Ñ2°b°TÑ9ˆàc�EŒLÐ-°Y´_ÈYÔM]Ð^Ñ^Ô^ÔcÐeqÐrØ˜˜R˜S˜S˜Ô!ñ"àñð 	õ
 "œI yÐ2CÐ&DÈ"ÐMÑMÔMÐØ%6ˆ�\Ñ"àÐr!   c                 óL  — |                       d¦  «        €| S | d         }|dd…df                              d¦  «        }||j        d         z
  }|dk     r|dd…d|…f         | d<   n=|dk    r7|                     d|¦  «        }t	          j        | d         |gd¬¦  «        | d<   | S )zXExpands or crops the model's token_type_ids for decoding purposes, to the defined lengthÚtoken_type_idsNr•   r   r   r£   )r`   r$  rb   r  r.   r¦   )r8   r  r,  Úfinal_token_typeÚtype_length_diffÚtoken_type_copiess         r   rž   rž   _  sÛ   € à×ÒÐ(Ñ)Ô)Ð1ØÐð "Ð"2Ô3€NØ% a a a¨ eÔ,×6Ò6°rÑ:Ô:ÐØ! NÔ$8¸Ô$;Ñ;Ðà˜!ÒÐØ)7¸¸¸Ð;LÐ<LÐ;LÐ8LÔ)MˆÐ%Ñ&Ð&Ø	˜AÒ	Ð	Ø,×3Ò3°AÐ7GÑHÔHÐÝ).¬°LÐAQÔ4RÐTeÐ3fÐlnÐ)oÑ)oÔ)oˆÐ%Ñ&ØÐr!   )3rM   rs  Úcollections.abcr   Útypingr   r   r   r   ÚnumpyrŠ   r.   Útorch.nnr&  Úpytorch_utilsr	   Úutilsr
   r   Úconfiguration_utilsr   Úlogits_processr   r   r   Úsklearn.metricsr   Úmodeling_utilsr   Útokenization_utils_baser   r   r5   r´   ÚModuler  r  r)  rp  r�  r•  r¾  rÈ  rí  r²   rë  r2   r,   rœ   r�   rž   r3   r!   r   ú<module>r<     sF  ðð €€€Ø €€€Ø $Ð $Ð $Ð $Ð $Ð $Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5à Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð à .Ð .Ð .Ð .Ð .Ð .Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø hÐ hÐ hÐ hÐ hÐ hÐ hÐ hÐ hÐ hð ÐÑÔð *Ø)Ð)Ð)Ð)Ð)Ð)àð 6Ø0Ð0Ð0Ð0Ð0Ð0ØAÐAÐAÐAÐAÐAØ5Ð5Ð5Ð5Ð5Ð5ð&
ð &
ð &
ð &
ð &
ñ &
ô &
ð &
ðRD/ð D/ð D/ð D/ð D/Ð!3ñ D/ô D/ð D/ðNYð Yð Yð Yð YÐ4Nñ Yô Yð Yðxð ð ð ð ˜RœYñ ô ð ð.5ð 5ð 5ð 5ð 5˜œñ 5ô 5ð 5ðD`ð `ð `ð `ð `ñ `ô `ð `ðF3(ð 3(ð 3(ð 3(ð 3(ñ 3(ô 3(ð 3(ðlt>ð t>ð t>ð t>ð t>Ð,Yñ t>ô t>ð t>ðnYð Yð Yð Yð YÐ%7ñ Yô Yð Yðx5/ð 5/ð 5/ð 5/ð 5/Ð"<ñ 5/ô 5/ð 5/ðp~/ð ~/ð ~/ð ~/ð ~/Ð1Kñ ~/ô ~/ð ~/ðBMð Mð Mð Mð MÐ6ñ Mô Mð Mð`!¨$¨s°C¨x¬.ð !Àcð !Ð_cð !ÐhlÐmpÐruÐmuÔhvð !ð !ð !ð !ðH¨¨S°#¨X¬ð ÀCð Ð]að ÐfjÐknÐpsÐksÔftð ð ð ð ð4¨$¨s°C¨x¬.ð Àcð ÈdÐSVÐX[ÐS[Ìnð ð ð ð ð ð r!   