
    i#                        d dl mZ d dlmZ d dlZd dlm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mZ ddlmZ ddlmZ ddlmZmZmZm Z m!Z!m"Z" ddl#m$Z$m%Z% ddl&m'Z'm(Z( ddl)m*Z* ddl+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6  e/jn                  e8      Z9 G d dejt                        Z; G d dejt                        Z< G d dejt                        Z=d Z> ed      dNd       Z?dej                  deAd ej                  fd!ZB	 	 	 dOd"ejt                  d#ej                  d$ej                  d%ej                  d&ej                  dz  d'eCd(eCdz  d)eCdz  d eDej                  ej                  f   fd*ZE ee?       G d+ d,ejt                               ZF ee?       G d- d.ejt                               ZG G d/ d0e      ZH G d1 d2e      ZI G d3 d4ejt                        ZJ G d5 d6ejt                        ZKe- G d7 d8e(             ZLd&ej                  dz  d efd9ZMd:eAd efd;ZNd<ej                  dz  dej                  d=eAdz  d ej                  fd>ZP G d? d@eL      ZQ G dA dBeL      ZRe- G dC dDeL             ZSe- G dE dFeL             ZT G dG dHeLe      ZUe- G dI dJeL             ZVe- G dK dLeL             ZWg dMZXy)P    )Callable)OptionalN   )initialization)ACT2FN)CacheDynamicCacheEncoderDecoderCache)GenerationMixin)use_kernel_func_from_hubuse_kernelized_func)create_causal_mask!create_sliding_window_causal_mask)FlashAttentionKwargs)GradientCheckpointingLayer)BaseModelOutput)BaseModelOutputWithPastAndCrossAttentionsSeq2SeqLMOutputSeq2SeqModelOutputSequenceClassifierOutputTokenClassifierOutput)ROPE_INIT_FUNCTIONSdynamic_rope_update)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack)TransformersKwargsauto_docstringcan_return_tuplelogging)OutputRecordercheck_model_inputsmaybe_autocast   )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      v/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/transformers/models/t5gemma/modeling_t5gemma.pyr.   zT5GemmaRMSNorm.__init__7   s.    ll5;;s#34    c                     |t        j                  |j                  d      j                  dd      | j                  z         z  S )N   T)keepdim)r1   rsqrtpowmeanr*   )r4   xs     r6   _normzT5GemmaRMSNorm._norm<   s4    5;;quuQx}}R}>IJJJr7   c                     | j                  |j                               }|d| j                  j                         z   z  }|j                  |      S )N      ?)r@   floatr3   type_as)r4   r?   outputs      r6   forwardzT5GemmaRMSNorm.forward?   sC    AGGI& 3!2!2!445~~a  r7   c                 ^    t        | j                  j                         d| j                   S )Nz, eps=)tupler3   shaper*   r4   s    r6   
extra_reprzT5GemmaRMSNorm.extra_reprF   s'    ))*+6$((<<r7   )gư>)
__name__
__module____qualname__intrC   r.   r@   rF   rK   __classcell__r5   s   @r6   r(   r(   6   s&    5C 5e 5
K!=r7   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r4   rW   r5   s     r6   r.   zT5GemmaMLP.__init__K   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r7   c                     | j                  | j                  |            | j                  |      z  }| j                  |      }| j	                  |      }|S r,   )r_   r[   r\   rb   r]   )r4   r?   hidden_statesr]   s       r6   rF   zT5GemmaMLP.forwardV   sH    DNN1$56aH]3NN=1	r7   )rL   rM   rN   r.   rF   rP   rQ   s   @r6   rS   rS   J   s    	7r7   rS   c                        e Zd ZU ej                  ed<   ddef fdZe	 	 	 ddedz  de	d   de
dz  ded	ef   fd
       Z ej                         ed               Z xZS )T5GemmaRotaryEmbeddinginv_freqNrW   c                    t         |           |j                  | _        |j                  | _        || _        | j
                  j                  d   | _        | j                  }| j                  dk7  rt        | j                     } || j
                  |      \  }| _
        | j                  d|d       | j                  d|j                         d       y )N	rope_typedefaultrh   F)
