Ë
    T^(h¬�  ã                   ó¬  — d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z
 ddl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 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(jV                  e,«      Z-dZ.dZ/dZ0dZ1d)d„Z2 G d„ de
jf                  «      Z4 G d„ de
jf                  «      Z5 G d„ de
jf                  «      Z6 G d„ de
jf                  «      Z7 G d„ de#«      Z8 e&d e0«       G d!„ d"e8«      «       Z9 e$e9e.ee/«        G d#„ d$e
jf                  «      Z: e&d%e0«       G d&„ d'e8«      «       Z; e$e;e.e e/«       g d(¢Z<y)*zFlax XGLM model.é    N)Úpartial)ÚOptionalÚTuple)Ú
FrozenDictÚfreezeÚunfreeze)Úcombine_masksÚmake_causal_mask)Údot_product_attention_weights)Úflatten_dictÚunflatten_dict)Úlax)ÚPRNGKeyé   )Ú-FlaxBaseModelOutputWithPastAndCrossAttentionsÚ%FlaxCausalLMOutputWithCrossAttentions)ÚACT2FNÚFlaxPreTrainedModelÚappend_call_sample_docstring)Úadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚloggingé   )Ú
XGLMConfigzfacebook/xglm-564Mr   a  
    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 ([`XGLMConfig`]): 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` (on GPUs) and
            `jax.numpy.bfloat16` (on TPUs).

            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 (`jnp.ndarray` of shape `(batch_size, sequence_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 (`jnp.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)
        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.max_position_embeddings - 1]`.
        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z  }t        j                  d«      |dz
  z  }t        j                  t        j                  |«      | z  «      }t        j
                  t        j                  | «      d«      t        j
                  |d«      z  }t        j                  t        j                  |«      t        j                  |«      gd«      }t        j                  || |f«      }|�	d||d d …f<   t        j                  |«      S )Né   i'  r   r   )ÚmathÚlogÚnpÚexpÚarangeÚexpand_dimsÚconcatenateÚsinÚcosÚreshapeÚjnpÚarray)Ún_posÚdimÚpadding_idxÚhalf_dimÚembs        úi/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/xglm/modeling_flax_xglm.pyÚcreate_sinusoidal_positionsr/   p   sÃ   € Ø�a‰x€HÜ
�(‰(�5‹/˜X¨™\Ñ
*€CÜ
�&‰&”—‘˜8Ó$¨ tÑ+Ó
,€CÜ
�.‰.œŸ™ 5Ó)¨1Ó
-´·±¸sÀAÓ0FÑ
F€CÜ
�.‰.œ"Ÿ&™& ›+¤r§v¡v¨c£{Ð3°QÓ
7€CÜ
�*‰*�S˜5 #˜,Ó
'€CàÐØˆˆKšˆNÑä�9‰9�S‹>Ðó    c                   óP  — e Zd ZU eed<   eed<   eed<   dZeed<   dZe	ed<   dZ
e	ed	<   ej                  Zej                  ed
<   dd„Zd„ Zd„ Zej$                  d„ «       Z	 	 	 	 ddej(                  deej(                     deej(                     de	de	deej(                     fd„Zy)ÚFlaxXGLMAttentionÚconfigÚ	embed_dimÚ	num_headsç        ÚdropoutFÚcausalTÚbiasÚdtypeÚreturnNc           	      ó  — | j                   | j                  z  | _        | j                  | j                  z  | j                   k7  r&t        d| j                   › d| j                  › d�«      ‚t	        t
        j                  | j                   | j                  | j                  t        j
                  j                  j                  | j                  j                  «      ¬«      } |«        |«        |«       c| _        | _        | _         |«       | _        t        j$                  | j&                  ¬«      | _        | j*                  r>t-        t/        j0                  d| j                  j2                  fd¬«      d¬«      | _        y y )	Nz;embed_dim must be divisible by num_heads (got `embed_dim`: z and `num_heads`: z).©Úuse_biasr:   Úkernel_init©Úrater   Úbool©r:   )r4   r5   Úhead_dimÚ
ValueErrorr   ÚnnÚDenser9   r:   ÚjaxÚinitializersÚnormalr3   Úinit_stdÚq_projÚk_projÚv_projÚout_projÚDropoutr7   Údropout_layerr8   r
   r'   ÚonesÚmax_position_embeddingsÚcausal_mask)ÚselfÚdenses     r.   ÚsetupzFlaxXGLMAttention.setup‡   s  € ØŸ™¨$¯.©.Ñ8ˆŒà�=‰=˜4Ÿ>™>Ñ)¨T¯^©^Ò;ÜØMÈdÏnÉnÐM]ð ^$Ø$(§N¡NÐ#3°2ð7óð ô
 Ü�H‰HØ�N‰NØ—Y‘YØ—*‘*ÜŸ™×+Ñ+×2Ñ2°4·;±;×3GÑ3GÓHô
ˆñ 16³¹»Á%Ã'Ð-ˆŒ�T”[ $¤+Ù›ˆŒäŸZ™Z¨T¯\©\Ô:ˆÔà�;Š;Ü/Ü—‘˜!˜TŸ[™[×@Ñ@ÐAÈÔPÐX^ô ˆDÕð r0   c                 óp   — |j                  |j                  d d | j                  | j                  fz   «      S ©Nr   )r&   Úshaper5   rD   ©rU   Úhidden_statess     r.   Ú_split_headszFlaxXGLMAttention._split_heads¢   s5   € Ø×$Ñ$ ]×%8Ñ%8¸¸!Ð%<ÀÇÁÐPT×P]ÑP]Ð?^Ñ%^Ó_Ð_r0   c                 óZ   — |j                  |j                  d d | j                  fz   «      S rY   )r&   rZ   r4   r[   s     r.   Ú_merge_headszFlaxXGLMAttention._merge_heads¥   s,   € Ø×$Ñ$ ]×%8Ñ%8¸¸!Ð%<ÀÇÁÐ?PÑ%PÓQÐQr0   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   rC   )r'   r(   Úint32© r0   r.   ú<lambda>z9FlaxXGLMAttention._concatenate_to_cache.<locals>.<lambda>³   s   € ÄCÇIÁIÈaÔWZ×W`ÑW`ÔDa€ r0   )r   r   r   )Úhas_variableÚvariabler'   ÚzerosrZ   r:   ÚvalueÚlenr   Údynamic_update_sliceÚbroadcast_tor!   Útupler	   )rU   Úkeyrl   ÚqueryÚattention_maskÚis_initializedrb   rc   rd   Ú
batch_dimsÚ
max_lengthr5   Údepth_per_headÚ	cur_indexÚindicesÚnum_updated_cache_vectorsÚpad_masks                    r.   Ú_concatenate_to_cachez'FlaxXGLMAttention._concatenate_to_cache¨   st  € ð ×*Ñ*¨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   r\   Úkey_value_statesrs   Ú
init_cacheÚdeterministicc                 óÊ  — |du}|j                   d   }| j                  |«      }|r#| j                  |«      }	| j                  |«      }
n"| j                  |«      }	| j                  |«      }
| j	                  |«      }| j	                  |	«      }	| j	                  |
«      }
| j
                  rÍ|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   }t        j                  ||f|j                   dd z   «      }|�N| j
                  rBt        j                  t        j                  |d¬«      j                   «      }t        ||«      }n(| j
                  r}n|�t        j                  |d¬«      }| j
                  r,| j                  dd«      s|r| j                  |	|
