Ë
    S^(h¡~  ã                   ó  — d Z ddlmZmZ ddlmZ ddlZddlm	Z
 ddl	ZddlmZmZmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZ d	d
lmZmZ d	dlmZmZmZ d	dlm Z m!Z!m"Z" ddl#m$Z$  e"jJ                  e&«      Z'dZ(dZ)dZ*dZ+dZ,d„ Z-d„ Z.d„ Z/ G d„ dej`                  «      Z1 G d„ dej`                  «      Z2 G d„ dej`                  «      Z3 G d„ dej`                  «      Z4 G d„ d ej`                  «      Z5 G d!„ d"e«      Z6 G d#„ d$ej`                  «      Z7 G d%„ d&ej`                  «      Z8 e d'e+«       G d(„ d)e6«      «       Z9 ee9e)ee(e*¬*«        G d+„ d,ej`                  «      Z: e d-e+«       G d.„ d/e6«      «       Z; ee;e)ee(e*¬*«       g d0¢Z<y)1zFlax Gemma model.é    )ÚOptionalÚTupleN)Ú
FrozenDictÚfreezeÚunfreeze)Úcombine_masksÚmake_causal_mask)Údot_product_attention_weights)Úflatten_dictÚunflatten_dict)Úlaxé   )ÚFlaxBaseModelOutputÚFlaxCausalLMOutput)ÚACT2FNÚFlaxPreTrainedModelÚappend_call_sample_docstring)Úadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚloggingé   )ÚGemmaConfigr   zgoogle/gemma-2bz openlm-research/open_llama_3b_v2añ  

    This model inherits from [`FlaxPreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

    This model is also a Flax Linen
    [flax.nn.Module](https://flax.readthedocs.io/en/latest/_autosummary/flax.nn.module.html) subclass. Use it as a
    regular Flax Module and refer to the Flax documentation for all matter related to general usage and behavior.

    Finally, this model supports inherent JAX features such as:

    - [Just-In-Time (JIT) compilation](https://jax.readthedocs.io/en/latest/jax.html#just-in-time-compilation-jit)
    - [Automatic Differentiation](https://jax.readthedocs.io/en/latest/jax.html#automatic-differentiation)
    - [Vectorization](https://jax.readthedocs.io/en/latest/jax.html#vectorization-vmap)
    - [Parallelization](https://jax.readthedocs.io/en/latest/jax.html#parallelization-pmap)

    Parameters:
        config ([`GemmaConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~FlaxPreTrainedModel.from_pretrained`] method to load the model weights.
        dtype (`jax.numpy.dtype`, *optional*, defaults to `jax.numpy.float32`):
            The data type of the computation. Can be one of `jax.numpy.float32`, `jax.numpy.float16`, or
            `jax.numpy.bfloat16`.

            This can be used to enable mixed-precision training or half-precision inference on GPUs or TPUs. If
            specified all the computation will be performed with the given `dtype`.

            **Note that this only specifies the dtype of the computation and does not influence the dtype of model
            parameters.**

            If you wish to change the dtype of the model parameters, see [`~FlaxPreTrainedModel.to_fp16`] and
            [`~FlaxPreTrainedModel.to_bf16`].
a’
  
    Args:
        input_ids (`numpy.ndarray` of shape `(batch_size, input_ids_length)`):
            Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
            it.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        attention_mask (`numpy.ndarray` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
            `past_key_values`).

            If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
            and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
            information on the default strategy.

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.
        position_ids (`numpy.ndarray` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.n_positions - 1]`.

            [What are position IDs?](../glossary#position-ids)
        past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`):
            Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast
            auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
c                 óÚ  — ddt        j                  d|d«      d |dz   |z  z  z  }t        j                  dt        j                  | «      |«      j                  d«      }t        j                  ||fd¬«      }t        j                  t        j
                  |«      d d …d d d …f   t        j                  |«      d d …d d d …f   fd¬«      }t        j                  |d d …d d …d | …f   «      S )	Ng      ð?i'  r   é   zi , j -> i jÚfloat32éÿÿÿÿ©Úaxis)	ÚnpÚarangeÚeinsumÚastypeÚconcatenateÚsinÚcosÚjnpÚarray)Únum_posÚdimÚinv_freqÚfreqsÚembÚouts         úk/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/gemma/modeling_flax_gemma.pyÚcreate_sinusoidal_positionsr/   }   sÂ   € Ø�e¤§	¡	¨!¨S°!Ó 4°\¸¸q¹Ð BÀSÑ HÑIÑJ€HÜ�I‰I�n¤b§i¡i°Ó&8¸(ÓC×JÑJÈ9ÓU€Eä
�.‰.˜% ˜¨bÔ
1€CÜ
�.‰.œ"Ÿ&™& ›+¢a¨ªq jÑ1´2·6±6¸#³;ºqÀ$Ê¸zÑ3JÐKÐRTÔ
U€CÜ�9‰9�SššA˜x ˜x˜Ñ(Ó)Ð)ó    c                 ó–   — t        j                  | d| j                  d   dz  d…f    | dd| j                  d   dz  …f   fd¬«      }|S )z*Rotates half the hidden dims of the input..r   r   Nr   )r&   r#   Úshape)ÚtensorÚrotate_half_tensors     r.   Úrotate_halfr5   ‡   sa   € äŸ™Ø
��f—l‘l 2Ñ&¨!Ñ+Ñ-Ð-Ñ
.Ð	.°°sÐ<S¸f¿l¹lÈ2Ñ>NÐRSÑ>SÐ<SÐ7SÑ0TÐUÐ\^ôÐð Ðr0   c                 ó*   — | |z  t        | «      |z  z   S ©N)r5   )r3   Úsin_posÚcos_poss      r.   Úapply_rotary_pos_embr:   �   s   € Ø�WÑ¤¨VÓ!4°wÑ!>Ñ?Ð?r0   c                   óZ   — e Zd ZU eed<   ej                  Zej                  ed<   d„ Zd„ Z	y)ÚFlaxGemmaRMSNormÚconfigÚdtypec                 ó”   — | j                   j                  | _        | j                  dd„ | j                   j                  «      | _        y )NÚweightc                 ó,   — t        j                  |«      S r7   )r&   Úones)Ú_r2   s     r.   ú<lambda>z(FlaxGemmaRMSNorm.setup.<locals>.<lambda>š   s   € ¼C¿H¹HÀU»O€ r0   )r=   Úrms_norm_epsÚepsilonÚparamÚhidden_sizer@   ©Úselfs    r.   ÚsetupzFlaxGemmaRMSNorm.setup˜   s2   € Ø—{‘{×/Ñ/ˆŒØ—j‘j Ñ+KÈTÏ[É[×MdÑMdÓeˆ�r0   c                 óJ  — t        j                  |t         j                  ¬«      }t        j                  |d«      }|j	                  dd¬«      }|t        j
                  || j                  z   «      z  }d| j                  z   t        j                  || j                  ¬«      z  S )N©r>   r   r   T)Úkeepdimsr   )	r&   Úasarrayr   ÚpowerÚmeanÚsqrtrF   r@   r>   )rJ   Úhidden_statesÚvariances      r.   Ú__call__zFlaxGemmaRMSNorm.__call__œ   sv   € Ü—;‘;˜}´C·K±KÔ@ˆÜ—9‘9˜X qÓ)ˆØ—=‘= ¨d�=Ó3ˆà%¬¯©°¸D¿L¹LÑ1HÓ(IÑIˆà�D—K‘K‘¤3§;¡;¨}ÀDÇJÁJÔ#OÑOÐOr0   N©
Ú__name__Ú
__module__Ú__qualname__r   Ú__annotations__r&   r   r>   rK   rU   © r0   r.   r<   r<   ”   s&   … ØÓØ—{‘{€Eˆ3�9‰9Ó"òfóPr0   r<   c                   óZ   — e Zd ZU eed<   ej                  Zej                  ed<   d„ Zd„ Z	y)ÚFlaxGemmaRotaryEmbeddingr=   r>   c                 óz   — | j                   j                  }t        | j                   j                  |«      | _        y r7   )r=   Úhead_dimr/   Úmax_position_embeddingsÚsincos)rJ   r_   s     r.   rK   zFlaxGemmaRotaryEmbedding.setup¬   s*   € Ø—;‘;×'Ñ'ˆÜ1°$·+±+×2UÑ2UÐW_Ó`ˆ�r0   c                 ó  — | j                   |   }t        j                  |dd¬«      \  }}t        |||«      }t        |||«      }t        j                  || j
                  ¬«      }t        j                  || j
                  ¬«      }||fS )Nr   r   r   rM   )ra   r&   Úsplitr:   rO   r>   )rJ   ÚkeyÚqueryÚposition_idsra   r8   r9   s          r.   rU   z!FlaxGemmaRotaryEmbedding.__call__°   su   € Ø—‘˜\Ñ*ˆÜŸ9™9 V¨Q°RÔ8Ñˆ�ä" 3¨°Ó9ˆÜ$ U¨G°WÓ=ˆä�k‰k˜# T§Z¡ZÔ0ˆÜ—‘˜E¨¯©Ô4ˆà�EˆzÐr0   NrV   r[   r0   r.   r]   r]   §   s%   … ØÓØ—{‘{€Eˆ3�9‰9Ó"òaó
