Ë
    ÛÓ^j”Ç  ã                   ó2  — d Z ddlZddlmZ ddlmZ ddlZddlmZ ddlm	Z	m
Z
mZ ddlmZ dd	lmZmZ dd
lmZmZ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 m!Z!m"Z" ddl#m$Z$m%Z% ddl&m'Z' ddl(m)Z)m*Z*m+Z+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1m2Z2 ddl3m4Z4  e,jj                  e6«      Z7d5d„Z8 G d„ dejr                  «      Z: G d„ dejr                  «      Z; G d„ de«      Z< G d„ dejr                  «      Z=e* G d„ d e%«      «       Z> e*d!¬"«      e G d#„ d$e)«      «       «       Z?e* G d%„ d&e>«      «       Z@ e*d'¬"«       G d(„ d)e>e«      «       ZA e*d*¬"«       G d+„ d,e>e«      «       ZB e*d-¬"«       G d.„ d/e>«      «       ZCe* G d0„ d1e>«      «       ZDe* G d2„ d3e>«      «       ZEg d4¢ZFy)6zPyTorch OpenAI GPT-2 model.é    N)ÚCallable)Ú	dataclass)Únn)ÚBCEWithLogitsLossÚCrossEntropyLossÚMSELossé   )Úinitialization)ÚACT2FNÚget_activation)ÚCacheÚDynamicCacheÚEncoderDecoderCache)ÚGenerationMixin)Úcreate_bidirectional_maskÚcreate_causal_mask)ÚGradientCheckpointingLayer)Ú)BaseModelOutputWithPastAndCrossAttentionsÚ!CausalLMOutputWithCrossAttentionsÚQuestionAnsweringModelOutputÚ SequenceClassifierOutputWithPastÚTokenClassifierOutput)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedModel)ÚConv1D)ÚModelOutputÚauto_docstringÚcan_return_tupleÚlogging)Úmaybe_autocastÚmerge_with_config_defaults)ÚOutputRecorderÚcapture_outputsé   )Ú
GPT2Configc                 ó¶  — |€|j                  d«      dz  }t        j                  ||j                  dd«      «      |z  }|�||z   }t        j
                  j                  |d¬«      }|j                  |j                  «      }t        j
                  j                  ||| j                  ¬«      }t        j                  ||«      }	|	j                  dd«      }	|	|fS )Néÿÿÿÿç      à¿éþÿÿÿ©Údim)ÚpÚtrainingr$   é   )ÚsizeÚtorchÚmatmulÚ	transposer   Ú
functionalÚsoftmaxÚtypeÚdtypeÚdropoutr-   )
ÚmoduleÚqueryÚkeyÚvalueÚattention_maskÚscalingr7   ÚkwargsÚattn_weightsÚattn_outputs
             úq/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/gpt2/modeling_gpt2.pyÚeager_attention_forwardrB   6   sÄ   € Ø€Ø—*‘*˜R“. DÑ(ˆä—<‘<  s§}¡}°R¸Ó'<Ó=ÀÑG€LàÐ!Ø# nÑ4ˆä—=‘=×(Ñ(¨¸2Ð(Ó>€Lð  ×$Ñ$ U§[¡[Ó1€LÜ—=‘=×(Ñ(¨¸È6Ï?É?Ð(Ó[€Lä—,‘,˜|¨UÓ3€KØ×'Ñ'¨¨1Ó-€Kà˜Ð$Ð$ó    c                   ó  ‡ — e Zd Zdˆ fd„	Zdd„Z	 	 	 	 	 ddeej                     dz  dedz  dej                  dz  dej                  dz  dej                  dz  d	e
dz  d
eej                  eej                     z  df   fd„Zˆ xZS )ÚGPT2AttentionNc                 ó–  •— t         ‰| �  «        || _        |j                  | _        |j
                  | _        | j                  | j                  z  | _        | j                  | _        | j                  | j                  z  | j                  k7  r&t        d| j                  › d| j                  › d�«      ‚|j                  | _
        |j                  | _        |j                  | _        || _        || _        d| _        | j                  r| j                  dz  | _        | j                  r+| xj                  t!        | j                  dz   «      z  c_        | j                  rNt#        d| j                  z  | j                  «      | _        t#        | j                  | j                  «      | _        n(t#        d| j                  z  | j                  «      | _        t#        | j                  | j                  «      | _        t+        j,                  |j.                  «      | _        t+        j,                  |j2                  «      | _        | | _        y )	Nz=`embed_dim` must be divisible by num_heads (got `embed_dim`: z and `num_heads`: z).g      ð?r(   r$   r.   r	   )ÚsuperÚ__init__ÚconfigÚhidden_sizeÚ	embed_dimÚnum_attention_headsÚ	num_headsÚhead_dimÚ
