Ë
    S^(hàu  ã                   ó:  — d 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mZmZmZ ddlmZmZmZ ddlmZ ddlmZmZ dd	l
mZ d
dlmZmZmZ d
dlm Z m!Z! d
dl"m#Z#m$Z$m%Z% ddl&m'Z'  e%jP                  e)«      Z*dZ+dZ,dZ-dZ.ej^                  fdej`                  de1deejd                     fd„Z3 G d„ de	jh                  «      Z5 G d„ de	jh                  «      Z6 G d„ de	jh                  «      Z7 G d„ de	jh                  «      Z8 G d „ d!e «      Z9 G d"„ d#e	jh                  «      Z: G d$„ d%e	jh                  «      Z; e#d&e-«       G d'„ d(e9«      «       Z< e!e<e+ee,«        G d)„ d*e	jh                  «      Z= e#d+e-«       G d,„ d-e9«      «       Z> e!e>e+ee,«       g d.¢Z?y)/zFlax BLOOM model.é    N)Úpartial)ÚOptionalÚTuple)Ú
FrozenDictÚfreezeÚunfreeze)Úcombine_masksÚdot_product_attention_weightsÚmake_causal_mask©Útanh)Úflatten_dictÚunflatten_dict)Úlaxé   )ÚFlaxBaseModelOutputÚ-FlaxBaseModelOutputWithPastAndCrossAttentionsÚFlaxCausalLMOutput)ÚFlaxPreTrainedModelÚappend_call_sample_docstring)Úadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚloggingé   )ÚBloomConfigzbigscience/bloomr   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 ([`BloomConfig`]): 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 [`BloomTokenizer`]. 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)
        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.
Úattention_maskÚ	num_headsÚdtypec                 ó¸  — | j                   \  }}dt        j                  t        j                  |«      «      z  }t	        j
                  ddt        j                  |«      dz
   z   z  t        j                  ¬«      }t	        j                  dd|z   t        j                  ¬«      }t        j                  j                  ||«      }||k7  r¹t	        j
                  ddt        j                  d|z  «      dz
   z   z  t        j                  ¬«      }	t        |||z
  «      }
t	        j                  ddd|
z  z   dt        j                  ¬«      }t	        j                  |t        j                  j                  |	|«      gd¬«      }| j                  d¬«      dz
  | z  dd…ddd…f   }|d	   |z  }t	        j                  |d¬«      }t	        j                  ||«      S )
a{  
    Flax implementation of the BLOOM Alibi tensor. BLOOM Alibi tensor is not causal as the original paper mentions, it
    relies on a translation invariance of softmax for quick implementation: with l being a tensor, and a fixed value
    `softmax(l+a) = softmax(l)`. Based on
    https://github.com/ofirpress/attention_with_linear_biases/blob/a35aaca144e0eb6b789dfcb46784c4b8e31b7983/fairseq/models/transformer.py#L742
    Link to paper: https://arxiv.org/abs/2108.12409

    Args:
        attention_mask (`jnp.ndarray`):
            Token-wise attention mask, this should be of shape `(batch_size, max_seq_len)`.
        num_heads (`int`):
            Number of attention heads.
        dtype (`jnp.dtype`, *optional*, defaults to `jnp.float32`):
            The data type (dtype) of the output tensor.

    Returns: Alibi tensor of shape `(batch_size * num_heads, 1, max_seq_len)`.
    é   r   ©r   r   r   ©ÚaxiséÿÿÿÿN).N)ÚshapeÚmathÚfloorÚlog2ÚjnpÚarrayÚfloat32ÚarangeÚjaxr   ÚpowÚminÚcatÚcumsumÚexpand_dimsÚasarray)r   r   r   Ú
