Ë
    T^(h
 ã                   ó¬  — d dl Z d dlmZ d dlmZmZmZmZmZ d dl	Z	d dl
mc 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 ddlmZ dd	lmZ dd
lmZmZmZmZmZ ddlm Z  ddl!m"Z"m#Z#m$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z*m+Z+  e%«       rd dl,m-Z- d dl.m/Z/ d dl0m1Z1 ne2Z/dZ3dZ4 e&jj                  e6«      Z7 G d„ de«      Z8	 	 dTde	jr                  de	jr                  dee	jr                     dee	jr                     dee	jr                  e	jr                  e	jr                  e:ee	jr                     ee	jr                     f   f
d„Z;de	jr                  de	jr                  de:de:de	jr                  f
d„Z< G d „ d!e	jz                  j|                  «      Z?	 	 dTd"ee	jr                     d#ee:   fd$„Z@ G d%„ d&e/«      ZA G d'„ d(ej„                  «      ZC G d)„ d*ej„                  «      ZD G d+„ d,e*«      ZE	 dUd-d.d/e	jr                  de	jr                  d0e	jr                  dee	jŒ                     d1ee:e:f   d2e:d3e:d4eeG   deee	jr                  e	jr                  f   ee	jr                     f   fd5„ZHe	j’                  fd-d.d/e	jr                  d6eAd"e	jr                  d#e:d1ee:e:f   d2e:d3e:d7e	j”                  dee	jr                     fd8„ZKd-d.d/e	jr                  de	jr                  d0e	jr                  dee	jŒ                     d1ee:e:f   d2e:d3e:dee	jr                     fd9„ZLeKeHeLd:œZM G d;„ d.ej„                  «      ZN G d<„ d=ej„                  «      ZOd>ZP e#d?eP«       G d@„ dAe «      «       ZQdBZR e#d?eP«       G dC„ dDeQ«      «       ZS G dE„ dFej„                  «      ZT e#dGeP«       G dH„ dIeQ«      «       ZU e#dJeP«       G dK„ dLeQ«      «       ZV e#dMeP«       G dN„ dOeQ«      «       ZW e#dPeP«       G dQ„ dReQ«      «       ZXg dS¢ZYy)Vé    N)Únullcontext)ÚDictÚLiteralÚOptionalÚTupleÚUnion)Únn)ÚBCEWithLogitsLossÚCrossEntropyLossÚMSELossé   )ÚACT2FN)ÚPretrainedConfig)Ú_prepare_4d_attention_mask)ÚBaseModelOutputÚMaskedLMOutputÚQuestionAnsweringModelOutputÚSequenceClassifierOutputÚTokenClassifierOutput)ÚPreTrainedModel)Úadd_code_sample_docstringsÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚis_flash_attn_2_availableÚlogging)Úis_triton_availableé   )ÚGemmaRotaryEmbeddingÚapply_rotary_pos_emb)Ú flash_attn_varlen_qkvpacked_func)ÚRotaryEmbedding)Úapply_rotaryzanswerdotai/ModernBERT-baseÚModernBertConfigc                   óŠ   ‡ — e Zd ZdZdZdgZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dded   fˆ fd„Zˆ fd„Zˆ xZ	S )	r#   a÷  
    This is the configuration class to store the configuration of a [`ModernBertModel`]. It is used to instantiate an ModernBert
    model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
    defaults will yield a similar configuration to that of the ModernBERT-base.
    e.g. [answerdotai/ModernBERT-base](https://huggingface.co/answerdotai/ModernBERT-base)

    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
    documentation from [`PretrainedConfig`] for more information.

    Args:
        vocab_size (`int`, *optional*, defaults to 50368):
            Vocabulary size of the ModernBert model. Defines the number of different tokens that can be represented by the
            `inputs_ids` passed when calling [`ModernBertModel`]
        hidden_size (`int`, *optional*, defaults to 768):
            Dimension of the hidden representations.
        intermediate_size (`int`, *optional*, defaults to 1152):
            Dimension of the MLP representations.
        num_hidden_layers (`int`, *optional*, defaults to 22):
            Number of hidden layers in the Transformer decoder.
        num_attention_heads (`int`, *optional*, defaults to 12):
            Number of attention heads for each attention layer in the Transformer decoder.
        hidden_activation (`str` or `function`, *optional*, defaults to `"gelu"`):
            The non-linear activation function (function or string) in the decoder. Will default to `"gelu"`
            if not specified.
        max_position_embeddings (`int`, *optional*, defaults to 8192):
            The maximum sequence length that this model might ever be used with.
        initializer_range (`float`, *optional*, defaults to 0.02):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        initializer_cutoff_factor (`float`, *optional*, defaults to 2.0):
            The cutoff factor for the truncated_normal_initializer for initializing all weight matrices.
        norm_eps (`float`, *optional*, defaults to 1e-05):
            The epsilon used by the rms normalization layers.
        norm_bias (`bool`, *optional*, defaults to `False`):
            Whether to use bias in the normalization layers.
        pad_token_id (`int`, *optional*, defaults to 50283):
            Padding token id.
        eos_token_id (`int`, *optional*, defaults to 50282):
            End of stream token id.
        bos_token_id (`int`, *optional*, defaults to 50281):
            Beginning of stream token id.
        cls_token_id (`int`, *optional*, defaults to 50281):
            Classification token id.
        sep_token_id (`int`, *optional*, defaults to 50282):
            Separation token id.
        global_rope_theta (`float`, *optional*, defaults to 160000.0):
            The base period of the global RoPE embeddings.
        attention_bias (`bool`, *optional*, defaults to `False`):
            Whether to use a bias in the query, key, value and output projection layers during self-attention.
        attention_dropout (`float`, *optional*, defaults to 0.0):
            The dropout ratio for the attention probabilities.
        global_attn_every_n_layers (`int`, *optional*, defaults to 3):
            The number of layers between global attention layers.
        local_attention (`int`, *optional*, defaults to 128):
            The window size for local attention.
        local_rope_theta (`float`, *optional*, defaults to 10000.0):
            The base period of the local RoPE embeddings.
        embedding_dropout (`float`, *optional*, defaults to 0.0):
            The dropout ratio for the embeddings.
        mlp_bias (`bool`, *optional*, defaults to `False`):
            Whether to use bias in the MLP layers.
        mlp_dropout (`float`, *optional*, defaults to 0.0):
            The dropout ratio for the MLP layers.
        decoder_bias (`bool`, *optional*, defaults to `True`):
            Whether to use bias in the decoder layers.
        classifier_pooling (`str`, *optional*, defaults to `"cls"`):
            The pooling method for the classifier. Should be either `"cls"` or `"mean"`. In local attention layers, the
            CLS token doesn't attend to all tokens on long sequences.
        classifier_dropout (`float`, *optional*, defaults to 0.0):
            The dropout ratio for the classifier.
        classifier_bias (`bool`, *optional*, defaults to `False`):
            Whether to use bias in the classifier.
        classifier_activation (`str`, *optional*, defaults to `"gelu"`):
            The activation function for the classifier.
        deterministic_flash_attn (`bool`, *optional*, defaults to `False`):
            Whether to use deterministic flash attention. If `False`, inference will be faster but not deterministic.
        sparse_prediction (`bool`, *optional*, defaults to `False`):
            Whether to use sparse prediction for the masked language model instead of returning the full dense logits.
        sparse_pred_ignore_index (`int`, *optional*, defaults to -100):
            The index to ignore for the sparse prediction.
        reference_compile (`bool`, *optional*):
            Whether to compile the layers of the model which were compiled during pretraining. If `None`, then parts of
            the model will be compiled if 1) `triton` is installed, 2) the model is not on MPS, 3) the model is not
            shared between devices, and 4) the model is not resized after initialization. If `True`, then the model may
            be faster in some scenarios.
        repad_logits_with_grad (`bool`, *optional*, defaults to `False`):
            When True, ModernBertForMaskedLM keeps track of the logits' gradient when repadding for output. This only
            applies when using Flash Attention 2 with passed labels. Otherwise output logits always have a gradient.

    Examples:

    ```python
    >>> from transformers import ModernBertModel, ModernBertConfig

    >>> # Initializing a ModernBert style configuration
    >>> configuration = ModernBertConfig()

    >>> # Initializing a model from the modernbert-base style configuration
    >>> model = ModernBertModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```Ú
modernbertÚpast_key_valuesÚclassifier_pooling©ÚclsÚmeanc$           	      ó"  •— t        ‰%| �  d|||||dœ|$¤Ž || _        || _        || _        || _        || _        || _        || _        |	| _	        |
| _
        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        | | _        |!| _        |"| _        |#| _        | j.                  dvrtA        d| j.                  › d�«      ‚y )N)Úpad_token_idÚbos_token_idÚeos_token_idÚcls_token_idÚsep_token_idr(   zQInvalid value for `classifier_pooling`, should be either "cls" or "mean", but is ú.© )!ÚsuperÚ__init__Ú
