
    +i1                        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l0m1Z1m2Z2 ddl3m4Z4m5Z5 ddl6m7Z7m8Z8  e/jr                  e:      Z; G d dejx                        Z= G d dejx                        Z> G d dejx                        Z?d Z@ ed      dOd       ZAdej                  d eCd!ej                  fd"ZD	 	 	 dPd#ejx                  d$ej                  d%ej                  d&ej                  d'ej                  dz  d(eEd)eEdz  d*eEdz  d!eFej                  ej                  f   fd+ZG eeA       G d, d-ejx                               ZH eeA       G d. d/ejx                               ZI G d0 d1e      ZJ G d2 d3e      ZK G d4 d5ejx                        ZL G d6 d7ejx                        ZMe- G d8 d9e(             ZNd'ej                  dz  d!efd:ZOd;eCd!efd<ZPd=ej                  dz  dej                  d>eCdz  d!ej                  fd?ZR G d@ dAeN      ZS G dB dCeN      ZTe- G dD dEeN             ZUe- G dF dGeN             ZV G dH dIeNe      ZWe- G dJ dKeN             ZXe- G dL dMeN             ZYg dNZZy)Q    )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)maybe_autocastmerge_with_config_defaults)OutputRecordercapture_outputs   )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      h/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/transformers/models/t5gemma/modeling_t5gemma.pyr/   zT5GemmaRMSNorm.__init__8   s.    ll5;;s#34    c                     |t        j                  |j                  d      j                  dd      | j                  z         z  S )N   T)keepdim)r2   rsqrtpowmeanr+   )r5   xs     r7   _normzT5GemmaRMSNorm._norm=   s4    5;;quuQx}}R}>IJJJr8   c                     | j                  |j                               }|d| j                  j                         z   z  }|j                  |      S )N      ?)rA   floatr4   type_as)r5   r@   outputs      r7   forwardzT5GemmaRMSNorm.forward@   sC    AGGI& 3!2!2!445~~a  r8   c                 ^    t        | j                  j                         d| j                   S )Nz, eps=)tupler4   shaper+   r5   s    r7   
