
    cCiǼ                     
   S r SSKJrJrJr  SSK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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Jr  SSKJr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+  \&" 5       (       a  SSK,J-r-  SSK.J/r/  \'R`                  " \15      r2 " S S\Rf                  5      r4 S3S\Rj                  S\Rl                  S\Rl                  S\Rl                  S\\Rl                     S\7S\74S jjr8 " S  S!\Rj                  5      r9 " S" S#\5      r:\$ " S$ S%\5      5       r; " S& S'\;5      r<\$ " S( S)\;5      5       r= " S* S+\;\5      r>\$" S,S-9 " S. S/\;5      5       r?\$ " S0 S1\;5      5       r@/ S2QrAg)4zPyTorch OPT model.    )CallableOptionalUnionN)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FN)CacheDynamicCache)GenerationMixin)AttentionMaskConverter)FlashAttentionKwargs)GradientCheckpointingLayer)BaseModelOutputWithPastCausalLMOutputWithPastQuestionAnsweringModelOutput SequenceClassifierOutputWithPast)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack)TransformersKwargsauto_docstringcan_return_tupleis_torch_flex_attn_availablelogging)deprecate_kwarg   )	OPTConfig)	BlockMask)make_flex_block_causal_maskc                      ^  \ rS rSrSrS\S\4U 4S jjr  SS\R                  S\S\	\R                     4U 4S	 jjjr
S
rU =r$ )OPTLearnedPositionalEmbedding3   zF
This module learns positional embeddings up to a fixed maximum size.
num_embeddingsembedding_dimc                 L   > SU l         [        TU ]	  XR                   -   U5        g N   )offsetsuper__init__)selfr&   r'   	__class__s      ^/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/opt/modeling_opt.pyr-   &OPTLearnedPositionalEmbedding.__init__8   s"     ++5}E    attention_maskpast_key_values_lengthposition_idsc                    > Uc5  [         R                  " USS9nX1-  S-
  R                  5       nUSS2US24   n[        TU ]  X0R
                  -   5      $ )z3`input_ids_shape` is expected to be [bsz x seqlen].Nr   dim)torchcumsumlongr,   forwardr+   )r.   r3   r4   r5   r/   s       r0   r<   %OPTLearnedPositionalEmbedding.forward>   sZ      <<A>L(9A=CCEL'+A+B(BCLw|kk9::r2   )r+   )r   N)__name__
__module____qualname____firstlineno____doc__intr-   r9   
LongTensorr   r<   __static_attributes____classcell__r/   s   @r0   r$   r$   3   s]    Fs F3 F '(37	;((; !$; u//0	; ;r2   r$   modulequerykeyvaluer3   scalingdropoutc                    [         R                  " XR                  SS5      5      U-  nUb  X-   n[        R                  R                  US[         R                  S9R                  UR                  5      n[        R                  R                  XU R                  S9n[         R                  " X5      n	U	R                  SS5      R                  5       n	X4$ )N)r8   dtypeptrainingr   r*   )r9   matmul	transposer   
functionalsoftmaxfloat32torQ   rM   rT   
contiguous)
rH   rI   rJ   rK   r3   rL   rM   kwargsattn_weightsattn_outputs
             r0   eager_attention_forwardr_   P   s     <<}}R'<=GL!#4==((2U]](SVVW\WbWbcL==((6??([L,,|3K''1-88:K$$r2   c                   6  ^  \ rS rSrSr SS\S\\   4U 4S jjjr\	" SSSS	9     SS
