
    h                     `   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)m*Z* ddl+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1m2Z2  e*jf                  e4      Z5 G d dejl                        Z7 G d dejl                        Z8 G d dejl                        Z9d Z:dMdZ;dejx                  de=dejx                  fdZ>	 	 	 dNdejl                  d ejx                  d!ejx                  d"ejx                  d#eejx                     d$e?d%ee?   d&ee?   de@ejx                  ejx                  f   fd'ZA G d( d)ejl                        ZB G d* d+ejl                        ZC G d, d-e      ZD G d. d/eD      ZE G d0 d1ejl                        ZF G d2 d3ejl                        ZG G d4 d5ejl                        ZHe' G d6 d7e"             ZId#eejx                     defd8ZJd9e=defd:ZKd;eej                     dejx                  d<ee=   dejx                  fd=ZM G d> d?eI      ZN G d@ dAeN      ZOe' G dB dCeI             ZPe' G dD dEeI             ZQ G dF dGeIe      ZRe' G dH dIeI             ZSe' G dJ dKeI             ZTg dLZUy)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is_torchdynamo_compiling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      j/var/www/html/eduruby.in/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)r0   rsqrtpowmeanr)   )r3   xs     r5   _normzT5GemmaRMSNorm._norm;   s4    5;;quuQx}}R}>IJJJr6   c                     | j                  |j                               }|d| j                  j                         z   z  }|j                  |      S )Ng      ?)r?   floatr2   type_as)r3   r>   outputs      r5   forwardzT5GemmaRMSNorm.forward>   sC    AGGI& 3!2!2!445~~a  r6   c                 ^    t        | j                  j                         d| j                   S )Nz, eps=)tupler2   shaper)   r3   s    r5   
extra_reprzT5GemmaRMSNorm.extra_reprE   s'    ))*+6$((<<r6   )gư>)
__name__
__module____qualname__intrA   r-   r?   rD   rI   __classcell__r4   s   @r5   r'   r'   5   s&    5C 5e 5
K!=r6   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r3   rU   r4   s     r5   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r6   c                     | j                  | j                  |            | j                  |      z  }| j                  |      }| j	                  |      }|S r+   )r]   rY   rZ   r`   r[   )r3   r>   hidden_statesr[   s       r5   rD   zT5GemmaMLP.forwardU   sH    DNN1$56aH]3NN=1	r6   )rJ   rK   rL   r-   rD   rN   rO   s   @r5   rQ   rQ   I   s    	7r6   rQ   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defaultrf   F)
persistent)r,   r-   hasattr
isinstancerh   dictgetri   max_position_embeddingsmax_seq_len_cachedoriginal_max_seq_lenrU   r   rope_init_fnattention_scalingregister_bufferrf   original_inv_freq)r3   rU   devicerf   r4   s       r5   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!%r6   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   r9   r#   mpscpuF)device_typeenabledr8   r(   dtype)rf   rA   expandrG   torx   rn   rj   strr0   autocast	transposecatcosru   sinr   )
r3   r>   position_idsinv_freq_expandedposition_ids_expandedr|   freqsembr   r   s
             r5   rD   zT5GemmaRotaryEmbedding.forwardp   sV    !MM$4-8>>@GGHZHZ[\H]_acdehhijiqiqr ,QaZ 8 > > @'1!((--'E!((--[`J`ahhmmfk^^UC 	5&,,.1F1L1L1NNYYZ[]^_E))UEN3C'')d444C'')d444C		5 vvAGGv$cff177f&;;;	5 	5s    BF%%F.r+   )rJ   rK   rL   r0   Tensor__annotations__r-   no_gradr   rD   rN   rO   s   @r5   re   re   \   s6    ll/" U]]_<  <r6   re   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..Nr9   r8   r~   )rG   r0   r   )r>   x1x2s      r5   rotate_halfr      sZ    	