r0   r]   c                   ó¼   — e Zd ZU eed<   ej                  Zej                  ed<   dZe	ed<   dZ
e	ed<   d„ Zd„ Zd	„ Zej                  d
„ «       Z	 	 	 dde	de	de	fd„Zy)ÚFlaxGemmaAttentionr=   r>   TÚcausalFÚis_cross_attentionc                 óh  — | j                   }|j                  | _        |j                  | _        |j
                  | _        | j                  t        j                  u| _	        |j                  | _
        | j                  | j                  z  | _        t        j                  j                  j                  | j                   j                   «      }t        j"                  | j                  | j
                  z  |j$                  | j                  |¬«      | _        t        j"                  | j                  | j
                  z  |j$                  | j                  |¬«      | _        t        j"                  | j                  | j
                  z  |j$                  | j                  |¬«      | _        t        j"                  | j                  |j$                  | j                  |¬«      | _        t/        t        j0                  d|j2                  fd¬«      d¬«      | _        t7        || j                  ¬«      | _        y )N©Úuse_biasr>   Úkernel_initr   ÚboolrM   )r=   rH   Ú	embed_dimÚnum_attention_headsÚ	num_headsr_   r>   r&   r   Úattention_softmax_in_fp32Únum_key_value_headsÚnum_key_value_groupsÚjaxÚnnÚinitializersÚnormalÚinitializer_rangeÚDenseÚattention_biasÚq_projÚk_projÚv_projÚo_projr	   rB   r`   Úcausal_maskr]   Ú
