
    rh                     T   d dl mZmZmZ d dlZd dlmZ d dlmZm	Z	 ddl
mZ ddlmZmZmZ 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/m0Z0  e-jb                  e2      Z3 G d dejh                        Z5 G d dejh                        Z6 G d dejh                        Z7d Z8dLdZ9dejt                  de;dejt                  fdZ<	 	 	 dMdejh                  dejt                  d ejt                  d!ejt                  d"eejt                     d#e=d$ee=   d%ee=   de>ejt                  ejt                  f   fd&Z? G d' d(ejh                        Z@ G d) d*ejh                        ZA G d+ d,e      ZB G d- d.eB      ZC G d/ d0ejh                        ZD G d1 d2ejh                        ZE G d3 d4ejh                        ZFe* G d5 d6e%             ZGd"eejt                     defd7ZHd8e;defd9ZId:eej                     dejt                  d;ee;   dejt                  fd<ZK G d= d>eG      ZL G d? d@eL      ZMe* G dA dBeG             ZNe* G dC dDeG             ZO G dE dFeGe      ZPe* G dG dHeG             ZQe* G dI dJeG             ZRg dKZSy)N    )CallableOptionalUnionN)OutputRecordercheck_model_inputs   )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   )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      /var/www/html/ai-insurance-compliance-backend/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d fd	Z ej
                         ed               Z xZS )T5GemmaRotaryEmbeddingc                    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defaultinv_freqF)
persistent)r+   r,   hasattr
isinstancerf   dictgetrg   max_position_embeddingsmax_seq_len_cachedoriginal_max_seq_lenrT   r   rope_init_fnattention_scalingregister_bufferrj   original_inv_freq)r2   rT   devicerj   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)rj   r@   expandrF   torw   rm   rh   strr/   autocast	transposecatcosrt   sinr   )
r2   r=   position_idsinv_freq_expandedposition_ids_expandedr{   freqsembr   r   s
             r4   rC   zT5GemmaRotaryEmbedding.forwardn   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*   )	rI   rJ   rK   r,   r/   no_gradr   rC   rM   rN   s   @r4   rd   rd   \   s,    /" 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                   6    e Zd ZdZdedef fdZ	 	 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   rb   position_embeddingsr   past_key_value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'5'<'<ZW[WeWegs't$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/   TensorrE   r   r
   
LongTensorr   r   rC   rM   rN   s   @r4   r   r      s    Gv2 vs v> +/59+)||+) #5<<#=>+) !.	+)
 !+) !!1!12+) -.+) 
u||Xell3XeELL>Q5RR	S+)r5   r   c                        e Zd ZdZdedef fdZ	 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   rb   r   encoder_hidden_statesr   r   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.forward9  sI    !(TUU#))#2.88b8$--8{{=166|DNNqRST%'2266t~~FJ"0"F"F!"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<@))$..9,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   rE   rC   rM   rN   s   @r4   r   r     s    Gc2 cs cB +/3)||3) !.3)  (5	3)
 !3) -.3) 
u||Xell3XeELL>Q5RR	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__r  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   o  s    7# 70 2637|| #5<<#=> !.	
 u//0 
u  !	"r5   r   c                   P    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   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   rb   r   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)%BPB\>>>bf)	
 	
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   rE   r   r   r   boolr	  rC   rM   rN   s   @r4   r  r    s    <e# e 26378<$)598<9=.||. #5<<#=>. !.	.
 u//0. !!45. D>. !!1!12.  (5. !) 6. 
		.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                   6    e Zd ZdZdedef fdZ	 	 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   rb   r   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   rE   r   r
   r   r   r   rC   rM   rN   s   @r4   r#  r#    s    Gv} v v< +/59+)||+) #5<<#=>+) !.	+)
 !+) !!1!12+) -.+) 
