
    %	&hsR                        d dl Z d dlmZmZ d dlZd dlZd dlmZ ddlmZm	Z	 ddl
mZ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mZ ddlmZ ddlmZ  ej<                  e      Z dZ!dZ" G d de      Z#d Z$ G d dejJ                        Z& G d de&      Z' G d de&      Z(e&e'e(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& d'e      Z0g d(Z1y))    N)OptionalTuple)nn   )CacheStaticCache)_flash_attention_forward!flash_attn_supports_top_left_mask)logging   )GemmaForCausalLM)LlamaDecoderLayerLlamaForQuestionAnsweringLlamaForSequenceClassificationLlamaForTokenClassification
LlamaModelLlamaPreTrainedModelapply_rotary_pos_emb	repeat_kv)
MistralMLP   )DiffLlamaConfigzkajuma/DiffLlama-0.3B-handcutr   c                       e Zd Zy)DiffLlamaMLPN__name__
__module____qualname__     /var/www/pru.catia.catastroantioquia-mas.com/valormas/lib/python3.12/site-packages/transformers/models/diffllama/modular_diffllama.pyr   r   1       r    r   c                 >    ddt        j                  d| z        z  z
  S )Ng?g333333?g333333ӿ)mathexp)	layer_idxs    r!   lambda_init_fnr'   5   s     txxy 01111r    c                   b    e Zd ZdZddedee   f fdZ	 	 	 	 	 	 ddej                  de
ej                  ej                  f   deej                     deej                     d	ee   d
ededeej                     de
ej                  eej                     ee
ej                        f   fdZ xZS )DiffLlamaAttentionz=Multi-headed attention from 'Attention Is All You Need' paperconfigr&   c                    t         |           || _        || _        |-t        j                  d| j                  j                   d       |j                  | _        |j                  | _	        |j                  | _        t        |d| j                  | j                  z        | _        |j                  | _        | j                  | j                  z  | _        |j                   | _        |j"                  | _        d| _        t'        j(                  | j                  | j                  | j                  z  |j*                        | _        t'        j(                  | j                  | j                  | j                  z  |j*                        | _        t'        j(                  | j                  | j                  | j                  z  |j*                        | _        t'        j(                  | j                  | j                  z  | j                  |j*                        | _        t5        |      | _        t'        j8                  t;        j<                  d|j>                  | j                  f            | _         t'        j8                  t;        j<                  d|j>                  | j                  f            | _!        t'        j8                  t;        j<                  d|j>                  | j                  f            | _"        t'        j8                  t;        j<                  d|j>                  | j                  f            | _#        t'        jH                  d| j                  z  |jJ                  d	
      | _&        y )NzInstantiating z without passing a `layer_idx` is not recommended and will lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` when creating this class.head_dimT)biasr   )sizer   F)epselementwise_affine)'super__init__r*   r&   loggerwarning_once	__class__r   attention_dropouthidden_sizenum_attention_heads	num_headsgetattrr,   num_key_value_headsnum_key_value_groupsmax_position_embeddings
rope_theta	is_causalr   Linearattention_biasq_projk_projv_projo_projr'   lambda_init	Parametertorchnormallambda_std_dev	lambda_q1	lambda_k1	lambda_q2	lambda_k2RMSNormrms_norm_eps	groupnormselfr*   r&   r5   s      r!   r2   zDiffLlamaAttention.__init__<   s~   " !8!8 9 :, , "(!9!9!--33
D4D4D4VW#)#=#= $(NNd6N6N$N!'-'E'E$ ++ii 0 0$..4==2PW]WlWlmii 0 0$2J2JT]]2Zagavavwii 0 0$2J2JT]]2Zagavavwii >@P@PW]WlWlm))4ell1f6K6KSWS`S`Rb&cdell1f6K6KSWS`S`Rb&cdell1f6K6KSWS`S`Rb&cdell1f6K6KSWS`S`Rb&cdA$56;N;Nchir    hidden_statesposition_embeddingsattention_maskposition_idspast_key_valueoutput_attentions	use_cachecache_positionreturnc	                 $   |j                         \  }
}}|}| j                  |      }| j                  |      }| j                  |      }|j	                  |
|| j
                  | j                        j                  dd      }|j	                  |
|| j                  | j                        j                  dd      }|j	                  |
|| j                  | j                        j                  dd      }|\  }}t        ||||      \  }}|'|||d}|j                  ||| j                  |      \  }}t        || j                        }t        || j                        }t        j                  t        j                   |dd      d      }|j#                  dddd      }t        j$                  ||j                  dd            t'        j(                  | j                        z  }|#|d d d d d d d |j*                  d   f   }||z   }t,        j.                  j1                  |dt        j2                        j5                  |j6                        }t,        j.                  j9                  || j:                  | j<                  	      }t        j>                  t        j@                  | jB                  | jD                  z  dt        j2                              j5                  |j6                        }t        j>                  t        j@                  | jF                  | jH                  z  dt        j2                              j5                  |j6                        }||z
  | jJ                  z   }t        j$                  ||      }t        j                   |dd      \  }}|||z  z
  }d| jJ                  z
  | jM                  |      z  }|j                  dd      jO                         }|jQ                  |
|d      }| jS                  |      }|sd }||fS )
Nr   r   sincosr[   dimr   rb   dtype)ptraining)*r.   rB   rC   rD   viewr9   r,   	transposer;   r   updater&   r   r<   rH   catchunkrepeatmatmulr$   sqrtshaper   
functionalsoftmaxfloat32torf   dropoutr6   rh   r%   sumrK   rL   rM   rN   rF   rQ   
contiguousreshaperE   )rS   rT   rU   rV   rW   rX   rY   rZ   r[   kwargsbsz
target_len_q_lenquery_states
key_statesvalue_statesr`   r_   cache_kwargsattn_weightscausal_masklambda_1lambda_2lambda_fullattn_outputattn_output1attn_output2s                               r!   forwardzDiffLlamaAttention.forward^   s    +//1Z{{=1[[/
{{=1#((eT^^T]]S]]^_abc__S%1I1I4==Yccdeghi
#((eT5M5Mt}}]gghiklm&S#7jRUWZ#[ j%#&snUL'5'<'<ZW[WeWegs't$Jz4+D+DE
 t/H/HIyy\1!!D"M#**1aA6||L*2F2Fq!2LMPTPYPYZ^ZgZgPhh%(Aq2HJ4D4DR4H2H)HIK'+5L }},,\r,WZZ[g[m[mn}},,\T=S=S^b^k^k,l99UYYt~~'FBV[VcVcdehh
 99UYYt~~'FBV[VcVcdehh
 )D,<,<<ll<>%*[[aQ%G"l"[<%??4+++t~~k/JJ!++Aq1<<>!))#ub9kk+. LL((r    NNNNFFN)r   r   r   __doc__r   r   intr2   rH   Tensorr   
LongTensorr   boolr   __classcell__r5   s   @r!   r)   r)   9   s    G j  j8C=  jL 2637*."'59B)||B) #5<<#=>B) !.	B)
 u//0B) !B)  B) B) !!1!12B) 
