
    <h־                        S r SSKJrJrJr  SSK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*  \'" 5       (       a  SSK+J,r,  SSK-J.r.  \(R^                  " \05      r1 " S S\Rd                  5      r3 S2S\Rh                  S\Rj                  S\Rj                  S\Rj                  S\\Rj                     S\6S\64S jjr7 " S S \Rh                  5      r8 " S! S"\5      r9\% " 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>\% " S/ S0\:5      5       r?/ S1Qr@g)3z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   )	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      \/var/www/html/shao/venv/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-   r2   r3   r4   r.   s       r/   r;   %OPTLearnedPositionalEmbedding.forward>   sZ      <<A>L(9A=CCEL'+A+B(BCLw|kk9::r1   )r*   )r   N)__name__
__module____qualname____firstlineno____doc__intr,   r8   
LongTensorr   r;   __static_attributes____classcell__r.   s   @r/   r#   r#   3   s]    Fs F3 F '(37	;((; !$; u//0	; ;r1   r#   modulequerykeyvaluer2   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)r7   dtypeptrainingr   r)   )r8   matmul	transposer   
functionalsoftmaxfloat32torP   rL   rS   
contiguous)
rG   rH   rI   rJ   r2   rK   rL   kwargsattn_weightsattn_outputs
             r/   eager_attention_forwardr^   P   s     <<}}R'<=GL!#4==((2U]](SVVW\WbWbcL==((6??([L,,|3K''1-88:K$$r1   c                   :  ^  \ rS rSrSr SS\S\\   4U 4S jjjr     SS\	R                  S\\\	R                        S\\	R                     S	\\	R                     S
