ó
    ˆŽ¡j¶C  ã                   óâ  • S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SS	KJrJrJr  SS
KJr  SSKJr  SSKJrJrJr  SSKJr  SSKJr  SSKJrJrJ r J!r!J"r"  SSK#J$r$  SSK%J&r&  SSK'J(r(   " S S\"5      r)\ " S S\5      5       r* " S S\ 5      r+ " S S\5      r,\ " S S\*5      5       r-\" SS 9 " S! S"\*\5      5       r. " S# S$\5      r/ " S% S&\$5      r0 " S' S(\!5      r1/ S)Qr2g)*zPyTorch PLBART model.é    N)Únn)ÚCrossEntropyLossé   )Úinitialization)ÚCache)ÚGenerationMixin)ÚBaseModelOutputÚSeq2SeqLMOutputÚSeq2SeqModelOutput)ÚPreTrainedModel)ÚUnpack)ÚTransformersKwargsÚauto_docstringÚcan_return_tuple)Úmerge_with_config_defaults)Úcapture_outputsé   )ÚBartClassificationHeadÚBartDecoderÚBartEncoderÚBartForCausalLMÚBartScaledWordEmbedding)Ú'BigBirdPegasusForSequenceClassification)Úshift_tokens_righté   )ÚPLBartConfigc                   ó   • \ rS rSrSrg)ÚPLBartScaledWordEmbeddingé/   © N©Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__static_attributes__r    ó    Ú·C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\transformers/models/plbart/modular_plbart.pyr   r   /   ó   † Úr'   r   c                   óP   ^ • \ rS rSr% \\S'   SrSrSS/rSr	Sr
SrU 4S jrSrU =r$ )	ÚPLBartPreTrainedModelé3   ÚconfigÚmodelTÚPLBartDecoderLayerÚPLBartEncoderLayerc                 ó�   >• [         TU ]  U5        [        U[        5      (       a!  [        R
                  " UR                  5        g g ©N)ÚsuperÚ_init_weightsÚ
isinstanceÚPLBartForConditionalGenerationÚinitÚzeros_Úfinal_logits_bias)ÚselfÚmoduleÚ	__class__s     €r(   r4   Ú#PLBartPreTrainedModel._init_weights=   s5   ø€ Ü‰Ñ˜fÔ%Ü�fÔ<×=Ñ=Ü�KŠK˜×0Ñ0Õ1ð >r'   r    )r"   r#   r$   r%   r   Ú__annotations__Úbase_model_prefixÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_supports_flash_attnÚ_supports_sdpaÚ_supports_flex_attnr4   r&   Ú__classcell__©r<   s   @r(   r+   r+   3   s<   ø‡ àÓØÐØ&*Ð#Ø-Ð/CÐDÐØÐØ€NØÐ÷2ó 2r'   r+   c                   ó   • \ rS rSrSrg)ÚPLBartEncoderéC   r    Nr!   r    r'   r(   rH   rH   C   r)   r'   rH   c                   ó   • \ rS rSrSrg)ÚPLBartDecoderéG   r    Nr!   r    r'   r(   rK   rK   G   r)   r'   rK   c                   óž  ^ • \ rS rSrSSS.rS\4U 4S jjrS rS r\	\
\         SS	\R                  S-  S
\R                  S-  S\R                  S-  S\R                  S-  S\\R                      S-  S\S-  S\R                   S-  S\R                   S-  S\S-  S\\   S\\R                     \-  4S jj5       5       5       rSrU =r$ )ÚPLBartModeléK   zshared.weight)zencoder.embed_tokens.weightzdecoder.embed_tokens.weightr-   c                 óJ  >• [         TU ]  U5        UR                  UR                  p2UR                  (       a   [
        R                  " UR                  5      OSn[        X1R                  X$S9U l	        [        U5      U l        [        U5      U l        U R                  5         g )Ng      ð?)Úembed_scale)r3   Ú__init__Úpad_token_idÚ
vocab_sizeÚscale_embeddingÚmathÚsqrtÚd_modelr   ÚsharedrH   ÚencoderrK   ÚdecoderÚ	post_init)r:   r-   Úpadding_idxrT   rQ   r<   s        €r(   rR   ÚPLBartModel.__init__R   ss   ø€ Ü‰Ñ˜Ô à"(×"5Ñ"5°v×7HÑ7H�ZØ39×3I×3I”d—i’i §¡Ô/ÈsˆÜ/°
¿N¹NÈKÑqˆŒä$ VÓ,ˆŒÜ$ VÓ,ˆŒà�‰Õr'   c                 ó   • U R                   $ r2   )rY   )r:   s    r(   Úget_input_embeddingsÚ PLBartModel.get_input_embeddings^   s   € Ø�{‰{Ðr'   c                 ó|   • Xl         U R                   U R                  l        U R                   U R                  l        g r2   )rY   rZ   Úembed_tokensr[   )r:   Úvalues     r(   Úset_input_embeddingsÚ PLBartModel.set_input_embeddingsa   s'   € ØŒØ$(§K¡Kˆ�‰Ô!Ø$(§K¡Kˆ�‰Õ!r'   NÚ	input_idsÚattention_maskÚdecoder_input_idsÚdecoder_attention_maskÚencoder_outputsÚpast_key_valuesÚinputs_embedsÚdecoder_inputs_embedsÚ	use_cacheÚkwargsÚreturnc
                 ó  • Uc"  Uc  [        XR                  R                  5      nUc  U R                  " S	UUUS.U
D6nOK[	        U[
        5      (       d6  [        US   [        U5      S:”  a  US   OS[        U5      S:”  a  US   OSS9nU R                  " S	UUUS   UUUU	S.U
D6n[        UR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                  S9$ )
a÷  
decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
    Indices of decoder input sequence tokens in the vocabulary.

    Indices can be obtained using [`AutoTokenizer`] or [`PLBartMultiTokenizer`] depending on the checkpoint.
    See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details.

    [What are decoder input IDs?](../glossary#decoder-input-ids)

    PLBart uses a specific language id token as the starting token for `decoder_input_ids` generation that
    varies according to source and target language, *e.g.* 50003 for *en_XX*, and 50001 for *java*. If
    `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
    `past_key_values`).

    For translation and summarization training, `decoder_input_ids` should be provided. If no
    `decoder_input_ids` is provided, the model will create this tensor by shifting the `input_ids` to the right
    for denoising pre-training following the paper.
decoder_attention_mask (:
    obj:*torch.LongTensor* of shape `(batch_size, target_sequence_length)`, *optional*):
    Default behavior:
    generate a tensor that ignores pad tokens in `decoder_input_ids`. Causal mask will also be used by default.
N)rg   rh   rm   r   r   r   )Úlast_hidden_stateÚhidden_statesÚ
attentions)rg   rh   Úencoder_hidden_statesÚencoder_attention_maskrl   rm   ro   )rs   rl   Údecoder_hidden_statesÚdecoder_attentionsÚcross_attentionsÚencoder_last_hidden_staterv   Úencoder_attentionsr    )r   r-   rS   rZ   r5   r	   Úlenr[   r   rs   rl   rt   ru   rz   )r:   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   Údecoder_outputss               r(   ÚforwardÚPLBartModel.forwardf   s0  € ðP Ñ$Ð)>Ñ)FÜ 2°9¿k¹k×>VÑ>VÓ WÐàÑ"Ø/3¯|ª|ð 0Ø#Ø-Ø+ñ0ð ñ	0‰Oô ˜O¬_×=Ñ=Ü-Ø"1°!Ñ"4Ü47¸Ó4HÈ1Ó4L˜o¨aÒ0ÐRVÜ14°_Ó1EÈÓ1I˜?¨1Ò-ÈtñˆOð Ÿ,š,ð 	
Ø'Ø1Ø"1°!Ñ"4Ø#1Ø+Ø/Øñ	
ð ñ	
ˆô "Ø-×?Ñ?Ø+×;Ñ;Ø"1×"?Ñ"?Ø.×9Ñ9Ø,×=Ñ=Ø&5×&GÑ&GØ"1×"?Ñ"?Ø.×9Ñ9ñ	
ð 		
r'   )r[   rZ   rY   )	NNNNNNNNN)r"   r#   r$   r%   Ú_tied_weights_keysr   rR   r`   re   r   r   r   ÚtorchÚ
LongTensorÚTensorÚlistÚFloatTensorr   Úboolr   r   Útupler   r   r&   rE   rF   s   @r(   rN   rN   K   sP  ø† ð (7Ø'6ñÐð

˜|÷ 
òò0ð
  ØØð .2Ø26Ø59Ø6:Ø:>Ø(,Ø26Ø:>Ø!%ñJ
à×#Ñ# dÑ*ðJ
ð ×(Ñ(¨4Ñ/ðJ
ð !×+Ñ+¨dÑ2ð	J
ð
 !&§¡¨tÑ 3ðJ
ð ˜e×/Ñ/Ñ0°4Ñ7ðJ
ð  ™ðJ
ð ×(Ñ(¨4Ñ/ðJ
ð  %×0Ñ0°4Ñ7ðJ
ð ˜$‘;ðJ
ð Ð+Ñ,ðJ
ð 
ˆu�|‰|Ñ	Ð1Ñ	1ôJ
ó ó ó  öJ
r'   rN   zv
    The PLBART Model with a language modeling head. Can be used for code-to-text, text-to-code and code-to-code.
    )Úcustom_introc                   ó.  ^ • \ rS rSrSrS/rSS0rS\4U 4S jjr SS	\	S
\	S-  S\
S\R                  4U 4S jjjrS	\	SS4S jr\\\          SS\R&                  S-  S\R&                  S-  S\R&                  S-  S\R(                  S-  S\\R,                     S-  S\S-  S\R,                  S-  S\R,                  S-  S\R(                  S-  S\
S-  S\\   S\\R(                     \-  4S jj5       5       5       rS\R(                  4S jrSrU =r$ )r6   é¶   r.   r9   zlm_head.weightzmodel.shared.weightr-   c                 óˆ  >• [         TU ]  U5        [        U5      U l        [        R
                  " [        R                  " SU R                  R                  R                  45      5      U l
        [        R                  " UR                  U R                  R                  R                  SS9U l        U R                  5         g )Nr   F)Úbias)r3   rR   rN   r.   r   ÚBufferr‚   ÚzerosrY   Únum_embeddingsr9   ÚLinearrX   Úlm_headr\   )r:   r-   r<   s     €r(   rR   Ú'PLBartForConditionalGeneration.__init__Â   s~   ø€ Ü‰Ñ˜Ô Ü  Ó(ˆŒ
Ü!#§¢¬5¯;ª;¸¸4¿:¹:×;LÑ;L×;[Ñ;[Ð7\Ó+]Ó!^ˆÔÜ—y’y §¡°·±×1BÑ1B×1QÑ1QÐX]Ñ^ˆŒà�‰Õr'   NÚnew_num_tokensÚpad_to_multiple_ofÚmean_resizingrq   c                 óx   >• [         TU ]  XU5      nU R                  UR                  R                  S   5        U$ )Nr   )r3   Úresize_token_embeddingsÚ_resize_final_logits_biasÚweightÚshape)r:   r”   r•   r–   Únew_embeddingsr<   s        €r(   r˜   Ú6PLBartForConditionalGeneration.resize_token_embeddingsÊ   s<   ø€ ô ™Ñ8¸Ð]jÓkˆØ×&Ñ& ~×'<Ñ'<×'BÑ'BÀ1Ñ'EÔFØÐr'   c                 ó>  • U R                   R                  S   nX::  a  U R                   S S 2S U24   nON[        R                  " SX-
  4U R                   R                  S9n[        R
                  " U R                   U/SS9n[        R                  " U5      U l         g )Néÿÿÿÿr   )Údevice)Údim)r9   r›   r‚   r�   r    Úcatr   rŽ   )r:   r”   Úold_num_tokensÚnew_biasÚ
extra_biass        r(   r™   Ú8PLBartForConditionalGeneration._resize_final_logits_biasÑ   sˆ   € Ø×/Ñ/×5Ñ5°bÑ9ˆØÓ+Ø×-Ñ-ªa°°.°Ð.@ÑA‰HäŸš a¨Ñ)HÐ%IÐRV×RhÑRh×RoÑRoÑpˆJÜ—y’y $×"8Ñ"8¸*Ð!EÈ1ÑMˆHÜ!#§¢¨8Ó!4ˆÕr'   rg   rh   ri   rj   rk   rl   rm   rn   Úlabelsro   rp   c                 óR  • U	b%  Uc"  Uc  [        X�R                  R                  5      nU R                  " U4UUUUUUUU
S.UD6nU R	                  UR
                  5      nXÐR                  R                  UR                  5      -   nSnU	bF  [        5       nU" UR                  SU R                  R                  5      U	R                  S5      5      n[        UUUR                  UR                  UR                  UR                   UR"                  UR$                  UR&                  S9	$ )a€	  
decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
    Indices of decoder input sequence tokens in the vocabulary.

    Indices can be obtained using [`AutoTokenizer`] or [`PLBartMultiTokenizer`] depending on the checkpoint.
    See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details.

    [What are decoder input IDs?](../glossary#decoder-input-ids)

    PLBart uses a specific language id token as the starting token for `decoder_input_ids` generation that
    varies according to source and target language, *e.g.* 50003 for *en_XX*, and 50001 for *java*. If
    `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
    `past_key_values`).

    For translation and summarization training, `decoder_input_ids` should be provided. If no
    `decoder_input_ids` is provided, the model will create this tensor by shifting the `input_ids` to the right
    for denoising pre-training following the paper.
decoder_attention_mask (:
    obj:*torch.LongTensor* of shape `(batch_size, target_sequence_length)`, *optional*):
    Default behavior:
    generate a tensor that ignores pad tokens in `decoder_input_ids`. Causal mask will also be used by default.
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
    config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
    (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

Example Mask-filling:

```python
>>> from transformers import AutoTokenizer, PLBartForConditionalGeneration

>>> model = PLBartForConditionalGeneration.from_pretrained("uclanlp/plbart-base")
>>> tokenizer = AutoTokenizer.from_pretrained("uclanlp/plbart-base")

>>> # en_XX is the language symbol id <LID> for English
>>> TXT = "<s> Is 0 the <mask> Fibonacci number ? </s> en_XX"
>>> input_ids = tokenizer([TXT], add_special_tokens=False, return_tensors="pt").input_ids

>>> logits = model(input_ids).logits
>>> masked_index = (input_ids[0] == tokenizer.mask_token_id).nonzero().item()
>>> probs = logits[0, masked_index].softmax(dim=0)
>>> values, predictions = probs.topk(5)

>>> tokenizer.decode(predictions).split()
['first', 'same', 'highest', 'result', 'number']
```
N)rh   ri   rk   rj   rl   rm   rn   ro   rŸ   )	ÚlossÚlogitsrl   rx   ry   rz   r{   rv   r|   )r   r-   rS   r.   r’   rs   r9   Útor    r   ÚviewrT   r
   rl   rx   ry   rz   r{   rv   r|   )r:   rg   rh   ri   rj   rk   rl   rm   rn   r§   ro   rp   ÚoutputsÚ	lm_logitsÚmasked_lm_lossÚloss_fcts                   r(   r   Ú&PLBartForConditionalGeneration.forwardÚ   s,  € ð@ ÑØ Ñ(Ð-BÑ-JÜ$6°v¿{¹{×?WÑ?WÓ$XÐ!à&*§j¢jØð'
à)Ø/Ø+Ø#9Ø+Ø'Ø"7Øñ'
ð ñ'
ˆð —L‘L ×!:Ñ!:Ó;ˆ	Ø× 6Ñ 6× 9Ñ 9¸)×:JÑ:JÓ KÑKˆ	àˆØÑÜ'Ó)ˆHÙ% i§n¡n°R¸¿¹×9OÑ9OÓ&PÐRX×R]ÑR]Ð^`ÓRaÓbˆNäØØØ#×3Ñ3Ø")×"?Ñ"?Ø&×9Ñ9Ø$×5Ñ5Ø&-×&GÑ&GØ")×"?Ñ"?Ø&×9Ñ9ñ

ð 
	
r'   c                 ó@   • [        XR                  R                  5      $ r2   )r   r-   rS   )r:   r§   s     r(   Ú%prepare_decoder_input_ids_from_labelsÚDPLBartForConditionalGeneration.prepare_decoder_input_ids_from_labels>  s   € Ü! &¯+©+×*BÑ*BÓCÐCr'   )r9   r’   r.   )NT)
NNNNNNNNNN) r"   r#   r$   r%   r?   Ú_keys_to_ignore_on_load_missingr�   r   rR   Úintr‡   r   Ú	Embeddingr˜   r™   r   r   r   r‚   rƒ   r„   r…   r†   r   r   r   rˆ   r
   r   r³   r&   rE   rF   s   @r(   r6   r6   ¶   sÐ  ø† ð  ÐØ':Ð&;Ð#àÐ/ðÐð˜|÷ ð aeñØ!ðØ7:¸T±zðØY]ðà	�‰÷ð ð5¸ð 5Àô 5ð  ØØð .2Ø26Ø59Ø6:Ø:>Ø(,Ø26Ø:>Ø&*Ø!%ñ_
à×#Ñ# dÑ*ð_
ð ×(Ñ(¨4Ñ/ð_
ð !×+Ñ+¨dÑ2ð	_
ð
 !&§¡¨tÑ 3ð_
ð ˜e×/Ñ/Ñ0°4Ñ7ð_
ð  ™ð_
ð ×(Ñ(¨4Ñ/ð_
ð  %×0Ñ0°4Ñ7ð_
ð —‘˜tÑ#ð_
ð ˜$‘;ð_
ð Ð+Ñ,ð_
ð 
ˆu�|‰|Ñ	˜Ñ	.ô_
ó ó ó  ð_
ðBD¸E¿L¹L÷ Dò Dr'   r6   c                   ó   • \ rS rSrSrg)ÚPLBartClassificationHeadiB  r    Nr!   r    r'   r(   r¹   r¹   B  r)   r'   r¹   c                   ó(   ^ • \ rS rSrU 4S jrSrU =r$ )ÚPLBartForSequenceClassificationiF  c                  ó:   >• [        5       R                  " S0 U D6  g)a	  
decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
    Indices of decoder input sequence tokens in the vocabulary.

    Indices can be obtained using [`AutoTokenizer`] or [`PLBartMultiTokenizer`] depending on the checkpoint.
    See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details.

    [What are decoder input IDs?](../glossary#decoder-input-ids)

    PLBart uses a specific language id token as the starting token for `decoder_input_ids` generation that
    varies according to source and target language, *e.g.* 50003 for *en_XX*, and 50001 for *java*. If
    `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
    `past_key_values`).

    For translation and summarization training, `decoder_input_ids` should be provided. If no
    `decoder_input_ids` is provided, the model will create this tensor by shifting the `input_ids` to the right
    for denoising pre-training following the paper.
decoder_attention_mask (:
    obj:*torch.LongTensor* of shape `(batch_size, target_sequence_length)`, *optional*):
    Default behavior:
    generate a tensor that ignores pad tokens in `decoder_input_ids`. Causal mask will also be used by default.
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
    Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
    config.num_labels - 1]`. If `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
Nr    ©r3   r   ©Úsuper_kwargsr<   s    €r(   r   Ú'PLBartForSequenceClassification.forwardG  s   ø€ ô4 	‹�ŠÑ'˜,Ó'r'   r    )r"   r#   r$   r%   r   r&   rE   rF   s   @r(   r»   r»   F  s   ø† ÷(ó (r'   r»   c                   ó<   ^ • \ rS rSr\\U 4S j5       5       rSrU =r$ )ÚPLBartForCausalLMid  c                  ó:   >• [        5       R                  " S0 U D6  g)a¾  
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
    config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
    (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

Example:

```python
>>> from transformers import AutoTokenizer, PLBartForCausalLM

>>> tokenizer = AutoTokenizer.from_pretrained("uclanlp/plbart-base")
>>> model = PLBartForCausalLM.from_pretrained("uclanlp/plbart-base")
>>> assert model.config.is_decoder, f"{model.__class__} has to be configured as a decoder."
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
>>> outputs = model(**inputs)

>>> logits = outputs.logits
>>> expected_shape = [1, inputs.input_ids.shape[-1], model.config.vocab_size]
>>> list(logits.shape) == expected_shape
True
```Nr    r½   r¾   s    €r(   r   ÚPLBartForCausalLM.forwarde  s   ø€ ô2 	‹�ŠÑ'˜,Ó'r'   r    )	r"   r#   r$   r%   r   r   r   r&   rE   rF   s   @r(   rÂ   rÂ   d  s   ø† ØØô(ó ó ö(r'   rÂ   )rÂ   r6   r»   rN   r+   )3Ú__doc__rV   r‚   r   Útorch.nnr   Ú r   r7   Úcache_utilsr   Ú
generationr   Úmodeling_outputsr	   r
   r   Úmodeling_utilsr   Úprocessing_utilsr   Úutilsr   r   r   Úutils.genericr   Úutils.output_capturingr   Úbart.modeling_bartr   r   r   r   r   Ú(bigbird_pegasus.modeling_bigbird_pegasusr   Úmbart.modeling_mbartr   Úconfiguration_plbartr   r   r+   rH   rK   rN   r6   r¹   r»   rÂ   Ú__all__r    r'   r(   Ú<module>rÕ      s  ðñ ã ã Ý Ý %å &Ý  Ý )÷ñ õ
 .Ý &ß IÑ IÝ 7Ý 5÷õ õ _Ý 5Ý .ô	Ð 7ô 	ð ô2˜Oó 2ó ð2ô	�Kô 	ô	�Kô 	ð ôg
Ð'ó g
ó ðg
ñT ðñô
DDÐ%:¸Oó DDóð
DDôN	Ð5ô 	ô(Ð&Mô (ô<(˜ô (ò:�r'   