vocab_sizeÚmax_position_embeddingsÚhidden_sizeÚintermediate_sizeÚnum_hidden_layersÚnum_attention_headsÚinitializer_rangeÚinitializer_cutoff_factorÚnorm_epsÚ	norm_biasÚglobal_rope_thetaÚattention_biasÚattention_dropoutÚhidden_activationÚglobal_attn_every_n_layersÚlocal_attentionÚlocal_rope_thetaÚembedding_dropoutÚmlp_biasÚmlp_dropoutÚdecoder_biasr'   Úclassifier_dropoutÚclassifier_biasÚclassifier_activationÚdeterministic_flash_attnÚsparse_predictionÚsparse_pred_ignore_indexÚreference_compileÚrepad_logits_with_gradÚ
ValueError)&Úselfr5   r7   r8   r9   r:   rB   r6   r;   r<   r=   r>   r,   r.   r-   r/   r0   r?   r@   rA   rC   rD   rE   rF   rG   rH   rI   r'   rJ   rK   rL   rM   rN   rO   rP   rQ   ÚkwargsÚ	__class__s&                                        €úo/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/modernbert/modular_modernbert.pyr4   zModernBertConfig.__init__¨   sT  ø€ ôN 	‰Ñð 	
Ø%Ø%Ø%Ø%Ø%ñ	
ð ò	
ð %ˆŒØ'>ˆÔ$Ø&ˆÔØ!2ˆÔØ!2ˆÔØ#6ˆÔ Ø!2ˆÔØ)BˆÔ&Ø ˆŒØ"ˆŒØ!2ˆÔØ,ˆÔØ!2ˆÔØ!2ˆÔØ*DˆÔ'Ø.ˆÔØ 0ˆÔØ!2ˆÔØ ˆŒØ&ˆÔØ(ˆÔØ"4ˆÔØ"4ˆÔØ.ˆÔØ%:ˆÔ"Ø(@ˆÔ%Ø!2ˆÔØ(@ˆÔ%Ø!2ˆÔØ&<ˆÔ#à×"Ñ"¨/Ñ9ÜØcÐdh×d{Ñd{Ðc|Ð|}Ð~óð ð :ó    c                 óH   •— t         ‰| �  «       }|j                  dd «       |S )NrP   )r3   Úto_dictÚpop)rS   ÚoutputrU   s     €rV   rY   zModernBertConfig.to_dictû   s#   ø€ Ü‘‘Ó"ˆØ�
‰
Ð&¨Ô-ØˆrW   )#iÀÄ  i   i€  é   é   Úgelui    g{®Gáz”?ç       @gñhãˆµøä>FikÄ  éjÄ  éiÄ  ra   r`   g     ˆAFç        r   é€   ç     ˆÃ@rb   Frb   Tr)   rb   Fr^   FFiœÿÿÿNF)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
model_typeÚkeys_to_ignore_at_inferencer   r4   rY   Ú__classcell__©rU   s   @rV   r#   r#   =   s§   ø„ ñeðN €JØ#4Ð"5Ðð ØØØØØ Ø $ØØ"%ØØØØØØØØ"ØØØ#$ØØ ØØØØØ5:ØØØ$Ø!&ØØ!%ØØ$ñIQð8 $ MÑ2õ9Q÷fð rW   ÚinputsÚattention_maskÚposition_idsÚlabelsÚreturnc                 ó¢  — |j                  dt        j                  ¬«      }t        j                  |j	                  «       d¬«      j	                  «       }t        |j                  «       j                  «       «      }t        j                  j                  j                  t        j                  |dt        j                  ¬«      d«      }| j                  «       dk(  r| j	                  «       |   }n*| j                  ^}	}
}|	|
z  } | j                  |g|¢­Ž |   }|�|j	                  «       |   nd}|�|j	                  «       |   nd}||||||fS )	aˆ  
    Remove padding from input sequences.

    Args:
        inputs: (batch, seqlen, ...) or (batch, seqlen)
        attention_mask: (batch, seqlen), bool / int, 1 means valid and 0 means not valid.
        position_ids: (batch, seqlen), int, position ids
        labels: (batch, seqlen), int, labels

    Returns:
        unpadded_inputs: (total_nnz, ...), where total_nnz = number of tokens selected in attention_mask.
        indices: (total_nnz)
        cu_seqlens: (batch + 1), the cumulative sequence lengths
        max_seqlen_in_batch: int
        unpadded_position_ids: (total_nnz) or None
        unpadded_labels: (total_nnz) or None
    éÿÿÿÿ©ÚdimÚdtypeF)Úas_tupler   )é   r   r   N)ÚsumÚtorchÚint32ÚnonzeroÚflattenÚintÚmaxÚitemr	   Ú
functionalÚpadÚcumsumru   ÚshapeÚview)rm   rn   ro   rp   Úseqlens_in_batchÚindicesÚmax_seqlen_in_batchÚ
cu_seqlensÚunpadded_inputsÚbatchÚseqlenÚrestr„   Úunpadded_position_idsÚunpadded_labelss                  rV   Ú_unpad_modernbert_inputr�     s,  € ð. &×)Ñ)¨b¼¿¹Ð)ÓDÐÜ�m‰m˜N×2Ñ2Ó4¸uÔE×MÑMÓO€GÜÐ.×2Ñ2Ó4×9Ñ9Ó;Ó<ÐÜ—‘×$Ñ$×(Ñ(¬¯©Ð6FÈAÔUZ×U`ÑU`Ô)aÐciÓj€Jà‡z�zƒ|�qÒØ Ÿ.™.Ó*¨7Ñ3‰à%Ÿ|™|Ðˆˆv˜Ø˜‘ˆØ%˜&Ÿ+™+ eÐ3¨dÒ3°GÑ<ˆà?KÐ?W˜L×0Ñ0Ó2°7Ò;Ð]aÐØ39Ð3E�f—n‘nÓ& wÒ/È4€Oà˜G ZÐ1DÐF[Ð]lÐlÐlrW   r‡   r‹   rŒ   c                 ól  — | j                  «       dk(  rHt        j                  ||z  | j                  | j                  ¬«      }| ||<   |j                  ||«      }|S | j                  ^}}t        j                  ||z  g|¢­| j                  | j                  dœŽ}| ||<    |j
                  ||g|¢­Ž }|S )aQ  
    Add padding to sequences.

    Args:
        inputs: (total_nnz, ...) or (total_nnz,), where total_nnz = number of tokens selected in attention_mask.
        indices: (total_nnz)
        batch: int, batch size
        seqlen: int, max sequence length

    Returns:
        padded_inputs: (batch, seqlen, ...) or (batch, seqlen)
    rx   )rv   Údevice)ru   rz   Úzerosrv   r’   r…   r„   )rm   r‡   r‹   rŒ   r[   Úpadded_inputsÚ_r�   s           rV   Ú_pad_modernbert_outputr–   *  s¬   € ð$ ‡z�zƒ|�qÒÜ—‘˜U V™^°6·<±<ÈÏÉÔVˆØ ˆˆw‰ØŸ™ E¨6Ó2ˆð Ðð —<‘<ˆˆˆDÜ—‘˜U V™^Ð]¨dÑ]¸&¿,¹,ÈvÏ}É}Ò]ˆØ ˆˆw‰Ø#˜Ÿ™ E¨6Ð9°DÒ9ˆàÐrW   c                   ó\   — e Zd Ze	 	 ddeej                     dee   fd„«       Zed„ «       Z	y)ÚApplyRotaryEmbUnpadNr‰   Ú
max_seqlenc           
      óÚ   — |j                  «       }|j                  \  }}}}	|d d …d d…f   j                  |d|	«      }
t        |
||d||dd¬«       | j	                  |||«       || _        |S )Nr   rs   r   FT)Úseqlen_offsetsr‰   r™   ÚinterleavedÚinplace)Ú
contiguousr„   r…   r"   Úsave_for_backwardr™   )ÚctxÚqkvÚcosÚsinr‰   r™   Ú	total_nnzÚ_threeÚ_nheadsÚheaddimÚqks              rV   ÚforwardzApplyRotaryEmbUnpad.forwardJ  s‚   € ð �n‰nÓˆØ.1¯i©iÑ+ˆ	�6˜7 Gð ’�B�Q�B�‰Z�_‰_˜Y¨¨GÓ4ˆÜØØØØØ!Ø!ØØõ		
ð 	×Ñ˜c 3¨
Ô3Ø#ˆŒØˆ
rW   c                 óê   — | j                   \  }}}|j                  «       }|j                  \  }}}}|d d …d d…f   j                  |d|«      }	t	        |	||d|| j
                  ddd¬«	       |d d d d d d fS )Nr   rs   r   FT)r›   r‰   r™   rœ   r�   Ú	conjugate)Úsaved_tensorsrž   r„   r…   r"   r™   )
r    Údor¢   r£   r‰   r¤   r¥   r¦   r§   Údqks
             rV   ÚbackwardzApplyRotaryEmbUnpad.backwardi  s�   € à"×0Ñ0ÑˆˆS�*Ø�]‰]‹_ˆØ.0¯h©hÑ+ˆ	�6˜7 Gð ’�B�Q�B�‰i�n‰n˜Y¨¨GÓ4ˆÜØØØØØ!Ø—~‘~ØØØõ
	
ð �4˜˜t T¨4°Ð5Ð5rW   ©NN)
re   rf   rg   Ústaticmethodr   rz   ÚTensorr~   r©   r¯   r2   rW   rV   r˜   r˜   I  sQ   „ Øð .2Ø$(ñð
 ˜UŸ\™\Ñ*ðð ˜S‘Mòó ðð< ñ6ó ñ6rW   r˜   r‰   r™   c                 ó4   — t         j                  | ||||«      S )aÅ  
    Arguments:
        qkv: (total_nnz, 3, nheads, headdim) - input tensor for packed QKV.
        cos, sin: (seqlen_rotary, rotary_dim / 2)
        interleaved: if True, rotate pairs of even and odd dimensions (GPT-J style) instead
            of 1st half and 2nd half (GPT-NeoX style).
        inplace: if True, apply rotary embedding in-place.
        seqlen_offsets: (batch_size,) or int. Each sequence in x is shifted by this amount.
            Most commonly used in inference when we have KV cache.
        cu_seqlens: (batch + 1,) or None
        max_seqlen: int
    Return:
        out: (total_nnz, dim)
    rotary_dim must be <= headdim
    Apply rotary embedding to the first rotary_dim of x.
    )r˜   Úapply)r¡   r¢   r£   r‰   r™   s        rV   Úapply_rotary_unpaddedrµ   €  s   € ô. ×$Ñ$ S¨#¨s°JÀ
ÓKÐKrW   c                   ó"  ‡ — e Zd ZdZ	 	 	 	 ddededee   deej                     deej                     f
ˆ fd„Z
	 ddej                  d	ej                  dee   d
eej                  eej                  ej                  f   f   fd„Zd
efd„Zˆ xZS )Ú!ModernBertUnpaddedRotaryEmbeddingzP
    The rotary position embeddings applied directly to unpadded sequences.
    ru   Úbaser™   r’   rv   c                 óv   •— t         ‰| �  ||d|d¬«       || _        |�|�|�| j                  |||¬«       yyyy)a  
        max_seqlen: if max_seqlen, device, and dtype are provided, we precompute the cos_sin_cache
            up to max_seqlen. If the max_seqlen, device, or dtype during training/inference differ,
            the cos_sin_cache wll be recomputed during the forward pass.
        TF)ru   r¸   Úpos_idx_in_fp32r’   rœ   N©r’   rv   )r3   r4   r™   Ú_update_cos_sin_cache)rS   ru   r¸   r™   r’   rv   rU   s         €rV   r4   z*ModernBertUnpaddedRotaryEmbedding.__init__Ÿ  sV   ø€ ô 	‰Ñ˜S t¸TÈ&Ð^cÐÔdØ$ˆŒàÐ! fÐ&8¸UÐ=NØ×&Ñ& z¸&ÈÐ&ÕNð >OÐ&8Ð!rW   r¡   r‰   rq   c                 ó¢   — |�(| j                  ||j                  |j                  ¬«       t        || j                  | j
                  ||¬«      }|S )zØ
        Apply rotary embedding *inplace* to qkv.
        qkv: (total_nnz, 3, nheads, headdim)
        cu_seqlens: (batch + 1,) cumulative sequence lengths
        max_seqlen: int max seq length in the batch
        r»   ©r‰   r™   )r¼   r’   rv   rµ   Ú_cos_cachedÚ_sin_cached)rS   r¡   r‰   r™   s       rV   r©   z)ModernBertUnpaddedRotaryEmbedding.forward²  sS   € ð Ð!Ø×&Ñ& z¸#¿*¹*ÈCÏIÉIÐ&ÔVä#ØØ×ÑØ×ÑØ!Ø!ô
ˆð ˆ
rW   c                 óT   — d| j                   › d| j                  › d| j                  › �S )Nzdim=z, base=z, scale_base=)ru   r¸   Ú
scale_base©rS   s    rV   Ú
extra_reprz,ModernBertUnpaddedRotaryEmbedding.extra_reprË  s(   € Ø�d—h‘h�Z˜w t§y¡y k°¸t¿¹Ð>OÐPÐPrW   )rd   NNN©N)re   rf   rg   rh   r~   Úfloatr   rz   r’   rv   r4   r²   r   r   r©   ÚstrrÄ   rk   rl   s   @rV   r·   r·   š  sÑ   ø„ ñð Ø$(Ø)-Ø'+ñOàðOð ðOð ˜S‘Mð	Oð
 ˜Ÿ™Ñ&ðOð ˜Ÿ™Ñ$õOð. %)ñ	à�\‰\ðð —L‘Lðð ˜S‘Mð	ð
 
