
     `i                        d dl mZ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mZ dd
l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 )    )OptionalUnionN   )Cache)FlashAttentionKwargs)GradientCheckpointingLayer)CausalLMOutputWithPast)Unpack)TransformersKwargslogging)deprecate_kwarg   )GlmAttentionGlmForCausalLMGlmForSequenceClassificationGlmForTokenClassification)Phi3MLP   )
Glm4Config)Glm4RMSNormzTHUDM/GLM-4-9B-0414c                       e Zd ZdS )Glm4MLPN__name__
__module____qualname__     y/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/glm4/modular_glm4.pyr   r   &           Dr   r   c                   t    e Zd Zdedef fdZ eddd          	 	 	 	 	 	 dd
ej        de	ej                 de	ej
                 de	e         de	e         de	ej
                 de	eej        ej        f                  dee         deej        e	eej        ej        f                  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+=6CVWWW(3F4FFL_(`(`(`%(3F4FFL_(`(`(`%"-f.@fFY"Z"Z"Zr   past_key_valuepast_key_valuesz4.58)new_nameversionNFhidden_statesattention_maskposition_ids	use_cachecache_positionposition_embeddingskwargsreturnc                 $   |}	|                      |          } | j        d|||||||d|\  }}
|                     |          }|	|z   }|}	|                     |          }|                     |          }|                     |          }|	|z   }|S )N)r8   r9   r:   r5   r;   r<   r=   r   )r.   r+   r0   r/   r,   r1   )r2   r8   r9   r:   r5   r;   r<   r=   r>   residual_s              r   forwardzGlm4DecoderLayer.forward6   s     !,,];;)4> 	
')%+) 3	
 	
 	
 	
q 55mDD =0 55mDD////>> =0r   )NNNFNN)r   r   r   r   intr(   r   torchTensorr   
LongTensorr   booltupler
   r   FloatTensorrC   __classcell__r3   s   @r   r"   r"   *   sU       	[z 	[c 	[ 	[ 	[ 	[ 	[ 	[ _%0A6RRR 2637+/$)59KO! !|! !.! u/0	!
 "%! D>! !!12! &eEL%,,F&GH! -.! 
u (51BEDU1U+V"WW	X! ! ! SR! ! ! ! !r   r"   c                       e Zd ZdS )r*   Nr   r   r   r   r*   r*   [   r    r   r*   c                   D     e Zd Zdee         deeef         f fdZ xZ	S )Glm4ForCausalLMsuper_kwargsr?   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'   rC   )r2   rP   r3   s     r   rC   zGlm4ForCausalLM.forward`   s!    4 uww.....r   )
r   r   r   r
   r   r   rI   r	   rC   rK   rL   s   @r   rO   rO   _   s`        /12/ 
u,,	-/ / / / / / / / / /r   rO   c                       e Zd ZdS )Glm4ForSequenceClassificationNr   r   r   r   rS   rS   }   r    r   rS   c                       e Zd ZdS )Glm4ForTokenClassificationNr   r   r   r   rU   rU      r    r   rU   )Glm4PreTrainedModel	Glm4ModelrO   rS   rU   ))typingr   r   rE   cache_utilsr   modeling_flash_attention_utilsr   modeling_layersr   modeling_outputsr	   processing_utilsr
   utilsr   r   utils.deprecationr   glm.modeling_glmr   r   r   r   phi3.modeling_phi3r   configuration_glm4r   modeling_glm4r   
get_loggerr   logger_CHECKPOINT_FOR_DOCr   r"   r*   rO   rS   rU   __all__r   r   r   <module>rh      s5    # " " " " " " "              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 0 0 0 0 0 0 t t t t t t t t t t t t ( ( ( ( ( ( * * * * * * & & & & & & 
	H	%	%+ 	 	 	 	 	g 	 	 	. . . . .1 . . .b	 	 	 	 	L 	 	 	/ / / / /n / / /<	 	 	 	 	$@ 	 	 		 	 	 	 	!: 	 	 	  r   