rotary_emb)rJ   r=   Úkernels      r.   rK   zFlaxGemmaAttention.setupÃ   s“  € Ø—‘ˆØ×+Ñ+ˆŒØ×3Ñ3ˆŒØŸ™ˆŒØ)-¯©¼3¿;¹;Ð)FˆÔ&à#)×#=Ñ#=ˆÔ Ø$(§N¡N°d×6NÑ6NÑ$NˆÔ!ä—‘×$Ñ$×+Ñ+¨D¯K©K×,IÑ,IÓJˆÜ—h‘hØ�N‰N˜TŸ]™]Ñ*°V×5JÑ5JÐRV×R\ÑR\Ðjpô
ˆŒô —h‘hØ×$Ñ$ t§}¡}Ñ4Ø×*Ñ*Ø—*‘*Øô	
ˆŒô —h‘hØ×$Ñ$ t§}¡}Ñ4Ø×*Ñ*Ø—*‘*Øô	
ˆŒô —h‘h˜tŸ~™~¸×8MÑ8MÐUY×U_ÑU_ÐmsÔtˆŒä+¬C¯H©H°a¸×9WÑ9WÐ5XÐ`fÔ,gÐouÔvˆÔÜ2°6ÀÇÁÔLˆ�r0   c                 ó\   — |j                  |j                  d d || j                  fz   «      S ©Nr   )Úreshaper2   r_   )rJ   rS   rr   s      r.   Ú_split_headszFlaxGemmaAttention._split_headsâ   s.   € Ø×$Ñ$ ]×%8Ñ%8¸¸!Ð%<À	È4Ï=É=Ð?YÑ%YÓZÐZr0   c                 ót   — |j                  |j                  d d | j                  | j                  z  fz   «      S r…   )r†   r2   rr   r_   )rJ   rS   s     r.   Ú_merge_headszFlaxGemmaAttention._merge_headså   s8   € Ø×$Ñ$ ]×%8Ñ%8¸¸!Ð%<ÀÇÁÐQU×Q^ÑQ^Ñ@^Ð?`Ñ%`ÓaÐar0   c                 ó(  — | j                  dd«      }| j                  ddt        j                  |j                  |j
                  «      }| j                  ddt        j                  |j                  |j
                  «      }| j                  ddd„ «      }|rø|j                  j                  �^ }	}
}}|j                  }dt        |	«      z  |ddfz   }t        j                  |j                  ||«      }t        j                  |j                  ||«      }||_        ||_        |j                  d   }|j                  |z   |_        t        j                  t        j                  |
«      ||z   k  t        |	«      d||
fz   «      }t        ||«      }|||fS )	a\  
        This function takes projected key, value states from a single input token and concatenates the states to cached
        states from previous steps. This function is slightly adapted from the official Flax repository:
        https://github.com/google/flax/blob/491ce18759622506588784b4fca0e4bf05f8c8cd/flax/linen/attention.py#L252
        ÚcacheÚ
cached_keyÚcached_valueÚcache_indexc                  óL   — t        j                  dt         j                  ¬«      S )Nr   rM   )r&   r'   Úint32r[   r0   r.   rD   z:FlaxGemmaAttention._concatenate_to_cache.<locals>.<lambda>ô   s   € ÄCÇIÁIÈaÔWZ×W`ÑW`ÔDa€ r0   )r   r   r   )Úhas_variableÚvariabler&   Úzerosr2   r>   ÚvalueÚlenr   Údynamic_update_sliceÚbroadcast_tor    Útupler   )rJ   rd   r”   re   Úattention_maskÚis_initializedrŒ   r�   rŽ   Ú
batch_dimsÚ
max_lengthrr   Údepth_per_headÚ	cur_indexÚindicesÚnum_updated_cache_vectorsÚpad_masks                    r.   Ú_concatenate_to_cachez(FlaxGemmaAttention._concatenate_to_cacheè   sr  € ð ×*Ñ*¨7°LÓAˆØ—]‘] 7¨L¼#¿)¹)ÀSÇYÁYÐPS×PYÑPYÓZˆ
Ø—}‘} W¨n¼c¿i¹iÈÏÉÐV[×VaÑVaÓbˆØ—m‘m G¨]Ñ<aÓbˆáØAK×AQÑAQ×AWÑAWÑ>ˆZ˜ Y°à#×)Ñ)ˆIØœS ›_Ñ,°	¸1¸aÐ/@Ñ@ˆGÜ×*Ñ*¨:×+;Ñ+;¸SÀ'ÓJˆCÜ×,Ñ,¨\×-?Ñ-?ÀÈÓPˆEØ"ˆJÔØ!&ˆLÔØ(-¯©°A©Ð%Ø +× 1Ñ 1Ð4MÑ MˆKÔä×'Ñ'Ü—
‘
˜:Ó&¨Ð5NÑ)NÑNÜ�jÓ! QÐ(AÀ:Ð$NÑNóˆHô +¨8°^ÓDˆNØ�E˜>Ð)Ð)r0   ÚdeterministicÚ
init_cacheÚoutput_attentionsc           
      ó†  — | j                  |«      }| j                  |«      }| j                  |«      }	| j                  || j                  «      }| j                  || j
                  «      }| j                  |	| j
                  «      }	| j                  |||«      \  }}|j                  d   |j                  d   }}
| j                  dd«      r[| j                  d   d   }| j                  d   d   j                  d   }t        j                  | j                  dd|dfdd|
|f«      }n| j                  d d …d d …d |
…d |…f   }|j                  d   }t        j                  ||f|j                  dd  z   «      }t        j                  t        j                  |d¬«      |j                  «      }t!        ||«      }d }|s*| j"                  j$                  dkD  r| j'                  d	«      }| j                  dd«      s|r| j)                  ||	||«      \  }}	}t        j*                  |dkD  t        j,                  |j                  d«      j/                  | j0                  «      t        j,                  |j                  t        j2                  | j0                  «      j4                  «      j/                  | j0                  «      «      }t        j6                  || j8                  d
¬«      }t        j6                  |	| j8                  d
¬«      }	| j:                  rt        j<                  n| j0                  }t?        ||||| j"                  j$                  ||¬«      }| j:                  r|j/                  | j0                  «      }t        j@                  d||	«      }| jC                  |«      }| jE                  |«      }|r||f}|S |f}|S )Nr   r‹   rŒ   rŽ   r   )éýÿÿÿéþÿÿÿr   g        Údropoutr   )Úrepeatsr   )ÚbiasÚdropout_rngÚdropout_rater£   r>   z...hqk,...khd->...qhd)#r}   r~   r   r‡   rr   rt   r‚   r2   r‘   Ú	variablesr   Údynamic_slicer�   r&   r—   Úexpand_dimsr   r=   Úattention_dropoutÚmake_rngr¢   ÚselectÚfullr"   r>   ÚfinfoÚminÚrepeatru   rs   r   r
   r!   r‰   r€   )rJ   rS   r™   rf   r£   r¤   r¥   re   rd   r”   Úquery_lengthÚ
key_lengthÚ
mask_shiftÚmax_decoder_lengthr�   Ú
batch_sizer¬   r|   Úattention_dtypeÚattn_weightsÚattn_outputÚoutputss                         r.   rU   zFlaxGemmaAttention.__call__	  sP  € ð —‘˜MÓ*ˆØ�k‰k˜-Ó(ˆØ—‘˜MÓ*ˆà×!Ñ! %¨¯©Ó8ˆØ×Ñ  T×%=Ñ%=Ó>ˆØ×!Ñ! %¨×)AÑ)AÓBˆà—_‘_ S¨%°Ó>‰
ˆˆUà#(§;¡;¨q¡>°3·9±9¸Q±<�jˆà×Ñ˜W lÔ3ØŸ™¨Ñ0°Ñ?ˆJØ!%§¡°Ñ!8¸Ñ!F×!LÑ!LÈQÑ!OÐÜ×+Ñ+Ø× Ñ  1 a¨°QÐ"7¸!¸QÀÐN`Ð9aó‰Kð ×*Ñ*ª1ªa°°,°ÀÀÀÐ+KÑLˆKà"×(Ñ(¨Ñ+ˆ
Ü×&Ñ& {°Z°MÀK×DUÑDUÐVWÐVXÐDYÑ4YÓZˆä×)Ñ)¬#¯/©/¸.ÈxÔ*XÐZe×ZkÑZkÓlˆÜ& ~°{ÓCˆàˆÙ §¡×!>Ñ!>ÀÒ!DØŸ-™-¨	Ó2ˆKð ×Ñ˜W lÔ3±zØ)-×)CÑ)CÀCÈÐPUÐWeÓ)fÑ&ˆC�˜ô Ÿ™Ø˜QÑÜ�H‰H�^×)Ñ)¨3Ó/×6Ñ6°t·z±zÓBÜ�H‰H�^×)Ñ)¬3¯9©9°T·Z±ZÓ+@×+DÑ+DÓE×LÑLÈTÏZÉZÓXó
ˆô �j‰j˜ d×&?Ñ&?ÀaÔHˆÜ—
‘
˜5¨$×*CÑ*CÈ!ÔLˆð *.×)GÒ)Gœ#Ÿ+š+ÈTÏZÉZˆÜ4ØØØØ#ØŸ™×6Ñ6Ø'Ø!ô
ˆð ×)Ò)Ø'×.Ñ.¨t¯z©zÓ:ˆLä—j‘jÐ!8¸,ÈÓNˆØ×'Ñ'¨Ó4ˆØ—k‘k +Ó.ˆá1B�; Ð-ˆØˆð JUÈˆØˆr0   N)TFF)rW   rX   rY   r   rZ   r&   r   r>   ri   ro   rj   rK   r‡   r‰   rw   Úcompactr¢   rU   r[   r0   r.   rh   rh   ½   sŒ   … ØÓØ—{‘{€Eˆ3�9‰9Ó"Ø€FˆDÓØ$Ð˜Ó$òMò>[òbð ‡Z�Zñ*ó ð*ðH #Ø Ø"'ñKð
 ðKð ðKð  ôKr0   rh   c                   óZ   — e Zd ZU eed<   ej                  Zej                  ed<   d„ Zd„ Z	y)ÚFlaxGemmaMLPr=   r>   c                 óþ  — | j                   j                  }| j                   j                  �| j                   j                  nd|z  }t        j                  j
                  j                  | j                   j                  «      }| j                   j                  €Gt        j                  d| j                   j                  › d| j                   j                  › d�«       d}n| j                   j                  }t        |   | _        t	        j                  |d| j                  |¬«      | _        t	        j                  |d| j                  |¬«      | _        t	        j                  |d| j                  |¬«      | _        y )Né   z¢Gemma's activation function should be approximate GeLU and not exact GeLU. Changing the activation function to `gelu_pytorch_tanh`.if you want to use the legacy `z5`, edit the `model.config` to set `hidden_activation=zi`   instead of `hidden_act`. See https://github.com/huggingface/transformers/pull/29402 for more details.Úgelu_pytorch_tanhFrl   )r=   rH   Úintermediate_sizerv   rw   rx   ry   rz   Úhidden_activationÚloggerÚwarning_onceÚ