ˆu�|‰|˜U 5§<¡<°·±Ð#=Ñ>Ð>Ñ	?óð2Q˜C÷ QrW   r·   c                   óì   ‡ — e Zd ZdZdefˆ fd„Z ej                  d¬«      dej                  dej                  fd„«       Z
	 ddeej                     d	eej                     dej                  fd
„Zˆ xZS )ÚModernBertEmbeddingszV
    Same as BertEmbeddings with a tiny tweak for positional embeddings indexing.
    Úconfigc                 ód  •— t         ‰| �  «        || _        t        j                  |j
                  |j                  |j                  ¬«      | _        t        j                  |j                  |j                  |j                  ¬«      | _        t        j                  |j                  «      | _        y )N)Úpadding_idx©ÚepsÚbias)r3   r4   rÊ   r	   Ú	Embeddingr5   r7   r,   Útok_embeddingsÚ	LayerNormr=   r>   ÚnormÚDropoutrF   Údrop©rS   rÊ   rU   s     €rV   r4   zModernBertEmbeddings.__init__Ô  sw   ø€ Ü‰ÑÔØˆŒÜ Ÿl™l¨6×+<Ñ+<¸f×>PÑ>PÐ^d×^qÑ^qÔrˆÔÜ—L‘L ×!3Ñ!3¸¿¹Èv×O_ÑO_Ô`ˆŒ	Ü—J‘J˜v×7Ñ7Ó8ˆ�	rW   T©ÚdynamicÚ	input_idsrq   c                 ó`   — | j                  | j                  | j                  |«      «      «      S rÅ   )rÕ   rÓ   rÑ   )rS   rÙ   s     rV   Úcompiled_embeddingsz(ModernBertEmbeddings.compiled_embeddingsÛ  s%   € à�y‰y˜Ÿ™ 4×#6Ñ#6°yÓ#AÓBÓCÐCrW   Úinputs_embedsc                 óú   — |�"| j                  | j                  |«      «      }|S | j                  j                  r| j	                  |«      n.| j                  | j                  | j                  |«      «      «      }|S rÅ   )rÕ   rÓ   rÊ   rP   rÛ   rÑ   )rS   rÙ   rÜ   Úhidden_statess       rV   r©   zModernBertEmbeddings.forwardß  su   € ð Ð$Ø ŸI™I d§i¡i°Ó&>Ó?ˆMð Ðð —;‘;×0Ò0ð ×(Ñ(¨Ô3à—Y‘Y˜tŸy™y¨×)<Ñ)<¸YÓ)GÓHÓIð ð
 ÐrW   r°   )re   rf   rg   rh   r#   r4   rz   ÚcompileÚ