batch_sizeÚ
seq_lengthÚclosest_power_of_2ÚbaseÚpowersÚslopesÚ
extra_baseÚnum_remaining_headsÚextra_powersÚarange_tensorÚalibis                 úk/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/bloom/modeling_flax_bloom.pyÚbuild_alibi_tensorr@   o   s‘  € ð$ ,×1Ñ1Ñ€J�
ØœdŸj™j¬¯©°9Ó)=Ó>Ñ>ÐÜ�9‰9�Q˜Q¤D§I¡IÐ.@Ó$AÀAÑ$EÐ"FÑFÐGÑHÔPS×P[ÑP[Ô\€DÜ�Z‰Z˜˜1Ð1Ñ1¼¿¹ÔE€FÜ�W‰W�[‰[˜˜vÓ&€Fà˜YÒ&Ü—Y‘Y˜q q¬d¯i©i¸Ð<NÑ8NÓ.OÐRSÑ.SÐ,TÑ'TÐ%UÑVÔ^a×^iÑ^iÔjˆ
Ü!Ð"4°iÐBTÑ6TÓUÐÜ—z‘z ! Q¨Ð-@Ñ)@Ñ%@À!Ì3Ï;É;ÔWˆÜ—‘˜&¤#§'¡'§+¡+¨j¸,Ó"GÐHÈqÔQˆð %×+Ñ+°Ð+Ó4°qÑ8¸NÑJÊAÈtÒUVÈJÑW€MØ�9Ñ Ñ-€EÜ�O‰O˜E¨Ô*€EÜ�;‰;�u˜eÓ$Ð$ó    c                   ó¢   — e Zd ZU eed<   ej                  Zej                  ed<   d„ Zd„ Z	d„ Z
ej                  d„ «       Z	 	 	 	 dded	ed
efd„Zy)ÚFlaxBloomAttentionÚconfigr   c                 ó  — | j                   j                  | _        | j                   j                  | _        | j                  | j                  z  | _        | j
                  t        j                  u| _        | j                  | j                  z  | j                  k7  r&t        d| j                  › d| j                  › d�«      ‚t        t        j                  | j
                  t        j                  j                  j                  | j                   j                   «      ¬«      } || j                  dz  «      | _         || j                  «      | _        t        j&                  | j                   j(                  ¬«      | _        y )NzC`hidden_size` must be divisible by `num_heads` (got `hidden_size`: z and `num_heads`: z).©r   Úkernel_initr   )Úrate)rD   Úhidden_sizeÚn_headr   Úhead_dimr   r)   r+   Úattention_softmax_in_fp32Ú
ValueErrorr   ÚnnÚDenser-   ÚinitializersÚnormalÚinitializer_rangeÚquery_key_valueÚdenseÚDropoutÚhidden_dropoutÚresid_dropout)ÚselfrT   s     r?   ÚsetupzFlaxBloomAttention.setup�   s!  € ØŸ;™;×2Ñ2ˆÔØŸ™×+Ñ+ˆŒØ×(Ñ(¨D¯N©NÑ:ˆŒØ)-¯©¼3¿;¹;Ð)FˆÔ&à�=‰=˜4Ÿ>™>Ñ)¨T×-=Ñ-=Ò=ÜØUÐVZ×VfÑVfÐUgð h Ø $§¡Ð/¨rð3óð ô
 Ü�H‰HØ—*‘*ÜŸ™×+Ñ+×2Ñ2°4·;±;×3PÑ3PÓQô