u||Xell3XeELL>Q5RR	SB)r    r)   c                   P    e Zd ZdZ fdZ	 	 	 	 	 	 ddej                  deej                  ej                  f   deej                     deej                     dee
   ded	ed
eej                     deej                  eej                     eeej                        f   fdZ xZS )DiffLlamaFlashAttention2aN  
    DiffLlama flash attention module. This module inherits from `DiffLlamaAttention` as the weights of the module stays
    untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
    flash attention and deal with padding tokens in case the input contains any of them.
    c                 B    t        |   |i | t               | _        y r   )r1   r2   r
   _flash_attn_uses_top_left_mask)rS   argsrz   r5   s      r!   r2   z!DiffLlamaFlashAttention2.__init__   s#    $)&)
 /P.Q+r    rT   rU   rV   rW   rX   rY   rZ   r[   r\   c	                 	   t        |t              rt        d      d}|j                         \  }	}
}| j	                  |      }| j                  |      }| j                  |      }|j                  |	|
| j                  | j                        j                  dd      }|j                  |	|
| j                  | j                        j                  dd      }|j                  |	|
| j                  | j                        j                  dd      }|+t        j                  d       | j                  ||      \  }}n|\  }}t        ||||      \  }}|'|||d}|j!                  ||| j"                  |      \  }}|j                  dd      }|j                  dd      }|j                  dd      }| j$                  r| j&                  nd}|j(                  }|t*        j,                  k(  rt+        j.                         rt+        j0                         }nMt3        | j4                  d      r| j4                  j6                  }n | j                  j8                  j(                  }t        j                  d	| d
       |j;                  |      }|j;                  |      }|j;                  |      }t+        j<                  |dd      \  }}|j?                  dddd      }|j?                  dddd      }tA        |||||