LongTensorr²   rÛ   r   r©   rk   rl   s   @rV   rÉ   rÉ   Ï  s�   ø„ ñð9Ð/õ 9ð €U‡]�]˜4Ô ðD¨U×-=Ñ-=ð DÀ%Ç,Á,ò Dó !ðDð eiñØ! %×"2Ñ"2Ñ3ðØKSÐTY×T`ÑT`ÑKaðà	�‰÷rW   rÉ   c                   ó`   ‡ — e Zd ZdZdefˆ fd„Zdej                  dej                  fd„Zˆ xZ	S )ÚModernBertMLPa6  Applies the GLU at the end of each ModernBERT layer.

    Compared to the default BERT architecture, this block replaces :class:`~transformers.model.bert.modeling_bert.BertIntermediate`
    and :class:`~transformers.model.bert.modeling_bert.SelfOutput` with a single module that has similar functionality.
    rÊ   c                 ó¬  •— t         ‰| �  «        || _        t        j                  |j
                  t        |j                  «      dz  |j                  ¬«      | _	        t        |j                     | _        t        j                  |j                  «      | _        t        j                  |j                  |j
                  |j                  ¬«      | _        y )Nr   ©rÏ   )r3   r4   rÊ   r	   ÚLinearr7   r~   r8   rG   ÚWir   rB   ÚactrÔ   rH   rÕ   ÚWorÖ   s     €rV   r4   zModernBertMLP.__init__ô  s“   ø€ Ü‰ÑÔØˆŒÜ—)‘)˜F×.Ñ.´°F×4LÑ4LÓ0MÐPQÑ0QÐX^×XgÑXgÔhˆŒÜ˜&×2Ñ2Ñ3ˆŒÜ—J‘J˜v×1Ñ1Ó2ˆŒ	Ü—)‘)˜F×4Ñ4°f×6HÑ6HÈvÏÉÔ_ˆ�rW   rÞ   rq   c                 ó°   — | j                  |«      j                  dd¬«      \  }}| j                  | j                  | j	                  |«      |z  «      «      S )Nr   rs   ©ru   )ræ   Úchunkrè   rÕ   rç   )rS   rÞ   ÚinputÚgates       rV   r©   zModernBertMLP.forwardü  sI   € Ø—g‘g˜mÓ,×2Ñ2°1¸"Ð2Ó=‰ˆˆtØ�w‰w�t—y‘y §¡¨%£°4Ñ!7Ó8Ó9Ð9rW   )
re   rf   rg   rh   r#   r4   rz   r²   r©   rk   rl   s   @rV   râ   râ   í  s2   ø„ ñð`Ð/õ `ð: U§\¡\ð :°e·l±l÷ :rW   râ   c            
       óL   ‡ — e Zd Zddedededeej                     fˆ fd„Z	ˆ xZ
S )ÚModernBertRotaryEmbeddingrÊ   ru   r¸   r’   c                 ód   •— t         ‰| �  | ||¬«       | j                  d |||¬«      \  }| _        y )N)rÊ   r’   )ru   r¸   )r3   r4   Úrope_init_fnÚattention_scaling)rS   rÊ   ru   r¸   r’   Úinv_freqrU   s         €rV   r4   z"ModernBertRotaryEmbedding.__init__  s9   ø€ Ü‰Ñ˜ f°VÐÔ<Ø+/×+<Ñ+<¸TÀ6ÈsÐY]Ð+<Ó+^Ñ(ˆ�$Õ(rW   rÅ   )re   rf   rg   r#   r~   rÆ   r   rz   r’   r4   rk   rl   s   @rV   rï   rï     s=   ø„ ñ_Ð/ð _°cð _Àð _ÐPXÐY^×YeÑYeÑPf÷ _ñ _rW   rï   ÚmoduleÚModernBertAttentionr¡   Úsliding_window_maskrD   Úbsru   Úoutput_attentionsc	                 óÆ  — | j                  ||¬«      \  }
}|j                  dd«      j                  d¬«      \  }}}t        |||
|«      \  }}| j                  dz  }t        j                  ||j                  dd«      «      |z  }|dk7  r|}||z   }t        j                  j                  |dt
        j                  ¬	«      j                  |j                  «      }t        j                  j                  || j                  | j                  ¬
«      }t        j                  ||«      }|j                  dd«      j!                  «       }|j#                  |d|«      }|r||fS |fS )N©ro   r   rx   r   rê   ç      à¿©rs   rs   rs   rt   )ÚpÚtraining)Ú
rotary_embÚ	transposeÚunbindr   Úhead_dimrz   Úmatmulr	   r�   ÚsoftmaxÚfloat32Útorv   ÚdropoutrA   rþ   rž   r…   )rô   r¡   rn   rö   ro   rD   r÷   ru   rø   Ú_kwargsr¢   r£   ÚqueryÚkeyÚvalueÚscaleÚattn_weightsÚattn_outputs                     rV   Úeager_attention_forwardr    sK  € ð × Ñ  °<Ð Ó@�H€CˆØŸ™ a¨Ó+×2Ñ2°qÐ2Ó9Ñ€Eˆ3�ä% e¨S°#°sÓ;�J€Eˆ3à�O‰O˜TÑ!€EÜ—<‘<  s§}¡}°Q¸Ó':Ó;¸eÑC€Là˜(Ò"Ø,ˆà .Ñ0€Lô —=‘=×(Ñ(¨¸2ÄUÇ]Á]Ð(ÓS×VÑVÐW\×WbÑWbÓc€LÜ—=‘=×(Ñ(¨¸×9QÑ9QÐ\b×\kÑ\kÐ(Ól€LÜ—,‘,˜|¨UÓ3€KØ×'Ñ'¨¨1Ó-×8Ñ8Ó:€KØ×"Ñ" 2 r¨3Ó/€KÙØ˜\Ð*Ð*Øˆ>ÐrW   rÿ   Útarget_dtypec	                 óÄ  —  ||||¬«      }|j                   t        j                  t        j                  fv}
|
rb|j                   }|j	                  |«      }t        |||| j                  r| j                  nd| j                  |¬«      }|j	                  |«      }n3t        |||| j                  r| j                  nd| j                  |¬«      }|j                  ||«      fS )Nr¾   rb   )r‰   r™   Ú	dropout_pÚdeterministicÚwindow_size)
rv   rz   Úfloat16Úbfloat16r  r    rþ   rA   rM   r…   )rô   r¡   rÿ   r‰   r™   rD   r÷   ru   r  r  Úconvert_dtypeÚ
orig_dtypeÚattns                rV   Úflash_attention_forwardr  ,  sÏ   € ñ �S Z¸JÔ
G€Cà—I‘I¤e§m¡m´U·^±^Ð%DÐD€MÙð —Y‘Yˆ
Ø�f‰f�\Ó"ˆä/ØØ!Ø!Ø28·/²/�f×.Ò.ÀsØ ×9Ñ9Ø'ô
ˆð �w‰w�zÓ"‰ä/ØØ!Ø!Ø28·/²/�f×.Ò.ÀsØ ×9Ñ9Ø'ô
ˆð �I‰I�b˜#ÓÐ Ð rW   c                 óv  — | j                  ||¬«      \  }	}
|j                  dd«      j                  d¬«      \  }}}t        |||	|
«      \  }}|dk7  r|}t	        j
                  |||| j                  r| j                  nd|¬«      j                  dd«      j                  «       }|j                  |d	|«      }|fS )
Nrú   r   rx   r   rê   rü   rb   )r  Ú	attn_maskrs   )
rÿ   r   r  r   ÚFÚscaled_dot_product_attentionrþ   rA   rž   r…   )rô   r¡   rn   rö   ro   rD   r÷   ru   r  r¢   r£   r	  r
  r  r  s                  rV   Úsdpa_attention_forwardr  W  sÇ   € ð × Ñ  °<Ð Ó@�H€CˆØŸ™ a¨Ó+×2Ñ2°qÐ2Ó9Ñ€Eˆ3�ä% e¨S°#°sÓ;�J€Eˆ3à˜(Ò"Ø,ˆô 	
×&Ñ&ØØØØ28·/²/�f×.Ò.ÀsØ$ô	
÷ 
‰�1�a‹ß	‰‹ð ð ×"Ñ" 2 r¨3Ó/€KØˆ>ÐrW   )Úflash_attention_2ÚeagerÚsdpac                   óz   ‡ — e Zd ZdZd	dedee   fˆ fd„Z	 d
dej                  dee
   dej                  fd„Zˆ xZS )rõ   a‚  Performs multi-headed self attention on a batch of unpadded sequences.

    If Flash Attention 2 is installed, this module uses Flash Attention to improve throughput.
    If Flash Attention 2 is not installed, the implementation will use PyTorch's SDPA kernel,
    which requires padding and unpadding inputs, adding some overhead.

    See `forward` method for additional details.
    rÊ   Úlayer_idc                 óâ  •— t         ‰| �  «        || _        || _        |j                  |j
                  z  dk7  r&t        d|j                  › d|j
                  › d�«      ‚|j                  | _        |j                  | _        |j
                  | _	        |j                  |j
                  z  | _
        | j                  | j                  z  | _        t        j                  |j                  d| j                  z  |j                  ¬«      | _        ||j                   z  dk7  r$|j"                  dz  |j"                  dz  f| _        nd| _        |j$                  }|j&                  }| j"                  dk7  r$|j(                  �|j(                  }|j"                  }|j*                  d	k(  rt-        | j                  ||¬
«      | _        nt1        || j                  |¬«      | _        t        j                  |j                  |j                  |j                  ¬«      | _        |j                  dkD  rt        j4                  |j                  «      nt        j6                  «       | _        t;        «       | _        y )Nr   zThe hidden size (z6) is not a multiple of the number of attention heads (ú)r   rä   r   rü   r   )ru   r™   r¸   )rÊ   ru   r¸   rb   )r3   r4   rÊ   r$  r7   r:   rR   rA   rM   Ú	num_headsr  Úall_head_sizer	   rå   r@   ÚWqkvrC   rD   r?   r6   rE   Ú_attn_implementationr·   rÿ   rï   rè   rÔ   ÚIdentityÚout_dropÚsetÚpruned_heads)rS   rÊ   r$  Ú
rope_thetar6   rU   s        €rV   r4   zModernBertAttention.__init__‹  s   ø€ Ü‰ÑÔØˆŒØ ˆŒà×Ñ × :Ñ :Ñ:¸aÒ?ÜØ# F×$6Ñ$6Ð#7Ð7mÐnt÷  oIñ  oIð  nJð  JKð  Lóð ð "(×!9Ñ!9ˆÔØ(.×(GÑ(GˆÔ%Ø×3Ñ3ˆŒØ×*Ñ*¨f×.HÑ.HÑHˆŒØ!Ÿ]™]¨T¯^©^Ñ;ˆÔÜ—I‘I˜f×0Ñ0°!°d×6HÑ6HÑ2HÈv×OdÑOdÔeˆŒ	à�f×7Ñ7Ñ7¸1Ò<Ø$*×$:Ñ$:¸aÑ$?À×AWÑAWÐ[\ÑA\Ð#]ˆDÕ à#+ˆDÔ à×-Ñ-ˆ
Ø"(×"@Ñ"@ÐØ×Ñ 8Ò+Ø×&Ñ&Ð2Ø#×4Ñ4�
Ø&,×&<Ñ&<Ð#à×&Ñ&Ð*=Ò=Ü?Ø—M‘MÐ.EÈJôˆD�Oô 8¸vÈ4Ï=É=Ð_iÔjˆDŒOä—)‘)˜F×.Ñ.°×0BÑ0BÈ×I^ÑI^Ô_ˆŒØ@F×@XÑ@XÐ[^Ò@^œŸ
™
 6×#;Ñ#;Ô<Ôdf×doÑdoÓdqˆŒÜ›EˆÕrW   rÞ   rø   rq   c           
      ó  — | j                  |«      }|j                  d   }| j                  j                  dk(  r)|j	                  dd| j
                  | j                  «      }n)|j	                  |dd| j
                  | j                  «      }t        | j                  j                     | f|| j                  | j                  || j                  |dœ|¤Ž}|d   }| j                  | j                  |«      «      }|f|dd  z   S )Nr   r   rs   r   )r¡   rÿ   rD   r÷   ru   rø   rx   )r)  r„   rÊ   r*  r…   r'  r  ÚMODERNBERT_ATTENTION_FUNCTIONrÿ   rD   r(  r,  rè   )rS   rÞ   rø   rT   r¡   r÷   Úattn_outputss          rV   r©   zModernBertAttention.forward³  só   € ð �i‰i˜Ó&ˆà× Ñ  Ñ#ˆØ�;‰;×+Ñ+Ð/BÒBØ—(‘(˜2˜q $§.¡.°$·-±-Ó@‰Cà—(‘(˜2˜r 1 d§n¡n°d·m±mÓDˆCä4°T·[±[×5UÑ5UÑVØð	
àØ—‘Ø ×0Ñ0ØØ×"Ñ"Ø/ñ	
ð ñ	
ˆð % Q™ˆØŸ™ d§g¡g¨mÓ&<Ó=ˆàÐ ,¨q¨rÐ"2Ñ2Ð2rW   rÅ   ©F)re   rf   rg   rh   r#   r   r~   r4   rz   r²   Úboolr©   rk   rl   s   @rV   rõ   rõ   �  sS   ø„ ññ&"Ð/ð &"¸8ÀC¹=õ &"ðV -2ñ3à—|‘|ð3ð $ D™>ð3ð
 
�‰÷3rW   c                   óf  ‡ — e Zd Zddedee   fˆ fd„Z ej                  d¬«      dej                  dej                  fd„«       Z
	 	 	 	 	 	 ddej                  d	eej                     d
eej                     deej                     deej                     dee   dee   dej                  fd„Zˆ xZS )ÚModernBertEncoderLayerrÊ   r$  c                 óž  •— t         ‰| �  «        || _        |dk(  rt        j                  «       | _        n;t        j                  |j                  |j                  |j                  ¬«      | _        t        ||¬«      | _        t        j                  |j                  |j                  |j                  ¬«      | _        t        |«      | _        y )Nr   rÍ   )rÊ   r$  )r3   r4   rÊ   r	   r+  Ú	attn_normrÒ   r7   r=   r>   rõ   r  Úmlp_normrâ   Úmlp©rS   rÊ   r$  rU   s      €rV   r4   zModernBertEncoderLayer.__init__Ò  s�   ø€ Ü‰ÑÔØˆŒØ�qŠ=ÜŸ[™[›]ˆD�NäŸ\™\¨&×*<Ñ*<À&Ç/Á/ÐX^×XhÑXhÔiˆDŒNÜ'¨vÀÔIˆŒ	ÜŸ™ V×%7Ñ%7¸V¿_¹_ÐSY×ScÑScÔdˆŒÜ  Ó(ˆ�rW   Tr×   rÞ   rq   c                 óB   — | j                  | j                  |«      «      S rÅ   )r:  r9  ©rS   rÞ   s     rV   Úcompiled_mlpz#ModernBertEncoderLayer.compiled_mlpÝ  s   € à�x‰x˜Ÿ™ mÓ4Ó5Ð5rW   rn   rö   ro   r‰   r™   rø   c           	      ó
  — | j                  | j                  |«      ||||||¬«      }||d   z   }| j                  j                  r| j	                  |«      n| j                  | j                  |«      «      }	||	z   }|f|dd  z   S )N©rn   rö   ro   r‰   r™   rø   r   rx   )r  r8  rÊ   rP   r>  r:  r9  )
rS   rÞ   rn   rö   ro   r‰   r™   rø   r2  Ú
mlp_outputs
             rV   r©   zModernBertEncoderLayer.forwardá  sŸ   € ð —y‘yØ�N‰N˜=Ó)Ø)Ø 3Ø%Ø!Ø!Ø/ð !ó 
ˆð &¨°Q©Ñ7ˆð �{‰{×,Ò,ð ×Ñ˜mÔ,à—‘˜$Ÿ-™-¨Ó6Ó7ð 	ð
 &¨
Ñ2ˆàÐ ,¨q¨rÐ"2Ñ2Ð2rW   rÅ   )NNNNNF)re   rf   rg   r#   r   r~   r4   rz   rß   r²   r>  rà   r4  r©   rk   rl   s   @rV   r6  r6  Ñ  sí   ø„ ñ	)Ð/ð 	)¸8ÀC¹=õ 	)ð €U‡]�]˜4Ô ð6¨%¯,©,ð 6¸5¿<¹<ò 6ó !ð6ð 26Ø6:Ø37Ø-1Ø$(Ø,1ñ3à—|‘|ð3ð ! §¡Ñ.ð3ð & e§l¡lÑ3ð	3ð
 ˜u×/Ñ/Ñ0ð3ð ˜UŸ\™\Ñ*ð3ð ˜S‘Mð3ð $ D™>ð3ð 
�‰÷3rW   r6  aO  
    This model inherits from [`PreTrainedModel`]. 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 PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
    Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
    and behavior.

    Parameters:
        config ([`ModernBertConfig`]):
            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
            [`~PreTrainedModel.from_pretrained`] method to load the model weights.
zXThe bare ModernBert Model outputting raw hidden-states without any specific head on top.c                   óÈ   ‡ — e Zd ZeZdZdZddgZdZdZ	dZ
dej                  fd„Ze	 	 	 	 dded	eej$                     d
eeeeeef   f      defˆ fd„«       Zd„ Zˆ fd„Zˆ xZS )ÚModernBertPreTrainedModelÚmodelTrÉ   r6  Frô   c                 ó¤  ‡— | j                   j                  Š‰€dŠdt        j                  dt        fˆfd„}| j                   j
                  | j                   j
                  t        j                  d| j                   j                  z  «      z  | j                   j
                  | j                   j                  dz  dœ}t        |t        «      r ||j                  |d   «       y t        |t        «      r- ||j                  |d	   «        ||j                  |d
   «       y t        |t         «      r- ||j"                  |d	   «        ||j                  |d
   «       y t        |t$        «      r ||j&                  |d
   «       y t        |t(        «      r ||j*                  |d
   «       y t        |t,        t.        t0        f«      r ||j2                  |d   «       y t        |t        j4                  «      rW|j6                  j8                  j;                  d«       |j<                  �%|j<                  j8                  j?                  «        y y y )Nr   rô   Ústdc                 ó  •— t         j                  j                  | j                  d|‰ |z  ‰|z  ¬«       t	        | t         j
                  «      r7| j                  �*t         j                  j                  | j                  «       y y y )Nrb   )r*   rF  ÚaÚb)r	   ÚinitÚtrunc_normal_ÚweightÚ
isinstancerå   rÏ   Úzeros_)rô   rF  Úcutoff_factors     €rV   Úinit_weightz<ModernBertPreTrainedModel._init_weights.<locals>.init_weight"  sq   ø€ Ü�G‰G×!Ñ!Ø—‘ØØØ �. 3Ñ&Ø #Ñ%ð "ô ô ˜&¤"§)¡)Ô,Ø—;‘;Ð*Ü—G‘G—N‘N 6§;¡;Õ/ð +ð -rW   r_   rû   )ÚinÚoutÚ	embeddingÚ	final_outrS  rQ  rR  rT  g      ð?) rÊ   r<   r	   ÚModulerÆ   r;   ÚmathÚsqrtr9   r7   rM  rÉ   rÑ   râ   ræ   rè   rõ   r)  ÚModernBertPredictionHeadÚdenseÚModernBertForMaskedLMÚdecoderÚ#ModernBertForSequenceClassificationÚ ModernBertForTokenClassificationÚModernBertForQuestionAnsweringÚ
classifierrÒ   rL  ÚdataÚfill_rÏ   Úzero_)rS   rô   rP  ÚstdsrO  s       @rV   Ú_init_weightsz'ModernBertPreTrainedModel._init_weights  sÊ  ø€ ØŸ™×=Ñ=ˆØÐ ØˆMð	0¤§	¡	ð 	0´õ 	0ð —+‘+×/Ñ/Ø—;‘;×0Ñ0´4·9±9¸SÀ4Ç;Á;×C`ÑC`Ñ=`Ó3aÑaØŸ™×6Ñ6ØŸ™×0Ñ0°$Ñ6ñ	
ˆô �fÔ2Ô3Ù˜×-Ñ-¨t°KÑ/@ÕAÜ˜¤Ô.Ù˜Ÿ	™	 4¨¡:Ô.Ù˜Ÿ	™	 4¨¡;Õ/Ü˜Ô 3Ô4Ù˜Ÿ™ T¨$¡ZÔ0Ù˜Ÿ	™	 4¨¡;Õ/Ü˜Ô 8Ô9Ù˜Ÿ™ d¨5¡kÕ2Ü˜Ô 5Ô6Ù˜Ÿ™¨¨U©Õ4ÜØÜ0Ô2RÔTrÐsô
ñ ˜×)Ñ)¨4°Ñ+<Õ=Ü˜¤§¡Ô-Ø�M‰M×Ñ×$Ñ$ SÔ)Ø�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ð .rW   Úuse_flash_attention_2Útorch_dtypeÚ
device_mapÚcheck_device_mapc                 óð   •— |j                   €,d|_         	 | j                  |t        j                  |d|¬«      S t        ‰| �  ||t        j                  ||¬«      S # t        t
        f$ r
 d |_         Y Œ:w xY w)Nr   F)rf  rg  Úhard_check_onlyrh  )re  rf  rg  rh  )Ú_attn_implementation_internalÚ_check_and_enable_flash_attn_2rz   r  rR   ÚImportErrorr3   Ú_autoset_attn_implementation)r)   rÊ   re  rf  rg  rh  rU   s         €rV   rn  z6ModernBertPreTrainedModel._autoset_attn_implementationL  s“   ø€ ð ×/Ñ/Ð7Ø3FˆFÔ0ð	<Ø×9Ñ9ØÜ %§¡Ø)Ø$)Ø%5ð :ó ð ô ‰wÑ3ØØ"7ÜŸ™Ø!Ø-ð 4ó 
