§
    ‚Štj¢  ã                   ój  — 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mZ d	d
lmZmZmZmZ d	dlmZ ddlmZ ddlmZ  ej        e¦  «        Zd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 )é    Né   )ÚCache)ÚFlashAttentionKwargs)ÚGradientCheckpointingLayer)ÚCausalLMOutputWithPast)ÚUnpack)ÚTransformersKwargsÚloggingé   )ÚGlmAttentionÚGlmForCausalLMÚGlmForSequenceClassificationÚGlmForTokenClassification)ÚPhi3MLPé   )Ú
Glm4Config)ÚGlm4RMSNormzTHUDM/GLM-4-9B-0414c                   ó   — e Zd ZdS )ÚGlm4MLPN©Ú__name__Ú
__module__Ú__qualname__© ó    úc/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/glm4/modular_glm4.pyr   r   #   ó   € € € € € Ø€Dr   r   c                   ó  ‡ — e Zd Zdedefˆ fd„Z	 	 	 	 	 ddej        dej        dz  dej        dz  d	e	dz  d
e
dz  deej        ej        f         dz  dee         deej        eej        ej        f         dz  f         fd„Zˆ xZS )ÚGlm4DecoderLayerÚconfigÚ	layer_idxc                 ó´  •— t          ¦   «                              ¦   «          |j        | _        t          ||¬¦  «        | _        t          |¦  «        | _        t          |j        |j        ¬¦  «        | _	        t          |j        |j        ¬¦  «        | _
        t          |j        |j        ¬¦  «        | _        t          |j        |j        ¬¦  «        | _        d S )N)r    r!   )Úeps)ÚsuperÚ__init__Úhidden_sizeÚGlm4AttentionÚ	self_attnr   Úmlpr   Úrms_norm_epsÚinput_layernormÚpost_attention_layernormÚpost_self_attn_layernormÚpost_mlp_layernorm)Úselfr    r!   Ú	__class__s      €r   r%   zGlm4DecoderLayer.__init__(   s·   ø€ Ý‰Œ×ÒÑÔÐØ!Ô-ˆÔÝ&¨fÀ	ÐJÑJÔJˆŒå˜6‘?”?ˆŒÝ*¨6Ô+=À6ÔCVÐWÑWÔWˆÔÝ(3°FÔ4FÈFÔL_Ð(`Ñ(`Ô(`ˆÔ%Ý(3°FÔ4FÈFÔL_Ð(`Ñ(`Ô(`ˆÔ%Ý"-¨fÔ.@ÀfÔFYÐ"ZÑ"ZÔ"ZˆÔÐÐr   NFÚhidden_statesÚattention_maskÚposition_idsÚpast_key_valuesÚ	use_cacheÚposition_embeddingsÚkwargsÚreturnc           
      ó"  — |}|                       |¦  «        } | j        d||||||dœ|¤Ž\  }}	|                      |¦  «        }||z   }|}|                      |¦  «        }|                      |¦  «        }|                      |¦  «        }||z   }|S )N)r1   r2   r3   r4   r5   r6   r   )r+   r(   r-   r,   r)   r.   )
r/   r1   r2   r3   r4   r5   r6   r7   ÚresidualÚ_s
             r   ÚforwardzGlm4DecoderLayer.forward3   sÅ   € ð !ˆØ×,Ò,¨]Ñ;Ô;ˆà)˜4œ>ð 
Ø'Ø)Ø%Ø+ØØ 3ð
ð 
ð ð
ð 
Ñˆ�qð ×5Ò5°mÑDÔDˆØ  =Ñ0ˆà ˆØ×5Ò5°mÑDÔDˆØŸš Ñ/Ô/ˆØ×/Ò/°Ñ>Ô>ˆØ  =Ñ0ˆØÐr   )NNNFN)r   r   r   r   Úintr%   ÚtorchÚTensorÚ
LongTensorr   ÚboolÚtupler   r   ÚFloatTensorr<   Ú__classcell__©r0   s   @r   r   r   '   s   ø€ € € € € ð	[˜zð 	[°cð 	[ð 	[ð 	[ð 	[ð 	[ð 	[ð /3Ø04Ø(,Ø!&ØHLðð à”|ðð œ tÑ+ðð Ô&¨Ñ-ð	ð
  ™ðð ˜$‘;ðð # 5¤<°´Ð#=Ô>ÀÑEðð Ð-Ô.ðð 
ˆuÔ  %¨Ô(9¸5Ô;LÐ(LÔ"MÐPTÑ"TÐTÔ	Uðð ð ð ð ð ð ð r   r   c                   ó   — e Zd ZdS )r'   Nr   r   r   r   r'   r'   U   r   r   r'   c                   ó:   ‡ — e Zd Zdee         deez  fˆ fd„Zˆ xZS )ÚGlm4ForCausalLMÚsuper_kwargsr8   c                 ó6   •—  t          ¦   «         j        di |¤ŽS )ah  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
            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, ..., config.vocab_size]`.

        Example:

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

        >>> model = Glm4ForCausalLM.from_pretrained("THUDM/GLM-4-9B-0414")
        >>> tokenizer = AutoTokenizer.from_pretrained("THUDM/GLM-4-9B-0414")

        >>> prompt = "Hey, are you conscious? Can you talk to me?"
        >>> inputs = tokenizer(prompt, return_tensors="pt")

        >>> # Generate
        >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
        >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
        "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
        ```r   )r$   r<   )r/   rI   r0   s     €r   r<   zGlm4ForCausalLM.forwardZ   s!   ø€ ð4 �u‰wŒwŒÐ.Ð. Ð.Ð.Ð.r   )	r   r   r   r   r	   rB   r   r<   rD   rE   s   @r   rH   rH   Y   s[   ø€ € € € € ð/àÐ1Ô2ð/ð 
Ð'Ñ	'ð/ð /ð /ð /ð /ð /ð /ð /ð /ð /r   rH   c                   ó   — e Zd ZdS )ÚGlm4ForSequenceClassificationNr   r   r   r   rL   rL   w   r   r   rL   c                   ó   — e Zd ZdS )ÚGlm4ForTokenClassificationNr   r   r   r   rN   rN   {   r   r   rN   )ÚGlm4PreTrainedModelÚ	Glm4ModelrH   rL   rN   )$r>   Úcache_utilsr   Úmodeling_flash_attention_utilsr   Úmodeling_layersr   Úmodeling_outputsr   Úprocessing_utilsr   Úutilsr	   r
   Úglm.modeling_glmr   r   r   r   Úphi3.modeling_phi3r   Úconfiguration_glm4r   Úmodeling_glm4r   Ú
get_loggerr   ÚloggerÚ_CHECKPOINT_FOR_DOCr   r   r'   rH   rL   rN   Ú__all__r   r   r   ú<module>r_      s  ðð  €€€à  Ð  Ð  Ð  Ð  Ð  Ø BÐ BÐ BÐ BÐ BÐ BØ 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø &Ð &Ð &Ð &Ð &Ð &Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø tÐ tÐ tÐ tÐ tÐ tÐ tÐ tÐ tÐ tÐ tÐ tØ (Ð (Ð (Ð (Ð (Ð (Ø *Ð *Ð *Ð *Ð *Ð *Ø &Ð &Ð &Ð &Ð &Ð &ð 
ˆÔ	˜HÑ	%Ô	%€à+Ð ð	ð 	ð 	ð 	ð 	ˆgñ 	ô 	ð 	ð+ð +ð +ð +ð +Ð1ñ +ô +ð +ð\	ð 	ð 	ð 	ð 	�Lñ 	ô 	ð 	ð/ð /ð /ð /ð /�nñ /ô /ð /ð<	ð 	ð 	ð 	ð 	Ð$@ñ 	ô 	ð 	ð	ð 	ð 	ð 	ð 	Ð!:ñ 	ô 	ð 	ðð ð €€€r   