||«      \  }	}
}|�°t        j                   |dkD  t        j"                  |j                   d	«      j%                  | j&                  «      t        j"                  |j                   t        j(                  | j&                  «      j*                  «      j%                  | j&                  «      «      }nd}d}|s | j,                  d	kD  r| j/                  d
«      }t1        ||	||| j,                  d|| j&                  d¬«	      }t        j2                  d||
«      }| j5                  |«      }| j7                  |«      }||fS )z#Input shape: Batch x Time x ChannelNr   r   ra   rb   rd   )éýÿÿÿéþÿÿÿ©Úaxisr6   r7   T)r9   Údropout_rngÚdropout_rateÚbroadcast_dropoutr   r:   Ú	precisionz...hqk,...khd->...qhd)rZ   rL   rM   rN   r]   r8   ri   Ú	variablesr   Údynamic_slicerT   r'   ro   r"   r	   r|   ÚselectÚfullÚastyper:   ÚfinfoÚminr7   Úmake_rngr   Úeinsumr_   rO   )rU   r\   r}   rs   r~   r   Úis_cross_attentionÚ
batch_sizeÚquery_statesÚ
key_statesÚvalue_statesÚquery_lengthÚ
key_lengthÚ
mask_shiftÚmax_decoder_lengthrT   Úattention_biasr…   Úattn_weightsÚattn_outputs                       r.   Ú__call__zFlaxXGLMAttention.__call__É   s,  € ð .°TÐ9ÐØ"×(Ñ(¨Ñ+ˆ
ð —{‘{ =Ó1ˆáàŸ™Ð%5Ó6ˆJØŸ;™;Ð'7Ó8‰Lð Ÿ™ ]Ó3ˆJØŸ;™; }Ó5ˆLà×(Ñ(¨Ó6ˆØ×&Ñ& zÓ2ˆ
Ø×(Ñ(¨Ó6ˆð �;Š;Ø'3×'9Ñ'9¸!Ñ'<¸j×>NÑ>NÈqÑ>Q˜*ˆLØ× Ñ  ¨,Ô7Ø!Ÿ^™^¨GÑ4°]ÑC�
Ø%)§^¡^°GÑ%<¸\Ñ%J×%PÑ%PÐQRÑ%SÐ"Ü!×/Ñ/Ø×$Ñ$ q¨!¨Z¸Ð&;¸aÀÀLÐRdÐ=eó‘ð #×.Ñ.ªq²!°]°l°]ÀKÀZÀKÐ/OÑP�Ü×*Ñ*¨;¸¸È×HYÑHYÐZ[ÐZ\ÐH]Ñ8]Ó^ˆKð Ð%¨$¯+ª+Ü ×-Ñ-¬c¯o©o¸nÐS[Ô.\Ð^i×^oÑ^oÓpˆNÜ*¨>¸;ÓG‰NØ�[Š[Ø(‰NØÐ'Ü Ÿ_™_¨^À(ÔKˆNð �;Š;˜D×-Ñ-¨g°|ÔDÉ
Ø7;×7QÑ7QØ˜L¨,¸ó8Ñ4ˆJ˜ nð
 Ð%ä ŸZ™ZØ Ñ"Ü—‘˜×-Ñ-¨sÓ3×:Ñ:¸4¿:¹:ÓFÜ—‘˜×-Ñ-¬s¯y©y¸¿¹Ó/D×/HÑ/HÓI×PÑPÐQU×Q[ÑQ[Ó\ó‰Nð "ˆNàˆÙ §¡°Ò!3ØŸ-™-¨	Ó2ˆKä4ØØØØ#ØŸ™Ø"Ø'Ø—*‘*Øô

ˆô —j‘jÐ!8¸,ÈÓUˆØ×'Ñ'¨Ó4ˆØ—m‘m KÓ0ˆà˜LÐ(Ð(r0   ©r;   N)NNFT)Ú__name__Ú
__module__Ú__qualname__r   Ú__annotations__Úintr7   Úfloatr8   rB   r9   r'   Úfloat32r:   rW   r]   r_   rF   Úcompactr|   Úndarrayr   r   rž   rg   r0   r.   r2   r2   ~   sß   … ØÓØƒNØƒNØ€GˆUÓØ€FˆDÓØ€Dˆ$ÓØ—{‘{€Eˆ3�9‰9Ó"óò6`òRð ‡Z�Zñ*ó ð*ðF 37Ø04Ø Ø"ñ[)à—{‘{ð[)ð # 3§;¡;Ñ/ð[)ð ! §¡Ñ-ð	[)ð
 ð[)ð ð[)ð 
ˆs�{‰{Ñ	ô[)r0   r2   c                   ó   — e Zd ZU eed<   ej                  Zej                  ed<   dd„Z	 	 	 	 	 ddej                  dej                  de
ej                     d	e
ej                     d
edededeej                     fd„Zy)ÚFlaxXGLMDecoderLayerr3   r:   r;   Nc                 ó|  — | j                   j                  | _        t        | j                   | j                  | j                   j                  | j                   j
                  d| j                  ¬«      | _        t        j                  | j                  d¬«      | _
        t        j                  | j                   j                  ¬«      | _        t        | j                   j                     | _        t        j                  | j                   j"                  ¬«      | _        | j                   j&                  r�t        | j                   | j                  | j                   j(                  | j                   j
                  | j                  ¬«      | _        t        j                  | j                  d¬«      | _        t        j.                  | j                   j0                  | j                  t2        j                  j4                  j7                  | j                   j8                  «      ¬«      | _        t        j.                  | j                  | j                  t2        j                  j4                  j7                  | j                   j8                  «      ¬«      | _        t        j                  | j                  d¬«      | _        y )NT)r3   r4   r5   r7   r8   r:   çñhãˆµøä>©r:   Úepsilonr@   )r3   r4   r5   r7   r:   )r:   r?   ) r3   Úd_modelr4   r2   Úattention_headsÚattention_dropoutr:   Ú	self_attnrF   Ú	LayerNormÚself_attn_layer_normrP   r7   rQ   r   Úactivation_functionÚactivation_fnÚactivation_dropoutÚactivation_dropout_layerÚadd_cross_attentionÚdecoder_attention_headsÚencoder_attnÚencoder_attn_layer_normrG   Úffn_dimrH   rI   rJ   rK   Úfc1Úfc2Úfinal_layer_norm©rU   s    r.   rW   zFlaxXGLMDecoderLayer.setup+  s¼  € ØŸ™×,Ñ,ˆŒÜ*Ø—;‘;Ø—n‘nØ—k‘k×1Ñ1Ø—K‘K×1Ñ1ØØ—*‘*ô
