
    h0                     \   d dl mZmZmZ d dlZd dlmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZmZmZmZmZmZ ddlmZmZ ddl m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+ ddl,m-Z-m.Z. ddl/m0Z0m1Z1  e)jd                  e3      Z4 G d dejj                        Z6 G d dejj                        Z7 G d dejj                        Z8d Z9dMdZ:dejv                  de<dejv                  fdZ=	 	 	 dNdejj                  d ejv                  d!ejv                  d"ejv                  d#eejv                     d$e>d%ee>   d&ee>   de?ejv                  ejv                  f   fd'Z@ G d( d)ejj                        ZA G d* d+ejj                        ZB G d, d-e      ZC G d. d/eC      ZD G d0 d1ejj                        ZE G d2 d3ejj                        ZF G d4 d5ejj                        ZGe' G d6 d7e"             ZHd#eejv                     defd8ZId9e<defd:ZJd;eej                     dejv                  d<ee<   dejv                  fd=ZL G d> d?eH      ZM G d@ dAeM      ZNe' G dB dCeH             ZOe' G dD dEeH             ZP G dF dGeHe      ZQe' G dH dIeH             ZRe' G dJ dKeH             ZSg dLZTy)O    )CallableOptionalUnionN   )ACT2FN)CacheDynamicCacheEncoderDecoderCache)GenerationMixin)create_causal_mask!create_sliding_window_causal_mask)FlashAttentionKwargs)GradientCheckpointingLayer)BaseModelOutput)BaseModelOutputWithPastAndCrossAttentionsSeq2SeqLMOutputSeq2SeqModelOutputSequenceClassifierOutputTokenClassifierOutput)ROPE_INIT_FUNCTIONSdynamic_rope_update)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack)TransformersKwargsauto_docstringcan_return_tuplelogging)deprecate_kwarg)OutputRecordercheck_model_inputs   )T5GemmaConfigT5GemmaModuleConfigc                   <     e Zd Zddedef fdZd Zd Zd Z xZ	S )T5GemmaRMSNormdimepsc                     t         |           || _        t        j                  t        j                  |            | _        y N)super__init__r(   nn	Parametertorchzerosweight)selfr'   r(   	__class__s      k/var/www/html/aiagenthome/venv/lib/python3.12/site-packages/transformers/models/t5gemma/modeling_t5gemma.pyr,   zT5GemmaRMSNorm.__init__6   s.    ll5;;s#34    c                     |t        j                  |j                  d      j                  dd      | j                  z         z  S )N   T)keepdim)r/   rsqrtpowmeanr(   )r2   xs     r4   _normzT5GemmaRMSNorm._norm;   s4    5;;quuQx}}R}>IJJJr5   c                     | j                  |j                               }|d| j                  j                         z   z  }|j                  |      S )Ng      ?)r>   floatr1   type_as)r2   r=   outputs      r4   forwardzT5GemmaRMSNorm.forward>   sC    AGGI& 3!2!2!445~~a  r5   c                 ^    t        | j                  j                         d| j                   S )Nz, eps=)tupler1   shaper(   r2   s    r4   
extra_reprzT5GemmaRMSNorm.extra_reprE   s'    ))*+6$((<<r5   )gư>)
__name__
__module____qualname__intr@   r,   r>   rC   rH   __classcell__r3   s   @r4   r&   r&   5   s&    5C 5e 5
K!=r5   r&   c                   $     e Zd Z fdZd Z xZS )
T5GemmaMLPc                    t         |           || _        |j                  | _        |j                  | _        t        j                  | j                  | j                  d      | _        t        j                  | j                  | j                  d      | _        t        j                  | j                  | j                  d      | _	        t        |j                     | _        t        j                  |j                        | _        y )NFbias)r+   r,   confighidden_sizeintermediate_sizer-   Linear	gate_projup_proj	down_projr   hidden_activationact_fnDropoutdropout_ratedropoutr2   rT   r3   s     r4   r,   zT5GemmaMLP.__init__J   s    !--!'!9!94#3#3T5K5KRWXyy!1!143I3IPUV4#9#94;K;KRWXV556zz&"5"56r5   c                     | j                  | j                  |            | j                  |      z  }| j                  |      }| j	                  |      }|S r*   )r\   rX   rY   r_   rZ   )r2   r=   hidden_statesrZ   s       r4   rC   zT5GemmaMLP.forwardU   sH    DNN1$56aH]3NN=1	r5   )rI   rJ   rK   r,   rC   rM   rN   s   @r4   rP   rP   I   s    	7r5   rP   c                   x     e Zd ZU ej                  ed<   d fd	Z ej                         ed               Z	 xZ
S )T5GemmaRotaryEmbeddinginv_freqc                    t         |           t        |d      rUt        |j                  t
              r;|j                  j                  d|j                  j                  d            | _        nd| _        |j                  | _	        |j                  | _
        || _        t        | j                     | _        | j                  | j                  |      \  }| _        | j                  d|d       | j                   | _        y )Nrope_scaling	rope_typetypedefaultre   F)
persistent)r+   r,   hasattr
isinstancerg   dictgetrh   max_position_embeddingsmax_seq_len_cachedoriginal_max_seq_lenrT   r   rope_init_fnattention_scalingregister_bufferre   original_inv_freq)r2   rT   devicere   r3   s       r4   r,   zT5GemmaRotaryEmbedding.__init___   s    6>*z&:M:Mt/T#0044[&BUBUBYBYZ`BabDN&DN"("@"@$*$B$B!/?+/+<+<T[[&+Q($(ZeD!%r5   c                 b   | j                   d d d d f   j                         j                  |j                  d   dd      j	                  |j
                        }|d d d d d f   j                         }t        |j
                  j                  t              r/|j
                  j                  dk7  r|j
                  j                  nd}t        j                  |d      5  |j                         |j                         z  j                  dd      }t        j                  ||fd	      }|j                         | j                  z  }|j                         | j                  z  }	d d d        j	                  |j                   
      	j	                  |j                   
      fS # 1 sw Y   AxY w)Nr   r8   r"   mpscpuF)device_typeenabledr7   r'   dtype)re   r@   expandrF   torw   rm   ri   strr/   autocast	transposecatcosrt   sinr   )