||tC        | dd       | jD                  | jF                  
      }tA        |||||
||tC        | dd       | jD                  | jF                  
      }t+        jH                  ||gd      }t+        j<                  |dd      \  }}t+        jJ                  t+        jL                  | jN                  | jP                  z  dt*        j,                              j;                  |j(                        }t+        jJ                  t+        jL                  | jR                  | jT                  z  dt*        j,                              j;                  |j(                        }||z
  | jV                  z   }|||z  z
  }d| jV                  z
  | jY                  |      z  }|j[                  |	|
d      j]                         }| j_                  |      }|sd }|fS )Nz`static` cache implementation is not compatible with `attn_implementation==flash_attention_2` make sure to use `sdpa` in the mean time, and open an issue at https://github.com/huggingface/transformersFr   r   aY  The attention layers in this model are transitioning from computing the RoPE embeddings internally through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed `position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be removed and `position_embeddings` will be mandatory.r^           _pre_quantization_dtypezThe input hidden states seems to be silently casted in float32, this might be related to the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in .ra   sliding_window)rW   rv   r   use_top_left_maskr?   rc   re   )0
isinstancer   
ValueErrorr.   rB   rC   rD   ri   r9   r,   rj   r;   r3   r4   
rotary_embr   rk   r&   rh   r6   rf   rH   rt   is_autocast_enabledget_autocast_gpu_dtypehasattrr*   r   weightru   rm   rn   r	   r:   r   r?   rl   r%   rw   rK   rL   rM   rN   rF   rQ   ry   rx   rE   )rS   rT   rU   rV   rW   rX   rY   rZ   r[   r{   r~   r}   r   r   r   r`   r_   r   dropout_rateinput_dtypetarget_dtypevalue_states1value_states2r   r   r   r   r   r   r   s                                 r!   r   z DiffLlamaFlashAttention2.forward   sh    nk2} 
 "%**,UA{{=1[[/
{{=1
 $((eT^^T]]S]]^_abc__S%1I1I4==Yccdeghi
#((eT5M5Mt}}]gghiklm&G |\BHC*HC#7jRUWZ#[ j%#&snUL'5'<'<ZW[WeWegs't$J $--a3))!Q/
#--a315t--C #((%--'((*$;;=&?@#{{BB#{{1177 >$ (??<8L#|4J'??<8L',{{<'J$}%,,Q1a8%,,Q1a8/% "4)94@"AAnn
 0% "4)94@"AAnn
 ii| <"E%*[[aQ%G"l99UYYt~~'FBV[VcVcdehh
 99UYYt~~'FBV[VcVcdehh
 )D,<,<<"[<%??4+++t~~k/JJ!))#ub9DDFkk+. LL((r    r   )r   r   r   r   r2   rH   r   r   r   r   r   r   r   r   r   s   @r!   r   r      s    R 6:37*."'59D)||D) #5<<#=>D) !!1!12	D)
 u//0D) !D)  D) D) !!1!12D) 
u||Xell3XeELL>Q5RR	SD)r    r   c                   J    e Zd ZdZ	 	 	 	 	 	 ddej
                  deej
                  ej
                  f   deej
                     deej                     dee	   de
de
d	eej                     d
eej
                  eej
                     eeej
                        f   f fdZ xZS )DiffLlamaSdpaAttentiona   
    DiffLlama attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
    `DiffLlamaAttention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to
    SDPA API.
    rT   rU   rV   rW   rX   rY   rZ   r[   r\   c	           
         |r,t         j                  d       t        |   ||||||||      S |j	                         \  }
}}| j                  |      }| j                  |      }| j                  |      }|j                  |
|| j                  | j                        j                  dd      }|j                  |
|| j                  | j                        j                  dd      }|j                  |
|| j                  | j                        j                  dd      }|\  }}t        ||||      \  }}|'|||d}|j                  ||| j                  |      \  }}t!        || j"                        }t!        || j"                        }t%        j&                  t%        j(                  |dd      d      }|j+                  dddd      }|}||d d d d d d d |j,                  d   f   }|j.                  j0                  d	k(  r2|0|j3                         }|j3                         }|j3                         }||dkD  rd
nd}t$        j4                  j6                  j9                  ||||| j:                  r| j<                  nd|      }t%        j(                  |dd      \  }}t%        j>                  t%        j@                  | jB                  | jD                  z  dt$        jF                              jI                  |jJ                        }t%        j>                  t%        j@                  | jL                  | jN                  z  dt$        jF                              jI                  |jJ                        }||z
  | jP                  z   }|||z  z
  }d| jP                  z
  | jS                  |      z  }|j                  dd      j3                         }|j                  |