\
R                  S\\   S\\
R                     S\\
R                     S\S\\
R                     S\\
R                  \\
R                     \\   4   4S jj5       rSrU =r$ )OPTAttentiong   z=Multi-headed attention from 'Attention Is All You Need' paperconfig	layer_idxc                   > [         TU ]  5         Xl        UR                  U l        UR
                  U l        UR                  U l        UR                  U l	        X l
        Uc-  [        R                  SU R                  R                   S35        U R                  U R                  -  U l        SU l        U R                  U R                  -  U R                  :w  a&  [#        SU R                   SU R                   S35      eU R                  S-  U l        [&        R(                  " U R                  U R                  U R                  S9U l        [&        R(                  " U R                  U R                  U R                  S9U l        [&        R(                  " U R                  U R                  U R                  S9U l        [&        R(                  " U R                  U R                  U R                  S9U l        g )	NzInstantiating z without passing a `layer_idx` is not recommended and will lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` when creating this class.Tz;embed_dim must be divisible by num_heads (got `embed_dim`: z and `num_heads`: z).g      ࿩bias)r,   r-   rc   hidden_size	embed_dimnum_attention_heads	num_headsattention_dropoutrM   enable_biasrd   loggerwarning_oncer/   r>   head_dim	is_causal
ValueErrorrL   r   Lineark_projv_projq_projout_proj)r.   rc   rd   r\   r/   s       r0   r-   OPTAttention.__init__j   s{    	++33//!--" !8!8 9 :, , $..8MMDNN*t~~=MdnnM]$T^^$4B8  }}d*iiTEUEUViiTEUEUViiTEUEUV		$..$..tGWGWXr2   past_key_valuepast_key_values4.58new_nameversionhidden_statesr3   layer_head_maskoutput_attentionscache_positionreturnc                    UR                  5       u  pn
U R                  U5      U R                  -  nUR                  USU R                  U R
                  5      R                  SS5      nU R                  U5      nU R                  U5      nUR                  USU R                  U R
                  5      R                  SS5      nUR                  USU R                  U R
                  5      R                  SS5      nUb!  UR                  XU R                  SU05      u  p[        nU R                  R                  S:w  a  [        U R                  R                     nU" U UUUU4U R                  (       d  SOU R                   SS	.UD6u  nnUR#                  XS5      R%                  5       nU R'                  U5      nU(       d  SnUU4$ )
z#Input shape: Batch x Time x ChannelrO   r   r*   Nr   eager              ?)rM   rL   )sizerv   rL   viewrk   rp   rV   rt   ru   updaterd   r_   rc   _attn_implementationr   rT   rM   reshaper[   rw   )r.   r   rz   r3   r   r   r   r\   bsztgt_len_query_states
key_statesvalue_statesattention_interfacer^   r]   s                    r0   r<   OPTAttention.forward   s    (,,.a {{=1DLL@#((b$..$--PZZ[\^_`[[/
{{=1__S"dnndmmLVVWXZ[\
#((b$..$--PZZ[\^_`&'6'='=$..;K^:\($J )@;;++w6"9$++:Z:Z"[$7	%
  $}}C$,,	%
 	%
!\ "))#;FFHmmK0 LL((r2   )rc   rM   ri   rm   rp   rq   rt   rd   rk   rw   rv   rL   ru   N)NNNFN)r>   r?   r@   rA   rB   r    r   rC   r-   r   r9   Tensorr   booltupler<   rE   rF   rG   s   @r0   ra   ra   g   s    G
 $(!Y!Y C=!Y !YF %0A6R ,01526"'156)||6) "%6) !.	6)
 "%,,/6)  6) !.6) 
u||Xell3Xe_D	E6) S6)r2   ra   c                     ^  \ rS rSrSS\S\\   4U 4S jjjr\" SSSS9       SS	\	R                  S
\\	R                     S\\	R                     S\\   S\\   S\\   S\\	R                     S\\	R                     S\\   S\\	R"                  \\\	R"                  \	R"                  4      4   4S jj5       rSrU =r$ )OPTDecoderLayer   rc   rd   c                 p  > [         TU ]  5         UR                  U l        [	        XS9U l        UR                  U l        UR                  U l        [        UR                     U l
        [        R                  " U R                  UR                  S9U l        [        R                  " U R                  UR                   UR"                  S9U l        [        R                  " UR                   U R                  UR"                  S9U l        [        R                  " U R                  UR                  S9U l        g )N)rc   rd   elementwise_affinerf   )r,   r-   rh   ri   ra   	self_attndo_layer_norm_beforerM   r   activation_functionactivation_fnr   	LayerNormlayer_norm_elementwise_affineself_attn_layer_normrs   ffn_dimrm   fc1fc2final_layer_norm)r.   rc   rd   r/   s      r0   r-   OPTDecoderLayer.__init__   s    ++%VI$*$?$?!~~#F$>$>?$&LLNNv/S/S%