ð 	
øô ¤Ð,ò <Ø7;�Ö4ð<ús   –#A ÁA5Á4A5c                 óª  — | j                   j                  du ry t        | d«      rTt        | j                  «      dkD  r<| j                   j                  rt
        j                  d«       d| j                   _        | j                  j                  dk(  r<| j                   j                  rt
        j                  d«       d| j                   _        | j                  j                  dk(  r<| j                   j                  rt
        j                  d«       d| j                   _        | j                   j                  €t        «       | j                   _        y y )	NFÚhf_device_maprx   zqIf `accelerate` split the model across devices, `torch.compile` will not work. Falling back to non-compiled mode.Úmpsz|Compiling the model with `torch.compile` and using a `torch.mps` device is not supported. Falling back to non-compiled mode.Úcpuz|Compiling the model with `torch.compile` and using a `torch.cpu` device is not supported. Falling back to non-compiled mode.)
rÊ   rP   ÚhasattrÚlenrp  ÚloggerÚwarning_oncer’   Útyper   rÃ   s    rV   Ú_maybe_set_compilez,ModernBertPreTrainedModel._maybe_set_compilem  s  € Ø�;‰;×(Ñ(¨EÑ1Øä�4˜Ô)¬c°$×2DÑ2DÓ.EÈÒ.IØ�{‰{×,Ò,Ü×#Ñ#ð9ôð -2ˆD�K‰KÔ)à�;‰;×Ñ˜uÒ$Ø�{‰{×,Ò,Ü×#Ñ#ð9ôð -2ˆD�K‰KÔ)à�;‰;×Ñ˜uÒ$Ø�{‰{×,Ò,Ü×#Ñ#ð9ôð -2ˆD�K‰KÔ)à�;‰;×(Ñ(Ð0Ü,?Ó,AˆD�K‰KÕ)ð 1rW   c                 óÎ   •— t        ‰| �  |i |¤Ž}| j                  j                  dv r<| j                  j                  rt        j                  d«       d| j                  _        |S )N>   NTzcResizing token embeddings with `torch.compile` is not supported. Falling back to non-compiled mode.F)r3   Úresize_token_embeddingsrÊ   rP   ru  rv  )rS   ÚargsrT   Úmodel_embedsrU   s       €rV   rz  z1ModernBertPreTrainedModel.resize_token_embeddingsŒ  s[   ø€ Ü‘wÑ6¸ÐGÀÑGˆà�;‰;×(Ñ(¨LÑ8Ø�{‰{×,Ò,Ü×#Ñ#Øyôð -2ˆD�K‰KÔ)àÐrW   )FNNT)re   rf   rg   r#   Úconfig_classÚbase_model_prefixÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_supports_flash_attn_2Ú_supports_sdpaÚ_supports_flex_attnr	   rU  rd  Úclassmethodr4  r   rz   rv   r   rÇ   r   r~   rn  rx  rz  rk   rl   s   @rV   rC  rC    s¿   ø„ ð
 $€LØÐØ&*Ð#Ø/Ð1IÐJÐØ!ÐØ€NØÐð-) B§I¡Ió -)ð^ ð ',Ø-1Ø;?Ø!%ñ
ð  $ð
ð ˜eŸk™kÑ*ð	
ð
 ˜U 3¨¨S°#¨X©Ð#6Ñ7Ñ8ð
ð ô
ó ð
ò@B÷>
ð 
rW   rC  aø  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. With Flash Attention 2.0, 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 (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

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

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

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

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

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.
        sliding_window_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding or far-away tokens. In ModernBert, only every few layers
            perform global attention, while the rest perform local attention. This mask is used to avoid attending to
            far-away tokens in the local attention layers when not using Flash Attention.
        position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.n_positions - 1]`.

            [What are position IDs?](../glossary#position-ids)
        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        indices (`torch.Tensor` of shape `(total_unpadded_tokens,)`, *optional*):
            Indices of the non-padding tokens in the input sequence. Used for unpadding the output.
        cu_seqlens (`torch.Tensor` of shape `(batch + 1,)`, *optional*):
            Cumulative sequence lengths of the input sequences. Used to index the unpadded tensors.
        max_seqlen (`int`, *optional*):
            Maximum sequence length in the batch excluding padding tokens. Used to unpad input_ids and pad output tensors.
        batch_size (`int`, *optional*):
            Batch size of the input sequences. Used to pad the output tensors.
        seq_len (`int`, *optional*):
            Sequence length of the input sequences including padding tokens. Used to pad the output tensors.
        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            "       óü  ‡ — e Zd Zdefˆ fd„Zd„ Zd„ Z ee«       e	e
ee¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 ddeej                     deej                      deej                      d	eej                     d
eej                      deej                      deej                      dee   dee   dee   dee   dee   dee   deeej                   df   ef   fd„«       «       Zdej                   dedej                   fd„Zˆ xZS )ÚModernBertModelrÊ   c           	      óŠ  •— t         ‰| �  |«       || _        t        |«      | _        t        j                  t        |j                  «      D �cg c]  }t        ||«      ‘Œ c}«      | _
        t        j                  |j                  |j                  |j                  ¬«      | _        d| _        | j#                  «        y c c}w )NrÍ   F)r3   r4   rÊ   rÉ   Ú
embeddingsr	   Ú
ModuleListÚranger9   r6  ÚlayersrÒ   r7   r=   r>   Ú
final_normÚgradient_checkpointingÚ	post_initr;  s      €rV   r4   zModernBertModel.__init__Û  s“   ø€ Ü‰Ñ˜Ô ØˆŒÜ.¨vÓ6ˆŒÜ—m‘mÜFKÈF×LdÑLdÓFeÖf¸(Ô# F¨HÕ5Òfó
ˆŒô Ÿ,™, v×'9Ñ'9¸v¿¹ÐU[×UeÑUeÔfˆŒØ&+ˆÔ#Ø�‰Õùò	 gs   ÁC c                 ó.   — | j                   j                  S rÅ   ©rˆ  rÑ   rÃ   s    rV   Úget_input_embeddingsz$ModernBertModel.get_input_embeddingsæ  s   € Ø�‰×-Ñ-Ð-rW   c                 ó&   — || j                   _        y rÅ   r�  )rS   r  s     rV   Úset_input_embeddingsz$ModernBertModel.set_input_embeddingsé  s   € Ø).ˆ�‰Õ&rW   ©Ú
checkpointÚoutput_typer}  rÙ   rn   rö   ro   rÜ   r‡   r‰   r™   Ú
batch_sizeÚseq_lenrø   Úoutput_hidden_statesÚreturn_dictrq   .c                 ój  ‡‡	‡
— |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|d u |d uz  rt	        d«      ‚|rdnd }|rdnd }| j                  «        |�| j                  ||«       ‰	€)‰
€'|�|j                  d d \  Š	Š
n|j                  d d \  Š	Š
|�|j                  n|j                  }|€(t        j                  ‰	‰
f|t        j                  ¬«      }d}| j                   j                  dk(  rM‰€‰|€‡|€…d}|€0t        j                  «       5  t        ||¬«      ^}Š}}}d d d «       nQt        ||¬«      ^}Š}}}n>|€&t        j                  ‰
|¬	«      j!                  d
«      }| j#                  ||¬«      \  }}| j%                  ||¬«      }| j&                  D ]t  }|r||fz   }| j(                  r/| j*                  r#| j-                  |j.                  |||||||«      }n ||||||||¬«      }|d
   }|sŒ]t1        |«      dkD  sŒl||d   fz   }Œv |r||fz   }| j3                  |«      }|r't5        |‰‰	‰
¬«      }|�t7        ˆ	ˆˆ
fd„|D «       «      }|st7        d„ |||fD «       «      S t9        |||¬«      S # 1 sw Y   �ŒxY w)Nz:You must specify exactly one of input_ids or inputs_embedsr2   r   r»   Fr   T)rm   rn   )r’   r   )rø   )rÙ   rÜ   r@  rx   ©rm   r‡   r‹   rŒ   c              3   ó<   •K  — | ]  }t        |‰‰‰¬ «      –— Œ y­w)rœ  N)r–   )Ú.0Úhsr—  r‡   r˜  s     €€€rV   ú	<genexpr>z*ModernBertModel.forward.<locals>.<genexpr>[  s(   øè ø€ ò *àô +°"¸gÈZÐ`g×hÐhñ*ùs   ƒc              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrÅ   r2   )rž  Úvs     rV   r   z*ModernBertModel.forward.<locals>.<genexpr>a  s   è ø€ Òm˜qÐ_`Ñ_lœÑmùs   ‚Š)Úlast_hidden_staterÞ   Ú
attentions)rÊ   rø   r™  Úuse_return_dictrR   rx  Ú%warn_if_padding_and_no_attention_maskr„   r’   rz   Úonesr4  r*  Úno_gradr�   ÚarangeÚ	unsqueezeÚ_update_attention_maskrˆ  r‹  r�  rþ   Ú_gradient_checkpointing_funcÚ__call__rt  rŒ  r–   Útupler   )rS   rÙ   rn   rö   ro   rÜ   r‡   r‰   r™   r—  r˜  rø   r™  rš  Úall_hidden_statesÚall_self_attentionsr’   Úrepadr•   rÞ   Úencoder_layerÚlayer_outputss         `  ``           rV   r©   zModernBertModel.forwardì  sG  ú€ ð, 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà˜Ð -°tÐ";Ò<ÜÐYÓZÐZá"6™B¸DÐÙ$5™b¸4Ðà×ÑÔ!àÐ Ø×6Ñ6°yÀ.ÔQàÐ ' /ØÐ(Ø&3×&9Ñ&9¸"¸1Ð&=Ñ#�
™Gà&/§o¡o°b°qÐ&9Ñ#�
˜GØ%.Ð%:�×!Ò!À×@TÑ@TˆàÐ!Ü"ŸZ™Z¨°WÐ(=ÀfÔTY×T^ÑT^Ô_ˆNàˆØ�;‰;×+Ñ+Ð/BÒBØˆ :Ð#5¸*Ð:LØ�Ø Ð(ÜŸ™›ñ ÜI`Ø#,¸^ôJÐF˜	 7¨J¸
ÀQ÷ð ô
 JaØ,¸^ôJÐF�M 7¨J¸
ÁQð Ð#Ü$Ÿ|™|¨G¸FÔC×MÑMÈaÓP�à26×2MÑ2MØÐ2Cð 3Nó 3Ñ/ˆNÐ/ð Ÿ™°)È=˜ÓYˆà!Ÿ[™[ò 	PˆMÙ#Ø$5¸Ð8HÑ$HÐ!à×*Ò*¨t¯}ª}Ø $× AÑ AØ!×*Ñ*Ø!Ø"Ø'Ø ØØØ%ó	!‘ñ !.Ø!Ø#1Ø(;Ø!-Ø)Ø)Ø&7ô!�ð *¨!Ñ,ˆMÚ ¤S¨Ó%7¸!Ó%;Ø&9¸]È1Ñ=MÐ<OÑ&OÑ#ð7	Pñ:  Ø 1°]Ð4DÑ DÐàŸ™¨Ó6ˆáÜ2Ø$¨g¸ZÐPWôˆMð !Ð,Ü$)õ *à/ô*ó %Ð!ñ
 ÜÑm ]Ð4EÐGZÐ$[ÔmÓmÐmÜØ+Ø+Ø*ô
ð 	
÷Añ ús   Ä>J(Ê(J2c                 ó   — |r†| j                   j                  dk(  r't        j                  d«       d| j                   _        nF| j                   j                  dk7  r-t        j                  d| j                   j                  › d�«       t	        || j
                  «      }t        j                  |j                  d   «      j                  d«      }t        j                  ||j                  z
  «      }|| j                   j                  dz  k  j                  d«      j                  d«      j                  |j                  «      }|j                  |j!                  «       t        j"                  | j
                  «      j$                  «      }||fS )Nr"  z’Outputting attentions is only supported with the 'eager' attention implementation, not with "sdpa". Falling back to `attn_implementation="eager"`.r!  zZOutputting attentions is only supported with the eager attention implementation, not with zT. Consider setting `attn_implementation="eager"`. Setting `output_attentions=False`.r   r   )rÊ   r*  ru  rv  r   rv   rz   r©  r„   rª  ÚabsÚTrD   r  r’   Úmasked_fillÚlogical_notÚfinfoÚmin)rS   rn   rø   Úglobal_attention_maskÚrowsÚdistanceÚwindow_maskrö   s           rV   r«  z&ModernBertModel._update_attention_maskh  sS  € ÙØ�{‰{×/Ñ/°6Ò9Ü×#Ñ#ðVôð 4;�—‘Õ0Ø—‘×1Ñ1°WÒ<Ü×#Ñ#ð Ø $§¡× @Ñ @ÐAð B:ð:ôô !;¸>È4Ï:É:Ó VÐô �|‰|Ð1×7Ñ7¸Ñ:Ó;×EÑEÀaÓHˆä—9‘9˜T D§F¡F™]Ó+ˆð ˜Ÿ™×4Ñ4¸Ñ9Ñ9×DÑDÀQÓG×QÑQÐRSÓT×WÑWÐXf×XmÑXmÓnð 	ð 4×?Ñ?À×@WÑ@WÓ@YÔ[`×[fÑ[fÐgk×gqÑgqÓ[r×[vÑ[vÓwÐà$Ð&9Ð9Ð9rW   ©NNNNNNNNNNNNN)re   rf   rg   r#   r4   r‘  r“  r   ÚMODERNBERT_INPUTS_DOCSTRINGr   Ú_CHECKPOINT_FOR_DOCr   Ú_CONFIG_FOR_DOCr   rz   rà   r²   r~   r4  r   r   r©   r«  rk   rl   s   @rV   r†  r†  Ö  s¨  ø„ ð
	Ð/õ 	ò.ò/ñ +Ð+FÓGÙØ&Ø#Ø$ôð 15Ø15Ø6:Ø37Ø04Ø*.Ø-1Ø$(Ø$(Ø!%Ø,0Ø/3Ø&*ñt