u||Xell3XeELL>Q5RR	S+)r5   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 )	T5GemmaPreTrainedModelrT   modelTT5GemmaBlockpast_key_values)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 y )Nr   r   r   )r<   stdrS   )r+   _init_weightsrT   initializer_rangerm   r  r  r1   rF   datanormal_rl   rS   zero_r  tie_word_embeddings)r2   r   r-  scaler3   s       r4   r.  z$T5GemmaPreTrainedModel._init_weightsS  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 /r5   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_idr8  shifted_input_idss        r4   _shift_rightz#T5GemmaPreTrainedModel._shift_righta  s     "&!4!4!A!A{{**77!)YZZ &//	@%.sCRCx%8%>%>%@#qr'"$:&!XYY 	&&'8D'@,O  r5   )rI   rJ   rK   r#   __annotations__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_outputsr.  r?  rM   rN   s   @r4   r'  r'  A  s]    &*#'(#4"5N!"&,&
O!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   )rL  rM  rN  rO  r   s       r4   
inner_maskz/bidirectional_mask_function.<locals>.inner_mask  s=    !::b

33i/033EJJ??r5   rL   r  )r   rR  s   ` r4   bidirectional_mask_functionrT  |  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.
    rL  rM  rN  rO  r   c                 &    |z
  |k  ||z   k  z  S r*   r  )rL  rM  rN  rO  r   s       r4   rR  z>sliding_window_bidirectional_mask_function.<locals>.inner_mask  s"    &/FU^=S4STTr5   rS  )r   rR  s   ` r4   *sliding_window_bidirectional_mask_functionrW    s9    
Uc US U Uc Ud U r5   	token_idsr8  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/   longrQ  rF   )rX  rb   r8  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,   r8  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e	 3e
 zz&"5"56 	 fs   D%r<  r   r   inputs_embedsr   r   c           	         |d u |d uz  rt        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                  j.                   D ]  } ||	|
||j0                     |fi |}	 | j3                  |	      }	| j+                  |	      }	t5        |	      S )N:You must specify exactly one of input_ids or inputs_embedsr   r"   rw   rT   input_embedsr   r   r*  r   or_mask_function)rp  and_mask_functionfull_attentionr         ?r~   )last_hidden_stater  )r   rb  r/   arangerF   rw   r   r\  rT   r8  rm   rn   r   rT  r   rW  r   rd  tensorrU   r   r_   r   rh  r   rc  r   )r2   r<  r   r   rj  r   r   self_attn_mask_mappingmask_kwargsrb   r   
normalizerlayer_modules                r4   rC   zT5GemmaEncoder.forward  s    -t";<YZZ  --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+
 	
r5   NNNN)rI   rJ   rK   r   r   rJ  r,   r   r   r/   r   r   r	  r   r   r   rC   rM   rN   s   @r4   r^  r^    s    *,
$  15153759>
E,,->
 !.>
 u//0	>

   1 12>
 +,>
 
>
 >
r5   r^  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-   rf  rg  rh  r  r   ri  r   s      r4   r,   zT5GemmaDecoder.__init__  sS     mmEJ6KcKcEde	 3e
 	 fs   A'r<  r   r   r*  rj  r  r   r   r  r   r   c
                    |d u |d uz  rt        d      |t        d      || j                  |      }| j                  s!|r|t        t	               t	                     }|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 )Nrl  z0`encoder_hidden_states` must be given in decoder)r  r   r   r"   rm  rn  rr  rs  rp  rt  r~   )ru  r*  r  )r   rb  r   r   r   get_seq_lengthr/   rv  rF   rw   r   r\  rT   r8  rm   rn   r  r   r   rT  rd  rw  rU   r   r_   r   rh  r   rc  r   )r2   r<  r   r   r*  rj  r  r   r   r  r   past_seen_tokensrx  ry  cross_attn_mask_mappingrb   r   rz  r{  s                      r4   rC   zT5GemmaDecoder.forward  s    -t";<YZZ (OPP  --i8M}}/F1%1^&2nO !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++
 	
r5   )	NNNNNNNNN)rI   rJ   rK   r   r   r   r  rJ  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=]
E,,-]
 !.]
 u//0	]

 ""56]
   1 12]
 D>]
 !!1!12]
  (5]
 !) 6]
 +,]
 
3]
 ]