ˆñ  % T×%5Ñ%5¸Ñ%9Ó:ˆÔÙ˜4×+Ñ+Ó,ˆŒ
ÜŸZ™Z¨T¯[©[×-GÑ-GÔHˆÕrA   c                 óv   — |j                  |j                  d d | j                  | j                  dz  fz   «      S )Nr$   r   )Úreshaper%   r   rK   ©rX   Úhidden_statess     r?   Ú_split_headszFlaxBloomAttention._split_heads³   s;   € Ø×$Ñ$ ]×%8Ñ%8¸¸"Ð%=ÀÇÁÐQU×Q^ÑQ^ÐabÑQbÐ@cÑ%cÓdÐdrA   c                 óZ   — |j                  |j                  d d | j                  fz   «      S )Nr    )r[   r%   rI   r\   s     r?   Ú_merge_headszFlaxBloomAttention._merge_heads¶   s.   € Ø×$Ñ$ ]×%8Ñ%8¸¸!Ð%<À×@PÑ@PÐ?RÑ%RÓSÐSrA   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   r!   )r)   r*   Úint32© rA   r?   ú<lambda>z:FlaxBloomAttention._concatenate_to_cache.<locals>.<lambda>Å   s   € ÄCÇIÁIÈaÔWZ×W`ÑW`ÔDa€ rA   )r   r   r   )Úhas_variableÚvariabler)   Úzerosr%   r   ÚvalueÚlenr   Údynamic_update_sliceÚbroadcast_tor,   Útupler	   )rX   Úkeyrm   Úqueryr   Úis_initializedrc   rd   re   Ú
batch_dimsÚ
max_lengthr   Údepth_per_headÚ	cur_indexÚindicesÚnum_updated_cache_vectorsÚpad_masks                    r?   Ú_concatenate_to_cachez(FlaxBloomAttention._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˜>Ð)Ð)rA   NÚdeterministicÚ
init_cacheÚoutput_attentionsc           	      óR  — |j                   d d \  }}	| j                  |«      }
| j                  |
«      }
t        j                  |
dd¬«      \  }}}t        |d¬«      }| j                  dd«      r| j                  d   d	   nd
}| j                  dd«      rH| j                  d   d   j                   d   }t        j                  j                  |d
d
|d
fdd|	|f«      }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$                  | j&                  «      j(                  }t        j*                  |d
kD  t        j,                  |j                   d«      j/                  | j&                  «      t        j,                  |j                   |«      j/                  | j&                  «      «      }||z   }| j0                  rt        j2                  n| j&                  }t5        ||||| j                  j                  ||¬«      }| j0                  r|j/                  | j&                  «      }t        j6                  d||«      }| j9                  |«      }| j;                  |«      }| j=                  ||¬«      }||z   }|r||f}|S |f}|S )Nr    r   r$   r"   Úboolr!   rb   rc   re   r   r   )éýÿÿÿéþÿÿÿg        Údropout)ÚbiasÚdropout_rngÚdropout_rater}   r   z...hqk,...khd->...qhd©r}   )r%   rS   r^   r)   Úsplitr   rj   Ú	variablesr-   r   Údynamic_slicerp   r2   r	   rD   Úattention_dropoutÚmake_rngr|   Úfinfor   r/   ÚselectÚfullÚastyperL   r+   r
   Úeinsumr`   rT   rW   )rX   r]   Úresidualr>   r   r}   r~   r   r4   r5   Ú	fused_qkvrs   rr   rm   Úcausal_attention_maskÚcausal_attention_mask_shiftÚmax_decoder_lengthr†   Ú
mask_valueÚattention_biasÚattention_dtypeÚattn_weightsÚattn_outputÚoutputss                           r?   Ú__call__zFlaxBloomAttention.__call__Û   sÿ  € ð "/×!4Ñ!4°R°aÐ!8Ñˆ
�Jð ×(Ñ(¨Ó7ˆ	Ø×%Ñ% iÓ0ˆ	ÜŸI™I i°¸Ô<Ñˆˆs�Eä 0°ÀvÔ NÐð 7;×6GÑ6GÈÐQ]Ô6^ˆD�N‰N˜7Ñ# MÒ2Ðdeð 	$ð
 ×Ñ˜W lÔ3Ø!%§¡°Ñ!8¸Ñ!F×!LÑ!LÈQÑ!OÐÜ$'§G¡G×$9Ñ$9Ø%Ø�AÐ2°AÐ6Ø�A�zÐ#5Ð6ó%Ð!ô !$× 0Ñ 0Ø! J =Ð3H×3NÑ3NÈqÈrÐ3RÑ#Ró!
Ðô ×)Ñ)¬#¯/©/¸.ÈxÔ*XÐZo×ZuÑZuÓvˆÜ& ~Ð7LÓMˆàˆÙ §¡×!>Ñ!>ÀÒ!DØŸ-™-¨	Ó2ˆKð ×Ñ˜W lÔ3±zØ)-×)CÑ)CÀCÈÐPUÐWeÓ)fÑ&ˆC�˜ô —Y‘Y˜tŸz™zÓ*×.Ñ.ˆ
ÜŸ™Ø˜QÑÜ�H‰H�^×)Ñ)¨3Ó/×6Ñ6°t·z±zÓBÜ�H‰H�^×)Ñ)¨:Ó6×=Ñ=¸d¿j¹jÓIó
ˆð (¨%Ñ/ˆð *.×)GÒ)Gœ#Ÿ+š+ÈTÏZÉZˆä4ØØØØ#ØŸ™×6Ñ6Ø'Ø!ô
ˆð ×)Ò)Ø'×.Ñ.¨t¯z©zÓ:ˆLä—j‘jÐ!8¸,ÈÓNˆØ×'Ñ'¨Ó4ˆØ—j‘j Ó-ˆØ×(Ñ(¨ÀMÐ(ÓRˆà! HÑ,ˆá1B�; Ð-ˆØˆð JUÈˆØˆrA   ©NTFF)Ú__name__Ú
__module__Ú__qualname__r   Ú__annotations__r)   r+   r   rY   r^   r`   rN   Úcompactr|   r�   rž   rh   rA   r?   rC   rC   ™   sz   … ØÓØ—{‘{€Eˆ3�9‰9Ó"òIò,eòTð ‡Z�Zñ*ó ð*ðJ Ø"Ø Ø"'ñTð ðTð ðTð  ôTrA   rC   c                   ó   — e Zd Zd„ Zd„ Zy)Ú	BloomGELUc                 ó.   — t         j                  | _        y ©N)r)   r+   r   ©rX   s    r?   rY   zBloomGELU.setup3  s   € Ü—[‘[ˆ�
rA   c                 óH   — |dz  dt        d|z  dd|z  |z  z   z  «      z   z  S )Ng      à?g      ð?g Þe3Eˆé?r   g÷Hmâä¦?r   )rX   Úxs     r?   rž   zBloomGELU.__call__6  s2   € Ø�3‰w˜#¤ Z°!¡^°q¸8Àa¹<È!Ñ;KÑ7KÑ%LÓ MÑMÑNÐNrA   N)r    r¡   r¢   rY   rž   rh   rA   r?   r¦   r¦   2  s   „ ò!óOrA   r¦   c                   ób   — e Zd ZU eed<   ej                  Zej                  ed<   d„ Zdde	fd„Z
y)ÚFlaxBloomMLPrD   r   c                 óÆ  — | j                   j                  }t        j                  j                  j                  | j                   j                  «      }t        j                  d|z  | j                  |¬«      | _	        t        j                  || j                  |¬«      | _
        t        j                  | j                   j                  «      | _        t        «       | _        y )Né   rF   )rD   rI   r-   rN   rP   rQ   rR   rO   r   Údense_h_to_4hÚdense_4h_to_hrU   rV   r¦   Úact)rX   rI   rG   s      r?   rY   zFlaxBloomMLP.setup>  s‘   € Ø—k‘k×-Ñ-ˆä—f‘f×)Ñ)×0Ñ0°·±×1NÑ1NÓOˆäŸX™X a¨+¡o¸T¿Z¹ZÐU`ÔaˆÔÜŸX™X k¸¿¹ÐQ\Ô]ˆÔÜ Ÿj™j¨¯©×)CÑ)CÓDˆÔÜ“;ˆ�rA   r}   c                 óœ   — | j                  |«      }| j                  |«      }| j                  |«      }||z   }| j                  ||¬«      }|S )Nrˆ   )r°   r²   r±   rV   )rX   r]   r“   r}   Úintermediate_outputs        r?   rž   zFlaxBloomMLP.__call__H  sX   € Ø×*Ñ*¨=Ó9ˆØŸ™ Ó/ˆà"×0Ñ0°Ó?Ðà1°HÑ<ÐØ×+Ñ+Ð,?È}Ð+Ó]ˆàÐrA   N)T©r    r¡   r¢   r   r£   r)   r+   r   rY   r�   rž   rh   rA   r?   r­   r­   :  s+   … ØÓØ—{‘{€Eˆ3�9‰9Ó"òñ	¸tô 	rA   r­   c                   ór   — 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)
ÚFlaxBloomBlockrD   r   c                 óð  — t        j                  | j                  j                  | j                  ¬«      | _        t        | j                  | j                  ¬«      | _        t        j                  | j                  j                  | j                  ¬«      | _        t        | j                  | j                  ¬«      | _
        | j                  j                  | _        | j                  j                  | _        y )N©Úepsilonr   r!   )rN   Ú	LayerNormrD   Úlayer_norm_epsilonr   Úinput_layernormrC   Úself_attentionÚpost_attention_layernormr­   ÚmlpÚ(apply_residual_connection_post_layernormrV   r©   s    r?   rY   zFlaxBloomBlock.setupX  s›   € Ü!Ÿ|™|°D·K±K×4RÑ4RÐZ^×ZdÑZdÔeˆÔä0°·±ÀDÇJÁJÔOˆÔÜ(*¯©¸T¿[¹[×=[Ñ=[Ðcg×cmÑcmÔ(nˆÔ%ä §¡°4·:±:Ô>ˆŒà8<¿¹×8lÑ8lˆÔ5Ø"Ÿk™k×8Ñ8ˆÕrA   Nr}   r~   r   c           	      ó  — | j                  |«      }| j                  r|}n|}| j                  |||||||¬«      }	|	d   }
|	dd  }| j                  |
«      }| j                  r|}n|
}| j	                  |||¬«      }|f|z   }|S )N)r“   r>   r   r}   r~   r   r   r   rˆ   )r½   rÁ   r¾   r¿   rÀ   )rX   r]   r>   r   r}   r~   r   Úlayernorm_outputr“   Úattn_outputsÚattention_outputr�   Úpost_layernormÚoutputs                 r?   rž   zFlaxBloomBlock.__call__c  s¹   € ð  ×/Ñ/°Ó>Ðð ×8Ò8Ø'‰Hà$ˆHð ×*Ñ*ØØØØ)Ø'Ø!Ø/ð +ó 
ˆð (¨™?Ðà˜q˜rÐ"ˆà×6Ñ6Ð7GÓHˆð ×8Ò8Ø%‰Hà'ˆHà—‘˜.¨(À-�ÓPˆà�)˜gÑ%ˆàˆrA   rŸ   rµ   rh   rA   r?   r·   r·   T  sP   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò	9ð Ø"Ø Ø"'ñ,ð
 ð,ð ð,ð  ô,rA   r·   c                   ód  ‡ — 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 )ÚFlaxBloomPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    ÚtransformerNÚmodule_class)r   r   r   TrD   Úinput_shapeÚseedr   Ú_do_initc                 óZ   •—  | j                   d||dœ|¤Ž}t        ‰| �	  ||||||¬«       y )N)rD   r   )rÌ   rÍ   r   rÎ   rh   )rË   ÚsuperÚ__init__)	rX   rD   rÌ   rÍ   r   rÎ   ÚkwargsÚmoduleÚ	__class__s	           €r?   rÑ   z!FlaxBloomPreTrainedModel.__init__œ  s=   ø€ ð #�×"Ñ"ÐH¨&¸ÑHÀÑHˆÜ‰Ñ˜ °[ÀtÐSXÐckÐÕlrA   ÚrngÚparamsÚreturnc                 ó¼  — t        j                  |d¬«      }t        j                  |«      }t        j                  j                  |«      \  }}||dœ}| j                  j                  |||d¬«      d   }	|�dt        t        |	«      «      }	t        t        |«      «      }| j                  D ]
  }
|	|
   ||
<   Œ t        «       | _
        t        t        |«      «      S |	S )NÚi4r!   )rÖ   r„   F)Úreturn_dictrÖ   )r)   rl   Ú	ones_liker-   Úrandomr‰   rÓ   Úinitr   r   Ú_missing_keysÚsetr   r   )rX   rÕ   rÌ   rÖ   Ú	input_idsr   Ú
params_rngr†   ÚrngsÚrandom_paramsÚmissing_keys              r?   Úinit_weightsz%FlaxBloomPreTrainedModel.init_weights¨  sÑ   € ä—I‘I˜k°Ô6ˆ	ÜŸ™ yÓ1ˆÜ"%§*¡*×"2Ñ"2°3Ó"7Ñˆ
�KØ$°Ñ=ˆàŸ™×(Ñ(¨¨y¸.ÐV[Ð(Ó\Ð]eÑfˆàÐÜ(¬°-Ó)@ÓAˆMÜ!¤(¨6Ó"2Ó3ˆFØ#×1Ñ1ò A�Ø&3°KÑ&@��{Ò#ðAä!$£ˆDÔÜœ.¨Ó0Ó1Ð1à Ð rA   c                 óô   — t        j                  ||fd¬«      }t        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!   r   FT)rÚ   r~   rb   )	r)   ÚonesrÛ   rÓ   rÝ   r-   rÜ   ÚPRNGKeyr   )rX   r4   rv   rà   r   Úinit_variabless         r?   r~   z#FlaxBloomPreTrainedModel.init_cache»  sm   € ô —H‘H˜j¨*Ð5¸TÔBˆ	ÜŸ™ yÓ1ˆàŸ™×)Ñ)Ü�J‰J×Ñ˜qÓ! 9¨nÈ%Ð\`ð *ó 
ˆô ˜ wÑ/Ó0Ð0rA   Úpast_key_valuesr†   Útrainr   Úoutput_hidden_statesrÚ   c
                 ób  — |�|n| j                   j                  }|�|n| j                   j                  }|	�|	n| j                   j                  }	|j                  \  }
}|€t        j                  |
|f«      }i }|�||d<   d|xs | j                  i}|r	||d<   dg}nd}| j                  j                  |t        j                  |d¬«      t        j                  |d¬«      | d|||	||¬«
      }|�|	r|\  }}t        |d   «      |d<   |S |�"|	s |\  }}|d d	 t        |d   «      fz   |d	d  z   }|S )
Nr„   rÖ   rb   FrÙ   r!   )râ   Úmutablerê   r   )rD   r   rì   Úuse_return_dictr%   r)   rç   rÖ   rÓ   Úapplyr*   r   )rX   rà   r   rê   rÖ   r†   rë   r   rì   rÚ   r4   Úsequence_lengthrâ   Úinputsrî   r�   s                   r?   rž   z!FlaxBloomPreTrainedModel.__call__Í  sz  € ð 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà&/§o¡oÑ#ˆ
�OàÐ!Ü Ÿ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ØØØ ØØØð $ó 
ˆð Ð&©;Ø'.Ñ$ˆG�_Ü)1°/À'Ñ2JÓ)KˆGÐ%Ñ&ØˆNØÐ(±Ø'.Ñ$ˆG�_Ø˜b˜q�k¤X¨o¸gÑ.FÓ%GÐ$IÑIÈGÐTUÐTVÈKÑWˆGàˆrA   r¨   )NNNNFNNN)r    r¡   r¢   Ú__doc__r   Úconfig_classÚbase_model_prefixrË   rN   ÚModuler£   r)   r+   r   Úintr   r�   rÑ   r-   rÜ   rè   r   rå   r~   r   ÚBLOOM_INPUTS_DOCSTRINGÚdictr   rž   Ú__classcell__)rÔ   s   @r?   rÉ   rÉ   ’  s7  ø… ñð
 €LØ%ÐØ"€L�"—)‘)Ó"ð
 $ØØŸ;™;Øñ
màð
mð ð
mð ð	
mð
 �y‰yð
mð õ
mñ! §
¡
× 2Ñ 2ð !Àð !ÐPZð !Ðfpó !ò&1ñ$ +Ð+AÓBð Ø $ØØ*.ØØ,0Ø/3Ø&*ñ=ð ð	=ð
 ð=ð —Z‘Z×'Ñ'ð=ð ð=ð $ D™>ð=ð ' t™nð=ð ˜d‘^ò=ó Cô=rA   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)ÚFlaxBloomBlockCollectionrD   r   c           	      óÄ   — t        | j                  j                  «      D �cg c]-  }t        | j                  t	        |«      | j
                  ¬«      ‘Œ/ c}| _        y c c}w )N)Únamer   )ÚrangerD   Únum_hidden_layersr·   Ústrr   Úlayers)rX   Úlayer_numbers     r?   rY   zFlaxBloomBlockCollection.setup  sH   € ô !& d§k¡k×&CÑ&CÓ Dö
àô ˜4Ÿ;™;¬S°Ó->ÀdÇjÁjÖQò
ˆ�ùò 
s   ¢2ANr}   r~   r   rì   c           	      óØ   — |rdnd }|rdnd }	t        | j                  j                  «      D ]6  }
|r|	|fz  }	 | j                  |
   ||||||¬«      }|d   }|sŒ.||d   fz  }Œ8 ||	|f}|S )Nrh   )r>   r   r}   r~   r   r   r   )rÿ   rD   r   r  )rX   r]   r>   r   r}   r~   r   rì   Úall_attentionsÚall_hidden_statesr  Úlayer_outputsr�   s                r?   rž   z!FlaxBloomBlockCollection.__call__  s¢   € ñ  1™°dˆÙ"6™B¸DÐä! $§+¡+×"?Ñ"?Ó@ò 	6ˆLÙ#Ø! mÐ%5Ñ5Ð!à5˜DŸK™K¨Ñ5ØØØ-Ø+Ø%Ø"3ôˆMð *¨!Ñ,ˆMâ Ø =°Ñ#3Ð"5Ñ5‘ð	6ð$ !Ð"3°^ÐDˆàˆrA   )NTFFFrµ   rh   rA   r?   rü   rü     s]   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò
ð Ø"Ø Ø"'Ø%*ñ!ð
 ð!ð ð!ð  ð!ð #ô!rA   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)ÚFlaxBloomModulerD   r   c                 ór  — | j                   j                  | _        t        j                  | j                   j
                  | j                  t        j                  j                  j                  | j                   j                  ¬«      | j                  ¬«      | _        t        j                  | j                   j                  | j                  ¬«      | _        t        | j                   | j                  ¬«      | _        t        j                  | j                   j                  | j                  ¬«      | _        y )N©Ústddev)Úembedding_initr   r¹   r!   )rD   rI   Ú	embed_dimrN   ÚEmbedÚ
vocab_sizer-   rP   rQ   rR   r   Úword_embeddingsr»   r¼   Úword_embeddings_layernormrü   ÚhÚln_fr©   s    r?   rY   zFlaxBloomModule.setup@  sÆ   € ØŸ™×0Ñ0ˆŒô  "Ÿx™xØ�K‰K×"Ñ"Ø�N‰NÜŸ6™6×.Ñ.×5Ñ5¸T¿[¹[×=ZÑ=ZÐ5Ó[Ø—*‘*ô	 
ˆÔô *,¯©¸d¿k¹k×>\Ñ>\Ðdh×dnÑdnÔ)oˆÔ&ô *¨$¯+©+¸T¿Z¹ZÔHˆŒô —L‘L¨¯©×)GÑ)GÈtÏzÉzÔZˆ�	rA   Nr~   r   rì   rÚ   c           	      ó˜  — | j                  |«      }| j                  |«      }	t        || j                  j                  |	j
                  ¬«      }
| j                  |	|
|||||¬«      }|d   }	| j                  |	«      }	|r|d   |	fz   }|	|f|dd  z   }n	|	f|dd  z   }|st        d„ |d   |d   fD «       «      S t        |	|d   |d   ¬«      S )	Nr!   )r>   r   r}   r~   rì   r   r   r   r    c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wr¨   rh   )Ú.0Úvs     r?   ú	<genexpr>z+FlaxBloomModule.__call__.<locals>.<genexpr>y  s   è ø€ ÒO˜qÀÁœÑOùs   ‚Šr$   )Úlast_hidden_stater]   Ú
attentions)
r  r  r@   rD   rJ   r   r  r  rq   r   )rX   rà   r   r}   r~   r   rì   rÚ   Úinputs_embedsr]   r>   r�   r  s                r?   rž   zFlaxBloomModule.__call__T  s  € ð ×,Ñ,¨YÓ7ˆà×6Ñ6°}ÓEˆô # >°4·;±;×3EÑ3EÈ]×M`ÑM`Ôaˆà—&‘&ØØØ)Ø'Ø!Ø!5Ø/ð ó 
ˆð   ™
ˆØŸ	™	 -Ó0ˆáØ '¨¡
¨mÐ-=Ñ =ÐØ$Ð&7Ð8¸7À1À2¸;ÑF‰Gà$Ð&¨°°¨Ñ4ˆGáÜÑO W¨Q¡Z°¸±Ð$=ÔOÓOÐOä<Ø+Ø! !™*Ø˜r‘{ô
ð 	
rA   )NNTFFFTrµ   rh   rA   r?   r	  r	  <  sd   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò[ð, ØØØ Ø"'Ø%*Ø ñ+
ð
 ð+
ð  ð+
ð #ð+
ð ô+
rA   r	  z_The bare Bloom Model transformer outputting raw hidden-states without any specific head on top.c                   ó   — e Zd ZeZy)ÚFlaxBloomModelN)r    r¡   r¢   r	  rË   rh   rA   r?   r  r  ‚  s	   „ ð #�LrA   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
)ÚFlaxBloomForCausalLMModulerD   r   c                 ó@  — t        | j                  | j                  ¬«      | _        t	        j
                  | j                  j                  d| j                  t        j                  j                  j                  | j                  j                  ¬«      ¬«      | _        y )Nr!   Fr  )Úuse_biasr   rG   )r	  rD   r   rÊ   rN   rO   r  r-   rP   rQ   rR   Úlm_headr©   s    r?   rY   z FlaxBloomForCausalLMModule.setup’  se   € Ü*¨4¯;©;¸d¿j¹jÔIˆÔÜ—x‘xØ�K‰K×"Ñ"ØØ—*‘*ÜŸ™×+Ñ+×2Ñ2¸$¿+¹+×:WÑ:WÐ2ÓXô	
ˆ�rA   r}   r~   r   rì   rÚ   c           	      óx  — | 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Ö   r  Ú	embeddingÚkernelr   )Úlogitsr]   r  )
rÊ   rD   Útie_word_embeddingsrŠ   ÚTr#  rð   r   r]   r  )rX   rà   r   r}   r~   r   rì   rÚ   r�   r]   Úshared_kernelÚ	lm_logitss               r?   rž   z#FlaxBloomForCausalLMModule.__call__›  sÏ   € ð ×"Ñ"ØØ)Ø'Ø!Ø/Ø!5Ø#ð #ó 
ˆð   ™
ˆà�;‰;×*Ò*Ø ×,Ñ,×6Ñ6°xÑ@ÐARÑSÐT_Ñ`×bÑbˆMØŸ™×*Ñ*¨H°xÀÐ6OÐ+PÐR_Ó`‰IàŸ™ ]Ó3ˆIáØ�< '¨!¨" +Ñ-Ð-ä!¨À'×BWÑBWÐdk×dvÑdvÔwÐwrA   N)TFFFTrµ   rh   rA   r?   r   r   Ž  sm   … ØÓØ—{‘{€Eˆ3�9‰9Ó"ò
ð #Ø Ø"'Ø%*Ø ñxð ð	xð
 ðxð  ðxð #ðxð ôxrA   r   zˆ
    The Bloom 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)ÚFlaxBloomForCausalLMNr   c                 ó²   — |j                   \  }}| j                  ||«      }t        j                  ||fd¬«      }|�t	        j
                  ||d«      }||dœS )NrÙ   r!   )r   r   )rê   r   )r%   r~   r)   rç   r   ro   )rX   rà   rv   r   r4   r5   rê   Úextended_attention_masks           r?   Úprepare_inputs_for_generationz2FlaxBloomForCausalLM.prepare_inputs_for_generationÇ  sh   € à!*§¡Ñˆ
�JàŸ/™/¨*°jÓAˆô
 #&§(¡(¨J¸
Ð+CÈ4Ô"PÐØÐ%Ü&)×&>Ñ&>Ð?VÐXfÐhnÓ&oÐ#ð  /Ø5ñ
ð 	
rA   c                 ó$   — |j                   |d<   |S )Nrê   )rê   )rX   Úmodel_outputsÚmodel_kwargss      r?   Úupdate_inputs_for_generationz1FlaxBloomForCausalLM.update_inputs_for_generationÙ  s   € Ø*7×*GÑ*GˆÐ&Ñ'ØÐrA   r¨   )
r    r¡   r¢   r   rË   r   r-   ÚArrayr0  r4  rh   rA   r?   r-  r-  ½  s'   „ ð .€Lñ
ÐS[Ð\_×\eÑ\eÑSfó 
ó$rA   r-  )r-  r  rÉ   )@ró   r&   Ú	functoolsr   Útypingr   r   Ú
flax.linenÚlinenrN   r-   Ú	jax.numpyÚnumpyr)   Úflax.core.frozen_dictr   r   r   r	   r
   r   Úflax.linen.activationr   Úflax.traverse_utilr   r   r   Úmodeling_flax_outputsr   r   r   Úmodeling_flax_utilsr   r   Úutilsr   r   r   Úconfiguration_bloomr   Ú
get_loggerr    ÚloggerÚ_CHECKPOINT_FOR_DOCÚ_CONFIG_FOR_DOCÚBLOOM_START_DOCSTRINGrø   r+   Úndarrayr÷   r   r@   rö   rC   r¦   r­   r·   rÉ   rü   r	  r  r   r-  Ú__all__rh   rA   r?   ú<module>rJ     sÀ  ðñ ã Ý ß "å Û 
Ý ß >Ñ >ß UÑ UÝ &ß ;Ý ÷ñ ÷
 Uß YÑ YÝ ,ð 
ˆ×	Ñ	˜HÓ	%€à(Ð Ø€ð!Ð ðFÐ ð< be×alÑalñ '% s§{¡{ð '%¸sð '%È8ÐTW×T]ÑT]ÑK^ó '%ôTV˜Ÿ™ô VôrO�—	‘	ô Oô�2—9‘9ô ô4;�R—Y‘Yô ;ô|yÐ2ô yôx+˜rŸy™yô +ô\C
�b—i‘iô C
ñL ØeØóô
#Ð-ó #óð
#ñ ˜^Ð-@ÐBUÐWfÔ gô,x §¡ô ,xñ^ ðð óôÐ3ó óðñ4 Ð1Ð3FÐHZÐ\kÔ lò Q�rA   