à˜E×,Ñ,Ñ-ðt
ð ! §¡Ñ.ðt
ð & e§l¡lÑ3ð	t
ð
 ˜u×/Ñ/Ñ0ðt
ð   §¡Ñ-ðt
ð ˜%Ÿ,™,Ñ'ðt
ð ˜UŸ\™\Ñ*ðt
ð ˜S‘Mðt
ð ˜S‘Mðt
ð ˜#‘ðt
ð $ D™>ðt
ð ' t™nðt
ð ˜d‘^ðt
ð 
ˆu�U—\‘\ 3Ð&Ñ'¨Ð8Ñ	9òt
óó Hðt
ðl:°U·\±\ð :ÐVZð :Ð_d×_kÑ_k÷ :rW   r†  c                   ó\   ‡ — e Zd Zdefˆ fd„Zdej                  dej                  fd„Zˆ xZS )rX  rÊ   c                 óJ  •— t         ‰| �  «        || _        t        j                  |j
                  |j
                  |j                  «      | _        t        |j                     | _
        t        j                  |j
                  |j                  |j                  ¬«      | _        y )NrÍ   )r3   r4   rÊ   r	   rå   r7   rK   rY  r   rL   rç   rÒ   r=   r>   rÓ   rÖ   s     €rV   r4   z!ModernBertPredictionHead.__init__‰  sq   ø€ Ü‰ÑÔØˆŒÜ—Y‘Y˜v×1Ñ1°6×3EÑ3EÀv×G]ÑG]Ó^ˆŒ
Ü˜&×6Ñ6Ñ7ˆŒÜ—L‘L ×!3Ñ!3¸¿¹Èv×O_ÑO_Ô`ˆ�	rW   rÞ   rq   c                 ó`   — | j                  | j                  | j                  |«      «      «      S rÅ   )rÓ   rç   rY  r=  s     rV   r©   z ModernBertPredictionHead.forward�  s#   € Ø�y‰y˜Ÿ™ $§*¡*¨]Ó";Ó<Ó=Ð=rW   )	re   rf   rg   r#   r4   rz   r²   r©   rk   rl   s   @rV   rX  rX  ˆ  s-   ø„ ðaÐ/õ að> U§\¡\ð >°e·l±l÷ >rW   rX  zZThe ModernBert Model with a decoder head on top that is used for masked language modeling.c            #       ó`  ‡ — e Zd ZdgZdefˆ fd„Zd„ Zdej                  fd„Z	 e
j                  d¬«      d	e
j                  d
e
j                  fd„«       Z ee«       eeee¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddee
j*                     dee
j                     dee
j                     dee
j                     dee
j                     dee
j                     dee
j                     dee
j                     dee   dee   dee   dee   dee   dee   d
eee
j                     ef   fd„«       «       Zˆ xZS )rZ  zdecoder.weightrÊ   c                 ót  •— t         ‰| �  |«       || _        t        |«      | _        t        |«      | _        t        j                  |j                  |j                  |j                  ¬«      | _        | j                  j                  | _        | j                  j                  | _        | j                  «        y )Nrä   )r3   r4   rÊ   r†  rD  rX  Úheadr	   rå   r7   r5   rI   r[  rN   rO   rŽ  rÖ   s     €rV   r4   zModernBertForMaskedLM.__init__›  s…   ø€ Ü‰Ñ˜Ô ØˆŒÜ$ VÓ,ˆŒ
Ü,¨VÓ4ˆŒ	Ü—y‘y ×!3Ñ!3°V×5FÑ5FÈV×M`ÑM`ÔaˆŒà!%§¡×!>Ñ!>ˆÔØ(,¯©×(LÑ(LˆÔ%ð 	�‰ÕrW   c                 ó   — | j                   S rÅ   ©r[  rÃ   s    rV   Úget_output_embeddingsz+ModernBertForMaskedLM.get_output_embeddings¨  s   € Ø�|‰|ÐrW   Únew_embeddingsc                 ó   — || _         y rÅ   rÊ  )rS   rÌ  s     rV   Úset_output_embeddingsz+ModernBertForMaskedLM.set_output_embeddings«  s	   € Ø%ˆ�rW   Tr×   r[   rq   c                 óB   — | j                  | j                  |«      «      S rÅ   )r[  rÈ  )rS   r[   s     rV   Úcompiled_headz#ModernBertForMaskedLM.compiled_head®  s   € à�|‰|˜DŸI™I fÓ-Ó.Ð.rW   r”  rÙ   rn   rö   ro   rÜ   rp   r‡   r‰   r™   r—  r˜  rø   r™  rš  c                 óH  — |�|n| j                   j                  }| j                  «        | j                   j                  dk(  rÁ|€¿|€½|	€»|
€)|€'|�|j                  d d \  }
}n|j                  d d \  }
}|�|j
                  n|j
                  }|€(t        j                  |
|f|t        j                  ¬«      }|€4t        j                  «       5  t        ||||¬«      \  }}}}	}}d d d «       nt        ||||¬«      \  }}}}	}}| j                  ||||||||	|
||||¬«      }|d   }| j                  rK|�I|j                  d«      }|j                  |j                  d   d«      }|| j                  k7  }||   }||   }| j                   j                  r| j!                  |«      n| j#                  | j%                  |«      «      }d }|�(| j'                  ||| j                   j(                  ¬«      }| j                   j                  dk(  rN| j                   j*                  s|€
t-        «       nt        j                  «       5  t/        |||
|¬	«      }d d d «       |s|f}|�|f|z   S |S t1        |||j2                  |j4                  ¬
«      S # 1 sw Y   �Œ�xY w# 1 sw Y   ŒHxY w)Nr   r   r»   )rm   rn   ro   rp   ©rÙ   rn   rö   ro   rÜ   r‡   r‰   r™   r—  r˜  rø   r™  rš  r   rs   )r5   rœ  ©ÚlossÚlogitsrÞ   r¤  )rÊ   r¥  rx  r*  r„   r’   rz   r§  r4  r¨  r�   rD  rN   r…   rO   rP   rÐ  r[  rÈ  Úloss_functionr5   rQ   r   r–   r   rÞ   r¤  )rS   rÙ   rn   rö   ro   rÜ   rp   r‡   r‰   r™   r—  r˜  rø   r™  rš  rT   r’   Úoutputsr£  Úmask_tokensrÕ  rÔ  r[   s                          rV   r©   zModernBertForMaskedLM.forward²  sì  € ð0 &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆØ×ÑÔ!à�;‰;×+Ñ+Ð/BÒBØˆ :Ð#5¸*Ð:LØÐ%¨'¨/Ø$Ð0Ø.;×.AÑ.AÀ"À1Ð.EÑ+˜
¡Gà.7¯o©o¸b¸qÐ.AÑ+˜
 GØ-6Ð-B˜×)Ò)È×H\ÑH\�à!Ð)Ü%*§Z¡Z°¸WÐ0EÈfÔ\a×\fÑ\fÔ%g�Nà Ð(ÜŸ™›ñ Ü[rØ#,¸^ÐZfÐouô\ÑX˜	 7¨J¸
ÀLÐRX÷ð ô
 \sØ,¸^ÐZfÐouô\ÑX�M 7¨J¸
ÀLÐRXð —*‘*ØØ)Ø 3Ø%Ø'ØØ!Ø!Ø!ØØ/Ø!5Ø#ð ó 
ˆð $ A™JÐà×!Ò! fÐ&8à—[‘[ “_ˆFØ 1× 6Ñ 6°v·|±|ÀA±ÈÓ KÐð ! D×$AÑ$AÑAˆKØ 1°+Ñ >ÐØ˜KÑ(ˆFð �{‰{×,Ò,ð ×ÑÐ0Ô1à—‘˜dŸi™iÐ(9Ó:Ó;ð 	ð ˆØÐØ×%Ñ% f¨fÀÇÁ×AWÑAWÐ%ÓXˆDà�;‰;×+Ñ+Ð/BÒBØ"&§+¡+×"DÒ"DÈÈ””Ô\a×\iÑ\iÓ\kñ rÜ/°vÀwÐV`ÐipÔq�÷rñ Ø�YˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEäØØØ!×/Ñ/Ø×)Ñ)ô	
ð 	
÷mñ ú÷^rð rús   ÃJÉJÊJÊJ!©NNNNNNNNNNNNNN)re   rf   rg   Ú_tied_weights_keysr#   r4   rË  r	   rå   rÎ  rz   rß   r²   rÐ  r   rÀ  r   rÁ  r   rÂ  r   rà   r~   r4  r   r   r©   rk   rl   s   @rV   rZ  rZ  ”  sÜ  ø„ ð
 +Ð+ÐðÐ/õ òð&°B·I±Ió &ð €U‡]�]˜4Ô ð/ E§L¡Lð /°U·\±\ò /ó !ð/ñ +Ð+FÓGÙØ&Ø"Ø$ôð 15Ø15Ø6:Ø/3Ø04Ø)-Ø*.Ø-1Ø$(Ø$(Ø!%Ø,0Ø/3Ø&*ñ]
à˜E×,Ñ,Ñ-ð]
ð ! §¡Ñ.ð]
ð & e§l¡lÑ3ð	]
ð
 ˜uŸ|™|Ñ,ð]