persistentoriginal_inv_freq)r-   r.   max_position_embeddingsmax_seq_len_cachedoriginal_max_seq_lenrW   rope_parametersrj   compute_default_rope_parametersr   attention_scalingregister_bufferclone)r4   rW   devicerope_init_fnrh   r5   s        r6   r.   zT5GemmaRotaryEmbedding.__init__`   s    "("@"@$*$B$B!44[A!%!E!E>>Y&.t~~>L+7V+L($(ZeD0(..2BuUr7   rv   ztorch.deviceseq_lenreturnztorch.Tensorc                    | j                   d   }t        | dd      xs | j                  | j                  z  }d}d|t	        j
                  d|dt        j                        j                  |t        j                        |z  z  z  }||fS )	a  
        Computes the inverse frequencies according to the original RoPE implementation
        Args:
            config ([`~transformers.PreTrainedConfig`]):
                The model configuration.
            device (`torch.device`):
                The device to use for initialization of the inverse frequencies.
            seq_len (`int`, *optional*):
                The current sequence length. Unused for this type of RoPE.
        Returns:
            Tuple of (`torch.Tensor`, `float`), containing the inverse frequencies for the RoPE embeddings and the
            post-processing scaling factor applied to the computed cos/sin (unused in this type of RoPE).
        
rope_thetahead_dimNrB   r   r9   dtyperv   r~   )	rq   getattrrX   num_attention_headsr1   arangeint64torC   )rW   rv   rx   baser)   attention_factorrh   s          r6   rr   z6T5GemmaRotaryEmbedding.compute_default_rope_parametersp   s    & %%l3fj$/c63E3EIcIc3c U\\!S!5;;?BB&X]XcXcBdgjjk
 )))r7   c                 N   | 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        |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   r:   r$   mpscpuF)device_typeenabledr9   r)   r}   )rh   rC   expandrI   r   rv   
isinstancetypestrr#   	transposer1   catcosrs   sinr~   )
r4   r?   position_idsinv_freq_expandedposition_ids_expandedr   freqsembr   r   s
             r6   rF   zT5GemmaRotaryEmbedding.forward   sR    !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,   NNN)rL   rM   rN   r1   Tensor__annotations__r%   r.   staticmethodr   rO   rH   rC   rr   no_gradr   rF   rP   rQ   s   @r6   rg   rg   ]   s    llV} V  '++/"*$*(* t* 
~u$	%	* *: U]]_<  <r7   rg   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..Nr:   r9   r   )rI   r1   r   )r?   x1x2s      r6   rotate_halfr      sZ    	
3"!''"+"""	#B	
3q ""	#B99rc2YB''r7   rotary_pos_embc                     |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.
        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   unsqueeze_dimq_embedk_embeds          r6   apply_rotary_pos_embr      sY    & --
&C
--
&C3w;q>C/0G3w;q>C/0GGr7   re   n_repry   c                     | 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)rI   r   reshape)re   r   batchnum_key_value_headsslenr|   s         r6   	repeat_kvr      so    
 2?1D1D.Ehz!!Qa"23::5BUW\^bdlmM  (;e(CT8TTr7   modulequerykeyvalueattention_maskrb   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      r9   r   r:   )r)   r~   )ptrainingr$   )r|   r   num_key_value_groupsr1   matmulr   tanhrI   r/   
functionalsoftmaxfloat32r   r~   rb   r   
contiguous)r   r   r   r   r   rb   r   r   kwargs
key_statesvalue_statesattn_weightscausal_maskattn_outputs                 r6   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$$r7   c                   @    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z  dej                  dz  d	e
dz  d
ej                  dz  dee   de	ej                  ej                  dz  e	ej                     dz  f   fdZ xZS )T5GemmaSelfAttention=Multi-headed attention from 'Attention Is All You Need' paperrW   	layer_idxc                 V   t         |           t        |d      r|j                  |   nd | _        || _        || _        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
                  j2                  | _        | j                  dk(  r|j4                  | _        y d | _        y )Nlayer_typesr|   r   rU   sliding_attention)r-   r.   hasattrr   
layer_typerW   r   r   rX   r   r|   r   r   query_pre_attn_scalarr   attention_dropout
is_decoder	is_causalr/   rZ   attention_biasq_projk_projv_projo_projattn_logit_softcappingsliding_windowr4   rW   r   r5   s      r6   r.   zT5GemmaSelfAttention.__init__   s   ;B6=;Y&,,Y7_c"
F4F4F&JdJd4de$*$>$>&B\B\$\!33T9!%!>!>**ii : :T]] JQWQfQf
 ii : :T]] JQWQfQf
 ii : :T]] JQWQfQf
 ii&&68J8JQWQfQf
 '+kk&H&H#7;J]7]f33cgr7   Nre   position_embeddingsr   past_key_valuescache_positionr   ry   c                 D   |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                  j                  t              } || |	|
||f| j                  r| j                   nd| j"                  | j$                  | j&                  d|\  }} |j(                  g |d j+                         }| j-                  |      }||fS )Nr:   r$   r9   )r   r   r           rb   r   r   r   )rI   r|   r   viewr   r   r   r   updater   r   get_interfacerW   _attn_implementationr   r   r   r   r   r   r   r   r   )r4   re   r   r   r   r   r   input_shapehidden_shapequery_statesr   r   r   r   cache_kwargsattention_interfacer   r   s                     r6   rF   zT5GemmaSelfAttention.forward  s    $))#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(?(M(MKK,,.E)
 %8%
 /3mmD**LL..//%
 %
!\ *k));;;;FFHkk+.L((r7   NNNN)rL   rM   rN   __doc__r&   rO   r.   r1   r   rH   r   
LongTensorr   r   rF   rP   rQ   s   @r6   r   r      s    Gh2 hs h< IM.2(,26+)||+) #5<<#=>E+) t+	+)
 +) ((4/+) -.+) 
u||U\\D0%2E2LL	M+)r7   r   c                        e Zd ZdZdedef fdZ	 ddej                  dej                  dz  dej                  dz  d	e	dz  d
e
e   deej                  ej                  dz  eej                     dz  f   fdZ xZS )T5GemmaCrossAttentionr   rW   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   FrU   zBCross-attention needs cross_attention_hidden_size to be specified.)r-   r.   rW   r   r   rX   r   r|   r   r   r   r   r   r   r/   rZ   r   r   cross_attention_hidden_sizer   r   r   r   
ValueErrorr   s      r6   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 6r7   Nre   r   encoder_hidden_statesr   r   ry   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$                  j&                  t(              } || ||||f| j*                  r| j,                  nd| j.                  d | j0                  d|\  }} |j2                  g |d j5                         }| j7                  |      }||fS )Nz5Encoder hidden state is required for cross attention.r:   r$   r9   Tr   r   )r   rI   r|   r   r   r   
is_updatedgetr   cross_attention_cacher   r   r   layerskeysvaluesr   r   rW   r   r   r   r   r   r   r   r   r   )r4   re   r   r   r   r   r   r   r   r   curr_past_key_valuesencoder_input_shapeencoder_hidden_shaper   r   r   r   r   s                     r6   rF   zT5GemmaCrossAttention.forward[  s?    !(TUU#))#2.88b8$--8{{=166|DNNqRST&(3377GJ#2#H#H "*"7"="=cr"B#L%8#L"#Ldmm#L %:;@@AUV``abdefJ;;'<=BBCWXbbcdfghL*+?+F+FzS_aeaoao+p(
L=A**4>>:-44T^^DIIJ/66t~~FMML(?(M(MKK,,.E)
 %8%
 /3mmD**LL//%
 %
!\ *k));;;;FFHkk+.L((r7   r,   )rL   rM   rN   r   r&   rO   r.   r1   r   r   r   r   rH   rF   rP   rQ   s   @r6   r   r   ;  s    Gc2 cs cB )-3)||3) t+3)  %||d2	3)
 3) -.3) 
u||U\\D0%2E2LL	M3)r7   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z  dej                  dz  dej                  dz  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)rW   r   r*   )r-   r.   rX   rW   r   r   attention_typer   	self_attnr(   rms_norm_epspre_self_attn_layernormpost_self_attn_layernormrS   mlppre_feedforward_layernormpost_feedforward_layernormr/   r`   ra   rb   r   s      r6   r.   zT5GemmaEncoderLayer.__init__  s    !--"$00;-
 (6f6H6HfNaNa'b$(6v7I7IvObOb(c%f%)78J8JPVPcPc)d&*89K9KQWQdQd*e'zz&"5"56r7   Nre   r   r   r   ry   c           	      >   |}| j                  |      } | j                  d||||d d|\  }}| j                  |      }|| j                  |      z   }|}| j	                  |      }| j                  |      }| j                  |      }|| j                  |      z   }|S )N)re   r   r   r   r    )r  r
  r  rb   r  r  r  )r4   re   r   r   r   r   residual_s           r6   rF   zT5GemmaEncoderLayer.forward  s     !44]C)4>> 
