Ë
    ÛÓ^j*  ã                   óP  — d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	 ddl
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mZmZ ddlmZ  ej8                  e«      Z G d„ de«      Z G d„ de«      Z  G d„ de«      Z! G d„ de«      Z" G d„ de«      Z# G d„ de«      Z$g d¢Z%y)zPyTorch BitNet model.é    )ÚCallableNé   )ÚCache)ÚFlashAttentionKwargs)ÚCausalLMOutputWithPast)ÚALL_ATTENTION_FUNCTIONS)ÚUnpack)Úloggingé   )ÚGemmaMLP)ÚLlamaAttentionÚLlamaDecoderLayerÚLlamaForCausalLMÚ
LlamaModelÚLlamaRMSNormÚapply_rotary_pos_embÚeager_attention_forwardé   )ÚBitNetConfigc                   ó   — e Zd Zy)ÚBitNetRMSNormN©Ú__name__Ú
__module__Ú__qualname__© ó    út/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/bitnet/modular_bitnet.pyr   r   )   ó   „ Ør   r   c                   ó*   ‡ — e Zd Zdefˆ fd„Zd„ Zˆ xZS )Ú	BitNetMLPÚconfigc                 óp   •— t         ‰| �  |«       t        |j                  |j                  ¬«      | _        y ©N)Úeps)ÚsuperÚ__init__r   Úintermediate_sizeÚrms_norm_epsÚffn_sub_norm)Úselfr"   Ú	__class__s     €r   r'   zBitNetMLP.__init__.   s+   ø€ Ü‰Ñ˜Ô Ü)¨&×*BÑ*BÈ×H[ÑH[Ô\ˆÕr   c           	      ó¦   — | j                  | j                  | j                  | j                  |«      «      | j	                  |«      z  «      «      }|S ©N)Ú	down_projr*   Úact_fnÚ	gate_projÚup_proj)r+   Úxr/   s      r   ÚforwardzBitNetMLP.forward2   sF   € Ø—N‘N 4×#4Ñ#4°T·[±[ÀÇÁÐPQÓARÓ5SÐVZ×VbÑVbÐcdÓVeÑ5eÓ#fÓgˆ	ØÐr   )r   r   r   r   r'   r4   Ú__classcell__©r,   s   @r   r!   r!   -   s   ø„ ð]˜|õ ]ör   r!   c                   óð   ‡ — e Zd Zdedefˆ fd„Z	 ddej                  deej                  ej                  f   dej                  dz  de	dz  d	e
e   d
eej                  ej                  dz  f   fd„Zˆ xZS )ÚBitNetAttentionr"   Ú	layer_idxc                 ór   •— t         ‰| �  ||«       t        |j                  |j                  ¬«      | _        y r$   )r&   r'   r   Úhidden_sizer)   Úattn_sub_norm)r+   r"   r9   r,   s      €r   r'   zBitNetAttention.__init__8   s-   ø€ Ü‰Ñ˜ Ô+Ü*¨6×+=Ñ+=À6×CVÑCVÔWˆÕr   NÚhidden_statesÚposition_embeddingsÚattention_maskÚpast_key_valuesÚkwargsÚreturnc                 ó,  — |j                   d d }g |¢d‘| j                  ‘­}| j                  |«      j                  |«      j	                  dd«      }| j                  |«      j                  |«      j	                  dd«      }	| j                  |«      j                  |«      j	                  dd«      }
|\  }}t        ||	||«      \  }}	|� |j                  |	|
| j                  «      \  }	}
t        j                  | j                  j                  t        «      } || ||	|
|f| j                  sdn| j                   | j"                  dœ|¤Ž\  }} |j$                  g |¢d‘­Ž j'                  «       }| j)                  |«      }| j+                  |«      }||fS )Néÿÿÿÿr   r   g        )ÚdropoutÚscaling)ÚshapeÚhead_dimÚq_projÚviewÚ	transposeÚk_projÚv_projr   Úupdater9   r   Úget_interfacer"   Ú_attn_implementationr   ÚtrainingÚattention_dropoutrF   ÚreshapeÚ
contiguousr<   Úo_proj)r+   r=   r>   r?   r@   rA   Úinput_shapeÚhidden_shapeÚquery_statesÚ
key_statesÚvalue_statesÚcosÚsinÚattention_interfaceÚattn_outputÚattn_weightss                   r   r4   zBitNetAttention.forward<   s¡  € ð $×)Ñ)¨#¨2Ð.ˆØ8˜Ð8 bÐ8¨$¯-©-Ñ8ˆà—{‘{ =Ó1×6Ñ6°|ÓD×NÑNÈqÐRSÓTˆØ—[‘[ Ó/×4Ñ4°\ÓB×LÑLÈQÐPQÓRˆ
Ø—{‘{ =Ó1×6Ñ6°|ÓD×NÑNÈqÐRSÓTˆà&‰ˆˆSÜ#7¸ÀjÐRUÐWZÓ#[Ñ ˆ�jàÐ&Ø'6×'=Ñ'=¸jÈ,ÐX\×XfÑXfÓ'gÑ$ˆJ˜ä(?×(MÑ(MØ�K‰K×,Ñ,Ô.Eó)
Ðñ %8ØØØØØð	%
ð  $Ÿ}š}‘C°$×2HÑ2HØ—L‘Lñ	%
ð ñ	%
Ñ!ˆ�\ð *�k×)Ñ)Ð;¨;Ð;¸Ò;×FÑFÓHˆØ×(Ñ(¨Ó5ˆØ—k‘k +Ó.ˆØ˜LÐ(Ð(r   r.   )r   r   r   r   Úintr'   ÚtorchÚTensorÚtupler   r	   r   r4   r5   r6   s   @r   r8   r8   7   s�   ø„ ðX˜|ð X¸õ Xð )-ñ')à—|‘|ð')ð # 5§<¡<°·±Ð#=Ñ>ð')ð Ÿ™ tÑ+ð	')ð
  ™ð')ð Ð-Ñ.ð')ð 