ˆŒô %'§L¡L°t·z±zÈ5Ô$QˆÔ!ÜŸZ™Z¨T¯[©[×-@Ñ-@ÔAˆÔÜ# D§K¡K×$CÑ$CÑDˆÔÜ(*¯
©
¸¿¹×8VÑ8VÔ(WˆÔ%à�;‰;×*Ò*Ü 1Ø—{‘{ØŸ.™.ØŸ+™+×=Ñ=ØŸ™×5Ñ5Ø—j‘jô!ˆDÔô ,.¯<©<¸d¿j¹jÐRWÔ+XˆDÔ(ä—8‘8Ø�K‰K×ÑØ—*‘*ÜŸ™×+Ñ+×2Ñ2°4·;±;×3GÑ3GÓHô
ˆŒô
 —8‘8Ø�N‰N $§*¡*¼#¿&¹&×:MÑ:M×:TÑ:TÐUY×U`ÑU`×UiÑUiÓ:jô
ˆŒô !#§¡°4·:±:ÀuÔ MˆÕr0   r\   rs   Úencoder_hidden_statesÚencoder_attention_maskr~   Úoutput_attentionsr   c                 ó  — |}| j                  |«      }| j                  |||¬«      \  }}	| j                  ||¬«      }||z   }d }
|�B|}| j                  |«      }| j	                  |||¬«      \  }}
| j                  ||¬«      }||z   }|}| j                  |«      }| j                  | j                  |«      «      }| j                  ||¬«      }| j                  |«      }| j                  ||¬«      }||z   }|f}|r||	|
fz  }|S )N)r\   rs   r~   ©r   )r\   r}   rs   )
r´   r²   rQ   r¼   r»   rÀ   r¶   r¾   r¸   r¿   )rU   r\   rs   rÂ   rÃ   r~   rÄ   r   ÚresidualÚself_attn_weightsÚcross_attn_weightsÚoutputss               r.   rž   zFlaxXGLMDecoderLayer.__call__O  sZ  € ð !ˆØ×1Ñ1°-Ó@ˆð ,0¯>©>Ø'¸ÐS]ð ,:ó ,
Ñ(ˆÐ(ð ×*Ñ*¨=ÈÐ*ÓVˆØ  =Ñ0ˆð "ÐØ Ð,Ø$ˆHà ×8Ñ8¸ÓGˆMØ04×0AÑ0AØ+Ø!6Ø5ð 1Bó 1Ñ-ˆMÐ-ð
 !×.Ñ.¨}ÈMÐ.ÓZˆMØ$ }Ñ4ˆMð !ˆØ×-Ñ-¨mÓ<ˆØ×*Ñ*¨4¯8©8°MÓ+BÓCˆØ×5Ñ5°mÐS`Ð5ÓaˆØŸ™ Ó/ˆØ×*Ñ*¨=ÈÐ*ÓVˆØ  =Ñ0ˆà Ð"ˆáØÐ)Ð+=Ð>Ñ>ˆGàˆr0   rŸ   )NNFTT)r    r¡   r¢   r   r£   r'   r¦   r:   rW   r¨   r   rB   r   rž   rg   r0   r.   rª   rª   '  s¨   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ó!NðP 8<Ø8<Ø Ø"&Ø"ñ0à—{‘{ð0ð Ÿ™ð0ð  (¨¯©Ñ4ð	0ð
 !)¨¯©Ñ 5ð0ð ð0ð  ð0ð ð0ð 
ˆs�{‰{Ñ	ô0r0   rª   c                   ó¼   — e Zd ZU eed<   ej                  Zej                  ed<   d„ Z	 	 	 	 	 	 	 dde	ej                     de	ej                     deded	ed
edefd„Zy)ÚFlaxXGLMDecoderLayerCollectionr3   r:   c           	      óú   — t        | j                  j                  «      D �cg c]-  }t        | j                  t	        |«      | j
                  ¬«      ‘Œ/ c}| _        | j                  j                  | _        y c c}w )N)Únamer:   )Úranger3   Ú
num_layersrª   Ústrr:   ÚlayersÚ	layerdrop)rU   Úis     r.   rW   z$FlaxXGLMDecoderLayerCollection.setup†  sY   € äV[Ð\`×\gÑ\g×\rÑ\rÓVsö
ØQRÔ  §¡´3°q³6ÀÇÁÖLò
ˆŒð Ÿ™×.Ñ.ˆ�ùò
s   ¢2A8NrÂ   rÃ   r   r~   rÄ   Úoutput_hidden_statesÚreturn_dictc
           
      ój  — |rdnd }
|rdnd }|r|�dnd }| j                   D ]`  }|r|
|fz  }
t        j                  dd«      }|s|| j                  k  rd}n ||||||||¬«      }|d   }|sŒL||d   fz  }|€ŒX||d   fz  }Œb |r|
|fz  }
||
||f}|	st	        d„ |D «       «      S t        ||
||¬«      S )	Nrg   r   r   )NNN)rs   rÂ   rÃ   r~   rÄ   r   r   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­w©Nrg   ©Ú.0Úvs     r.   ú	<genexpr>z:FlaxXGLMDecoderLayerCollection.__call__.<locals>.<genexpr>½  ó   è ø€ Ò=˜q¨q©}œÑ=ùó   ‚Š©Úlast_hidden_stater\   Ú
attentionsÚcross_attentions)rÒ   ÚrandomÚuniformrÓ   rp   r   )rU   r\   rs   rÂ   rÃ   r   r~   rÄ   rÕ   rÖ   Úall_hidden_statesÚall_self_attnsÚall_cross_attentionsÚdecoder_layerÚdropout_probabilityÚlayer_outputsrÊ   s                    r.   rž   z'FlaxXGLMDecoderLayerCollection.__call__Œ  s  € ñ #7™B¸DÐÙ0™°dˆÙ&7Ð<QÐ<]™rÐdhÐà!Ÿ[™[ò 	@ˆMÙ#Ø! mÐ%5Ñ5Ð!ä"(§.¡.°°AÓ"6ÐÙ Ð&9¸D¿N¹NÒ&JØ 2‘á -Ø!Ø#1Ø*?Ø+AØ)Ø&7Ø"/ô!�ð *¨!Ñ,ˆMÚ Ø =°Ñ#3Ð"5Ñ5�à(Ñ4Ø(¨]¸1Ñ-=Ð,?Ñ?Ñ(ð/	@ñ4  Ø -Ð!1Ñ1Ðà Ð"3°^ÐEYÐZˆáÜÑ= GÔ=Ó=Ð=ä<Ø+Ø+Ø%Ø1ô	
ð 	
r0   )NNTFFFT©r    r¡   r¢   r   r£   r'   r¦   r:   rW   r   r¨   rB   rž   rg   r0   r.   rÌ   rÌ   ‚  s“   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò/ð 8<Ø8<Ø"Ø Ø"'Ø%*Ø ñ8
ð  (¨¯©Ñ4ð	8
ð
 !)¨¯©Ñ 5ð8