' 3)% 
 
q 55mD 4<<#>> 66}E/77F 4<<#>>r7   r   )rL   rM   rN   r   rO   r.   r1   r   rH   r   FloatTensorrF   rP   rQ   s   @r6   r  r    s    7# 7. IM.204|| #5<<#=>E t+	
 &&- 
u  !	"r7   r  c                   X    e Zd ZdZdef fdZ	 	 	 	 	 	 	 	 ddej                  deej                  ej                  f   dz  dej                  dz  dej                  dz  d	e
dz  d
edz  dej                  dz  dej                  dz  dej                  dz  dej                  fdZ xZS )T5GemmaDecoderLayerz2Decoder sub-layer: an extra cross-attention layer.r   c                     t         |           |j                  | _        || _        || _        |j
                  |   | _        t        ||      | _        t        |j                  |j                        | _        t        |j                  |j                        | _        t        |      | _        t        |j                  |j                        | _        t        |j                  |j                        | _        t#        j$                  |j&                        | _        t+        ||      | _        t        |j                  |j                        | _        t        |j                  |j                        | _        y r  )r-   r.   rX   rW   r   r   r	  r   r
  r(   r  r  r  rS   r  r  r  r/   r`   ra   rb   r   
cross_attnpre_cross_attn_layernormpost_cross_attn_layernormr   s      r6   r.   zT5GemmaDecoderLayer.__init__  s&   !--"$00;-
 (6f6H6HfNaNa'b$(6v7I7IvObOb(c%f%)78J8JPVPcPc)d&*89K9KQWQdQd*e'zz&"5"56/vS(6v7I7IvObOb(c%)78J8JPVPcPc)d&r7   Nre   r   r   r   r   	use_cacher   r   encoder_attention_maskry   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)re   r   r   r   r   r  r   )re   r   r   r   r  r  )r  r
  self_attention_cacher  rb   r  r  r  r  r  r  )r4   re   r   r   r   r   r  r   r   r  r   r  r  s                r6   rF   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<<#>>r7   )NNNNFNNN)rL   rM   rN   r   rO   r.   r1   r   rH   r   r
   boolr  rF   rP   rQ   s   @r6   r  r    s    <e# e4 IM.2046:!&26596:.||. #5<<#=>E. t+	.
 &&-. -t3. $;. ((4/.  %||d2. !&t 3. 
		.r7   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.rX   
num_labelsclassifier_dropout_ratec                     t         |           t        j                  |      | _        t        j
                  ||      | _        y )N)r   )r-   r.   r/   r`   rb   rZ   out_proj)r4   rX   r#  r$  r5   s       r6   r.   z"T5GemmaClassificationHead.__init__  s1    zz$;<		+z:r7   re   ry   c                 J    | j                  |      }| j                  |      }|S r,   )rb   r&  )r4   re   s     r6   rF   z!T5GemmaClassificationHead.forward  s$    ]3m4r7   )r   )rL   rM   rN   r   rO   rC   r.   r1   r   rF   rP   rQ   s   @r6   r"  r"    s<    7;C ;S ;SX ;