hidden_actr   Úactr{   r>   Ú	gate_projÚ	down_projÚup_proj)rJ   rp   Ú	inner_dimrn   rÈ   s        r.   rK   zFlaxGemmaMLP.setup[  s)  € Ø—K‘K×+Ñ+ˆ	Ø59·[±[×5RÑ5RÐ5^�D—K‘K×1Ò1ÐdeÐhqÑdqˆ	ä—f‘f×)Ñ)×0Ñ0°·±×1NÑ1NÓOˆØ�;‰;×(Ñ(Ð0Ü×Ñð2à26·+±+×2HÑ2HÐ1Ið JEØEIÇ[Á[×E[ÑE[ÐD\ð ]zðzôð !4Ñà $§¡× =Ñ =ÐÜÐ+Ñ,ˆŒäŸ™ )°eÀ4Ç:Á:Ð[fÔgˆŒÜŸ™ )°eÀ4Ç:Á:Ð[fÔgˆŒÜ—x‘x 	°EÀÇÁÐYdÔeˆ�r0   c                 ó�   — | j                  |«      }| j                  | j                  |«      «      }| j                  ||z  «      }|S r7   )rÏ   rÌ   rÍ   rÎ   )rJ   rS   Úup_proj_statesÚgate_statess       r.   rU   zFlaxGemmaMLP.__call__q  s@   € ØŸ™ mÓ4ˆØ—h‘h˜tŸ~™~¨mÓ<Ó=ˆàŸ™ ~¸Ñ'CÓDˆØÐr0   NrV   r[   r0   r.   rÃ   rÃ   W  s%   … ØÓØ—{‘{€Eˆ3�9‰9Ó"òfó,r0   rÃ   c                   ót   — e Zd ZU eed<   ej                  Zej                  ed<   d„ Z	 	 	 	 	 d	de	de	de	fd„Z
y)
ÚFlaxGemmaDecoderLayerr=   r>   c                 ó4  — t        | j                  | j                  ¬«      | _        t	        | j                  | j                  ¬«      | _        t        | j                  | j                  ¬«      | _        t        | j                  | j                  ¬«      | _        y )NrM   )	r<   r=   r>   Úinput_layernormrh   Ú	self_attnÚpost_attention_layernormrÃ   ÚmlprI   s    r.   rK   zFlaxGemmaDecoderLayer.setup~  s\   € Ü/°·±À4Ç:Á:ÔNˆÔÜ+¨D¯K©K¸t¿z¹zÔJˆŒÜ(8¸¿¹ÈDÏJÉJÔ(WˆÔ%Ü §¡°4·:±:Ô>ˆ�r0   Nr£   r¤   r¥   c                 óÎ   — |}| j                  |«      }| j                  ||||||¬«      }|d   }	||	z   }|}| j                  |«      }| j                  |«      }||z   }|f|dd  z   S )N©r™   rf   r£   r¤   r¥   r   r   )r×   rØ   rÙ   rÚ   )
rJ   rS   r™   rf   r£   r¤   r¥   ÚresidualrÀ   r¿   s
             r.   rU   zFlaxGemmaDecoderLayer.__call__„  s”   € ð !ˆØ×,Ñ,¨]Ó;ˆØ—.‘.ØØ)Ø%Ø'Ø!Ø/ð !ó 