split_sizeÚ
ValueErrorÚscale_attn_weightsÚscale_attn_by_inverse_layer_idxÚreorder_and_upcast_attnÚis_cross_attentionÚ	layer_idxr=   Úfloatr   Úc_attnÚq_attnÚc_projr   ÚDropoutÚ
attn_pdropÚattn_dropoutÚresid_pdropÚresid_dropoutÚ	is_causal)ÚselfrI   rT   rU   Ú	__class__s       €rA   rH   zGPT2Attention.__init__L   s¿  ø€ Ü‰ÑÔØˆŒØ×+Ñ+ˆŒØ×3Ñ3ˆŒØŸ™¨$¯.©.Ñ8ˆŒØŸ.™.ˆŒØ�=‰=˜4Ÿ>™>Ñ)¨T¯^©^Ò;ÜØOÐPT×P^ÑP^ÐO_ð `Ø—N‘NÐ# 2ð'óð ð
 #)×";Ñ";ˆÔØ/5×/UÑ/UˆÔ,Ø'-×'EÑ'EˆÔ$Ø"4ˆÔØ"ˆŒð ˆŒØ×"Ò"ØŸ=™=¨$Ñ.ˆDŒLØ×/Ò/Ø�LŠLœE $§.¡.°1Ñ"4Ó5Ñ5�Là×"Ò"Ü   T§^¡^Ñ!3°T·^±^ÓDˆDŒKÜ  §¡°·±Ó@ˆD�Kä   T§^¡^Ñ!3°T·^±^ÓDˆDŒKÜ˜TŸ^™^¨T¯^©^Ó<ˆŒäŸJ™J v×'8Ñ'8Ó9ˆÔÜŸZ™Z¨×(:Ñ(:Ó;ˆÔØ/Ð/ˆ�rC   c                 ó�  — |j                  «       \  }}}}|j                  «       \  }	}	}
}	t        j                  ||z  ||
t        j                  |j                  ¬«      }t        |j                  j                  d¬«      5  |j                  d||«      |j                  dd«      j                  d||
«      }}t        j                  ||j                  «       |j                  «       d| j                  ¬«      }|j                  ||||
«      }d d d «       |�||z   }t        j                  j                  |d¬«      }|j                  t        j                  k7  rt!        d	«      ‚|j                  |j                  «      }| j#                  |«      }t        j$                  ||«      }|j                  d
d«      }||fS # 1 sw Y   Œ±xY w)N)r6   ÚdeviceF)Úenabledr'   r)   r   )ÚbetaÚalphar*   zDError with upcasting, attn_weights does not have dtype torch.float32r$   r.   )r/   r0   ÚemptyÚfloat32rc   r    r5   Úreshaper2   ÚbaddbmmrV   r=   r   r3   r4   r6   ÚRuntimeErrorr\   r1   )r`   r9   r:   r;   r<   ÚbszrM   Ú	q_seq_lenÚdkÚ_Ú	k_seq_lenr?   ÚqÚkr@   s                  rA   Ú_upcast_and_reordered_attnz(GPT2Attention._upcast_and_reordered_attnq   s’  € à(-¯
©
«Ñ%ˆˆY˜	 2Ø ŸX™X›ZÑˆˆ1ˆi˜ô —{‘{ 3¨¡?°I¸yÔPU×P]ÑP]Ðfk×frÑfrÔsˆô ˜EŸL™L×-Ñ-°uÔ=ñ 	VØ—=‘=  Y°Ó3°S·]±]À2ÀrÓ5J×5RÑ5RÐSUÐWYÐ[dÓ5eˆqˆAÜ Ÿ=™=¨°q·w±w³yÀ!Ç'Á'Ã)ÐRSÐ[_×[gÑ[gÔhˆLØ'×/Ñ/°°YÀ	È9ÓUˆL÷	Vð
 Ð%à'¨.Ñ8ˆLä—}‘}×,Ñ,¨\¸rÐ,ÓBˆð ×Ñ¤§¡Ò.ÜÐeÓfÐfØ#×(Ñ(¨¯©Ó5ˆØ×(Ñ(¨Ó6ˆä—l‘l <°Ó7ˆØ!×+Ñ+¨A¨qÓ1ˆà˜LÐ(Ð(÷)	Vð 	Vús   ÂBF<Æ<GÚhidden_statesÚpast_key_valuesr<   Úencoder_hidden_statesÚencoder_attention_maskÚoutput_attentionsÚreturn.c                 óü  — |d u}|�St        |t        «      rA|j                  j                  | j                  «      }	|r|j
                  }
n|j                  }
n|}
|�rt        | d«      st        d«      ‚| j                  |«      }|}|�J	rH
j                  | j                     j                  }|
j                  | j                     j                  }�n$| j                  |«      j                  | j                  d¬«      \  }}g |j                   d d ¢d‘| j"                  ‘­}|j%                  |«      j'                  dd«      }|j%                  |«      j'                  dd«      }n’| j                  |«      j                  | j                  d¬«      \  }}}g |j                   d d ¢d‘| j"                  ‘­}|j%                  |«      j'                  dd«      }|j%                  |«      j'                  dd«      }g |j                   d d ¢d‘| j"                  ‘­}|j%                  |«      j'                  dd«      }|�|r|�?|r=	s;
j)                  ||| j                  «      \  }}|rd|j                  | j                  <   | j*                  j,                  dk(  }t/        j0                  | j*                  j,                  t2        «      }|r$| j4                  r| j7                  ||||«      \  }}n? || ||||f| j8                  r| j:                  j<                  nd	| j>                  d
œ|¤Ž\  }} |j@                  g |j                   d d ¢d‘­Ž jC                  «       }| jE                  |«      }| jG                  |«      }||fS )NrX   z§If class is used as cross attention, the weights `q_attn` have to be defined. Please make sure to instantiate class with `GPT2Attention(..., is_cross_attention=True)`.r.   r*   r'   r$   TÚeagerç        )r7   r=   r)   )$Ú
isinstancer   Ú
is_updatedÚgetrU   Úcross_attention_cacheÚself_attention_cacheÚhasattrrP   rX   ÚlayersÚkeysÚvaluesrW   ÚsplitrO   ÚshaperN   Úviewr2   ÚupdaterI   Ú_attn_implementationr   Úget_interfacerB   rS   rs   r-   r\   r,   r=   ri   Ú
contiguousrY   r^   )r`   rt   ru   r<   rv   rw   rx   r>   rT   r~   Úcurr_past_key_valuesÚquery_statesÚ
key_statesÚvalue_statesÚshape_kvÚshape_qÚusing_eagerÚattention_interfacer@   r?   s                       rA   ÚforwardzGPT2Attention.forward�   sx  € ð 3¸$Ð>ÐØÐ&Ü˜/Ô+>Ô?Ø,×7Ñ7×;Ñ;¸D¿N¹NÓK�
Ù%à+:×+PÑ+PÑ(à+:×+OÑ+OÑ(à'6Ð$âÜ˜4 Ô*Ü ðpóð ð  Ÿ;™; }Ó5ˆLØ3ˆNð Ð*©zØ1×8Ñ8¸¿¹ÑH×MÑM�
Ø3×:Ñ:¸4¿>¹>ÑJ×QÑQ’à+/¯;©;Ð7LÓ+M×+SÑ+SÐTX×TcÑTcÐijÐ+SÓ+kÑ(�
˜LØF˜Z×-Ñ-¨c¨rÐ2ÐF°BÐF¸¿¹ÑF�Ø'Ÿ_™_¨XÓ6×@Ñ@ÀÀAÓF�
Ø+×0Ñ0°Ó:×DÑDÀQÈÓJ‘à59·[±[ÀÓ5O×5UÑ5UÐVZ×VeÑVeÐklÐ5UÓ5mÑ2ˆL˜* lØB˜×)Ñ)¨#¨2Ð.ÐB°ÐB°D·M±MÑBˆHØ#Ÿ™¨Ó2×<Ñ<¸QÀÓBˆJØ'×,Ñ,¨XÓ6×@Ñ@ÀÀAÓFˆLà?�L×&Ñ& s¨Ð+Ð?¨RÐ?°·±Ñ?ˆØ#×(Ñ(¨Ó1×;Ñ;¸A¸qÓAˆàÐ'Ñ0BØÐ'Ñ,>Ázà';×'BÑ'BÀ:È|Ð]a×]kÑ]kÓ'lÑ$ˆJ˜á!Ø=A�×*Ñ*¨4¯>©>Ñ:à—k‘k×6Ñ6¸'ÑAˆÜ(?×(MÑ(MØ�K‰K×,Ñ,Ô.Eó)
Ðñ ˜4×7Ò7Ø(,×(GÑ(GØ˜j¨,¸ó)Ñ%ˆK™ñ )<ØØØØØð	)ð 04¯}ª}˜×)Ñ)×+Ò+À#ØŸ™ñ	)ð ñ	)Ñ%ˆK˜ð *�k×)Ñ)ÐF¨;×+<Ñ+<¸S¸bÐ+AÐFÀ2ÒF×QÑQÓSˆØ—k‘k +Ó.ˆØ×(Ñ(¨Ó5ˆà˜LÐ(Ð(rC   )FN©N©NNNNF)Ú__name__Ú
__module__Ú__qualname__rH   rs   Útupler0   ÚFloatTensorr   ÚTensorÚboolr•   Ú__classcell__©ra   s   @rA   rE   rE   K   sÄ   ø„ õ#0óJ)ðD )-Ø37Ø59Ø;?Ø).ñR)à˜U×.Ñ.Ñ/°$Ñ6ðR)ð  ™ðR)ð ×)Ñ)¨DÑ0ð	R)ð
  %Ÿ|™|¨dÑ2ðR)ð !&× 1Ñ 1°DÑ 8ðR)ð   $™;ðR)ð 
ˆu�|‰|˜e E§L¡LÑ1Ñ1°3Ð6Ñ	7÷R)rC   rE   c                   ób   ‡ — e Zd Zˆ fd„Zdeej                     dz  dej                  fd„Zˆ xZS )ÚGPT2MLPc                 óö   •— t         ‰| �  «        |j                  }t        ||«      | _        t        ||«      | _        t        |j                     | _        t        j                  |j                  «      | _        y r–   )rG   rH   rJ   r   Úc_fcrY   r   Úactivation_functionÚactr   rZ   r]   r7   )r`   Úintermediate_sizerI   rK   ra   s       €rA   rH   zGPT2MLP.__init__æ   s_   ø€ Ü‰ÑÔØ×&Ñ&ˆ	ÜÐ,¨iÓ8ˆŒ	Ü˜YÐ(9Ó:ˆŒÜ˜&×4Ñ4Ñ5ˆŒÜ—z‘z &×"4Ñ"4Ó5ˆ�rC   rt   Nry   c                 óŽ   — | j                  |«      }| j                  |«      }| j                  |«      }| j                  |«      }|S r–   )r¤   r¦   rY   r7   )r`   rt   s     rA   r•   zGPT2MLP.forwardî   s@   € ØŸ	™	 -Ó0ˆØŸ™ Ó/ˆØŸ™ MÓ2ˆØŸ™ ]Ó3ˆØÐrC   )	r˜   r™   rš   rH   r›   r0   rœ   r•   rŸ   r    s   @rA   r¢   r¢   å   s1   ø„ ô6ð U¨5×+<Ñ+<Ñ%=ÀÑ%Dð È×IZÑIZ÷ rC   r¢   c                   óÞ   ‡ — e Zd Zdˆ fd„	Z	 	 	 	 	 ddeej                     dz  dedz  dej                  dz  dej                  dz  dej                  dz  de	dz  d	ej                  fd
„Z
ˆ xZS )Ú	GPT2BlockNc                 óÜ  •— t         ‰| �  «        |j                  }|j                  �|j                  nd|z  }t	        j
                  ||j                  ¬«      | _        t        ||¬«      | _	        t	        j
                  ||j                  ¬«      | _
        |j                  r9t        |d|¬«      | _        t	        j
                  ||j                  ¬«      | _        t        ||«      | _        y )Né   ©Úeps)rI   rU   T)rI   rT   rU   )rG   rH   rJ   Ún_innerr   Ú	LayerNormÚlayer_norm_epsilonÚln_1rE   ÚattnÚln_2Úadd_cross_attentionÚcrossattentionÚln_cross_attnr¢   Úmlp)r`   rI   rU   rJ   Ú	inner_dimra   s        €rA   rH   zGPT2Block.__init__÷   s²   ø€ Ü‰ÑÔØ×(Ñ(ˆØ&,§n¡nÐ&@�F—N’NÀaÈ+Áoˆ	ä—L‘L °&×2KÑ2KÔLˆŒ	Ü!¨¸9ÔEˆŒ	Ü—L‘L °&×2KÑ2KÔLˆŒ	à×%Ò%Ü"/°vÐRVÐbkÔ"lˆDÔÜ!#§¡¨k¸v×?XÑ?XÔ!YˆDÔä˜9 fÓ-ˆ�rC   rt   ru   r<   rv   rw   Ú	use_cachery   c                 óX  — |}| j                  |«      } | j                  |f|||dœ|¤Ž\  }	}
|	|z   }|�Lt        | d«      st        d| › d�«      ‚|}| j	                  |«      }| j                  |||||¬«      \  }}
||z   }|}| j                  |«      }| j                  |«      }||z   }|S )N)ru   r<   rº   r¶   z'If `encoder_hidden_states` are passed, z` has to be instantiated with cross-attention layers by setting `config.add_cross_attention=True`)ru   r<   rv   rw   )r²   r³   r‚   rP   r·   r¶   r´   r¸   )r`   rt   ru   r<   rv   rw   rº   r>   Úresidualr@   ro   Úcross_attn_outputÚfeed_forward_hidden_statess                rA   r•   zGPT2Block.forward  s  € ð !ˆØŸ	™	 -Ó0ˆØ"˜Ÿ™Øð
à+Ø)Øñ	
ð
 ñ