extra_reprzT5GemmaRMSNorm.extra_reprG   s'    ))*+6$((<<r8   )gư>)
__name__
__module____qualname__intrD   r/   rA   rG   rL   __classcell__r6   s   @r7   r)   r)   7   s&    5C 5e 5
K!=r8   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_sizer0   Linear	gate_projup_proj	down_projr   hidden_activationact_fnDropoutdropout_ratedropoutr5   rX   r6   s     r7   r/   zT5GemmaMLP.__init__L   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r8   c                     | j                  | j                  |            | j                  |      z  }| j                  |      }| j	                  |      }|S r-   )r`   r\   r]   rc   r^   )r5   r@   hidden_statesr^   s       r7   rG   zT5GemmaMLP.forwardW   sH    DNN1$56aH]3NN=1	r8   )rM   rN   rO   r/   rG   rQ   rR   s   @r7   rT   rT   K   s    	7r8   rT   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_freqNrX   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defaultri   F)
persistentoriginal_inv_freq)r.   r/   max_position_embeddingsmax_seq_len_cachedoriginal_max_seq_lenrX   rope_parametersrk   compute_default_rope_parametersr   attention_scalingregister_bufferclone)r5   rX   devicerope_init_fnri   r6   s        r7   r/   zT5GemmaRotaryEmbedding.__init__a   s    "("@"@$*$B$B!44[A!%!E!E>>Y&.t~~>L+7V+L($(ZeD0(..2BuUr8   rw   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_dimNrC   r   r:   dtyperw   r   )	rr   getattrrY   num_attention_headsr2   arangeint64torD   )rX   rw   ry   baser*   attention_factorri   s          r7   rs   z6T5GemmaRotaryEmbedding.compute_default_rope_parametersq   s    & %%l3fj$/c63E3EIcIc3c U\\!S!5;;?BB&X]XcXcBdgjjk
 )))r8   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enabledr:   r*   r~   )ri   rD   expandrJ   r   rw   
isinstancetypestrr!   	transposer2   catcosrt   sinr   )
r5   r@   position_idsinv_freq_expandedposition_ids_expandedr   freqsembr   r   s
             r7   rG   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)rM   rN   rO   r2   Tensor__annotations__r&   r/   staticmethodr   rP   rI   rD   rs   no_gradr   rG   rQ   rR   s   @r7   rh   rh   ^   s    llV} V  '++/"*$*(* t* 
~u$	%	* *: U]]_<  <r8   rh   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;   r:   r   )rJ   r2   r   )r@   x1x2s      r7   rotate_halfr      sZ    	
3"!''"+"""	#B	
3q ""	#B99rc2YB''r8   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          r7   apply_rotary_pos_embr      sY    & --
&C
--
&C3w;q>C/0G3w;q>C/0GGr8   rf   n_reprz   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)rJ   r   reshape)rf   r   batchnum_key_value_headsslenr}   s         r7   	repeat_kvr      so    
 2?1D1D.Ehz!!Qa"23::5BUW\^bdlmM  (;e(CT8TTr8   modulequerykeyvalueattention_maskrc   scalingsoftcapc                 |   || j                   dz  }t        || j                        }	t        || j                        }
t        j                  ||	j                  dd            |z  }|||z  }t        j                  |      }||z  }|||z   }t        j                  j                  |dt        j                        j                  |j                        }t        j                  j                  ||| j                        }t        j                  ||
      }|j                  dd      j                         }||fS )N      r:   r   r;   )r*   r   )ptrainingr%   )r}   r   num_key_value_groupsr2   matmulr   tanhr0   
functionalsoftmaxfloat32r   r   rc   r   
contiguous)r   r   r   r   r   rc   r   r   kwargs
key_statesvalue_statesattn_weightsattn_outputs                r7   eager_attention_forwardr      s    //4'3 ; ;<JUF$?$?@L<<z';';Aq'ABWLL#g-zz,/#g-!#n4 ==((2U]](SVVW\WbWbcL==((6??([L,,|\:K''1-88:K$$r8   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' paperrX   	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   rV   sliding_attention)r.   r/   hasattrr   
layer_typerX   r   r   rY   r   r}   r   r   query_pre_attn_scalarr   attention_dropout
is_decoder	is_causalr0   r[   attention_biasq_projk_projv_projo_projattn_logit_softcappingsliding_windowr5   rX   r   r6   s      r7   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r8   Nrf   position_embeddingsr   past_key_valuescache_positionr   rz   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%   r:   )r   r   r           rc   r   r   r   )rJ   r}   r   viewr   r   r   r   updater   r   get_interfacerX   _attn_implementationr   r   r   r   r   r   r   r   r   )r5   rf   r   r   r   r   r   input_shapehidden_shapequery_statesr   r   r   r   cache_kwargsattention_interfacer   r   s                     r7   rG   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((r8   NNNN)rM   rN   rO   __doc__r'   rP   r/   r2   r   rI   r   
LongTensorr   r   rG   rQ   rR   s   @r7   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+)r8   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   rX   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   FrV   zBCross-attention needs cross_attention_hidden_size to be specified.)r.   r/   rX   r   r   rY   r   r}   r   r   r   r   r   r   r0   r[   r   r   cross_attention_hidden_sizer   r   r   r   
ValueErrorr   s      r7   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 6r8   Nrf   r   encoder_hidden_statesr   r   rz   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%   r:   Tr   r   )r   rJ   r}   r   r   r   
is_updatedgetr   cross_attention_cacher   r   r   layerskeysvaluesr   r   rX   r   r   r   r   r   r   r   r   r   )r5   rf   r   r   r   r   r   r   r   r   curr_past_key_valuesencoder_input_shapeencoder_hidden_shaper   r   r   r   r   s                     r7   rG   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((r8   r-   )rM   rN   rO   r   r'   rP   r/   r2   r   r   r   r   rI   rG   rQ   rR   s   @r7   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)r8   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)rX   r   r+   )r.   r/   rY   rX   r   r   attention_typer   	self_attnr)   rms_norm_epspre_self_attn_layernormpost_self_attn_layernormrT   mlppre_feedforward_layernormpost_feedforward_layernormr0   ra   rb   rc   r   s      r7   r/   zT5GemmaEncoderLayer.__init__  s    !--"$00;-
 (6f6H6HfNaNa'b$(6v7I7IvObOb(c%f%)78J8JPVPcPc)d&*89K9KQWQdQd*e'zz&"5"56r8   Nrf   r   r   r   rz   c           	      >   |}| j                  |      } | j                  d||||d d|\  }}| j                  |      }|| j                  |      z   }|}| j	                  |      }| j                  |      }| j                  |      }|| j                  |      z   }|S )N)rf   r   r   r   r    )r  r	  r  rc   r  r  r  )r5   rf   r   r   r   r   residual_s           r7   rG   zT5GemmaEncoderLayer.forward  s     !44]C)4>> 
' 3)% 
 
q 55mD 4<<#>> 66}E/77F 4<<#>>r8   r   )rM   rN   rO   r   rP   r/   r2   r   rI   r   FloatTensorrG   rQ   rR   s   @r7   r  r    s    7# 7. IM.204|| #5<<#=>E t+	
 &&- 
u  !	"r8   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/   rY   rX   r   r   r  r   r	  r)   r
  r  r  rT   r  r  r  r0   ra   rb   rc   r   
cross_attnpre_cross_attn_layernormpost_cross_attn_layernormr   s      r7   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&r8   Nrf   r   r   r   r   	use_cacher   r   encoder_attention_maskrz   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)rf   r   r   r   r   r  r   )rf   r   r   r   r  r  )r  r	  self_attention_cacher  rc   r  r  r  r  r  r  )r5   rf   r   r   r   r   r  r   r   r  r   r  r  s                r7   rG   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<<#>>r8   )NNNNFNNN)rM   rN   rO   r   rP   r/   r2   r   rI   r   r
   boolr  rG   rQ   rR   s   @r7   r  r    s    <e# e4 IM.2046:!&26596:.||. #5<<#=>E. t+	.
 &&-. -t3. $;. ((4/.  %||d2. !&t 3. 
		.r8   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.rY   
num_labelsclassifier_dropout_ratec                     t         |           t        j                  |      | _        t        j
                  ||      | _        y )N)r   )r.   r/   r0   ra   rc   r[   out_proj)r5   rY   r"  r#  r6   s       r7   r/   z"T5GemmaClassificationHead.__init__  s1    zz$;<		+z:r8   rf   rz   c                 J    | j                  |      }| j                  |      }|S r-   )rc   r%  )r5   rf   s     r7   rG   z!T5GemmaClassificationHead.forward  s$    ]3m4r8   )r   )rM   rN   rO   r   rP   rD   r/   r2   r   rG   rQ   rR   s   @r7   r!  r!    s<    7;C ;S ;SX ;
U\\ ell r8   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.rY   
vocab_sizerW   c                 \    t         |           t        j                  |||      | _        y )NrV   )r.   r/   r0   r[   r%  )r5   rY   r)  rW   r6   s       r7   r/   zT5GemmaLMHead.__init__!  s"    		+zEr8   rf   rz   c                 (    | j                  |      }|S r-   )r%  )r5   rf   logitss      r7   rG   zT5GemmaLMHead.forward%  s    }-r8   )F)rM   rN   rO   r   rP   r  r/   r2   r   rG   rQ   rR   s   @r7   r(  r(    s?    8FC FS F FU\\ ell r8   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 )T5GemmaPreTrainedModelrX   modelTr  r  r   r%   r	  )index
layer_namer  )rf   
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?   stdrW   RMSNorm)r.   _init_weightsrX   initializer_ranger   r!  r%  r4   rJ   initnormal_r   rW   zeros_r(  tie_word_embeddingsr6   rM   )r5   r   r4  scaler6   s       r7   r6  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& 4r8   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)	rX   decoderbos_token_idpad_token_idr   	new_zerosrJ   rv   masked_fill_)r5   	input_idsdecoder_start_token_idr@  shifted_input_idss        r7   _shift_rightz#T5GemmaPreTrainedModel._shift_rightR  s     "&!4!4!A!A{{**77!)YZZ &//	@%.sCRCx%8%>%>%@#qr'"$:&!XYY 	&&'8D'@,O  r8   )rM   rN   rO   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_outputsr2   r   r6  rF  rQ   rR   s   @r7   r.  r.  *  s    &*#.0EF#4"5N!"&,/q[Q/q\R0lS
 U]]_' '"!r8   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_idxrz   c                     %t        j                  dt         j                        S | |f   j                  t         j                        S )Nr  r~   )r2   onesr  r   )rR  rS  rT  rU  r   s       r7   
inner_maskz/bidirectional_mask_function.<locals>.inner_maskr  s=    !::b

33i/033EJJ??r8   rP   r  )r   rX  s   ` r7   bidirectional_mask_functionrZ  m  s9    
@c @S @ @c @d @
 r8   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.
    rR  rS  rT  rU  rz   c                 &    |z
  |k  ||z   k  z  S r-   r  )rR  rS  rT  rU  r   s       r7   rX  z>sliding_window_bidirectional_mask_function.<locals>.inner_mask  s"    &/FU^=S4STTr8   rY  )r   rX  s   ` r7   *sliding_window_bidirectional_mask_functionr]  z  s9    