ð   §¡Ñ-ð]
ð ˜Ÿ™Ñ&ð]
ð ˜%Ÿ,™,Ñ'ð]
ð ˜UŸ\™\Ñ*ð]
ð ˜S‘Mð]
ð ˜S‘Mð]
ð ˜#‘ð]
ð $ D™>ð]
ð ' t™nð]
ð ˜d‘^ð]
ð" 
ˆu�U—\‘\Ñ" NÐ2Ñ	3ò#]
óó Hô]
rW   rZ  zVThe ModernBert Model with a sequence classification head on top that performs pooling.c            #       óÐ  ‡ — e Zd Zdefˆ fd„Z ee«       eee	e
¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     deej                     deej                     d	eej                     d
eej                     deej                     dee   dee   dee   dee   dee   dee   deeej                     e	f   fd„«       «       Zˆ xZS )r\  rÊ   c                 ón  •— t         ‰| �  |«       |j                  | _        || _        t	        |«      | _        t        |«      | _        t        j                  j                  |j                  «      | _        t        j                  |j                  |j                  «      | _        | j!                  «        y rÅ   )r3   r4   Ú
num_labelsrÊ   r†  rD  rX  rÈ  rz   r	   rÔ   rJ   rÕ   rå   r7   r_  rŽ  rÖ   s     €rV   r4   z,ModernBertForSequenceClassification.__init__  s‚   ø€ Ü‰Ñ˜Ô Ø ×+Ñ+ˆŒØˆŒä$ VÓ,ˆŒ
Ü,¨VÓ4ˆŒ	Ü—H‘H×$Ñ$ V×%>Ñ%>Ó?ˆŒ	ÜŸ)™) F×$6Ñ$6¸×8IÑ8IÓJˆŒð 	�‰ÕrW   r”  rÙ   rn   rö   ro   rÜ   rp   r‡   r‰   r™   r—  r˜  rø   r™  rš  rq   c                 óf  — |�|n| j                   j                  }| j                  «        | j                  ||||||||	|
||||¬«      }|d   }| j                   j                  dk(  r
|dd…df   }nQ| j                   j                  dk(  r8||j                  d«      z  j                  d¬«      |j                  dd	¬
«      z  }| j                  |«      }| j                  |«      }| j                  |«      }d}|��‡| j                   j                  €�| j                  dk(  rd| j                   _
        nl| j                  dkD  rL|j                  t        j                  k(  s|j                  t        j                  k(  rd| j                   _
        nd| j                   _
        | j                   j                  dk(  rIt!        «       }| j                  dk(  r& ||j#                  «       |j#                  «       «      }nŒ |||«      }n‚| j                   j                  dk(  r=t%        «       } ||j'                  d| j                  «      |j'                  d«      «      }n,| j                   j                  dk(  rt)        «       } |||«      }|s|f}|�|f|z   S |S t+        |||j,                  |j.                  ¬«      S )a�  
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        NrÒ  r   r)   r*   rs   rx   rê   T)ru   ÚkeepdimÚ
regressionÚsingle_label_classificationÚmulti_label_classificationrÓ  )rÊ   r¥  rx  rD  r'   rª  ry   rÈ  rÕ   r_  Úproblem_typerÝ  rv   rz   Úlongr~   r   Úsqueezer   r…   r
   r   rÞ   r¤  )rS   rÙ   rn   rö   ro   rÜ   rp   r‡   r‰   r™   r—  r˜  rø   r™  rš  rT   r×  r£  Úpooled_outputrÕ  rÔ  Úloss_fctr[   s                          rV   r©   z+ModernBertForSequenceClassification.forward*  s  € ð< &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆØ×ÑÔ!à—*‘*ØØ)Ø 3Ø%Ø'ØØ!Ø!Ø!ØØ/Ø!5Ø#ð ó 