\S\\	R                     S\\	R                  \\	R                     \\   4   4S jj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,   rb   hidden_size	embed_dimnum_attention_heads	num_headsattention_dropoutrL   enable_biasrc   loggerwarning_oncer.   r=   head_dim	is_causal
ValueErrorrK   r   Lineark_projv_projq_projout_proj)r-   rb   rc   r[   r.   s       r/   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r1   hidden_statespast_key_valuer2   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  aT  U R                  R                  S:X  a  U(       a  [        R                  S5        O[         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 ChannelrN   r   r)   Nr|   eagersdpaz`torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to eager attention. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.              ?)rL   rK   )sizeru   rK   viewrj   ro   rU   rs   rt   updaterc   r^   rb   _attn_implementationrm   rn   r   rS   rL   reshaperZ   rv   )r-   rx   ry   r2   rz   r{   r|   r[   bsztgt_len_query_states
key_statesvalue_statesattention_interfacer]   r\   s                    r/   r;   OPTAttention.forward   s    (,,.a {{=1DLL@#((b$..$--PZZ[\^_`[[/
{{=1__S"dnndmmLVVWXZ[\
#((b$..$--PZZ[\^_`%'5'<'<$..;K^:\($J )@;;++w6{{//69>O##L
 '>dkk>^>^&_#$7	%
  $}}C$,,	%
 	%
!\ "))#;FFHmmK0 LL((r1   )rb   rL   rh   rl   ro   rp   rs   rc   rj   rv   ru   rK   rt   N)NNNFN)r=   r>   r?   r@   rA   r   r   rB   r,   r8   Tensortupleboolr   r;   rD   rE   rF   s   @r/   r`   r`   g   s    G
 $(!Y!Y C=!Y !YL 9=1526"'15<)||<) !u||!45<) !.	<)
 "%,,/<)  <) !.<) 
u||Xell3Xe_D	E<) <)r1   r`   c                     ^  \ rS rSrSS\S\\   4U 4S jjjr       SS\R                  S\\R                     S\\R                     S\\
\R                        S	\\   S
\\   S\\R                     S\\R                     S\\   S\
\R                  \\
\R                  \R                  4      4   4S jjrSrU =r$ )OPTDecoderLayer   rb   rc   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)rb   rc   elementwise_affinere   )r+   r,   rg   rh   r`   	self_attndo_layer_norm_beforerL   r   activation_functionactivation_fnr   	LayerNormlayer_norm_elementwise_affineself_attn_layer_normrr   ffn_dimrl   fc1fc2final_layer_norm)r-   rb   rc   r.   s      r/   r,   OPTDecoderLayer.__init__   s    ++%VI$*$?$?!~~#F$>$>?$&LLNNv/S/S%
! 99T^^V^^&BTBTU99V^^T^^&BTBTU "T^^PVPtPt ur1   rx   r2   rz   ry   r{   	use_cacher4   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$ )a4  
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_value (`Tuple(torch.FloatTensor)`, *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..
)rx   ry   r4   r2   rz   r{   r|   rQ   rN    )r   r   r   r   rV   rL   rS   shaper   r   r   r   r   r   r   )r-   rx   r2   rz   ry   r{   r   r4   r|   r[   residualself_attn_weightshidden_states_shapeoutputss                 r/   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r1   )	r   r   rL   rh   r   r   r   r   r   r   )NNNFFNN)r=   r>   r?   r@   r   r   rB   r,   r8   r   r   r   rC   r   r   FloatTensorr;   rD   rE   rF   s   @r/   r   r      s#   vy vXc] v v( 26268<,1$)3715P||P !.P "%,,/	P
 !u||!45P $D>P D>P u//0P !.P -.P 
u  (51B1BEDUDU1U+V"WW	XP Pr1   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)	OPTPreTrainedModeli1  rb   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   )rb   init_std
isinstancer   rr   weightdatanormal_rf   zero_	Embeddingpadding_idxr   fill_)r-   rG   r   s      r/   _init_weights OPTPreTrainedModel._init_weights>  s   kk""fbii((MM&&CS&9{{&  &&( '--MM&&CS&9!!-""6#5#56<<> .--MM$$S)KK""$ .r1   r   N)r=   r>   r?   r@   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   rD   r   r1   r/   r   r   1  s?    &*#*+"&N!%r1   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$ )
OPTDecoderiM  z
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`OPTDecoderLayer`]

Args:
    config: OPTConfig
rb   c           
      D  > [         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 )NFre   r   )rc   )r+   r,   rL   	layerdroppad_token_idr   max_position_embeddingsmax_target_positions
vocab_sizer   r   word_embed_proj_dimembed_tokensr#   rg   embed_positionsrr   project_out
project_inr   _remove_final_layer_normr   r   r   
ModuleListrangenum_hidden_layersr   layersgradient_checkpointing	post_init)r-   rb   ir.   s      r/   r,   OPTDecoder.__init__U  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r2   r    input_tensorr|   past_key_valuesr{   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   Fr   )inputs_embedsr3   is_trainingr   rN   )sequence_lengthtarget_lengthrP   r|   
batch_size)cudaxpunpu)rb   r   anyr   r8   r   r!   get_seq_lengthis_compileabler   _ignore_causal_mask_sdparS   rP   r   get_max_cache_shape5_prepare_4d_causal_attention_mask_with_cache_positiondevicetypefinfomin_unmask_unattended)r-   r2   r   r|   r   r{   past_seen_tokensusing_compilable_cacherP   r   r   causal_mask	min_dtypes                r/   _update_causal_maskOPTDecoder._update_causal_mask{  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r1   r   r   rP   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_valuerP   r   r   )diagonalr   rN   r   )r7   r8   r   r   fullr   triuaranger   expandcloner   rY   masked_fill)r2   r   r   rP   r|   r   r[   r   r   mask_lengthpadding_masks              r/   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 r1   	input_ids	head_maskr   r   output_hidden_statesreturn_dictr4   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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
  [        5       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`):
        Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
        shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of

        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`.FrN   r   r   r  r6   )r4   r   r  zThe `z` should be specified for z layers, but it is for .)r2   r4   rz   ry   r{   r   r|   last_hidden_stater   rx   
attentions)#rb   r{   r  r   use_return_dictrq   r   rS   rm   rn   r   r   r   r   r   r8   r  r   onesr   r9   r:   r   r   rY   zipr   lenr   	enumeraterandr   r   r   r   )r-   r  r2   r  r   r   r   r{   r  r  r4   r|   r[   r   
seq_lengthr   
pos_embedsrx   all_hidden_statesall_self_attns	attn_mask	mask_nameidxdecoder_layerdropout_probabilitylayer_outputss                             r/   r;   OPTDecoder.forward  s   P 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*nO?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&+++%	
 	