Uc US U Uc Ud U r8   	token_idsr@  c                    | <|t        d      | |k7  j                  |j                  t        j                        }|S t        j
                  |j                  d   |j                  d   f|j                  t        j                        }|S )z%Construct the default attention mask.z3`pad_token_id` is required for padding information.r   r%   r   )r   r   rw   r2   longrW  rJ   )r^  rf   r@  r   s       r7   make_default_2d_attention_maskra    s     RSS#|3778L8LejjY
    #]%8%8%;<]EYEYafakak
 r8   c                        e Zd ZeedZ f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ez  fd
              Z xZS )T5GemmaEncoder)r2  rf   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rX   )r.   r/   r@  padding_idxr)  r0   	EmbeddingrY   embed_tokensr)   r
  normgradient_checkpointing
ModuleListrangenum_hidden_layersr  r   ra   rb   rc   rh   
rotary_emb	post_initr   s      r7   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%NrC  r   r   inputs_embedsr   rz   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%   rw   rX   rs  r   r   r   r   or_mask_function)rx  and_mask_functionfull_attentionr         ?r~   )last_hidden_stater  )r   popri  r2   r   rJ   rw   r   ra  rX   r@  r   dictr   rZ  r   r]  r   tensorrY   r   rc   ro  r   rn  r  rj  r   )r5   rC  r   r   rs  r   r   self_attn_mask_mappingmask_kwargsrf   
normalizerr   layer_modules                r7   rG   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+
 	