ˆu�|‰|˜UŸ\™\¨DÑ0Ð0Ñ	1÷')r   r8   c                   ó   — e Zd Zy)ÚBitNetDecoderLayerNr   r   r   r   re   re   f   r   r   re   c                   ó   — e Zd Zy)ÚBitNetModelNr   r   r   r   rg   rg   j   r   r   rg   c                   ó4   ‡ — e Zd ZddiZdZdZdefˆ fd„Zˆ xZS )ÚBitNetForCausalLMzlm_head.weightzmodel.embed_tokens.weightNrB   c                 ó"   •— t        ‰| �  di |¤ŽS )a$  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the masked language modeling loss. Indices should either be in `[0, transformers.,
            config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
            (masked), the loss is only computed for the tokens with labels in `[0, transformers., config.vocab_size]`.

        Example:

        ```python
        >>> from transformers import AutoTokenizer, BitNetForCausalLM

        >>> model = BitNetForCausalLM.from_pretrained("microsoft/bitnet-b1.58-2B-4T")
        >>> tokenizer = AutoTokenizer.from_pretrained("microsoft/bitnet-b1.58-2B-4T")

        >>> prompt = f'<|begin_of_text|>User: Hey, are you conscious? Can you talk to me?<|eot_id|>Assistant: '
        >>> inputs = tokenizer(prompt, return_tensors="pt")

        >>> # Generate
        >>> generate_ids = model.generate(inputs.input_ids, max_length=100)
        >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
        "User: Hey, are you conscious? Can you talk to me?Assistant: No, I'm not conscious. I'm an artificial intelligence designed to assist with information and tasks. How can I help you today?"
        ```r   )r&   r4   )r+   Úsuper_kwargsr,   s     €r   r4   zBitNetForCausalLM.forwards   s   ø€ ô4 ‰w‰Ñ. Ñ.Ð.r   )	r   r   r   Ú_tied_weights_keysÚ_tp_planÚ_pp_planr   r4   r5   r6   s   @r   ri   ri   n   s-   ø„ Ø*Ð,GÐHÐØ€HØ€Hð/ð 
 ÷/ñ /r   ri   )ri   rg   ÚBitNetPreTrainedModel)&Ú__doc__Úcollections.abcr   ra   Úcache_utilsr   Úmodeling_flash_attention_utilsr   Úmodeling_outputsr   Úmodeling_utilsr   Úprocessing_utilsr	   Úutilsr
   Úgemma.modeling_gemmar   Úllama.modeling_llamar   r   r   r   r   r   r   Úconfiguration_bitnetr   Ú
get_loggerr   Úloggerr   r!   r8   re   rg   ri   Ú__all__r   r   r   ú<module>r~      s    ðñ å $ã å  Ý BÝ 6Ý 5Ý &Ý Ý +÷÷ ñ õ /ð 
ˆ×	Ñ	˜HÓ	%€ô	�Lô 	ô�ô ô,)�nô ,)ô^	Ð*ô 	ô	�*ô 	ô/Ð(ô /òD�r   