r1   )rL   r   r   r   r   r   r   r   r   r   r   r   )FNNNNNNNNNNN)r=   r>   r?   r@   rA   r   r,   r   r8   r   r   r   r   staticmethodrB   rP   r   r   r   rC   r   r   r   r   r   r;   rD   rE   rF   s   @r/   r   r   M  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u
E,,-u
 !.u
 ELL)	u

 "%u
   1 12u
 D>u
 $D>u
 'tnu
 d^u
 u//0u
 !.u
 -.u
 
u--	.u
 u
r1   r   c                     ^  \ rS rSrS\4U 4S jjrS rS rS r\	\
           SS\\R                     S\\R                     S	\\R                     S
\\\\R"                     \4      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  rb   c                 d   > [         TU ]  U5        [        U5      U l        U R	                  5         g r   )r+   r,   r   decoderr   r-   rb   r.   s     r/   r,   OPTModel.__init__  s&     !&)r1   c                 .    U R                   R                  $ r   r+  r   r-   s    r/   get_input_embeddingsOPTModel.get_input_embeddings  s    ||(((r1   c                 $    XR                   l        g r   r/  r-   rJ   s     r/   set_input_embeddingsOPTModel.set_input_embeddings  s    $)!r1   c                     U R                   $ r   r+  r0  s    r/   get_decoderOPTModel.get_decoder  s    ||r1   r  r2   r  r   r   r   r{   r  r  r4   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  r2   r4   r  r   r   r   r{   r  r  r|   r  r   )rb   r{   r  r   r  r+  r   r  r   rx   r  )r-   r  r2   r  r   r   r   r{   r  r  r4   r|   r[   decoder_outputss                 r/   r;   OPTModel.forward  s    " 2C1N-TXT_T_TqTq$8$D $++JjJj 	 "+!6IDKK<Q<Q	%0%<k$++B]B] ,, 
)%+'/!5)
 
 '-??+;;)77&11	
 	
r1   r8  r&  )r=   r>   r?   r@   r   r,   r1  r5  r9  r   r   r   r8   rC   r   r   listr   r   r   r   r   r   r   r;   rD   rE   rF   s   @r/   r)  r)    sT   y )*  1515,0KO59$(,0/3&*3715+
E,,-+
 !.+
 ELL)	+

 "%U->->(?(F"GH+
   1 12+
 D>+
 $D>+
 'tn+
 d^+
 u//0+
 !.+
 -.+
 
u--	.+
  +
r1   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\\\\R$                     \4      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 NFre   )
r+   r,   r)  r   r   rr   r   r   lm_headr   r,  s     r/   r,   OPTForCausalLM.__init__  sK     f%
 yy!;!;V=N=NUZ[ 	r1   c                 B    U R                   R                  R                  $ r   r   r+  r   r0  s    r/   r1  #OPTForCausalLM.get_input_embeddings      zz!!...r1   c                 8    XR                   R                  l        g r   rG  r4  s     r/   r5  #OPTForCausalLM.set_input_embeddings      */

'r1   c                 $    XR                   l        g r   r   r+  )r-   r+  s     r/   set_decoderOPTForCausalLM.set_decoder  s    $

r1   c                 .    U R                   R                  $ r   rN  r0  s    r/   r9  OPTForCausalLM.get_decoder  s    zz!!!r1   r  r2   r  r   r   labelsr   r{   r  r  r4   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logitsr   rx   r  r   )rb   r{   r  r  r   r+  rD  rZ   rY   r   loss_functionr   r   r   rx   r  )r-   r  r2   r  r   r   rS  r   r{   r  r  r4   r|   r[   r   rW  rV  s                    r/   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!//))
 	
r1   )rD  r   NNNNNNNNNNNN)r=   r>   r?   r@   _tied_weights_keysr,   r1  r5  rO  r9  r   r   r   r8   rC   r   r   r?  r   r   r   r   r   r   r   r;   rD   rE   rF   s   @r/   rA  rA    s   *+/0%"  1515,0KO59-1$(,0/3&*3715P
E,,-P
 !.P
 ELL)	P

 "%U->->(?(F"GHP
   1 12P
 ))*P
 D>P
 $D>P
 'tnP
 d^P
 u//0P
 !.P
 +,P
 
u,,	-P
  P