‰ˆ�Qð $ hÑ.ˆà Ð,ä˜4Ð!1Ô2Ü Ø=¸d¸Vð DZð Zóð ð %ˆHØ ×.Ñ.¨}Ó=ˆMØ#'×#6Ñ#6ØØ /Ø-Ø&;Ø'=ð $7ó $Ñ Ð˜qð %Ð'8Ñ8ˆMà ˆØŸ	™	 -Ó0ˆØ%)§X¡X¨mÓ%<Ð"à Ð#=Ñ=ˆàÐrC   r–   r—   )r˜   r™   rš   rH   r›   r0   rœ   r   r�   rž   r•   rŸ   r    s   @rA   rª   rª   ö   s�   ø„ õ.ð$ )-Ø37Ø59Ø;?Ø!&ñ/à˜U×.Ñ.Ñ/°$Ñ6ð/ð  ™ð/ð ×)Ñ)¨DÑ0ð	/ð
  %Ÿ|™|¨dÑ2ð/ð !&× 1Ñ 1°DÑ 8ð/ð ˜$‘;ð/ð 
�‰÷/rC   rª   c                   ó‚   ‡ — e Zd ZdZdefˆ fd„Z	 d	dej                  dej                  dz  dej                  fd„Z	ˆ xZ
S )
ÚGPT2SequenceSummaryaÌ  
    Compute a single vector summary of a sequence hidden states.

    Args:
        config ([`GPT2Config`]):
            The config used by the model. Relevant arguments in the config class of the model are (refer to the actual
            config class of your model for the default values it uses):

            - **summary_type** (`str`) -- The method to use to make this summary. Accepted values are:

                - `"last"` -- Take the last token hidden state (like XLNet)
                - `"first"` -- Take the first token hidden state (like Bert)
                - `"mean"` -- Take the mean of all tokens hidden states
                - `"cls_index"` -- Supply a Tensor of classification token position (GPT/GPT-2)
                - `"attn"` -- Not implemented now, use multi-head attention

            - **summary_use_proj** (`bool`) -- Add a projection after the vector extraction.
            - **summary_proj_to_labels** (`bool`) -- If `True`, the projection outputs to `config.num_labels` classes
              (otherwise to `config.hidden_size`).
            - **summary_activation** (`Optional[str]`) -- Set to `"tanh"` to add a tanh activation to the output,
              another string or `None` will add no activation.
            - **summary_first_dropout** (`float`) -- Optional dropout probability before the projection and activation.
            - **summary_last_dropout** (`float`)-- Optional dropout probability after the projection and activation.
    rI   c                 óf  •— t         ‰| �  «        t        |dd«      | _        | j                  dk(  rt        ‚t        j                  «       | _        t        |d«      rq|j                  ret        |d«      r(|j                  r|j                  dkD  r|j                  }n|j                  }t        j                  |j                  |«      | _        t        |dd «      }|rt        |«      nt        j                  «       | _        t        j                  «       | _        t        |d«      r3|j"                  dkD  r$t        j$                  |j"                  «      | _        t        j                  «       | _        t        |d	«      r5|j(                  dkD  r%t        j$                  |j(                  «      | _        y y y )
NÚsummary_typeÚlastr³   Úsummary_use_projÚsummary_proj_to_labelsr   Úsummary_activationÚsummary_first_dropoutÚsummary_last_dropout)rG   rH   ÚgetattrrÂ   ÚNotImplementedErrorr   ÚIdentityÚsummaryr‚   rÄ   rÅ   Ú
num_labelsrJ   ÚLinearr   Ú
activationÚfirst_dropoutrÇ   rZ   Úlast_dropoutrÈ   )r`   rI   Únum_classesÚactivation_stringra   s       €rA   rH   zGPT2SequenceSummary.__init__S  sU  ø€ Ü‰ÑÔä# F¨N¸FÓCˆÔØ×Ñ Ò&ô &Ð%ä—{‘{“}ˆŒÜ�6Ð-Ô.°6×3JÒ3JÜ�vÐ7Ô8¸V×=ZÒ=ZÐ_e×_pÑ_pÐstÒ_tØ$×/Ñ/‘à$×0Ñ0�ÜŸ9™9 V×%7Ñ%7¸ÓEˆDŒLä# FÐ,@À$ÓGÐÙIZ¤NÐ3DÔ$EÔ`b×`kÑ`kÓ`mˆŒäŸ[™[›]ˆÔÜ�6Ð2Ô3¸×8TÑ8TÐWXÒ8XÜ!#§¡¨F×,HÑ,HÓ!IˆDÔäŸK™K›MˆÔÜ�6Ð1Ô2°v×7RÑ7RÐUVÒ7VÜ "§
¡
¨6×+FÑ+FÓ GˆDÕð 8WÐ2rC   Nrt   Ú	cls_indexry   c                 óü  — | j                   dk(  r|dd…df   }�n| j                   dk(  r|dd…df   }�n| j                   dk(  r|j                  d¬«      }ná| j                   d	k(  r½|€At        j                  |d
dd…dd…f   |j                  d   dz
  t        j
                  ¬«      }nX|j                  d«      j                  d«      }|j                  d|j                  «       dz
  z  |j                  d«      fz   «      }|j                  d|«      j                  d«      }n| j                   dk(  rt        ‚| j                  «      }| j                  |«      }| j                  |«      }| j!                  |«      }|S )ak  
        Compute a single vector summary of a sequence hidden states.

        Args:
            hidden_states (`torch.FloatTensor` of shape `[batch_size, seq_len, hidden_size]`):
                The hidden states of the last layer.
            cls_index (`torch.LongTensor` of shape `[batch_size]` or `[batch_size, ...]` where ... are optional leading dimensions of `hidden_states`, *optional*):
                Used if `summary_type == "cls_index"` and takes the last token of the sequence as classification token.

        Returns:
            `torch.FloatTensor`: The summary of the sequence hidden states.
        rÃ   Nr'   Úfirstr   Úmeanr$   r*   rÔ   .r)   )r6   ©r'   r³   )rÂ   r×   r0   Ú	full_liker‡   ÚlongÚ	unsqueezeÚexpandr+   r/   ÚgatherÚsqueezerÊ   rÐ   rÌ   rÏ   rÑ   )r`   rt   rÔ   Úoutputs       rA   r•   zGPT2SequenceSummary.forwardp  sn  € ð ×Ñ Ò&Ø"¢1 b 5Ñ)ŠFØ×Ñ 'Ò)Ø"¢1 a 4Ñ(ŠFØ×Ñ &Ò(Ø"×'Ñ'¨AÐ'Ó.‰FØ×Ñ +Ò-ØÐ Ü!ŸO™OØ! # r¨ rª1 *Ñ-Ø!×'Ñ'¨Ñ+¨aÑ/ÜŸ*™*ô‘	ð &×/Ñ/°Ó3×=Ñ=¸bÓA�	Ø%×,Ñ,¨U°i·m±m³oÈÑ6IÑ-JÈm×N`ÑN`ÐacÓNdÐMfÑ-fÓg�	à"×)Ñ)¨"¨iÓ8×@Ñ@ÀÓD‰FØ×Ñ &Ò(Ü%Ð%à×#Ñ# FÓ+ˆØ—‘˜fÓ%ˆØ—‘ Ó(ˆØ×"Ñ" 6Ó*ˆàˆrC   r–   )r˜   r™   rš   Ú__doc__r%   rH   r0   rœ   Ú