ˆð ˜a‘jˆØ  ;Ñ.ˆà ˆØ×5Ñ5°mÓDˆØŸ™ Ó/ˆà  =Ñ0ˆàÐ '¨!¨" +Ñ-Ð-r0   )NNTFF©rW   rX   rY   r   rZ   r&   r   r>   rK   ro   rU   r[   r0   r.   rÕ   rÕ   z  sS   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò?ð ØØ"Ø Ø"'ñ.ð
 ð.ð ð.ð  ô.r0   rÕ   c                   óf  ‡ — e Zd ZU dZeZdZdZej                  e
d<   ddej                  dfded	ed
edej                  def
ˆ fd„Zddej&                  j(                  d	ededefd„Zd„ Z ee«      	 	 	 	 	 	 	 	 	 ddededej&                  j(                  dedee   dee   dee   fd„«       Zˆ xZS )ÚFlaxGemmaPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    ÚmodelNÚmodule_class)r   r   r   Tr=   Úinput_shapeÚseedr>   Ú_do_initc                 óZ   •—  | j                   d||dœ|¤Ž}t        ‰| �	  ||||||¬«       y )N)r=   r>   )rã   rä   r>   rå   r[   )râ   ÚsuperÚ__init__)	rJ   r=   rã   rä   r>   rå   ÚkwargsÚmoduleÚ	__class__s	           €r.   rè   z!FlaxGemmaPreTrainedModel.__init__¯  s=   ø€ ð #�×"Ñ"ÐH¨&¸ÑHÀÑHˆÜ‰Ñ˜ °[ÀtÐSXÐckÐÕlr0   ÚrngÚparamsÚreturnc                 óP  — t        j                  |d¬«      }t        j                  |«      }t        j                  t        j                  t        j
                  |«      j                  d   «      |«      }t        j                  j                  |«      \  }}||dœ}	| j                  j                  |	|||d¬«      d   }
|�dt        t        |
«      «      }
t        t        |«      «      }| j                  D ]
  }|