U\\ ell r7   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.rX   
vocab_sizerV   c                 \    t         |           t        j                  |||      | _        y )NrU   )r-   r.   r/   rZ   r&  )r4   rX   r*  rV   r5   s       r6   r.   zT5GemmaLMHead.__init__!  s"    		+zEr7   re   ry   c                 (    | j                  |      }|S r,   )r&  )r4   re   logitss      r6   rF   zT5GemmaLMHead.forward%  s    }-r7   )F)rL   rM   rN   r   rO   r   r.   r1   r   rF   rP   rQ   s   @r6   r)  r)    s?    8FC FS F FU\\ ell r7   r)  c                        e Zd ZU eed<   dZdZddgZdgZdZ	dZ
dZdZdZe eedd	       eedd
	       eedd
	      gdZ ej(                          fd       Zd Z xZS )T5GemmaPreTrainedModelrW   modelTr  r  r   r$   r
  )index
layer_namer  )re   
attentionsc                 4   t         |   |       | j                  j                  }t	        |t
              r|j                  j                  j                  d   dz  }t        j                  |j                  j                  d||z         t        |j                  d      rA|j                  j                  *t        j                  |j                  j                         y y y t	        |t              rm| j                  j                  sV|j                  j                  j                  d   dz  }t        j                  |j                  j                  d||z         y y d|j                   j"                  v r t        j                  |j                         y y )Nr   r   r   )r>   stdrV   RMSNorm)r-   _init_weightsrW   initializer_ranger   r"  r&  r3   rI   initnormal_r   rV   zeros_r)  tie_word_embeddingsr5   rL   )r4   r   r5  scaler5   s       r6   r7  z$T5GemmaPreTrainedModel._init_weights@  s)    	f%kk++f78OO**003t;ELL//csU{Kv/FOO4H4H4TFOO001 5U/.;;22..44Q74?V__33#3;O 3 &**333KK& 4r7   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. .r:   r$   ).r   z9self.model.config.decoder.pad_token_id has to be defined.i)	rW   decoderbos_token_idpad_token_idr   	new_zerosrI   ru   masked_fill_)r4   	input_idsdecoder_start_token_idrA  shifted_input_idss        r6   _shift_rightz#T5GemmaPreTrainedModel._shift_rightR  s     "&!4!4!A!A{{**77!)YZZ &//	@%.sCRCx%8%>%>%@#qr'"$:&!XYY 	&&'8D'@,O  r7   )rL   rM   rN   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!   r   r   _can_record_outputsr1   r   r7  rG  rP   rQ   s   @r6   r/  r/  *  s    &*#.0EF#4"5N!"&,/q[Q/q\R0lS
 U]]_' '"!r7   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_idxry   c                     %t        j                  dt         j                        S | |f   j                  t         j                        S )Nr  r}   )r1   onesr   r   )rS  rT  rU  rV  r   s       r6   