|d      }| jU                  |      }|d fS )Na  DiffLlamaModel is using DiffLlamaSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.)rT   rV   rW   rX   rY   rZ   r[   rU   r   r   r^   ra   rc   rd   cudaTFr   )	attn_mask	dropout_pr?   re   )+r3   r4   r1   r   r.   rB   rC   rD   ri   r9   r,   rj   r;   r   rk   r&   r   r<   rH   rl   rm   rn   rq   devicetyperx   r   rr   scaled_dot_product_attentionrh   r6   r%   rw   rK   rL   rt   ru   rf   rM   rN   rF   rQ   rE   )rS   rT   rU   rV   rW   rX   rY   rZ   r[   rz   r{   r~   r}   r   r   r   r`   r_   r   r   r?   r   r   r   r   r   r   r5   s                              r!   r   zDiffLlamaSdpaAttention.forwardA  s    [ 7?+-)-"3#-$7 # 	 	 &**,UA{{=1[[/
{{=1#((eT^^T]]S]]^_abc__S%1I1I4==Yccdeghi
#((eT5M5Mt}}]gghiklm&S#7jRUWZ#[ j%#&snUL'5'<'<ZW[WeWegs't$Jz4+D+DE
 t/H/HIyy\1!!D"M#**1aA6$%%aA/E1A1A"1E/E&EFK ##v-+2I'224L#..0J'224L (/EAID5	hh))FF!04d,,3 G 
 &+[[aQ%G"l99UYYt~~'FBV[VcVcdehh
 99UYYt~~'FBV[VcVcdehh
 )D,<,<<"[<%??4+++t~~k/JJ!++Aq1<<>!&&sE26kk+.D  r    r   )r   r   r   r   rH   r   r   r   r   r   r   r   r   r   s   @r!   r   r   9  s     2637*."'59\!||\! #5<<#=>\! !.	\!
 u//0\! !\!  \! \! !!1!12\! 
u||Xell3XeELL>Q5RR	S\! \!r    r   )eagerflash_attention_2sdpac                   (     e Zd Zdedef fdZ xZS )DiffLlamaDecoderLayerr*   r&   c                 d    t         |   ||       t        |j                     ||      | _        y )N)r*   r&   )r1   r2   DIFFLLAMA_ATTENTION_CLASSES_attn_implementation	self_attnrR   s      r!   r2   zDiffLlamaDecoderLayer.__init__  s-    +4V5P5PQY_ktur    )r   r   r   r   r   r2   r   r   s   @r!   r   r     s    v v3 v vr    r   c                       e Zd ZdZdZy)DiffLlamaPreTrainedModelFN)r   r   r   _supports_flex_attn_supports_attention_backendr   r    r!   r   r     s    "'r    r   c                       e Zd Zy)DiffLlamaModelNr   r   r    r!   r   r     r"   r    r   c                       e Zd Zy)DiffLlamaForCausalLMNr   r   r    r!   r   r     r"   r    r   c                       e Zd Zy)"DiffLlamaForSequenceClassificationNr   r   r    r!   r   r     r"   r    r   c                       e Zd Zy)DiffLlamaForQuestionAnsweringNr   r   r    r!   r   r     r"   r    r   c                       e Zd Zy)DiffLlamaForTokenClassificationNr   r   r    r!   r   r     r"   r    r   )r   r   r   r   r   r   )2r$   typingr   r   rH   torch.utils.checkpointr   cache_utilsr   r   modeling_flash_attention_utilsr	   r
   utilsr   gemma.modeling_gemmar   llama.modeling_llamar   r   r   r   r   r   r   r   mistral.modeling_mistralr   configuration_diffllamar   
get_loggerr   r3   _CHECKPOINT_FOR_DOC_CONFIG_FOR_DOCr   r'   Moduler)   r   r   r   r   r   r   r   r   r   r   __all__r   r    r!   <module>r      s  $  "    - i  3	 	 	 2 4 
		H	%5 #	: 	2g) g)TS)1 S)ld!/ d!P  1" v- v(3 (
	Z 		+ 		)G 		$= 		&A 	r    