r5   r~  c                       e Zd Zdef f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   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   r^  encoderr~  r6  ri  r`   s     r4   r,   zT5GemmaModel.__init__q  sO     ((uvv%fnn5%fnn5r5   c                     | j                   S r*   r  rG   s    r4   get_encoderzT5GemmaModel.get_encoder|      ||r5   c                     | j                   S r*   )r6  rG   s    r4   get_decoderzT5GemmaModel.get_decoder  r  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   r<  r   r   decoder_input_idsdecoder_attention_maskdecoder_position_idsencoder_outputsr*  rj  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)
        r<  r   r   rj  )	r<  r   r   rj  r*  r   r  r  r   output_hidden_statesF)ru  r*  decoder_hidden_statesdecoder_attentionsr  encoder_last_hidden_stater   encoder_attentionsr  )	r  ru  r6  r   r*  ro   rb   r+  r  )r2   r<  r   r   r  r  r  r  r*  rj  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   r	  
BoolTensorr   r   r   r  r   r   r   rC   rM   rN   s   @r4   r  r  o  sd   	} 	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   r^  r  ri  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   r<  r   r   rj  r   r   c                 4     | j                   d||||d|}|S )Nr  r  r  )r2   r<  r   r   rj  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(  r6  r  r  rU   lm_head	loss_typeri  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   r3  _tie_or_clone_weightsr  r  r  r  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(  r6  rG   s    r4   r  z+T5GemmaForConditionalGeneration.get_decoder  r  r5   r<  r   r   r  r  r  r  r*  rj  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)r<  r   r   r  r  r  r  r*  rj  r  r  r   )	lossr!  r*  r  r  r  r  r   r  r  )r   rT   r   r    r   loggerwarning_oncer?  r(  ru  rm   rL   slicer  r  final_logit_softcappingr/   r   loss_functionr  r   r*  r  r  r  r  r   r  )r2   r<  r   r   r  r  r  r  r*  rj  r  r  r  r   r  r   msgr  rb   slice_indicesr!  decoder_configr  s                          r4   rC   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

 
	
r5   c                 $    | j                  |      S r*   )r?  )r2   r  s     r4   %prepare_decoder_input_ids_from_labelszET5GemmaForConditionalGeneration.prepare_decoder_input_ids_from_labelsa  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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 )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   r6  r   r  scoreri  r2   rT   r  rU   classifier_dropoutr3   s        r4   r,   z)T5GemmaForSequenceClassification.__init__g  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   r<  r   r   r  r  r  r  rj  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  rj  r  r  r   r   rj  r   r"   z=Cannot handle batch sizes > 1 if no padding token is defined.r8   rZ  )maxz will not detect padding tokens in `inputs_embeds`. Results may be unexpected if using padding tokens in conjunction with `inputs_embeds.`rm  )r!  r  pooled_logitsrT   r  r!  rb   r+  )rT   r  NotImplementedErrorr3   rI   r   r?  r(  ru  r  r  rb   r+  r  rF   r8  r   rw   r/   int32rv  argmaxclampr  r  r  r   )r2   r<  r   r   r  r  r  r  rj  r  r  r   outputsru  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  e  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   r<  r   r   r  r  r  r  rj  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   r?  r(  ru  r  r  rb   r+  r  r  r   )r2   r<  r   r   r  r  r  r  rj  r  r  r   r  ru  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)Ttypingr   r   r   r/   torch.nnr-   transformers.utils.genericr   r   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!   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'  rT  rW  r   r\  r^  r~  r  r  r  r  r  __all__r  r5   r4   <module>r     s  , - ,   I ! C C ) R B 9  L F & l l E 
		H	%=RYY =( &<RYY <D(6	UU\\ 	U# 	U%,, 	U$ ## %II %<< % 
 % <<	 %
 U\\* %  % e_ % e_ % 5<<%& %FH)299 H)VR)BII R)j14 1h7- 7t		 	BII 	G)ryy G)T 7!_ 7! 7!t
0F 
8 
s x (()<< 3- \\	"W
+ W
tm
^ m
` R
) R
 R
j !0 ! !Hx)&<o x)v I
'= I
 I
X o
$: o
 o
dr5   