|   ||<   Œ t        «       | _        t!        t#        |«      «      S |
S )NÚi4rM   r   )rí   r©   F)Úreturn_dictrí   )r&   r“   Ú	ones_liker—   r    Ú
atleast_2dr2   rv   Úrandomrc   rê   Úinitr   r   Ú_missing_keysÚsetr   r   )rJ   rì   rã   rí   Ú	input_idsr™   rf   Ú
params_rngr¬   ÚrngsÚrandom_paramsÚmissing_keys               r.   Úinit_weightsz%FlaxGemmaPreTrainedModel.init_weights»  s  € ä—I‘I˜k°Ô6ˆ	ÜŸ™ yÓ1ˆÜ×'Ñ'¬¯
©
´3·>±>À)Ó3L×3RÑ3RÐSUÑ3VÓ(WÐYdÓeˆÜ"%§*¡*×"2Ñ"2°3Ó"7Ñˆ
�KØ$°Ñ=ˆàŸ™×(Ñ(¨¨y¸.È,ÐdiÐ(ÓjÐksÑtˆàÐÜ(¬°-Ó)@ÓAˆMÜ!¤(¨6Ó"2Ó3ˆFØ#×1Ñ1ò A�Ø&3°KÑ&@��{Ò#ðAä!$£ˆDÔÜœ.¨Ó0Ó1Ð1à Ð r0   c                 ó˜  — t        j                  ||f«      }t        j                  |«      }t        j                  t        j                  t        j
                  |«      j                  d   «      |j                  «      }| j                  j                  t        j                  j                  d«      |||dd¬«      }t        |d   «      S )aW  
        Args:
            batch_size (`int`):
                batch_size used for fast auto-regressive decoding. Defines the batch size of the initialized cache.
            max_length (`int`):
                maximum possible length for auto-regressive decoding. Defines the sequence length of the initialized
                cache.
        r   r   FT)rñ   r¤   r‹   )r&   rB   rò   r—   r    ró   r2   rê   rõ   rv   rô   ÚPRNGKeyr   )rJ   r¼   rœ   rø   r™   rf   Úinit_variabless          r.   r¤   z#FlaxGemmaPreTrainedModel.init_cacheÏ  s¥   € ô —H‘H˜j¨*Ð5Ó6ˆ	ÜŸ™ yÓ1ˆÜ×'Ñ'¬¯
©
´3·>±>À)Ó3L×3RÑ3RÐSUÑ3VÓ(WÐYb×YhÑYhÓiˆàŸ™×)Ñ)Ü�J‰J×Ñ˜qÓ! 9¨n¸lÐX]Ðjnð *ó 
ˆô ˜ wÑ/Ó0Ð0r0   Úpast_key_valuesr¬   Útrainr¥   Úoutput_hidden_statesrñ   c                 ó  — |�|n| j                   j                  }|	�|	n| j                   j                  }	|
�|
n| j                   j                  }
|j                  \  }}|€?|�t        d«      ‚t        j                  t        j                  |«      d d d …f   ||f«      }|€t        j                  ||f«      }i }|�||d<   d|xs | j                  i}|r	||d<   dg}nd}| j                  j                  |t        j                  |d¬«      t        j                  |d¬«      t        j                  |d¬«      | d||	|
||¬«      }|�|
r|\  }}t        |d   «      |d	<   |S |�"|
s |\  }}|d d
 t        |d   «      fz   |d
d  z   }|S )NzCMake sure to provide `position_ids` when passing `past_key_values`.r©   rí   r‹   Frð   rM   )rú   Úmutabler  r   )r=   r¥   r  rñ   r2   Ú
ValueErrorr&   r—   r    rB   rí   rê   Úapplyr'   r   )rJ   rø   r™   rf   rí   r  r¬   r  r¥   r  rñ   r¼   Úsequence_lengthrú   Úinputsr  rÀ   s                    r.   rU   z!FlaxGemmaPreTrainedModel.__call__â  sË  € ð 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×BYÑBYˆà&/§o¡oÑ#ˆ
�OàÐØÐ*Ü Ð!fÓgÐgä×+Ñ+¬C¯J©J°Ó,GÈÊaÈÑ,PÐS]Ð_nÐRoÓpˆLàÐ!Ü ŸX™X z°?Ð&CÓDˆNð ˆØÐ"Ø)ˆD�‰Oà˜FÒ1 d§k¡kÐ2ˆñ Ø-ˆF�7‰OØ�i‰GàˆGà—+‘+×#Ñ#ØÜ�I‰I�i tÔ,Ü�I‰I�n¨DÔ1Ü�I‰I�l¨$Ô/ØˆIØØØ ØØØð $ó 
ˆð Ð&©;Ø'.Ñ$ˆG�_Ü)1°/À'Ñ2JÓ)KˆGÐ%Ñ&ØˆNØÐ(±Ø'.Ñ$ˆG�_Ø˜b˜q�k¤X¨o¸gÑ.FÓ%GÐ$IÑIÈGÐTUÐTVÈKÑWˆGàˆr0   r7   )	NNNNNFNNN)rW   rX   rY   Ú__doc__r   Úconfig_classÚbase_model_prefixrâ   rw   ÚModulerZ   r&   r   r   Úintr>   ro   rè   rv   rô   rÿ   r   rý   r¤   r   ÚGEMMA_INPUTS_DOCSTRINGÚdictr   rU   Ú__classcell__)rë   s   @r.   rà   rà   ¥  sC  ø… ñð
 €LØÐØ"€L�"—)‘)Ó"ð
 $ØØŸ;™;Øñ
màð
mð ð
mð ð	
mð
 �y‰yð
mð õ
mñ! §
¡
× 2Ñ 2ð !Àð !ÐPZð !Ðfpó !ò(1ñ& +Ð+AÓBð ØØØ $Ø*.ØØ,0Ø/3Ø&*ñCð
 ðCð ðCð —Z‘Z×'Ñ'ðCð ðCð $ D™>ðCð ' t™nðCð ˜d‘^òCó CôCr0   rà   c                   ó€   — e Zd ZU eed<   ej                  Zej                  ed<   d„ Z	 	 	 	 	 	 	 dde	de	de	de	d	e	f
d
„Z
y)ÚFlaxGemmaLayerCollectionr=   r>   c           
      óÄ   — t        | j                  j                  «      D �cg c]-  }t        | j                  | j                  t        |«      ¬«      ‘Œ/ c}| _        y c c}w )N)r>   Úname)Úranger=   Únum_hidden_layersrÕ   r>   ÚstrÚblocks)rJ   Úis     r.   rK   zFlaxGemmaLayerCollection.setup.  sG   € ô ˜4Ÿ;™;×8Ñ8Ó9ö
