Ë
    S^(h¾o  ã                   óÀ  — 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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#jL                  e'«      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«      Z4 G d„ dej`                  «      Z5 G d „ d!ej`                  «      Z6 e!d"e+«       G d#„ d$e4«      «       Z7 ee7e)ee*«        G d%„ d&ej`                  «      Z8 e!d'e+«       G d(„ d)e4«      «       Z9 ee9e)ee*«       g d*¢Z:y)+é    )Úpartial)Ú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é   )Ú
GPTJConfigÚgptjr   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 ([`GPTJConfig`]): 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 (`numpy.ndarray` of shape `(batch_size, input_ids_length)`):
            `input_ids_length` = `sequence_length`. Indices of input sequence tokens in the vocabulary.

            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)
        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]`.
        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«      |z  z  z  }t        j                  dt        j                  | «      |«      j                  d«      }t        j                  |«      t        j
                  |«      }}|dz  |dz  z   }t        j                  | |f«      }||d d …d|…f<   ||d d …|d …f<   t        j                  |«      S )Ng      ð?i'  r   é   zi , j -> i jÚfloat32)	ÚnpÚarangeÚeinsumÚastypeÚsinÚcosÚzerosÚjnpÚarray)Únum_posÚdimÚinv_freqÚsinusoid_inpr"   r#   ÚsentinelÚouts           úi/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/gptj/modeling_flax_gptj.pyÚcreate_sinusoidal_positionsr.   m   s¹   € Ø�e¤§	¡	¨!¨S°!Ó 4°sÑ :Ñ;Ñ<€HÜ—9‘9˜^¬R¯Y©Y°wÓ-?ÀÓJ×QÑQÐR[Ó\€LÜ�v‰v�lÓ#¤R§V¡V¨LÓ%9ˆ€Cà�a‰x˜# ™'Ñ!€HÜ
�(‰(�G˜S�>Ó
"€CØ€CŠˆ1ˆXˆ:ˆÑØ€CŠˆ8‰9ˆÑä�9‰9�S‹>Ðó    c           
      ó¼   — t        j                  | d d …d d …d d …dd d…f    | d d …d d …d d …d d d…f   fd¬«      }|j                  |j                  d d dz   «      }|S )Nr   r   éÿÿÿÿ©Úaxiséþÿÿÿ)r1   )r%   ÚstackÚreshapeÚshape)ÚtensorÚrotate_half_tensors     r-   Úrotate_every_twor:   z   sk   € ÜŸ™ VªAªq²!°Q°T¸°T¨MÑ%:Ð$:¸FÂ1ÂaÊÉCÈaÈCÀ<Ñ<PÐ#QÐXZÔ[ÐØ+×3Ñ3Ð4F×4LÑ4LÈSÈbÐ4QÐTYÑ4YÓZÐØÐr/   c                 ó°   — |\  }}|d d …d d …d d d …f   j                  dd«      }|d d …d d …d d d …f   j                  dd«      }| |z  t        | «      |z  z   S )Nr   r   )Úrepeatr:   )r8   ÚsincosÚsin_posÚcos_poss       r-   Úapply_rotary_pos_embr@   €   se   € ØÑ€GˆWØ’aš˜D¢!�mÑ$×+Ñ+¨A¨qÓ1€GØ’aš˜D¢!�mÑ$×+Ñ+¨A¨qÓ1€GØ�WÑÔ!1°&Ó!9¸GÑ!CÑDÐDr/   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)ÚFlaxGPTJAttentionÚconfigÚdtypeTÚcausalFÚis_cross_attentionc           	      ó  — | j                   }|j                  | _        |j                  | _        | j                  | j                  z  | _        |j                  | _        t        t        j                  | j                  d| j                  t        j                  j                  j                  | j                   j                  «      ¬«      } |«        |«        |«       c| _        | _        | _         |«       | _        t        j&                  |j(                  ¬«      | _        t-        t/        j0                  d|j2                  fd¬«      d¬«      | _        | j                  xs | j                  }t7        |j2                  |«      | _        y )NF)Úuse_biasrD   Úkernel_init©Úrater   Úbool©rD   )rC   Úhidden_sizeÚ	embed_dimÚnum_attention_headsÚ	num_headsÚhead_dimÚ
rotary_dimr   ÚnnÚDenserD   ÚjaxÚinitializersÚnormalÚinitializer_rangeÚq_projÚk_projÚv_projÚout_projÚDropoutÚresid_pdropÚresid_dropoutr
   r%   ÚonesÚmax_position_embeddingsÚcausal_maskr.   Úembed_positions)ÚselfrC   ÚdenseÚpos_embd_dims       r-   ÚsetupzFlaxGPTJAttention.setup�   s  € Ø—‘ˆØ×+Ñ+ˆŒØ×3Ñ3ˆŒØŸ™¨$¯.©.Ñ8ˆŒà ×+Ñ+ˆŒäÜ�H‰HØ�N‰NØØ—*‘*ÜŸ™×+Ñ+×2Ñ2°4·;±;×3PÑ3PÓQô
ˆñ 16³¹»Á%Ã'Ð-ˆŒ�T”[ $¤+Ù›ˆŒäŸZ™Z¨V×-?Ñ-?Ô@ˆÔä+¬C¯H©H°a¸×9WÑ9WÐ5XÐ`fÔ,gÐouÔvˆÔà—‘Ò8¨$¯.©.ˆÜ:¸6×;YÑ;YÐ[gÓhˆÕr/   c                 óp   — |j                  |j                  d d | j                  | j                  fz   «      S ©Nr   )r6   r7   rQ   rR   ©re   Úhidden_statess     r-   Ú_split_headszFlaxGPTJAttention._split_heads§   s5   € Ø×$Ñ$ ]×%8Ñ%8¸¸!Ð%<ÀÇÁÐPT×P]ÑP]Ð?^Ñ%^Ó_Ð_r/   c                 óZ   — |j                  |j                  d d | j                  fz   «      S rj   )r6   r7   rO   rk   s     r-   Ú_merge_headszFlaxGPTJAttention._merge_headsª   s,   € Ø×$Ñ$ ]×%8Ñ%8¸¸!Ð%<ÀÇÁÐ?PÑ%PÓQÐQr/   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&   Úint32© r/   r-   ú<lambda>z9FlaxGPTJAttention._concatenate_to_cache.<locals>.<lambda>¸   s   € ÄCÇIÁIÈaÔWZ×W`ÑW`ÔDa€ r/   )r   r   r   )Úhas_variableÚvariabler%   r$   r7   rD   ÚvalueÚlenr   Údynamic_update_sliceÚbroadcast_tor   Útupler	   )re   Úkeyr{   ÚqueryÚattention_maskÚis_initializedrr   rs   rt   Ú
batch_dimsÚ
max_lengthrQ   Údepth_per_headÚ	cur_indexÚindicesÚnum_updated_cache_vectorsÚpad_masks                    r-   Ú_concatenate_to_cachez'FlaxGPTJAttention._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˜>Ð)Ð)r/   ÚdeterministicÚ
init_cacheÚoutput_attentionsc           
      óX  — | j                  |«      }| j                  |«      }| j                  |«      }	| j                  |«      }| j                  |«      }| j                  |	«      }	t	        j
                  | j                  |d¬«      }
