a
    h                     @   s&  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eZ dZ!G dd deZ"G dd de	Z#G dd deZ$G dd deZ%G dd deZ&G d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                   @   s   e Zd ZdS )Glm4MLPN__name__
__module____qualname__ r   r   a/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/models/glm4/modular_glm4.pyr   &   s   r   c                       s   e Zd Zeed fddZedddddeje	ej e	ej
 e	e e	e e	ej
 e	eejejf  ee eeje	eejejf  f d
	ddZ  ZS )Glm4DecoderLayerconfig	layer_idxc                    sv   t    |j| _t||d| _t|| _t|j|jd| _	t|j|jd| _
t|j|jd| _t|j|jd| _d S )Nr   )eps)super__init__Zhidden_sizeGlm4Attention	self_attnr   mlpr   Zrms_norm_epsinput_layernormpost_attention_layernormpost_self_attn_layernormpost_mlp_layernorm)selfr   r    	__class__r   r   r#   +   s    

zGlm4DecoderLayer.__init__Zpast_key_valuepast_key_valuesz4.58)new_nameversionNF)	hidden_statesattention_maskposition_idsr.   	use_cachecache_positionposition_embeddingskwargsreturnc              
   K   sr   |}	|  |}| jf |||||||d|\}}
| |}|	| }|}	| |}| |}| |}|	| }|S )N)r1   r2   r3   r.   r4   r5   r6   )r'   r%   r)   r(   r&   r*   )r+   r1   r2   r3   r.   r4   r5   r6   r7   Zresidual_r   r   r   forward6   s*    





zGlm4DecoderLayer.forward)NNNFNN)r   r   r   r   intr#   r   torchZTensorr   Z
LongTensorr   booltupler	   r   ZFloatTensorr:   __classcell__r   r   r,   r   r   *   s&         r   c                   @   s   e Zd ZdS )r$   Nr   r   r   r   r   r$   [   s   r$   c                       s0   e Zd Zee eeef d fddZ  Z	S )Glm4ForCausalLM)super_kwargsr8   c                    s   t  jf 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+   rA   r,   r   r   r:   `   s    zGlm4ForCausalLM.forward)
r   r   r   r	   r
   r   r>   r   r:   r?   r   r   r,   r   r@   _   s   
r@   c                   @   s   e Zd ZdS )Glm4ForSequenceClassificationNr   r   r   r   r   rB   }   s   rB   c                   @   s   e Zd ZdS )Glm4ForTokenClassificationNr   r   r   r   r   rC      s   rC   )ZGlm4PreTrainedModelZ	Glm4Modelr@   rB   rC   ))typingr   r   r<   Zcache_utilsr   Zmodeling_flash_attention_utilsr   Zmodeling_layersr   Zmodeling_outputsr   Zprocessing_utilsr	   utilsr
   r   Zutils.deprecationr   Zglm.modeling_glmr   r   r   r   Zphi3.modeling_phi3r   Zconfiguration_glm4r   Zmodeling_glm4r   Z
get_loggerr   loggerZ_CHECKPOINT_FOR_DOCr   r   r$   r@   rB   rC   __all__r   r   r   r   <module>   s*   
1