àô " $§+¡+°T·Z±ZÄcÈ!ÃfÖMò
ˆ�ùò 
s   ¢2ANr£   r¤   r¥   r  rñ   c	           	      ó˜   — |rdnd }	|rdnd }
| j                   D ])  }|r|
|fz  }
 |||||||¬«      }|d   }|sŒ!|	|d   fz  }	Œ+ ||
|	f}|S )Nr[   rÜ   r   r   )r  )rJ   rS   r™   rf   r£   r¤   r¥   r  rñ   Úall_attentionsÚall_hidden_statesÚblockÚlayer_outputsrÀ   s                 r.   rU   z!FlaxGemmaLayerCollection.__call__4  sŒ   € ñ  1™°dˆÙ"6™B¸DÐà—[‘[ò 	6ˆEÙ#Ø! mÐ%5Ñ5Ð!Ù!ØØ-Ø)Ø+Ø%Ø"3ôˆMð *¨!Ñ,ˆMâ Ø =°Ñ#3Ð"5Ñ5‘ð	6ð" !Ð"3°^ÐDˆàˆr0   )NNTFFFFrÞ   r[   r0   r.   r  r  *  sm   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò
ð ØØ"Ø Ø"'Ø%*Ø!ñ!ð
 ð!ð ð!ð  ð!ð #ð!ð ô!r0   r  c            	       ó|   — e Zd ZU eed<   ej                  Zej                  ed<   d„ Z	 	 	 	 	 	 	 d
de	de	de	de	fd	„Z
y)ÚFlaxGemmaModuler=   r>   c                 óÚ  — | j                   j                  | _        t        j                  j                  j                  | j                   j                  ¬«      }t        j                  | j                   j                  | j                  || j                  ¬«      | _
        t        | j                   | j                  ¬«      | _        t        | j                   | j                  ¬«      | _        y )N©Ústddev)Úembedding_initr>   rM   )r=   rH   rv   rw   rx   ry   rz   ÚEmbedÚ
vocab_sizer>   Úembed_tokensr  Úlayersr<   Únorm)rJ   r%  s     r.   rK   zFlaxGemmaModule.setup]  sš   € ØŸ;™;×2Ñ2ˆÔÜŸ™×,Ñ,×3Ñ3¸4¿;¹;×;XÑ;XÐ3ÓYˆÜŸH™HØ�K‰K×"Ñ"Ø×ÑØ)Ø—*‘*ô	
ˆÔô /¨t¯{©{À$Ç*Á*ÔMˆŒÜ$ T§[¡[¸¿
¹
ÔCˆ�	r0   Nr¤   r¥   r  rñ   c	           
      óf  — | j                  |j                  d«      «      }	|	| j                  j                  dz  z  }	| j	                  |	|||||||¬«      }
|
d   }| j                  |«      }|r|
d   |fz   }||f|
dd  z   }
n	|f|
dd  z   }
|st        d„ |
D «       «      S t        ||
d   |
d   ¬	«      S )
Nrð   g      à?©rf   r™   r£   r¤   r¥   r  rñ   r   r   r   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wr7   r[   )Ú.0Úvs     r.   ú	<genexpr>z+FlaxGemmaModule.__call__.<locals>.<genexpr>Ž  s   è ø€ Ò=˜q¨q©}œÑ=ùs   ‚Šr   )Úlast_hidden_staterS   Ú
attentions)r(  r"   r=   rH   r)  r*  r˜   r   )rJ   rø   r™   rf   r£   r¤   r¥   r  rñ   Úinput_embedsrÀ   rS   r  s                r.   rU   zFlaxGemmaModule.__call__j  sì   € ð ×(Ñ(¨×)9Ñ)9¸$Ó)?Ó@ˆà# t§{¡{×'>Ñ'>ÀÑ'CÑDˆà—+‘+ØØ%Ø)Ø'Ø!Ø/Ø!5Ø#ð ó 	
ˆð   ™
ˆØŸ	™	 -Ó0ˆáØ '¨¡
¨mÐ-=Ñ =ÐØ$Ð&7Ð8¸7À1À2¸;ÑF‰Gà$Ð&¨°°¨Ñ4ˆGáÜÑ= GÔ=Ó=Ð=ä"Ø+Ø! !™*Ø˜r‘{ô
ð 	
r0   ©NNTFFFTrÞ   r[   r0   r.   r!  r!  Y  sd   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò
Dð  ØØØ Ø"'Ø%*Ø ñ*
ð ð*
ð  ð*
ð #ð*
ð ô*
r0   r!  z_The bare Gemma Model transformer outputting raw hidden-states without any specific head on top.c                   ó   — e Zd ZeZy)ÚFlaxGemmaModelN)rW   rX   rY   r!  râ   r[   r0   r.   r6  r6  —  s	   „ ð #�Lr0   r6  )Úreal_checkpointc                   ó€   — e Zd ZU eed<   ej                  Zej                  ed<   d„ Z	 	 	 	 	 	 	 dde	de	de	de	d	e	f
d
„Z
y)ÚFlaxGemmaForCausalLMModuler=   r>   c                 ó@  — t        | j                  | j                  ¬«      | _        t	        j
                  | j                  j                  d| j                  t        j                  j                  j                  | j                  j                  ¬«      ¬«      | _        y )NrM   Fr#  rl   )r!  r=   r>   rá   rw   r{   r'  rv   rx   ry   rz   Úlm_headrI   s    r.   rK   z FlaxGemmaForCausalLMModule.setup®  sd   € Ü$ T§[¡[¸¿
