Ë
    ÛÓ^jÞ  ã                   ó<  — 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
mZmZmZ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d„ Zdd„Ze G d„ de
«      «       Z G d„ de«      Z G d„ de«      Z G d„ de«      Zg d¢Zy)é    )ÚOptionalNé   )Úlogging)Úno_inherit_decoratoré   )ÚLlamaAttentionÚLlamaForCausalLMÚLlamaForSequenceClassificationÚLlamaForTokenClassificationÚLlamaRotaryEmbedding)ÚPhi3MLPé   )Ú	GlmConfigzTHUDM/glm-4-9bc                   ó   — e Zd Zy)ÚGlmMLPN©Ú__name__Ú
__module__Ú__qualname__© ó    ún/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/glm/modular_glm.pyr   r   &   ó   „ Ør   r   c                   óR   — e Zd Ze	 	 	 d	dedz  ded   dedz  dedef   fd„«       Z	y)
ÚGlmRotaryEmbeddingNÚconfigÚdeviceztorch.deviceÚseq_lenÚreturnztorch.Tensorc                 ón  — | j                   d   }| j                   j                  dd«      }t        | dd«      xs | j                  | j                  z  }t        ||z  «      }d}d|t        j                  d|dt        j                  ¬«      j                  |t        j                  ¬	«      |z  z  z  }||fS )
a¨  
        Computes the inverse frequencies according to the original RoPE implementation
        Args:
            config ([`~transformers.PreTrainedConfig`]):
                The model configuration.
            device (`torch.device`):
                The device to use for initialization of the inverse frequencies.
            seq_len (`int`, *optional*):
                The current sequence length. Unused for this type of RoPE.
        Returns:
            Tuple of (`torch.Tensor`, `float`), containing the inverse frequencies for the RoPE embeddings and the
            post-processing scaling factor applied to the computed cos/sin (unused in this type of RoPE).
        Ú
rope_thetaÚpartial_rotary_factorg      ð?Úhead_dimNr   r   )Údtype)r   r$   )Úrope_parametersÚgetÚgetattrÚhidden_sizeÚnum_attention_headsÚintÚtorchÚarangeÚint64ÚtoÚfloat)	r   r   r   Úbaser"   r#   ÚdimÚattention_factorÚinv_freqs	            r   Úcompute_default_rope_parametersz2GlmRotaryEmbedding.compute_default_rope_parameters+   s»   € ð& ×%Ñ% lÑ3ˆØ &× 6Ñ 6× :Ñ :Ð;RÐTWÓ XÐÜ˜6 :¨tÓ4Òh¸×8JÑ8JÈf×NhÑNhÑ8hˆÜ�(Ð2Ñ2Ó3ˆàÐð Ø”U—\‘\ ! S¨!´5·;±;Ô?×BÑBÈ&ÔX]×XcÑXcÐBÓdÐgjÑjÑkñ
ˆð Ð)Ð)Ð)r   )NNN)
r   r   r   Ústaticmethodr   r   r*   Útupler/   r4   r   r   r   r   r   *   sY   „ Øà#'Ø+/Ø"ñ*Ø˜DÑ ð*à˜Ñ(ð*ð �t‘ð*ð 
ˆ~˜uÐ$Ñ	%ò	*ó ñ*r   r   c                 ó|   — | dddd…f   }| dddd…f   }t        j                  | |fd¬«      j                  d«      S )	z*Rotates half the hidden dims of the input..r   Nr   r   éÿÿÿÿ©r1   éþÿÿÿ)r+   ÚstackÚflatten)ÚxÚx1Úx2s      r   Úrotate_halfr@   L   sJ   € à	
ˆ3���1�ˆ9‰€BØ	
ˆ3���1�ˆ9‰€BÜ�;‰;˜˜˜R�y bÔ)×1Ñ1°"Ó5Ð5r   c                 ó  — |j                  |«      }|j                  |«      }|dd|j                  d   dz  …f   j                  dd¬«      }|dd|j                  d   dz  …f   j                  dd¬«      }|j                  d   }| dd|…f   | d|d…f   }}|dd|…f   |d|d…f   }	}||z  t        |«      |z  z   }
||z  t        |«      |z  z   }t	        j
                  |