! 99T^^V^^&BTBTU99V^^T^^&BTBTU "T^^PVPtPt ur2   ry   rz   r{   r|   r   r3   r   r   	use_cacher5   r   r\   r   c	                 *   Un
U R                   (       a  U R                  U5      nU R                  " SUUUUUUUS.U	D6u  p[        R                  R                  XR
                  U R                  S9nX-   nU R                   (       d  U R                  U5      nUR                  nUR                  SUR                  S5      5      nUn
U R                   (       a  U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      n[        R                  R                  XR
                  U R                  S9nX-   R                  U5      nU R                   (       d  U R                  U5      nU4nU(       a  X4-  nU$ )a"  
Args:
    hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
    attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
        `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
    layer_head_mask (`torch.FloatTensor`, *optional*): mask for attention heads in a given layer of size
        `(encoder_attention_heads,)`.
    output_attentions (`bool`, *optional*):
        Whether or not to return the attentions tensors of all attention layers. See `attentions` under
        returned tensors for more detail.
    use_cache (`bool`, *optional*):
        If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
        (see `past_key_values`).
    past_key_values (`Cache`, *optional*): cached past key and value projection states
    cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
        Indices depicting the position of the input sequence tokens in the sequence..
)r   rz   r5   r3   r   r   r   rR   rO    )r   r   r   r   rW   rM   rT   shaper   r   r   r   r   r   r   )r.   r   r3   r   rz   r   r   r5   r   r\   residualself_attn_weightshidden_states_shapeoutputss                 r0   r<   OPTDecoderLayer.forward   s   > ! $$ 55mDM ,0>> 	,
'+%)+/)	,
 	,
( --m||VZVcVc-d 0 (( 55mDM ,11%--b-2D2DR2HI  $$ 11-@M/**=9/--m||VZVcVc-d!1778KL (( 11-@M "++Gr2   )	r   r   rM   ri   r   r   r   r   r   r   )NNNFFNN)r>   r?   r@   rA   r    r   rC   r-   r   r9   r   r   r   rD   r   r   r   FloatTensorr<   rE   rF   rG   s   @r0   r   r      s/   vy vXc] v v" %0A6R 2626+/,1$)3715P||P !.P "%,,/	P
 "%P $D>P D>P u//0P !.P -.P 
u  (51B1BEDUDU1U+V"WW	XP SPr2   r   c                   H    \ rS rSr% \\S'   SrSrS/rSr	Sr
SrSrSrS rSrg)	OPTPreTrainedModeli-  rc   modelTr   c                    U R                   R                  n[        U[        R                  5      (       aW  UR
                  R                  R                  SUS9  UR                  b%  UR                  R                  R                  5         g g [        U[        R                  5      (       ad  UR
                  R                  R                  SUS9  UR                  b2  UR
                  R                  UR                     R                  5         g g [        U[        R                  5      (       aJ  UR
                  R                  R                  S5        UR                  R                  R                  5         g g )Nr   )meanstdr   )rc   init_std
isinstancer   rs   weightdatanormal_rg   zero_	Embeddingpadding_idxr   fill_)r.   rH   r   s      r0   _init_weights OPTPreTrainedModel._init_weights:  s   kk""fbii((MM&&CS&9{{&  &&( '--MM&&CS&9!!-""6#5#56<<> .--MM$$S)KK""$ .r2   r   N)r>   r?   r@   rA   r    __annotations__base_model_prefixsupports_gradient_checkpointing_no_split_modules_supports_attention_backend_supports_flash_attn_supports_sdpa_supports_flex_attn_can_compile_fullgraphr   rE   r   r2   r0   r   r   -  s?    &*#*+"&N!%r2   r   c                   2  ^  \ rS rSrSrS\4U 4S jjr SS\\R                  S4   S\R                  S\R                  S	\
S
\4
S jjr\S\R                  S\S\S\R                  S\R                  S\4S j5       r\           SS\\R&                     S\\R                     S\\R                     S	\\
   S\\R(                     S\\   S
\\   S\\   S\\   S\\R&                     S\\R                     S\\   S\\\4   4S jj5       rSrU =r$ )
OPTDecoderiI  z
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`OPTDecoderLayer`]

Args:
    config: OPTConfig
rc   c           
      F  > [         TU ]  U5        UR                  U l        UR                  U l        UR                  U l        UR                  U l        UR                  U l        [        R                  " UR                  UR                  U R
                  5      U l        [        UR                  UR                  5      U l        UR                  UR                  :w  a0  [        R                   " UR                  UR                  SS9U l        OS U l        UR                  UR                  :w  a0  [        R                   " UR                  UR                  SS9U l        OS U l        UR&                  (       a@  UR(                  (       d/  [        R*                  " UR                  UR,                  S9U l        OS U l        [        R0                  " [3        UR4                  5       Vs/ s H  n[7        XS9PM     sn5      U l        SU l        U R=                  5         g s  snf )NFrf   r   )rd   )r,   r-   rM   	layerdroppad_token_idr   max_position_embeddingsmax_target_positions
vocab_sizer   r   word_embed_proj_dimembed_tokensr$   rh   embed_positionsrs   project_out
project_inr   _remove_final_layer_normr   r   r   
ModuleListrangenum_hidden_layersr   layersgradient_checkpointing	post_init)r.   rc   ir/   s      r0   r-   OPTDecoder.__init__Q  s    ~~))!..$*$B$B! ++LL):):F<V<VX\XhXhi<V=[=[]c]o]op%%););;!yy););V=W=W^cdD#D%%););; ii(B(BFDVDV]bcDO"DO
 &&v/N/N$&LL""v7[7[%D! %)D!mmSXY_YqYqSr$sSra_V%ISr$st&+#	 %ts   'Hr3   r!   input_tensorr   rz   r   c           	         U R                   R                  S:X  a  Ub  US:H  R                  5       (       a  U$ g U R                   R                  S:X  a,  [        U[        R
                  5      (       a  [        U5      nU$ Ub  UR                  5       OSnUb  UR                  OSnU R                   R                  S:X  a5  U(       d.  U(       d'  [        R                  " UUUU R                  S9(       a  g UR                  nUR                  S   n	U(       a  UR                  5       n
O5[        U[        R
                  5      (       a  UR                  S	   OXi-   S-   n
U R                  UU	U
UUUR                  S   S
9nU R                   R                  S:X  aZ  UbW  UR                   R"                  S;   a=  U(       d6  [        R$                  " U5      R&                  n[        R(                  " X5      nU$ )Nflash_attention_2r   flex_attentionr   Fsdpa)inputs_embedsr4   is_trainingr   rO   )sequence_lengthtarget_lengthrQ   r   
batch_size)cudaxpunpu)rc   r   anyr   r9   r   r"   get_seq_lengthis_compileabler   _ignore_causal_mask_sdparT   rQ   r   get_max_cache_shape5_prepare_4d_causal_attention_mask_with_cache_positiondevicetypefinfomin_unmask_unattended)r.   r3   r   r   rz   r   past_seen_tokensusing_compilable_cacherQ   r   r   causal_mask	min_dtypes                r0   _update_causal_maskOPTDecoder._update_causal_maskw  s    ;;++/BB)~/D.I.I.K.K%%;;++/??.%,,77!<^!L!!
 @O?Z?99;`aCRC^!?!?di ;;++v5>T]n%>>*'7 MM	 ""&,,Q/!+??AM nell;; $$R(%7!;  PP+')#))!, Q 
 KK,,6*%%**.DD%
 E*..I0CCK[Kr2   r   r   rQ   r   c                    U b  U R                  5       S:X  a  U nU$ [        R                  " U5      R                  n[        R                  " X4XUR
                  S9nUS:w  a  [        R                  " USS9nU[        R                  " X$R
                  S9UR                  SS5      :  -  nUSSSS2SS24   R                  USSS5      nU b  UR                  5       nU R                  S   n	USS2SS2SS2SU	24   U SS2SSSS24   R                  UR
                  5      -   n
U
S:H  n
USS2SS2SS2SU	24   R                  X5      USS2SS2SS2SU	24'   U$ )	a  
Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape
`(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.

Args:
    attention_mask (`torch.Tensor`):
        A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape
        `(batch_size, 1, query_length, key_value_length)`.
    sequence_length (`int`):
        The sequence length being processed.
    target_length (`int`):
        The target length: when generating with static cache, the mask should be as long as the static cache,
        to account for the 0 padding, the part of the cache that is not filled yet.
    dtype (`torch.dtype`):
        The dtype to use for the 4D attention mask.
    cache_position (`torch.Tensor`):
        Indices depicting the position of the input sequence tokens in the sequence.
    batch_size (`torch.Tensor`):
        Batch size.
N   )
fill_valuerQ   r   r   )diagonalr   rO   r   )r8   r9   r   r   fullr   triuaranger   expandcloner   rZ   masked_fill)r3   r   r   rQ   r   r   r\   r   r   mask_lengthpadding_masks              r0   r   @OPTDecoder._prepare_4d_causal_attention_mask_with_cache_position  s}   > %.*<*<*>!*C(K* ' E*..I** 0Y\j\q\qK !##jjqA5<<>S>STWeWmWmnprsWtttK%dD!Q&67>>z1bRTUK))//1,2226*1aL[L+@ANSTVZ\`bcScDdDgDg&&E    ,q05@Aq,;,AV5W5c5c 6Aq!\k\12 r2   	input_ids	head_maskr   r   output_hidden_statesreturn_dictr5   r\   r   c                 n   Ub  UOU R                   R                  nUb  UOU R                   R                  nUb  UOU R                   R                  nU	b  U	OU R                   R                  n	USL USL-  (       a  [        S5      eU R                  (       a/  U R                  (       a  U(       a  [        R                  S5        SnUb  UR                  SUR                  S   5      nUc  U R                  U5      nU(       a  Uc  [        U R                   S9nUb  UR                  5       OSnUc/  [        R                   " XUR                  S   -   UR"                  S	9nUc=  XR                  S   -   n[        R$                  " UR                  S   XR"                  S	9nU R'                  X%XU5      nU
c5  [        R(                  " USS
9n
X-  S-
  R+                  5       n
U
SS2US24   n
U R-                  X-U
S9nU R.                  b  U R/                  U5      nUUR1                  UR"                  5      -   nU(       a  SOSnU(       a  SOSn[3        U/S/5       Hn  u  nnUc  M  UR5                  5       S   [7        U R8                  5      :w  d  M7  [        SU S[7        U R8                  5       SUR5                  5       S    S35      e   [;        U R8                  5       H|  u  nnU(       a  UU4-  nU R                  (       a(  [        R<                  " / 5      nUU R>                  :  a  ML  U" U4UU
Ub  UU   OSUUUUS.UD6nUS   nU(       d  Ms  UUS   4-  nM~     U R@                  b  U RA                  U5      nU RB                  b  U RC                  U5      nU(       a  UU4-  n[E        UUUUS9$ )a:  
Args:
    input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
        Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you
        provide it.

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

        [What are input IDs?](../glossary#input-ids)
    attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
        Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

        - 1 for tokens that are **not masked**,
        - 0 for tokens that are **masked**.

        [What are attention masks?](../glossary#attention-mask)
    head_mask (`torch.Tensor` of shape `(num_hidden_layers, num_attention_heads)`, *optional*):
        Mask to nullify selected heads of the attention modules. Mask values selected in `[0, 1]`:

        - 1 indicates the head is **not masked**,
        - 0 indicates the head is **masked**.

    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 self-attention blocks and in the
        cross-attention blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.

        If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those
        that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of
        all `decoder_input_ids` of shape `(batch_size, sequence_length)`.

    inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
        Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation.
        This is useful if you want more control over how to convert `input_ids` indices into associated vectors
        than the model's internal embedding lookup matrix.
    output_attentions (`bool`, *optional*):
        Whether or not to return the attentions tensors of all attention layers. See `attentions` under
        returned tensors for more detail.
    output_hidden_states (`bool`, *optional*):
        Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors
        for more detail.
    return_dict (`bool`, *optional*):
        Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
    position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
        Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
        config.n_positions - 1]`. for padding use -1.

        [What are position IDs?](../glossary#position-ids)
    cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
        Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`,
        this tensor is not affected by padding. It is used to update the cache in the correct position and to infer
        the complete sequence length.
Nz:You must specify exactly one of input_ids or inputs_embedszX`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`.FrO   )rc   r   r   r  r7   )r5   r   r  zThe `z` should be specified for z layers, but it is for .)r3   r5   r   rz   r   r   r   last_hidden_staterz   r   
attentions)#rc   r   r  r   use_return_dictrr   r   rT   rn   ro   r   r   r   r   r   r9   r
  r   onesr  r:   r;   r   r   rZ   zipr   lenr   	enumeraterandr   r   r   r   )r.   r  r3   r  rz   r   r   r   r  r  r5   r   r\   r   
seq_lengthr   
pos_embedsr   all_hidden_statesall_self_attns	attn_mask	mask_nameidxdecoder_layerdropout_probabilitylayer_outputss                             r0   r<   OPTDecoder.forward  s   N 2C1N-TXT_T_TqTq$8$D $++JjJj 	 "+!6IDKK<Q<Q	%0%<k$++B]B]-t";<YZZ&&4==Yj I !r9??2+>?I  --i8M0*$++>O?N?Z?99;`a!"\\ ]5H5H5K"KTaThThN !),?,?,BBJ"ZZ(;(;A(>
SgSghN..>L]

  <<A>L(9A=CCEL'+;+<(<=L)).Ye)f
??& OOM:M%
m6J6J(KK #7BD0d %(k]$C Iy$>>#A&3t{{+;<$	{*DSEUDV W%NN,Q/03  %D #,DKK"8C#!m%55!}}&+jjn#&7)
*)3<3H3d /"3#-
 
M *!,M  =#3"553 #96   , 11-@M' ,,];M  -!11&+++%	
 	
r2   )rM   r   r   r   r   r   r   r   r   r   r   r   )FNNNNNNNNNNN)r>   r?   r@   rA   rB   r    r-   r   r9   r   r   r   r  staticmethodrC   rQ   r   r   r   rD   r   r   r   r   r   r<   rE   rF   rG   s   @r0   r   r   I  s   #y #X #(BellK78B llB 	B
 B  BH 444 4 {{	4
 4 4 4l  1515,0+/59$(,0/3&*3715t
E,,-t
 !.t
 ELL)	t

 "%t
   1 12t
 D>t
 $D>t
 'tnt
 d^t
 u//0t
 !.t
 -.t
 
u--	.t
 t
r2   r   c                   r  ^  \ rS rSrS\4U 4S jjrS rS r\\	           SS\
\R                     S\
\R                     S\
\R                     S	\
\   S
\
\R                     S\
\   S\
\   S\
\   S\
\   S\
\R                     S\
\R                     S\\   S\\\4   4S jj5       5       rSrU =r$ )OPTModeli  rc   c                 d   > [         TU ]  U5        [        U5      U l        U R	                  5         g r   )r,   r-   r   decoderr   r.   rc   r/   s     r0   r-   OPTModel.__init__  s&     !&)r2   c                 .    U R                   R                  $ r   r0  r   r.   s    r0   get_input_embeddingsOPTModel.get_input_embeddings  s    ||(((r2   c                 $    XR                   l        g r   r4  r.   rK   s     r0   set_input_embeddingsOPTModel.set_input_embeddings  s    $)!r2   r  r3   r  rz   r   r   r   r  r  r5   r   r\   r   c                 ~   Ub  UOU R                   R                  nUb  UOU R                   R                  nUb  UOU R                   R                  nU	b  U	OU R                   R                  n	U R
                  " SUUU
UUUUUUSUS.UD6n[        UR                  UR                  UR                  UR                  S9$ )NTr  r3   r5   r  rz   r   r   r   r  r  r   r  r   )rc   r   r  r   r  r0  r   r  rz   r   r  )r.   r  r3   r  rz   r   r   r   r  r  r5   r   r\   decoder_outputss                 r0   r<   OPTModel.forward  s    " 2C1N-TXT_T_TqTq$8$D $++JjJj 	 "+!6IDKK<Q<Q	%0%<k$++B]B] ,, 
)%+'/!5)
 
 '-??+;;)77&11	
 	