LongTensorr•   rŸ   r    s   @rA   rÀ   rÀ   9  sQ   ø„ ñð2H˜zõ Hð< VZñ)Ø"×.Ñ.ð)Ø;@×;KÑ;KÈdÑ;Rð)à	×	Ñ	÷)rC   rÀ   c                   ó®   ‡ — e Zd ZU eed<   dZdZdgZdgZdZ	dZ
dZdZe eedd¬«       eed	d¬«      d
œZddgZ ej&                  «       ˆ fd„«       Zˆ xZS )ÚGPT2PreTrainedModelrI   ÚtransformerTrª   ru   z.attnr$   )Ú
layer_nameÚindexz.crossattention)rt   Ú
attentionsÚcross_attentionsz	attn.biaszcrossattention.biasc           
      ó  •— t         ‰| �  |«       t        |t        «      rat	        j
                  |j                  d| j                  j                  ¬«       |j                  �t	        j                  |j                  «       t        |t        «      rx|j                  «       D ]d  \  }}|dk(  sŒt	        j
                  |d| j                  j                  t        j                  d| j                  j                  z  «      z  ¬«       Œf yy)zInitialize the weights.r|   )r×   ÚstdNzc_proj.weightr.   )rG   Ú_init_weightsr}   r   ÚinitÚnormal_ÚweightrI   Úinitializer_rangeÚbiasÚzeros_r   Únamed_parametersÚmathÚsqrtÚn_layer)r`   r8   Únamer,   ra   s       €rA   rë   z!GPT2PreTrainedModel._init_weights°  sÉ   ø€ ô 	‰Ñ˜fÔ%Ü�fœfÔ%Ü�L‰L˜Ÿ™¨S°d·k±k×6SÑ6SÕTØ�{‰{Ð&Ü—‘˜FŸK™KÔ(ô �fœoÔ.Ø!×2Ñ2Ó4ò v‘��aØ˜?Ó*ä—L‘L ¨°$·+±+×2OÑ2OÔRV×R[ÑR[Ð\]Ð`d×`kÑ`k×`sÑ`sÑ\sÓRtÑ2tÖuñvð /rC   )r˜   r™   rš   r%   Ú__annotations__Úbase_model_prefixÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_skip_keys_device_placementÚ_supports_flash_attnÚ_supports_sdpaÚ_supports_attention_backendÚ_can_compile_fullgraphrª   r"   rE   Ú_can_record_outputsÚ"_keys_to_ignore_on_load_unexpectedr0   Úno_gradrë   rŸ   r    s   @rA   rã   rã   œ  sŒ   ø… àÓØ%ÐØ&*Ð#Ø$˜ÐØ#4Ð"5ÐØÐØ€NØ"&ÐØ!Ðà"Ù$ ]¸wÈaÔPÙ*¨=ÐEVÐ^_Ô`ñÐð +6Ð7LÐ)MÐ&à€U‡]�]ƒ_óvó ôvrC   rã   z^
    Base class for outputs of models predicting if two sentences are consecutive or not.
    )Úcustom_introc                   ó"  — e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	ej                  dz  ed<   dZ
ej                  dz  ed<   dZedz  ed<   dZeej                     dz  ed<   dZeej                     dz  ed	<   y)
ÚGPT2DoubleHeadsModelOutputa\  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
        Language modeling loss.
    mc_loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `mc_labels` is provided):
        Multiple choice classification loss.
    logits (`torch.FloatTensor` of shape `(batch_size, num_choices, sequence_length, config.vocab_size)`):
        Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
    mc_logits (`torch.FloatTensor` of shape `(batch_size, num_choices)`):
        Prediction scores of the multiple choice classification head (scores for each choice before SoftMax).
    past_key_values (`Cache`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
        It is a [`~cache_utils.Cache`] instance. For more details, see our [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache).

        Contains pre-computed hidden-states (key and values in the attention blocks) that can be used (see
        `past_key_values` input) to speed up sequential decoding.
    NÚlossÚmc_lossÚlogitsÚ	mc_logitsru   rt   rç   )r˜   r™   rš   rà   r  r0   rœ   r÷   r  r  r	  ru   r   rt   r›   rç   © rC   rA   r  r  Æ  sŸ   … ñð  &*€Dˆ%×
Ñ
˜dÑ
"Ó)Ø(,€GˆU×Ñ Ñ%Ó,Ø'+€FˆE×Ñ Ñ$Ó+Ø*.€Iˆu× Ñ  4Ñ'Ó.Ø$(€O�U˜T‘\Ó(Ø59€M�5˜×*Ñ*Ñ+¨dÑ2Ó9Ø26€J��e×'Ñ'Ñ(¨4Ñ/Ô6rC   r  c                   óN  ‡ — e Zd Zˆ fd„Zd„ Zd„ Zeee	 	 	 	 	 	 	 	 	 dde	j                  dz  dedz  de	j                  dz  de	j                  dz  d	e	j                  dz  d
e	j                  dz  de	j                  dz  de	j                  dz  dedz  defd„«       «       «       Zˆ xZS )Ú	GPT2Modelc           	      ó�  •— t         ‰| �  |«       |j                  | _        t	        j
                  |j                  | j                  «      | _        t	        j
                  |j                  | j                  «      | _	        t	        j                  |j                  «      | _        t	        j                  t        |j                  «      D �cg c]  }t!        ||¬«      ‘Œ c}«      | _        t	        j$                  | j                  |j&                  ¬«      | _        d| _        |j,                  | _        | j/                  «        y c c}w )N)rU   r­   F)rG   rH   rJ   rK   r   Ú	EmbeddingÚ