3"!''"+"""	#B	
3q ""	#B99rc2YB''r6   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           r5   apply_rotary_pos_embr      sY    ( --
&C
--
&C3w;q>C/0G3w;q>C/0GGr6   rc   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)rG   r   reshape)rc   r   batchnum_key_value_headsslenhead_dims         r5   	repeat_kvr      so    
 2?1D1D.Ehz!!Qa"23::5BUW\^bdlmM  (;e(CT8TTr6   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      r8   r   r9   )r(   r   )ptrainingr#   )r   r   num_key_value_groupsr0   matmulr   tanhrG   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                 r5   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$$r6   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' paperrU   	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   rS   sliding_attention)r,   r-   rU   r   getattrrV   num_attention_headsr   r   r   query_pre_attn_scalarr   attention_dropout
is_decoder	is_causalr.   rX   attention_biasq_projk_projv_projo_projattn_logit_softcappinglayer_typessliding_windowr3   rU   r   r4   s      r5   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r6   past_key_valuepast_key_values4.58new_nameversionrc   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 Nr9   r#   r8   )r   r   r   eager        r`   r   r   r   rG   r   r   viewr   r   r   r   updater   r   rU   _attn_implementationr   r   r   r   r   r   r   r   r   r3   rc   r   r   r   r   r   input_shapehidden_shapequery_statesr   r   r   r   cache_kwargsattention_interfacer   r   s                     r5   rD   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((r6   NN)rJ   rK   rL   __doc__r%   rM   r-   r    r0   r   rF   r   r   
LongTensorr   r   rD   rN   rO   s   @r5   r   r      s    Gv2 vs v4 %0A6R ,059+)||+) #5<<#=>+) !.	+)
 "%+) !!1!12+) -.+) 
u||Xell3XeELL>Q5RR	S+) S+)r6   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   rU   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   FrS   zBCross-attention needs cross_attention_hidden_size to be specified.)r,   r-   rU   r   r   rV   r   r   r   r   r   r   r   r   r.   rX   r   r   cross_attention_hidden_sizer   r   r   r   
ValueErrorr   s      r5   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 6r6   r   r   r   r   rc   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.r9   r#   r8   Tr   r   r   )r   rG   r   r   r   r   
is_updatedrp   r   cross_attention_cacher   r   r   layerskeysvaluesr   rU   r   r   r   r   r   r   r   r   r   )r3   rc   r   r   r   r   r   r   r   r   curr_past_key_valueencoder_input_shapeencoder_hidden_shaper   r   r   r   r   s                     r5   rD   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((r6   r+   )rJ   rK   rL   r   r%   rM   r-   r    r0   r   r   r   r   r   rF   rD   rN   rO   s   @r5   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)r6   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)rU   r   r)   )r,   r-   rV   rU   r   r   attention_typer   	self_attnr'   rms_norm_epspre_self_attn_layernormpost_self_attn_layernormrQ   mlppre_feedforward_layernormpost_feedforward_layernormr.   r^   r_   r`   r   s      r5   r-   zT5GemmaEncoderLayer.__init__v  s    !--"$00;-
 (6f6H6HfNaNa'b$(6v7I7IvObOb(c%f%)78J8JPVPcPc)d&*89K9KQWQdQd*e'zz&"5"56r6   rc   r   r   r   r   c           	      >   |}| j                  |      } | j                  d||||d d|\  }}| j                  |      }|| j                  |      z   }|}| j	                  |      }| j                  |      }| j                  |      }|| j                  |      z   }|S )N)rc   r   r   r   r    )r  r  r  r`   r
  r	  r  )r3   rc   r   r   r   r   residual_s           r5   rD   zT5GemmaEncoderLayer.forward  s     !44]C)4>> 
' 3)% 
 
q 55mD 4<<#>> 66}E/77F 4<<#>>r6   r   )rJ   rK   rL   r   rM   r-   r0   r   rF   r   r   FloatTensorrD   rN   rO   s   @r5   r   r   s  s    7# 70 2637|| #5<<#=> !.	
 u//0 
u  !	"r6   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'   rV   r  pre_cross_attn_layernormpost_cross_attn_layernormr   s      r5   r-   zT5GemmaDecoderLayer.__init__  sW    +/vS(6v7I7IvObOb(c%)78J8JPVPcPc)d&r6   r   r   r   r   rc   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)rc   r   r   r   r   r  r   )rc   r   r   r   r  r  )r  r  self_attention_cacher  r`   r  r  r  r
  r	  r  )r3   rc   r   r   r   r   r  r   r   r  r   r  r  s                r5   rD   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<<#>>r6   )NNNFNNN)rJ   rK   rL   r   rM   r-   r    r0   r   rF   r   r   r
   boolr  rD   rN   rO   s   @r5   r  r    s   <e# e %0A6R
 26379=$)598<9=.||. #5<<#=>. !.	.
 u//0. ""56. D>. !!1!12.  (5. !) 6. 
		. S.r6   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.rV   
num_labelsclassifier_dropout_ratec                     t         |           t        j                  |      | _        t        j
                  ||      | _        y )N)r   )r,   r-   r.   r^   r`   rX   out_proj)r3   rV   r  r  r4   s       r5   r-   z"T5GemmaClassificationHead.__init__  s1    zz$;<		+z:r6   rc   r   c                 J    | j                  |      }| j                  |      }|S r+   )r`   r!  )r3   rc   s     r5   rD   z!T5GemmaClassificationHead.forward  s$    ]3m4r6   )r   )rJ   rK   rL   r   rM   rA   r-   r0   r   rD   rN   rO   s   @r5   r  r    s<    7;C ;S ;SX ;
U\\ ell r6   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.rV   
vocab_sizerT   c                 \    t         |           t        j                  |||      | _        y )NrS   )r,   r-   r.   rX   r!  )r3   rV   r%  rT   r4   s       r5   r-   zT5GemmaLMHead.__init__  s"    		+zEr6   rc   r   c                 (    | j                  |      }|S r+   )r!  )r3   rc   logitss      r5   rD   zT5GemmaLMHead.forward  s    }-r6   )F)rJ   rK   rL   r   rM   r  r-   r0   r   rD   rN   rO   s   @r5   r$  r$    s?    8FC FS F FU\\ ell r6   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   rU   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   TrS   r   )r,   r-   rU   r   r   rV   r   r   r   r   r   r   r   r   r.   rX   r   r   r   r   r   r   r   r   r   s      r5   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r6   r   r   r   r   rc   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                     r5   rD   zT5GemmaAttention.forward  r   r6   r   )rJ   rK   rL   r   r$   rM   r-   r    r0   r   rF   r   r   r   r   r   rD   rN   rO   s   @r5   r*  r*    s    Gv} v v2 %0A6R ,059+)||+) #5<<#=>+) !.	+)
 "%+) !!1!12+) -.+) 
u||Xell3XeELL>Q5RR	S+) S+)r6   r*  c                   b     e Zd ZU eed<   dZdZdgZdgZdZ	dZ
dZdZdZeedZ fdZd Z xZS )	T5GemmaPreTrainedModelrU   modelTT5GemmaBlockr   )rc   
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 y )Nr   r   r   )r=   stdrT   )r,   _init_weightsrU   initializer_rangern   r  r!  r2   rG   datanormal_rm   rT   zero_r$  tie_word_embeddings)r3   r   r3  scaler4   s       r5   r4  z$T5GemmaPreTrainedModel._init_weightsY  s   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 /r6   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. .r9   r#   ).r   z9self.model.config.decoder.pad_token_id has to be defined.i)	rU   decoderbos_token_idpad_token_idr   	new_zerosrG   clonemasked_fill_)r3   	input_idsdecoder_start_token_idr>  shifted_input_idss        r5   _shift_rightz#T5GemmaPreTrainedModel._shift_rightg  s     "&!4!4!A!A{{**77!)YZZ &//	@%.sCRCx%8%>%>%@#qr'"$:&!XYY 	&&'8D'@,O  r6   )rJ   rK   rL   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_outputsr4  rE  rN   rO   s   @r5   r.  r.  G  s]    &*#'(#4"5N!"&,&
O!r6   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   )r0   onesr  r   )rQ  rR  rS  rT  r   s       r5   
inner_maskz/bidirectional_mask_function.<locals>.inner_mask  s=    !::b

33i/033EJJ??r6   rM   r  )r   rW  s   ` r5   bidirectional_mask_functionrY    s9    
@c @S @ @c @d @
 r6   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.
    rQ  rR  rS  rT  r   c                 &    |z
  |k  ||z   k  z  S r+   r  )rQ  rR  rS  rT  r   s       r5   rW  z>sliding_window_bidirectional_mask_function.<locals>.inner_mask  s"    &/FU^=S4STTr6   rX  )r   rW  s   ` r5   *sliding_window_bidirectional_mask_functionr\    s9    
Uc US U Uc Ud U r6   	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#   rx   r   )r   r   rx   r0   longrV  rG   )r]  rc   r>  r   s       r5   make_default_2d_attention_maskra    s     RSS#|3778L8LejjY
    #]%8%8%;<]EYEYafakak
 r6   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)r1  rc   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  rU   F)r,   r-   r>  padding_idxr%  r.   	EmbeddingrV   embed_tokensr'   r  normre   
rotary_embgradient_checkpointing
ModuleListrangenum_hidden_layersr   r   r^   r_   r`   	post_initr   s      r5   r-   zT5GemmaEncoder.__init__  s     !.. ++LL):):F<N<NPTP`P`a"6#5#56;N;NO	0?&+#mmEJ6KcKcEde	 3e
 zz&"5"56 	 fs   D%rB  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#   rx   rU   input_embedsr   r   r   r   or_mask_function)rv  and_mask_functionfull_attentionr         ?r   )last_hidden_stater  )r   poprh  r0   arangerG   rx   r   ra  rU   r>  rn   ro   r   rY  r   r\  r   rj  tensorrV   r   r`   r   rn  r  ri  r   )r3   rB  r   r   rp  r   r   self_attn_mask_mappingmask_kwargsrc   r   
normalizerlayer_modules                r5   rD   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H 	L(#&|'B'BC	
 M	 		-0]3+
 	
r6   NNNN)rJ   rK   rL   r   r   rO  r-   r"   r   r0   r   r   r  r   r   r   rD   rN   rO   s   @r5   rc  rc    s    *,
$  15153759A
E,,-A
 !.A
 u//0	A

   1 12A
 +,A
 
A
 A
r6   rc  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)r1  cross_attentionsrc   c           	          t         |   |       t        j                  t	        |j
                        D cg c]  }t        ||       c}      | _        | j                          y c c}w r+   )	r,   r-   r.   rl  rm  rn  r  r   ro  r   s      r5   r-   zT5GemmaDecoder.__init__  sS     mmEJ6KcKcEde	 3e
 	 fs   A'rB  r   r   r   rp  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 )Nrr  z0`encoder_hidden_states` must be given in decoderre  r   r#   rs  rt  rx  ry  rv  rz  r   )r{  r   r  )r   rh  r   r
   r	   rU   get_seq_lengthr0   r}  rG   rx   r   ra  r>  rn   ro   r  r   r   rY  rj  r~  rV   r   r`   r   rn  r  ri  r   )r3   rB  r   r   r   rp  r  r   r   r  r   past_seen_tokensr  r  cross_attn_mask_mappingrc   r   r  r  s                      r5   rD   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H 	L(#&|'B'BC%'(89 M	 		-0]38++
 	
r6   )	NNNNNNNNN)rJ   rK   rL   r!   r   r   r  rO  r-   r"   r   r0   r   r   r
   r  r  r   r   r   rD   rN   rO   s   @r5   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
r6   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 )T5GemmaModelrU   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   rc  encoderr  r<  ro  ra   s     r5   r-   zT5GemmaModel.__init__w  sO     ((uvv%fnn5%fnn5r6   c                     | j                   S r+   r  rH   s    r5   get_encoderzT5GemmaModel.get_encoder  s    ||r6   c                 6    | j                   j                         S r+   r  get_input_embeddingsrH   s    r5   r  z!T5GemmaModel.get_input_embeddings      ||0022r6   c                 8    | j                   j                  |      S r+   r  set_input_embeddingsr3   new_embeddingss     r5   r  z!T5GemmaModel.set_input_embeddings      ||00@@r6   rB  r   r   decoder_input_idsdecoder_attention_maskdecoder_position_idsencoder_outputsr   rp  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)
        rB  r   r   rp  )	rB  r   r   rp  r   r   r  r  r   output_hidden_statesF)r{  r   decoder_hidden_statesdecoder_attentionsr  encoder_last_hidden_stater   encoder_attentionsr  )	r  r{  r<  r   r   rp   rc   r1  r  )r3   rB  r   r   r  r  r  r  r   rp  r  r  r   r   r   decoder_outputss                   r5   rD   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
 	
r6   )NNNNNNNNNNNN)rJ   rK   rL   r$   r-   r  r  r  r   r   r   r0   r   r  
BoolTensorr   r
   r   r  r   r   r   rD   rN   rO   s   @r5   r  r  u  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
r6   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 )T5GemmaEncoderModelrU   c                     t         |   |       |j                  rt        d      t	        |j
                        | _        | j                          y )NzQT5GemmaEncoderModel only supports encoder-only model. Use `T5GemmaModel` instead.)r,   r-   r  r   rc  r  ro  ra   s     r5   r-   zT5GemmaEncoderModel.__init__  s?     $$pqq%fnn5r6   c                 6    | j                   j                         S r+   r  rH   s    r5   r  z(T5GemmaEncoderModel.get_input_embeddings  r  r6   c                 8    | j                   j                  |      S r+   r  r  s     r5   r  z(T5GemmaEncoderModel.set_input_embeddings  r  r6   rB  r   r   rp  r   r   c                 4     | j                   d||||d|}|S )Nr  r  r  )r3   rB  r   r   rp  r   r  s          r5   rD   zT5GemmaEncoderModel.forward  s7     '$,, 
)%'	

 
 r6   r  )rJ   rK   rL   r$   r-   r  r  r   r   r   r0   r   r  r   r   r   r   rD   rN   rO   s   @r5   r  r    s    } 3A  156:3704E,,- !!2!23 u//0	
  - +, 
  r6   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_reprc   r(  rU   c                    d|_         t        | 	  |       t        |      | _        |j
                  j                  | _        t        |j
                  j                  | j                        | _	        d| _
        | j                          y )NTForMaskedLM)r  r,   r-   r  r/  r<  r%  r$  rV   lm_head	loss_typero  ra   s     r5   r-   z(T5GemmaForConditionalGeneration.__init__  sb    $(! !&)
 ..33$V^^%?%?Q&r6   c                 &    || j                   _        y r+   r  r!  r  s     r5   set_output_embeddingsz5T5GemmaForConditionalGeneration.set_output_embeddings  s     .r6   c                 .    | j                   j                  S r+   r  rH   s    r5   get_output_embeddingsz5T5GemmaForConditionalGeneration.get_output_embeddings   s    ||$$$r6   c                     | j                   j                  rC| j                  | j                  j                  | j                         j                                y y r+   )rU   r9  _tie_or_clone_weightsr  r!  get_decoderr  rH   s    r5   _tie_weightsz,T5GemmaForConditionalGeneration._tie_weights  s@    ;;**&&t||'<'<d>N>N>P>e>e>gh +r6   c                 .    | j                   j                  S r+   )r/  r  rH   s    r5   r  z+T5GemmaForConditionalGeneration.get_encoder      zz!!!r6   c                 .    | j                   j                  S r+   )r/  r<  rH   s    r5   r  z+T5GemmaForConditionalGeneration.get_decoder  r  r6   rB  r   r   r  r  r  r  r   rp  r  labelsr  r   logits_to_keepr   r   c                 x   | j                   r]| j                  j                  dk7  rDd| j                  j                   d}t               rt	        |      t
        j                  |       |||
| 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.                  |j0                  |j2                  |j4                  |j6                  	      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]`.
        r   ziIt is strongly recommended to train T5Gemma models with the `eager` attention implementation instead of `zp`. Use `eager` with `AutoModelForCausalLM.from_pretrained('<path-to-checkpoint>', attn_implementation='eager')`.N)rB  r   r   r  r  r  r  r   rp  r  r  r   )	lossr(  r   r  r  r  r  r   r  r  )r   rU   r   r   r   loggerwarning_oncerE  r/  r{  rn   rM   slicer  r  final_logit_softcappingr0   r   loss_functionr%  r   r   r  r  r  r  r   r  )r3   rB  r   r   r  r  r  r  r   rp  r  r  r  r   r  r   msgr  rc   slice_indicesr(  decoder_configr  s                          r5   rD   z'T5GemmaForConditionalGeneration.forward  s   : ==T[[==H#{{??@  Aqr  () o%##C("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

 
	
r6   c                 $    | j                  |      S r+   )rE  )r3   r  s     r5   %prepare_decoder_input_ids_from_labelszET5GemmaForConditionalGeneration.prepare_decoder_input_ids_from_labelsd  s      ((r6   )NNNNNNNNNNNNNr   )!rJ   rK   rL   _tied_weights_keys_tp_plan_pp_planr$   r-   r  r  r  r  r  r   r   r   r0   r   r  r  r   r
   r  r   rM   r   r   r   rF   r   rD   r  rN   rO   s   @r5   r  r    s   =?XY"M2H"o%6
$CDH	} 	/%i
""  156:378<=A;?599=59=A-1$(5934R
E,,-R
 !!2!23R
 u//0	R

 $E$4$45R
 !))9)9 :R
 'u'7'78R
 "/2R
 ""56R
   1 12R
  ((9(9:R
 ))*R
 D>R
 !!1!12R
 c5<</0R
  +,!R
" 
uU&&'8	9#R
  R
h)ELL )r6   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 ) T5GemmaForSequenceClassificationrU   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  rV   r<  r   r  scorero  r3   rU   r  rV   classifier_dropoutr4   s        r5   r-   z)T5GemmaForSequenceClassification.__init__j  s    
 )(:F%  ++$$%f-DJ,V4DJnn00$$ ..44K$V-FL.{DOOM_`
r6   c                 6    | j                   j                         S r+   r/  r  rH   s    r5   r  z5T5GemmaForSequenceClassification.get_input_embeddings      zz..00r6   c                 :    | j                   j                  |       y r+   r/  r  r3   r   s     r5   r  z5T5GemmaForSequenceClassification.set_input_embeddings      

''.r6   rB  r   r   r  r  r  r  rp  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  rp  r  r  r   r   rp  r   r#   z=Cannot handle batch sizes > 1 if no padding token is defined.r9   r_  )maxz will not detect padding tokens in `inputs_embeds`. Results may be unexpected if using padding tokens in conjunction with `inputs_embeds.`rs  )r(  r  pooled_logitsrU   r  r(  rc   r1  )rU   r  NotImplementedErrorr4   rJ   r   rE  r/  r{  r  r  rc   r1  r  rG   r>  r   rx   r0   int32r}  argmaxclampr  r  r  r   )r3   rB  r   r   r  r  r  r  rp  r  r  r   outputsr{  rc   r1  r(  
batch_sizelast_non_pad_tokennon_pad_masktoken_indicesr  r  s                          r5   rD   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' '!	
 	
r6   r+   
NNNNNNNNNN)rJ   rK   rL   r$   r   r  r-   r  r  r   r   r0   r   r   r   r  r   r   r   rD   rN   rO   s   @r5   r  r  h  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
r6   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 )T5GemmaForTokenClassificationrU   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        r5   r-   z&T5GemmaForTokenClassification.__init__  s    
 )(:F%  ++$$%f-DJ,V4DJnn00$$ ..44K$V-FL.{DOOM_`
r6   c                 6    | j                   j                         S r+   r  rH   s    r5   r  z2T5GemmaForTokenClassification.get_input_embeddings  r  r6   c                 :    | j                   j                  |       y r+   r  r  s     r5   r  z2T5GemmaForTokenClassification.set_input_embeddings  r  r6   rB  r   r   r  r  r  r  rp  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  )rU   r  r  r4   rJ   r   rE  r/  r{  r  r  rc   r1  r  r  r   )r3   rB  r   r   r  r  r  r  rp  r  r  r   r  r{  rc   r1  r(  r  s                     r5   rD   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$'!	
 	
r6   r+   r  )rJ   rK   rL   r$   r   r  r-   r  r  r   r   r0   r   r   r   r  r   r   r   rD   rN   rO   s   @r5   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
r6   r  )r  r  r  r.  r  r  )Nr#   )r   NN)Vtypingr   r   r   r0   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   r   utils.deprecationr    utils.genericr!   r"   configuration_t5gemmar$   r%   
get_loggerrJ   r  Moduler'   rQ   re   r   r   r   rM   r   rA   rF   r   r   r   r   r  r  r$  r*  r.  rY  r\  r   ra  rc  r  r  r  r  r  r  __all__r  r6   r5   <module>r     s  , - ,   ! C C ) R B 9  L F & l l 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 7!_ 7! 7!t
0F 
8 
s x (()<< 3- \\	"Z
+ Z
zj
^ j
Z O
) O
 O
d !0 ! !Hx)&<o x)v I
'= I
 I
X o
$: o
 o
dr6   