t	        j                  |
dd¬«      }
| j                  �·|d d …d d …d d …d | j                  …f   }|d d …d d …d d …| j                  d …f   }|d d …d d …d d …d | j                  …f   }|d d …d d …d d …| j                  d …f   }t        ||
«      }t        ||
«      }t	        j                  ||gd¬«      }t	        j                  ||gd¬«      }nt        ||
«      }t        ||
«      }|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        j0                  |dkD  t	        j2                  |j                  d
«      j5                  | j6                  «      t	        j2                  |j                  t	        j8                  | j6                  «      j:                  «      j5                  | j6                  «      «      }t=        ||||| j(                  j*                  || j6                  d ¬«      }t	        j>                  d||	«      }| jA                  |«      }| jC                  |«      }| jE                  ||¬«      }|r||f}|S |f}|S )Nr   r2   r   r1   r   rq   rr   rt   )éýÿÿÿr4   g        Údropout)ÚbiasÚdropout_rngÚdropout_raterŒ   rD   Ú	precisionz...hqk,...khd->...qhd©rŒ   )#rZ   r[   r\   rm   r%   Útakerd   ÚsplitrS   r@   Úconcatenater7   ry   Ú	variablesr   Údynamic_slicerc   r~   Úexpand_dimsr	   rC   Ú
attn_pdropÚmake_rngr‹   ÚselectÚfullr!   rD   ÚfinfoÚminr   r    ro   r]   r`   )re   rl   r‚   Úposition_idsrŒ   r�   rŽ   r�   r€   r{   r=   Úk_rotÚk_passÚq_rotÚq_passÚquery_lengthÚ
key_lengthÚ
mask_shiftÚmax_decoder_lengthrc   Ú
batch_sizer“   Úattention_biasÚattn_weightsÚattn_outputÚoutputss                             r-   Ú__call__zFlaxGPTJAttention.__call__Î   sÔ  € ð —‘˜MÓ*ˆØ�k‰k˜-Ó(ˆØ—‘˜MÓ*ˆà×!Ñ! %Ó(ˆØ×Ñ Ó$ˆØ×!Ñ! %Ó(ˆä—‘˜$×.Ñ.°À1ÔEˆÜ—‘˜6 1¨2Ô.ˆØ�?‰?Ð&Øšš1šaÐ!2 4§?¡?Ð!2Ð2Ñ3ˆEØššAšq $§/¡/Ñ"3Ð3Ñ4ˆFàš!šQ¢Ð#4 T§_¡_Ð#4Ð4Ñ5ˆEØš1ša¢ D§O¡OÑ$5Ð5Ñ6ˆFä(¨°Ó7ˆEÜ(¨°Ó7ˆEä—/‘/ 5¨& /¸Ô;ˆCÜ—O‘O U¨F O¸"Ô=‰Eä& s¨FÓ3ˆCÜ(¨°Ó7ˆEà#(§;¡;¨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ˆàˆÙ §¡×!7Ñ!7¸#Ò!=ØŸ-™-¨	Ó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ó
ˆô 5ØØØØ#ØŸ™×/Ñ/Ø'Ø—*‘*Øô	
ˆô —j‘jÐ!8¸,ÈÓNˆØ×'Ñ'¨Ó4ˆØ—m‘m KÓ0ˆØ×(Ñ(¨ÀMÐ(ÓRˆá1B�; Ð-ˆØˆð JUÈˆØˆr/   N)TFF)Ú__name__Ú
__module__Ú__qualname__r   Ú__annotations__r%   r   rD   rE   rL   rF   rh   rm   ro   rT   Úcompactr‹   r±   rw   r/   r-   rB   rB   ‡   sŒ   … ØÓØ—{‘{€Eˆ3�9‰9Ó"Ø€FˆDÓØ$Ð˜Ó$òiò4`òRð ‡Z�Zñ*ó ð*ðJ #Ø Ø"'ñVð
 ðVð ðVð  ôVr/   rB   c                   ól   — e Zd ZU eed<   eed<   ej                  Zej                  ed<   d„ Z	dde