r2   )r0  r+  )r>   r?   r@   rA   r    r-   r6  r:  r   r   r   r9   rD   r   r   r   r   r   r   r   r   r   r<   rE   rF   rG   s   @r0   r.  r.    s9   y )*  1515,0+/59$(,0/3&*3715+
E,,-+
 !.+
 ELL)	+

 "%+
   1 12+
 D>+
 $D>+
 'tn+
 d^+
 u//0+
 !.+
 -.+
 
u--	.+
  +
r2   r.  c            !         ^  \ rS rSrS/rU 4S jrS rS rS rS r	\
\            SS\\R                     S	\\R                     S
\\R                     S\\   S\\R"                     S\\R                     S\\   S\\   S\\   S\\   S\\R                     S\\R                     S\\   S\\\4   4S jj5       5       rSrU =r$ )OPTForCausalLMi  zlm_head.weightc                    > [         TU ]  U5        [        U5      U l        [        R
                  " UR                  UR                  SS9U l        U R                  5         g NFrf   )
r,   r-   r.  r   r   rs   r   r   lm_headr   r1  s     r0   r-   OPTForCausalLM.__init__  sK     f%
 yy!;!;V=N=NUZ[ 	r2   c                 B    U R                   R                  R                  $ r   r   r0  r   r5  s    r0   r6  #OPTForCausalLM.get_input_embeddings      zz!!...r2   c                 8    XR                   R                  l        g r   rG  r9  s     r0   r:  #OPTForCausalLM.set_input_embeddings      */

'r2   c                 $    XR                   l        g r   r   r0  )r.   r0  s     r0   set_decoderOPTForCausalLM.set_decoder  s    $

r2   c                 .    U R                   R                  $ r   rN  r5  s    r0   get_decoderOPTForCausalLM.get_decoder  s    zz!!!r2   r  r3   r  rz   r   labelsr   r   r  r  r5   r   r\   r   c                 "   Ub  UOU R                   R                  nU	b  U	OU R                   R                  n	U
b  U
OU R                   R                  n
U R                  R
                  " SUUUUUUUUU	SUS.UD6nU R                  US   5      R                  5       nSnUbE  UR                  UR                  5      nU R                  " UU4SU R                   R                  0UD6n[        UUUR                  UR                  UR                  S9$ )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, OPTForCausalLM

>>> model = OPTForCausalLM.from_pretrained("facebook/opt-350m")
>>> tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m")

>>> prompt = "Hey, are you conscious? Can you talk to me?"
>>> inputs = tokenizer(prompt, return_tensors="pt")

>>> # Generate
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
"Hey, are you conscious? Can you talk to me?\nI'm not conscious. I'm just a little bit of a weirdo."
```NTr=  r   r   losslogitsrz   r   r  r   )rc   r   r  r  r   r0  rD  r[   rZ   r   loss_functionr   r   rz   r   r  )r.   r  r3   r  rz   r   rT  r   r   r  r  r5   r   r\   r   rX  rW  s                    r0   r<   OPTForCausalLM.forward  s7   R 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B] **$$ 
)%+'/!5)
 
 gaj)446YYv}}-F%%  ;;11 	D &#33!//))
 	
r2   )rD  r   NNNNNNNNNNNN)r>   r?   r@   rA   _tied_weights_keysr-   r6  r:  rO  rR  r   r   r   r9   rD   r   r   r   r   r   r   r   r   r   r<   rE   rF   rG   s   @r0   rA  rA    sm   *+/0%"  1515,0+/59-1$(,0/3&*3715P
E,,-P
 !.P
 ELL)	P

 "%P
   1 12P
 ))*P
 D>P
 $D>P
 'tnP
 d^P
 u//0P
 !.P
 +,P
 
u,,	-P
  P
r2   rA  a  
    The OPT Model transformer with a sequence classification head on top (linear layer).

    [`OPTForSequenceClassification`] uses the last token in order to do the classification, as other causal models
    (e.g. GPT-2) 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).
    )custom_introc                   ^  ^  \ rS rSrS\4U 4S jjr\           SS\\R                     S\\R                     S\\R                     S\\   S\\R                     S	\\R                     S
\\   S\\   S\\   S\\   S\\R                     S\\\4   4S jj5       rS rS rSrU =r$ )OPTForSequenceClassificationiW  rc   c                    > [         TU ]  U5        UR                  U l        [        U5      U l        [
        R                  " UR                  U R                  SS9U l        U R                  5         g rC  )
r,   r-   
num_labelsr.  r   r   rs   r   scorer   r1  s     r0   r-   %OPTForSequenceClassification.__init__f  sT      ++f%
YYv994??QVW
 	r2   r  r3   r  rz   r   rT  r   r   r  r  r5   r   c                    U
b  U
OU R                   R                  n
U R                  UUUUUUUUU	U
S9
nUS   nU R                  U5      nUb  UR                  SS u  nnOUR                  SS u  nnU R                   R
                  c  US:w  a  [        S5      eU R                   R
                  c  SnOUb  XR                   R
                  :g  R                  UR                  [        R                  5      n[        R                  " UR                  S   UR                  [        R                  S9nUU-  R                  S5      nO.Sn[        R                  U R                  R                    S	35        U[        R                  " XR                  S
9U4   nSnUGb  U R                   R"                  c  U R$                  S:X  a  SU R                   l        OoU R$                  S:  aN  UR&                  [        R(                  :X  d  UR&                  [        R*                  :X  a  SU R                   l        OSU R                   l        U R                   R"                  S:X  aJ  [-        5       nU R$                  S:X  a&  U" UR/                  5       UR/                  5       5      nOU" UU5      nOU R                   R"                  S:X  a=  [1        5       nU" UR3                  SU R$                  5      UR3                  S5      5      nO-U R                   R"                  S:X  a  [5        5       nU" UU5      nU
(       d  U4USS -   nUb  U4U-   $ U$ [7        UUUR8                  UR:                  UR<                  S9$ )ae  
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).
N	rz   r3   r5   r  r   r   r   r  r  r   r*   r   z=Cannot handle batch sizes > 1 if no padding token is defined.rO   )r   rQ   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_classificationrV  )rc   r  r   rb  r   r   rr   rZ   r   r9   int32r
  argmaxrn   ro   r/   r>   problem_typera  rQ   r;   rC   r	   squeezer   r   r   r   rz   r   r  )r.   r  r3   r  rz   r   rT  r   r   r  r  r5   transformer_outputsr   rX  r   r   last_non_pad_tokennon_pad_masktoken_indicespooled_logitsrW  loss_fctoutputs                           r0   r<   $OPTForSequenceClassification.forwardo  s   * &1%<k$++B]B]"jj+)%'/!5# ) 
 ,A.M* *3//"1*='J*7*=*=bq*A'J;;##+
a\]];;##+!#"%)A)AAEEfmmUZU`U`aL!LL)<V]]Z_ZeZefM"/,">!F!Fr!J!#>>**+ ,Z Z
 u||J}}MOaab{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#M$9$9$;V^^=MND#M6:D))-JJ+- 2 22t GUWY))-II,.v6#%(;AB(??F)-)9TGf$EvE/ /??-;;*55
 	
r2   c                 B    U R                   R                  R                  $ r   rG  r5  s    r0   r6  1OPTForSequenceClassification.get_input_embeddings  rI  r2   c                 8    XR                   R                  l        g r   rG  r9  s     r0   r:  1OPTForSequenceClassification.set_input_embeddings  rL  r2   )r   ra  rb  r+  )r>   r?   r@   rA   r    r-   r   r   r9   rD   r   r   r   r   r   r   r<   r6  r:  rE   rF   rG   s   @r0   r_  r_  W  s;   y   156:15+/59-1$(,0/3&*37\
E,,-\
 !!2!23\
 E--.	\

 "%\
   1 12\
 ))*\
 D>\
 $D>\
 'tn\
 d^\
 u//0\
 
u66	7\
 \
|/0 0r2   r_  c                   ~  ^  \ rS rSrS\4U 4S jjr\            SS\\R                     S\\R                     S\\R                     S\\   S\\R                     S	\\R                     S
\\R                     S\\   S\\   S\\   S\\   S\\R                     S\\\4   4S jj5       rS rS rSrU =r$ )OPTForQuestionAnsweringi  rc   c                    > [         TU ]  U5        [        U5      U l        [        R
                  " UR                  S5      U l        U R                  5         g r)   )	r,   r-   r.  r   r   rs   r   
qa_outputsr   r1  s     r0   r-    OPTForQuestionAnswering.__init__  s@     f%
))F$>$>B 	r2   r  r3   r  rz   r   start_positionsend_positionsr   r   r  r  r5   r   c                    Ub  UOU R                   R                  nU R                  UUUUUUUU	U
US9
nUS   nU R                  U5      nUR	                  SSS9u  nnUR                  S5      R                  5       nUR                  S5      R                  5       nSnUb  Ub  [        UR                  5       5      S:  a  UR                  S5      n[        UR                  5       5      S:  a  UR                  S5      nUR                  S5      nUR                  SU5      R                  UR                  5      nUR                  SU5      R                  UR                  5      n[        US9nU" UU5      nU" UU5      nUU-   S-  nU(       d  UU4USS -   nUb  U4U-   $ U$ [        UUUUR                  UR                  S	9$ )
a  
Example:

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

>>> torch.manual_seed(4)  # doctest: +IGNORE_RESULT
>>> tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m")

>>> # note: we are loading a OPTForQuestionAnswering from the hub here,
>>> # so the head will be randomly initialized, hence the predictions will be random
>>> model = OPTForQuestionAnswering.from_pretrained("facebook/opt-350m")

>>> question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"

>>> inputs = tokenizer(question, text, return_tensors="pt")
>>> with torch.no_grad():
...     outputs = model(**inputs)

>>> answer_start_index = outputs.start_logits.argmax()
>>> answer_end_index = outputs.end_logits.argmax()

>>> answer_offset = len(tokenizer(question)[0])

>>> predict_answer_tokens = inputs.input_ids[
...     0, answer_offset + answer_start_index : answer_offset + answer_end_index + 1
... ]
>>> predicted = tokenizer.decode(predict_answer_tokens)
>>> predicted
' a nice puppet'
```Nre  r   r   rO   r7   )ignore_indexr*   )rW  start_logits
end_logitsr   r  )rc   r  r   r|  splitrl  r[   r  r   clamprZ   r   r   r   r   r  )r.   r  r3   r  rz   r   r~  r  r   r   r  r  r5   rm  r   rX  r  r  
total_lossignored_indexrr  
start_lossend_lossrs  s                           r0   r<   OPTForQuestionAnswering.forward  s   ` &1%<k$++B]B]"jj+)%'/!5# ) 
 ,A./#)<<r<#: j#++B/::<''+668

&=+D?'')*Q."1"9"9""==%%'(1, - 5 5b 9(--a0M-33A}EHHWO)//=ADDV]]SM']CH!,@J
M:H$x/14J"J/2Eab2IIF/9/EZMF*Q6Q+%!-;;*55
 	
r2   c                 B    U R                   R                  R                  $ r   rG  r5  s    r0   r6  ,OPTForQuestionAnswering.get_input_embeddingsA  rI  r2   c                 8    XR                   R                  l        g r   rG  r9  s     r0   r:  ,OPTForQuestionAnswering.set_input_embeddingsD  rL  r2   )r   r|  r[  )r>   r?   r@   rA   r    r-   r   r   r9   rD   r   r   r   r   r   r   r<   r6  r:  rE   rF   rG   s   @r0   rz  rz    sT   y   156:15+/596:48$(,0/3&*37_
E,,-_
 !!2!23_
 E--.	_

 "%_
   1 12_
 "%"2"23_
   0 01_
 D>_
 $D>_
 'tn_
 d^_
 u//0_
 
u22	3_
 _
B/0 0r2   rz  )rA  r.  r   r_  rz  )r   )BrB   typingr   r   r   r9   r   torch.nnr   r   r	   activationsr   cache_utilsr   r   
generationr   modeling_attn_mask_utilsr   modeling_flash_attention_utilsr   modeling_layersr   modeling_outputsr   r   r   r   modeling_utilsr   r   processing_utilsr   utilsr   r   r   r   r   utils.deprecationr   configuration_optr    !torch.nn.attention.flex_attentionr!   integrations.flex_attentionr"   
get_loggerr>   rn   r   r$   Moduler   floatr_   ra   r   r   r   r.  rA  r_  rz  __all__r   r2   r0   <module>r     s    , ,   A A ! . ) > B 9  G & p p 0 (  !!;J 
		H	%;BLL ;H %II%<<% 
% <<	%
 U\\*% % %.])299 ])@c0 cL % % %6_
# _
D :
! :
 :
zk
' k
\ m0#5 m0m0` o00 o0 o0dr2   