inner_maskz/bidirectional_mask_function.<locals>.inner_maskr  s=    !::b

33i/033EJJ??r7   rO   r   )r   rY  s   ` r6   bidirectional_mask_functionr[  m  s9    
@c @S @ @c @d @
 r7   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.
    rS  rT  rU  rV  ry   c                 &    |z
  |k  ||z   k  z  S r,   r  )rS  rT  rU  rV  r   s       r6   rY  z>sliding_window_bidirectional_mask_function.<locals>.inner_mask  s"    &/FU^=S4STTr7   rZ  )r   rY  s   ` r6   *sliding_window_bidirectional_mask_functionr^  z  s9    
Uc US U Uc Ud U r7   	token_idsrA  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$   r   )r   r   rv   r1   longrX  rI   )r_  re   rA  r   s       r6   make_default_2d_attention_maskrb    s     RSS#|3778L8LejjY
    #]%8%8%;<]EYEYafakak
 r7   c                        e Zd ZeedZ fdZe	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dee   d	eez  fd
       Z xZS )T5GemmaEncoder)r3  re   c           	      T   t         |   |       |j                  | _        |j                  | _        t        j                  |j                  |j                  | j                        | _        t        |j                  |j                        | _        d| _        t        j                  t        |j                        D cg c]  }t!        ||       c}      | _        t        j$                  |j&                        | _        t+        |      | _        | j/                          y c c}w Nr  FrW   )r-   r.   rA  padding_idxr*  r/   	EmbeddingrX   embed_tokensr(   r  normgradient_checkpointing
ModuleListrangenum_hidden_layersr  r   r`   ra   rb   rg   
rotary_emb	post_initr   s      r6   r.   zT5GemmaEncoder.__init__       !.. ++LL):):F<N<NPTP`P`a"6#5#56;N;NO	&+#mmEJ6KcKcEde	 3e
 zz&"5"560? 	 f    D%NrD  r   r   inputs_embedsr   ry   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	}|}	t        j$                  | j                  j&                  d
z  |	j(                        }
|	|
z  }	| j+                  |	      }	| 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$   rv   rW   input_embedsr   r   r   r   or_mask_function)rz  and_mask_functionfull_attentionr         ?r}   )last_hidden_stater  )r   poprj  r1   r   rI   rv   r   rb  rW   rA  r   dictr   r[  r   r^  r   tensorrX   r~   rb   rp  r   ro  r	  rk  r   )r4   rD  r   r   rt  r   r   self_attn_mask_mappingmask_kwargsre   
normalizerr   layer_modules                r6   rF   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&
&" &\\$++"9"93">mFYFYZ
%
2]3"oom\J KK(G$++*G*GH 	L(#&|'B'BC	
 M	 		-0]3+
 	
r7   r   )rL   rM   rN   r   r  rQ  r.   r"   r1   r   r   r  r   r   rH   r   rF   rP   rQ   s   @r6   rd  rd    s    *,
$  .2.20426A
##d*A
 t+A
 &&-	A

 ((4/A
 +,A
 
	 A
 A
r7   rd  c                   j    e Zd Z eed       eed      edZ fdZe		 	 	 	 	 	 	 	 	 dde
j                  dz  de
j                  dz  de
j                  dz  d	edz  d
e
j                  dz  dedz  de
j                  dz  de
j                  dz  de
j                  dz  dee   deez  fd       Z xZS )T5GemmaDecoderr$   )r1  )r3  cross_attentionsre   c           	      T   t         |   |       |j                  | _        |j                  | _        t        j                  |j                  |j                  | j                        | _        t        |j                  |j                        | _        d| _        t        j                  t        |j                        D cg c]  }t!        ||       c}      | _        t        j$                  |j&                        | _        t+        |      | _        | j/                          y c c}w rf  )r-   r.   rA  rh  r*  r/   ri  rX   rj  r(   r  rk  rl  rm  rn  ro  r  r   r`   ra   rb   rg   rp  rq  r   s      r6   r.   zT5GemmaDecoder.__init__  rr  rs  NrD  r   r   r   rt  r  r   r   r  r   ry   c
                    |d u |d uz  rt        d      |t        d      || j                  |      }| j                  s,|r*|(t        t	        | j
                        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}|}t        j(                  | j
                  j*                  dz  |j,                        }||z  }| j/                  |      }| j1                  ||      }| j2                  d | j
                  j4                   D ]#  } |||||j6                     ||||||d	   f	i |
}% | j9                  |      }| j/                  |      }t;        ||      S )Nrv  z0`encoder_hidden_states` must be given in decoderrg  r   r$   rw  rx  r|  r}  rz  r~  r}   )r  r   r  )r   rj  r   r
   r	   rW   get_seq_lengthr1   r   rI   rv   r   rb  rA  r   r  r  r   r   r[  r  rX   r~   rb   rp  r   ro  r	  rk  r   )r4   rD  r   r   r   rt  r  r   r   r  r   past_seen_tokensr  r  cross_attn_mask_mappingre   r  r   r  s                      r6   rF   zT5GemmaDecoder.forward  s    -t";<YZZ (OPP  --i8M}}/F 2,dkk2RT`TbcO!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#'# &\\$++"9"93">mFYFYZ