fd„Zy)	ÚFlaxGPTJMLPrC   Úintermediate_sizerD   c                 óü  — | j                   j                  }t        j                  j                  j                  | j                   j                  «      }t        j                  | j                  | j                  |¬«      | _
        t        j                  || j                  |¬«      | _        t        | j                   j                     | _        t        j                  | j                   j                   ¬«      | _        y )N©rD   rI   rJ   )rC   rN   rV   rT   rW   rX   rY   rU   r¹   rD   Úfc_inÚfc_outr   Úactivation_functionÚactr^   r_   r‘   )re   rO   rI   s      r-   rh   zFlaxGPTJMLP.setup,  sœ   € Ø—K‘K×+Ñ+ˆ	Ü—f‘f×)Ñ)×0Ñ0°·±×1NÑ1NÓOˆä—X‘X˜d×4Ñ4¸D¿J¹JÐT_Ô`ˆŒ
Ü—h‘h˜y°·
±
ÈÔTˆŒä˜$Ÿ+™+×9Ñ9Ñ:ˆŒÜ—z‘z t§{¡{×'>Ñ'>Ô?ˆ�r/   rŒ   c                 ó’   — | j                  |«      }| j                  |«      }| j                  |«      }| j                  ||¬«      }|S )Nr–   )r¼   r¿   r½   r‘   )re   rl   rŒ   s      r-   r±   zFlaxGPTJMLP.__call__6  sD   € ØŸ
™
 =Ó1ˆØŸ™ Ó/ˆØŸ™ MÓ2ˆØŸ™ ]À-˜ÓPˆØÐr/   N)T)r²   r³   r´   r   rµ   Úintr%   r   rD   rh   rL   r±   rw   r/   r-   r¸   r¸   '  s2   … ØÓØÓØ—{‘{€Eˆ3�9‰9Ó"ò@ñ°Tô r/   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)
ÚFlaxGPTJBlockrC   rD   c                 ó   — | j                   j                  }| j                   j                  �| j                   j                  nd|z  }t        j                  | j                   j
                  | j                  ¬«      | _        t        | j                   | j                  ¬«      | _	        t        | j                   || j                  ¬«      | _        y )Né   ©ÚepsilonrD   rM   )rC   rN   Ún_innerrT   Ú	LayerNormÚlayer_norm_epsilonrD   Úln_1rB   Úattnr¸   Úmlp)re   rN   Ú	inner_dims      r-   rh   zFlaxGPTJBlock.setupB  s†   € Ø—k‘k×-Ñ-ˆØ+/¯;©;×+>Ñ+>Ð+J�D—K‘K×'Ò'ÐPQÐT_ÑP_ˆ	ä—L‘L¨¯©×)GÑ)GÈtÏzÉzÔZˆŒ	Ü% d§k¡k¸¿¹ÔDˆŒ	ä˜tŸ{™{¨I¸T¿Z¹ZÔHˆ�r/   NrŒ   r�   rŽ   c                 ó¨   — |}| j                  |«      }| j                  ||||||¬«      }|d   }	| j                  ||¬«      }
|	|
z   |z   }|f|dd  z   S )N)r‚   r£   rŒ   r�   rŽ   r   r–   r   )rË   rÌ   rÍ   )re   rl   r‚   r£   rŒ   r�   rŽ   ÚresidualÚattn_outputsr¯   Úfeed_forward_hidden_statess              r-   r±   zFlaxGPTJBlock.__call__K  s   € ð !ˆØŸ	™	 -Ó0ˆØ—y‘yØØ)Ø%Ø'Ø!Ø/ð !ó 
ˆð # 1‘oˆà%)§X¡X¨mÈ= XÓ%YÐ"à#Ð&@Ñ@À8ÑKˆàÐ ,¨q¨rÐ"2Ñ2Ð2r/   )NNTFF©r²   r³   r´   r   rµ   r%   r   rD   rh   rL   r±   rw   r/   r-   rÃ   rÃ   >  sT   … ØÓØ—{‘{€Eˆ3�9‰9Ó"òIð ØØ"Ø Ø"'ñ3ð
 ð3ð ð3ð  ô3r/   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 )ÚFlaxGPTJPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    ÚtransformerNÚmodule_class)r   r   r   TrC   Úinput_shapeÚseedrD   Ú_do_initc                 óZ   •—  | j                   d||dœ|¤Ž}t        ‰| �	  ||||||¬«       y )N)rC   rD   )rØ   rÙ   rD   rÚ   rw   )r×   ÚsuperÚ__init__)	re   rC   rØ   rÙ   rD   rÚ   ÚkwargsÚmoduleÚ	__class__s	           €r-   rÝ   z FlaxGPTJPreTrainedModel.__init__q  s=   ø€ ð #�×"Ñ"ÐH¨&¸ÑHÀÑHˆÜ‰Ñ˜ °[ÀtÐSXÐckÐÕlr/   ÚrngÚparamsÚreturnc           	      ó$  — 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Úi4rM   r1   )râ   r‘   F)Úreturn_dictrâ   )r%   r$   Ú	ones_liker~   r   Ú
atleast_2dr7   rV   Úrandomr˜   rC   Úadd_cross_attentionÚn_embdrß   Úinitr   r   Ú_missing_keysÚsetr   r   )re   rá   rØ   râ   Ú	input_idsr‚   r£   Ú
params_rngr“   ÚrngsÚencoder_hidden_statesÚencoder_attention_maskÚmodule_init_outputsÚrandom_paramsÚmissing_keys                  r-   Úinit_weightsz$FlaxGPTJPreTrainedModel.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à Ð r/   c                 ó†  — t        j                  ||f«      }t        j                  |«      }t        j                  t        j                  t        j
                  |«      j                  d   «      |j                  «      }| j                  j                  t        j                  j                  d«      |||dd¬«      }|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.
        r1   r   FT)ræ   r�   rq   )r%   ra   rç   r~   r   rè   r7   rß   rì   rV   ré   ÚPRNGKey)re   r¬   r…   rï   r‚   r£   Úinit_variabless          r-   r�   z"FlaxGPTJPreTrainedModel.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ð *ó 
ˆð ˜gÑ&Ð&r/   Ú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â   rq   Frå   rM   )rñ   Úmutablerû   r   )rC   rŽ   rý   ræ   r7   Ú
ValueErrorr%   r~   r   ra   râ   rß   Úapplyr&   r   )re   rï   r‚   r£   râ   rû   r“   rü   rŽ   rý   ræ   r¬   Úsequence_lengthrñ   Úinputsrÿ   r°   s                    r-   r±   z FlaxGPTJPreTrainedModel.__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àˆr/   ©N)	NNNNNFNNN)r²   r³   r´   Ú__doc__r   Úconfig_classÚbase_model_prefixr×   rT   ÚModulerµ   r%   r   r   rÁ   rD   rL   rÝ   rV   ré   rù   r   r÷   r�   r   ÚGPTJ_INPUTS_DOCSTRINGÚdictr   r±   Ú__classcell__)rà   s   @r-   rÕ   rÕ   g  sD  ø… ñð
 €LØ%ÐØ"€L�"—)‘)Ó"ð
 $ØØŸ;™;Øñ
màð
mð ð
mð ð	
mð
 �y‰yð
mð õ
mñ!! §
¡
× 2Ñ 2ð !!Àð !!ÐPZð !!Ðfpó !!òF'ñ& +Ð+@ÓAð ØØØ $Ø*.ØØ,0Ø/3Ø&*ñCð
 ðCð ðCð —Z‘Z×'Ñ'ðCð ðCð $ D™>ðCð ' t™nðCð ˜d‘^òCó BôCr/   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)ÚFlaxGPTJBlockCollectionrC   rD   c           	      óÄ   — t        | j                  j                  «      D �cg c]-  }t        | j                  t	        |«      | j
                  ¬«      ‘Œ/ c}| _        y c c}w )N)ÚnamerD   )ÚrangerC   Únum_hidden_layersrÃ   ÚstrrD   Úblocks)re   Úis     r-   rh   zFlaxGPTJBlockCollection.setupþ  sE   € äOTÐUY×U`ÑU`×UrÑUrÓOsö
ØJKŒM˜$Ÿ+™+¬C°«F¸$¿*¹*ÖEò
ˆ�ùò 
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 )Nrw   )r£   rŒ   r�   rŽ   r   r   )r  )re   rl   r‚   r£   rŒ   r�   rŽ   rý   ræ   Úall_attentionsÚall_hidden_statesÚblockÚlayer_outputsr°   s                 r-   r±   z FlaxGPTJBlockCollection.__call__  sŒ   € ñ  1™°dˆÙ"6™B¸DÐà—[‘[ò 	6ˆEÙ#Ø! mÐ%5Ñ5Ð!á!ØØØ)Ø+Ø%Ø"3ôˆMð *¨!Ñ,ˆMâ Ø =°Ñ#3Ð"5Ñ5‘ð	6ð$ !Ð"3°^ÐDˆàˆr/   )NNTFFFTrÓ   rw   r/   r-   r  r  ú  sm   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò
ð ØØ"Ø Ø"'Ø%*Ø ñ"ð
 ð"ð ð"ð  ð"ð #ð"ð ô"r/   r  c            	       óx   — 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	)ÚFlaxGPTJModulerC   rD   c                 óZ  — | j                   j                  | _        t        j                  | j                   j
                  | j                   j                  t        j                  j                  j                  | j                   j                  ¬«      ¬«      | _
        t        j                  | j                   j                  ¬«      | _        t        | j                   | j                  ¬«      | _        t        j"                  | j                   j$                  | j                  ¬«      | _        y )N©Ústddev)Úembedding_initrJ   rM   rÆ   )rC   rN   rO   rT   ÚEmbedÚ
vocab_sizerV   rW   rX   rY   Úwter^   Ú
embd_pdropr‘   r  rD   ÚhrÉ   rÊ   Úln_f©re   s    r-   rh   zFlaxGPTJModule.setup,  s´   € ØŸ™×0Ñ0ˆŒä—8‘8Ø�K‰K×"Ñ"Ø�K‰K×#Ñ#ÜŸ6™6×.Ñ.×5Ñ5¸T¿[¹[×=ZÑ=ZÐ5Ó[ô
ˆŒô
 —z‘z t§{¡{×'=Ñ'=Ô>ˆŒÜ(¨¯©¸D¿J¹JÔGˆŒÜ—L‘L¨¯©×)GÑ)GÈtÏzÉzÔZˆ�	r/   r�   rŽ   rý   ræ   c	           
      óT  — | j                  |j                  d«      «      }	| j                  |	|¬«      }
| 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å   r–   )r£   rŒ   r�   rŽ   rý   ræ   r   r   r   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wr  rw   )Ú.0Úvs     r-   ú	<genexpr>z*FlaxGPTJModule.__call__.<locals>.<genexpr>\  s   è ø€ Ò=˜q¨q©}œÑ=ùs   ‚Šr1   )Úlast_hidden_staterl   Ú
attentions)r"  r!   r‘   r$  r%  r   r   )re   rï   r‚   r£   rŒ   r�   rŽ   rý   ræ   Úinput_embedsrl   r°   r  s                r-   r±   zFlaxGPTJModule.__call__8  sã   € ð —x‘x 	× 0Ñ 0°Ó 6Ó7ˆàŸ™ \À˜ÓOˆà—&‘&ØØØ%Ø'Ø!Ø/Ø!5Ø#ð ó 	
ˆð   ™
ˆØŸ	™	 -Ó0ˆáØ '¨¡
¨mÐ-=Ñ =ÐØ$Ð&7Ð8¸7À1À2¸;ÑF‰Gà$Ð&¨°°¨Ñ4ˆGáÜÑ= GÔ=Ó=Ð=ä"Ø+Ø! !™*Ø˜r‘{ô
ð 	
r/   N©TFFFTrÓ   rw   r/   r-   r  r  (  s^   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò
[ð" Ø Ø"'Ø%*Ø ñ*
ð ð*
ð  ð*
ð #ð*
ð ô*
r/   r  z^The bare GPTJ Model transformer outputting raw hidden-states without any specific head on top.c                   ó   — e Zd ZeZy)ÚFlaxGPTJModelN)r²   r³   r´   r  r×   rw   r/   r-   r1  r1  e  s	   „ ð
 "�Lr/   r1  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
)ÚFlaxGPTJForCausalLMModulerC   rD   c                 ó>  — t        | j                  | j                  ¬«      | _        t	        j
                  | j                  j                  | j                  t        j                  j                  j                  | j                  j                  ¬«      ¬«      | _        y )NrM   r  r»   )r  rC   rD   rÖ   rT   rU   r!  rV   rW   rX   rY   Úlm_headr&  s    r-   rh   zFlaxGPTJForCausalLMModule.setupy  sb   € Ü)¨$¯+©+¸T¿Z¹ZÔHˆÔÜ—x‘xØ�K‰K×"Ñ"Ø—*‘*ÜŸ™×+Ñ+×2Ñ2¸$¿+¹+×:WÑ:WÐ2ÓXô
ˆ�r/   rŒ   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 )	N)rŒ   r�   rŽ   rý   ræ   r   râ   r"  Ú	embeddingÚkernelr   )Úlogitsrl   r-  )
rÖ   rC   Útie_word_embeddingsrš   ÚTr5  r  r   rl   r-  )re   rï   r‚   r£   rŒ   r�   rŽ   rý   ræ   r°   rl   Úshared_kernelÚ	lm_logitss                r-   r±   z"FlaxGPTJForCausalLMModule.__call__�  sÐ   € ð ×"Ñ"ØØØØ'Ø!Ø/Ø!5Ø#ð #ó 	
ˆð   ™
ˆà�;‰;×*Ò*Ø ×,Ñ,×6Ñ6°xÑ@ÀÑGÈÑT×VÑVˆMØŸ™×*Ñ*¨H°xÀÐ6OÐ+PÐR_Ó`‰IàŸ™ ]Ó3ˆIáØ�< '¨!¨" +Ñ-Ð-ä!¨À'×BWÑBWÐdk×dvÑdvÔwÐwr/   Nr/  rÓ   rw   r/   r-   r3  r3  u  sm   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò
ð #Ø Ø"'Ø%*Ø ñ!xð
 ð!xð ð!xð  ð!xð #ð!xð ô!xr/   r3  zJ
    The GPTJ Model transformer with a language modeling head on top.
    c                   ó>   — e Zd ZeZddeej                     fd„Zd„ Z	y)ÚFlaxGPTJForCausalLMNr‚   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   r1   r2   r   )r   r   )rû   r‚   r£   )	r7   r�   r%   ra   Úcumsumr   r}   r~   r   )	re   rï   r…   r‚   r¬   Ú