r8   r   )rM   rN   rO   r   r  rP  r/   r"   r$   r2   r   r   r  r   r   rI   r   rG   rQ   rR   s   @r7   rc  rc    s    *,
$   .2.20426A
##d*A
 t+A
 &&-	A

 ((4/A
 +,A
 
	 A
   A
r8   rc  c                   t    e Zd Z eed       eed      edZ fdZe	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%   )r0  )r2  cross_attentionsrf   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 re  )r.   r/   r@  rg  r)  r0   rh  rY   ri  r)   r
  rj  rk  rl  rm  rn  r  r   ra   rb   rc   rh   ro  rp  r   s      r7   r/   zT5GemmaDecoder.__init__  rq  rr  NrC  r   r   r   rs  r  r   r   r  r   rz   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 )Nru  z0`encoder_hidden_states` must be given in decoderrf  r   r%   rv  rw  rz  r{  rx  r|  r~   )r}  r   r  )r   ri  r   r
   r	   rX   get_seq_lengthr2   r   rJ   rw   r   ra  r@  r   r  r  r   r   rZ  r  rY   r   rc   ro  r   rn  r  rj  r   )r5   rC  r   r   r   rs  r  r   r   r  r   past_seen_tokensr  r  cross_attn_mask_mappingrf   r  r   r  s                      r7   rG   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++!6"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++
 	
r8   )	NNNNNNNNN)rM   rN   rO   r#   r   r   r  rP  r/   r"   r$   r2   r   r   r
   r  r  r   r   rI   r   rG   rQ   rR   s   @r7   r  r    s7   $%9C*+@J,$   .2.2046:26!%26596:\