vocab_sizeÚwteÚmax_position_embeddingsÚwperZ   Ú
embd_pdropÚdropÚ
ModuleListÚrangeÚnum_hidden_layersrª   Úhr°   r±   Úln_fÚgradient_checkpointingrŠ   Ú	post_init)r`   rI   Úira   s      €rA   rH   zGPT2Model.__init__è  sÜ   ø€ Ü‰Ñ˜Ô à×+Ñ+ˆŒä—<‘< × 1Ñ 1°4·>±>ÓBˆŒÜ—<‘< × >Ñ >ÀÇÁÓOˆŒä—J‘J˜v×0Ñ0Ó1ˆŒ	Ü—‘ÌÈf×NfÑNfÓHgÖhÀ1¤	¨&¸AÖ >ÒhÓiˆŒÜ—L‘L §¡°V×5NÑ5NÔOˆŒ	à&+ˆÔ#Ø$*×$?Ñ$?ˆÔ!ð 	�‰Õùò  is   Ã
Ec                 ó   — | j                   S r–   ©r  )r`   s    rA   Úget_input_embeddingszGPT2Model.get_input_embeddingsú  s   € Ø�x‰xˆrC   c                 ó   — || _         y r–   r  )r`   Únew_embeddingss     rA   Úset_input_embeddingszGPT2Model.set_input_embeddingsý  s	   € Ø!ˆ�rC   NÚ	input_idsru   r<   Útoken_type_idsÚposition_idsÚinputs_embedsrv   rw   rº   ry   c
                 ó°  — |
j                  dd«       |
j                  dd«       |�|�t        d«      ‚|�G| j                  ||«       |j                  «       }|j	                  d|d   «      }|j
                  d   }n0|�#|j                  «       dd }|j
                  d   }nt        d«      ‚|�|j	                  d|d   «      }|	r^|€t        | j                  ¬«      }| j                  j                  r0t        |t        «      s t        |t        | j                  ¬«      «      }|€| j                  |«      }|€V|�|j                  «       nd}t        j                  |j
                  d	   |j                  ¬
«      |z   }|j!                  d«      }| j#                  |«      }||j%                  |j                  «      z   }|�!|j&                  dk  r|j	                  |d«      }t)        | j                  ||||¬«      }d}|�t+        | j                  |||¬«      }|�| j                  |«      }||z   }| j-                  |«      }d|d	d z   |j                  d«      fz   }t/        | j0                  «      D ]/  \  }} ||| j2                  r| j4                  s|nd||f||	|dœ|
¤Ž}Œ1 | j7                  |«      }|j	                  |«      }|	r|nd}t9        ||¬«      S )á¾  
        input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`):
            `input_ids_length` = `sequence_length` if `past_key_values` is `None` else
            `past_key_values.get_seq_length()` (`sequence_length` of input past key value states). Indices of input
            sequence tokens in the vocabulary.

            If `past_key_values` is used, only `input_ids` that do not have their past calculated should be passed as
            `input_ids`.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        rx   NÚoutput_hidden_stateszDYou cannot specify both input_ids and inputs_embeds at the same timer'   r   z5You have to specify either input_ids or inputs_embeds)rI   r$   ©rc   r¬   )rI   r&  r<   ru   r%  )rI   r&  r<   rv   rØ   )rw   rº   r%  )Úlast_hidden_stateru   )ÚpoprP   Ú%warn_if_padding_and_no_attention_maskr/   rˆ   r‡   r   rI   rµ   r}   r   r  Úget_seq_lengthr0   Úarangerc   rÛ   r  ÚtoÚndimr   r   r  Ú	enumerater  r  r-   r  r   )r`   r#  ru   r<   r$  r%  r&  rv   rw   rº   r>   Úinput_shapeÚ