ð ð8
ð ð8
ð  ð8
ð #ð8
ð ô8
r0   rÌ   c                   ó¼   — e Zd ZU eed<   ej                  Zej                  ed<   d„ Z	 	 	 	 	 	 	 dde	ej                     de	ej                     deded	ed
edefd„Zy)ÚFlaxXGLMModuler3   r:   c                 óf  — t        j                  | j                  j                  ¬«      | _        | j                  j
                  }| j                  j                  | _        | j                  j                  | _	        | j                  j                  r)t        j                  | j                  j
                  «      nd| _        t        j                  | j                  j                  |t         j                   j"                  j%                  | j                  j&                  «      ¬«      | _        d| _        t-        | j                  j                  | j*                  z   |«      | _        t1        | j                  | j2                  «      | _        t        j6                  | j2                  d¬«      | _        y )Nr@   g      ð?)Úembedding_initr   r¬   r­   )rF   rP   r3   r7   rQ   r¯   Úpad_token_idr+   rS   Úmax_target_positionsÚscale_embeddingr   ÚsqrtÚembed_scaleÚEmbedÚ
vocab_sizerH   rI   rJ   rK   Úembed_tokensÚoffsetr/   Úembed_positionsrÌ   r:   rÒ   r³   Ú
layer_norm)rU   r4   s     r.   rW   zFlaxXGLMModule.setupË  s  € ÜŸZ™Z¨T¯[©[×-@Ñ-@ÔAˆÔà—K‘K×'Ñ'ˆ	ØŸ;™;×3Ñ3ˆÔØ$(§K¡K×$GÑ$GˆÔ!Ø=A¿[¹[×=XÒ=Xœ4Ÿ9™9 T§[¡[×%8Ñ%8Ô9Ð^aˆÔäŸH™HØ�K‰K×"Ñ"ØÜŸ6™6×.Ñ.×5Ñ5°d·k±k×6JÑ6JÓKô
ˆÔð ˆŒÜ:Ø�K‰K×/Ñ/°$·+±+Ñ=¸yó 
ˆÔô 5°T·[±[À$Ç*Á*ÓMˆŒÜŸ,™,¨T¯Z©ZÀÔGˆ�r0   NrÂ   rÃ   r~   rÄ   rÕ   rÖ   r   c                 ó&  — |j                   }|j                  d|d   «      }| j                  |«      | j                  z  }|| j                  z   }t        j                  | j                  |d¬«      }||z   }| j                  ||
¬«      }| j                  |||||
||||	¬«	      }|d   }| j                  |«      }d }|r|d   }|d d |fz   }|	s#||f|r|dd  n|dd  z   }t        d„ |D «       «      S t        |||j                  |j                  ¬	«      S )
Néÿÿÿÿr   rƒ   rÆ   ©r   r~   rÄ   rÕ   rÖ   r   r   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrÙ   rg   rÚ   s     r.   rÝ   z*FlaxXGLMModule.__call__.<locals>.<genexpr>  rÞ   rß   rà   )rZ   r&   rø   rõ   rù   r'   Útakerú   rQ   rÒ   rû   rp   r   râ   rã   )rU   Ú	input_idsrs   Úposition_idsrÂ   rÃ   r~   rÄ   rÕ   rÖ   r   Úinput_shapeÚinputs_embedsÚ	positionsr\   rÊ   Úlast_hidden_statess                    r.   rž   zFlaxXGLMModule.__call__â  sR  € ð  —o‘oˆØ×%Ñ% b¨+°b©/Ó:ˆ	à×)Ñ)¨)Ó4°t×7GÑ7GÑGˆð $ d§k¡kÑ1ˆÜ—H‘H˜T×1Ñ1°<ÀaÔHˆ	à%¨	Ñ1ˆØ×*Ñ*¨=ÈÐ*ÓVˆà—+‘+ØØØ!Ø"Ø'Ø!Ø/Ø!5Ø#ð ó 