r1   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\\\\R                     \4      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$ )OPTForSequenceClassificationi_  rb   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   rr   r   scorer   r,  s     r/   r,   %OPTForSequenceClassification.__init__n  sT      ++f%
YYv994??QVW
 	r1   r  r2   r  r   r   rS  r   r{   r  r  r4   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	r   r2   r4   r  r   r   r{   r  r  r   r)   r   z=Cannot handle batch sizes > 1 if no padding token is defined.rN   )r   rP   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_classificationrU  )rb   r  r   ra  r   r   rq   rY   r   r8   int32r  argmaxrm   rn   r.   r=   problem_typer`  rP   r:   rB   r	   squeezer   r   r   r   r   rx   r  )r-   r  r2   r  r   r   rS  r   r{   r  r  r4   transformer_outputsrx   rW  r   r   last_non_pad_tokennon_pad_masktoken_indicespooled_logitsrV  loss_fctoutputs                           r/   r;   $OPTForSequenceClassification.forwardw  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
 	
r1   c                 B    U R                   R                  R                  $ r   rG  r0  s    r/   r1  1OPTForSequenceClassification.get_input_embeddings  rI  r1   c                 8    XR                   R                  l        g r   rG  r4  s     r/   r5  1OPTForSequenceClassification.set_input_embeddings  rL  r1   )r   r`  ra  r&  )r=   r>   r?   r@   r   r,   r   r   r8   rC   r   r   r?  r   r   r   r   r;   r1  r5  rD   rE   rF   s   @r/   r^  r^  _  sQ   y   156:15KO59-1$(,0/3&*37\
E,,-\
 !!2!23\
 E--.	\

 "%U->->(?(F"GH\
   1 12\
 ))*\
 D>\
 $D>\
 'tn\
 d^\
 u//0\
 
u66	7\
 \
|/0 0r1   r^  c                     ^  \ rS rSrS\4U 4S jjr\            SS\\R                     S\\R                     S\\R                     S\\\\R                     \4      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  rb   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   rr   r   
qa_outputsr   r,  s     r/   r,    OPTForQuestionAnswering.__init__  s@     f%
))F$>$>B 	r1   r  r2   r  r   r   start_positionsend_positionsr   r{   r  r  r4   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'
```Nrd  r   r   rN   r6   )ignore_indexr)   )rV  start_logits
end_logitsrx   r  )rb   r  r   r{  splitrk  rZ   r  r   clamprY   r   r   r   rx   r  )r-   r  r2   r  r   r   r}  r~  r   r{   r  r  r4   rl  rx   rW  r  r  
total_lossignored_indexrq  
start_lossend_lossrr  s                           r/   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
 	
r1   c                 B    U R                   R                  R                  $ r   rG  r0  s    r/   r1  ,OPTForQuestionAnswering.get_input_embeddingsI  rI  r1   c                 8    XR                   R                  l        g r   rG  r4  s     r/   r5  ,OPTForQuestionAnswering.set_input_embeddingsL  rL  r1   )r   r{  rZ  )r=   r>   r?   r@   r   r,   r   r   r8   rC   r   r   r?  r   r   r   r   r;   r1  r5  rD   rE   rF   s   @r/   ry  ry    sj   y   156:15KO596:48$(,0/3&*37_
E,,-_
 !!2!23_
 E--.	_

 "%U->->(?(F"GH_
   1 12_
 "%"2"23_
   0 01_
 D>_
 $D>_
 'tn_
 d^_
 u//0_
 
u22	3_
 _
B/0 0r1   ry  )rA  r)  r   r^  ry  )r   )ArA   typingr   r   r   r8   torch.utils.checkpointr   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   configuration_optr   !torch.nn.attention.flex_attentionr    integrations.flex_attentionr!   
get_loggerr=   rm   r   r#   Moduler   floatr^   r`   r   r   r   r)  rA  r^  ry  __all__r   r1   r/   <module>r     s    , ,    A A ! . ) > B 9  G & p p (  !!;J 
		H	%;BLL ;H %II%<<% 
% <<	%
 U\\*% % %.b)299 b)Jb0 bJ % % %6`
# `
F =
! =
 =
@k
' k
\ m0#5 m0m0` o00 o0 o0dr1   