|gd¬«      }
t	        j
                  ||	gd¬«      }|
|fS )a…  Applies Rotary Position Embedding to the query and key tensors.

    Args:
        q (`torch.Tensor`): The query tensor.
        k (`torch.Tensor`): The key tensor.
        cos (`torch.Tensor`): The cosine part of the rotary embedding.
        sin (`torch.Tensor`): The sine part of the rotary embedding.
        unsqueeze_dim (`int`, *optional*, defaults to 1):
            The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
            sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
            that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
            k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
            cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
            the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
    Returns:
        `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
    .Nr8   r   r9   )Ú	unsqueezeÚshapeÚrepeat_interleaver@   r+   Úcat)ÚqÚkÚcosÚsinÚunsqueeze_dimÚ
rotary_dimÚq_rotÚq_passÚk_rotÚk_passÚq_embedÚk_embeds               r   Úapply_rotary_pos_embrR   S   sD  € ð$ �-‰-˜Ó
&€CØ
�-‰-˜Ó
&€Cð ˆcÐ'�S—Y‘Y˜r‘] aÑ'Ð'Ð'Ñ
(×
:Ñ
:¸1À"Ð
:Ó
E€CØ
ˆcÐ'�S—Y‘Y˜r‘] aÑ'Ð'Ð'Ñ
(×
:Ñ
:¸1À"Ð
:Ó
E€Cð —‘˜2‘€JØ�c˜;˜J˜;Ð&Ñ'¨¨3°
±Ð+;Ñ)<ˆ6€EØ�c˜;˜J˜;Ð&Ñ'¨¨3°
±Ð+;Ñ)<ˆ6€Eð �s‰{œ{¨5Ó1°CÑ7Ñ8€GØ�s‰{œ{¨5Ó1°CÑ7Ñ8€Gô �i‰i˜ &Ð)¨rÔ2€GÜ�i‰i˜ &Ð)¨rÔ2€GØ�GÐÐr   c                   ó0   ‡ — e Zd Zddededz  fˆ fd„Zˆ xZS )ÚGlmAttentionNr   Ú	layer_idxc                 ó¢   •— t         ‰| �  ||«       t        j                  |j                  | j
                  z  |j                  d¬«      | _        y )NF)Úbias)ÚsuperÚ__init__ÚnnÚLinearr)   r#   r(   Úo_proj)Úselfr   rU   Ú	__class__s      €r   rY   zGlmAttention.__init__}   s<   ø€ Ü‰Ñ˜ Ô+Ü—i‘i × :Ñ :¸T¿]¹]Ñ JÈF×L^ÑL^ÐejÔkˆ�r   )N)r   r   r   r   r*   rY   Ú__classcell__)r^   s   @r   rT   rT   {   s#   ø„ ñl˜yð l°S¸4±Z÷ lñ lr   rT   c                   ó   — e Zd Zy)ÚGlmForCausalLMNr   r   r   r   ra   ra   ‚   r   r   ra   c                   ó   — e Zd Zy)ÚGlmForSequenceClassificationNr   r   r   r   rc   rc   †   r   r   rc   c                   ó   — e Zd Zy)ÚGlmForTokenClassificationNr   r   r   r   re   re   Š   r   r   re   )ÚGlmPreTrainedModelÚGlmModelra   rc   re   )r   ) Útypingr   r+   Útorch.nnrZ   Úutilsr   Úutils.genericr   Úllama.modeling_llamar   r	   r
   r   r   Úphi3.modeling_phi3r   Úconfiguration_glmr   Ú
get_loggerr   ÚloggerÚ_CHECKPOINT_FOR_DOCr   r   r@   rR   rT   ra   rc   re   Ú__all__r   r   r   ú<module>rs      s³   ðõ ã Ý å Ý 1÷õ õ )Ý (ð 
ˆ×	Ñ	˜HÓ	%€à&Ð ô	ˆWô 	ô*Ð-ô *òD6ó%ðP ôl�>ó ló ðlô	Ð%ô 	ô	Ð#Aô 	ô	Ð ;ô 	ò�r   