ˆð % Q™ZÐØ!Ÿ_™_Ð-?Ó@ÐàˆÙØ# A™JˆMØ)¨#¨2Ð.Ð2DÐ1FÑFˆMáØ)¨=Ð9ÑL`¸WÀQÀR¹[ÐfmÐnoÐnpÐfqÑrˆGÜÑ= GÔ=Ó=Ð=ä<Ø0Ø'Ø×)Ñ)Ø$×5Ñ5ô	
ð 	
r0   ©NNFFFTTrì   rg   r0   r.   rî   rî   Ç  s”   … ØÓØ—{‘{€Eˆ3�9‰9Ó"òHð8 8<Ø8<Ø Ø"'Ø%*Ø Ø"ñ6
ð
  (¨¯©Ñ4ð6
ð !)¨¯©Ñ 5ð6
ð ð6
ð  ð6
ð #ð6
ð ð6
ð ô6
r0   rî   c                   óÞ  ‡ — e Zd ZU eZdZeed<   dZe	j                  ed<   ddej                  dfded	e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j4                  deej4                     deej4                     deej4                     deej4                     dee   dee   dee   dedededefd„«       Zˆ xZS )!ÚFlaxXGLMPreTrainedModelÚmodelÚbase_model_prefixNÚmodule_class)r   r   r   Tr3   r  Úseedr:   Ú_do_initc                 óZ   •—  | j                   d||dœ|¤Ž}t        ‰| �	  ||||||¬«       y )N)r3   r:   )r  r  r:   r  rg   )r  ÚsuperÚ__init__)	rU   r3   r  r  r:   r  ÚkwargsÚmoduleÚ	__class__s	           €r.   r  z FlaxXGLMPreTrainedModel.__init__   s=   ø€ ð #�×"Ñ"ÐH¨&¸ÑHÀÑHˆÜ‰Ñ˜ °[ÀtÐSXÐckÐÕlr0   ÚrngÚparamsr;   c           	      ó$  — t        j                  |d¬«      }t        j                  |«      }t        j                  t        j                  t        j
                  |«      j                  d   «      |«      }t        j                  j                  |«      \  }}||dœ}	| j                  j                  rRt        j                  || j                  j                  fz   «      }