ˆð $ A™JÐà�;‰;×)Ñ)¨UÒ2Ø 1²!°Q°$Ñ 7ÑØ�[‰[×+Ñ+¨vÒ5Ø!2°^×5MÑ5MÈbÓ5QÑ!Q× VÑ VÐ[\Ð VÓ ]Ð`n×`rÑ`rØ˜tð asó añ !Ðð Ÿ	™	Ð"3Ó4ˆØŸ	™	 -Ó0ˆØ—‘ Ó/ˆàˆØÑØ�{‰{×'Ñ'Ð/Ø—?‘? aÒ'Ø/;�D—K‘KÕ,Ø—_‘_ qÒ(¨f¯l©l¼e¿j¹jÒ.HÈFÏLÉLÔ\a×\eÑ\eÒLeØ/L�D—K‘KÕ,à/K�D—K‘KÔ,à�{‰{×'Ñ'¨<Ò7Ü"›9�Ø—?‘? aÒ'Ù# F§N¡NÓ$4°f·n±nÓ6FÓG‘Dá# F¨FÓ3‘DØ—‘×)Ñ)Ð-JÒJÜ+Ó-�Ù §¡¨B°·±Ó @À&Ç+Á+ÈbÃ/ÓR‘Ø—‘×)Ñ)Ð-IÒIÜ,Ó.�Ù ¨Ó/�áØ�YˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEä'ØØØ!×/Ñ/Ø×)Ñ)ô	
ð 	
rW   rÙ  )re   rf   rg   r#   r4   r   rÀ  r   rÁ  r   rÂ  r   rz   rà   r²   r~   r4  r   r   r©   rk   rl   s   @rV   r\  r\    sˆ  ø„ ð
Ð/õ ñ +Ð+FÓGÙØ&Ø,Ø$ôð 15Ø15Ø6:Ø/3Ø04Ø)-Ø*.Ø-1Ø$(Ø$(Ø!%Ø,0Ø/3Ø&*ñW
à˜E×,Ñ,Ñ-ðW
ð ! §¡Ñ.ðW
ð & e§l¡lÑ3ð	W
ð
 ˜uŸ|™|Ñ,ðW
ð   §¡Ñ-ðW
ð ˜Ÿ™Ñ&ðW
ð ˜%Ÿ,™,Ñ'ðW
ð ˜UŸ\™\Ñ*ðW
ð ˜S‘MðW
ð ˜S‘MðW
ð ˜#‘ðW
ð $ D™>ðW
ð ' t™nðW
ð ˜d‘^ðW
ð" 
ˆu�U—\‘\Ñ"Ð$<Ð<Ñ	=ò#W
óó HôW
rW   r\  zlThe ModernBert Model with a token classification head on top, e.g. for Named Entity Recognition (NER) tasks.c            #       óÐ  ‡ — e Zd Zdefˆ fd„Z ee«       eee	e
¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     deej                     deej                     d	eej                     d
eej                     deej                     dee   dee   dee   dee   dee   dee   deeej                     e	f   fd„«       «       Zˆ xZS )r]  rÊ   c                 ó`  •— t         ‰| �  |«       |j                  | _        t        |«      | _        t        |«      | _        t        j                  j                  |j                  «      | _        t        j                  |j                  |j                  «      | _        | j                  «        y rÅ   ©r3   r4   rÝ  r†  rD  rX  rÈ  rz   r	   rÔ   rJ   rÕ   rå   r7   r_  rŽ  rÖ   s     €rV   r4   z)ModernBertForTokenClassification.__init__�  s{   ø€ Ü‰Ñ˜Ô Ø ×+Ñ+ˆŒä$ VÓ,ˆŒ
Ü,¨VÓ4ˆŒ	Ü—H‘H×$Ñ$ V×%>Ñ%>Ó?ˆŒ	ÜŸ)™) F×$6Ñ$6¸×8IÑ8IÓJˆŒð 	�‰ÕrW   r”  rÙ   rn   rö   ro   rÜ   rp   r‡   r‰   r™   r—  r˜  rø   r™  rš  rq   c                 óò  — |�|n| j                   j                  }| j                  «        | j                  ||||||||	|
||||¬«      }|d   }| j	                  |«      }| j                  |«      }| j                  |«      }d}|�<t        «       } ||j                  d| j                  «      |j                  d«      «      }|s|f|dd z   }|�|f|z   S |S t        |||j                  |j                  ¬«      S )zÛ
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`.
        NrÒ  r   rs   rx   rÓ  )rÊ   r¥  rx  rD  rÈ  rÕ   r_  r   r…   rÝ  r   rÞ   r¤  )rS   rÙ   rn   rö   ro   rÜ   rp   r‡   r‰   r™   r—  r˜  rø   r™  rš  r×  r£  rÕ  rÔ  rç  r[   s                        rV   r©   z(ModernBertForTokenClassification.forward›  s"  € ð6 &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆØ×ÑÔ!à—*‘*ØØ)Ø 3Ø%Ø'ØØ!Ø!Ø!ØØ/Ø!5Ø#ð ó 
ˆð $ A™JÐà ŸI™IÐ&7Ó8ÐØ ŸI™IÐ&7Ó8ÐØ—‘Ð!2Ó3ˆàˆØÐÜ'Ó)ˆHÙ˜FŸK™K¨¨D¯O©OÓ<¸f¿k¹kÈ"»oÓNˆDáØ�Y ¨¨ Ñ,ˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEä$ØØØ!×/Ñ/Ø×)Ñ)ô	
ð 	
rW   rÙ  )re   rf   rg   r#   r4   r   rÀ  r   rÁ  r   rÂ  r   rz   rà   r²   r~   r4  r   r   r©   rk   rl   s   @rV   r]  r]  Š  sw  ø„ ð

Ð/õ 
ñ +Ð+FÓGÙØ&Ø)Ø$ôð 15Ø15Ø6:Ø/3Ø04Ø)-Ø*.Ø-1Ø$(Ø$(Ø!%Ø,0Ø/3Ø&*ñ;
à˜E×,Ñ,Ñ-ð;
ð ! §¡Ñ.ð;
ð & e§l¡lÑ3ð	;
ð
 ˜uŸ|™|Ñ,ð;
ð   §¡Ñ-ð;
ð ˜Ÿ™Ñ&ð;
ð ˜%Ÿ,™,Ñ'ð;
ð ˜UŸ\™\Ñ*ð;
ð ˜S‘Mð;
ð ˜S‘Mð;
ð ˜#‘ð;
ð $ D™>ð;
ð ' t™nð;
ð ˜d‘^ð;
ð  
ˆu�U—\‘\Ñ"Ð$9Ð9Ñ	:ò!;
óó Hô;
rW   r]  zæ
    The ModernBert Model with a span classification head on top for extractive question-answering tasks like SQuAD
    (a linear layer on top of the hidden-states output to compute `span start logits` and `span end logits`).
    c            #       óÎ  ‡ — e Zd Zdefˆ fd„Z ee«       eee	e
¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     deej                     deej                     d	eej                     d
eej                     deej                     dee   dee   dee   dee   dee   dee   deeej                     e	f   fd„«       «       Zˆ xZS )r^  rÊ   c                 ó`  •— t         ‰| �  |«       |j                  | _        t        |«      | _        t        |«      | _        t        j                  j                  |j                  «      | _        t        j                  |j                  |j                  «      | _        | j                  «        y rÅ   rê  rÖ   s     €rV   r4   z'ModernBertForQuestionAnswering.__init__ç  sy   ø€ Ü‰Ñ˜Ô Ø ×+Ñ+ˆŒä$ VÓ,ˆŒ
Ü,¨VÓ4ˆŒ	Ü—H‘H×$Ñ$ V×%>Ñ%>Ó?ˆŒ	ÜŸ)™) F×$6Ñ$6¸×8IÑ8IÓJˆŒà�‰ÕrW   r”  rÙ   rn   rö   ro   Ústart_positionsÚend_positionsr‡   r‰   r™   r—  r˜  rø   r™  rš  rq   c                 óT  — |�|n| j                   j                  }| j                  «        | j                  |||||||	|
||||¬«      }|d   }| j	                  |«      }| j                  |«      }| j                  |«      }|j                  dd¬«      \  }}|j                  d«      j                  «       }|j                  d«      j                  «       }d }|�|� | j                  ||||fi |¤Ž}|s||f|dd  z   }|�|f|z   S |S t        ||||j                  |j                  ¬«      S )N)rn   rö   ro   r‡   r‰   r™   r—  r˜  rø   r™  rš  r   rx   rs   rê   )rÔ  Ústart_logitsÚ
end_logitsrÞ   r¤  )rÊ   r¥  rx  rD  rÈ  rÕ   r_  Úsplitrå  rž   rÖ  r   rÞ   r¤  )rS   rÙ   rn   rö   ro   rî  rï  r‡   r‰   r™   r—  r˜  rø   r™  rš  rT   r×  r£  rÕ  rñ  rò  rÔ  r[   s                          rV   r©   z&ModernBertForQuestionAnswering.forwardò  sg  € ð0 &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆØ×ÑÔ!à—*‘*ØØ)Ø 3Ø%ØØ!Ø!Ø!ØØ/Ø!5Ø#ð ó 
ˆð $ A™JÐà ŸI™IÐ&7Ó8ÐØ ŸI™IÐ&7Ó8ÐØ—‘Ð!2Ó3ˆà#)§<¡<°°r <Ó#:Ñ ˆ�jØ#×+Ñ+¨BÓ/×:Ñ:Ó<ˆØ×'Ñ'¨Ó+×6Ñ6Ó8ˆ
àˆØÐ&¨=Ð+DØ%�4×%Ñ% l°JÀÐQ^ÑiÐbhÑiˆDáØ" JÐ/°'¸!¸"°+Ñ=ˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEä+ØØ%Ø!Ø!×/Ñ/Ø×)Ñ)ô
ð 	
rW   r¿  )re   rf   rg   r#   r4   r   rÀ  r   rÁ  r   rÂ  r   rz   r²   r~   r4  r   r   r©   rk   rl   s   @rV   r^  r^  ß  sr  ø„ ð	Ð/õ 	ñ +Ð+FÓGÙØ&Ø0Ø$ôð 26Ø6:Ø/3Ø26Ø04Ø*.Ø-1Ø$(Ø$(Ø!%Ø,0Ø/3Ø&*ñ;
à˜EŸL™LÑ)ð;
ð ! §¡Ñ.ð;
ð & e§l¡lÑ3ð	;
ð
 ˜uŸ|™|Ñ,ð;
ð " %§,¡,Ñ/ð;
ð   §¡Ñ-ð;
ð ˜%Ÿ,™,Ñ'ð;
ð ˜UŸ\™\Ñ*ð;
ð ˜S‘Mð;
ð ˜S‘Mð;
ð ˜#‘ð;
ð $ D™>ð;
ð ' t™nð;
ð ˜d‘^ð;
ð" 
ˆu�U—\‘\Ñ"Ð$@Ð@Ñ	Aò#;
óó Hô;
rW   r^  )r#   r†  rC  rZ  r\  r]  r^  r°   r3  )ZrV  Ú
contextlibr   Útypingr   r   r   r   r   rz   Útorch.nn.functionalr	   r�   r  Útorch.utils.checkpointÚtorch.nnr
   r   r   Úactivationsr   Úconfiguration_utilsr   Úmodeling_attn_mask_utilsr   Úmodeling_outputsr   r   r   r   r   Úmodeling_utilsr   Úutilsr   r   r   r   r   Úutils.import_utilsr   Úgemma.modeling_gemmar   r   Úflash_attn.flash_attn_interfacer    Úflash_attn.layers.rotaryr!   Úflash_attn.ops.triton.rotaryr"   ÚobjectrÁ  rÂ  Ú
get_loggerre   ru  r#   r²   r~   r�   r–   ÚautogradÚFunctionr˜   rµ   r·   rU  rÉ   râ   rï   rà   r4  r  r  rv   r  r  r1  rõ   r6  ÚMODERNBERT_START_DOCSTRINGrC  rÀ  r†  rX  rZ  r\  r]  r^  Ú__all__r2   rW   rV   ú<module>r
     s  ðó  Ý "ß 8Õ 8ã ß Ð Û Ý ß AÑ Aå !Ý 3Ý B÷õ õ .÷õ õ 6ß Mñ ÔÝPÝ8Þ9à€Oà3Ð Ø$€à	ˆ×	Ñ	˜HÓ	%€ôAÐ'ô AðN ,0Ø%)ñ	&mØ�L‰Lð&mà—L‘Lð&mð ˜5Ÿ<™<Ñ(ð&mð �U—\‘\Ñ"ð	&mð
 ˆ5�<‰<˜Ÿ™ u§|¡|°S¸(À5Ç<Á<Ñ:PÐRZÐ[`×[gÑ[gÑRhÐhÑió&mðRØ�L‰Lðà�\‰\ðð ðð ð	ð
 ‡\�\óô>46˜%Ÿ.™.×1Ñ1ô 46ðv *.Ø $ñLð ˜Ÿ™Ñ&ð	Lð
 ˜‘óLô42Q¨ô 2Qôj˜2Ÿ9™9ô ô<:�B—I‘Iô :ô(_Ð 4ô _ð ).ñ"Ø!ð"à	�‰ð"ð —L‘Lð"ð Ÿ™ð	"ð
 ˜5×+Ñ+Ñ,ð"ð ˜3 ˜8‘_ð"ð 	ð"ð 
ð"ð   ‘~ð"ð ˆ5�—‘˜uŸ|™|Ð+Ñ,¨e°E·L±LÑ.AÐAÑBó"ð\ !&§¡ñ(!Ø!ð(!à	�‰ð(!ð 2ð(!ð —‘ð	(!ð
 ð(!ð ˜3 ˜8‘_ð(!ð 	ð(!ð 
ð(!ð —+‘+ð(!ð ˆ5�<‰<Ñó(!ðV Ø!ð à	�‰ð ð —L‘Lð ð Ÿ™ð	 ð
 ˜5×+Ñ+Ñ,ð ð ˜3 ˜8‘_ð ð 	ð ð 
ð ð ˆ5�<‰<Ñó ðH 1Ø$Ø"ñ!Ð ôM3˜"Ÿ)™)ô M3ô`+3˜RŸY™Yô +3ð\Ð ñ" Ø^ØóôB ó Bó	ðBðJ:Ð ñz Ø^Øóôk:Ð/ó k:ó	ðk:ô\	>˜rŸy™yô 	>ñ Ø`Øóô}
Ð5ó }
ó	ð}
ñ@ Ø\Øóôk
Ð*Có k
ó	ðk
ñ\ ØrØóôN
Ð'@ó N
ó	ðN
ñb ðð óôM
Ð%>ó M
óðM
ò`�rW   