%
2]3"oom\J KK(G$++*G*GH 	L(#&|'B'BC%'(89 M	 		-0]38++
 	
r7   )	NNNNNNNNN)rL   rM   rN   r!   r   r   r  rQ  r.   r"   r1   r   r   r
   r  r   r   r   rH   r   rF   rP   rQ   s   @r6   r  r    s/   $%9C*+@J,$  .2.2046:26!%26596:\
##d*\
 t+\
 &&-	\

 -t3\
 ((4/\
 $;\
 ((4/\
  %||d2\
 !&t 3\
 +,\
 
:	:\
 \
r7   r  c                       e Zd Zdef fdZd Zd Zee	 	 	 	 	 	 	 	 	 	 	 	 dde	j                  dz  de	j                  dz  de	j                  dz  d	e	j                  dz  d
e	j                  dz  de	j                  dz  dedz  dedz  de	j                  dz  de	j                  dz  dedz  de	j                  dz  dee   defd              Z xZS )T5GemmaModelrW   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   rd  encoderr  r?  rq  rc   s     r6   r.   zT5GemmaModel.__init__n  sO     ((uvv%fnn5%fnn5r7   c                 6    | j                   j                         S r,   r  get_input_embeddingsrJ   s    r6   r  z!T5GemmaModel.get_input_embeddingsy      ||0022r7   c                 8    | j                   j                  |      S r,   r  set_input_embeddingsr4   new_embeddingss     r6   r  z!T5GemmaModel.set_input_embeddings|      ||00@@r7   NrD  r   r   decoder_input_idsdecoder_attention_maskdecoder_position_idsencoder_outputsr   rt  decoder_inputs_embedsr  r   r   ry   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)
        rD  r   r   rt  )	rD  r   r   rt  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   r   re   r3  r  )r4   rD  r   r   r  r  r  r  r   rt  r  r  r   r   r   decoder_outputss                   r6   rF   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
 	
r7   )NNNNNNNNNNNN)rL   rM   rN   r%   r.   r  r  r   r   r1   r   r  
BoolTensorr   r
   r   r   r   r   r   rF   rP   rQ   s   @r6   r  r  l  sY   	} 	3A  .2370459:>8<266:-159!%268
##d*8
 ))D08
 &&-	8

 !++d28
 !& 0 04 78
 $..58
 )4/8
 -t38
 ||d*8
  %||d28
 $;8
 ((4/8
 +,8
 
8
  8
r7   r  c                        e Zd Zdef fdZd Zd Zee	 	 	 	 dde	j                  dz  de	j                  dz  de	j                  dz  d	e	j                  dz  d
ee   defd              Z xZS )T5GemmaEncoderModelrW   c                     t         |   |       |j                  rt        d      t	        |j
                        | _        | j                          y )NzQT5GemmaEncoderModel only supports encoder-only model. Use `T5GemmaModel` instead.)r-   r.   r  r   rd  r  rq  rc   s     r6   r.   zT5GemmaEncoderModel.__init__  s?     $$pqq%fnn5r7   c                 6    | j                   j                         S r,   r  rJ   s    r6   r  z(T5GemmaEncoderModel.get_input_embeddings  r  r7   c                 8    | j                   j                  |      S r,   r  r  s     r6   r  z(T5GemmaEncoderModel.set_input_embeddings  r  r7   NrD  r   r   rt  r   ry   c                 4     | j                   d||||d|}|S )Nr  r  )r  )r4   rD  r   r   rt  r   r  s          r6   rF   zT5GemmaEncoderModel.forward  s7     '$,, 
)%'	

 
 r7   r   )rL   rM   rN   r%   r.   r  r  r   r   r1   r   r  r   r   r   r   rF   rP   rQ   s   @r6   r  r    s    } 3A  .23704-1##d* ))D0 &&-	
 ||d* +, 
  r7   r  c            $       @    e Zd ZddiZddiZddgdgfiZdef fdZd	 Zd
 Z	e