##d*\
 t+\
 &&-	\

 -t3\
 ((4/\
 $;\
 ((4/\
  %||d2\
 !&t 3\
 +,\
 
:	:\
   \
r8   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 )T5GemmaModelrX   c                     t         |   |       |j                  st        d      t	        |j
                        | _        t        |j                        | _        | j                          y )NzVT5GemmaModel only support encoder-decoder modeling. Use `T5GemmaEncoderModel` instead.)	r.   r/   is_encoder_decoderr   rc  encoderr  r>  rp  rd   s     r7   r/   zT5GemmaModel.__init__p  sO     ((uvv%fnn5%fnn5r8   c                 6    | j                   j                         S r-   r  get_input_embeddingsrK   s    r7   r  z!T5GemmaModel.get_input_embeddings{      ||0022r8   c                 8    | j                   j                  |      S r-   r  set_input_embeddingsr5   new_embeddingss     r7   r  z!T5GemmaModel.set_input_embeddings~      ||00@@r8   NrC  r   r   decoder_input_idsdecoder_attention_maskdecoder_position_idsencoder_outputsr   rs  decoder_inputs_embedsr  r   r   rz   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)
        rC  r   r   rs  )	rC  r   r   rs  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   rf   r2  r  )r5   rC  r   r   r  r  r  r  r   rs  r  r  r   r   r   decoder_outputss                   r7   rG   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
 	
r8   )NNNNNNNNNNNN)rM   rN   rO   r&   r/   r  r  r   r   r2   r   r  
BoolTensorr   r
   r   r  r   r   r   rG   rQ   rR   s   @r7   r  r  n  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
r8   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 )T5GemmaEncoderModelrX   c                     t         |   |       |j                  rt        d      t	        |j
                        | _        | j                          y )NzQT5GemmaEncoderModel only supports encoder-only model. Use `T5GemmaModel` instead.)r.   r/   r  r   rc  r  rp  rd   s     r7   r/   zT5GemmaEncoderModel.__init__  s?     $$pqq%fnn5r8   c                 6    | j                   j                         S r-   r  rK   s    r7   r  z(T5GemmaEncoderModel.get_input_embeddings  r  r8   c                 8    | j                   j                  |      S r-   r  r  s     r7   r  z(T5GemmaEncoderModel.set_input_embeddings  r  r8   NrC  r   r   rs  r   rz   c                 4     | j                   d||||d|}|S )Nr  r  )r  )r5   rC  r   r   rs  r   r  s          r7   rG   zT5GemmaEncoderModel.forward  s7     '$,, 
)%'	

 
 r8   r   )rM   rN   rO   r&   r/   r  r  r   r   r2   r   r  r   r   r   r   rG   rQ   rR   s   @r7   r  r    s    } 3A  .23704-1##d* ))D0 &&-	
 ||d* +, 
  r8   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_outputrf   r,  rX   c                    d|_         t        | 	  |       t        |      | _        |j
                  j                  | _        t        |j
                  j                  | j                        | _	        d| _
        | j                          y )NTForMaskedLM)r  r.   r/   r  r/  r>  r)  r(  rY   lm_head	loss_typerp  rd   s     r7   r/   z(T5GemmaForConditionalGeneration.__init__  sb    $(! !&)
 ..33$V^^%?%?Q&r8   c                 &    || j                   _        y r-   r  r%  r  s     r7   set_output_embeddingsz5T5GemmaForConditionalGeneration.set_output_embeddings  s     .r8   c                 .    | j                   j                  S r-   r  rK   s    r7   get_output_embeddingsz5T5GemmaForConditionalGeneration.get_output_embeddings  s    ||$$$r8   NrC  r   r   r  r  r  r  r   rs  r  labelsr  r   logits_to_keepr   rz   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)rC  r   r   r  r  r  r  r   rs  r  r  r   )	lossr,  r   r  r  r  r  r   r  r  )rF  r/  r}  r   rP   slicer  get_decoderrX   final_logit_softcappingr2   r   loss_functionr)  r   r   r  r  r  r  r   r  )r5   rC  r   r   r  r  r  r  r   rs  r  r  r  r   r  r   r  rf   slice_indicesr,  decoder_configr  s                         r7   rG   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

 
	
