§
    ‚Štj*  ã                   ór  — 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j        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%dS )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dS )ÚBitNetRMSNormN©Ú__name__Ú
__module__Ú__qualname__© ó    úg/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/bitnet/modular_bitnet.pyr   r   )   ó   € € € € € Ø€Dr   r   c                   ó*   ‡ — e Zd Zdefˆ fd„Zd„ Zˆ xZS )Ú	BitNetMLPÚconfigc                 óŠ   •— t          ¦   «                              |¦  «         t          |j        |j        ¬¦  «        | _        d S ©N)Úeps)ÚsuperÚ__init__r   Úintermediate_sizeÚrms_norm_epsÚffn_sub_norm)Úselfr"   Ú	__class__s     €r   r'   zBitNetMLP.__init__.   s<   ø€ Ý‰Œ×Ò˜Ñ Ô Ð Ý)¨&Ô*BÈÔH[Ð\Ñ\Ô\ˆÔÐÐr   c           	      óÎ   — |                       |                      |                      |                      |¦  «        ¦  «        |                      |¦  «        z  ¦  «        ¦  «        }|S ©N)Ú	down_projr*   Úact_fnÚ	gate_projÚup_proj)r+   Úxr/   s      r   ÚforwardzBitNetMLP.forward2   sU   € Ø—N’N 4×#4Ò#4°T·[²[ÀÇÂÐPQÑARÔARÑ5SÔ5SÐVZ×VbÒVbÐcdÑVeÔVeÑ5eÑ#fÔ#fÑgÔgˆ	ØÐr   )r   r   r   r   r'   r4   Ú__classcell__©r,   s   @r   r!   r!   -   sZ   ø€ € € € € ð]˜|ð ]ð ]ð ]ð ]ð ]ð ]ðð ð ð ð ð ð 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                 óŒ   •— t          ¦   «                              ||¦  «         t          |j        |j        ¬¦  «        | _        d S r$   )r&   r'   r   Úhidden_sizer)   Úattn_sub_norm)r+   r"   r9   r,   s      €r   r'   zBitNetAttention.__init__8   s>   ø€ Ý‰Œ×Ò˜ Ñ+Ô+Ð+Ý*¨6Ô+=À6ÔCVÐWÑWÔWˆÔÐÐr   NÚhidden_statesÚposition_embeddingsÚattention_maskÚpast_key_valuesÚkwargsÚreturnc                 óL  — |j         d d…         }g |¢d‘| j        ‘R }|                      |¦  «                             |¦  «                             dd¦  «        }|                      |¦  «                             |¦  «                             dd¦  «        }	|                      |¦  «                             |¦  «                             dd¦  «        }
|\  }}t          ||	||¦  «        \  }}	|�|                     |	|
| j	        ¦  «        \  }	}
t          j        | j        j        t          ¦  «        } || ||	|
|f| j        sdn| j        | j        dœ|¤Ž\  }} |j        g |¢d‘R Ž                      ¦   «         }|                      |¦  «        }|                      |¦  «        }||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Ð8ˆà—{’{ =Ñ1Ô1×6Ò6°|ÑDÔD×NÒNÈqÐRSÑTÔTˆØ—[’[ Ñ/Ô/×4Ò4°\ÑBÔB×LÒLÈQÐPQÑRÔRˆ
Ø—{’{ =Ñ1Ô1×6Ò6°|ÑDÔD×NÒNÈqÐRSÑTÔTˆà&‰ˆˆSÝ#7¸ÀjÐRUÐWZÑ#[Ô#[Ñ ˆ�jàÐ&Ø'6×'=Ò'=¸jÈ,ÐX\ÔXfÑ'gÔ'gÑ$ˆJ˜å(?Ô(MØŒKÔ,Õ.Eñ)
ô )
Ðð %8Ð$7ØØØØØð	%
ð  $œ}ÐH�C�C°$Ô2HØ”Lð	%
ð 	%
ð ð	%
ð 	%
Ñ!ˆ�\ð *�kÔ)Ð;¨;Ð;¸Ð;Ð;Ð;×FÒFÑHÔHˆØ×(Ò(¨Ñ5Ô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ð Xð Xð Xð Xð Xð )-ð')ð ')à”|ð')ð # 5¤<°´Ð#=Ô>ð')ð œ tÑ+ð	')ð
  ™ð')ð Ð-Ô.ð')ð 
ˆuŒ|˜Uœ\¨DÑ0Ð0Ô	1ð')ð ')ð ')ð ')ð ')ð ')ð ')ð ')r   r8   c                   ó   — e Zd ZdS )ÚBitNetDecoderLayerNr   r   r   r   re   re   f   r   r   re   c                   ó   — e Zd ZdS )Ú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                 ó6   •—  t          ¦   «         j        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 �u‰wŒ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Ð BÐ BÐ BÐ BÐ BØ 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø &Ð &Ð &Ð &Ð &Ð &Ø Ð Ð Ð Ð Ð Ø +Ð +Ð +Ð +Ð +Ð +ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð /Ð .Ð .Ð .Ð .Ð .ð 
ˆÔ	˜HÑ	%Ô	%€ð	ð 	ð 	ð 	ð 	�Lñ 	ô 	ð 	ðð ð ð ð �ñ ô ð ð,)ð ,)ð ,)ð ,)ð ,)�nñ ,)ô ,)ð ,)ð^	ð 	ð 	ð 	ð 	Ð*ñ 	ô 	ð 	ð	ð 	ð 	ð 	ð 	�*ñ 	ô 	ð 	ð/ð /ð /ð /ð /Ð(ñ /ô /ð /ðDð ð €€€r   