e	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dedz  dedz  dej                  dz  dej                  dz  dej                  dz  dedz  dej                  dz  deej(                  z  dee   deej                     ez  f d              Zdej(                  fdZ xZS )T5GemmaForConditionalGenerationzlm_head.out_proj.weightz!model.decoder.embed_tokens.weightzlm_head.out_projcolwise_gather_outputre   r-  rW   c                    d|_         t        | 	  |       t        |      | _        |j
                  j                  | _        t        |j
                  j                  | j                        | _	        d| _
        | j                          y )NTForMaskedLM)r  r-   r.   r  r0  r?  r*  r)  rX   lm_head	loss_typerq  rc   s     r6   r.   z(T5GemmaForConditionalGeneration.__init__  sb    $(! !&)
 ..33$V^^%?%?Q&r7   c                 &    || j                   _        y r,   r  r&  r  s     r6   set_output_embeddingsz5T5GemmaForConditionalGeneration.set_output_embeddings  s     .r7   c                 .    | j                   j                  S r,   r  rJ   s    r6   get_output_embeddingsz5T5GemmaForConditionalGeneration.get_output_embeddings  s    ||$$$r7   NrD  r   r   r  r  r  r  r   rt  r  labelsr  r   logits_to_keepr   ry   c                    |||
| j                  |      } | j                  d|||||||||	|
||d|}|j                  }t        |t              rt        | d      n|}| j                  |dd|ddf         }| j                         j                  }|j                  3||j                  z  }t        j                  |      }||j                  z  }d}| | j                  ||| j                  fi |}t        |||j                  |j                   |j"                  |j$                  |j&                  |j(                  |j*                  	      S )a  
        decoder_position_ids (`torch.LongTensor` of shape `(batch_size, decoder_sequence_length)`, *optional*):
            Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0,
            config.decoder.n_positions - 1]`. [What are position IDs?](../glossary#position-ids)
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
            config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
            (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
        N)rD  r   r   r  r  r  r  r   rt  r  r  r   )	lossr-  r   r  r  r  r  r   r  r  )rG  r0  r  r   rO   slicer  get_decoderrW   final_logit_softcappingr1   r   loss_functionr*  r   r   r  r  r  r  r   r  )r4   rD  r   r   r  r  r  r  r   rt  r  r  r  r   r  r   r  re   slice_indicesr-  decoder_configr  s                         r6   rF   z'T5GemmaForConditionalGeneration.forward  sv   < "3";@U@] $ 1 1& 9.8djj /
)%/#9!5++'"7)/
 /
  (998B>SV8W~ot4]kmA}a,?@A))+2211=nDDDFZZ'FnDDDF%4%%ffdooPPD+;;"1"G"G.AA,==&5&O&O"1"G"G.AA

 
	
r7   c                 $    | j                  |      S r,   )rG  )r4   r  s     r6   %prepare_decoder_input_ids_from_labelszET5GemmaForConditionalGeneration.prepare_decoder_input_ids_from_labelsD  s      ((r7   )NNNNNNNNNNNNNr   )rL   rM   rN   _tied_weights_keys_tp_plan_pp_planr%   r.   r  r  r   r   r1   r   r  r  r   r
   r   rO   r   r   r   rH   r   rF   r  rP   rQ   s   @r6   r  r    s   35XY"$;<H"o%6
$CDH	} 	/%  .2370459:>8<266:26:>*.!%26-.I
##d*I
 ))D0I
 &&-	I

 !++d2I
 !& 0 04 7I
 $..5I
 )4/I
 -t3I
 ((4/I
  %0047I
   4'I
 $;I
 ((4/I
 ell*I
  +,!I
" 
u  	!O	3#I
  I
V)ELL )r7   r  c                       e Zd Zddededz  f fdZd Zd Zee		 	 	 	 	 	 	 	 	 	 dde
j                  dz  de
j                  dz  d	e
j                  dz  d
e
j                  dz  de
j                  dz  de
j                  dz  dedz  de
j                  dz  de
j                  dz  de
j                  dz  dee   defd              Z xZS ) T5GemmaForSequenceClassificationNrW   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  r0  r  r  rX   r?  r   r"  scorerq  r4   rW   r  rX   classifier_dropoutr5   s        r6   r.   z)T5GemmaForSequenceClassification.__init__J  s    
 )(:F%  ++$$%f-DJ,V4DJnn00$$ ..44K$V-FL.{DOOM_`