r8   c                 $    | j                  |      S r-   )rF  )r5   r  s     r7   %prepare_decoder_input_ids_from_labelszET5GemmaForConditionalGeneration.prepare_decoder_input_ids_from_labelsF  s      ((r8   )NNNNNNNNNNNNNr   )rM   rN   rO   _tied_weights_keys_tp_plan_pp_planr&   r/   r  r  r   r   r2   r   r  r  r   r
   r  rP   r   r   r   rI   r   rG   r  rQ   rR   s   @r7   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 )r8   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 ) T5GemmaForSequenceClassificationNrX   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  rY   r>  r   r!  scorerp  r5   rX   r  rY   classifier_dropoutr6   s        r7   r/   z)T5GemmaForSequenceClassification.__init__L  s    
 )(:F%  ++$$%f-DJ,V4DJnn00$$ ..44K$V-FL.{DOOM_`
r8   c                 6    | j                   j                         S r-   r/  r  rK   s    r7   r  z5T5GemmaForSequenceClassification.get_input_embeddingsc      zz..00r8   c                 :    | j                   j                  |       y r-   r/  r  r5   r   s     r7   r  z5T5GemmaForSequenceClassification.set_input_embeddingsf      

''.r8   rC  r   r   r  r  r  r  rs  r  r  r   rz   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  rs  r  r  r   r   rs  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.`rv  )r,  r  pooled_logitsrX   r  r,  rf   r2  )rX   r  NotImplementedErrorr6   rM   r   rF  r/  r}  r  r  rf   r2  r  rJ   r@  r   rw   r2   int32r   argmaxclamploggerwarning_oncer  r   )r5   rC  r   r   r  r  r  r  rs  r  r  r   outputsr}  rf   r2  r,  
batch_sizelast_non_pad_tokennon_pad_masktoken_indicesr  r  s                          r7   rG   z(T5GemmaForSequenceClassification.forwardi  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' '!	
 	
r8   r-   
NNNNNNNNNN)rM   rN   rO   r&   r  r/   r  r  r   r   r2   r   r   r   r  r   r   r   rG   rQ   rR   s   @r7   r  r  J  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
r8   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 )T5GemmaForTokenClassificationNrX   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        r7   r/   z&T5GemmaForTokenClassification.__init__  s    
 )(:F%  ++$$%f-DJ,V4DJnn00$$ ..44K$V-FL.{DOOM_`
r8   c                 6    | j                   j                         S r-   r  rK   s    r7   r  z2T5GemmaForTokenClassification.get_input_embeddings  r  r8   c                 :    | j                   j                  |       y r-   r  r  s     r7   r  z2T5GemmaForTokenClassification.set_input_embeddings  r  r8   rC  r   r   r  r  r  r  rs  r  r  r   rz   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  )rX   r  r  r6   rM   r   rF  r/  r}  r  r  rf   r2  r  r  r   )r5   rC  r   r   r  r  r  r  rs  r  r  r   r  r}  rf   r2  r,  r  s                     r7   rG   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$'!	
 	
r8   r-   r  )rM   rN   rO   r&   r  r/   r  r  r   r   r2   r   r   r   r  r   r   r   rG   rQ   rR   s   @r7   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
r8   r  )r  r  r  r.  r  r  )r%   )r   NN)[collections.abcr   typingr   r2   torch.nnr0    r   r8  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"   utils.output_capturingr#   r$   configuration_t5gemmar&   r'   
get_loggerrM   r  Moduler)   rT   rh   r   r   r   rP   r   rD   rI   r   r   r   r  r  r!  r(  r.  rZ  r]  r   ra  rc  r  r  r  r  r  r  __all__r  r8   r7   <module>r     sH  * %    & ! C C ) I R B 9  L F & R R G E E 
		H	%=RYY =( &><RYY ><B( *+ ,2	UU\\ 	U# 	U%,, 	U$   %II%<<% 
% <<	%
 LL4'% % T\% T\% 5<<%&%D )*I)299 I) +I)X )*R)BII R) +R)j14 1hH4 HV		 	BII 	 ?!_ ?! ?!D
t0C 
 
s x $&<< * \\	"[
+ [
|w
+ w
t L
) L
 L
^ !0 ! !Hd)&<o d)N I
'= I
 I
X o
$: o
 o
dr8   