r2   r=   position_idsinv_freq_expandedposition_ids_expandedr{   freqsembr   r   s
             r4   rC   zT5GemmaRotaryEmbedding.forwardp   sQ    !MM$4-8>>@GGHZHZ[\H]_acdehhijiqiqr ,QaZ 8 > > @'1!((--'E!((--[`J`ahhmmfk^^UC&,,.1F1L1L1NNYYZ[]^_E))UEN3C'')d444C'')d444C	 D vvAGGv$cff177f&;;; DCs    BF%%F.r*   )rI   rJ   rK   r/   Tensor__annotations__r,   no_gradr   rC   rM   rN   s   @r4   rd   rd   \   s6    ll/" U]]_<  <r5   rd   c                     | dd| j                   d   dz  f   }| d| j                   d   dz  df   }t        j                  | |fd      S )z*Rotates half the hidden dims of the input..Nr8   r7   r}   )rF   r/   r   )r=   x1x2s      r4   rotate_halfr      sZ    	
3"!''"+"""	#B	
3q ""	#B99rc2YB''r5   c                     |j                  |      }|j                  |      }| |z  t        |       |z  z   }||z  t        |      |z  z   }||fS )a  Applies Rotary Position Embedding to the query and key tensors.

    Args:
        q (`torch.Tensor`): The query tensor.
        k (`torch.Tensor`): The key tensor.
        cos (`torch.Tensor`): The cosine part of the rotary embedding.
        sin (`torch.Tensor`): The sine part of the rotary embedding.
        position_ids (`torch.Tensor`, *optional*):
            Deprecated and unused.
        unsqueeze_dim (`int`, *optional*, defaults to 1):
            The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
            sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
            that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
            k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
            cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
            the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
    Returns:
        `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
    )	unsqueezer   )qkr   r   r   unsqueeze_dimq_embedk_embeds           r4   apply_rotary_pos_embr      sY    ( --
&C
--
&C3w;q>C/0G3w;q>C/0GGr5   rb   n_repreturnc                     | j                   \  }}}}|dk(  r| S | dddddddddf   j                  |||||      } | j                  |||z  ||      S )z
    This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
    num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
    r"   N)rF   r   reshape)rb   r   batchnum_key_value_headsslenhead_dims         r4   	repeat_kvr      so    
 2?1D1D.Ehz!!Qa"23::5BUW\^bdlmM  (;e(CT8TTr5   modulequerykeyvalueattention_maskr_   scalingsoftcapc                    || j                   dz  }t        || j                        }	t        || j                        }
t        j                  ||	j                  dd            |z  }|||z  }t        j                  |      }||z  }|#|d d d d d d d |	j                  d   f   }||z   }t        j                  j                  |dt        j                        j                  |j                        }t        j                  j                  ||| j                        }t        j                  ||
      }|j                  dd      j!                         }||fS )	N      r7   r   r8   )r'   r   )ptrainingr"   )r   r   num_key_value_groupsr/   matmulr   tanhrF   r-   
functionalsoftmaxfloat32r   r   r_   r   
contiguous)r   r   r   r   r   r_   r   r   kwargs
key_statesvalue_statesattn_weightscausal_maskattn_outputs                 r4   eager_attention_forwardr      sA    //4'3 ; ;<JUF$?$?@L<<z';';Aq'ABWLL#g-zz,/#g-!$Q1.D
0@0@0D.D%DE#k1 ==((2U]](SVVW\WbWbcL==((6??([L,,|\:K''1-88:K$$r5   c                   R    e Zd ZdZdedef fdZ eddd      	 	 dd	ej                  d
e
ej                  ej                  f   deej                     dee   deej                     dee   de
ej                  eej                     ee
ej                        f   fd       Z xZS )T5GemmaSelfAttention=Multi-headed attention from 'Attention Is All You Need' paperrT   	layer_idxc                    t         |           || _        || _        t	        |d|j
                  |j                  z        | _        |j                  |j                  z  | _	        |j                  dz  | _        | j                  j                  | _        |j                  | _        t        j                   |j
                  |j                  | j                  z  |j"                        | _        t        j                   |j
                  |j                  | j                  z  |j"                        | _        t        j                   |j
                  |j                  | j                  z  |j"                        | _        t        j                   |j                  | j                  z  |j
                  |j"                        | _        | j                  j,                  | _        |j.                  |   dk(  r|j0                  | _        y d | _        y )Nr   r   rR   sliding_attention)r+   r,   rT   r   getattrrU   num_attention_headsr   r   r   query_pre_attn_scalarr   attention_dropout
is_decoder	is_causalr-   rW   attention_biasq_projk_projv_projo_projattn_logit_softcappinglayer_typessliding_windowr2   rT   r   r3   s      r4   r,   zT5GemmaSelfAttention.__init__   s   "
F4F4F&JdJd4de$*$>$>&B\B\$\!33T9!%!>!>**ii : :T]] JQWQfQf
 ii : :T]] JQWQfQf
 ii : :T]] JQWQfQf
 ii&&68J8JQWQfQf
 '+kk&H&H#7=7I7I)7TXk7kf33qur5   past_key_valuepast_key_values4.58new_nameversionrb   position_embeddingsr   cache_positionr   r   c                 `   |j                   d d }g |d| j                  }| j                  |      j                  |      j	                  dd      }	| j                  |      j                  |      j	                  dd      }
| j                  |      j                  |      j	                  dd      }|\  }}t        |	|
||      \  }	}
|'|||d}|j                  |
|| j                  |      \  }
}t        }| j                  j                  dk7  rt        | j                  j                     } || |	|
||f| j                  r| j                  nd| j                   | j"                  | j$                  d|\  }} |j&                  g |d j)                         }| j+                  |      }||fS Nr8   r"   r7   )r   r   r   eager        r_   r   r   r   rF   r   r   viewr   r   r   r   updater   r   rT   _attn_implementationr   r   r   r   r   r   r   r   r   r2   rb   r   r   r   r   r   input_shapehidden_shapequery_statesr   r   r   r   cache_kwargsattention_interfacer   r   s                     r4   rC   zT5GemmaSelfAttention.forward       $))#2.88b8$--8{{=166|DNNqRST[[/44\BLLQPQR
{{=166|DNNqRST&S#7jRUWZ#[ j&#&snUL'6'='=j,X\XfXfht'u$J(?;;++w6"9$++:Z:Z"[$7%
 /3mmD**LL..//%
 %
!\ *k));;;;FFHkk+.L((r5   NN)rI   rJ   rK   __doc__r$   rL   r,   r   r/   r   rE   r   r   
LongTensorr   r   rC   rM   rN   s   @r4   r   r      s    Gv2 vs v4 %0A6R ,059+)||+) #5<<#=>+) !.	+)
 "%+) !!1!12+) -.+) 
u||Xell3XeELL>Q5RR	S+) S+)r5   r   c                       e Zd ZdZdedef fdZ eddd      	 dd	ej                  d
e
ej                     de
ej                     de
e   dee   deej                  e
ej                     e
eej                        f   fd       Z xZS )T5GemmaCrossAttentionr   rT   r   c                    t         |           || _        || _        t	        |d|j
                  |j                  z        | _        |j                  |j                  z  | _	        |j                  dz  | _        | j                  j                  | _        d| _        t        j                  |j
                  |j                  | j                  z  |j                         | _        t        j                  |j$                  |j                  | j                  z  |j                         | _        t        j                  |j$                  |j                  | j                  z  |j                         | _        t        j                  |j                  | j                  z  |j
                  |j                         | _        | j                  j,                  | _        |j$                  t/        d      y )Nr   r   FrR   zBCross-attention needs cross_attention_hidden_size to be specified.)r+   r,   rT   r   r   rU   r   r   r   r   r   r   r   r   r-   rW   r   r   cross_attention_hidden_sizer   r   r   r   
ValueErrorr   s      r4   r,   zT5GemmaCrossAttention.__init__   s   "
F4F4F&JdJd4de$*$>$>&B\B\$\!33T9!%!>!>ii : :T]] JQWQfQf
 ii..0J0JT]]0Zagavav
 ii..0J0JT]]0Zagavav
 ii&&68J8JQWQfQf
 '+kk&H&H#--5abb 6r5   r   r   r   r   rb   r   encoder_hidden_statesr   r   c                    |t        d      |j                  d d }g |d| j                  }| j                  |      j	                  |      j                  dd      }|1|j                  j                  | j                        }	|j                  }
|	s|j                  d d }g |d| j                  }| j                  |      j	                  |      j                  dd      }| j                  |      j	                  |      j                  dd      }|
j                  ||| j                        \  }}d|j                  | j                  <   nF
j                  | j                     j                  }|
j                  | j                     j                  }t         }| j"                  j$                  dk7  rt&        | j"                  j$                     } || ||||f| j(                  r| j*                  nd| j,                  d | j.                  d|\  }} |j0                  g |d j3                         }| j5                  |      }||fS )	Nz5Encoder hidden state is required for cross attention.r8   r"   r7   Tr   r   r   )r   rF   r   r   r   r   
is_updatedro   r   cross_attention_cacher   r   r   layerskeysvaluesr   rT   r   r   r   r   r   r   r   r   r   )r2   rb   r   r   r   r   r   r   r   r   curr_past_key_valueencoder_input_shapeencoder_hidden_shaper   r   r   r   r   s                     r4   rC   zT5GemmaCrossAttention.forward<  sI    !(TUU#))#2.88b8$--8{{=166|DNNqRST&(3377GJ"1"G"G"*"7"="=cr"B#L%8#L"#Ldmm#L %:;@@AUV``abdefJ;;'<=BBCWXbbcdfghL*+>+E+EjR^`d`n`n+o(
L=A**4>>:,33DNNCHHJ.55dnnELLL(?;;++w6"9$++:Z:Z"[$7%
 /3mmD**LL//%
 %
!\ *k));;;;FFHkk+.L((r5   r*   )rI   rJ   rK   r   r$   rL   r,   r   r/   r   r   r   r   r   rE   rC   rM   rN   s   @r4   r   r     s    Gc2 cs c8 %0A6R ,03)||3) !.3)  (5	3)
 "%3) -.3) 
u||Xell3XeELL>Q5RR	S3) S3)r5   r   c                        e Zd ZdZdef fdZ	 	 d
dej                  deej                  ej                  f   de	ej                     de	ej                     deej                  f   f
d	Z xZS )T5GemmaEncoderLayerzEncoder sub-layer.r   c                 D   t         |           |j                  | _        || _        || _        |j
                  |   | _        t        ||      | _        t        |j                  |j                        | _        t        |j                  |j                        | _        t        |      | _        t        |j                  |j                        | _        t        |j                  |j                        | _        t#        j$                  |j&                        | _        y N)rT   r   r(   )r+   r,   rU   rT   r   r   attention_typer   	self_attnr&   rms_norm_epspre_self_attn_layernormpost_self_attn_layernormrP   mlppre_feedforward_layernormpost_feedforward_layernormr-   r]   r^   r_   r   s      r4   r,   zT5GemmaEncoderLayer.__init__v  s    !--"$00;-
 (6f6H6HfNaNa'b$(6v7I7IvObOb(c%f%)78J8JPVPcPc)d&*89K9KQWQdQd*e'zz&"5"56r5   rb   r   r   r   r   c           	      >   |}| j                  |      } | j                  d||||d d|\  }}| j                  |      }|| j                  |      z   }|}| j	                  |      }| j                  |      }| j                  |      }|| j                  |      z   }|S )N)rb   r   r   r   r    )r  r  r  r_   r	  r  r
  )r2   rb   r   r   r   r   residual_s           r4   rC   zT5GemmaEncoderLayer.forward  s     !44]C)4>> 
' 3)% 
 
q 55mD 4<<#>> 66}E/77F 4<<#>>r5   r   )rI   rJ   rK   r   rL   r,   r/   r   rE   r   r   FloatTensorrC   rM   rN   s   @r4   r   r   s  s    7# 70 2637|| #5<<#=> !.	
 u//0 
u  !	"r5   r   c                   l    e Zd ZdZdef fdZ eddd      	 	 	 	 	 	 	 ddej                  d	e	ej                  ej                  f   d
e
ej                     de
ej                     de
e   de
e   de
ej                     de
ej                     de
ej                     dej                  fd       Z xZS )T5GemmaDecoderLayerz2Decoder sub-layer: an extra cross-attention layer.r   c                     t         |   ||       t        ||      | _        t	        |j
                  |j                        | _        t	        |j
                  |j                        | _        y r  )	r+   r,   r   
cross_attnr&   rU   r  pre_cross_attn_layernormpost_cross_attn_layernormr   s      r4   r,   zT5GemmaDecoderLayer.__init__  sW    +/vS(6v7I7IvObOb(c%)78J8JPVPcPc)d&r5   r   r   r   r   rb   r   r   r   	use_cacher   r   encoder_attention_maskr   c
                    |}| j                  |      } | j                  d||||||j                  nd ||d|
\  }}| j                  |      }|| j	                  |      z   }|}| j                  |      } | j                  d|||	||d|
\  }}| j                  |      }|| j	                  |      z   }|}| j                  |      }| j                  |      }| j                  |      }|| j	                  |      z   }|S )N)rb   r   r   r   r   r  r   )rb   r   r   r   r  r  )r  r  self_attention_cacher  r_   r  r  r  r	  r  r
  )r2   rb   r   r   r   r   r  r   r   r  r   r  r  s                r4   rC   zT5GemmaDecoderLayer.forward  s>    !44]C)4>> 	
' 3)%DSD_O@@ei)	
 	
q 55mD 4<<#>> 55mD*4?? 
'"71+
 
q 66}E 4<<#>> 66}E/77F 4<<#>>r5   )NNNFNNN)rI   rJ   rK   r   rL   r,   r   r/   r   rE   r   r   r
   boolr  rC   rM   rN   s   @r4   r  r    s   <e# e %0A6R
 26379=$)598<9=.||. #5<<#=>. !.	.
 u//0. ""56. D>. !!1!12.  (5. !) 6. 
		. S.r5   r  c                   j     e Zd ZdZd	dededef fdZdej                  dej                  fdZ	 xZ
S )
T5GemmaClassificationHeadz-Head for sentence-level classification tasks.rU   
num_labelsclassifier_dropout_ratec                     t         |           t        j                  |      | _        t        j
                  ||      | _        y )N)r   )r+   r,   r-   r]   r_   rW   out_proj)r2   rU   r  r  r3   s       r4   r,   z"T5GemmaClassificationHead.__init__  s1    zz$;<		+z:r5   rb   r   c                 J    | j                  |      }| j                  |      }|S r*   )r_   r   )r2   rb   s     r4   rC   z!T5GemmaClassificationHead.forward  s$    ]3m4r5   )r   )rI   rJ   rK   r   rL   r@   r,   r/   r   rC   rM   rN   s   @r4   r  r    s<    7;C ;S ;SX ;
U\\ ell r5   r  c                   j     e Zd ZdZd	dededef fdZdej                  dej                  fdZ	 xZ
S )
T5GemmaLMHeadz.Head for language modeling (generation) tasks.rU   
vocab_sizerS   c                 \    t         |           t        j                  |||      | _        y )NrR   )r+   r,   r-   rW   r   )r2   rU   r$  rS   r3   s       r4   r,   zT5GemmaLMHead.__init__  s"    		+zEr5   rb   r   c                 (    | j                  |      }|S r*   )r   )r2   rb   logitss      r4   rC   zT5GemmaLMHead.forward  s    }-r5   )F)rI   rJ   rK   r   rL   r  r,   r/   r   rC   rM   rN   s   @r4   r#  r#    s?    8FC FS F FU\\ ell r5   r#  c                   R    e Zd ZdZdedef fdZ eddd      	 	 dd	ej                  d
e
ej                  ej                  f   deej                     dee   deej                     dee   de
ej                  eej                     ee
ej                        f   fd       Z xZS )T5GemmaAttentionr   rT   r   c                    t         |           || _        || _        t	        |d|j
                  |j                  z        | _        |j                  |j                  z  | _	        |j                  dz  | _        | j                  j                  | _        d| _        t        j                  |j
                  |j                  | j                  z  |j                         | _        t        j                  |j
                  |j                  | j                  z  |j                         | _        t        j                  |j
                  |j                  | j                  z  |j                         | _        t        j                  |j                  | j                  z  |j
                  |j                         | _        | j                  j*                  | _        |j,                  |   dk(  r|j.                  | _        y d | _        y )Nr   r   TrR   r   )r+   r,   rT   r   r   rU   r   r   r   r   r   r   r   r   r-   rW   r   r   r   r   r   r   r   r   r   s      r4   r,   zT5GemmaAttention.__init__  s   "
F4F4F&JdJd4de$*$>$>&B\B\$\!33T9!%!>!>ii : :T]] JQWQfQf
 ii : :T]] JQWQfQf
 ii : :T]] JQWQfQf
 ii&&68J8JQWQfQf
 '+kk&H&H#7=7I7I)7TXk7kf33qur5   r   r   r   r   rb   r   r   r   r   r   c                 `   |j                   d d }g |d| j                  }| j                  |      j                  |      j	                  dd      }	| j                  |      j                  |      j	                  dd      }
| j                  |      j                  |      j	                  dd      }|\  }}t        |	|
||      \  }	}
|'|||d}|j                  |
|| j                  |      \  }
}t        }| j                  j                  dk7  rt        | j                  j                     } || |	|
||f| j                  r| j                  nd| j                   | j"                  | j$                  d|\  }} |j&                  g |d j)                         }| j+                  |      }||fS r   r   r   s                     r4   rC   zT5GemmaAttention.forward  r   r5   r   )rI   rJ   rK   r   r#   rL   r,   r   r/   r   rE   r   r   r   r   r   rC   rM   rN   s   @r4   r)  r)    s    Gv} v v2 %0A6R ,059+)||+) #5<<#=>+) !.	+)
 "%+) !!1!12+) -.+) 
u||Xell3XeELL>Q5RR	S+) S+)r5   r)  c                   d     e Zd ZU eed<   dZdZddgZdgZdZ	dZ
dZdZdZeedZ fdZd	 Z xZS )
T5GemmaPreTrainedModelrT   modelTr   r  r   )rb   
attentionsc                 \   t         |   |       | j                  j                  }t	        |t
              r|j                  j                  j                  d   dz  }|j                  j                  j                  j                  d||z         t        |j                  d      rF|j                  j                  /|j                  j                  j                  j                          y y y t	        |t              rr| j                  j                  s[|j                  j                  j                  d   dz  }|j                  j                  j                  j                  d||z         y y d|j                   j"                  v r%|j                  j                  j                          y y )Nr   r   r   )r<   stdrS   RMSNorm)r+   _init_weightsrT   initializer_rangerm   r  r   r1   rF   datanormal_rl   rS   zero_r#  tie_word_embeddingsr3   rI   )r2   r   r1  scaler3   s       r4   r3  z$T5GemmaPreTrainedModel._init_weightsY  sE   f%kk++f78OO**003t;EOO""''//ScEk/Jv/FOO4H4H4T$$))//1 5U/.;;22..44Q74?&&++33#+3N 3 &**333MM$$& 4r5   c                 `   | j                   j                  j                  }| j                   j                  j                  }|t	        d      |j                  |j                        }|dddf   j                         |dddf<   ||d<   |t	        d      |j                  |dk(  |       |S )	z
        Shifts input_ids to the right, prepends the decoder_start_token_id, and handles
        pad_token_id replacement for labels that were -100.
        This is a common preparation step for decoder inputs in sequence-to-sequence models.
        Nz:self.model.config.decoder.bos_token_id has to be defined. .r8   r"   ).r   z9self.model.config.decoder.pad_token_id has to be defined.i)	rT   decoderbos_token_idpad_token_idr   	new_zerosrF   clonemasked_fill_)r2   	input_idsdecoder_start_token_idr=  shifted_input_idss        r4   _shift_rightz#T5GemmaPreTrainedModel._shift_rightj  s     "&!4!4!A!A{{**77!)YZZ &//	@%.sCRCx%8%>%>%@#qr'"$:&!XYY 	&&'8D'@,O  r5   )rI   rJ   rK   r#   r   base_model_prefixsupports_gradient_checkpointing_no_split_modules_skip_keys_device_placement_supports_flash_attn_supports_sdpa_supports_flex_attn_can_compile_fullgraph_supports_attention_backendr  r)  _can_record_outputsr3  rD  rM   rN   s   @r4   r-  r-  G  s_    &*#.0EF#4"5N!"&,&
'"!r5   r-  c           
      P     dt         dt         dt         dt         dt        f
 fd}|S )z4
    This creates bidirectional attention mask.
    	batch_idxhead_idxq_idxkv_idxr   c                     %t        j                  dt         j                        S | |f   j                  t         j                        S )Nr  r~   )r/   onesr  r   )rP  rQ  rR  rS  r   s       r4   
inner_maskz/bidirectional_mask_function.<locals>.inner_mask  s=    !::b

33i/033EJJ??r5   rL   r  )r   rV  s   ` r4   bidirectional_mask_functionrX    s9    
@c @S @ @c @d @
 r5   r   c           
      P     dt         dt         dt         dt         dt        f
 fd}|S )zH
    This creates bidirectional attention mask with sliding window.
    rP  rQ  rR  rS  r   c                 &    |z
  |k  ||z   k  z  S r*   r  )rP  rQ  rR  rS  r   s       r4   rV  z>sliding_window_bidirectional_mask_function.<locals>.inner_mask  s"    &/FU^=S4STTr5   rW  )r   rV  s   ` r4   *sliding_window_bidirectional_mask_functionr[    s9    
Uc US U Uc Ud U r5   	token_idsr=  c                    | <|t        d      | |k7  j                  |j                  t        j                        }|S t        j
                  |j                  d   |j                  d   f|j                  t        j                        }|S )z%Construct the default attention mask.z3`pad_token_id` is required for padding information.r   r"   rw   r   )r   r   rw   r/   longrU  rF   )r\  rb   r=  r   s       r4   make_default_2d_attention_maskr`    s     RSS#|3778L8LejjY
    #]%8%8%;<]EYEYafakak
 r5   c                        e Zd ZeedZ fdZe	 	 	 	 d
dee	j                     dee	j                     dee	j                     dee	j                     dee   defd	       Z xZS )T5GemmaEncoder)r/  rb   c           	      T   t         |   |       |j                  | _        |j                  | _        t        j                  |j                  |j                  | j                        | _        t        |j                  |j                        | _        t        |      | _        d| _        t        j                  t!        |j"                        D cg c]  }t%        ||       c}      | _        t        j(                  |j*                        | _        | j/                          y c c}w )Nr  rT   F)r+   r,   r=  padding_idxr$  r-   	EmbeddingrU   embed_tokensr&   r  normrd   
rotary_embgradient_checkpointing
ModuleListrangenum_hidden_layersr   r   r]   r^   r_   	post_initr   s      r4   r,   zT5GemmaEncoder.__init__  s     !.. ++LL):):F<N<NPTP`P`a"6#5#56;N;NO	0?&+#mmEJ6KcKcEdeEd	 3Ede
 zz&"5"56 	 fs   D%rA  r   r   inputs_embedsr   r   c           	         |d u |d uz  rt        d      |j                  dd        || j                  |      }t        j                  d|j
                  d   |j                        }||j                  d      }|!t        ||| j                  j                        }t        |x}t              sb| j                  |||d |d}t        di |dt        |      it        di |t!        | j                  j"                        t        |      dd	}|}	| j%                  |	|      }
t        j&                  | j                  j(                  d
z  |	j*                        }|	|z  }	| j-                  |	      }	| j.                  d | j                  j0                   D ]  } ||	|
||j2                     |fi |}	 | j5                  |	      }	| j-                  |	      }	t7        |	      S )N:You must specify exactly one of input_ids or inputs_embedsr   r   r"   rw   rT   input_embedsr   r   r   r   or_mask_function)ru  and_mask_functionfull_attentionr         ?r~   )last_hidden_stater  )r   poprg  r/   arangerF   rw   r   r`  rT   r=  rm   rn   r   rX  r   r[  r   ri  tensorrU   r   r_   r   rm  r  rh  r   )r2   rA  r   r   ro  r   r   self_attn_mask_mappingmask_kwargsrb   r   
normalizerlayer_modules                r4   rC   zT5GemmaEncoder.forward  s    -t";<YZZ 	

$d+  --i8Ma)<)<Q)?H\H\])33A6L!;I}VZVaVaVnVnoNNB0DI++ -"0"0#' ,K #5 #!#%@%P# &G &!&%OPTP[P[PjPj%k&A.&Q&
&" &"oom\J\\$++"9"93">mFYFYZ
%
2]3 KK(G$++*G*GHL(#&|'B'BC	
 M I 		-0]3+
 	
r5   NNNN)rI   rJ   rK   r   r   rN  r,   r!   r   r/   r   r   r  r   r   r   rC   rM   rN   s   @r4   rb  rb    s    *,
$  15153759A
E,,-A
 !.A
 u//0	A

   1 12A
 +,A
 
A
 A
r5   rb  c                   d    e Zd Z eed       eed      edZ fdZe		 	 	 	 	 	 	 	 	 dde
ej                     de
ej                     de
ej                     de
e   d	e
ej                     d
e
e   de
ej                     de
ej                     de
ej                     dee   defd       Z xZS )T5GemmaDecoderr"   )index)r/  cross_attentionsrb   c           	          t         |   |       t        j                  t	        |j
                        D cg c]  }t        ||       c}      | _        | j                          y c c}w r*   )	r+   r,   r-   rk  rl  rm  r  r   rn  r   s      r4   r,   zT5GemmaDecoder.__init__  sY     mmEJ6KcKcEdeEd	 3Ede
 	 fs   A'rA  r   r   r   ro  r  r   r   r  r   r   c
                    |d u |d uz  rt        d      |t        d      || j                  |      }| j                  s8|r6|4t        t	        | j
                        t	        | j
                              }|F||j                         nd}t        j                  |||j                  d   z   |j                        }||j                  d      }|#|!t        ||| j
                  j                        }t        |x}t              s8| j
                  |||||j                   nd |d}t#        di |t%        di |d}t        |	x}t              s-| j
                  ||	|d d d}d	t#        di |d
t'        |	      ii}|}| j)                  ||      }t        j*                  | j
                  j,                  dz  |j.                        }||z  }| j1                  |      }| j2                  d | j
                  j4                   D ]#  } |||||j6                     ||||||d	   f	i |
}% | j9                  |      }| j1                  |      }t;        ||      S )Nrq  z0`encoder_hidden_states` must be given in decoderrd  r   r"   rr  rs  rw  rx  ru  ry  r~   )rz  r   r  )r   rg  r   r
   r	   rT   get_seq_lengthr/   r|  rF   rw   r   r`  r=  rm   rn   r  r   r   rX  ri  r}  rU   r   r_   r   rm  r  rh  r   )r2   rA  r   r   r   ro  r  r   r   r  r   past_seen_tokensr~  r  cross_attn_mask_mappingrb   r   r  r  s                      r4   rC   zT5GemmaDecoder.forward  s    -t";<YZZ (OPP  --i8M}}/F1,dkk2RT`hlhshsTtuO!CRC^==?de"\\ "2]5H5H5K"KTaThThN )33A6L!o&=;I}VZVaVaVnVnoNNB0DI++ -"0"0KZKf?#G#Glp ,K #5"C{"C%F%U%U&"
 5KK1TR++ 5"8"0#' $K !"4 #!#%@AW%X#'# &"oom\J\\$++"9"93">mFYFYZ
%
2]3 KK(G$++*G*GHL(#&|'B'BC%'(89 M I 		-0]38++
 	
r5   )	NNNNNNNNN)rI   rJ   rK   r    r   r   r  rN  r,   r!   r   r/   r   r   r
   r  r  r   r   r   rC   rM   rN   s   @r4   r  r    s*   $%9C*+@J,  1515379=59$(598<9=Z
E,,-Z
 !.Z
 u//0	Z

 ""56Z
   1 12Z
 D>Z
 !!1!12Z
  (5Z
 !) 6Z
 +,Z
 
3Z
 Z
r5   r  c                       e Zd Zdef fdZd Zd Zd Zee		 	 	 	 	 	 	 	 	 	 	 	 dde
ej                     de
ej                     de
ej                     d	e
ej                     d
e
ej                     de
ej                     de
e   de
e   de
ej"                     de
ej"                     de
e   de
ej                     dee   defd              Z xZS )T5GemmaModelrT   c                     t         |   |       |j                  st        d      t	        |j
                        | _        t        |j                        | _        | j                          y )NzVT5GemmaModel only support encoder-decoder modeling. Use `T5GemmaEncoderModel` instead.)	r+   r,   is_encoder_decoderr   rb  encoderr  r;  rn  r`   s     r4   r,   zT5GemmaModel.__init__z  sO     ((uvv%fnn5%fnn5r5   c                     | j                   S r*   r  rG   s    r4   get_encoderzT5GemmaModel.get_encoder  s    ||r5   c                 6    | j                   j                         S r*   r  get_input_embeddingsrG   s    r4   r  z!T5GemmaModel.get_input_embeddings      ||0022r5   c                 8    | j                   j                  |      S r*   r  set_input_embeddingsr2   new_embeddingss     r4   r  z!T5GemmaModel.set_input_embeddings      ||00@@r5   rA  r   r   decoder_input_idsdecoder_attention_maskdecoder_position_idsencoder_outputsr   ro  decoder_inputs_embedsr  r   r   r   c                    | | j                   d||||	d|}|j                  } | j                  d||||
|||||d	|}t        |j                  |j                  |j                  dd      r|j                  n|j                  f|j                  |j                  |j                  |j                  |j                        S )aX  
        decoder_position_ids (`torch.LongTensor` of shape `(batch_size, decoder_sequence_length)`, *optional*):
            Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0,
            config.decoder.n_positions - 1]`. [What are position IDs?](../glossary#position-ids)
        rA  r   r   ro  )	rA  r   r   ro  r   r   r  r  r   output_hidden_statesF)rz  r   decoder_hidden_statesdecoder_attentionsr  encoder_last_hidden_stater   encoder_attentionsr  )	r  rz  r;  r   r   ro   rb   r/  r  )r2   rA  r   r   r  r  r  r  r   ro  r  r  r   r   r   decoder_outputss                   r4   rC   zT5GemmaModel.forward  s    . "*dll #-)+	
 O !0 A A&$,, 
'1-/+"7#1)
 
 "-??+;;zz0%8 #2"?"?!335.99,==&5&G&G"1"?"?.99
 	
r5   )NNNNNNNNNNNN)rI   rJ   rK   r#   r,   r  r  r  r   r   r   r/   r   r  
BoolTensorr   r
   r   r  r   r   r   rC   rM   rN   s   @r4   r  r  x  s_   	} 	3A  156:378<=A;?599=048<$(598
E,,-8
 !!2!238
 u//0	8

 $E$4$458
 !))9)9 :8
 'u'7'788
 "/28
 ""568
  -8
  (58
 D>8
 !!1!128
 +,8
 
8
  8
r5   r  c                        e Zd Zdef fdZd Zd Zee	 	 	 	 dde	e
j                     de	e
j                     de	e
j                     de	e
j                     d	ee   d
efd              Z xZS )T5GemmaEncoderModelrT   c                     t         |   |       |j                  rt        d      t	        |j
                        | _        | j                          y )NzQT5GemmaEncoderModel only supports encoder-only model. Use `T5GemmaModel` instead.)r+   r,   r  r   rb  r  rn  r`   s     r4   r,   zT5GemmaEncoderModel.__init__  s?     $$pqq%fnn5r5   c                 6    | j                   j                         S r*   r  rG   s    r4   r  z(T5GemmaEncoderModel.get_input_embeddings  r  r5   c                 8    | j                   j                  |      S r*   r  r  s     r4   r  z(T5GemmaEncoderModel.set_input_embeddings  r  r5   rA  r   r   ro  r   r   c                 4     | j                   d||||d|}|S )Nr  r  r  )r2   rA  r   r   ro  r   r  s          r4   rC   zT5GemmaEncoderModel.forward  s7     '$,, 
)%'	

 
 r5   r  )rI   rJ   rK   r#   r,   r  r  r   r   r   r/   r   r  r   r   r   r   rC   rM   rN   s   @r4   r  r    s    } 3A  156:3704E,,- !!2!23 u//0	
  - +, 
  r5   r  c            %       Z    e Zd ZddgZddiZddgdgfiZdef fdZd	 Zd
 Z	d Z
d Zd Zee	 	 	 	 	 	 	 	 	 	 	 	 	 	 d deej"                     deej$                     deej"                     deej"                     deej&                     deej"                     dee   dee   deej$                     deej$                     deej"                     dee   deej"                     deeej2                  f   dee   deeej$                     ef   f d              Zdej2                  fdZ xZ S )!T5GemmaForConditionalGenerationz!model.decoder.embed_tokens.weightzlm_head.out_proj.weightzlm_head.out_projcolwise_reprb   r'  rT   c                    d|_         t        | 	  |       t        |      | _        |j
                  j                  | _        t        |j
                  j                  | j                        | _	        d| _
        | j                          y )NTForMaskedLM)r  r+   r,   r  r.  r;  r$  r#  rU   lm_head	loss_typern  r`   s     r4   r,   z(T5GemmaForConditionalGeneration.__init__  sb    $(! !&)
 ..33$V^^%?%?Q&r5   c                 &    || j                   _        y r*   r  r   r  s     r4   set_output_embeddingsz5T5GemmaForConditionalGeneration.set_output_embeddings   s     .r5   c                 .    | j                   j                  S r*   r  rG   s    r4   get_output_embeddingsz5T5GemmaForConditionalGeneration.get_output_embeddings  s    ||$$$r5   c                     | j                   j                  rC| j                  | j                  j                  | j                         j                                y y r*   )rT   r8  _tie_or_clone_weightsr  r   get_decoderr  rG   s    r4   _tie_weightsz,T5GemmaForConditionalGeneration._tie_weights  s@    ;;**&&t||'<'<d>N>N>P>e>e>gh +r5   c                 .    | j                   j                  S r*   )r.  r  rG   s    r4   r  z+T5GemmaForConditionalGeneration.get_encoder      zz!!!r5   c                 .    | j                   j                  S r*   )r.  r;  rG   s    r4   r  z+T5GemmaForConditionalGeneration.get_decoder  r  r5   rA  r   r   r  r  r  r  r   ro  r  labelsr  r   logits_to_keepr   r   c                    |||
| j                  |      } | j                  d|||||||||	|
||d|}|j                  }t        |t              rt        | d      n|}| j                  |dd|ddf         }| j                         j                  }|j                  3||j                  z  }t        j                  |      }||j                  z  }d}| | j                  ||| j                  fi |}t        |||j                  |j                   |j"                  |j$                  |j&                  |j(                  |j*                  	      S )a  
        decoder_position_ids (`torch.LongTensor` of shape `(batch_size, decoder_sequence_length)`, *optional*):
            Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0,
            config.decoder.n_positions - 1]`. [What are position IDs?](../glossary#position-ids)
        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]`.
        N)rA  r   r   r  r  r  r  r   ro  r  r  r   )	lossr'  r   r  r  r  r  r   r  r  )rD  r.  rz  rm   rL   slicer  r  rT   final_logit_softcappingr/   r   loss_functionr$  r   r   r  r  r  r  r   r  )r2   rA  r   r   r  r  r  r  r   ro  r  r  r  r   r  r   r  rb   slice_indicesr'  decoder_configr  s                         r4   rC   z'T5GemmaForConditionalGeneration.forward  sv   < "3";@U@] $ 1 1& 9.8djj /
)%/#9!5++'"7)/
 /
  (998B>SV8W~ot4]kmA}a,?@A))+2211=nDDDFZZ'FnDDDF%4%%ffdooPPD+;;"1"G"G.AA,==&5&O&O"1"G"G.AA

 
	
r5   c                 $    | j                  |      S r*   )rD  )r2   r  s     r4   %prepare_decoder_input_ids_from_labelszET5GemmaForConditionalGeneration.prepare_decoder_input_ids_from_labels^  s      ((r5   )NNNNNNNNNNNNNr   )!rI   rJ   rK   _tied_weights_keys_tp_plan_pp_planr#   r,   r  r  r  r  r  r   r   r   r/   r   r  r  r   r
   r  r   rL   r   r   r   rE   r   rC   r  rM   rN   s   @r4   r  r    s   =?XY"M2H"o%6
$CDH	} 	/%i
""  156:378<=A;?599=59=A-1$(5934I
E,,-I
 !!2!23I
 u//0	I

 $E$4$45I
 !))9)9 :I
 'u'7'78I
 "/2I
 ""56I
   1 12I
  ((9(9:I
 ))*I
 D>I
 !!1!12I
 c5<</0I
  +,!I
" 
uU&&'8	9#I
  I
V)ELL )r5   r  c                       e Zd Zddedee   f fdZd Zd Ze	e
	 	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     d	eej                     d
eej                     deej                     dee   deej                     deej                     deej                     dee   defd              Z xZS ) T5GemmaForSequenceClassificationrT   r  c                    |||_         t        | 	  |       |j                  | _        |j                   rt	        |      | _        nt        |      | _        |j                  j                  }|j                   r|j                  j                  }t        |dd      }t        || j                  |      | _        | j                          y)z
        is_encoder_decoder (`Optional`, *optional*):
            Whether use encoder_decoder for sequence classification. When set to False, only encoder is used.
        Nr  皙?r  r+   r,   r  r  r.  r  r  rU   r;  r   r  scorern  r2   rT   r  rU   classifier_dropoutr3   s        r4   r,   z)T5GemmaForSequenceClassification.__init__d  s    
 )(:F%  ++$$%f-DJ,V4DJnn00$$ ..44K$V-FL.{DOOM_`
r5   c                 6    | j                   j                         S r*   r.  r  rG   s    r4   r  z5T5GemmaForSequenceClassification.get_input_embeddings{      zz..00r5   c                 :    | j                   j                  |       y r*   r.  r  r2   r   s     r4   r  z5T5GemmaForSequenceClassification.set_input_embeddings~      

''.r5   rA  r   r   r  r  r  r  ro  r  r  r   r   c                    | j                   j                  r'|%|#t        d| j                  j                   d      | j                   j                  r"| |	|t        d      | j                  |      }| j                   j                  rB | j                  |f||||||||	dd	|}|j                  }|j                  }|j                  }n; | j                  |f|||d|}|j                  }|j                  }|j                  }| j                  |      }||j                  d   }n|j                  d   }| j                   j                  |d	k7  rt        d
      | j                   j                  d}n||| j                   j                  k7  j!                  |j"                  t$        j&                        }t%        j(                  |j                  d   |j"                  t$        j&                        }||z  j+                  d      }| j                   j                  r[|d	z  }t%        j,                  ||j                  d   d	z
        }n.d}t.        j1                  | j                  j                   d       |t%        j(                  ||j"                        |f   }d}|
| j3                  ||
|| j                         }t5        ||||      S )  
        decoder_position_ids (`torch.LongTensor` of shape `(batch_size, decoder_sequence_length)`, *optional*):
            Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0,
            config.decoder.n_positions - 1]`. [What are position IDs?](../glossary#position-ids)
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        N8Passing input embeddings is currently not supported for  in encoder-decoder mode.If no `decoder_input_ids` or `decoder_inputs_embeds` are passed, `input_ids` cannot be `None`. Please pass either `input_ids` or `decoder_input_ids` or `decoder_inputs_embeds`.F	r   r   r  r  r  r  ro  r  r  r   r   ro  r   r"   z=Cannot handle batch sizes > 1 if no padding token is defined.r8   r^  )maxz will not detect padding tokens in `inputs_embeds`. Results may be unexpected if using padding tokens in conjunction with `inputs_embeds.`rr  )r'  r  pooled_logitsrT   r  r'  rb   r/  )rT   r  NotImplementedErrorr3   rI   r   rD  r.  rz  r  r  rb   r/  r  rF   r=  r   rw   r/   int32r|  argmaxclamploggerwarning_oncer  r   )r2   rA  r   r   r  r  r  r  ro  r  r  r   outputsrz  rb   r/  r'  
batch_sizelast_non_pad_tokennon_pad_masktoken_indicesr  r  s                          r4   rC   z(T5GemmaForSequenceClassification.forward  s   2 ;;))y/@]E^%J4>>KbKbJcc|} 
 ;;))/@/HMbMj  U 
 !% 1 1) <;;))*4$**+-)"3'=%9 /+&;+ +G !( 9 9#99M 33J'1tzz(-)+	(
 (G !( 9 9#11M ++J-. "+J&,,Q/J;;##+
a\]];;##+!#"%)A)AAEEfmmUZU`U`aL!LL)<V]]Z_ZeZefM"/,">!F!Fr!J{{--"a'"%*[[1CIZI`I`acIdghIh%i"!#>>**+ ,Z Z
 u||Jv}}MOaab%%VFR_hlhshs%tD' '!	
 	
r5   r*   
NNNNNNNNNN)rI   rJ   rK   r#   r   r  r,   r  r  r   r   r/   r   r   r   r  r   r   r   rC   rM   rN   s   @r4   r  r  b  sN   } (4. .1/  1515378<9=;?5959=A-1i
E,,-i
 !.i
 u//0	i

 $E$4$45i
 !) 6i
 'u'7'78i
 "/2i
   1 12i
  ((9(9:i
 ))*i
 +,i
 
"i
  i
r5   r  c                       e Zd Zddedee   f fdZd Zd Ze	e
	 	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     d	eej                     d
eej                     deej                     dee   deej                     deej                     deej                     dee   defd              Z xZS )T5GemmaForTokenClassificationrT   r  c                    |||_         t        | 	  |       |j                  | _        |j                   rt	        |      | _        nt        |      | _        |j                  j                  }|j                   r|j                  j                  }t        |dd      }t        || j                  |      | _        | j                          y)z
        is_encoder_decoder (`Optional`, *optional*):
            Whether use encoder_decoder for token classification. When set to False, only encoder is used.
        Nr  r  r  r  s        r4   r,   z&T5GemmaForTokenClassification.__init__  s    
 )(:F%  ++$$%f-DJ,V4DJnn00$$ ..44K$V-FL.{DOOM_`
r5   c                 6    | j                   j                         S r*   r  rG   s    r4   r  z2T5GemmaForTokenClassification.get_input_embeddings	  r  r5   c                 :    | j                   j                  |       y r*   r  r  s     r4   r  z2T5GemmaForTokenClassification.set_input_embeddings  r  r5   rA  r   r   r  r  r  r  ro  r  r  r   r   c                    | j                   j                  r'|%|#t        d| j                  j                   d      | j                   j                  r"| |	|t        d      | j                  |      }| j                   j                  rB | j                  |f||||||||	dd	|}|j                  }|j                  }|j                  }n; | j                  |f|||d|}|j                  }|j                  }|j                  }| j                  |      }d}|
| j                  ||
| j                         }t        ||||      S )	r  Nr  r  r  Fr  r  r  )rT   r  r  r3   rI   r   rD  r.  rz  r  r  rb   r/  r  r  r   )r2   rA  r   r   r  r  r  r  ro  r  r  r   r  rz  rb   r/  r'  r  s                     r4   rC   z%T5GemmaForTokenClassification.forward  s   4 ;;))y/@]E^%J4>>KbKbJcc|}  ;;))/@/HMbMj  U 
 !% 1 1) <;;))*4$**+-)"3'=%9 /+&;+ +G !( 9 9#99M 33J'1tzz(-)+	(
 (G !( 9 9#11M ++J-.%%ffdkkBD$'!	
 	
r5   r*   r  )rI   rJ   rK   r#   r   r  r,   r  r  r   r   r/   r   r   r   r  r   r   r   rC   rM   rN   s   @r4   r  r    sN   } (4. 01/  1515378<9=;?5959=A-1N
E,,-N
 !.N
 u//0	N

 $E$4$45N
 !) 6N
 'u'7'78N
 "/2N
   1 12N
  ((9(9:N
 ))*N
 +,N
 
N
  N
r5   r  )r  r  r  r-  r  r  )Nr"   )r   NN)Utypingr   r   r   r/   torch.nnr-   activationsr   cache_utilsr   r	   r
   
generationr   masking_utilsr   r   modeling_flash_attention_utilsr   modeling_layersr   modeling_outputsr   r   r   r   r   r   modeling_rope_utilsr   r   modeling_utilsr   r   processing_utilsr   utilsr   r   r   r   utils.deprecationr   utils.genericr    r!   configuration_t5gemmar#   r$   
get_loggerrI   r  Moduler&   rP   rd   r   r   r   rL   r   r@   rE   r   r   r   r   r  r  r#  r)  r-  rX  r[  r   r`  rb  r  r  r  r  r  r  __all__r  r5   r4   <module>r     s  , - ,   ! C C ) R B 9  L F & R R 0 ? E 
		H	%=RYY =( &!<RYY !<H(6	UU\\ 	U# 	U%,, 	U$ ## %II %<< % 
 % <<	 %
 U\\* %  % e_ % e_ % 5<<%& %FI)299 I)XS)BII S)l14 1h8- 8v		 	BII 	H)ryy H)V :!_ :! :!z
0F 
8 
s x (()<< 3- \\	"Z
+ Z
zj
^ j
Z O
) O
 O
d !0 ! !Ho)&<o o)d I
'= I
 I
X o
$: o
 o
dr5   