r7   c                 6    | j                   j                         S r,   r0  r  rJ   s    r6   r  z5T5GemmaForSequenceClassification.get_input_embeddingsa      zz..00r7   c                 :    | j                   j                  |       y r,   r0  r  r4   r   s     r6   r  z5T5GemmaForSequenceClassification.set_input_embeddingsd      

''.r7   rD  r   r   r  r  r  r  rt  r  r  r   ry   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  rt  r  r  r   r   rt  r   r$   z=Cannot handle batch sizes > 1 if no padding token is defined.r:   r   )maxz will not detect padding tokens in `inputs_embeds`. Results may be unexpected if using padding tokens in conjunction with `inputs_embeds.`rw  )r-  r  pooled_logitsrW   r  r-  re   r3  )rW   r  NotImplementedErrorr5   rL   r   rG  r0  r  r  r  re   r3  r  rI   rA  r   rv   r1   int32r   argmaxclamploggerwarning_oncer  r   )r4   rD  r   r   r  r  r  r  rt  r  r  r   outputsr  re   r3  r-  
batch_sizelast_non_pad_tokennon_pad_masktoken_indicesr  r  s                          r6   rF   z(T5GemmaForSequenceClassification.forwardg  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' '!	
 	
r7   r,   
NNNNNNNNNN)rL   rM   rN   r%   r   r.   r  r  r   r   r1   r   r   r   r  r   r   r   rF   rP   rQ   s   @r6   r  r  H  sN   } $+ .1/  .2.204596:8<2626:>*.i
##d*i
 t+i
 &&-	i

 !++d2i
 !&t 3i
 $..5i
 )4/i
 ((4/i
  %0047i
   4'i
 +,i
 
"i
  i
r7   r  c                       e Zd Zddededz  f fdZd Zd Zee		 	 	 	 	 	 	 	 	 	 dde
j                  dz  de
j                  dz  d	e
j                  dz  d
e
j                  dz  de
j                  dz  de
j                  dz  dedz  de
j                  dz  de
j                  dz  de
j                  dz  dee   defd              Z xZS )T5GemmaForTokenClassificationNrW   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        r6   r.   z&T5GemmaForTokenClassification.__init__  s    
 )(:F%  ++$$%f-DJ,V4DJnn00$$ ..44K$V-FL.{DOOM_`
r7   c                 6    | j                   j                         S r,   r  rJ   s    r6   r  z2T5GemmaForTokenClassification.get_input_embeddings  r  r7   c                 :    | j                   j                  |       y r,   r  r  s     r6   r  z2T5GemmaForTokenClassification.set_input_embeddings  r  r7   rD  r   r   r  r  r  r  rt  r  r  r   ry   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  )rW   r  r  r5   rL   r   rG  r0  r  r  r  re   r3  r  r  r   )r4   rD  r   r   r  r  r  r  rt  r  r  r   r  r  re   r3  r-  r  s                     r6   rF   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$'!	
 	
r7   r,   r  )rL   rM   rN   r%   r   r.   r  r  r   r   r1   r   r   r   r  r   r   r   rF   rP   rQ   s   @r6   r  r    sN   } $+ 01/  .2.204596:8<2626:>*.N
##d*N
 t+N
 &&-	N

 !++d2N
 !&t 3N
 $..5N
 )4/N
 ((4/N
  %0047N
   4'N
 +,N
 
N
  N
r7   r  )r  r  r  r/  r  r  )r$   )r   NN)Ycollections.abcr   typingr   r1   torch.nnr/    r   r9  activationsr   cache_utilsr   r	   r
   
generationr   integrationsr   r   masking_utilsr   r   modeling_flash_attention_utilsr   modeling_layersr   modeling_outputsr   r   r   r   r   r   modeling_rope_utilsr   r   modeling_utilsr   r   processing_utilsr   utilsr   r   r   r    utils.genericr!   r"   r#   configuration_t5gemmar%   r&   
get_loggerrL   r  Moduler(   rS   rg   r   r   r   rO   r   rC   rH   r   r   r   r  r  r"  r)  r/  r[  r^  r   rb  rd  r  r  r  r  r  r  __all__r  r7   r6   <module>r     sH  * %    & ! C C ) I R B 9  L F & R R O O E 
		H	%=RYY =( &><RYY ><B( *+ ,2	UU\\ 	U# 	U%,, 	U$    %II %<< % 
 % <<	 %
 LL4' %  % T\ % T\ % 5<<%& %F )*I)299 I) +I)X )*R)BII R) +R)j14 1hH4 HV		 	BII 	 ?!_ ?! ?!D
t0C 
 
s x $&<< * \\	"Z
+ Z
zv
+ v
r L
) L
 L
^ !0 ! !Hd)&<o d)N I
'= I
 I
X o
$: o
 o
dr7   