batch_sizeÚpast_seen_tokensÚposition_embedsrt   Úcausal_maskÚtoken_type_embedsÚoutput_shaper  Úblocks                        rA   r•   zGPT2Model.forward   s  € ð< 	�
‰
Ð&¨Ô-Ø�
‰
Ð)¨4Ô0àÐ  ]Ð%>ÜÐcÓdÐdØÐ"Ø×6Ñ6°yÀ.ÔQØ#Ÿ.™.Ó*ˆKØ!Ÿ™ r¨;°r©?Ó;ˆIØ"Ÿ™¨Ñ+‰JØÐ&Ø'×,Ñ,Ó.¨s°Ð3ˆKØ&×,Ñ,¨QÑ/‰JäÐTÓUÐUàÐ%Ø+×0Ñ0°°[À±_ÓEˆNñ ØÐ&Ü".°d·k±kÔ"B�à�{‰{×.Ò.´zÀ/ÔSfÔ7gÜ"5°oÄ|Ð[_×[fÑ[fÔGgÓ"h�àÐ Ø ŸH™H YÓ/ˆMàÐØCRÐC^˜×=Ñ=Ô?ÐdeÐÜ Ÿ<™<¨×(;Ñ(;¸AÑ(>À}×G[ÑG[Ô\Ð_oÑoˆLØ'×1Ñ1°!Ó4ˆLàŸ(™( <Ó0ˆØ%¨×(:Ñ(:¸=×;OÑ;OÓ(PÑPˆð Ð%¨.×*=Ñ*=ÀÒ*AØ+×0Ñ0°¸RÓ@ˆNä(Ø—;‘;Ø'Ø)Ø+Ø%ô
ˆð "&ÐØ Ð,Ü%>Ø—{‘{Ø+Ø5Ø&;ô	&Ð"ð Ð%Ø $§¡¨Ó 8ÐØ)Ð,=Ñ=ˆMàŸ	™	 -Ó0ˆà˜{¨1¨2˜Ñ.°-×2DÑ2DÀRÓ2HÐ1JÑJˆä! $§&¡&Ó)ò 
	‰HˆAˆuÙ!ØØ(,×(CÒ(CÈÏÊ‘Ð\`ØØ%ð		ð
 (>Ø#Ø)ñ	ð ñ	‰Mð
	ð Ÿ	™	 -Ó0ˆà%×*Ñ*¨<Ó8ˆá-6™/¸DˆÜ8Ø+Ø+ô
ð 	
rC   )	NNNNNNNNN)r˜   r™   rš   rH   r  r"  r!   r#   r   r0   rá   r   rœ   r�   rž   r   r•   rŸ   r    s   @rA   r  r  æ  s  ø„ ôò$ò"ð  ØØð .2Ø(,Ø37Ø26Ø04Ø26Ø59Ø;?Ø!%ñr
à×#Ñ# dÑ*ðr
ð  ™ðr
ð ×)Ñ)¨DÑ0ð	r
ð
 ×(Ñ(¨4Ñ/ðr
ð ×&Ñ&¨Ñ-ðr
ð ×(Ñ(¨4Ñ/ðr
ð  %Ÿ|™|¨dÑ2ðr
ð !&× 1Ñ 1°DÑ 8ðr
ð ˜$‘;ðr
ð 
3òr
ó ó ó  ôr
rC   r  z‡
    The GPT2 Model transformer with a language modeling head on top (linear layer with weights tied to the input
    embeddings).
    c                   ó€  ‡ — e Zd ZddiZˆ fd„Zee	 	 	 	 	 	 	 	 	 	 	 ddej                  dz  de	dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dej                  dz  dej                  dz  dej                  dz  dedz  deej                  z  defd„«       «       Zˆ xZS )ÚGPT2LMHeadModelúlm_head.weightútransformer.wte.weightc                 óÆ   •— t         ‰| �  |«       t        |«      | _        t	        j
                  |j                  |j                  d¬«      | _        | j                  «        y ©NF©rð   )
rG   rH   r  rä   r   rÎ   Ún_embdr  Úlm_headr  ©r`   rI   ra   s     €rA   rH   zGPT2LMHeadModel.__init__�  sG   ø€ Ü‰Ñ˜Ô Ü$ VÓ,ˆÔÜ—y‘y §¡°×0AÑ0AÈÔNˆŒð 	�‰ÕrC   Nr#  ru   r<   r$  r%  r&  rv   rw   Úlabelsrº   Úlogits_to_keepry   c                 ó”  —  | j                   |f||||||||
dœ|¤Ž}|j                  }t        |t        «      rt	        | d«      n|}| j                  |dd…|dd…f   «      }d}|	�* | j                  ||	fd| j                  j                  i|¤Ž}t        |||j                  |j                  |j                  |j                  ¬«      S )ai  
        input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`):
            `input_ids_length` = `sequence_length` if `past_key_values` is `None` else
            `past_key_values.get_seq_length()` (`sequence_length` of input past key value states). Indices of input
            sequence tokens in the vocabulary.

            If `past_key_values` is used, only `input_ids` that do not have their past calculated should be passed as
            `input_ids`.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        labels (`torch.LongTensor` of shape `(batch_size, input_ids_length)`, *optional*):
            Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
            `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
            are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`
        )ru   r<   r$  r%  r&  rv   rw   rº   Nr  )r  r  ru   rt   rç   rè   )rä   r+  r}   ÚintÚslicerC  Úloss_functionrI   r  r   ru   rt   rç   rè   )r`   r#  ru   r<   r$  r%  r&  rv   rw   rE  rº   rF  r>   Útransformer_outputsrt   Úslice_indicesr  r  s                     rA   r•   zGPT2LMHeadModel.forward‰  s  € ðF JZÈ×IYÑIYØðJ
à+Ø)Ø)Ø%Ø'Ø"7Ø#9ØñJ
ð ñJ
Ðð ,×=Ñ=ˆä8BÀ>ÔSVÔ8Wœ˜~˜o¨tÔ4Ð]kˆØ—‘˜mªA¨}ºaÐ,?Ñ@ÓAˆàˆØÐà%�4×%Ñ%ØØñð  Ÿ;™;×1Ñ1ðð ñ	ˆDô 1ØØØ/×?Ñ?Ø-×;Ñ;Ø*×5Ñ5Ø0×AÑAô
ð 	
rC   )NNNNNNNNNNr   )r˜   r™   rš   Ú_tied_weights_keysrH   r   r   r0   rá   r   rœ   r�   rž   rH  r   r•   rŸ   r    s   @rA   r<  r<  x  s@  ø„ ð +Ð,DÐEÐôð Øð .2Ø(,Ø37Ø26Ø04Ø26Ø59Ø;?Ø*.Ø!%Ø-.ñC
à×#Ñ# dÑ*ðC
ð  ™ðC
ð ×)Ñ)¨DÑ0ð	C
ð
 ×(Ñ(¨4Ñ/ðC
ð ×&Ñ&¨Ñ-ðC
ð ×(Ñ(¨4Ñ/ðC
ð  %Ÿ|™|¨dÑ2ðC
ð !&× 1Ñ 1°DÑ 8ðC
ð × Ñ  4Ñ'ðC
ð ˜$‘;ðC
ð ˜eŸl™lÑ*ðC
ð 
+òC
ó ó ôC
rC   r<  a  
        The GPT2 Model transformer with a language modeling and a multiple-choice classification head on top e.g. for
    RocStories/SWAG tasks. The two heads are two linear layers. The language modeling head has its weights tied to the
    input embeddings, the classification head takes as input the input of a specified classification token index in the
    input sequence).
    c                   ó`  ‡ — e Zd ZddiZˆ fd„Zee	 	 	 	 	 	 	 	 	 	 ddej                  dz  de	dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dej                  dz  dej                  dz  dej                  dz  dedz  defd„«       «       Zˆ xZS )ÚGPT2DoubleHeadsModelr=  r>  c                 óô   •— t         ‰| �  |«       d|_        t        |«      | _        t        j                  |j                  |j                  d¬«      | _	        t        |«      | _        | j                  «        y )Nr$   FrA  )rG   rH   rÍ   r  rä   r   rÎ   rB  r  rC  rÀ   Úmultiple_choice_headr  rD  s     €rA   rH   zGPT2DoubleHeadsModel.__init__Ü  s\   ø€ Ü‰Ñ˜Ô ØˆÔÜ$ VÓ,ˆÔÜ—y‘y §¡°×0AÑ0AÈÔNˆŒÜ$7¸Ó$?ˆÔ!ð 	�‰ÕrC   Nr#  ru   r<   r$  r%  r&  Úmc_token_idsrE  Ú	mc_labelsrº   ry   c           
      ó¾  —  | j                   |f||||||
dœ|¤Ž}|j                  }| j                  |«      }| j                  ||«      j	                  d«      }d}|	�At        «       } ||j                  d|j                  d«      «      |	j                  d«      «      }d}|��|j                  |j                  «      }|ddd…dd…f   j                  «       }|ddd…f   j                  «       }t        «       } ||j                  d|j                  d«      «      |j                  d«      «      }t        |||||j                  |j                  |j                  ¬«      S )af  
        input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`):
            `input_ids_length` = `sequence_length` if `past_key_values` is `None` else
            `past_key_values.get_seq_length()` (`sequence_length` of input past key value states). Indices of input
            sequence tokens in the vocabulary.

            If `past_key_values` is used, only `input_ids` that do not have their past calculated should be passed as
            `input_ids`.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        mc_token_ids (`torch.LongTensor` of shape `(batch_size, num_choices)`, *optional*, default to index of the last token of the input):
            Index of the classification token in each input sequence. Selected in the range `[0, input_ids.size(-1) -
            1]`.
        labels (`torch.LongTensor` of shape `(batch_size, input_ids_length)`, *optional*):
            Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
            `labels = input_ids`. Indices are selected in `[-100, 0, ..., config.vocab_size - 1]`. All labels set to
            `-100` are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size - 1]`
        mc_labels (`torch.LongTensor` of shape `(batch_size)`, *optional*):
            Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]`
            where *num_choices* is the size of the second dimension of the input tensors. (see *input_ids* above)

        Example:

        ```python
        >>> import torch
        >>> from transformers import AutoTokenizer, GPT2DoubleHeadsModel

        >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
        >>> model = GPT2DoubleHeadsModel.from_pretrained("openai-community/gpt2")

        >>> # Add a [CLS] to the vocabulary (we should train it also!)
        >>> num_added_tokens = tokenizer.add_special_tokens({"cls_token": "[CLS]"})
        >>> # Update the model embeddings with the new vocabulary size
        >>> embedding_layer = model.resize_token_embeddings(len(tokenizer))

        >>> choices = ["Hello, my dog is cute [CLS]", "Hello, my cat is cute [CLS]"]
        >>> encoded_choices = [tokenizer.encode(s) for s in choices]
        >>> cls_token_location = [tokens.index(tokenizer.cls_token_id) for tokens in encoded_choices]

        >>> input_ids = torch.tensor(encoded_choices).unsqueeze(0)  # Batch size: 1, number of choices: 2
        >>> mc_token_ids = torch.tensor([cls_token_location])  # Batch size: 1

        >>> outputs = model(input_ids, mc_token_ids=mc_token_ids)
        >>> lm_logits = outputs.logits
        >>> mc_logits = outputs.mc_logits
        ```©ru   r<   r$  r%  r&  rº   r'   N.r$   )r  r  r  r	  ru   rt   rç   )rä   r+  rC  rQ  rÞ   r   rˆ   r/   r0  rc   rŒ   r  ru   rt   rç   )r`   r#  ru   r<   r$  r%  r&  rR  rE  rS  rº   r>   rK  rt   Ú	lm_logitsr	  r  Úloss_fctÚlm_lossÚshift_logitsÚshift_labelss                        rA   r•   zGPT2DoubleHeadsModel.forwardæ  sw  € ðB JZÈ×IYÑIYØð	J
à+Ø)Ø)Ø%Ø'Øñ	J
ð ñ	J
Ðð ,×=Ñ=ˆà—L‘L Ó/ˆ	Ø×-Ñ-¨m¸\ÓJ×RÑRÐSUÓVˆ	àˆØÐ Ü'Ó)ˆHÙ˜yŸ~™~¨b°)·.±.ÀÓ2DÓEÀyÇ~Á~ÐVXÓGYÓZˆGØˆØÐØ—Y‘Y˜y×/Ñ/Ó0ˆFØ$ S¨#¨2¨#ªq [Ñ1×<Ñ<Ó>ˆLØ! # q¡r '™?×5Ñ5Ó7ˆLÜ'Ó)ˆHÙ˜|×0Ñ0°°\×5FÑ5FÀrÓ5JÓKÈ\×M^ÑM^Ð_aÓMbÓcˆGä)ØØØØØ/×?Ñ?Ø-×;Ñ;Ø*×5Ñ5ô
ð 	
rC   )
NNNNNNNNNN)r˜   r™   rš   rM  rH   r   r   r0   rá   r   rœ   rž   r  r•   rŸ   r    s   @rA   rO  rO  Ñ  s+  ø„ ð +Ð,DÐEÐôð Øð .2Ø(,Ø37Ø26Ø04Ø26Ø04Ø*.Ø-1Ø!%ñc
à×#Ñ# dÑ*ðc
ð  ™ðc
ð ×)Ñ)¨DÑ0ð	c
ð
 ×(Ñ(¨4Ñ/ðc
ð ×&Ñ&¨Ñ-ðc
ð ×(Ñ(¨4Ñ/ðc
ð ×&Ñ&¨Ñ-ðc
ð × Ñ  4Ñ'ðc
ð ×#Ñ# dÑ*ðc
ð ˜$‘;ðc
ð 
$òc
ó ó ôc
rC   rO  aÔ  
    The GPT2 Model transformer with a sequence classification head on top (linear layer).

    [`GPT2ForSequenceClassification`] uses the last token in order to do the classification, as other causal models
    (e.g. GPT-1) do.

    Since it does classification on the last token, it requires to know the position of the last token. If a
    `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
    no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
    padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
    each row of the batch).
    c                   ó  ‡ — e Zd Zˆ fd„Zee	 	 	 	 	 	 	 	 ddej                  dz  dedz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
e
dz  defd„«       «       Zˆ xZS )ÚGPT2ForSequenceClassificationc                 óè   •— t         ‰| �  |«       |j                  | _        t        |«      | _        t        j                  |j                  | j                  d¬«      | _        | j                  «        y r@  )
rG   rH   rÍ   r  rä   r   rÎ   rB  Úscorer  rD  s     €rA   rH   z&GPT2ForSequenceClassification.__init__]  sR   ø€ Ü‰Ñ˜Ô Ø ×+Ñ+ˆŒÜ$ VÓ,ˆÔÜ—Y‘Y˜vŸ}™}¨d¯o©oÀEÔJˆŒ
ð 	�‰ÕrC   Nr#  ru   r<   r$  r%  r&  rE  rº   ry   c	           
      ó‚  —  | j                   |f||||||dœ|	¤Ž}
|
j                  }| j                  |«      }|�|j                  dd \  }}n|j                  dd \  }}| j                  j
                  €|dk7  rt        d«      ‚| j                  j
                  €d}nÃ|�“|| j                  j
                  k7  j                  |j                  t        j                  «      }t        j                  |j                  d   |j                  t        j                  ¬«      }||z  j                  d«      }n.d}t        j                  | j                  j                   › d�«       |t        j                  ||j                  ¬	«      |f   }d}|��‡| j                  j"                  €�| j$                  dk(  rd
| j                  _        nl| j$                  dkD  rL|j&                  t        j(                  k(  s|j&                  t        j*                  k(  rd| j                  _        nd| j                  _        | j                  j"                  d
k(  rIt-        «       }| j$                  dk(  r& ||j/                  «       |j/                  «       «      }nŒ |||«      }n‚| j                  j"                  dk(  r=t1        «       } ||j3                  d| j$                  «      |j3                  d«      «      }n,| j                  j"                  dk(  rt5        «       } |||«      }t7        |||
j8                  |
j:                  |
j<                  ¬«      S )aB  
        input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`):
            `input_ids_length` = `sequence_length` if `past_key_values` is `None` else
            `past_key_values.get_seq_length()` (`sequence_length` of input past key value states). Indices of input
            sequence tokens in the vocabulary.

            If `past_key_values` is used, only `input_ids` that do not have their past calculated should be passed as
            `input_ids`.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        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 regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        rU  Nr.   r$   z=Cannot handle batch sizes > 1 if no padding token is defined.r'   )rc   r6   zŠ will not detect padding tokens in `inputs_embeds`. Results may be unexpected if using padding tokens in conjunction with `inputs_embeds.`r*  Ú
regressionÚsingle_label_classificationÚmulti_label_classification)r  r  ru   rt   rç   )rä   r+  r^  r‡   rI   Úpad_token_idrP   r0  rc   r0   Úint32r/  ÚargmaxÚloggerÚwarning_oncera   r˜   Úproblem_typerÍ   r6   rÚ   rH  r   rÞ   r   rˆ   r   r   ru   rt   rç   )r`   r#  ru   r<   r$  r%  r&  rE  rº   r>   rK  rt   r  r4  Úsequence_lengthÚlast_non_pad_tokenÚnon_pad_maskÚtoken_indicesÚpooled_logitsr  rW  s                        rA   r•   z%GPT2ForSequenceClassification.forwardf  sà  € ð@ JZÈ×IYÑIYØð	J
à+Ø)Ø)Ø%Ø'Øñ	J
ð ñ	J
Ðð ,×=Ñ=ˆØ—‘˜MÓ*ˆàÐ Ø*3¯/©/¸"¸1Ð*=Ñ'ˆJ™à*7×*=Ñ*=¸b¸qÐ*AÑ'ˆJ˜à�;‰;×#Ñ#Ð+°
¸a²ÜÐ\Ó]Ð]Ø�;‰;×#Ñ#Ð+Ø!#ÑØÐ"à%¨¯©×)AÑ)AÑA×EÑEÀfÇmÁmÔUZ×U`ÑU`ÓaˆLÜ!ŸL™L¨¯©¸Ñ)<ÀVÇ]Á]ÔZ_×ZeÑZeÔfˆMØ"/°,Ñ">×!FÑ!FÀrÓ!JÑà!#ÐÜ×ÑØ—>‘>×*Ñ*Ð+ð ,Zð Zôð
 œuŸ|™|¨J¸v¿}¹}ÔMÐOaÐaÑbˆàˆØÑØ�{‰{×'Ñ'Ð/Ø—?‘? aÒ'Ø/;�D—K‘KÕ,Ø—_‘_ qÒ(¨f¯l©l¼e¿j¹jÒ.HÈFÏLÉLÔ\a×\eÑ\eÒLeØ/L�D—K‘KÕ,à/K�D—K‘KÔ,à�{‰{×'Ñ'¨<Ò7Ü"›9�Ø—?‘? aÒ'Ù# M×$9Ñ$9Ó$;¸V¿^¹^Ó=MÓN‘Dá# M°6Ó:‘DØ—‘×)Ñ)Ð-JÒJÜ+Ó-�Ù × 2Ñ 2°2°t·±Ó GÈÏÉÐUWËÓY‘Ø—‘×)Ñ)Ð-IÒIÜ,Ó.�Ù ¨vÓ6�Ü/ØØ Ø/×?Ñ?Ø-×;Ñ;Ø*×5Ñ5ô
ð 	
rC   ©NNNNNNNN)r˜   r™   rš   rH   r   r   r0   rá   r   rœ   rž   r   r•   rŸ   r    s   @rA   r\  r\  N  së   ø„ ôð Øð .2Ø(,Ø37Ø26Ø04Ø26Ø*.Ø!%ñ^
à×#Ñ# dÑ*ð^
ð  ™ð^
ð ×)Ñ)¨DÑ0ð	^
ð
 ×(Ñ(¨4Ñ/ð^
ð ×&Ñ&¨Ñ-ð^
ð ×(Ñ(¨4Ñ/ð^
ð × Ñ  4Ñ'ð^
ð ˜$‘;ð^
ð 
*ò^
ó ó ô^
rC   r\  c                   ó  ‡ — e Zd Zˆ fd„Zee	 	 	 	 	 	 	 	 ddej                  dz  dedz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
e
dz  defd„«       «       Zˆ xZS )ÚGPT2ForTokenClassificationc                 ó°  •— t         ‰| �  |«       |j                  | _        t        |«      | _        t        |d«      r|j                  �|j                  }n't        |d«      r|j                  �|j                  }nd}t        j                  |«      | _
        t        j                  |j                  |j                  «      | _        | j                  «        y )NÚclassifier_dropoutÚhidden_dropoutgš™™™™™¹?)rG   rH   rÍ   r  rä   r‚   rr  rs  r   rZ   r7   rÎ   rJ   Ú
classifierr  )r`   rI   rr  ra   s      €rA   rH   z#GPT2ForTokenClassification.__init__Ë  s¯   ø€ Ü‰Ñ˜Ô Ø ×+Ñ+ˆŒä$ VÓ,ˆÔÜ�6Ð/Ô0°V×5NÑ5NÐ5ZØ!'×!:Ñ!:ÑÜ�VÐ-Ô.°6×3HÑ3HÐ3TØ!'×!6Ñ!6Ñà!$ÐÜ—z‘zÐ"4Ó5ˆŒÜŸ)™) F×$6Ñ$6¸×8IÑ8IÓJˆŒð 	�‰ÕrC   Nr#  ru   r<   r$  r%  r&  rE  rº   ry   c	           
      óŽ  —  | j                   |f||||||dœ|	¤Ž}