¹
ÔCˆŒ
Ü—x‘xØ�K‰K×"Ñ"ØØ—*‘*ÜŸ™×+Ñ+×2Ñ2¸$¿+¹+×:WÑ:WÐ2ÓXô	
ˆ�r0   Nr£   r¤   r¥   r  rñ   c	           
      óz  — | j                  ||||||||¬«      }	|	d   }
| j                  j                  rJ| j                   j                  d   d   d   j                  }| j
                  j                  dd|ii|
«      }n| j                  |
«      }|s	|f|	dd  z   S t        ||	j                  |	j                  ¬«      S )	Nr,  r   rí   r(  Ú	embeddingrƒ   r   )ÚlogitsrS   r2  )
rá   r=   Útie_word_embeddingsr®   ÚTr;  r  r   rS   r2  )rJ   rø   r™   rf   r£   r¤   r¥   r  rñ   rÀ   rS   Úshared_kernelÚ	lm_logitss                r.   rU   z#FlaxGemmaForCausalLMModule.__call__¸  sÌ   € ð —*‘*ØØ%Ø)Ø'Ø!Ø/Ø!5Ø#ð ó 	
ˆð   ™
ˆØ�;‰;×*Ò*Ø ŸJ™J×0Ñ0°Ñ:¸>ÑJÈ;ÑW×YÑYˆMØŸ™×*Ñ*¨H°xÀÐ6OÐ+PÐR_Ó`‰IàŸ™ ]Ó3ˆIáØ�< '¨!¨" +Ñ-Ð-ä!¨À'×BWÑBWÐdk×dvÑdvÔwÐwr0   r4  rÞ   r[   r0   r.   r9  r9  ª  ss   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò
ð ØØ"Ø Ø"'Ø%*Ø ñ xð
 ð xð ð xð  ð xð #ð xð ô xr0   r9  zZ
    The Gemma Model transformer with a language modeling head (linear layer) on top.
    c                   ó>   — e Zd ZeZddeej                     fd„Zd„ Z	y)ÚFlaxGemmaForCausalLMNr™   c                 óH  — |j                   \  }}| j                  ||«      }t        j                  ||fd¬«      }|�-|j	                  d¬«      dz
  }t        j                  ||d«      }n4t        j                  t        j                  |d¬«      d d d …f   ||f«      }|||dœS )Nrð   rM   r   r   r   )r   r   )r  r™   rf   )	r2   r¤   r&   rB   Úcumsumr   r–   r—   r    )	rJ   rø   rœ   r™   r¼   Ú
seq_lengthr  Úextended_attention_maskrf   s	            r.   Úprepare_inputs_for_generationz2FlaxGemmaForCausalLM.prepare_inputs_for_generationå  s±   € à!*§¡Ñˆ
�JàŸ/™/¨*°jÓAˆô #&§(¡(¨J¸
Ð+CÈ4Ô"PÐØÐ%Ø)×0Ñ0°bÐ0Ó9¸AÑ=ˆLÜ&)×&>Ñ&>Ð?VÐXfÐhnÓ&oÑ#ä×+Ñ+¬C¯J©J°zÈÔ,NÈtÒUVÈwÑ,WÐZdÐfpÐYqÓrˆLð  /Ø5Ø(ñ
ð 	
r0   c                 óL   — |j                   |d<   |d   d d …dd …f   dz   |d<   |S )Nr  rf   r   r   )r  )rJ   Úmodel_outputsÚmodel_kwargss      r.   Úupdate_inputs_for_generationz1FlaxGemmaForCausalLM.update_inputs_for_generationú  s8   € Ø*7×*GÑ*GˆÐ&Ñ'Ø'3°NÑ'CÂAÀrÁsÀFÑ'KÈaÑ'Oˆ�^Ñ$ØÐr0   r7   )
rW   rX   rY   r9  râ   r   rv   ÚArrayrI  rM  r[   r0   r.   rD  rD  Û  s'   „ ð .€Lñ
ÐS[Ð\_×\eÑ\eÑSfó 
ó*r0   rD  )rD  r6  rà   )=r
  Útypingr   r   Ú
flax.linenÚlinenrw   rv   Ú	jax.numpyÚnumpyr&   r   Úflax.core.frozen_dictr   r   r   r   r	   Úflax.linen.attentionr
   Úflax.traverse_utilr   r   r   Úmodeling_flax_outputsr   r   Úmodeling_flax_utilsr   r   r   Úutilsr   r   r   Úconfiguration_gemmar   Ú
get_loggerrW   rÉ   Ú_CONFIG_FOR_DOCÚ_CHECKPOINT_FOR_DOCÚ_REAL_CHECKPOINT_FOR_DOCÚGEMMA_START_DOCSTRINGr  r/   r5   r:   r  r<   r]   rh   rÃ   rÕ   rà   r  r!  r6  r9  rD  Ú__all__r[   r0   r.   ú<module>ra     s¬  ðñ ç "å Û 
Ý Û ß >Ñ >ß 6Ý >ß ;Ý ç Lß \Ñ \ß YÑ YÝ ,ð 
ˆ×	Ñ	˜HÓ	%€à€Ø'Ð Ø=Ð ð!Ð ðF.Ð òb*òò@ôP�r—y‘yô Pô&˜rŸy™yô ô,W˜Ÿ™ô Wôt�2—9‘9ô ôF'.˜BŸI™Iô '.ôVAÐ2ô AôJ+˜rŸy™yô +ô^;
�b—i‘iô ;
ñ| ØeØóô
#Ð-ó #óð
#ñ ØØØØØ,õô.x §¡ô .xñb ðð ó	ôÐ3ó óðñ< ØØØØØ,õò Q�r0   