seq_lengthrû   Úextended_attention_maskr£   s	            r-   Úprepare_inputs_for_generationz1FlaxGPTJForCausalLM.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Ø(ñ
ð 	
r/   c                 óL   — |j                   |d<   |d   d d …dd …f   dz   |d<   |S )Nrû   r£   r1   r   )rû   )re   Úmodel_outputsÚmodel_kwargss      r-   Úupdate_inputs_for_generationz0FlaxGPTJForCausalLM.update_inputs_for_generationÃ  s8   € Ø*7×*GÑ*GˆÐ&Ñ'Ø'3°NÑ'CÂAÀrÁsÀFÑ'KÈaÑ'Oˆ�^Ñ$ØÐr/   r  )
r²   r³   r´   r3  r×   r   rV   ÚArrayrD  rH  rw   r/   r-   r?  r?  ¥  s'   „ ð -€Lñ
ÐS[Ð\_×\eÑ\eÑSfó 
ó*r/   r?  )r?  r1  rÕ   );Ú	functoolsr   Útypingr   r   Ú
flax.linenÚlinenrT   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_gptjr   Ú
get_loggerr²   ÚloggerÚ_CHECKPOINT_FOR_DOCÚ_CONFIG_FOR_DOCÚGPTJ_START_DOCSTRINGr	  r.   r:   r@   r  rB   r¸   rÃ   rÕ   r  r  r1  r3  r?  Ú__all__rw   r/   r-   ú<module>r]     s}  ðõ  ß "å Û 
Ý Û ß >Ñ >ß 6Ý >ß ;Ý ç Lß \Ñ \ß YÑ YÝ *ð 
ˆ×	Ñ	˜HÓ	%€àÐ Ø€ð!Ð ðFÐ òB
òòEô]˜Ÿ	™	ô ]ô@�"—)‘)ô ô.&3�B—I‘Iô &3ôRPÐ1ô Pôf+˜bŸi™iô +ô\:
�R—Y‘Yô :
ñz ØdØóô"Ð+ó "ó	ð"ñ ØØØØô	ô-x §	¡	ô -xñ` ðð ó	ôÐ1ó óðñ< ØØØØô	ò N�r/   