|}| j                  j                  |	||||
|d¬«      }n | j                  j                  |	|||d¬«      }|d   }|�dt        t!        |«      «      }t        t!        |«      «      }| j"                  D ]
  }||   ||<   Œ t%        «       | _        t'        t)        |«      «      S |S )NÚi4rC   rý   )r  r7   F)rÖ   r  )r'   rk   Ú	ones_likero   r!   Ú
atleast_2drZ   rH   rä   Úsplitr3   r¹   Ún_embdr  Úinitr   r   Ú_missing_keysÚsetr   r   )rU   r  r  r  r  rs   r  Ú
params_rngr…   ÚrngsrÂ   rÃ   Úmodule_init_outputsÚrandom_paramsÚmissing_keys                  r.   Úinit_weightsz$FlaxXGLMPreTrainedModel.init_weights,  so  € ä—I‘I˜k°Ô6ˆ	ÜŸ™ yÓ1ˆÜ×'Ñ'¬¯
©
´3·>±>À)Ó3L×3RÑ3RÐSUÑ3VÓ(WÐYdÓeˆÜ"%§*¡*×"2Ñ"2°3Ó"7Ñˆ
�KØ$°Ñ=ˆà�;‰;×*Ò*Ü$'§I¡I¨k¸T¿[¹[×=OÑ=OÐ<QÑ.QÓ$RÐ!Ø%3Ð"Ø"&§+¡+×"2Ñ"2ØØØØØ%Ø&Ø!ð #3ó #Ñð #'§+¡+×"2Ñ"2°4¸ÀNÐT`ÐnsÐ"2Ó"tÐà+¨HÑ5ˆàÐÜ(¬°-Ó)@ÓAˆMÜ!¤(¨6Ó"2Ó3ˆFØ#×1Ñ1ò A�Ø&3°KÑ&@��{Ò#ðAä!$£ˆDÔÜœ.¨Ó0Ó1Ð1à Ð r0   c                 ó   — t        j                  ||fd¬«      }t        j                  |d¬«      }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  rC   rý   r   FT)rÖ   r~   ra   )r'   rR   r  ro   r!   r  rZ   r  r  rH   rä   r   r   )rU   r“   rv   r  rs   r  Úinit_variabless          r.   r~   z"FlaxXGLMPreTrainedModel.init_cacheO  s©   € ô —H‘H˜j¨*Ð5¸TÔBˆ	ÜŸ™ y¸Ô=ˆÜ×'Ñ'¬¯
©
´3·>±>À)Ó3L×3RÑ3RÐSUÑ3VÓ(WÐYb×YhÑYhÓiˆàŸ™×)Ñ)Ü�J‰J×Ñ˜qÓ! 9¨n¸lÐX]Ðjnð *ó 
ˆô ˜ wÑ/Ó0Ð0r0   r  rs   r  rÂ   rÃ   rÄ   rÕ   rÖ   ÚtrainÚpast_key_valuesr…   c                 óL  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|�+|€)|j                  d d \  }}t        j                  ||f«      }|€t        j                  |«      }|€A|j                  \  }}t        j                  t        j                  |«      d d d …f   ||f«      }|�d|ini }d|
xs | j                  i}|r	||d<   dg}nd}| j                  j                  |t        j                  |d¬«      t        j                  |d¬«      t        j                  |d¬«      ||||||	 ||¬«      }|�|r|\  }}t        |d   «      |d	<   |S |�"|s |\  }}|d d
 t        |d   «      fz   |d
d  z   }|S )Nr   r7   r  ra   Fr  rC   )r  rs   r  rÂ   rÃ   rÄ   rÕ   rÖ   r   r!  Úmutabler)  r   )r3   rÄ   rÕ   rÖ   rZ   r'   rR   r  ro   r!   r  r  Úapplyr(   r   )rU   r  rs   r  rÂ   rÃ   rÄ   rÕ   rÖ   r(  r  r)  r…   r“   Úsequence_lengthr!  Úinputsr+  rÊ   s                      r.   rž   z FlaxXGLMPreTrainedModel.__call__b  sí  € ð  2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×BYÑBYˆà Ð,Ð1GÐ1OØ*?×*EÑ*EÀbÀqÐ*IÑ'ˆJ˜Ü%(§X¡X¨z¸?Ð.KÓ%LÐ"ð Ð!Ü Ÿ]™]¨9Ó5ˆNØÐØ*3¯/©/Ñ'ˆJ˜Ü×+Ñ+¬C¯J©J°Ó,GÈÊaÈÑ,PÐS]Ð_nÐRoÓpˆLð ,7Ð+B�	˜;Ñ'Èˆà˜FÒ1 d§k¡kÐ2ˆñ
 Ø-ˆF�7‰OØ�i‰GàˆGà—+‘+×#Ñ#ØÜ—i‘i 	°Ô6ÜŸ9™9 ^¸4Ô@ÜŸ™ <°tÔ<Ø"7Ø#9Ø/Ø!5Ø#Ø#˜)ØØð $ó 
ˆð  Ð&©;Ø'.Ñ$ˆG�_Ü)1°/À'Ñ2JÓ)KˆGÐ%Ñ&ØˆNØÐ(±Ø'.Ñ$ˆG�_Ø˜b˜q�k¤X¨o¸gÑ.FÓ%GÐ$IÑIÈGÐTUÐTVÈKÑWˆGàˆr0   rÙ   )NNNNNNNFNNN)r    r¡   r¢   r   Úconfig_classr  rÑ   r£   r  rF   ÚModuler'   r¦   r   r¤   r:   rB   r  rH   rä   r   r   r%  r~   r   ÚXGLM_INPUTS_DOCSTRINGr¨   r   Údictrž   Ú__classcell__)r  s   @r.   r	  r	    sŸ  ø… Ø€LØ$Ð�sÓ$Ø"€L�"—)‘)Ó"ð
 #)ØØŸ;™;Øñ
màð
mð ˜3‘Zð
mð ð	
mð
 �y‰yð
mð õ
mñ!! §
¡
× 2Ñ 2ð !!Àð !!ÐPZð !!Ðfpó !!òF1ñ& +Ð+@ÓAð 15Ø.2Ø7;Ø8<Ø,0Ø/3Ø&*ØØØ $Ø#ñFà—;‘;ðFð ! §¡Ñ-ðFð ˜sŸ{™{Ñ+ð	Fð
  (¨¯©Ñ4ðFð !)¨¯©Ñ 5ðFð $ D™>ðFð ' t™nðFð ˜d‘^ðFð ðFð ðFð ðFð òFó BôFr0   r	  z^The bare XGLM Model transformer outputting raw hidden-states without any specific head on top.c                   ó   — e Zd ZeZy)ÚFlaxXGLMModelN)r    r¡   r¢   rî   r  rg   r0   r.   r5  r5  ¬  s	   „ ð
 "�Lr0   r5  c                   ó¼   — e Zd ZU eed<   ej                  Zej                  ed<   d„ Z	 	 	 	 	 	 	 dde	ej                     de	ej                     deded	ed
edefd„Zy)ÚFlaxXGLMForCausalLMModuler3   r:   c                 ó<  — t        | j                  | j                  «      | _        t	        j
                  | j                  j                  d| j                  t        j                  j                  j                  | j                  j                  «      ¬«      | _        y )NFr=   )rî   r3   r:   r
  rF   rG   r÷   rH   rI   rJ   rK   Úlm_headrÁ   s    r.   rW   zFlaxXGLMForCausalLMModule.setupÀ  sa   € Ü# D§K¡K°·±Ó<ˆŒ
Ü—x‘xØ�K‰K×"Ñ"ØØ—*‘*ÜŸ™×+Ñ+×2Ñ2°4·;±;×3GÑ3GÓHô	
ˆ�r0   NrÂ   rÃ   r~   rÄ   rÕ   rÖ   r   c                 ó”  — | j                  ||||||
||||	¬«
      }|d   }| j                  j                  rJ| j                   j                  d   d   d   }| j                  j                  dd|j                  ii|«      }n| j	                  |«      }|	s	|f|dd  z   S t        ||j                  |j                  |j                  ¬«      S )	Nrþ   r   r  rø   Ú	embeddingÚkernelr   )Úlogitsr\   râ   rã   )r
  r3   Útie_word_embeddingsr‰   r9  r,  ÚTr   r\   râ   rã   )rU   r  rs   r  rÂ   rÃ   r~   rÄ   rÕ   rÖ   r   rÊ   r\   Úshared_embeddingÚ	lm_logitss                  r.   rž   z"FlaxXGLMForCausalLMModule.__call__É  sã   € ð —*‘*ØØØØ!Ø"Ø'Ø!Ø/Ø!5Ø#ð ó 
ˆð   ™
ˆà�;‰;×*Ò*Ø#Ÿz™z×3Ñ3°HÑ=¸nÑMÈkÑZÐØŸ™×*Ñ*¨H°xÐAQ×ASÑASÐ6TÐ+UÐWdÓe‰IàŸ™ ]Ó3ˆIáØ�< '¨!¨" +Ñ-Ð-ä4ØØ!×/Ñ/Ø×)Ñ)Ø$×5Ñ5ô	
ð 	
r0   r  rì   rg   r0   r.   r7  r7  ¼  s“   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò
ð 8<Ø8<Ø Ø"'Ø%*Ø Ø"ñ*
ð
  (¨¯©Ñ4ð*
ð !)¨¯©Ñ 5ð*
ð ð*
ð  ð*
ð #ð*
ð ð*
ð ô*
r0   r7  z‡
    The XGLM Model transformer with a language modeling head on top (linear layer with weights tied to the input
    embeddings).
    c                   ó>   — e Zd ZeZddeej                     fd„Zd„ Z	y)ÚFlaxXGLMForCausalLMNrs   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  rC   rý   rƒ   r   )r   r   )r)  rs   r  )	rZ   r~   r'   rR   Úcumsumr   rn   ro   r!   )	rU   r  rv   rs   r“   Ú
seq_lengthr)  Úextended_attention_maskr  s	            r.   Úprepare_inputs_for_generationz1FlaxXGLMForCausalLM.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)  r  rý   r   )r)  )rU   Úmodel_outputsÚmodel_kwargss      r.   Úupdate_inputs_for_generationz0FlaxXGLMForCausalLM.update_inputs_for_generation  s8   € Ø*7×*GÑ*GˆÐ&Ñ'Ø'3°NÑ'CÂAÀrÁsÀFÑ'KÈaÑ'Oˆ�^Ñ$ØÐr0   rÙ   )
r    r¡   r¢   r7  r  r   rH   ÚArrayrH  rL  rg   r0   r.   rC  rC  ö  s'   „ ð -€Lñ
ÐS[Ð\_×\eÑ\eÑSfó 
ó*r0   rC  )rC  r5  r	  )r   )=Ú__doc__r   rä   Ú	functoolsr   Útypingr   r   Ú
flax.linenÚlinenrF   rH   Ú	jax.numpyÚnumpyr'   r   Úflax.core.frozen_dictr   r   r   r	   r
   Úflax.linen.attentionr   Úflax.traverse_utilr   r   r   Ú
jax.randomr   Úmodeling_flax_outputsr   r   Úmodeling_flax_utilsr   r   r   Úutilsr   r   r   Úconfiguration_xglmr   Ú
get_loggerr    ÚloggerÚ_CHECKPOINT_FOR_DOCÚ_CONFIG_FOR_DOCÚXGLM_START_DOCSTRINGr1  r/   r0  r2   rª   rÌ   rî   r	  r5  r7  rC  Ú__all__rg   r0   r.   ú<module>rc     sv  ðñ ã Û Ý ß "å Û 
Ý Û ß >Ñ >ß 6Ý >ß ;Ý Ý ÷÷ ]Ñ \ß YÑ YÝ *ð 
ˆ×	Ñ	˜HÓ	%€à*Ð Ø€ð Ð ðDÐ ó>ôf)˜Ÿ	™	ô f)ôRX˜2Ÿ9™9ô XôvB
 R§Y¡Yô B
ôJQ
�R—Y‘Yô Q
ôhNÐ1ô Nñb ØdØóô"Ð+ó "ó	ð"ñ ØØØ1Øô	ô7
 §	¡	ô 7
ñt ðð óôÐ1ó óðñ< ØØØ)Øô	ò N�r0   