|
j                  }| j                  |«      }| j                  |«      }d}|�W|j	                  |j
                  «      }t        «       } ||j                  d| j                  «      |j                  d«      «      }t        |||
j                  |
j                  ¬«      S )aR  
        input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`):
            `input_ids_length` = `sequence_length` if `past_key_values` is `None` else
            `past_key_values.get_seq_length()` (`sequence_length` of input past key value states). Indices of input
            sequence tokens in the vocabulary.

            If `past_key_values` is used, only `input_ids` that do not have their past calculated should be passed as
            `input_ids`.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        rU  Nr'   )r  r  rt   rç   )rä   r+  r7   rt  r0  rc   r   rˆ   rÍ   r   rt   rç   )r`   r#  ru   r<   r$  r%  r&  rE  rº   r>   rK  rt   r  r  rW  s                  rA   r•   z"GPT2ForTokenClassification.forwardÜ  s×   € ð@ JZÈ×IYÑIYØð	J
à+Ø)Ø)Ø%Ø'Øñ	J
ð ñ	J
Ðð ,×=Ñ=ˆØŸ™ ]Ó3ˆØ—‘ Ó/ˆàˆØÐØ—Y‘Y˜vŸ}™}Ó-ˆFÜ'Ó)ˆHÙ˜FŸK™K¨¨D¯O©OÓ<¸f¿k¹kÈ"»oÓNˆDä$ØØØ-×;Ñ;Ø*×5Ñ5ô	
ð 	
rC   rn  )r˜   r™   rš   rH   r   r   r0   rá   r   rœ   rž   r   r•   rŸ   r    s   @rA   rp  rp  É  sà   ø„ ôð" Øð .2Ø(,Ø37Ø26Ø04Ø26Ø*.Ø!%ñ8
à×#Ñ# dÑ*ð8
ð  ™ð8
ð ×)Ñ)¨DÑ0ð	8
ð
 ×(Ñ(¨4Ñ/ð8
ð ×&Ñ&¨Ñ-ð8
ð ×(Ñ(¨4Ñ/ð8
ð × Ñ  4Ñ'ð8
ð ˜$‘;ð8
ð 
ò8
ó ó ô8
rC   rp  c                   ó   ‡ — e Zd Zˆ fd„Zee	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
e	fd„«       «       Z
ˆ xZS )ÚGPT2ForQuestionAnsweringc                 óÐ   •— t         ‰| �  |«       |j                  | _        t        |«      | _        t        j                  |j                  d«      | _        | j                  «        y )Nr.   )
rG   rH   rÍ   r  rä   r   rÎ   rJ   Ú
qa_outputsr  rD  s     €rA   rH   z!GPT2ForQuestionAnswering.__init__  sN   ø€ Ü‰Ñ˜Ô Ø ×+Ñ+ˆŒÜ$ VÓ,ˆÔÜŸ)™) F×$6Ñ$6¸Ó:ˆŒð 	�‰ÕrC   Nr#  r<   r$  r%  r&  Ústart_positionsÚend_positionsry   c                 ó6  —  | j                   |f||||dœ|¤Ž}	|	j                  }
| j                  |
«      }|j                  dd¬«      \  }}|j	                  d«      j                  «       }|j	                  d«      j                  «       }d}|�é|�çt        |j                  «       «      dkD  r*|j	                  d«      j                  |j                  «      }t        |j                  «       «      dkD  r*|j	                  d«      j                  |j                  «      }|j                  d«      }|j                  d|«      }|j                  d|«      }t        |¬«      } |||«      } |||«      }||z   dz  }t        ||||	j                  |	j                  ¬	«      S )
r(  )r<   r$  r%  r&  r$   r'   r*   Nr   )Úignore_indexr.   )r  Ústart_logitsÚ
end_logitsrt   rç   )rä   r+  ry  r†   rÞ   rŒ   Úlenr/   r0  rc   Úclampr   r   rt   rç   )r`   r#  r<   r$  r%  r&  rz  r{  r>   ÚoutputsÚsequence_outputr  r~  r  Ú
total_lossÚignored_indexrW  Ú
start_lossÚend_losss                      rA   r•   z GPT2ForQuestionAnswering.forward$  s¤  € ð6 >N¸T×=MÑ=MØð>
à)Ø)Ø%Ø'ñ>
ð ñ>
ˆð "×3Ñ3ˆà—‘ Ó1ˆØ#)§<¡<°°r <Ó#:Ñ ˆ�jØ#×+Ñ+¨BÓ/×:Ñ:Ó<ˆØ×'Ñ'¨Ó+×6Ñ6Ó8ˆ
àˆ
ØÐ&¨=Ð+Dä�?×'Ñ'Ó)Ó*¨QÒ.Ø"1×"9Ñ"9¸"Ó"=×"@Ñ"@À×ATÑATÓ"U�Ü�=×%Ñ%Ó'Ó(¨1Ò,Ø -× 5Ñ 5°bÓ 9× <Ñ <¸Z×=NÑ=NÓ O�à(×-Ñ-¨aÓ0ˆMØ-×3Ñ3°A°}ÓEˆOØ)×/Ñ/°°=ÓAˆMä'°]ÔCˆHÙ! ,°Ó@ˆJÙ 
¨MÓ:ˆHØ$ xÑ/°1Ñ4ˆJä+ØØ%Ø!Ø!×/Ñ/Ø×)Ñ)ô
ð 	
rC   )NNNNNNN)r˜   r™   rš   rH   r   r   r0   rá   rœ   r   r•   rŸ   r    s   @rA   rw  rw    sà   ø„ ôð Øð .2Ø37Ø26Ø04Ø26Ø37Ø15ñ@
à×#Ñ# dÑ*ð@
ð ×)Ñ)¨DÑ0ð@
ð ×(Ñ(¨4Ñ/ð	@
ð
 ×&Ñ&¨Ñ-ð@
ð ×(Ñ(¨4Ñ/ð@
ð ×)Ñ)¨DÑ0ð@
ð ×'Ñ'¨$Ñ.ð@
ð 
&ò@
ó ó ô@
rC   rw  )rO  rw  r\  rp  r<  r  rã   )Nr|   )Grà   ró   Úcollections.abcr   Údataclassesr   r0   r   Útorch.nnr   r   r   Ú r
   rì   Úactivationsr   r   Úcache_utilsr   r   r   Ú
generationr   Úmasking_utilsr   r   Úmodeling_layersr   Úmodeling_outputsr   r   r   r   r   Úmodeling_utilsr   r   Úpytorch_utilsr   Úutilsr   r   r   r   Úutils.genericr    r!   Úutils.output_capturingr"   r#   Úconfiguration_gpt2r%   Ú
get_loggerr˜   rf  rB   ÚModulerE   r¢   rª   rÀ   rã   r  r  r<  rO  r\  rp  rw  Ú__all__r
  rC   rA   ú<module>r›     sñ  ðñ "ã Ý $Ý !ã Ý ß AÑ Aå &ß 1ß CÑ CÝ )ß JÝ 9÷õ ÷ GÝ #÷ó ÷ Hß EÝ *ð 
ˆ×	Ñ	˜HÓ	%€ó%ô*W)�B—I‘Iô W)ôtˆb�i‰iô ô"?Ð*ô ?ôF`˜"Ÿ)™)ô `ðF ô&v˜/ó &vó ð&vñR ðôð
 ô7 ó 7ó óð7ð4 ôN
Ð#ó N
ó ðN
ñb ðôôP
Ð)¨?ó P
óðP
ñf ðôôr
Ð.°ó r
óðr
ñj ðôôj
Ð$7ó j
óðj
ðZ ôL
Ð!4ó L
ó ðL
ð^ ôL
Ð2ó L
ó ðL
ò^�rC   